FFmpeg
Loading...
Searching...
No Matches
rtc.c File Reference
#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavutil/bprint.h"
#include "libavutil/crc.h"
#include "libavutil/hmac.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/random_seed.h"
#include "libavutil/time.h"
#include "avio_internal.h"
#include "http.h"
#include "internal.h"
#include "network.h"
#include "rtc.h"
#include "tls.h"

Go to the source code of this file.

Macros

#define RTC_ICE_DTLS_READ_MAX_RETRY   10
 If we try to read from UDP and get EAGAIN, we sleep for 5ms and retry up to 10 times.
 
#define RTC_ICE_DTLS_READ_SLEEP_DURATION   5
 
#define STUN_MAGIC_COOKIE   0x2112A442
 
#define STUN_HOST_CANDIDATE_PRIORITY   126 << 24 | 65535 << 8 | 255
 Refer to RFC 8445 5.1.2 priority = (2^24)*(type preference) + (2^8)*(local preference) + (2^0)*(256 - component ID) host candidate priority is 126 << 24 | 65535 << 8 | 255.
 
#define RTC_ICE_STUN_HEADER_SIZE   20
 The STUN message header, which is 20 bytes long, comprises the STUNMessageType (1B), MessageLength (2B), MagicCookie (4B), and TransactionID (12B).
 
#define RTC_RTCP_PT_START   192
 For RTCP, PT is [128, 223] (or without marker [0, 95]).
 
#define RTC_RTCP_PT_END   223
 
#define RTC_ICE_CONSENT_CHECK_INTERVAL   5000
 Refer to RFC 7675 5.1,.
 
#define RTC_ICE_CONSENT_EXPIRED_TIMER   30000
 

Enumerations

enum  STUNAttr {
  STUN_ATTR_USERNAME = 0x0006 , STUN_ATTR_PRIORITY = 0x0024 , STUN_ATTR_USE_CANDIDATE = 0x0025 , STUN_ATTR_MESSAGE_INTEGRITY = 0x0008 ,
  STUN_ATTR_FINGERPRINT = 0x8028 , STUN_ATTR_ICE_CONTROLLING = 0x802A
}
 

Functions

static av_cold int certificate_key_init (RTCContext *rtc)
 Get or Generate a self-signed certificate and private key for DTLS, fingerprint for SDP.
 
static av_cold int dtls_initialize (RTCContext *rtc)
 
av_cold 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.
 
static int ice_create_request (RTCContext *rtc, uint8_t *buf, int buf_size, int *request_size)
 Creates and marshals an ICE binding request packet.
 
static int ice_create_response (RTCContext *rtc, char *tid, int tid_size, uint8_t *buf, int buf_size, int *response_size)
 Create an ICE binding response.
 
static int ice_is_binding_request (uint8_t *b, int size)
 A Binding request has class=0b00 (request) and method=0b000000000001 (Binding) and is encoded into the first 16 bits as 0x0001.
 
static int ice_is_binding_response (const uint8_t *b, int size)
 A Binding response has class=0b10 (success response) and method=0b000000000001, and is encoded into the first 16 bits as 0x0101.
 
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)
 
static int ice_handle_binding_request (RTCContext *rtc, char *buf, int buf_size)
 This function handles incoming binding request messages by responding to them.
 
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)
 
av_cold void ff_rtc_deinit (RTCContext *rtc)
 

Macro Definition Documentation

◆ RTC_ICE_DTLS_READ_MAX_RETRY

#define RTC_ICE_DTLS_READ_MAX_RETRY   10

If we try to read from UDP and get EAGAIN, we sleep for 5ms and retry up to 10 times.

This will limit the total duration (in milliseconds, 50ms)

Definition at line 43 of file rtc.c.

Referenced by ff_rtc_ice_dtls_handshake().

◆ RTC_ICE_DTLS_READ_SLEEP_DURATION

#define RTC_ICE_DTLS_READ_SLEEP_DURATION   5

Definition at line 44 of file rtc.c.

Referenced by ff_rtc_ice_dtls_handshake().

◆ STUN_MAGIC_COOKIE

#define STUN_MAGIC_COOKIE   0x2112A442

Definition at line 47 of file rtc.c.

Referenced by ice_create_request(), and ice_create_response().

◆ STUN_HOST_CANDIDATE_PRIORITY

#define STUN_HOST_CANDIDATE_PRIORITY   126 << 24 | 65535 << 8 | 255

Refer to RFC 8445 5.1.2 priority = (2^24)*(type preference) + (2^8)*(local preference) + (2^0)*(256 - component ID) host candidate priority is 126 << 24 | 65535 << 8 | 255.

Definition at line 54 of file rtc.c.

Referenced by ice_create_request().

◆ RTC_ICE_STUN_HEADER_SIZE

#define RTC_ICE_STUN_HEADER_SIZE   20

The STUN message header, which is 20 bytes long, comprises the STUNMessageType (1B), MessageLength (2B), MagicCookie (4B), and TransactionID (12B).

See https://datatracker.ietf.org/doc/html/rfc5389#section-6

Definition at line 62 of file rtc.c.

Referenced by ice_handle_binding_request(), ice_is_binding_request(), and ice_is_binding_response().

◆ RTC_RTCP_PT_START

#define RTC_RTCP_PT_START   192

For RTCP, PT is [128, 223] (or without marker [0, 95]).

