FFmpeg
Loading...
Searching...
No Matches
rtc.h File Reference
#include <stdint.h>
#include <stddef.h>
#include "libavutil/base64.h"
#include "libavutil/lfg.h"
#include "avformat.h"
#include "tls.h"
#include "url.h"

Go to the source code of this file.

Data Structures

struct  RTCContext
 

Macros

#define RTC_MAX_SDP_SIZE   8192
 Maximum size limit of a Session Description Protocol (SDP), be it an offer or answer.
 
#define RTC_DTLS_SRTP_KEY_LEN   16
 The size of the Secure Real-time Transport Protocol (SRTP) master key material that is exported by Secure Sockets Layer (SSL) after a successful Datagram Transport Layer Security (DTLS) handshake.
 
#define RTC_DTLS_SRTP_SALT_LEN   14
 
#define RTC_SRTP_KEY_SIZE   AV_BASE64_SIZE(RTC_DTLS_SRTP_KEY_LEN + RTC_DTLS_SRTP_SALT_LEN)
 
#define RTC_SRTP_SUITE   "SRTP_AES128_CM_HMAC_SHA1_80"
 The profile for OpenSSL's SRTP is SRTP_AES128_CM_SHA1_80, see ssl/d1_srtp.c.
 
#define RTC_DTLS_SRTP_CHECKSUM_LEN   16
 The maximum size of the Secure Real-time Transport Protocol (SRTP) HMAC checksum and padding that is appended to the end of the packet.
 
#define RTC_US_PER_MS   1000
 
#define RTC_MAX_UDP_BUFFER_SIZE   4096
 Maximum size of the buffer for sending and receiving UDP packets.
 
#define RTC_RTP_HEADER_SIZE   12
 The RTP header is 12 bytes long, comprising the Version(1B), PT(1B), SequenceNumber(2B), Timestamp(4B), and SSRC(4B).
 
#define RTC_ELAPSED(starttime, endtime)
 
#define RTC_OPTIONS(pstruct, field, flag)
 

Enumerations

enum  RTCState {
  RTC_STATE_NONE , RTC_STATE_INIT , RTC_STATE_OFFER , RTC_STATE_ANSWER ,
  RTC_STATE_NEGOTIATED , RTC_STATE_UDP_CONNECTED , RTC_STATE_ICE_CONNECTING , RTC_STATE_ICE_CONNECTED ,
  RTC_STATE_DTLS_FINISHED , RTC_STATE_SRTP_FINISHED , RTC_STATE_READY , RTC_STATE_FAILED
}
 
enum  RTCFlags { RTC_DTLS_ACTIVE = (1 << 0) }
 

Functions

int ff_rtc_initialize (RTCContext *rtc)
 
int ff_rtc_exchange_sdp (RTCContext *rtc)
 Exchange SDP offer with WebRTC peer to get the answer.
 
int ff_rtc_parse_answer (RTCContext *rtc)
 Parses the ICE ufrag, pwd, and candidates from the SDP answer.
 
int ff_rtc_media_is_rtp_rtcp (const uint8_t *b, int size)
 In RTP packets, the first byte is represented as 0b10xxxxxx, where the initial two bits (0b10) indicate the RTP version, see https://www.rfc-editor.org/rfc/rfc3550#section-5.1 The RTCP packet header is similar to RTP, see https://www.rfc-editor.org/rfc/rfc3550#section-6.4.1.
 
int ff_rtc_media_is_rtcp (const uint8_t *b, int size)
 
int ff_rtc_udp_connect (RTCContext *rtc)
 To establish a connection with the UDP server, we utilize ICE-LITE in a Client-Server mode.
 
int ff_rtc_ice_dtls_handshake (RTCContext *rtc)
 
int ff_rtc_srtp_export_keys (RTCContext *rtc)
 
int ff_rtc_dispose_session (RTCContext *rtc)
 RTC is connectionless, for it's based on UDP, so it check whether sesison is timeout.
 
int ff_rtc_ice_check_and_send_consent (RTCContext *rtc, int64_t now)
 Refer to RFC 7675 Periodically send Consent Freshness STUN Binding Request.
 
void ff_rtc_ice_check_and_update_consent (RTCContext *rtc, const uint8_t *buf, int size)
 
int ff_rtc_ice_check_consent (RTCContext *rtc, int64_t now)
 
void ff_rtc_deinit (RTCContext *rtc)
 

Macro Definition Documentation

◆ RTC_MAX_SDP_SIZE

#define RTC_MAX_SDP_SIZE   8192

Maximum size limit of a Session Description Protocol (SDP), be it an offer or answer.

Definition at line 39 of file rtc.h.

Referenced by ff_rtc_exchange_sdp(), and generate_sdp_offer().

◆ RTC_DTLS_SRTP_KEY_LEN

#define RTC_DTLS_SRTP_KEY_LEN   16

