| 1 | /* |
| 2 | * RTP input format |
| 3 | * Copyright (c) 2002 Fabrice Bellard |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "libavutil/mathematics.h" |
| 23 | #include "libavutil/avstring.h" |
| 24 | #include "libavutil/time.h" |
| 25 | #include "libavcodec/get_bits.h" |
| 26 | #include "avformat.h" |
| 27 | #include "network.h" |
| 28 | #include "srtp.h" |
| 29 | #include "url.h" |
| 30 | #include "rtpdec.h" |
| 31 | #include "rtpdec_formats.h" |
| 32 | |
| 33 | #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */ |
| 34 | |
| 35 | static RTPDynamicProtocolHandler gsm_dynamic_handler = { |
| 36 | .enc_name = "GSM", |
| 37 | .codec_type = AVMEDIA_TYPE_AUDIO, |
| 38 | .codec_id = AV_CODEC_ID_GSM, |
| 39 | }; |
| 40 | |
| 41 | static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = { |
| 42 | .enc_name = "X-MP3-draft-00", |
| 43 | .codec_type = AVMEDIA_TYPE_AUDIO, |
| 44 | .codec_id = AV_CODEC_ID_MP3ADU, |
| 45 | }; |
| 46 | |
| 47 | static RTPDynamicProtocolHandler speex_dynamic_handler = { |
| 48 | .enc_name = "speex", |
| 49 | .codec_type = AVMEDIA_TYPE_AUDIO, |
| 50 | .codec_id = AV_CODEC_ID_SPEEX, |
| 51 | }; |
| 52 | |
| 53 | static RTPDynamicProtocolHandler opus_dynamic_handler = { |
| 54 | .enc_name = "opus", |
| 55 | .codec_type = AVMEDIA_TYPE_AUDIO, |
| 56 | .codec_id = AV_CODEC_ID_OPUS, |
| 57 | }; |
| 58 | |
| 59 | static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL; |
| 60 | |
| 61 | void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler) |
| 62 | { |
| 63 | handler->next = rtp_first_dynamic_payload_handler; |
| 64 | rtp_first_dynamic_payload_handler = handler; |
| 65 | } |
| 66 | |
| 67 | void ff_register_rtp_dynamic_payload_handlers(void) |
| 68 | { |
| 69 | ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler); |
| 70 | ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler); |
| 71 | ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler); |
| 72 | ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler); |
| 73 | ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler); |
| 74 | ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler); |
| 75 | ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler); |
| 76 | ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler); |
| 77 | ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler); |
| 78 | ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler); |
| 79 | ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler); |
| 80 | ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler); |
| 81 | ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler); |
| 82 | ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler); |
| 83 | ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler); |
| 84 | ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler); |
| 85 | ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler); |
| 86 | ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler); |
| 87 | ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler); |
| 88 | ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler); |
| 89 | ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler); |
| 90 | ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler); |
| 91 | ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler); |
| 92 | ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler); |
| 93 | ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler); |
| 94 | ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler); |
| 95 | ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler); |
| 96 | ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler); |
| 97 | ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler); |
| 98 | ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler); |
| 99 | ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler); |
| 100 | ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler); |
| 101 | ff_register_dynamic_payload_handler(&gsm_dynamic_handler); |
| 102 | ff_register_dynamic_payload_handler(&opus_dynamic_handler); |
| 103 | ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler); |
| 104 | ff_register_dynamic_payload_handler(&speex_dynamic_handler); |
| 105 | } |
| 106 | |
| 107 | RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name, |
| 108 | enum AVMediaType codec_type) |
| 109 | { |
| 110 | RTPDynamicProtocolHandler *handler; |
| 111 | for (handler = rtp_first_dynamic_payload_handler; |
| 112 | handler; handler = handler->next) |
| 113 | if (!av_strcasecmp(name, handler->enc_name) && |
| 114 | codec_type == handler->codec_type) |
| 115 | return handler; |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id, |
| 120 | enum AVMediaType codec_type) |
| 121 | { |
| 122 | RTPDynamicProtocolHandler *handler; |
| 123 | for (handler = rtp_first_dynamic_payload_handler; |
| 124 | handler; handler = handler->next) |
| 125 | if (handler->static_payload_id && handler->static_payload_id == id && |
| 126 | codec_type == handler->codec_type) |
| 127 | return handler; |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, |
| 132 | int len) |
| 133 | { |
| 134 | int payload_len; |
| 135 | while (len >= 4) { |
| 136 | payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4); |
| 137 | |
| 138 | switch (buf[1]) { |
| 139 | case RTCP_SR: |
| 140 | if (payload_len < 20) { |
| 141 | av_log(NULL, AV_LOG_ERROR, |
| 142 | "Invalid length for RTCP SR packet\n"); |
| 143 | return AVERROR_INVALIDDATA; |
| 144 | } |
| 145 | |
| 146 | s->last_rtcp_reception_time = av_gettime_relative(); |
| 147 | s->last_rtcp_ntp_time = AV_RB64(buf + 8); |
| 148 | s->last_rtcp_timestamp = AV_RB32(buf + 16); |
| 149 | if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) { |
| 150 | s->first_rtcp_ntp_time = s->last_rtcp_ntp_time; |
| 151 | if (!s->base_timestamp) |
| 152 | s->base_timestamp = s->last_rtcp_timestamp; |
| 153 | s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp; |
| 154 | } |
| 155 | |
| 156 | break; |
| 157 | case RTCP_BYE: |
| 158 | return -RTCP_BYE; |
| 159 | } |
| 160 | |
| 161 | buf += payload_len; |
| 162 | len -= payload_len; |
| 163 | } |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | #define RTP_SEQ_MOD (1 << 16) |
| 168 | |
| 169 | static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) |
| 170 | { |
| 171 | memset(s, 0, sizeof(RTPStatistics)); |
| 172 | s->max_seq = base_sequence; |
| 173 | s->probation = 1; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Called whenever there is a large jump in sequence numbers, |
| 178 | * or when they get out of probation... |
| 179 | */ |
| 180 | static void rtp_init_sequence(RTPStatistics *s, uint16_t seq) |
| 181 | { |
| 182 | s->max_seq = seq; |
| 183 | s->cycles = 0; |
| 184 | s->base_seq = seq - 1; |
| 185 | s->bad_seq = RTP_SEQ_MOD + 1; |
| 186 | s->received = 0; |
| 187 | s->expected_prior = 0; |
| 188 | s->received_prior = 0; |
| 189 | s->jitter = 0; |
| 190 | s->transit = 0; |
| 191 | } |
| 192 | |
| 193 | /* Returns 1 if we should handle this packet. */ |
| 194 | static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq) |
| 195 | { |
| 196 | uint16_t udelta = seq - s->max_seq; |
| 197 | const int MAX_DROPOUT = 3000; |
| 198 | const int MAX_MISORDER = 100; |
| 199 | const int MIN_SEQUENTIAL = 2; |
| 200 | |
| 201 | /* source not valid until MIN_SEQUENTIAL packets with sequence |
| 202 | * seq. numbers have been received */ |
| 203 | if (s->probation) { |
| 204 | if (seq == s->max_seq + 1) { |
| 205 | s->probation--; |
| 206 | s->max_seq = seq; |
| 207 | if (s->probation == 0) { |
| 208 | rtp_init_sequence(s, seq); |
| 209 | s->received++; |
| 210 | return 1; |
| 211 | } |
| 212 | } else { |
| 213 | s->probation = MIN_SEQUENTIAL - 1; |
| 214 | s->max_seq = seq; |
| 215 | } |
| 216 | } else if (udelta < MAX_DROPOUT) { |
| 217 | // in order, with permissible gap |
| 218 | if (seq < s->max_seq) { |
| 219 | // sequence number wrapped; count another 64k cycles |
| 220 | s->cycles += RTP_SEQ_MOD; |
| 221 | } |
| 222 | s->max_seq = seq; |
| 223 | } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) { |
| 224 | // sequence made a large jump... |
| 225 | if (seq == s->bad_seq) { |
| 226 | /* two sequential packets -- assume that the other side |
| 227 | * restarted without telling us; just resync. */ |
| 228 | rtp_init_sequence(s, seq); |
| 229 | } else { |
| 230 | s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1); |
| 231 | return 0; |
| 232 | } |
| 233 | } else { |
| 234 | // duplicate or reordered packet... |
| 235 | } |
| 236 | s->received++; |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, |
| 241 | uint32_t arrival_timestamp) |
| 242 | { |
| 243 | // Most of this is pretty straight from RFC 3550 appendix A.8 |
| 244 | uint32_t transit = arrival_timestamp - sent_timestamp; |
| 245 | uint32_t prev_transit = s->transit; |
| 246 | int32_t d = transit - prev_transit; |
| 247 | // Doing the FFABS() call directly on the "transit - prev_transit" |
| 248 | // expression doesn't work, since it's an unsigned expression. Doing the |
| 249 | // transit calculation in unsigned is desired though, since it most |
| 250 | // probably will need to wrap around. |
| 251 | d = FFABS(d); |
| 252 | s->transit = transit; |
| 253 | if (!prev_transit) |
| 254 | return; |
| 255 | s->jitter += d - (int32_t) ((s->jitter + 8) >> 4); |
| 256 | } |
| 257 | |
| 258 | int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd, |
| 259 | AVIOContext *avio, int count) |
| 260 | { |
| 261 | AVIOContext *pb; |
| 262 | uint8_t *buf; |
| 263 | int len; |
| 264 | int rtcp_bytes; |
| 265 | RTPStatistics *stats = &s->statistics; |
| 266 | uint32_t lost; |
| 267 | uint32_t extended_max; |
| 268 | uint32_t expected_interval; |
| 269 | uint32_t received_interval; |
| 270 | int32_t lost_interval; |
| 271 | uint32_t expected; |
| 272 | uint32_t fraction; |
| 273 | |
| 274 | if ((!fd && !avio) || (count < 1)) |
| 275 | return -1; |
| 276 | |
| 277 | /* TODO: I think this is way too often; RFC 1889 has algorithm for this */ |
| 278 | /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */ |
| 279 | s->octet_count += count; |
| 280 | rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / |
| 281 | RTCP_TX_RATIO_DEN; |
| 282 | rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !? |
| 283 | if (rtcp_bytes < 28) |
| 284 | return -1; |
| 285 | s->last_octet_count = s->octet_count; |
| 286 | |
| 287 | if (!fd) |
| 288 | pb = avio; |
| 289 | else if (avio_open_dyn_buf(&pb) < 0) |
| 290 | return -1; |
| 291 | |
| 292 | // Receiver Report |
| 293 | avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */ |
| 294 | avio_w8(pb, RTCP_RR); |
| 295 | avio_wb16(pb, 7); /* length in words - 1 */ |
| 296 | // our own SSRC: we use the server's SSRC + 1 to avoid conflicts |
| 297 | avio_wb32(pb, s->ssrc + 1); |
| 298 | avio_wb32(pb, s->ssrc); // server SSRC |
| 299 | // some placeholders we should really fill... |
| 300 | // RFC 1889/p64 |
| 301 | extended_max = stats->cycles + stats->max_seq; |
| 302 | expected = extended_max - stats->base_seq; |
| 303 | lost = expected - stats->received; |
| 304 | lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits... |
| 305 | expected_interval = expected - stats->expected_prior; |
| 306 | stats->expected_prior = expected; |
| 307 | received_interval = stats->received - stats->received_prior; |
| 308 | stats->received_prior = stats->received; |
| 309 | lost_interval = expected_interval - received_interval; |
| 310 | if (expected_interval == 0 || lost_interval <= 0) |
| 311 | fraction = 0; |
| 312 | else |
| 313 | fraction = (lost_interval << 8) / expected_interval; |
| 314 | |
| 315 | fraction = (fraction << 24) | lost; |
| 316 | |
| 317 | avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */ |
| 318 | avio_wb32(pb, extended_max); /* max sequence received */ |
| 319 | avio_wb32(pb, stats->jitter >> 4); /* jitter */ |
| 320 | |
| 321 | if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) { |
| 322 | avio_wb32(pb, 0); /* last SR timestamp */ |
| 323 | avio_wb32(pb, 0); /* delay since last SR */ |
| 324 | } else { |
| 325 | uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special? |
| 326 | uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time, |
| 327 | 65536, AV_TIME_BASE); |
| 328 | |
| 329 | avio_wb32(pb, middle_32_bits); /* last SR timestamp */ |
| 330 | avio_wb32(pb, delay_since_last); /* delay since last SR */ |
| 331 | } |
| 332 | |
| 333 | // CNAME |
| 334 | avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */ |
| 335 | avio_w8(pb, RTCP_SDES); |
| 336 | len = strlen(s->hostname); |
| 337 | avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */ |
| 338 | avio_wb32(pb, s->ssrc + 1); |
| 339 | avio_w8(pb, 0x01); |
| 340 | avio_w8(pb, len); |
| 341 | avio_write(pb, s->hostname, len); |
| 342 | avio_w8(pb, 0); /* END */ |
| 343 | // padding |
| 344 | for (len = (7 + len) % 4; len % 4; len++) |
| 345 | avio_w8(pb, 0); |
| 346 | |
| 347 | avio_flush(pb); |
| 348 | if (!fd) |
| 349 | return 0; |
| 350 | len = avio_close_dyn_buf(pb, &buf); |
| 351 | if ((len > 0) && buf) { |
| 352 | int av_unused result; |
| 353 | av_dlog(s->ic, "sending %d bytes of RR\n", len); |
| 354 | result = ffurl_write(fd, buf, len); |
| 355 | av_dlog(s->ic, "result from ffurl_write: %d\n", result); |
| 356 | av_free(buf); |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | void ff_rtp_send_punch_packets(URLContext *rtp_handle) |
| 362 | { |
| 363 | AVIOContext *pb; |
| 364 | uint8_t *buf; |
| 365 | int len; |
| 366 | |
| 367 | /* Send a small RTP packet */ |
| 368 | if (avio_open_dyn_buf(&pb) < 0) |
| 369 | return; |
| 370 | |
| 371 | avio_w8(pb, (RTP_VERSION << 6)); |
| 372 | avio_w8(pb, 0); /* Payload type */ |
| 373 | avio_wb16(pb, 0); /* Seq */ |
| 374 | avio_wb32(pb, 0); /* Timestamp */ |
| 375 | avio_wb32(pb, 0); /* SSRC */ |
| 376 | |
| 377 | avio_flush(pb); |
| 378 | len = avio_close_dyn_buf(pb, &buf); |
| 379 | if ((len > 0) && buf) |
| 380 | ffurl_write(rtp_handle, buf, len); |
| 381 | av_free(buf); |
| 382 | |
| 383 | /* Send a minimal RTCP RR */ |
| 384 | if (avio_open_dyn_buf(&pb) < 0) |
| 385 | return; |
| 386 | |
| 387 | avio_w8(pb, (RTP_VERSION << 6)); |
| 388 | avio_w8(pb, RTCP_RR); /* receiver report */ |
| 389 | avio_wb16(pb, 1); /* length in words - 1 */ |
| 390 | avio_wb32(pb, 0); /* our own SSRC */ |
| 391 | |
| 392 | avio_flush(pb); |
| 393 | len = avio_close_dyn_buf(pb, &buf); |
| 394 | if ((len > 0) && buf) |
| 395 | ffurl_write(rtp_handle, buf, len); |
| 396 | av_free(buf); |
| 397 | } |
| 398 | |
| 399 | static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing, |
| 400 | uint16_t *missing_mask) |
| 401 | { |
| 402 | int i; |
| 403 | uint16_t next_seq = s->seq + 1; |
| 404 | RTPPacket *pkt = s->queue; |
| 405 | |
| 406 | if (!pkt || pkt->seq == next_seq) |
| 407 | return 0; |
| 408 | |
| 409 | *missing_mask = 0; |
| 410 | for (i = 1; i <= 16; i++) { |
| 411 | uint16_t missing_seq = next_seq + i; |
| 412 | while (pkt) { |
| 413 | int16_t diff = pkt->seq - missing_seq; |
| 414 | if (diff >= 0) |
| 415 | break; |
| 416 | pkt = pkt->next; |
| 417 | } |
| 418 | if (!pkt) |
| 419 | break; |
| 420 | if (pkt->seq == missing_seq) |
| 421 | continue; |
| 422 | *missing_mask |= 1 << (i - 1); |
| 423 | } |
| 424 | |
| 425 | *first_missing = next_seq; |
| 426 | return 1; |
| 427 | } |
| 428 | |
| 429 | int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd, |
| 430 | AVIOContext *avio) |
| 431 | { |
| 432 | int len, need_keyframe, missing_packets; |
| 433 | AVIOContext *pb; |
| 434 | uint8_t *buf; |
| 435 | int64_t now; |
| 436 | uint16_t first_missing = 0, missing_mask = 0; |
| 437 | |
| 438 | if (!fd && !avio) |
| 439 | return -1; |
| 440 | |
| 441 | need_keyframe = s->handler && s->handler->need_keyframe && |
| 442 | s->handler->need_keyframe(s->dynamic_protocol_context); |
| 443 | missing_packets = find_missing_packets(s, &first_missing, &missing_mask); |
| 444 | |
| 445 | if (!need_keyframe && !missing_packets) |
| 446 | return 0; |
| 447 | |
| 448 | /* Send new feedback if enough time has elapsed since the last |
| 449 | * feedback packet. */ |
| 450 | |
| 451 | now = av_gettime_relative(); |
| 452 | if (s->last_feedback_time && |
| 453 | (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL) |
| 454 | return 0; |
| 455 | s->last_feedback_time = now; |
| 456 | |
| 457 | if (!fd) |
| 458 | pb = avio; |
| 459 | else if (avio_open_dyn_buf(&pb) < 0) |
| 460 | return -1; |
| 461 | |
| 462 | if (need_keyframe) { |
| 463 | avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */ |
| 464 | avio_w8(pb, RTCP_PSFB); |
| 465 | avio_wb16(pb, 2); /* length in words - 1 */ |
| 466 | // our own SSRC: we use the server's SSRC + 1 to avoid conflicts |
| 467 | avio_wb32(pb, s->ssrc + 1); |
| 468 | avio_wb32(pb, s->ssrc); // server SSRC |
| 469 | } |
| 470 | |
| 471 | if (missing_packets) { |
| 472 | avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */ |
| 473 | avio_w8(pb, RTCP_RTPFB); |
| 474 | avio_wb16(pb, 3); /* length in words - 1 */ |
| 475 | avio_wb32(pb, s->ssrc + 1); |
| 476 | avio_wb32(pb, s->ssrc); // server SSRC |
| 477 | |
| 478 | avio_wb16(pb, first_missing); |
| 479 | avio_wb16(pb, missing_mask); |
| 480 | } |
| 481 | |
| 482 | avio_flush(pb); |
| 483 | if (!fd) |
| 484 | return 0; |
| 485 | len = avio_close_dyn_buf(pb, &buf); |
| 486 | if (len > 0 && buf) { |
| 487 | ffurl_write(fd, buf, len); |
| 488 | av_free(buf); |
| 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * open a new RTP parse context for stream 'st'. 'st' can be NULL for |
| 495 | * MPEG2-TS streams. |
| 496 | */ |
| 497 | RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, |
| 498 | int payload_type, int queue_size) |
| 499 | { |
| 500 | RTPDemuxContext *s; |
| 501 | |
| 502 | s = av_mallocz(sizeof(RTPDemuxContext)); |
| 503 | if (!s) |
| 504 | return NULL; |
| 505 | s->payload_type = payload_type; |
| 506 | s->last_rtcp_ntp_time = AV_NOPTS_VALUE; |
| 507 | s->first_rtcp_ntp_time = AV_NOPTS_VALUE; |
| 508 | s->ic = s1; |
| 509 | s->st = st; |
| 510 | s->queue_size = queue_size; |
| 511 | rtp_init_statistics(&s->statistics, 0); |
| 512 | if (st) { |
| 513 | switch (st->codec->codec_id) { |
| 514 | case AV_CODEC_ID_ADPCM_G722: |
| 515 | /* According to RFC 3551, the stream clock rate is 8000 |
| 516 | * even if the sample rate is 16000. */ |
| 517 | if (st->codec->sample_rate == 8000) |
| 518 | st->codec->sample_rate = 16000; |
| 519 | break; |
| 520 | default: |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | // needed to send back RTCP RR in RTSP sessions |
| 525 | gethostname(s->hostname, sizeof(s->hostname)); |
| 526 | return s; |
| 527 | } |
| 528 | |
| 529 | void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx, |
| 530 | RTPDynamicProtocolHandler *handler) |
| 531 | { |
| 532 | s->dynamic_protocol_context = ctx; |
| 533 | s->handler = handler; |
| 534 | } |
| 535 | |
| 536 | void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite, |
| 537 | const char *params) |
| 538 | { |
| 539 | if (!ff_srtp_set_crypto(&s->srtp, suite, params)) |
| 540 | s->srtp_enabled = 1; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * This was the second switch in rtp_parse packet. |
| 545 | * Normalizes time, if required, sets stream_index, etc. |
| 546 | */ |
| 547 | static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp) |
| 548 | { |
| 549 | if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE) |
| 550 | return; /* Timestamp already set by depacketizer */ |
| 551 | if (timestamp == RTP_NOTS_VALUE) |
| 552 | return; |
| 553 | |
| 554 | if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) { |
| 555 | int64_t addend; |
| 556 | int delta_timestamp; |
| 557 | |
| 558 | /* compute pts from timestamp with received ntp_time */ |
| 559 | delta_timestamp = timestamp - s->last_rtcp_timestamp; |
| 560 | /* convert to the PTS timebase */ |
| 561 | addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, |
| 562 | s->st->time_base.den, |
| 563 | (uint64_t) s->st->time_base.num << 32); |
| 564 | pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend + |
| 565 | delta_timestamp; |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | if (!s->base_timestamp) |
| 570 | s->base_timestamp = timestamp; |
| 571 | /* assume that the difference is INT32_MIN < x < INT32_MAX, |
| 572 | * but allow the first timestamp to exceed INT32_MAX */ |
| 573 | if (!s->timestamp) |
| 574 | s->unwrapped_timestamp += timestamp; |
| 575 | else |
| 576 | s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp); |
| 577 | s->timestamp = timestamp; |
| 578 | pkt->pts = s->unwrapped_timestamp + s->range_start_offset - |
| 579 | s->base_timestamp; |
| 580 | } |
| 581 | |
| 582 | static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
| 583 | const uint8_t *buf, int len) |
| 584 | { |
| 585 | unsigned int ssrc; |
| 586 | int payload_type, seq, flags = 0; |
| 587 | int ext, csrc; |
| 588 | AVStream *st; |
| 589 | uint32_t timestamp; |
| 590 | int rv = 0; |
| 591 | |
| 592 | csrc = buf[0] & 0x0f; |
| 593 | ext = buf[0] & 0x10; |
| 594 | payload_type = buf[1] & 0x7f; |
| 595 | if (buf[1] & 0x80) |
| 596 | flags |= RTP_FLAG_MARKER; |
| 597 | seq = AV_RB16(buf + 2); |
| 598 | timestamp = AV_RB32(buf + 4); |
| 599 | ssrc = AV_RB32(buf + 8); |
| 600 | /* store the ssrc in the RTPDemuxContext */ |
| 601 | s->ssrc = ssrc; |
| 602 | |
| 603 | /* NOTE: we can handle only one payload type */ |
| 604 | if (s->payload_type != payload_type) |
| 605 | return -1; |
| 606 | |
| 607 | st = s->st; |
| 608 | // only do something with this if all the rtp checks pass... |
| 609 | if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) { |
| 610 | av_log(st ? st->codec : NULL, AV_LOG_ERROR, |
| 611 | "RTP: PT=%02x: bad cseq %04x expected=%04x\n", |
| 612 | payload_type, seq, ((s->seq + 1) & 0xffff)); |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | if (buf[0] & 0x20) { |
| 617 | int padding = buf[len - 1]; |
| 618 | if (len >= 12 + padding) |
| 619 | len -= padding; |
| 620 | } |
| 621 | |
| 622 | s->seq = seq; |
| 623 | len -= 12; |
| 624 | buf += 12; |
| 625 | |
| 626 | len -= 4 * csrc; |
| 627 | buf += 4 * csrc; |
| 628 | if (len < 0) |
| 629 | return AVERROR_INVALIDDATA; |
| 630 | |
| 631 | /* RFC 3550 Section 5.3.1 RTP Header Extension handling */ |
| 632 | if (ext) { |
| 633 | if (len < 4) |
| 634 | return -1; |
| 635 | /* calculate the header extension length (stored as number |
| 636 | * of 32-bit words) */ |
| 637 | ext = (AV_RB16(buf + 2) + 1) << 2; |
| 638 | |
| 639 | if (len < ext) |
| 640 | return -1; |
| 641 | // skip past RTP header extension |
| 642 | len -= ext; |
| 643 | buf += ext; |
| 644 | } |
| 645 | |
| 646 | if (s->handler && s->handler->parse_packet) { |
| 647 | rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context, |
| 648 | s->st, pkt, ×tamp, buf, len, seq, |
| 649 | flags); |
| 650 | } else if (st) { |
| 651 | if ((rv = av_new_packet(pkt, len)) < 0) |
| 652 | return rv; |
| 653 | memcpy(pkt->data, buf, len); |
| 654 | pkt->stream_index = st->index; |
| 655 | } else { |
| 656 | return AVERROR(EINVAL); |
| 657 | } |
| 658 | |
| 659 | // now perform timestamp things.... |
| 660 | finalize_packet(s, pkt, timestamp); |
| 661 | |
| 662 | return rv; |
| 663 | } |
| 664 | |
| 665 | void ff_rtp_reset_packet_queue(RTPDemuxContext *s) |
| 666 | { |
| 667 | while (s->queue) { |
| 668 | RTPPacket *next = s->queue->next; |
| 669 | av_free(s->queue->buf); |
| 670 | av_free(s->queue); |
| 671 | s->queue = next; |
| 672 | } |
| 673 | s->seq = 0; |
| 674 | s->queue_len = 0; |
| 675 | s->prev_ret = 0; |
| 676 | } |
| 677 | |
| 678 | static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len) |
| 679 | { |
| 680 | uint16_t seq = AV_RB16(buf + 2); |
| 681 | RTPPacket **cur = &s->queue, *packet; |
| 682 | |
| 683 | /* Find the correct place in the queue to insert the packet */ |
| 684 | while (*cur) { |
| 685 | int16_t diff = seq - (*cur)->seq; |
| 686 | if (diff < 0) |
| 687 | break; |
| 688 | cur = &(*cur)->next; |
| 689 | } |
| 690 | |
| 691 | packet = av_mallocz(sizeof(*packet)); |
| 692 | if (!packet) |
| 693 | return; |
| 694 | packet->recvtime = av_gettime_relative(); |
| 695 | packet->seq = seq; |
| 696 | packet->len = len; |
| 697 | packet->buf = buf; |
| 698 | packet->next = *cur; |
| 699 | *cur = packet; |
| 700 | s->queue_len++; |
| 701 | } |
| 702 | |
| 703 | static int has_next_packet(RTPDemuxContext *s) |
| 704 | { |
| 705 | return s->queue && s->queue->seq == (uint16_t) (s->seq + 1); |
| 706 | } |
| 707 | |
| 708 | int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s) |
| 709 | { |
| 710 | return s->queue ? s->queue->recvtime : 0; |
| 711 | } |
| 712 | |
| 713 | static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt) |
| 714 | { |
| 715 | int rv; |
| 716 | RTPPacket *next; |
| 717 | |
| 718 | if (s->queue_len <= 0) |
| 719 | return -1; |
| 720 | |
| 721 | if (!has_next_packet(s)) |
| 722 | av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, |
| 723 | "RTP: missed %d packets\n", s->queue->seq - s->seq - 1); |
| 724 | |
| 725 | /* Parse the first packet in the queue, and dequeue it */ |
| 726 | rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len); |
| 727 | next = s->queue->next; |
| 728 | av_free(s->queue->buf); |
| 729 | av_free(s->queue); |
| 730 | s->queue = next; |
| 731 | s->queue_len--; |
| 732 | return rv; |
| 733 | } |
| 734 | |
| 735 | static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt, |
| 736 | uint8_t **bufptr, int len) |
| 737 | { |
| 738 | uint8_t *buf = bufptr ? *bufptr : NULL; |
| 739 | int flags = 0; |
| 740 | uint32_t timestamp; |
| 741 | int rv = 0; |
| 742 | |
| 743 | if (!buf) { |
| 744 | /* If parsing of the previous packet actually returned 0 or an error, |
| 745 | * there's nothing more to be parsed from that packet, but we may have |
| 746 | * indicated that we can return the next enqueued packet. */ |
| 747 | if (s->prev_ret <= 0) |
| 748 | return rtp_parse_queued_packet(s, pkt); |
| 749 | /* return the next packets, if any */ |
| 750 | if (s->handler && s->handler->parse_packet) { |
| 751 | /* timestamp should be overwritten by parse_packet, if not, |
| 752 | * the packet is left with pts == AV_NOPTS_VALUE */ |
| 753 | timestamp = RTP_NOTS_VALUE; |
| 754 | rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context, |
| 755 | s->st, pkt, ×tamp, NULL, 0, 0, |
| 756 | flags); |
| 757 | finalize_packet(s, pkt, timestamp); |
| 758 | return rv; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (len < 12) |
| 763 | return -1; |
| 764 | |
| 765 | if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) |
| 766 | return -1; |
| 767 | if (RTP_PT_IS_RTCP(buf[1])) { |
| 768 | return rtcp_parse_packet(s, buf, len); |
| 769 | } |
| 770 | |
| 771 | if (s->st) { |
| 772 | int64_t received = av_gettime_relative(); |
| 773 | uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q, |
| 774 | s->st->time_base); |
| 775 | timestamp = AV_RB32(buf + 4); |
| 776 | // Calculate the jitter immediately, before queueing the packet |
| 777 | // into the reordering queue. |
| 778 | rtcp_update_jitter(&s->statistics, timestamp, arrival_ts); |
| 779 | } |
| 780 | |
| 781 | if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) { |
| 782 | /* First packet, or no reordering */ |
| 783 | return rtp_parse_packet_internal(s, pkt, buf, len); |
| 784 | } else { |
| 785 | uint16_t seq = AV_RB16(buf + 2); |
| 786 | int16_t diff = seq - s->seq; |
| 787 | if (diff < 0) { |
| 788 | /* Packet older than the previously emitted one, drop */ |
| 789 | av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, |
| 790 | "RTP: dropping old packet received too late\n"); |
| 791 | return -1; |
| 792 | } else if (diff <= 1) { |
| 793 | /* Correct packet */ |
| 794 | rv = rtp_parse_packet_internal(s, pkt, buf, len); |
| 795 | return rv; |
| 796 | } else { |
| 797 | /* Still missing some packet, enqueue this one. */ |
| 798 | enqueue_packet(s, buf, len); |
| 799 | *bufptr = NULL; |
| 800 | /* Return the first enqueued packet if the queue is full, |
| 801 | * even if we're missing something */ |
| 802 | if (s->queue_len >= s->queue_size) |
| 803 | return rtp_parse_queued_packet(s, pkt); |
| 804 | return -1; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Parse an RTP or RTCP packet directly sent as a buffer. |
| 811 | * @param s RTP parse context. |
| 812 | * @param pkt returned packet |
| 813 | * @param bufptr pointer to the input buffer or NULL to read the next packets |
| 814 | * @param len buffer len |
| 815 | * @return 0 if a packet is returned, 1 if a packet is returned and more can follow |
| 816 | * (use buf as NULL to read the next). -1 if no packet (error or no more packet). |
| 817 | */ |
| 818 | int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, |
| 819 | uint8_t **bufptr, int len) |
| 820 | { |
| 821 | int rv; |
| 822 | if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0) |
| 823 | return -1; |
| 824 | rv = rtp_parse_one_packet(s, pkt, bufptr, len); |
| 825 | s->prev_ret = rv; |
| 826 | while (rv == AVERROR(EAGAIN) && has_next_packet(s)) |
| 827 | rv = rtp_parse_queued_packet(s, pkt); |
| 828 | return rv ? rv : has_next_packet(s); |
| 829 | } |
| 830 | |
| 831 | void ff_rtp_parse_close(RTPDemuxContext *s) |
| 832 | { |
| 833 | ff_rtp_reset_packet_queue(s); |
| 834 | ff_srtp_free(&s->srtp); |
| 835 | av_free(s); |
| 836 | } |
| 837 | |
| 838 | int ff_parse_fmtp(AVFormatContext *s, |
| 839 | AVStream *stream, PayloadContext *data, const char *p, |
| 840 | int (*parse_fmtp)(AVFormatContext *s, |
| 841 | AVStream *stream, |
| 842 | PayloadContext *data, |
| 843 | char *attr, char *value)) |
| 844 | { |
| 845 | char attr[256]; |
| 846 | char *value; |
| 847 | int res; |
| 848 | int value_size = strlen(p) + 1; |
| 849 | |
| 850 | if (!(value = av_malloc(value_size))) { |
| 851 | av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n"); |
| 852 | return AVERROR(ENOMEM); |
| 853 | } |
| 854 | |
| 855 | // remove protocol identifier |
| 856 | while (*p && *p == ' ') |
| 857 | p++; // strip spaces |
| 858 | while (*p && *p != ' ') |
| 859 | p++; // eat protocol identifier |
| 860 | while (*p && *p == ' ') |
| 861 | p++; // strip trailing spaces |
| 862 | |
| 863 | while (ff_rtsp_next_attr_and_value(&p, |
| 864 | attr, sizeof(attr), |
| 865 | value, value_size)) { |
| 866 | res = parse_fmtp(s, stream, data, attr, value); |
| 867 | if (res < 0 && res != AVERROR_PATCHWELCOME) { |
| 868 | av_free(value); |
| 869 | return res; |
| 870 | } |
| 871 | } |
| 872 | av_free(value); |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx) |
| 877 | { |
| 878 | int ret; |
| 879 | av_init_packet(pkt); |
| 880 | |
| 881 | pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data); |
| 882 | pkt->stream_index = stream_idx; |
| 883 | *dyn_buf = NULL; |
| 884 | if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) { |
| 885 | av_freep(&pkt->data); |
| 886 | return ret; |
| 887 | } |
| 888 | return pkt->size; |
| 889 | } |