vid_stream.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __PJMEDIA_VID_STREAM_H__
  19. #define __PJMEDIA_VID_STREAM_H__
  20. /**
  21. * @file vid_stream.h
  22. * @brief Video Stream.
  23. */
  24. #include <pjmedia/endpoint.h>
  25. #include <pjmedia/jbuf.h>
  26. #include <pjmedia/port.h>
  27. #include <pjmedia/rtcp.h>
  28. #include <pjmedia/rtcp_fb.h>
  29. #include <pjmedia/transport.h>
  30. #include <pjmedia/vid_codec.h>
  31. #include <pjmedia/stream_common.h>
  32. #include <pj/sock.h>
  33. PJ_BEGIN_DECL
  34. /**
  35. * @defgroup PJMED_VID_STRM Video streams
  36. * @ingroup PJMEDIA_PORT
  37. * @brief Video communication via the network
  38. * @{
  39. *
  40. * A video stream is a bidirectional video communication between two
  41. * endpoints. It corresponds to a video media description ("m=video" line)
  42. * in SDP session descriptor.
  43. *
  44. * A video stream consists of two unidirectional channels:
  45. * - encoding channel, which transmits unidirectional video to remote, and
  46. * - decoding channel, which receives unidirectional media from remote.
  47. *
  48. * A video stream exports two media port interface (see @ref PJMEDIA_PORT),
  49. * one for each direction, and application normally uses this interface to
  50. * interconnect the stream to other PJMEDIA components, e.g: the video
  51. * capture port supplies frames to the encoding port and video renderer
  52. * consumes frames from the decoding port.
  53. *
  54. * A video stream internally manages the following objects:
  55. * - an instance of video codec (see @ref PJMEDIA_VID_CODEC),
  56. * - an @ref PJMED_JBUF,
  57. * - two instances of RTP sessions (#pjmedia_rtp_session, one for each
  58. * direction),
  59. * - one instance of RTCP session (#pjmedia_rtcp_session),
  60. * - and a reference to video transport to send and receive packets
  61. * to/from the network (see @ref PJMEDIA_TRANSPORT).
  62. *
  63. * Video streams are created by calling #pjmedia_vid_stream_create(),
  64. * specifying #pjmedia_stream_info structure in the parameter. Application
  65. * can construct the #pjmedia_vid_stream_info structure manually, or use
  66. * #pjmedia_vid_stream_info_from_sdp() function to construct the
  67. * #pjmedia_vid_stream_info from local and remote SDP session descriptors.
  68. */
  69. /**
  70. * Enumeration of video stream sending rate control.
  71. */
  72. typedef enum pjmedia_vid_stream_rc_method
  73. {
  74. /**
  75. * No sending rate control. All outgoing RTP packets will be transmitted
  76. * immediately right after encoding process is done.
  77. */
  78. PJMEDIA_VID_STREAM_RC_NONE = 0,
  79. /**
  80. * Simple blocking. Each outgoing RTP packet transmission may be delayed
  81. * to avoid peak bandwidth that is much higher than specified. The thread
  82. * invoking the video stream put_frame(), e.g: video capture device thread,
  83. * will be blocked whenever transmission delay takes place.
  84. */
  85. PJMEDIA_VID_STREAM_RC_SIMPLE_BLOCKING = 1
  86. } pjmedia_vid_stream_rc_method;
  87. /**
  88. * Structure of configuration settings for video stream sending rate control.
  89. */
  90. typedef struct pjmedia_vid_stream_rc_config
  91. {
  92. /**
  93. * Rate control method.
  94. *
  95. * Default: PJMEDIA_VID_STREAM_RC_SIMPLE_BLOCKING.
  96. */
  97. pjmedia_vid_stream_rc_method method;
  98. /**
  99. * Upstream/outgoing bandwidth. If this is set to zero, the video stream
  100. * will use codec maximum bitrate setting.
  101. *
  102. * Default: 0 (follow codec maximum bitrate).
  103. */
  104. unsigned bandwidth;
  105. } pjmedia_vid_stream_rc_config;
  106. /**
  107. * Structure of configuration settings for video stream sending keyframe
  108. * after it is created.
  109. */
  110. typedef struct pjmedia_vid_stream_sk_config
  111. {
  112. /**
  113. * The number of keyframe to be sent after the stream is created.
  114. *
  115. * Default: PJMEDIA_VID_STREAM_START_KEYFRAME_CNT
  116. */
  117. unsigned count;
  118. /**
  119. * The keyframe sending interval after the stream is created.
  120. *
  121. * Default: PJMEDIA_VID_STREAM_START_KEYFRAME_INTERVAL_MSEC
  122. */
  123. unsigned interval;
  124. } pjmedia_vid_stream_sk_config;
  125. /**
  126. * This structure describes video stream information. Each video stream
  127. * corresponds to one "m=" line in SDP session descriptor, and it has
  128. * its own RTP/RTCP socket pair.
  129. */
  130. typedef struct pjmedia_vid_stream_info
  131. {
  132. pjmedia_type type; /**< Media type (audio, video) */
  133. pjmedia_tp_proto proto; /**< Transport protocol (RTP/AVP, etc.) */
  134. pjmedia_dir dir; /**< Media direction. */
  135. pj_sockaddr local_addr; /**< Local RTP address */
  136. pj_sockaddr rem_addr; /**< Remote RTP address */
  137. pj_sockaddr rem_rtcp; /**< Optional remote RTCP address. If
  138. sin_family is zero, the RTP address
  139. will be calculated from RTP. */
  140. pj_bool_t rtcp_mux; /**< Use RTP and RTCP multiplexing. */
  141. pjmedia_rtcp_fb_info loc_rtcp_fb; /**< Local RTCP-FB info. */
  142. pjmedia_rtcp_fb_info rem_rtcp_fb; /**< Remote RTCP-FB info. */
  143. unsigned tx_pt; /**< Outgoing codec paylaod type. */
  144. unsigned rx_pt; /**< Incoming codec paylaod type. */
  145. pj_uint32_t ssrc; /**< RTP SSRC. */
  146. pj_str_t cname; /**< RTCP CNAME. */
  147. pj_bool_t has_rem_ssrc;/**<Has remote RTP SSRC? */
  148. pj_uint32_t rem_ssrc; /**< Remote RTP SSRC. */
  149. pj_str_t rem_cname; /**< Remote RTCP CNAME. */
  150. pj_uint32_t rtp_ts; /**< Initial RTP timestamp. */
  151. pj_uint16_t rtp_seq; /**< Initial RTP sequence number. */
  152. pj_uint8_t rtp_seq_ts_set;
  153. /**< Bitmask flags if initial RTP sequence
  154. and/or timestamp for sender are set.
  155. bit 0/LSB : sequence flag
  156. bit 1 : timestamp flag */
  157. int jb_init; /**< Jitter buffer init delay in msec.
  158. (-1 for default). */
  159. int jb_min_pre; /**< Jitter buffer minimum prefetch
  160. delay in msec (-1 for default). */
  161. int jb_max_pre; /**< Jitter buffer maximum prefetch
  162. delay in msec (-1 for default). */
  163. int jb_max; /**< Jitter buffer max delay in msec. */
  164. #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
  165. pj_bool_t use_ka; /**< Stream keep-alive and NAT hole punch
  166. (see #PJMEDIA_STREAM_ENABLE_KA)
  167. is enabled? */
  168. pjmedia_stream_ka_config ka_cfg;
  169. /**< Stream send kep-alive settings. */
  170. #endif
  171. pjmedia_vid_codec_info codec_info; /**< Incoming codec format info. */
  172. pjmedia_vid_codec_param *codec_param; /**< Optional codec param. */
  173. pj_bool_t rtcp_sdes_bye_disabled;
  174. /**< Disable automatic sending of RTCP
  175. SDES and BYE. */
  176. pjmedia_vid_stream_rc_config rc_cfg;
  177. /**< Stream send rate control settings. */
  178. pjmedia_vid_stream_sk_config sk_cfg;
  179. /**< Stream send keyframe settings. */
  180. } pjmedia_vid_stream_info;
  181. /**
  182. * This function will initialize the video stream info based on information
  183. * in both SDP session descriptors for the specified stream index.
  184. * The remaining information will be taken from default codec parameters.
  185. * If socket info array is specified, the socket will be copied to the
  186. * session info as well.
  187. *
  188. * @param si Stream info structure to be initialized.
  189. * @param pool Pool to allocate memory.
  190. * @param endpt PJMEDIA endpoint instance.
  191. * @param local Local SDP session descriptor.
  192. * @param remote Remote SDP session descriptor.
  193. * @param stream_idx Media stream index in the session descriptor.
  194. *
  195. * @return PJ_SUCCESS if stream info is successfully initialized.
  196. */
  197. PJ_DECL(pj_status_t)
  198. pjmedia_vid_stream_info_from_sdp(pjmedia_vid_stream_info *si,
  199. pj_pool_t *pool,
  200. pjmedia_endpt *endpt,
  201. const pjmedia_sdp_session *local,
  202. const pjmedia_sdp_session *remote,
  203. unsigned stream_idx);
  204. /**
  205. * Initialize the video stream rate control with default settings.
  206. *
  207. * @param cfg Video stream rate control structure to be initialized.
  208. */
  209. PJ_DECL(void)
  210. pjmedia_vid_stream_rc_config_default(pjmedia_vid_stream_rc_config *cfg);
  211. /**
  212. * Initialize the video stream send keyframe with default settings.
  213. *
  214. * @param cfg Video stream send keyframe structure to be initialized.
  215. */
  216. PJ_DECL(void)
  217. pjmedia_vid_stream_sk_config_default(pjmedia_vid_stream_sk_config *cfg);
  218. /*
  219. * Opaque declaration for video stream.
  220. */
  221. typedef struct pjmedia_vid_stream pjmedia_vid_stream;
  222. /**
  223. * Create a video stream based on the specified parameter. After the video
  224. * stream has been created, application normally would want to get the media
  225. * port interface of the stream, by calling pjmedia_vid_stream_get_port().
  226. * The media port interface exports put_frame() and get_frame() function,
  227. * used to transmit and receive media frames from the stream.
  228. *
  229. * Without application calling put_frame() and get_frame(), there will be
  230. * no media frames transmitted or received by the stream.
  231. *
  232. * @param endpt Media endpoint.
  233. * @param pool Optional pool to allocate memory for the stream. If
  234. * this is not specified, one will be created internally.
  235. * A large number of memory may be needed because jitter
  236. * buffer needs to preallocate some storage.
  237. * @param info Stream information to create the stream. Upon return,
  238. * this info will be updated with the information from
  239. * the instantiated codec. Note that if the "pool"
  240. * argument is NULL, some fields in this "info" parameter
  241. * will be allocated from the internal pool of the
  242. * stream, which means that they will only remain valid
  243. * as long as the stream is not destroyed.
  244. * @param tp Media transport instance used to transmit and receive
  245. * RTP/RTCP packets to/from the underlying network.
  246. * @param user_data Arbitrary user data (for future callback feature).
  247. * @param p_stream Pointer to receive the video stream.
  248. *
  249. * @return PJ_SUCCESS on success.
  250. */
  251. PJ_DECL(pj_status_t) pjmedia_vid_stream_create(
  252. pjmedia_endpt *endpt,
  253. pj_pool_t *pool,
  254. pjmedia_vid_stream_info *info,
  255. pjmedia_transport *tp,
  256. void *user_data,
  257. pjmedia_vid_stream **p_stream);
  258. /**
  259. * Destroy the video stream.
  260. *
  261. * @param stream The video stream.
  262. *
  263. * @return PJ_SUCCESS on success.
  264. */
  265. PJ_DECL(pj_status_t) pjmedia_vid_stream_destroy(pjmedia_vid_stream *stream);
  266. /**
  267. * Get the media port interface of the stream. The media port interface
  268. * declares put_frame() and get_frame() function, which is the only
  269. * way for application to transmit and receive media frames from the
  270. * stream. As bidirectional video streaming may have different video
  271. * formats in the encoding and decoding direction, there are two media
  272. * ports exported by the video stream, one for each direction.
  273. *
  274. * @param stream The video stream.
  275. * @param dir The video direction.
  276. * @param p_port Pointer to receive the port interface.
  277. *
  278. * @return PJ_SUCCESS on success.
  279. */
  280. PJ_DECL(pj_status_t) pjmedia_vid_stream_get_port(
  281. pjmedia_vid_stream *stream,
  282. pjmedia_dir dir,
  283. pjmedia_port **p_port);
  284. /**
  285. * Get the media transport object associated with this stream.
  286. *
  287. * @param st The video stream.
  288. *
  289. * @return The transport object being used by the stream.
  290. */
  291. PJ_DECL(pjmedia_transport*) pjmedia_vid_stream_get_transport(
  292. pjmedia_vid_stream *st);
  293. /**
  294. * Get the stream statistics. See also #pjmedia_stream_get_stat_jbuf()
  295. *
  296. * @param stream The video stream.
  297. * @param stat Media stream statistics.
  298. *
  299. * @return PJ_SUCCESS on success.
  300. */
  301. PJ_DECL(pj_status_t) pjmedia_vid_stream_get_stat(
  302. const pjmedia_vid_stream *stream,
  303. pjmedia_rtcp_stat *stat);
  304. /**
  305. * Reset the video stream statistics.
  306. *
  307. * @param stream The video stream.
  308. *
  309. * @return PJ_SUCCESS on success.
  310. */
  311. PJ_DECL(pj_status_t) pjmedia_vid_stream_reset_stat(pjmedia_vid_stream *stream);
  312. /**
  313. * Get current jitter buffer state. See also #pjmedia_stream_get_stat()
  314. *
  315. * @param stream The video stream.
  316. * @param state Jitter buffer state.
  317. *
  318. * @return PJ_SUCCESS on success.
  319. */
  320. PJ_DECL(pj_status_t) pjmedia_vid_stream_get_stat_jbuf(
  321. const pjmedia_vid_stream *stream,
  322. pjmedia_jb_state *state);
  323. /**
  324. * Get the stream info.
  325. *
  326. * @param stream The video stream.
  327. * @param info Video stream info.
  328. *
  329. * @return PJ_SUCCESS on success.
  330. */
  331. PJ_DECL(pj_status_t) pjmedia_vid_stream_get_info(
  332. const pjmedia_vid_stream *stream,
  333. pjmedia_vid_stream_info *info);
  334. /**
  335. * Start the video stream. This will start the appropriate channels
  336. * in the video stream, depending on the video direction that was set
  337. * when the stream was created.
  338. *
  339. * @param stream The video stream.
  340. *
  341. * @return PJ_SUCCESS on success.
  342. */
  343. PJ_DECL(pj_status_t) pjmedia_vid_stream_start(pjmedia_vid_stream *stream);
  344. /**
  345. * Modify the video stream's codec parameter after the codec is opened.
  346. * Note that not all codec backends support modifying parameters during
  347. * runtime and only certain parameters can be changed.
  348. *
  349. * Currently, only Video Toolbox and OpenH264 backends support runtime
  350. * adjustment of encoding bitrate (avg_bps and max_bps).
  351. *
  352. * @param stream The video stream.
  353. * @param param The new codec parameter.
  354. *
  355. * @return PJ_SUCCESS on success.
  356. */
  357. PJ_DECL(pj_status_t)
  358. pjmedia_vid_stream_modify_codec_param(pjmedia_vid_stream *stream,
  359. const pjmedia_vid_codec_param *param);
  360. /**
  361. * Query if the stream is started on the specified direction.
  362. *
  363. * @param stream The video stream.
  364. * @param dir The direction to be checked.
  365. *
  366. * @return PJ_TRUE if stream is started.
  367. */
  368. PJ_DECL(pj_bool_t) pjmedia_vid_stream_is_running(pjmedia_vid_stream *stream,
  369. pjmedia_dir dir);
  370. /**
  371. * Pause stream channels.
  372. *
  373. * @param stream The video stream.
  374. * @param dir Which channel direction to pause.
  375. *
  376. * @return PJ_SUCCESS on success.
  377. */
  378. PJ_DECL(pj_status_t) pjmedia_vid_stream_pause(pjmedia_vid_stream *stream,
  379. pjmedia_dir dir);
  380. /**
  381. * Resume stream channels.
  382. *
  383. * @param stream The video stream.
  384. * @param dir Which channel direction to resume.
  385. *
  386. * @return PJ_SUCCESS on success;
  387. */
  388. PJ_DECL(pj_status_t) pjmedia_vid_stream_resume(pjmedia_vid_stream *stream,
  389. pjmedia_dir dir);
  390. /**
  391. * Force stream to send video keyframe on the next transmission.
  392. *
  393. * @param stream The video stream.
  394. *
  395. * @return PJ_SUCCESS on success;
  396. */
  397. PJ_DECL(pj_status_t) pjmedia_vid_stream_send_keyframe(
  398. pjmedia_vid_stream *stream);
  399. /**
  400. * Send RTCP SDES for the video stream.
  401. *
  402. * @param stream The video stream.
  403. *
  404. * @return PJ_SUCCESS on success.
  405. */
  406. PJ_DECL(pj_status_t) pjmedia_vid_stream_send_rtcp_sdes(
  407. pjmedia_vid_stream *stream);
  408. /**
  409. * Send RTCP BYE for the video stream.
  410. *
  411. * @param stream The video stream.
  412. *
  413. * @return PJ_SUCCESS on success.
  414. */
  415. PJ_DECL(pj_status_t) pjmedia_vid_stream_send_rtcp_bye(
  416. pjmedia_vid_stream *stream);
  417. /**
  418. * Send RTCP PLI for the video stream.
  419. *
  420. * @param stream The video stream.
  421. *
  422. * @return PJ_SUCCESS on success.
  423. */
  424. PJ_DECL(pj_status_t) pjmedia_vid_stream_send_rtcp_pli(
  425. pjmedia_vid_stream *stream);
  426. /**
  427. * Get the RTP session information of the video media stream. This function
  428. * can be useful for app with custom media transport to inject/filter some
  429. * outgoing/incoming proprietary packets into normal video RTP traffics.
  430. * This will return the original pointer to the internal states of the stream,
  431. * and generally it is not advisable for app to modify them.
  432. *
  433. * @param stream The video media stream.
  434. *
  435. * @param session_info The stream session info.
  436. *
  437. * @return PJ_SUCCESS on success.
  438. */
  439. PJ_DECL(pj_status_t)
  440. pjmedia_vid_stream_get_rtp_session_info(pjmedia_vid_stream *stream,
  441. pjmedia_stream_rtp_sess_info *session_info);
  442. /**
  443. * @}
  444. */
  445. PJ_END_DECL
  446. #endif /* __PJMEDIA_VID_STREAM_H__ */