Literally, RTCP starts from 64 not 0, so PT is [192, 223] (or without marker [64, 95]), see "RTCP Control Packet Types (PT)" at https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-4

For RTP, the PT is [96, 127], or [224, 255] with marker. See "RTP Payload Types (PT) for standard audio and video encodings" at https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-1

Definition at line 74 of file rtc.c.

Referenced by ff_rtc_media_is_rtcp().

◆ RTC_RTCP_PT_END

#define RTC_RTCP_PT_END   223

Definition at line 75 of file rtc.c.

Referenced by ff_rtc_media_is_rtcp().

◆ RTC_ICE_CONSENT_CHECK_INTERVAL

#define RTC_ICE_CONSENT_CHECK_INTERVAL   5000

Refer to RFC 7675 5.1,.

To prevent expiry of consent, a STUN binding request can be sent periodically. Implementations SHOULD set a default interval of 5 seconds(5000ms).

Consent expires after 30 seconds(30000ms).

Definition at line 85 of file rtc.c.

Referenced by ff_rtc_ice_check_and_send_consent().

◆ RTC_ICE_CONSENT_EXPIRED_TIMER

#define RTC_ICE_CONSENT_EXPIRED_TIMER   30000

Definition at line 86 of file rtc.c.

Referenced by ff_rtc_ice_check_consent().

Enumeration Type Documentation

◆ STUNAttr

enum STUNAttr
Enumerator
STUN_ATTR_USERNAME 
STUN_ATTR_PRIORITY 

shared secret response/bind request

STUN_ATTR_USE_CANDIDATE 

must be included in a Binding request

STUN_ATTR_MESSAGE_INTEGRITY 

bind request

STUN_ATTR_FINGERPRINT 

bind request/response

STUN_ATTR_ICE_CONTROLLING 

rfc5389

Definition at line 89 of file rtc.c.

Function Documentation

◆ certificate_key_init()

static av_cold int certificate_key_init ( RTCContext * rtc)
static

Get or Generate a self-signed certificate and private key for DTLS, fingerprint for SDP.

Definition at line 102 of file rtc.c.

Referenced by ff_rtc_initialize().

◆ dtls_initialize()

static av_cold int dtls_initialize ( RTCContext * rtc)
static

Definition at line 129 of file rtc.c.

Referenced by ff_rtc_ice_dtls_handshake().

◆ ff_rtc_initialize()

av_cold 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().

◆ ice_create_request()

static int ice_create_request ( RTCContext * rtc,
uint8_t * buf,
int buf_size,
int * request_size )
static

Creates and marshals an ICE binding request packet.

This function creates and marshals an ICE binding request packet. The function only generates the username attribute and does not include goog-network-info, use-candidate. However, some of these attributes may be added in the future.

Parameters
rtcPointer to the RTCContext
bufPointer to memory buffer to store the request packet
buf_sizeSize of the memory buffer
request_sizePointer to an integer that receives the size of the request packet
Returns
Returns 0 if successful or AVERROR_xxx if an error occurs.

Definition at line 459 of file rtc.c.

Referenced by ff_rtc_ice_check_and_send_consent(), and ff_rtc_ice_dtls_handshake().

◆ ice_create_response()

static int ice_create_response ( RTCContext * rtc,
char * tid,
int tid_size,
uint8_t * buf,
int buf_size,
int * response_size )
static

Create an ICE binding response.

This function generates an ICE binding response and writes it to the provided buffer. The response is signed using the local password for message integrity.

Parameters
rtcPointer to the RTCContext structure.
tidPointer to the transaction ID of the binding request. The tid_size should be 12.
tid_sizeThe size of the transaction ID, should be 12.
bufPointer to the buffer where the response will be written.
buf_sizeThe size of the buffer provided for the response.
response_sizePointer to an integer that will store the size of the generated response.
Returns
Returns 0 if successful or AVERROR_xxx if an error occurs.

Definition at line 558 of file rtc.c.

Referenced by ice_handle_binding_request().

◆ ice_is_binding_request()

static int ice_is_binding_request ( uint8_t * b,
int size )
static

A Binding request has class=0b00 (request) and method=0b000000000001 (Binding) and is encoded into the first 16 bits as 0x0001.

See https://datatracker.ietf.org/doc/html/rfc5389#section-6

Definition at line 624 of file rtc.c.

Referenced by ff_rtc_ice_dtls_handshake(), and ice_handle_binding_request().

◆ ice_is_binding_response()

static int ice_is_binding_response ( const uint8_t * b,
int size )
static

A Binding response has class=0b10 (success response) and method=0b000000000001, and is encoded into the first 16 bits as 0x0101.

Definition at line 633 of file rtc.c.

Referenced by ff_rtc_ice_check_and_update_consent(), and ff_rtc_ice_dtls_handshake().

◆ 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().

◆ ice_handle_binding_request()

static int ice_handle_binding_request ( RTCContext * rtc,
char * buf,
int buf_size )
static

This function handles incoming binding request messages by responding to them.

If the message is not a binding request, it will be ignored.

Definition at line 660 of file rtc.c.

Referenced by ff_rtc_ice_dtls_handshake().

◆ 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()

av_cold void ff_rtc_deinit ( RTCContext * rtc)

Definition at line 1000 of file rtc.c.

Referenced by whip_deinit().