The size of the Secure Real-time Transport Protocol (SRTP) master key material that is exported by Secure Sockets Layer (SSL) after a successful Datagram Transport Layer Security (DTLS) handshake.

This material consists of a key of 16 bytes and a salt of 14 bytes.

Definition at line 47 of file rtc.h.

Referenced by ff_rtc_srtp_export_keys(), and setup_srtp().

◆ RTC_DTLS_SRTP_SALT_LEN

#define RTC_DTLS_SRTP_SALT_LEN   14

Definition at line 48 of file rtc.h.

Referenced by ff_rtc_srtp_export_keys(), and setup_srtp().

◆ RTC_SRTP_KEY_SIZE

#define RTC_SRTP_KEY_SIZE   AV_BASE64_SIZE(RTC_DTLS_SRTP_KEY_LEN + RTC_DTLS_SRTP_SALT_LEN)

Definition at line 50 of file rtc.h.

◆ RTC_SRTP_SUITE

#define RTC_SRTP_SUITE   "SRTP_AES128_CM_HMAC_SHA1_80"

The profile for OpenSSL's SRTP is SRTP_AES128_CM_SHA1_80, see ssl/d1_srtp.c.

The profile for FFmpeg's SRTP is SRTP_AES128_CM_HMAC_SHA1_80, see libavformat/srtp.c.

Definition at line 56 of file rtc.h.

Referenced by setup_srtp().

◆ RTC_DTLS_SRTP_CHECKSUM_LEN

#define RTC_DTLS_SRTP_CHECKSUM_LEN   16

The maximum size of the Secure Real-time Transport Protocol (SRTP) HMAC checksum and padding that is appended to the end of the packet.

To calculate the maximum size of the User Datagram Protocol (UDP) packet that can be sent out, subtract this size from the pkt_size.

Definition at line 64 of file rtc.h.

Referenced by create_rtp_muxer(), initialize(), and rtp_history_store().

◆ RTC_US_PER_MS

#define RTC_US_PER_MS   1000

◆ RTC_MAX_UDP_BUFFER_SIZE

#define RTC_MAX_UDP_BUFFER_SIZE   4096

Maximum size of the buffer for sending and receiving UDP packets.

Please note that this size does not limit the size of the UDP packet that can be sent. To set the limit for packet size, modify the pkt_size parameter. For instance, it is possible to set the UDP buffer to 4096 to send or receive packets, but please keep in mind that the pkt_size option limits the packet size to 1400.

Definition at line 75 of file rtc.h.

Referenced by create_rtp_muxer(), and handle_rtx_packet().

◆ RTC_RTP_HEADER_SIZE

#define RTC_RTP_HEADER_SIZE   12

The RTP header is 12 bytes long, comprising the Version(1B), PT(1B), SequenceNumber(2B), Timestamp(4B), and SSRC(4B).

See https://www.rfc-editor.org/rfc/rfc3550#section-5.1

Definition at line 82 of file rtc.h.

Referenced by ff_rtc_media_is_rtcp(), ff_rtc_media_is_rtp_rtcp(), and handle_rtx_packet().

◆ RTC_ELAPSED

#define RTC_ELAPSED ( starttime,
endtime )

◆ RTC_OPTIONS

