uvgRTP
media_stream.hh
1 #pragma once
2 
3 #include <unordered_map>
4 #include <memory>
5 
6 #include "holepuncher.hh"
7 #include "pkt_dispatch.hh"
8 #include "rtcp.hh"
9 #include "socket.hh"
10 #include "srtp/srtcp.hh"
11 #include "srtp/srtp.hh"
12 #include "util.hh"
13 
14 #include "formats/media.hh"
15 
16 namespace uvgrtp {
17 
18  class media_stream {
19  public:
21  media_stream(std::string addr, int src_port, int dst_port, rtp_format_t fmt, int flags);
22  media_stream(std::string remote_addr, std::string local_addr, int src_port, int dst_port, rtp_format_t fmt, int flags);
23  ~media_stream();
24 
25  /* Initialize traditional RTP session
26  * Allocate Connection/Reader/Writer objects and initialize them
27  *
28  * Return RTP_OK on success
29  * Return RTP_MEMORY_ERROR if allocation failed
30  *
31  * Other error return codes are defined in {conn,writer,reader}.hh */
32  rtp_error_t init();
33 
34  /* Initialize Secure RTP session
35  * Allocate Connection/Reader/Writer objects and initialize them
36  *
37  * Return RTP_OK on success
38  * Return RTP_MEMORY_ERROR if allocation failed
39  *
40  * TODO document all error codes!
41  *
42  * Other error return codes are defined in {conn,writer,reader,srtp}.hh */
43  rtp_error_t init(uvgrtp::zrtp *zrtp);
45 
71  rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt);
72 
95  rtp_error_t push_frame(uint8_t *data, size_t data_len, int flags);
96 
119  rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, int flags);
120 
150  rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int flags);
151 
181  rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, uint32_t ts, int flags);
182 
191  uvgrtp::frame::rtp_frame *pull_frame();
192 
204  uvgrtp::frame::rtp_frame *pull_frame(size_t timeout);
205 
224  rtp_error_t install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *));
225 
227  /* If system call dispatcher is enabled and calling application has special requirements
228  * for the deallocation of a frame, it may install a deallocation hook which is called
229  * when SCD has processed the frame
230  *
231  * Return RTP_OK on success
232  * Return RTP_INVALID_VALUE if "hook" is nullptr */
233  rtp_error_t install_deallocation_hook(void (*hook)(void *));
234 
235  /* If needed, a notification hook can be installed to uvgRTP that can be used as
236  * an information side channel to the internal state of the library.
237  *
238  * When uvgRTP encouters a situation it doesn't know how to react to,
239  * it calls the notify hook with certain notify reason number (src/util.hh).
240  * Upon receiving a notification, application may ignore it or act on it somehow
241  *
242  * Currently only one notification type is supported and only receiver uses notifications
243  *
244  * "arg" is optional argument that is passed to hook when it is called. It may be nullptr
245  *
246  * Return RTP_OK on success
247  * Return RTP_INVALID_VALUE if "hook" is nullptr */
248  rtp_error_t install_notify_hook(void *arg, void (*hook)(void *, int));
250 
261  rtp_error_t configure_ctx(int flag, ssize_t value);
262 
264  /* Setter and getter for media-specific config that can be used f.ex with Opus */
265  void set_media_config(void *config);
266  void *get_media_config();
267 
268  /* Get unique key of the media stream
269  * Used by session to index media streams */
270  uint32_t get_key();
272 
286 
287  private:
288  /* Initialize the connection by initializing the socket
289  * and binding ourselves to specified interface and creating
290  * an outgoing address */
291  rtp_error_t init_connection();
292 
293  uint32_t key_;
294 
295  uvgrtp::srtp *srtp_;
296  uvgrtp::srtcp *srtcp_;
297  uvgrtp::socket *socket_;
298  uvgrtp::rtp *rtp_;
299  uvgrtp::rtcp *rtcp_;
300 
301  sockaddr_in addr_out_;
302  std::string addr_;
303  std::string laddr_;
304  int src_port_;
305  int dst_port_;
306  rtp_format_t fmt_;
307  int flags_;
308 
309  /* Media context config (SCD etc.) */
310  rtp_ctx_conf_t ctx_config_;
311 
312  /* Media config f.ex. for Opus */
313  void *media_config_;
314 
315  /* Has the media stream been initialized */
316  bool initialized_;
317 
318  /* Primary handler keys for the RTP packet dispatcher */
319  uint32_t rtp_handler_key_;
320  uint32_t zrtp_handler_key_;
321 
322  /* RTP packet dispatcher for the receiver */
323  uvgrtp::pkt_dispatcher *pkt_dispatcher_;
324  std::thread *dispatcher_thread_;
325 
326  /* Media object associated with this media stream. */
327  uvgrtp::formats::media *media_;
328 
329  /* Thread that keeps the holepunched connection open for unidirectional streams */
330  uvgrtp::holepuncher *holepuncher_;
331  };
332 };
333 
334 namespace uvg_rtp = uvgrtp;
Definition: media_stream.hh:18
rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt)
Add keying information for user-managed SRTP session.
uvgrtp::rtcp * get_rtcp()
Get pointer to the RTCP object of the media stream.
rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int flags)
Send data to remote participant with a custom timestamp.
uvgrtp::frame::rtp_frame * pull_frame(size_t timeout)
Poll a frame for a specified time from the media stream object.
rtp_error_t install_receive_hook(void *arg, void(*hook)(void *, uvgrtp::frame::rtp_frame *))
Asynchronous way of getting frames.
rtp_error_t push_frame(std::unique_ptr< uint8_t[]> data, size_t data_len, uint32_t ts, int flags)
Send data to remote participant with a custom timestamp.
rtp_error_t push_frame(uint8_t *data, size_t data_len, int flags)
Send data to remote participant with a custom timestamp.
rtp_error_t push_frame(std::unique_ptr< uint8_t[]> data, size_t data_len, int flags)
Send data to remote participant with a custom timestamp.
rtp_error_t configure_ctx(int flag, ssize_t value)
Configure the media stream, see RTP_CTX_CONFIGURATION_FLAGS for more details.
uvgrtp::frame::rtp_frame * pull_frame()
Poll a frame indefinitely from the media stream object.
Definition: rtcp.hh:74