43#define RTC_ICE_DTLS_READ_MAX_RETRY 10
44#define RTC_ICE_DTLS_READ_SLEEP_DURATION 5
47#define STUN_MAGIC_COOKIE 0x2112A442
54#define STUN_HOST_CANDIDATE_PRIORITY 126 << 24 | 65535 << 8 | 255
62#define RTC_ICE_STUN_HEADER_SIZE 20
74#define RTC_RTCP_PT_START 192
75#define RTC_RTCP_PT_END 223
85#define RTC_ICE_CONSENT_CHECK_INTERVAL 5000
86#define RTC_ICE_CONSENT_EXPIRED_TIMER 30000
192 av_log(rtc->
ctx,
AV_LOG_VERBOSE,
"Init state=%d, handshake_timeout=%dms, pkt_size=%d, seed=%d, elapsed=%.2fms\n",
211 char *hex_data =
NULL;
219 proto_name, rtc->
ctx->
url);
230 ret = snprintf(buf,
sizeof(buf),
"Cache-Control: no-cache\r\nContent-Type: application/sdp\r\n");
245 const size_t sdp_offer_len = strlen(rtc->
sdp_offer);
246 hex_data =
av_malloc(2 * sdp_offer_len + 1);
366 const char *
space = strchr(ptr,
' ');
377 char foundation[33], protocol[17],
host[129];
378 int component_id, priority, port;
379 ret = sscanf(ptr,
"%32s %d %16s %d %128s %d typ host", foundation, &component_id, protocol, &priority,
host, &port);
388 av_log(rtc->
ctx,
AV_LOG_ERROR,
"Protocol %s is not supported by RTC, choose udp, line %d %s of %s\n",
429 "No remote DTLS fingerprint in SDP answer; refusing unauthenticated session\n");
437 av_log(rtc->
ctx,
AV_LOG_VERBOSE,
"SDP state=%d, offer=%zuB, answer=%zuB, ufrag=%s, pwd=%zuB, transport=%s://%s:%d, elapsed=%.2fms\n",
460 uint8_t *buf,
int buf_size,
518 buf[2] = (
size - 20) >> 8;
519 buf[3] = (
size - 20) & 0xFF;
529 buf[2] = (
size - 20) >> 8;
530 buf[3] = (
size - 20) & 0xFF;
559 char *tid,
int tid_size,
560 uint8_t *buf,
int buf_size,
567 if (tid_size != 12) {
593 buf[2] = (
size - 20) >> 8;
594 buf[3] = (
size - 20) & 0xFF;
604 buf[2] = (
size - 20) >> 8;
605 buf[3] = (
size - 20) & 0xFF;
611 *response_size =
size;
676 memcpy(tid, buf + 8, 12);
774 av_log(rtc->
ctx,
AV_LOG_ERROR,
"DTLS handshake timeout=%dms, cost=%.2fms, elapsed=%.2fms, state=%d\n",
819 av_log(rtc->
ctx,
AV_LOG_VERBOSE,
"ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%.2fms\n",
852 char *cp = is_dtls_active ? send_key : recv_key;
853 char *sp = is_dtls_active ? recv_key : send_key;
909 ret = snprintf(buf,
sizeof(buf),
"Cache-Control: no-cache\r\n");
992 "Consent Freshness expired after %.2fms (limited %dms), terminate session\n",
static AVDictionary * opts
static uint32_t crc32(const uint8_t *data, size_t size)
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
void avio_wb32(AVIOContext *s, unsigned int val)
void avio_wb16(AVIOContext *s, unsigned int val)
#define AVIO_FLAG_READ
read-only
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
#define AVIO_FLAG_WRITE
write-only
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
void avio_wb64(AVIOContext *s, uint64_t val)
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
void ffio_fill(AVIOContext *s, int b, int64_t count)
int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
Same as ff_get_line but strip the white-space characters in the text tail.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define i(width, name, range_min, range_max)
Public header for CRC hash function implementation.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
int av_random_bytes(uint8_t *buf, size_t len)
Generate cryptographically secure random data, i.e.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
Finish hashing and output the HMAC digest.
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
Hash data with the HMAC.
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
Initialize an AVHMAC context with an authentication key.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
static uint64_t request_size(URLContext *h)
const char * ff_http_get_new_location(URLContext *h)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Memory handling functions.
int ff_socket_nonblock(int socket, int enable)
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 t...
#define STUN_MAGIC_COOKIE
void ff_rtc_ice_check_and_update_consent(RTCContext *rtc, const uint8_t *buf, int size)
int ff_rtc_parse_answer(RTCContext *rtc)
Parses the ICE ufrag, pwd, and candidates from the SDP answer.
static int ice_handle_binding_request(RTCContext *rtc, char *buf, int buf_size)
This function handles incoming binding request messages by responding to them.
av_cold int ff_rtc_initialize(RTCContext *rtc)
int ff_rtc_media_is_rtcp(const uint8_t *b, int size)
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.
int ff_rtc_ice_dtls_handshake(RTCContext *rtc)
#define RTC_ICE_CONSENT_EXPIRED_TIMER
@ STUN_ATTR_PRIORITY
shared secret response/bind request
@ STUN_ATTR_MESSAGE_INTEGRITY
bind request
@ STUN_ATTR_ICE_CONTROLLING
rfc5389
@ STUN_ATTR_USE_CANDIDATE
must be included in a Binding request
@ STUN_ATTR_FINGERPRINT
bind request/response
#define RTC_RTCP_PT_START
For RTCP, PT is [128, 223] (or without marker [0, 95]).
int ff_rtc_ice_check_consent(RTCContext *rtc, int64_t now)
static av_cold int dtls_initialize(RTCContext *rtc)
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 th...
int ff_rtc_srtp_export_keys(RTCContext *rtc)
static av_cold int certificate_key_init(RTCContext *rtc)
Get or Generate a self-signed certificate and private key for DTLS, fingerprint for SDP.
#define STUN_HOST_CANDIDATE_PRIORITY
Refer to RFC 8445 5.1.2 priority = (2^24)*(type preference) + (2^8)*(local preference) + (2^0)*(256 -...
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_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) indica...
#define RTC_ICE_STUN_HEADER_SIZE
The STUN message header, which is 20 bytes long, comprises the STUNMessageType (1B),...
static int ice_create_request(RTCContext *rtc, uint8_t *buf, int buf_size, int *request_size)
Creates and marshals an ICE binding request packet.
#define RTC_ICE_CONSENT_CHECK_INTERVAL
Refer to RFC 7675 5.1,.
av_cold void ff_rtc_deinit(RTCContext *rtc)
#define RTC_ICE_DTLS_READ_SLEEP_DURATION
#define RTC_ICE_DTLS_READ_MAX_RETRY
If we try to read from UDP and get EAGAIN, we sleep for 5ms and retry up to 10 times.
int ff_rtc_exchange_sdp(RTCContext *rtc)
Exchange SDP offer with WebRTC peer to get the answer.
int ff_rtc_ice_check_and_send_consent(RTCContext *rtc, int64_t now)
Refer to RFC 7675 Periodically send Consent Freshness STUN Binding Request.
int ff_rtc_udp_connect(RTCContext *rtc)
To establish a connection with the UDP server, we utilize ICE-LITE in a Client-Server mode.
#define RTC_MAX_SDP_SIZE
Maximum size limit of a Session Description Protocol (SDP), be it an offer or answer.
#define RTC_DTLS_SRTP_SALT_LEN
#define RTC_RTP_HEADER_SIZE
The RTP header is 12 bytes long, comprising the Version(1B), PT(1B), SequenceNumber(2B),...
#define RTC_DTLS_SRTP_KEY_LEN
The size of the Secure Real-time Transport Protocol (SRTP) master key material that is exported by Se...
#define RTC_ELAPSED(starttime, endtime)
@ RTC_STATE_ICE_CONNECTED
@ RTC_STATE_DTLS_FINISHED
@ RTC_STATE_UDP_CONNECTED
@ RTC_STATE_ICE_CONNECTING
@ RTC_STATE_NEGOTIATED
After parsing the answer received from the peer, the local peer negotiates the abilities in the offer...
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
char * url
input or output URL.
char * protocol_whitelist
',' separated list of allowed protocols.
char * protocol_blacklist
',' separated list of disallowed protocols.
char srtp_send_key[RTC_SRTP_KEY_SIZE]
uint8_t dtls_srtp_materials[(RTC_DTLS_SRTP_KEY_LEN+RTC_DTLS_SRTP_SALT_LEN) *2]
This represents the material used to build the SRTP master key.
char key_buf[MAX_CERTIFICATE_SIZE]
char * remote_fingerprint
int pkt_size
The size of RTP packet, should generally be set to MTU.
char buf[RTC_MAX_UDP_BUFFER_SIZE]
char * ice_protocol
This represents the ICE candidate protocol, priority, host and port.
char * sdp_offer
This is the SDP offer generated by the local peer based on the codec parameters, DTLS,...
int64_t last_consent_rx_time
char srtp_recv_key[RTC_SRTP_KEY_SIZE]
char cert_buf[MAX_CERTIFICATE_SIZE]
int64_t last_consent_tx_time
char * authorization
The optional Bearer token for HTTP Authorization.
int av_usleep(unsigned usec)
Sleep for a period of time.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
int ff_is_dtls_packet(const uint8_t *buf, int size)
Whether the packet is a DTLS packet, as defined by RFC 5764 Section 5.1.2.
int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
int ff_ssl_read_key_cert(char *key_url, char *cert_url, char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz)
int ff_tls_set_external_socket(URLContext *h, URLContext *sock)
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.