stream.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_STREAM_H__
  20. #define __PJMEDIA_STREAM_H__
  21. /**
  22. * @file stream.h
  23. * @brief Media Stream.
  24. */
  25. #include <pjmedia/codec.h>
  26. #include <pjmedia/endpoint.h>
  27. #include <pjmedia/jbuf.h>
  28. #include <pjmedia/port.h>
  29. #include <pjmedia/rtcp.h>
  30. #include <pjmedia/rtcp_fb.h>
  31. #include <pjmedia/transport.h>
  32. #include <pjmedia/vid_codec.h>
  33. #include <pjmedia/stream_common.h>
  34. #include <pj/sock.h>
  35. PJ_BEGIN_DECL
  36. /**
  37. * @defgroup PJMED_STRM Streams
  38. * @ingroup PJMEDIA_PORT
  39. * @brief Communicating with remote peer via the network
  40. * @{
  41. *
  42. * A media stream is a bidirectional multimedia communication between two
  43. * endpoints. It corresponds to a media description (m= line) in SDP
  44. * session descriptor.
  45. *
  46. * A media stream consists of two unidirectional channels:
  47. * - encoding channel, which transmits unidirectional media to remote, and
  48. * - decoding channel, which receives unidirectional media from remote.
  49. *
  50. * A media stream exports media port interface (see @ref PJMEDIA_PORT)
  51. * and application normally uses this interface to interconnect the stream
  52. * to other PJMEDIA components.
  53. *
  54. * A media stream internally manages the following objects:
  55. * - an instance of media codec (see @ref PJMEDIA_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 media transport to send and receive packets
  61. * to/from the network (see @ref PJMEDIA_TRANSPORT).
  62. *
  63. * Streams are created by calling #pjmedia_stream_create(), specifying
  64. * #pjmedia_stream_info structure in the parameter. Application can construct
  65. * the #pjmedia_stream_info structure manually, or use
  66. * #pjmedia_stream_info_from_sdp() or #pjmedia_session_info_from_sdp()
  67. * functions to construct the #pjmedia_stream_info from local and remote
  68. * SDP session descriptors.
  69. *
  70. * Application can also use @ref PJMEDIA_SESSION to indirectly create the
  71. * streams.
  72. */
  73. /**
  74. * Opaque declaration for media channel.
  75. * Media channel is unidirectional flow of media from sender to
  76. * receiver.
  77. */
  78. typedef struct pjmedia_channel pjmedia_channel;
  79. /**
  80. * This structure describes media stream information. Each media stream
  81. * corresponds to one "m=" line in SDP session descriptor, and it has
  82. * its own RTP/RTCP socket pair.
  83. */
  84. typedef struct pjmedia_stream_info
  85. {
  86. pjmedia_type type; /**< Media type (audio, video) */
  87. pjmedia_tp_proto proto; /**< Transport protocol (RTP/AVP, etc.) */
  88. pjmedia_dir dir; /**< Media direction. */
  89. pj_sockaddr local_addr; /**< Local RTP address */
  90. pj_sockaddr rem_addr; /**< Remote RTP address */
  91. pj_sockaddr rem_rtcp; /**< Optional remote RTCP address. If
  92. sin_family is zero, the RTP address
  93. will be calculated from RTP. */
  94. pj_bool_t rtcp_mux; /**< Use RTP and RTCP multiplexing. */
  95. #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
  96. pj_bool_t rtcp_xr_enabled;
  97. /**< Specify whether RTCP XR is enabled.*/
  98. pj_uint32_t rtcp_xr_interval; /**< RTCP XR interval. */
  99. pj_sockaddr rtcp_xr_dest;/**<Additional remote RTCP XR address.
  100. This is useful for third-party (e.g:
  101. network monitor) to monitor the
  102. stream. If sin_family is zero,
  103. this will be ignored. */
  104. #endif
  105. pjmedia_rtcp_fb_info loc_rtcp_fb; /**< Local RTCP-FB info. */
  106. pjmedia_rtcp_fb_info rem_rtcp_fb; /**< Remote RTCP-FB info. */
  107. pjmedia_codec_info fmt; /**< Incoming codec format info. */
  108. pjmedia_codec_param *param; /**< Optional codec param. */
  109. unsigned tx_pt; /**< Outgoing codec paylaod type. */
  110. unsigned rx_pt; /**< Incoming codec paylaod type. */
  111. unsigned tx_maxptime;/**< Outgoing codec max ptime. */
  112. int tx_event_pt;/**< Outgoing pt for telephone-events. */
  113. int rx_event_pt;/**< Incoming pt for telephone-events. */
  114. pj_uint32_t ssrc; /**< RTP SSRC. */
  115. pj_str_t cname; /**< RTCP CNAME. */
  116. pj_bool_t has_rem_ssrc;/**<Has remote RTP SSRC? */
  117. pj_uint32_t rem_ssrc; /**< Remote RTP SSRC. */
  118. pj_str_t rem_cname; /**< Remote RTCP CNAME. */
  119. pj_uint32_t rtp_ts; /**< Initial RTP timestamp. */
  120. pj_uint16_t rtp_seq; /**< Initial RTP sequence number. */
  121. pj_uint8_t rtp_seq_ts_set;
  122. /**< Bitmask flags if initial RTP sequence
  123. and/or timestamp for sender are set.
  124. bit 0/LSB : sequence flag
  125. bit 1 : timestamp flag */
  126. int jb_init; /**< Jitter buffer init delay in msec.
  127. (-1 for default). */
  128. int jb_min_pre; /**< Jitter buffer minimum prefetch
  129. delay in msec (-1 for default). */
  130. int jb_max_pre; /**< Jitter buffer maximum prefetch
  131. delay in msec (-1 for default). */
  132. int jb_max; /**< Jitter buffer max delay in msec. */
  133. pjmedia_jb_discard_algo jb_discard_algo;
  134. /**< Jitter buffer discard algorithm. */
  135. #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
  136. pj_bool_t use_ka; /**< Stream keep-alive and NAT hole punch
  137. (see #PJMEDIA_STREAM_ENABLE_KA)
  138. is enabled? */
  139. pjmedia_stream_ka_config ka_cfg;
  140. /**< Stream send kep-alive settings. */
  141. #endif
  142. pj_bool_t rtcp_sdes_bye_disabled;
  143. /**< Disable automatic sending of RTCP
  144. SDES and BYE. */
  145. } pjmedia_stream_info;
  146. /**
  147. * This enumeration defines flags used by #pjmedia_stream_dtmf_event.
  148. */
  149. typedef enum pjmedia_stream_dtmf_event_flags {
  150. /**
  151. * The event was already indicated earlier. The new indication contains an
  152. * updated event duration.
  153. */
  154. PJMEDIA_STREAM_DTMF_IS_UPDATE = (1 << 0),
  155. /**
  156. * The event has ended and the indication contains the final event
  157. * duration.
  158. * Note that end indications might get lost. Hence it is not guaranteed
  159. * to receive an event with PJMEDIA_STREAM_DTMF_IS_END for every event.
  160. */
  161. PJMEDIA_STREAM_DTMF_IS_END = (1 << 1),
  162. } pjmedia_stream_dtmf_event_flags;
  163. /**
  164. * This structure describes DTMF telephony-events indicated through
  165. * #pjmedia_stream_set_dtmf_event_callback().
  166. */
  167. typedef struct pjmedia_stream_dtmf_event {
  168. int digit; /**< DTMF digit as ASCII character. */
  169. pj_uint32_t timestamp; /**< RTP timestamp of the event. */
  170. pj_uint16_t duration; /**< Event duration, in milliseconds. */
  171. pj_uint16_t flags; /**< Event flags (see
  172. #pjmedia_stream_dtmf_event_flags). */
  173. } pjmedia_stream_dtmf_event;
  174. /**
  175. * This function will initialize the stream info based on information
  176. * in both SDP session descriptors for the specified stream index.
  177. * The remaining information will be taken from default codec parameters.
  178. * If socket info array is specified, the socket will be copied to the
  179. * session info as well.
  180. *
  181. * @param si Stream info structure to be initialized.
  182. * @param pool Pool to allocate memory.
  183. * @param endpt PJMEDIA endpoint instance.
  184. * @param local Local SDP session descriptor.
  185. * @param remote Remote SDP session descriptor.
  186. * @param stream_idx Media stream index in the session descriptor.
  187. *
  188. * @return PJ_SUCCESS if stream info is successfully initialized.
  189. */
  190. PJ_DECL(pj_status_t)
  191. pjmedia_stream_info_from_sdp( pjmedia_stream_info *si,
  192. pj_pool_t *pool,
  193. pjmedia_endpt *endpt,
  194. const pjmedia_sdp_session *local,
  195. const pjmedia_sdp_session *remote,
  196. unsigned stream_idx);
  197. /**
  198. * Create a media stream based on the specified parameter. After the stream
  199. * has been created, application normally would want to get the media port
  200. * interface of the streams, by calling pjmedia_stream_get_port(). The
  201. * media port interface exports put_frame() and get_frame() function, used
  202. * to transmit and receive media frames from the stream.
  203. *
  204. * Without application calling put_frame() and get_frame(), there will be
  205. * no media frames transmitted or received by the stream.
  206. *
  207. * @param endpt Media endpoint.
  208. * @param pool Pool to allocate memory for the stream. A large
  209. * number of memory may be needed because jitter
  210. * buffer needs to preallocate some storage.
  211. * @param info Stream information.
  212. * @param tp Stream transport instance used to transmit
  213. * and receive RTP/RTCP packets to/from the underlying
  214. * transport.
  215. * @param user_data Arbitrary user data (for future callback feature).
  216. * @param p_stream Pointer to receive the media stream.
  217. *
  218. * @return PJ_SUCCESS on success.
  219. */
  220. PJ_DECL(pj_status_t) pjmedia_stream_create(pjmedia_endpt *endpt,
  221. pj_pool_t *pool,
  222. const pjmedia_stream_info *info,
  223. pjmedia_transport *tp,
  224. void *user_data,
  225. pjmedia_stream **p_stream);
  226. /**
  227. * Destroy the media stream.
  228. *
  229. * @param stream The media stream.
  230. *
  231. * @return PJ_SUCCESS on success.
  232. */
  233. PJ_DECL(pj_status_t) pjmedia_stream_destroy(pjmedia_stream *stream);
  234. /**
  235. * Get the last frame type retreived from the jitter buffer.
  236. *
  237. * @param stream The media stream.
  238. *
  239. * @return Jitter buffer frame type.
  240. */
  241. PJ_DEF(char) pjmedia_stream_get_last_jb_frame_type(pjmedia_stream *stream);
  242. /**
  243. * Get the media port interface of the stream. The media port interface
  244. * declares put_frame() and get_frame() function, which is the only
  245. * way for application to transmit and receive media frames from the
  246. * stream.
  247. *
  248. * @param stream The media stream.
  249. * @param p_port Pointer to receive the port interface.
  250. *
  251. * @return PJ_SUCCESS on success.
  252. */
  253. PJ_DECL(pj_status_t) pjmedia_stream_get_port(pjmedia_stream *stream,
  254. pjmedia_port **p_port );
  255. /**
  256. * Get the media transport object associated with this stream.
  257. *
  258. * @param st The media stream.
  259. *
  260. * @return The transport object being used by the stream.
  261. */
  262. PJ_DECL(pjmedia_transport*) pjmedia_stream_get_transport(pjmedia_stream *st);
  263. /**
  264. * Start the media stream. This will start the appropriate channels
  265. * in the media stream, depending on the media direction that was set
  266. * when the stream was created.
  267. *
  268. * @param stream The media stream.
  269. *
  270. * @return PJ_SUCCESS on success.
  271. */
  272. PJ_DECL(pj_status_t) pjmedia_stream_start(pjmedia_stream *stream);
  273. /**
  274. * Modify the stream's codec parameter after the codec is opened.
  275. * Note that not all codec parameters can be modified during run-time.
  276. * Currently, only Opus codec supports changing key codec parameters
  277. * such as bitrate and bandwidth, while other codecs may only be able to
  278. * modify minor settings such as VAD or PLC.
  279. *
  280. * @param stream The media stream.
  281. * @param param The new codec parameter.
  282. *
  283. * @return PJ_SUCCESS on success.
  284. */
  285. PJ_DECL(pj_status_t)
  286. pjmedia_stream_modify_codec_param(pjmedia_stream *stream,
  287. const pjmedia_codec_param *param);
  288. /**
  289. * Get the stream info.
  290. *
  291. * @param stream The media stream.
  292. * @param info Stream info.
  293. *
  294. * @return PJ_SUCCESS on success.
  295. */
  296. PJ_DECL(pj_status_t) pjmedia_stream_get_info( const pjmedia_stream *stream,
  297. pjmedia_stream_info *info);
  298. /**
  299. * Get the stream statistics. See also
  300. * #pjmedia_stream_get_stat_jbuf()
  301. *
  302. * @param stream The media stream.
  303. * @param stat Media stream statistics.
  304. *
  305. * @return PJ_SUCCESS on success.
  306. */
  307. PJ_DECL(pj_status_t) pjmedia_stream_get_stat( const pjmedia_stream *stream,
  308. pjmedia_rtcp_stat *stat);
  309. /**
  310. * Reset the stream statistics.
  311. *
  312. * @param stream The media stream.
  313. *
  314. * @return PJ_SUCCESS on success.
  315. */
  316. PJ_DECL(pj_status_t) pjmedia_stream_reset_stat(pjmedia_stream *stream);
  317. #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
  318. /**
  319. * Get the stream extended report statistics (RTCP XR).
  320. *
  321. * @param stream The media stream.
  322. * @param stat Media stream extended report statistics.
  323. *
  324. * @return PJ_SUCCESS on success.
  325. */
  326. PJ_DECL(pj_status_t) pjmedia_stream_get_stat_xr( const pjmedia_stream *stream,
  327. pjmedia_rtcp_xr_stat *stat);
  328. #endif
  329. /**
  330. * Get current jitter buffer state. See also
  331. * #pjmedia_stream_get_stat()
  332. *
  333. * @param stream The media stream.
  334. * @param state Jitter buffer state.
  335. *
  336. * @return PJ_SUCCESS on success.
  337. */
  338. PJ_DECL(pj_status_t) pjmedia_stream_get_stat_jbuf(const pjmedia_stream *stream,
  339. pjmedia_jb_state *state);
  340. /**
  341. * Pause the individual channel in the stream.
  342. *
  343. * @param stream The media channel.
  344. * @param dir Which direction to pause.
  345. *
  346. * @return PJ_SUCCESS on success.
  347. */
  348. PJ_DECL(pj_status_t) pjmedia_stream_pause( pjmedia_stream *stream,
  349. pjmedia_dir dir);
  350. /**
  351. * Resume the individual channel in the stream.
  352. *
  353. * @param stream The media channel.
  354. * @param dir Which direction to resume.
  355. *
  356. * @return PJ_SUCCESS on success;
  357. */
  358. PJ_DECL(pj_status_t) pjmedia_stream_resume(pjmedia_stream *stream,
  359. pjmedia_dir dir);
  360. /**
  361. * Transmit DTMF to this stream. The DTMF will be transmitted uisng
  362. * RTP telephone-events as described in RFC 2833. This operation is
  363. * only valid for audio stream.
  364. *
  365. * @param stream The media stream.
  366. * @param ascii_digit String containing digits to be sent to remote as
  367. * described on RFC 2833 section 3.10.
  368. * If PJMEDIA_HAS_DTMF_FLASH is enabled, character 'R' is
  369. * used to represent the event type 16 (flash) as stated
  370. * in RFC 4730.
  371. * Currently the maximum number of digits are 32.
  372. *
  373. * @return PJ_SUCCESS on success.
  374. */
  375. PJ_DECL(pj_status_t) pjmedia_stream_dial_dtmf(pjmedia_stream *stream,
  376. const pj_str_t *ascii_digit);
  377. /**
  378. * Check if the stream has incoming DTMF digits in the incoming DTMF
  379. * queue. Incoming DTMF digits received via RFC 2833 mechanism are
  380. * saved in the incoming digits queue.
  381. *
  382. * @param stream The media stream.
  383. *
  384. * @return Non-zero (PJ_TRUE) if the stream has received DTMF
  385. * digits in the .
  386. */
  387. PJ_DECL(pj_bool_t) pjmedia_stream_check_dtmf(pjmedia_stream *stream);
  388. /**
  389. * Retrieve the incoming DTMF digits from the stream, and remove the digits
  390. * from stream's DTMF buffer. Note that the digits buffer will not be NULL
  391. * terminated.
  392. *
  393. * @param stream The media stream.
  394. * @param ascii_digits Buffer to receive the digits. The length of this
  395. * buffer is indicated in the "size" argument.
  396. * @param size On input, contains the maximum digits to be copied
  397. * to the buffer.
  398. * On output, it contains the actual digits that has
  399. * been copied to the buffer.
  400. *
  401. * @return Non-zero (PJ_TRUE) if the stream has received DTMF
  402. * digits in the .
  403. */
  404. PJ_DECL(pj_status_t) pjmedia_stream_get_dtmf( pjmedia_stream *stream,
  405. char *ascii_digits,
  406. unsigned *size);
  407. /**
  408. * Set callback to be called upon receiving DTMF digits. If callback is
  409. * registered, the stream will not buffer incoming DTMF but rather call
  410. * the callback as soon as DTMF digit is received completely.
  411. * This callback will not be called if another callback is set via
  412. * #pjmedia_stream_set_dtmf_event_callback() as well.
  413. *
  414. * @param stream The media stream.
  415. * @param cb Callback to be called upon receiving DTMF digits.
  416. * The DTMF digits will be given to the callback as
  417. * ASCII digits.
  418. * @param user_data User data to be returned back when the callback
  419. * is called.
  420. *
  421. * @return PJ_SUCCESS on success.
  422. */
  423. PJ_DECL(pj_status_t)
  424. pjmedia_stream_set_dtmf_callback(pjmedia_stream *stream,
  425. void (*cb)(pjmedia_stream*,
  426. void *user_data,
  427. int digit),
  428. void *user_data);
  429. /**
  430. * Set callback to be called upon receiving DTMF digits. If callback is
  431. * registered, the stream will not buffer incoming DTMF but rather call
  432. * the callback as soon as DTMF digit is received.
  433. *
  434. * @param stream The media stream.
  435. * @param cb Callback to be called upon receiving DTMF digits.
  436. * See #pjmedia_stream_dtmf_event.
  437. * @param user_data User data to be returned back when the callback
  438. * is called.
  439. *
  440. * @return PJ_SUCCESS on success.
  441. */
  442. PJ_DECL(pj_status_t)
  443. pjmedia_stream_set_dtmf_event_callback(pjmedia_stream *stream,
  444. void (*cb)(pjmedia_stream*,
  445. void *user_data,
  446. const pjmedia_stream_dtmf_event *event),
  447. void *user_data);
  448. /**
  449. * Send RTCP SDES for the media stream.
  450. *
  451. * @param stream The media stream.
  452. *
  453. * @return PJ_SUCCESS on success.
  454. */
  455. PJ_DECL(pj_status_t)
  456. pjmedia_stream_send_rtcp_sdes( pjmedia_stream *stream );
  457. /**
  458. * Send RTCP BYE for the media stream.
  459. *
  460. * @param stream The media stream.
  461. *
  462. * @return PJ_SUCCESS on success.
  463. */
  464. PJ_DECL(pj_status_t)
  465. pjmedia_stream_send_rtcp_bye( pjmedia_stream *stream );
  466. /**
  467. * Get the RTP session information of the media stream. This function can be
  468. * useful for app with custom media transport to inject/filter some
  469. * outgoing/incoming proprietary packets into normal audio RTP traffics.
  470. * This will return the original pointer to the internal states of the stream,
  471. * and generally it is not advisable for app to modify them.
  472. *
  473. * @param stream The media stream.
  474. *
  475. * @param session_info The stream session info.
  476. *
  477. * @return PJ_SUCCESS on success.
  478. */
  479. PJ_DECL(pj_status_t)
  480. pjmedia_stream_get_rtp_session_info(pjmedia_stream *stream,
  481. pjmedia_stream_rtp_sess_info *session_info);
  482. /**
  483. * @}
  484. */
  485. PJ_END_DECL
  486. #endif /* __PJMEDIA_STREAM_H__ */