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.

747 lines
24KB

  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. /* The only error that can be returned from ff_mpegts_parse_packet
  422. * is "no more data to return from the provided buffer", so return
  423. * AVERROR(EAGAIN) for all errors */
  424. if (ret < 0)
  425. return AVERROR(EAGAIN);
  426. if (ret < len) {
  427. s->read_buf_size = len - ret;
  428. memcpy(s->buf, buf + ret, s->read_buf_size);
  429. s->read_buf_index = 0;
  430. return 1;
  431. }
  432. return 0;
  433. } else if (s->parse_packet) {
  434. rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
  435. s->st, pkt, &timestamp, buf, len, flags);
  436. } else {
  437. // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
  438. switch(st->codec->codec_id) {
  439. case CODEC_ID_MP2:
  440. case CODEC_ID_MP3:
  441. /* better than nothing: skip mpeg audio RTP header */
  442. if (len <= 4)
  443. return -1;
  444. h = AV_RB32(buf);
  445. len -= 4;
  446. buf += 4;
  447. av_new_packet(pkt, len);
  448. memcpy(pkt->data, buf, len);
  449. break;
  450. case CODEC_ID_MPEG1VIDEO:
  451. case CODEC_ID_MPEG2VIDEO:
  452. /* better than nothing: skip mpeg video RTP header */
  453. if (len <= 4)
  454. return -1;
  455. h = AV_RB32(buf);
  456. buf += 4;
  457. len -= 4;
  458. if (h & (1 << 26)) {
  459. /* mpeg2 */
  460. if (len <= 4)
  461. return -1;
  462. buf += 4;
  463. len -= 4;
  464. }
  465. av_new_packet(pkt, len);
  466. memcpy(pkt->data, buf, len);
  467. break;
  468. default:
  469. av_new_packet(pkt, len);
  470. memcpy(pkt->data, buf, len);
  471. break;
  472. }
  473. pkt->stream_index = st->index;
  474. }
  475. // now perform timestamp things....
  476. finalize_packet(s, pkt, timestamp);
  477. return rv;
  478. }
  479. void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
  480. {
  481. while (s->queue) {
  482. RTPPacket *next = s->queue->next;
  483. av_free(s->queue->buf);
  484. av_free(s->queue);
  485. s->queue = next;
  486. }
  487. s->seq = 0;
  488. s->queue_len = 0;
  489. s->prev_ret = 0;
  490. }
  491. static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
  492. {
  493. uint16_t seq = AV_RB16(buf + 2);
  494. RTPPacket *cur = s->queue, *prev = NULL, *packet;
  495. /* Find the correct place in the queue to insert the packet */
  496. while (cur) {
  497. int16_t diff = seq - cur->seq;
  498. if (diff < 0)
  499. break;
  500. prev = cur;
  501. cur = cur->next;
  502. }
  503. packet = av_mallocz(sizeof(*packet));
  504. if (!packet)
  505. return;
  506. packet->recvtime = av_gettime();
  507. packet->seq = seq;
  508. packet->len = len;
  509. packet->buf = buf;
  510. packet->next = cur;
  511. if (prev)
  512. prev->next = packet;
  513. else
  514. s->queue = packet;
  515. s->queue_len++;
  516. }
  517. static int has_next_packet(RTPDemuxContext *s)
  518. {
  519. return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
  520. }
  521. int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
  522. {
  523. return s->queue ? s->queue->recvtime : 0;
  524. }
  525. static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
  526. {
  527. int rv;
  528. RTPPacket *next;
  529. if (s->queue_len <= 0)
  530. return -1;
  531. if (!has_next_packet(s))
  532. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  533. "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
  534. /* Parse the first packet in the queue, and dequeue it */
  535. rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
  536. next = s->queue->next;
  537. av_free(s->queue->buf);
  538. av_free(s->queue);
  539. s->queue = next;
  540. s->queue_len--;
  541. return rv;
  542. }
  543. static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
  544. uint8_t **bufptr, int len)
  545. {
  546. uint8_t* buf = bufptr ? *bufptr : NULL;
  547. int ret, flags = 0;
  548. uint32_t timestamp;
  549. int rv= 0;
  550. if (!buf) {
  551. /* If parsing of the previous packet actually returned 0 or an error,
  552. * there's nothing more to be parsed from that packet, but we may have
  553. * indicated that we can return the next enqueued packet. */
  554. if (s->prev_ret <= 0)
  555. return rtp_parse_queued_packet(s, pkt);
  556. /* return the next packets, if any */
  557. if(s->st && s->parse_packet) {
  558. /* timestamp should be overwritten by parse_packet, if not,
  559. * the packet is left with pts == AV_NOPTS_VALUE */
  560. timestamp = RTP_NOTS_VALUE;
  561. rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
  562. s->st, pkt, &timestamp, NULL, 0, flags);
  563. finalize_packet(s, pkt, timestamp);
  564. return rv;
  565. } else {
  566. // TODO: Move to a dynamic packet handler (like above)
  567. if (s->read_buf_index >= s->read_buf_size)
  568. return AVERROR(EAGAIN);
  569. ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
  570. s->read_buf_size - s->read_buf_index);
  571. if (ret < 0)
  572. return AVERROR(EAGAIN);
  573. s->read_buf_index += ret;
  574. if (s->read_buf_index < s->read_buf_size)
  575. return 1;
  576. else
  577. return 0;
  578. }
  579. }
  580. if (len < 12)
  581. return -1;
  582. if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
  583. return -1;
  584. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  585. return rtcp_parse_packet(s, buf, len);
  586. }
  587. if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
  588. /* First packet, or no reordering */
  589. return rtp_parse_packet_internal(s, pkt, buf, len);
  590. } else {
  591. uint16_t seq = AV_RB16(buf + 2);
  592. int16_t diff = seq - s->seq;
  593. if (diff < 0) {
  594. /* Packet older than the previously emitted one, drop */
  595. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  596. "RTP: dropping old packet received too late\n");
  597. return -1;
  598. } else if (diff <= 1) {
  599. /* Correct packet */
  600. rv = rtp_parse_packet_internal(s, pkt, buf, len);
  601. return rv;
  602. } else {
  603. /* Still missing some packet, enqueue this one. */
  604. enqueue_packet(s, buf, len);
  605. *bufptr = NULL;
  606. /* Return the first enqueued packet if the queue is full,
  607. * even if we're missing something */
  608. if (s->queue_len >= s->queue_size)
  609. return rtp_parse_queued_packet(s, pkt);
  610. return -1;
  611. }
  612. }
  613. }
  614. /**
  615. * Parse an RTP or RTCP packet directly sent as a buffer.
  616. * @param s RTP parse context.
  617. * @param pkt returned packet
  618. * @param bufptr pointer to the input buffer or NULL to read the next packets
  619. * @param len buffer len
  620. * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
  621. * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
  622. */
  623. int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  624. uint8_t **bufptr, int len)
  625. {
  626. int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
  627. s->prev_ret = rv;
  628. while (rv == AVERROR(EAGAIN) && has_next_packet(s))
  629. rv = rtp_parse_queued_packet(s, pkt);
  630. return rv ? rv : has_next_packet(s);
  631. }
  632. void rtp_parse_close(RTPDemuxContext *s)
  633. {
  634. ff_rtp_reset_packet_queue(s);
  635. if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
  636. ff_mpegts_parse_close(s->ts);
  637. }
  638. av_free(s);
  639. }
  640. int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
  641. int (*parse_fmtp)(AVStream *stream,
  642. PayloadContext *data,
  643. char *attr, char *value))
  644. {
  645. char attr[256];
  646. char *value;
  647. int res;
  648. int value_size = strlen(p) + 1;
  649. if (!(value = av_malloc(value_size))) {
  650. av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
  651. return AVERROR(ENOMEM);
  652. }
  653. // remove protocol identifier
  654. while (*p && *p == ' ') p++; // strip spaces
  655. while (*p && *p != ' ') p++; // eat protocol identifier
  656. while (*p && *p == ' ') p++; // strip trailing spaces
  657. while (ff_rtsp_next_attr_and_value(&p,
  658. attr, sizeof(attr),
  659. value, value_size)) {
  660. res = parse_fmtp(stream, data, attr, value);
  661. if (res < 0 && res != AVERROR_PATCHWELCOME) {
  662. av_free(value);
  663. return res;
  664. }
  665. }
  666. av_free(value);
  667. return 0;
  668. }