44#define WHIP_RTP_PAYLOAD_TYPE_H264 106
45#define WHIP_RTP_PAYLOAD_TYPE_OPUS 111
46#define WHIP_RTP_PAYLOAD_TYPE_VIDEO_RTX 105
52#define WHIP_SDP_SESSION_ID "4489045141692799359"
53#define WHIP_SDP_CREATOR_IP "127.0.0.1"
62#define WHIP_RTP_HISTORY_MIN 64
63#define WHIP_RTP_HISTORY_DEFAULT 512
64#define WHIP_RTP_HISTORY_MAX 2048
120 int ret, ideal_pkt_size = 532;
177 av_log(whip,
AV_LOG_ERROR,
"Unable to parse profile from empty extradata=%p, size=%d\n",
197 sps->profile_idc,
sps->level_idc);
234 for (
i = 0;
i <
s->nb_streams;
i++) {
292 const char *acodec_name =
NULL, *vcodec_name =
NULL;
294 int bundle_index = 0;
324 bundle[bundle_index++] =
'0';
325 bundle[bundle_index++] =
' ';
328 bundle[bundle_index++] =
'1';
329 bundle[bundle_index++] =
' ';
331 bundle[bundle_index - 1] =
'\0';
335 "o=FFmpeg %s 2 IN IP4 %s\r\n"
336 "s=FFmpegPublishSession\r\n"
338 "a=group:BUNDLE %s\r\n"
339 "a=extmap-allow-mixed\r\n"
340 "a=msid-semantic: WMS\r\n",
347 acodec_name =
"opus";
350 "m=audio 9 UDP/TLS/RTP/SAVPF %u\r\n"
351 "c=IN IP4 0.0.0.0\r\n"
354 "a=fingerprint:sha-256 %s\r\n"
358 "a=msid:FFmpeg audio\r\n"
360 "a=rtpmap:%u %s/%d/%d\r\n"
361 "a=ssrc:%u cname:FFmpeg\r\n"
362 "a=ssrc:%u msid:FFmpeg audio\r\n",
367 is_dtls_active ?
"active" :
"passive",
379 vcodec_name =
"H264";
386 "m=video 9 UDP/TLS/RTP/SAVPF %u %u\r\n"
387 "c=IN IP4 0.0.0.0\r\n"
390 "a=fingerprint:sha-256 %s\r\n"
394 "a=msid:FFmpeg video\r\n"
397 "a=rtpmap:%u %s/90000\r\n"
398 "a=fmtp:%u level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=%02x%02x%02x\r\n"
399 "a=rtcp-fb:%u nack\r\n"
400 "a=rtpmap:%u rtx/90000\r\n"
401 "a=fmtp:%u apt=%u\r\n"
402 "a=ssrc-group:FID %u %u\r\n"
403 "a=ssrc:%u cname:FFmpeg\r\n"
404 "a=ssrc:%u msid:FFmpeg video\r\n",
410 is_dtls_active ?
"active" :
"passive",
514 uint16_t seq =
AV_RB16(buf + 2);
531 return it->
seq == seq ? it :
NULL;
543 int ret, cipher_size, is_rtcp, is_video;
544 uint8_t payload_type;
556 payload_type = buf[1] & 0x7f;
566 if (cipher_size <= 0 || cipher_size < buf_size) {
567 av_log(whip,
AV_LOG_WARNING,
"Failed to encrypt packet=%dB, cipher=%dB\n", buf_size, cipher_size);
599 int ret,
i, is_video, buffer_size, max_packet_size;
613 for (
i = 0;
i <
s->nb_streams;
i++) {
677 s->streams[
i]->priv_data = rtp_ctx;
684 "elapsed=%.2fms(init:%.2f,offer:%.2f,answer:%.2f,udp:%.2f,ice:%.2f,dtls:%.2f,srtp:%.2f)\n",
717 uint8_t unit_type, sps_seen = 0, pps_seen = 0, idr_seen = 0, *
out;
718 const uint8_t *buf, *buf_end, *r1;
724 buf_end =
pkt->data +
pkt->size;
728 if ((nal_size = r1 - buf) > 0) {
729 unit_type = *buf & 0x1f;
742 if (!idr_seen || (sps_seen && pps_seen))
771 if ((nal_size = r1 - buf) > 0) {
773 memcpy(
out + 3, buf, nal_size);
835 int ori_size, rtx_size, cipher_size;
842 "RTP history packet seq=%"PRIu16
" not found, latest seq=%"PRIu16
"\n",
847 "Found RTP history packet for RTX, seq=%"PRIu16
", latest seq=%"PRIu16
"\n",
860 if (ori_size + 2 >
sizeof(rtx_buf)) {
865 memcpy(rtx_buf, ori_buf, ori_size);
866 ori_seq =
AV_RB16(rtx_buf + 2);
874 memmove(rtx_buf + 12 + 2, rtx_buf + 12, ori_size - 12);
875 AV_WB16(rtx_buf + 12, ori_seq);
877 rtx_size = ori_size + 2;
880 rtc->
buf,
sizeof(rtc->
buf));
881 if (cipher_size <= 0) {
883 "Failed to encrypt RTX packet, size=%d, cipher_size=%d\n",
884 rtx_size, cipher_size);
899 int rtcp_len, srtcp_len, header_len = 12;
908 if (rtcp_len <= header_len) {
913 srtcp_len = rtcp_len + 4 + 10;
914 if (srtcp_len !=
size) {
928 "NACK packet SSRC: %"PRIu32
" not match with video track SSRC: %"PRIu32
"\n",
932 while (header_len +
i + 4 <= rtcp_len) {
937 uint16_t pid =
AV_RB16(&buf[12 +
i]);
938 uint16_t blp =
AV_RB16(&buf[14 +
i]);
943 uint16_t seq = pid +
bit + 1;
946 if (!(blp & (1 <<
bit)))
994 uint8_t fmt = rtc->
buf[0] & 0x1f;
995 uint8_t
pt = rtc->
buf[1];
1023 av_log(whip,
AV_LOG_ERROR,
"UDP send blocked, please increase the buffer via -ts_buffer_size\n");
1045 for (
i = 0;
i <
s->nb_streams;
i++) {
1059 s->streams[
i]->priv_data =
NULL;
1074 int ret = 1, extradata_isom = 0;
1075 uint8_t *
b =
pkt->data;
1080 if (
pkt->size >= 5 &&
AV_RB32(
b) != 0x0000001 && (
AV_RB24(
b) != 0x000001 || extradata_isom)) {
1082 av_log(whip,
AV_LOG_VERBOSE,
"Enable BSF h264_mp4toannexb, packet=[%x %x %x %x %x ...], extradata_isom=%d\n",
1083 b[0],
b[1],
b[2],
b[3],
b[4], extradata_isom);
1091#define OFFSET(x) offsetof(WHIPContext, x)
1092#define ENC AV_OPT_FLAG_ENCODING_PARAM
1095 {
"whip_flags",
"Set flags affecting WHIP connection behavior",
OFFSET(rtc.flags),
AV_OPT_TYPE_FLAGS, { .i64 = 0}, 0, UINT_MAX,
ENC, .unit =
"flags" },
1101 .class_name =
"WHIP muxer",
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
const AVClass ff_avio_class
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.
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 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)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define bit(string, value)
static void deinit(AVFormatContext *s)
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
#define AV_PROFILE_UNKNOWN
#define AV_PROFILE_H264_CONSTRAINED
#define AV_PROFILE_H264_INTRA
int(* init)(AVBSFContext *ctx)
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
@ AV_OPT_TYPE_INT
Underlying C type is int.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
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.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
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_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
#define LIBAVUTIL_VERSION_INT
H.264 common definitions.
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
const uint8_t * ff_nal_find_startcode(const uint8_t *p, const uint8_t *end)
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.
av_cold int ff_rtc_initialize(RTCContext *rtc)
int ff_rtc_media_is_rtcp(const uint8_t *b, int size)
int ff_rtc_ice_dtls_handshake(RTCContext *rtc)
int ff_rtc_ice_check_consent(RTCContext *rtc, int64_t now)
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_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...
av_cold void ff_rtc_deinit(RTCContext *rtc)
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_UDP_BUFFER_SIZE
Maximum size of the buffer for sending and receiving UDP packets.
#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_SRTP_SUITE
The profile for OpenSSL's SRTP is SRTP_AES128_CM_SHA1_80, see ssl/d1_srtp.c.
#define RTC_RTP_HEADER_SIZE
The RTP header is 12 bytes long, comprising the Version(1B), PT(1B), SequenceNumber(2B),...
#define RTC_OPTIONS(pstruct, field, flag)
#define RTC_DTLS_SRTP_CHECKSUM_LEN
The maximum size of the Secure Real-time Transport Protocol (SRTP) HMAC checksum and padding that is ...
#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_SRTP_FINISHED
int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len, uint8_t *out, int outlen)
int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite, const char *params)
int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
void ff_srtp_free(struct SRTPContext *s)
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
This struct describes the properties of an encoded stream.
int extradata_size
Size of the extradata content in bytes.
AVChannelLayout ch_layout
The channel layout and number of channels.
enum AVMediaType codec_type
General type of the encoded data.
int profile
Codec-specific bitstream restrictions that the stream conforms to.
int video_delay
Number of delayed frames.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
The number of audio samples per second.
AVIOContext * pb
I/O context.
int flags
Flags modifying the (de)muxer behaviour.
const struct AVOutputFormat * oformat
The output container format.
int strict_std_compliance
Allow non-standard and experimental extension.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
int64_t start_time_realtime
Start time of the stream in real world time, in microseconds since the Unix epoch (00:00 1st January ...
AVStream ** streams
A list of all streams in the file.
const AVClass * av_class
A class for private options.
unsigned char * buffer
Start of the buffer.
This structure stores compressed data.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
char srtp_send_key[RTC_SRTP_KEY_SIZE]
int pkt_size
The size of RTP packet, should generally be set to MTU.
char buf[RTC_MAX_UDP_BUFFER_SIZE]
char * sdp_offer
This is the SDP offer generated by the local peer based on the codec parameters, DTLS,...
char srtp_recv_key[RTC_SRTP_KEY_SIZE]
SRTPContext srtp_rtcp_send
AVCodecParameters * audio_par
uint8_t video_payload_type
uint8_t video_rtx_payload_type
AVCodecParameters * video_par
SRTPContext srtp_audio_send
SRTPContext srtp_video_send
int h264_annexb_insert_sps_pps
The h264_mp4toannexb Bitstream Filter (BSF) bypasses the AnnexB packet; therefore,...
SRTPContext srtp_video_rtx_send
uint8_t audio_payload_type
static void error(const char *err)
static struct @232240247175332074307271266321204261331170211232 state
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.
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.
static int rtp_history_store(WHIPContext *whip, const uint8_t *buf, int size)
static int parse_codec(AVFormatContext *s)
Parses video SPS/PPS from the extradata of codecpar and checks the codec.
static void handle_nack_rtx(AVFormatContext *s, int size)
static const RtpHistoryItem * rtp_history_find(WHIPContext *whip, uint16_t seq)
#define WHIP_RTP_PAYLOAD_TYPE_OPUS
static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
static int generate_sdp_offer(AVFormatContext *s)
Generate SDP offer according to the codec parameters, DTLS and ICE information.
static int on_rtp_write_packet(void *opaque, const uint8_t *buf, int buf_size)
Callback triggered by the RTP muxer when it creates and sends out an RTP packet.
static int whip_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
#define WHIP_SDP_CREATOR_IP
static av_cold int whip_init(AVFormatContext *s)
static av_cold int initialize(AVFormatContext *s)
Initialize and check the options for the WebRTC muxer.
#define WHIP_RTP_PAYLOAD_TYPE_VIDEO_RTX
#define WHIP_SDP_SESSION_ID
In the case of ICE-LITE, these fields are not used; instead, they are defined as constant values.
#define WHIP_RTP_HISTORY_MIN
RTP history packet size.
#define WHIP_RTP_PAYLOAD_TYPE_H264
#define WHIP_RTP_HISTORY_DEFAULT
static int setup_srtp(AVFormatContext *s)
Establish the SRTP context using the keying material exported from DTLS.
static av_cold void whip_deinit(AVFormatContext *s)
#define WHIP_RTP_HISTORY_MAX
static int h264_annexb_insert_sps_pps(AVFormatContext *s, AVPacket *pkt)
Since the h264_mp4toannexb filter only processes the MP4 ISOM format and bypasses the annexb format,...
static void handle_rtx_packet(AVFormatContext *s, uint16_t seq)
See https://datatracker.ietf.org/doc/html/rfc4588#section-4 Create RTX packet and send it out.
static int parse_profile_level(AVFormatContext *s, AVCodecParameters *par)
When duplicating a stream, the demuxer has already set the extradata, profile, and level of the par.
static const AVClass whip_muxer_class
static int create_rtp_muxer(AVFormatContext *s)
Creates dedicated RTP muxers for each stream in the AVFormatContext to build RTP packets from the enc...