uvgRTP 3.1.3
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#include <cstdint>
10
11#ifndef _WIN32
12#include <sys/socket.h>
13#include <netinet/in.h>
14#else
15#include <ws2ipdef.h>
16#endif
17namespace uvgrtp {
18
19 // forward declarations
20 class rtp;
21 class rtcp;
22
23 class zrtp;
24 class base_srtp;
25 class srtp;
26 class srtcp;
27
28 class reception_flow;
29 class holepuncher;
30 class socket;
31 class socketfactory;
32 class rtcp_reader;
33
34 namespace frame {
35 struct rtp_frame;
36 }
37
38 namespace formats {
39 class media;
40 }
41
54 public:
56 media_stream(std::string cname, std::string remote_addr, std::string local_addr, uint16_t src_port, uint16_t dst_port,
57 rtp_format_t fmt, std::shared_ptr<uvgrtp::socketfactory> sfp, int rce_flags);
59
60 /* Initialize traditional RTP session.
61 * Allocate Connection/Reader/Writer objects and initialize them
62 *
63 * Return RTP_OK on success
64 * Return RTP_MEMORY_ERROR if allocation failed
65 *
66 * Other error return codes are defined in {conn,writer,reader}.hh */
67 rtp_error_t init(std::shared_ptr<uvgrtp::zrtp> zrtp);
68
69 /* Initialize Secure RTP session with automatic ZRTP negotiation
70 * Allocate Connection/Reader/Writer objects and initialize them
71 *
72 * Return RTP_OK on success
73 * Return RTP_MEMORY_ERROR if allocation failed
74 *
75 * TODO document all error codes!
76 *
77 * Other error return codes are defined in {conn,writer,reader,srtp}.hh */
78 rtp_error_t init_auto_zrtp(std::shared_ptr<uvgrtp::zrtp> zrtp);
80
96 rtp_error_t start_zrtp();
97
114 rtp_error_t add_srtp_ctx(uint8_t *key, uint8_t *salt);
115
138 rtp_error_t push_frame(uint8_t *data, size_t data_len, int rtp_flags);
139
162 rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, int rtp_flags);
163
193 rtp_error_t push_frame(uint8_t *data, size_t data_len, uint32_t ts, int rtp_flags);
194
227 rtp_error_t push_frame(uint8_t* data, size_t data_len, uint32_t ts, uint64_t ntp_ts, int rtp_flags);
228
258 rtp_error_t push_frame(std::unique_ptr<uint8_t[]> data, size_t data_len, uint32_t ts, int rtp_flags);
259
292 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);
293
294 // Disabled for now
295 //rtp_error_t push_user_packet(uint8_t* data, uint32_t len);
296 //rtp_error_t install_user_receive_hook(void* arg, void (*hook)(void*, uint8_t* data, uint32_t len));
297
307
319
338 rtp_error_t install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *));
339
349 rtp_error_t configure_ctx(int rcc_flag, ssize_t value);
350
359 int get_configuration_value(int rcc_flag);
360
362
363 /* Get unique key of the media stream
364 * Used by session to index media streams */
365 uint32_t get_key() const;
366
368
381
388 uint32_t get_ssrc() const;
389
390 private:
391 /* Initialize the connection by initializing the socket
392 * and binding ourselves to specified interface and creating
393 * an outgoing address */
394 rtp_error_t init_connection();
395
396 /* Create the media object for the stream */
397 rtp_error_t create_media(rtp_format_t fmt);
398
399 /* free all allocated resources */
400 rtp_error_t free_resources(rtp_error_t ret);
401
402 rtp_error_t init_srtp_with_zrtp(int rce_flags, int type, std::shared_ptr<uvgrtp::base_srtp> srtp,
403 std::shared_ptr<uvgrtp::zrtp> zrtp);
404
405 rtp_error_t start_components();
406
407 rtp_error_t install_packet_handlers();
408
409 uint32_t get_default_bandwidth_kbps(rtp_format_t fmt);
410
411 bool check_pull_preconditions();
412 rtp_error_t check_push_preconditions(int rtp_flags, bool smart_pointer);
413
414 void holepuncher();
415
416 inline uint8_t* copy_frame(uint8_t* original, size_t data_len);
417
418 uint32_t key_;
419
420 std::shared_ptr<uvgrtp::srtp> srtp_;
421 std::shared_ptr<uvgrtp::srtcp> srtcp_;
422 std::shared_ptr<uvgrtp::socket> socket_;
423 std::shared_ptr<uvgrtp::rtp> rtp_;
424 std::shared_ptr<uvgrtp::rtcp> rtcp_;
425 std::shared_ptr<uvgrtp::zrtp> zrtp_;
426
427 std::shared_ptr<uvgrtp::socketfactory> sfp_;
428
429 sockaddr_in remote_sockaddr_;
430 sockaddr_in6 remote_sockaddr_ip6_;
431 std::string remote_address_;
432 std::string local_address_;
433 uint16_t src_port_;
434 uint16_t dst_port_;
435 bool ipv6_;
436 rtp_format_t fmt_;
437 bool new_socket_;
438
439 /* Media context config */
440 int rce_flags_ = 0;
441
442 /* Has the media stream been initialized */
443 bool initialized_;
444
445 /* RTP packet reception flow. Dispatches packets to other components */
446 std::shared_ptr<uvgrtp::reception_flow> reception_flow_;
447
448 /* Media object associated with this media stream. */
449 std::unique_ptr<uvgrtp::formats::media> media_;
450
451 /* Thread that keeps the holepunched connection open for unidirectional streams */
452 std::unique_ptr<uvgrtp::holepuncher> holepuncher_;
453
454 std::string cname_;
455
456 ssize_t fps_numerator_ = 30;
457 ssize_t fps_denominator_ = 1;
458 uint32_t bandwidth_ = 0;
459 std::shared_ptr<std::atomic<std::uint32_t>> ssrc_;
460 std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc_;
461
462 // Save values associated with context flags, to be returned with get_configuration_value
463 // Values are initialized to -2, which means value not set
464 int snd_buf_size_;
465 int rcv_buf_size_;
466 int multicast_ttl_;
467 };
468}
469
470namespace uvg_rtp = uvgrtp;
The media_stream is an entity which represents one RTP stream.
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...
rtp_error_t start_zrtp()
Start the ZRTP negotiation manually.
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.
int get_configuration_value(int rcc_flag)
Get the values associated with configuration flags, see RTP_CTX_CONFIGURATION_FLAGS for more details.
RTCP instance handles all incoming and outgoing RTCP traffic, including report generation.
Definition rtcp.hh:120
See RFC 3550 section 5
Definition frame.hh:68