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.

716 lines
22KB

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