uvgRTP 2.3.0
An open-source library for RTP/SRTP media delivery
Loading...
Searching...
No Matches
media_stream.hh
1#pragma once
2
3#include "util.hh"
4
5#include <unordered_map>
6#include <memory>
7#include <string>
8#include <atomic>
9
10#ifndef _WIN32
11#include <sys/socket.h>
12#include <netinet/in.h>
13#else
14#include <ws2ipdef.h>
15#endif
16namespace uvgrtp {
17
18 // forward declarations
19 class rtp;
20 class rtcp;
21
22 class zrtp;
23 class base_srtp;
24 class srtp;
25 class srtcp;
26
27 class reception_flow;
28 class holepuncher;
29 class socket;
30
31 namespace frame {
32 struct rtp_frame;
33 }
34
35 namespace formats {
36 class media;
37 }
38
51 public:
53 media_stream(std::string cname, std::string remote_addr, std::string local_addr, uint16_t src_port, uint16_t dst_port,
54 rtp_format_t fmt, int rce_flags);
56
57 /* Initialize traditional RTP session
58 * Allocate Connection/Reader/Writer objects and initialize them
59 *
60 * Return RTP_OK on success
61 * Return RTP_MEMORY_ERROR if allocation failed
62 *
63 * Other error return codes are defined in {conn,writer,reader}.hh */
64 rtp_error_t init();
65
66 /* Initialize Secure RTP session
67 * Allocate Connection/Reader/Writer objects and initialize them
68 *
69 * Return RTP_OK on success
70 * Return RTP_MEMORY_ERROR if allocation failed
71 *
72 * TODO document all error codes!
73 *
74 * Other error return codes are defined in {conn,writer,reader,srtp}.hh */
75 rtp_error_t init(std::shared_ptr<uvgrtp::zrtp> zrtp);
77
94 rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt);
95
118 rtp_error_t push_frame(uint8_t *data, size_t data_len, int rtp_flags);
119
142 rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, int rtp_flags);
143
173 rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int rtp_flags);
174
207 rtp_error_t push_frame(uint8_t* data, size_t data_len, uint32_t ts, uint64_t ntp_ts, int rtp_flags);
208
238 rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, uint32_t ts, int rtp_flags);
239
272 rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, uint32_t ts, uint64_t ntp_ts, int rtp_flags);
273
283
295
314 rtp_error_t install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *));
315
325 rtp_error_t configure_ctx(int rcc_flag, ssize_t value);
326
328
329 /* Get unique key of the media stream
330 * Used by session to index media streams */
331 uint32_t get_key() const;
333
346
353 uint32_t get_ssrc() const;
354
355 private:
356 /* Initialize the connection by initializing the socket
357 * and binding ourselves to specified interface and creating
358 * an outgoing address */
359 rtp_error_t init_connection();
360
361 /* Create the media object for the stream */
362 rtp_error_t create_media(rtp_format_t fmt);
363
364 /* free all allocated resources */
365 rtp_error_t free_resources(rtp_error_t ret);
366
367 rtp_error_t init_srtp_with_zrtp(int rce_flags, int type, std::shared_ptr<uvgrtp::base_srtp> srtp,
368 std::shared_ptr<uvgrtp::zrtp> zrtp);
369
370 rtp_error_t start_components();
371
372 uint32_t get_default_bandwidth_kbps(rtp_format_t fmt);
373
374 bool check_pull_preconditions();
375 rtp_error_t check_push_preconditions(int rtp_flags, bool smart_pointer);
376
377 inline uint8_t* copy_frame(uint8_t* original, size_t data_len);
378
379 uint32_t key_;
380
381 std::shared_ptr<uvgrtp::srtp> srtp_;
382 std::shared_ptr<uvgrtp::srtcp> srtcp_;
383 std::shared_ptr<uvgrtp::socket> socket_;
384 std::shared_ptr<uvgrtp::rtp> rtp_;
385 std::shared_ptr<uvgrtp::rtcp> rtcp_;
386
387 sockaddr_in remote_sockaddr_;
388 sockaddr_in6 remote_sockaddr_ip6_;
389 std::string remote_address_;
390 std::string local_address_;
391 uint16_t src_port_;
392 uint16_t dst_port_;
393 bool ipv6_;
394 rtp_format_t fmt_;
395
396 /* Media context config */
397 int rce_flags_ = 0;
398
399 /* Has the media stream been initialized */
400 bool initialized_;
401
402 /* Primary handler keys for the RTP reception flow */
403 uint32_t rtp_handler_key_;
404 uint32_t zrtp_handler_key_;
405
406 /* RTP packet reception flow. Dispatches packets to other components */
407 std::unique_ptr<uvgrtp::reception_flow> reception_flow_;
408
409 /* Media object associated with this media stream. */
410 std::unique_ptr<uvgrtp::formats::media> media_;
411
412 /* Thread that keeps the holepunched connection open for unidirectional streams */
413 std::unique_ptr<uvgrtp::holepuncher> holepuncher_;
414
415 std::string cname_;
416
417 ssize_t fps_numerator_ = 30;
418 ssize_t fps_denominator_ = 1;
419 uint32_t bandwidth_ = 0;
420 std::shared_ptr<std::atomic<std::uint32_t>> ssrc_;
421 };
422}
423
424namespace uvg_rtp = uvgrtp;
The media_stream is an entity which represents one RTP stream.
Definition: media_stream.hh:50
rtp_error_t push_frame(std::unique_ptr< uint8_t[]> data, size_t data_len, int rtp_flags)
Send data to remote participant with a custom timestamp.
rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt)
Add keying information for user-managed SRTP session.
rtp_error_t push_frame(uint8_t *data, size_t data_len, int rtp_flags)
Send data to remote participant with a custom timestamp.
rtp_error_t configure_ctx(int rcc_flag, ssize_t value)
Configure the media stream, see RTP_CTX_CONFIGURATION_FLAGS for more details.
uvgrtp::frame::rtp_frame * pull_frame(size_t timeout_ms)
Poll a frame for a specified time from the media stream object.
rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, uint64_t ntp_ts, int rtp_flags)
Send data to remote participant with custom RTP and NTP timestamps.
rtp_error_t push_frame(std::unique_ptr< uint8_t[]> data, size_t data_len, uint32_t ts, uint64_t ntp_ts, int rtp_flags)
Send data to remote participant with custom RTP and NTP timestamps.
rtp_error_t install_receive_hook(void *arg, void(*hook)(void *, uvgrtp::frame::rtp_frame *))
Asynchronous way of getting frames.
uint32_t get_ssrc() const
Get SSRC identifier. You can use the SSRC value for example to find the report block belonging to thi...
uvgrtp::frame::rtp_frame * pull_frame()
Poll a frame indefinitely from the media stream object.
uvgrtp::rtcp * get_rtcp()
Get pointer to the RTCP object of the media stream.
rtp_error_t push_frame(std::unique_ptr< uint8_t[]> data, size_t data_len, uint32_t ts, int rtp_flags)
Send data to remote participant with a custom timestamp.
rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int rtp_flags)
Send data to remote participant with a custom timestamp.
RTCP instance handles all incoming and outgoing RTCP traffic, including report generation.
Definition: rtcp.hh:118
See RFC 3550 section 5
Definition: frame.hh:53