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.

742 lines
23KB

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