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.

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