You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

813 lines
26KB

  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. #include "libavutil/mathematics.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/time.h"
  24. #include "libavcodec/get_bits.h"
  25. #include "avformat.h"
  26. #include "mpegts.h"
  27. #include "url.h"
  28. #include "network.h"
  29. #include "rtpdec.h"
  30. #include "rtpdec_formats.h"
  31. //#define DEBUG
  32. /* TODO: - add RTCP statistics reporting (should be optional).
  33. - add support for h263/mpeg4 packetized output : IDEA: send a
  34. buffer to 'rtp_write_packet' contains all the packets for ONE
  35. frame. Each packet should have a four byte header containing
  36. the length in big endian format (same trick as
  37. 'ffio_open_dyn_packet_buf')
  38. */
  39. static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
  40. .enc_name = "X-MP3-draft-00",
  41. .codec_type = AVMEDIA_TYPE_AUDIO,
  42. .codec_id = AV_CODEC_ID_MP3ADU,
  43. };
  44. static RTPDynamicProtocolHandler speex_dynamic_handler = {
  45. .enc_name = "speex",
  46. .codec_type = AVMEDIA_TYPE_AUDIO,
  47. .codec_id = AV_CODEC_ID_SPEEX,
  48. };
  49. static RTPDynamicProtocolHandler opus_dynamic_handler = {
  50. .enc_name = "opus",
  51. .codec_type = AVMEDIA_TYPE_AUDIO,
  52. .codec_id = AV_CODEC_ID_OPUS,
  53. };
  54. /* statistics functions */
  55. static RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
  56. void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
  57. {
  58. handler->next= RTPFirstDynamicPayloadHandler;
  59. RTPFirstDynamicPayloadHandler= handler;
  60. }
  61. void av_register_rtp_dynamic_payload_handlers(void)
  62. {
  63. ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
  64. ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
  65. ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
  66. ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
  67. ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
  68. ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
  69. ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
  70. ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
  71. ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
  72. ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
  73. ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
  74. ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
  75. ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
  76. ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
  77. ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
  78. ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
  79. ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
  80. ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
  81. ff_register_dynamic_payload_handler(&speex_dynamic_handler);
  82. ff_register_dynamic_payload_handler(&opus_dynamic_handler);
  83. ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
  84. ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
  85. ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
  86. ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
  87. ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
  88. ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
  89. ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
  90. ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
  91. ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
  92. ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
  93. }
  94. RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
  95. enum AVMediaType codec_type)
  96. {
  97. RTPDynamicProtocolHandler *handler;
  98. for (handler = RTPFirstDynamicPayloadHandler;
  99. handler; handler = handler->next)
  100. if (!av_strcasecmp(name, handler->enc_name) &&
  101. codec_type == handler->codec_type)
  102. return handler;
  103. return NULL;
  104. }
  105. RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
  106. enum AVMediaType codec_type)
  107. {
  108. RTPDynamicProtocolHandler *handler;
  109. for (handler = RTPFirstDynamicPayloadHandler;
  110. handler; handler = handler->next)
  111. if (handler->static_payload_id && handler->static_payload_id == id &&
  112. codec_type == handler->codec_type)
  113. return handler;
  114. return NULL;
  115. }
  116. static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
  117. {
  118. int payload_len;
  119. while (len >= 4) {
  120. payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
  121. switch (buf[1]) {
  122. case RTCP_SR:
  123. if (payload_len < 20) {
  124. av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n");
  125. return AVERROR_INVALIDDATA;
  126. }
  127. s->last_rtcp_ntp_time = AV_RB64(buf + 8);
  128. s->last_rtcp_timestamp = AV_RB32(buf + 16);
  129. if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
  130. s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
  131. if (!s->base_timestamp)
  132. s->base_timestamp = s->last_rtcp_timestamp;
  133. s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
  134. }
  135. break;
  136. case RTCP_BYE:
  137. return -RTCP_BYE;
  138. }
  139. buf += payload_len;
  140. len -= payload_len;
  141. }
  142. return -1;
  143. }
  144. #define RTP_SEQ_MOD (1<<16)
  145. /**
  146. * called on parse open packet
  147. */
  148. static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
  149. {
  150. memset(s, 0, sizeof(RTPStatistics));
  151. s->max_seq= base_sequence;
  152. s->probation= 1;
  153. }
  154. /**
  155. * called whenever there is a large jump in sequence numbers, or when they get out of probation...
  156. */
  157. static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
  158. {
  159. s->max_seq= seq;
  160. s->cycles= 0;
  161. s->base_seq= seq -1;
  162. s->bad_seq= RTP_SEQ_MOD + 1;
  163. s->received= 0;
  164. s->expected_prior= 0;
  165. s->received_prior= 0;
  166. s->jitter= 0;
  167. s->transit= 0;
  168. }
  169. /**
  170. * returns 1 if we should handle this packet.
  171. */
  172. static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
  173. {
  174. uint16_t udelta= seq - s->max_seq;
  175. const int MAX_DROPOUT= 3000;
  176. const int MAX_MISORDER = 100;
  177. const int MIN_SEQUENTIAL = 2;
  178. /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
  179. if(s->probation)
  180. {
  181. if(seq==s->max_seq + 1) {
  182. s->probation--;
  183. s->max_seq= seq;
  184. if(s->probation==0) {
  185. rtp_init_sequence(s, seq);
  186. s->received++;
  187. return 1;
  188. }
  189. } else {
  190. s->probation= MIN_SEQUENTIAL - 1;
  191. s->max_seq = seq;
  192. }
  193. } else if (udelta < MAX_DROPOUT) {
  194. // in order, with permissible gap
  195. if(seq < s->max_seq) {
  196. //sequence number wrapped; count antother 64k cycles
  197. s->cycles += RTP_SEQ_MOD;
  198. }
  199. s->max_seq= seq;
  200. } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
  201. // sequence made a large jump...
  202. if(seq==s->bad_seq) {
  203. // two sequential packets-- assume that the other side restarted without telling us; just resync.
  204. rtp_init_sequence(s, seq);
  205. } else {
  206. s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
  207. return 0;
  208. }
  209. } else {
  210. // duplicate or reordered packet...
  211. }
  212. s->received++;
  213. return 1;
  214. }
  215. int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
  216. {
  217. AVIOContext *pb;
  218. uint8_t *buf;
  219. int len;
  220. int rtcp_bytes;
  221. RTPStatistics *stats= &s->statistics;
  222. uint32_t lost;
  223. uint32_t extended_max;
  224. uint32_t expected_interval;
  225. uint32_t received_interval;
  226. uint32_t lost_interval;
  227. uint32_t expected;
  228. uint32_t fraction;
  229. uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
  230. if (!s->rtp_ctx || (count < 1))
  231. return -1;
  232. /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
  233. /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
  234. s->octet_count += count;
  235. rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
  236. RTCP_TX_RATIO_DEN;
  237. rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
  238. if (rtcp_bytes < 28)
  239. return -1;
  240. s->last_octet_count = s->octet_count;
  241. if (avio_open_dyn_buf(&pb) < 0)
  242. return -1;
  243. // Receiver Report
  244. avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
  245. avio_w8(pb, RTCP_RR);
  246. avio_wb16(pb, 7); /* length in words - 1 */
  247. // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
  248. avio_wb32(pb, s->ssrc + 1);
  249. avio_wb32(pb, s->ssrc); // server SSRC
  250. // some placeholders we should really fill...
  251. // RFC 1889/p64
  252. extended_max= stats->cycles + stats->max_seq;
  253. expected= extended_max - stats->base_seq + 1;
  254. lost= expected - stats->received;
  255. lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
  256. expected_interval= expected - stats->expected_prior;
  257. stats->expected_prior= expected;
  258. received_interval= stats->received - stats->received_prior;
  259. stats->received_prior= stats->received;
  260. lost_interval= expected_interval - received_interval;
  261. if (expected_interval==0 || lost_interval<=0) fraction= 0;
  262. else fraction = (lost_interval<<8)/expected_interval;
  263. fraction= (fraction<<24) | lost;
  264. avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
  265. avio_wb32(pb, extended_max); /* max sequence received */
  266. avio_wb32(pb, stats->jitter>>4); /* jitter */
  267. if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
  268. {
  269. avio_wb32(pb, 0); /* last SR timestamp */
  270. avio_wb32(pb, 0); /* delay since last SR */
  271. } else {
  272. uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
  273. uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
  274. avio_wb32(pb, middle_32_bits); /* last SR timestamp */
  275. avio_wb32(pb, delay_since_last); /* delay since last SR */
  276. }
  277. // CNAME
  278. avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
  279. avio_w8(pb, RTCP_SDES);
  280. len = strlen(s->hostname);
  281. avio_wb16(pb, (6 + len + 3) / 4); /* length in words - 1 */
  282. avio_wb32(pb, s->ssrc + 1);
  283. avio_w8(pb, 0x01);
  284. avio_w8(pb, len);
  285. avio_write(pb, s->hostname, len);
  286. // padding
  287. for (len = (6 + len) % 4; len % 4; len++) {
  288. avio_w8(pb, 0);
  289. }
  290. avio_flush(pb);
  291. len = avio_close_dyn_buf(pb, &buf);
  292. if ((len > 0) && buf) {
  293. int av_unused result;
  294. av_dlog(s->ic, "sending %d bytes of RR\n", len);
  295. result= ffurl_write(s->rtp_ctx, buf, len);
  296. av_dlog(s->ic, "result from ffurl_write: %d\n", result);
  297. av_free(buf);
  298. }
  299. return 0;
  300. }
  301. void ff_rtp_send_punch_packets(URLContext* rtp_handle)
  302. {
  303. AVIOContext *pb;
  304. uint8_t *buf;
  305. int len;
  306. /* Send a small RTP packet */
  307. if (avio_open_dyn_buf(&pb) < 0)
  308. return;
  309. avio_w8(pb, (RTP_VERSION << 6));
  310. avio_w8(pb, 0); /* Payload type */
  311. avio_wb16(pb, 0); /* Seq */
  312. avio_wb32(pb, 0); /* Timestamp */
  313. avio_wb32(pb, 0); /* SSRC */
  314. avio_flush(pb);
  315. len = avio_close_dyn_buf(pb, &buf);
  316. if ((len > 0) && buf)
  317. ffurl_write(rtp_handle, buf, len);
  318. av_free(buf);
  319. /* Send a minimal RTCP RR */
  320. if (avio_open_dyn_buf(&pb) < 0)
  321. return;
  322. avio_w8(pb, (RTP_VERSION << 6));
  323. avio_w8(pb, RTCP_RR); /* receiver report */
  324. avio_wb16(pb, 1); /* length in words - 1 */
  325. avio_wb32(pb, 0); /* our own SSRC */
  326. avio_flush(pb);
  327. len = avio_close_dyn_buf(pb, &buf);
  328. if ((len > 0) && buf)
  329. ffurl_write(rtp_handle, buf, len);
  330. av_free(buf);
  331. }
  332. /**
  333. * open a new RTP parse context for stream 'st'. 'st' can be NULL for
  334. * MPEG2TS streams to indicate that they should be demuxed inside the
  335. * rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned)
  336. */
  337. RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size)
  338. {
  339. RTPDemuxContext *s;
  340. s = av_mallocz(sizeof(RTPDemuxContext));
  341. if (!s)
  342. return NULL;
  343. s->payload_type = payload_type;
  344. s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
  345. s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
  346. s->ic = s1;
  347. s->st = st;
  348. s->queue_size = queue_size;
  349. rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
  350. if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
  351. s->ts = ff_mpegts_parse_open(s->ic);
  352. if (s->ts == NULL) {
  353. av_free(s);
  354. return NULL;
  355. }
  356. } else if (st) {
  357. switch(st->codec->codec_id) {
  358. case AV_CODEC_ID_MPEG1VIDEO:
  359. case AV_CODEC_ID_MPEG2VIDEO:
  360. case AV_CODEC_ID_MP2:
  361. case AV_CODEC_ID_MP3:
  362. case AV_CODEC_ID_MPEG4:
  363. case AV_CODEC_ID_H263:
  364. case AV_CODEC_ID_H264:
  365. st->need_parsing = AVSTREAM_PARSE_FULL;
  366. break;
  367. case AV_CODEC_ID_VORBIS:
  368. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  369. break;
  370. case AV_CODEC_ID_ADPCM_G722:
  371. /* According to RFC 3551, the stream clock rate is 8000
  372. * even if the sample rate is 16000. */
  373. if (st->codec->sample_rate == 8000)
  374. st->codec->sample_rate = 16000;
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. // needed to send back RTCP RR in RTSP sessions
  381. s->rtp_ctx = rtpc;
  382. gethostname(s->hostname, sizeof(s->hostname));
  383. return s;
  384. }
  385. void
  386. ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
  387. RTPDynamicProtocolHandler *handler)
  388. {
  389. s->dynamic_protocol_context = ctx;
  390. s->parse_packet = handler->parse_packet;
  391. }
  392. /**
  393. * This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc.
  394. */
  395. static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
  396. {
  397. if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
  398. return; /* Timestamp already set by depacketizer */
  399. if (timestamp == RTP_NOTS_VALUE)
  400. return;
  401. if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
  402. int64_t addend;
  403. int delta_timestamp;
  404. /* compute pts from timestamp with received ntp_time */
  405. delta_timestamp = timestamp - s->last_rtcp_timestamp;
  406. /* convert to the PTS timebase */
  407. addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
  408. pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
  409. delta_timestamp;
  410. return;
  411. }
  412. if (!s->base_timestamp)
  413. s->base_timestamp = timestamp;
  414. /* assume that the difference is INT32_MIN < x < INT32_MAX, but allow the first timestamp to exceed INT32_MAX */
  415. if (!s->timestamp)
  416. s->unwrapped_timestamp += timestamp;
  417. else
  418. s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
  419. s->timestamp = timestamp;
  420. pkt->pts = s->unwrapped_timestamp + s->range_start_offset - s->base_timestamp;
  421. }
  422. static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
  423. const uint8_t *buf, int len)
  424. {
  425. unsigned int ssrc, h;
  426. int payload_type, seq, ret, flags = 0;
  427. int ext;
  428. AVStream *st;
  429. uint32_t timestamp;
  430. int rv= 0;
  431. ext = buf[0] & 0x10;
  432. payload_type = buf[1] & 0x7f;
  433. if (buf[1] & 0x80)
  434. flags |= RTP_FLAG_MARKER;
  435. seq = AV_RB16(buf + 2);
  436. timestamp = AV_RB32(buf + 4);
  437. ssrc = AV_RB32(buf + 8);
  438. /* store the ssrc in the RTPDemuxContext */
  439. s->ssrc = ssrc;
  440. /* NOTE: we can handle only one payload type */
  441. if (s->payload_type != payload_type)
  442. return -1;
  443. st = s->st;
  444. // only do something with this if all the rtp checks pass...
  445. if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
  446. {
  447. av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
  448. payload_type, seq, ((s->seq + 1) & 0xffff));
  449. return -1;
  450. }
  451. if (buf[0] & 0x20) {
  452. int padding = buf[len - 1];
  453. if (len >= 12 + padding)
  454. len -= padding;
  455. }
  456. s->seq = seq;
  457. len -= 12;
  458. buf += 12;
  459. /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
  460. if (ext) {
  461. if (len < 4)
  462. return -1;
  463. /* calculate the header extension length (stored as number
  464. * of 32-bit words) */
  465. ext = (AV_RB16(buf + 2) + 1) << 2;
  466. if (len < ext)
  467. return -1;
  468. // skip past RTP header extension
  469. len -= ext;
  470. buf += ext;
  471. }
  472. if (!st) {
  473. /* specific MPEG2TS demux support */
  474. ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
  475. /* The only error that can be returned from ff_mpegts_parse_packet
  476. * is "no more data to return from the provided buffer", so return
  477. * AVERROR(EAGAIN) for all errors */
  478. if (ret < 0)
  479. return AVERROR(EAGAIN);
  480. if (ret < len) {
  481. s->read_buf_size = len - ret;
  482. memcpy(s->buf, buf + ret, s->read_buf_size);
  483. s->read_buf_index = 0;
  484. return 1;
  485. }
  486. return 0;
  487. } else if (s->parse_packet) {
  488. rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
  489. s->st, pkt, &timestamp, buf, len, flags);
  490. } else {
  491. // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
  492. switch(st->codec->codec_id) {
  493. case AV_CODEC_ID_MP2:
  494. case AV_CODEC_ID_MP3:
  495. /* better than nothing: skip mpeg audio RTP header */
  496. if (len <= 4)
  497. return -1;
  498. h = AV_RB32(buf);
  499. len -= 4;
  500. buf += 4;
  501. if (av_new_packet(pkt, len) < 0)
  502. return AVERROR(ENOMEM);
  503. memcpy(pkt->data, buf, len);
  504. break;
  505. case AV_CODEC_ID_MPEG1VIDEO:
  506. case AV_CODEC_ID_MPEG2VIDEO:
  507. /* better than nothing: skip mpeg video RTP header */
  508. if (len <= 4)
  509. return -1;
  510. h = AV_RB32(buf);
  511. buf += 4;
  512. len -= 4;
  513. if (h & (1 << 26)) {
  514. /* mpeg2 */
  515. if (len <= 4)
  516. return -1;
  517. buf += 4;
  518. len -= 4;
  519. }
  520. if (av_new_packet(pkt, len) < 0)
  521. return AVERROR(ENOMEM);
  522. memcpy(pkt->data, buf, len);
  523. break;
  524. default:
  525. if (av_new_packet(pkt, len) < 0)
  526. return AVERROR(ENOMEM);
  527. memcpy(pkt->data, buf, len);
  528. break;
  529. }
  530. pkt->stream_index = st->index;
  531. }
  532. // now perform timestamp things....
  533. finalize_packet(s, pkt, timestamp);
  534. return rv;
  535. }
  536. void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
  537. {
  538. while (s->queue) {
  539. RTPPacket *next = s->queue->next;
  540. av_free(s->queue->buf);
  541. av_free(s->queue);
  542. s->queue = next;
  543. }
  544. s->seq = 0;
  545. s->queue_len = 0;
  546. s->prev_ret = 0;
  547. }
  548. static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
  549. {
  550. uint16_t seq = AV_RB16(buf + 2);
  551. RTPPacket *cur = s->queue, *prev = NULL, *packet;
  552. /* Find the correct place in the queue to insert the packet */
  553. while (cur) {
  554. int16_t diff = seq - cur->seq;
  555. if (diff < 0)
  556. break;
  557. prev = cur;
  558. cur = cur->next;
  559. }
  560. packet = av_mallocz(sizeof(*packet));
  561. if (!packet)
  562. return;
  563. packet->recvtime = av_gettime();
  564. packet->seq = seq;
  565. packet->len = len;
  566. packet->buf = buf;
  567. packet->next = cur;
  568. if (prev)
  569. prev->next = packet;
  570. else
  571. s->queue = packet;
  572. s->queue_len++;
  573. }
  574. static int has_next_packet(RTPDemuxContext *s)
  575. {
  576. return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
  577. }
  578. int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
  579. {
  580. return s->queue ? s->queue->recvtime : 0;
  581. }
  582. static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
  583. {
  584. int rv;
  585. RTPPacket *next;
  586. if (s->queue_len <= 0)
  587. return -1;
  588. if (!has_next_packet(s))
  589. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  590. "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
  591. /* Parse the first packet in the queue, and dequeue it */
  592. rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
  593. next = s->queue->next;
  594. av_free(s->queue->buf);
  595. av_free(s->queue);
  596. s->queue = next;
  597. s->queue_len--;
  598. return rv;
  599. }
  600. static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
  601. uint8_t **bufptr, int len)
  602. {
  603. uint8_t* buf = bufptr ? *bufptr : NULL;
  604. int ret, flags = 0;
  605. uint32_t timestamp;
  606. int rv= 0;
  607. if (!buf) {
  608. /* If parsing of the previous packet actually returned 0 or an error,
  609. * there's nothing more to be parsed from that packet, but we may have
  610. * indicated that we can return the next enqueued packet. */
  611. if (s->prev_ret <= 0)
  612. return rtp_parse_queued_packet(s, pkt);
  613. /* return the next packets, if any */
  614. if(s->st && s->parse_packet) {
  615. /* timestamp should be overwritten by parse_packet, if not,
  616. * the packet is left with pts == AV_NOPTS_VALUE */
  617. timestamp = RTP_NOTS_VALUE;
  618. rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
  619. s->st, pkt, &timestamp, NULL, 0, flags);
  620. finalize_packet(s, pkt, timestamp);
  621. return rv;
  622. } else {
  623. // TODO: Move to a dynamic packet handler (like above)
  624. if (s->read_buf_index >= s->read_buf_size)
  625. return AVERROR(EAGAIN);
  626. ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
  627. s->read_buf_size - s->read_buf_index);
  628. if (ret < 0)
  629. return AVERROR(EAGAIN);
  630. s->read_buf_index += ret;
  631. if (s->read_buf_index < s->read_buf_size)
  632. return 1;
  633. else
  634. return 0;
  635. }
  636. }
  637. if (len < 12)
  638. return -1;
  639. if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
  640. return -1;
  641. if (RTP_PT_IS_RTCP(buf[1])) {
  642. return rtcp_parse_packet(s, buf, len);
  643. }
  644. if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
  645. /* First packet, or no reordering */
  646. return rtp_parse_packet_internal(s, pkt, buf, len);
  647. } else {
  648. uint16_t seq = AV_RB16(buf + 2);
  649. int16_t diff = seq - s->seq;
  650. if (diff < 0) {
  651. /* Packet older than the previously emitted one, drop */
  652. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  653. "RTP: dropping old packet received too late\n");
  654. return -1;
  655. } else if (diff <= 1) {
  656. /* Correct packet */
  657. rv = rtp_parse_packet_internal(s, pkt, buf, len);
  658. return rv;
  659. } else {
  660. /* Still missing some packet, enqueue this one. */
  661. enqueue_packet(s, buf, len);
  662. *bufptr = NULL;
  663. /* Return the first enqueued packet if the queue is full,
  664. * even if we're missing something */
  665. if (s->queue_len >= s->queue_size)
  666. return rtp_parse_queued_packet(s, pkt);
  667. return -1;
  668. }
  669. }
  670. }
  671. /**
  672. * Parse an RTP or RTCP packet directly sent as a buffer.
  673. * @param s RTP parse context.
  674. * @param pkt returned packet
  675. * @param bufptr pointer to the input buffer or NULL to read the next packets
  676. * @param len buffer len
  677. * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
  678. * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
  679. */
  680. int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  681. uint8_t **bufptr, int len)
  682. {
  683. int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
  684. s->prev_ret = rv;
  685. while (rv == AVERROR(EAGAIN) && has_next_packet(s))
  686. rv = rtp_parse_queued_packet(s, pkt);
  687. return rv ? rv : has_next_packet(s);
  688. }
  689. void ff_rtp_parse_close(RTPDemuxContext *s)
  690. {
  691. ff_rtp_reset_packet_queue(s);
  692. if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
  693. ff_mpegts_parse_close(s->ts);
  694. }
  695. av_free(s);
  696. }
  697. int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
  698. int (*parse_fmtp)(AVStream *stream,
  699. PayloadContext *data,
  700. char *attr, char *value))
  701. {
  702. char attr[256];
  703. char *value;
  704. int res;
  705. int value_size = strlen(p) + 1;
  706. if (!(value = av_malloc(value_size))) {
  707. av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
  708. return AVERROR(ENOMEM);
  709. }
  710. // remove protocol identifier
  711. while (*p && *p == ' ') p++; // strip spaces
  712. while (*p && *p != ' ') p++; // eat protocol identifier
  713. while (*p && *p == ' ') p++; // strip trailing spaces
  714. while (ff_rtsp_next_attr_and_value(&p,
  715. attr, sizeof(attr),
  716. value, value_size)) {
  717. res = parse_fmtp(stream, data, attr, value);
  718. if (res < 0 && res != AVERROR_PATCHWELCOME) {
  719. av_free(value);
  720. return res;
  721. }
  722. }
  723. av_free(value);
  724. return 0;
  725. }