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.

781 lines
25KB

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