#define RTC_OPTIONS ( pstruct,
field,
flag )
Value:
{ "handshake_timeout", "Timeout in milliseconds for ICE and DTLS handshake.", offsetof(pstruct, field.handshake_timeout), AV_OPT_TYPE_INT, { .i64 = 5000 }, -1, INT_MAX, flag }, \
{ "timeout", "Set timeout for socket I/O operations", offsetof(pstruct, field.timeout), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT_MAX, flag }, \
{ "pkt_size", "The maximum size, in bytes, of RTP packets that send out", offsetof(pstruct, field.pkt_size), AV_OPT_TYPE_INT, { .i64 = 1200 }, -1, INT_MAX, flag }, \
{ "ts_buffer_size", "The buffer size, in bytes, of underlying protocol", offsetof(pstruct, field.ts_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, flag }, \
{ "dtls_active", "Set dtls role as active", 0, AV_OPT_TYPE_CONST, { .i64 = RTC_DTLS_ACTIVE }, 0, UINT_MAX, flag, .unit = "flags" }, \
{ "authorization", "The optional Bearer token for HTTP Authorization", offsetof(pstruct, field.authorization), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, flag }, \
{ "cert_file", "The optional certificate file path for DTLS", offsetof(pstruct, field.cert_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, flag }, \
{ "key_file", "The optional private key file path for DTLS", offsetof(pstruct, field.key_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, flag }
#define flag(name)
Definition cbs_h264.c:60
#define NULL
Definition coverity.c:32
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ RTC_DTLS_ACTIVE
Definition rtc.h:118

Definition at line 245 of file rtc.h.

Enumeration Type Documentation

◆ RTCState

enum RTCState
Enumerator
RTC_STATE_NONE 
RTC_STATE_INIT 
RTC_STATE_OFFER 
RTC_STATE_ANSWER 
RTC_STATE_NEGOTIATED 

After parsing the answer received from the peer, the local peer negotiates the abilities in the offer that it generated.

RTC_STATE_UDP_CONNECTED 
RTC_STATE_ICE_CONNECTING 
RTC_STATE_ICE_CONNECTED 
RTC_STATE_DTLS_FINISHED 
RTC_STATE_SRTP_FINISHED 
RTC_STATE_READY 
RTC_STATE_FAILED 

Definition at line 87 of file rtc.h.

◆ RTCFlags

enum RTCFlags
Enumerator
RTC_DTLS_ACTIVE 

Definition at line 117 of file rtc.h.

Function Documentation

◆ ff_rtc_initialize()

int ff_rtc_initialize ( RTCContext * rtc)

Definition at line 165 of file rtc.c.

Referenced by initialize().

◆ ff_rtc_exchange_sdp()

int ff_rtc_exchange_sdp ( RTCContext * rtc)

Exchange SDP offer with WebRTC peer to get the answer.

Returns
0 if OK, AVERROR_xxx on error

Definition at line 203 of file rtc.c.

Referenced by whip_init().

◆ ff_rtc_parse_answer()

int ff_rtc_parse_answer ( RTCContext * rtc)

Parses the ICE ufrag, pwd, and candidates from the SDP answer.

This function is used to extract the ICE ufrag, pwd, and candidates from the SDP answer. It returns an error if any of these fields is NULL. The function only uses the first candidate if there are multiple candidates. However, support for multiple candidates will be added in the future.

Parameters
rtcPointer to the RTCContext
Returns
Returns 0 if successful or AVERROR_xxx if an error occurs.

Definition at line 330 of file rtc.c.

Referenced by whip_init().

◆ ff_rtc_media_is_rtp_rtcp()

int ff_rtc_media_is_rtp_rtcp ( const uint8_t * b,
int size )

In RTP packets, the first byte is represented as 0b10xxxxxx, where the initial two bits (0b10) indicate the RTP version, see https://www.rfc-editor.org/rfc/rfc3550#section-5.1 The RTCP packet header is similar to RTP, see https://www.rfc-editor.org/rfc/rfc3550#section-6.4.1.

Definition at line 645 of file rtc.c.

Referenced by on_rtp_write_packet().

◆ ff_rtc_media_is_rtcp()

int ff_rtc_media_is_rtcp ( const uint8_t * b,
int size )

Definition at line 651 of file rtc.c.

Referenced by on_rtp_write_packet(), and whip_write_packet().

◆ ff_rtc_udp_connect()

int ff_rtc_udp_connect ( RTCContext * rtc)

To establish a connection with the UDP server, we utilize ICE-LITE in a Client-Server mode.

In this setup, FFmpeg acts as the UDP client, while the peer functions as the UDP server.

Definition at line 700 of file rtc.c.

Referenced by whip_init().

◆ ff_rtc_ice_dtls_handshake()

int ff_rtc_ice_dtls_handshake ( RTCContext * rtc)

Definition at line 737 of file rtc.c.

Referenced by whip_init().

◆ ff_rtc_srtp_export_keys()

int ff_rtc_srtp_export_keys ( RTCContext * rtc)

This represents the material used to build the SRTP master key. It is generated by DTLS and has the following layout: 16B 16B 14B 14B client_key | server_key | client_salt | server_salt

Definition at line 846 of file rtc.c.

Referenced by setup_srtp().

◆ ff_rtc_dispose_session()

int ff_rtc_dispose_session ( RTCContext * rtc)

RTC is connectionless, for it's based on UDP, so it check whether sesison is timeout.

In such case, publishers can't republish the stream util the session is timeout. This function is called to notify the server that the stream is ended, server should expire and close the session immediately, so that publishers can republish the stream quickly.

Definition at line 899 of file rtc.c.

Referenced by whip_deinit().

◆ ff_rtc_ice_check_and_send_consent()

int ff_rtc_ice_check_and_send_consent ( RTCContext * rtc,
int64_t now )

Refer to RFC 7675 Periodically send Consent Freshness STUN Binding Request.

Definition at line 956 of file rtc.c.

Referenced by whip_write_packet().

◆ ff_rtc_ice_check_and_update_consent()

void ff_rtc_ice_check_and_update_consent ( RTCContext * rtc,
const uint8_t * buf,
int size )

Definition at line 980 of file rtc.c.

Referenced by whip_write_packet().

◆ ff_rtc_ice_check_consent()

int ff_rtc_ice_check_consent ( RTCContext * rtc,
int64_t now )

Definition at line 988 of file rtc.c.

Referenced by whip_write_packet().

◆ ff_rtc_deinit()

void ff_rtc_deinit ( RTCContext * rtc)

Definition at line 1000 of file rtc.c.

Referenced by whip_deinit().