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 
65  rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt);
66 
89  rtp_error_t push_frame(uint8_t *data, size_t data_len, int flags);
90 
113  rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, int flags);
114 
144  rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int flags);
145 
175  rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, uint32_t ts, int flags);
176 
185  uvgrtp::frame::rtp_frame *pull_frame();
186 
198  uvgrtp::frame::rtp_frame *pull_frame(size_t timeout);
199 
218  rtp_error_t install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *));
219 
221  /* If system call dispatcher is enabled and calling application has special requirements
222  * for the deallocation of a frame, it may install a deallocation hook which is called
223  * when SCD has processed the frame
224  *
225  * Return RTP_OK on success
226  * Return RTP_INVALID_VALUE if "hook" is nullptr */
227  rtp_error_t install_deallocation_hook(void (*hook)(void *));
228 
229  /* If needed, a notification hook can be installed to uvgRTP that can be used as
230  * an information side channel to the internal state of the library.
231  *
232  * When uvgRTP encouters a situation it doesn't know how to react to,
233  * it calls the notify hook with certain notify reason number (src/util.hh).
234  * Upon receiving a notification, application may ignore it or act on it somehow
235  *
236  * Currently only one notification type is supported and only receiver uses notifications
237  *
238  * "arg" is optional argument that is passed to hook when it is called. It may be nullptr
239  *
240  * Return RTP_OK on success
241  * Return RTP_INVALID_VALUE if "hook" is nullptr */
242  rtp_error_t install_notify_hook(void *arg, void (*hook)(void *, int));
244 
255  rtp_error_t configure_ctx(int flag, ssize_t value);
256 
258  /* Setter and getter for media-specific config that can be used f.ex with Opus */
259  void set_media_config(void *config);
260  void *get_media_config();
261 
262  /* Get unique key of the media stream
263  * Used by session to index media streams */
264  uint32_t get_key();
266 
280 
281  private:
282  /* Initialize the connection by initializing the socket
283  * and binding ourselves to specified interface and creating
284  * an outgoing address */
285  rtp_error_t init_connection();
286 
287  /* Create the media object for the stream */
288  rtp_error_t create_media(rtp_format_t fmt);
289 
290  /* free all allocated resources */
291  rtp_error_t free_resources(rtp_error_t ret);
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 
325  /* Media object associated with this media stream. */
326  uvgrtp::formats::media *media_;
327 
328  /* Thread that keeps the holepunched connection open for unidirectional streams */
329  uvgrtp::holepuncher *holepuncher_;
330  };
331 };
332 
333 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