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.

782 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. av_set_pts_info(st, 32, 1, 90000);
  352. switch(st->codec->codec_id) {
  353. case CODEC_ID_MPEG1VIDEO:
  354. case CODEC_ID_MPEG2VIDEO:
  355. case CODEC_ID_MP2:
  356. case CODEC_ID_MP3:
  357. case CODEC_ID_MPEG4:
  358. case CODEC_ID_H263:
  359. case CODEC_ID_H264:
  360. st->need_parsing = AVSTREAM_PARSE_FULL;
  361. break;
  362. case CODEC_ID_ADPCM_G722:
  363. /* According to RFC 3551, the stream clock rate is 8000
  364. * even if the sample rate is 16000. */
  365. if (st->codec->sample_rate == 8000)
  366. st->codec->sample_rate = 16000;
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. // needed to send back RTCP RR in RTSP sessions
  373. s->rtp_ctx = rtpc;
  374. gethostname(s->hostname, sizeof(s->hostname));
  375. return s;
  376. }
  377. void
  378. rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
  379. RTPDynamicProtocolHandler *handler)
  380. {
  381. s->dynamic_protocol_context = ctx;
  382. s->parse_packet = handler->parse_packet;
  383. }
  384. /**
  385. * This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc.
  386. */
  387. static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
  388. {
  389. if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && timestamp != RTP_NOTS_VALUE) {
  390. int64_t addend;
  391. int delta_timestamp;
  392. /* compute pts from timestamp with received ntp_time */
  393. delta_timestamp = timestamp - s->last_rtcp_timestamp;
  394. /* convert to the PTS timebase */
  395. 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);
  396. pkt->pts = s->range_start_offset + addend + delta_timestamp;
  397. }
  398. }
  399. static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
  400. const uint8_t *buf, int len)
  401. {
  402. unsigned int ssrc, h;
  403. int payload_type, seq, ret, flags = 0;
  404. int ext;
  405. AVStream *st;
  406. uint32_t timestamp;
  407. int rv= 0;
  408. ext = buf[0] & 0x10;
  409. payload_type = buf[1] & 0x7f;
  410. if (buf[1] & 0x80)
  411. flags |= RTP_FLAG_MARKER;
  412. seq = AV_RB16(buf + 2);
  413. timestamp = AV_RB32(buf + 4);
  414. ssrc = AV_RB32(buf + 8);
  415. /* store the ssrc in the RTPDemuxContext */
  416. s->ssrc = ssrc;
  417. /* NOTE: we can handle only one payload type */
  418. if (s->payload_type != payload_type)
  419. return -1;
  420. st = s->st;
  421. // only do something with this if all the rtp checks pass...
  422. if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
  423. {
  424. av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
  425. payload_type, seq, ((s->seq + 1) & 0xffff));
  426. return -1;
  427. }
  428. if (buf[0] & 0x20) {
  429. int padding = buf[len - 1];
  430. if (len >= 12 + padding)
  431. len -= padding;
  432. }
  433. s->seq = seq;
  434. len -= 12;
  435. buf += 12;
  436. /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
  437. if (ext) {
  438. if (len < 4)
  439. return -1;
  440. /* calculate the header extension length (stored as number
  441. * of 32-bit words) */
  442. ext = (AV_RB16(buf + 2) + 1) << 2;
  443. if (len < ext)
  444. return -1;
  445. // skip past RTP header extension
  446. len -= ext;
  447. buf += ext;
  448. }
  449. if (!st) {
  450. /* specific MPEG2TS demux support */
  451. ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
  452. /* The only error that can be returned from ff_mpegts_parse_packet
  453. * is "no more data to return from the provided buffer", so return
  454. * AVERROR(EAGAIN) for all errors */
  455. if (ret < 0)
  456. return AVERROR(EAGAIN);
  457. if (ret < len) {
  458. s->read_buf_size = len - ret;
  459. memcpy(s->buf, buf + ret, s->read_buf_size);
  460. s->read_buf_index = 0;
  461. return 1;
  462. }
  463. return 0;
  464. } else if (s->parse_packet) {
  465. rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
  466. s->st, pkt, &timestamp, buf, len, flags);
  467. } else {
  468. // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
  469. switch(st->codec->codec_id) {
  470. case CODEC_ID_MP2:
  471. case CODEC_ID_MP3:
  472. /* better than nothing: skip mpeg audio RTP header */
  473. if (len <= 4)
  474. return -1;
  475. h = AV_RB32(buf);
  476. len -= 4;
  477. buf += 4;
  478. av_new_packet(pkt, len);
  479. memcpy(pkt->data, buf, len);
  480. break;
  481. case CODEC_ID_MPEG1VIDEO:
  482. case CODEC_ID_MPEG2VIDEO:
  483. /* better than nothing: skip mpeg video RTP header */
  484. if (len <= 4)
  485. return -1;
  486. h = AV_RB32(buf);
  487. buf += 4;
  488. len -= 4;
  489. if (h & (1 << 26)) {
  490. /* mpeg2 */
  491. if (len <= 4)
  492. return -1;
  493. buf += 4;
  494. len -= 4;
  495. }
  496. av_new_packet(pkt, len);
  497. memcpy(pkt->data, buf, len);
  498. break;
  499. default:
  500. av_new_packet(pkt, len);
  501. memcpy(pkt->data, buf, len);
  502. break;
  503. }
  504. pkt->stream_index = st->index;
  505. }
  506. // now perform timestamp things....
  507. finalize_packet(s, pkt, timestamp);
  508. return rv;
  509. }
  510. void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
  511. {
  512. while (s->queue) {
  513. RTPPacket *next = s->queue->next;
  514. av_free(s->queue->buf);
  515. av_free(s->queue);
  516. s->queue = next;
  517. }
  518. s->seq = 0;
  519. s->queue_len = 0;
  520. s->prev_ret = 0;
  521. }
  522. static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
  523. {
  524. uint16_t seq = AV_RB16(buf + 2);
  525. RTPPacket *cur = s->queue, *prev = NULL, *packet;
  526. /* Find the correct place in the queue to insert the packet */
  527. while (cur) {
  528. int16_t diff = seq - cur->seq;
  529. if (diff < 0)
  530. break;
  531. prev = cur;
  532. cur = cur->next;
  533. }
  534. packet = av_mallocz(sizeof(*packet));
  535. if (!packet)
  536. return;
  537. packet->recvtime = av_gettime();
  538. packet->seq = seq;
  539. packet->len = len;
  540. packet->buf = buf;
  541. packet->next = cur;
  542. if (prev)
  543. prev->next = packet;
  544. else
  545. s->queue = packet;
  546. s->queue_len++;
  547. }
  548. static int has_next_packet(RTPDemuxContext *s)
  549. {
  550. return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
  551. }
  552. int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
  553. {
  554. return s->queue ? s->queue->recvtime : 0;
  555. }
  556. static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
  557. {
  558. int rv;
  559. RTPPacket *next;
  560. if (s->queue_len <= 0)
  561. return -1;
  562. if (!has_next_packet(s))
  563. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  564. "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
  565. /* Parse the first packet in the queue, and dequeue it */
  566. rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
  567. next = s->queue->next;
  568. av_free(s->queue->buf);
  569. av_free(s->queue);
  570. s->queue = next;
  571. s->queue_len--;
  572. return rv;
  573. }
  574. static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
  575. uint8_t **bufptr, int len)
  576. {
  577. uint8_t* buf = bufptr ? *bufptr : NULL;
  578. int ret, flags = 0;
  579. uint32_t timestamp;
  580. int rv= 0;
  581. if (!buf) {
  582. /* If parsing of the previous packet actually returned 0 or an error,
  583. * there's nothing more to be parsed from that packet, but we may have
  584. * indicated that we can return the next enqueued packet. */
  585. if (s->prev_ret <= 0)
  586. return rtp_parse_queued_packet(s, pkt);
  587. /* return the next packets, if any */
  588. if(s->st && s->parse_packet) {
  589. /* timestamp should be overwritten by parse_packet, if not,
  590. * the packet is left with pts == AV_NOPTS_VALUE */
  591. timestamp = RTP_NOTS_VALUE;
  592. rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
  593. s->st, pkt, &timestamp, NULL, 0, flags);
  594. finalize_packet(s, pkt, timestamp);
  595. return rv;
  596. } else {
  597. // TODO: Move to a dynamic packet handler (like above)
  598. if (s->read_buf_index >= s->read_buf_size)
  599. return AVERROR(EAGAIN);
  600. ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
  601. s->read_buf_size - s->read_buf_index);
  602. if (ret < 0)
  603. return AVERROR(EAGAIN);
  604. s->read_buf_index += ret;
  605. if (s->read_buf_index < s->read_buf_size)
  606. return 1;
  607. else
  608. return 0;
  609. }
  610. }
  611. if (len < 12)
  612. return -1;
  613. if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
  614. return -1;
  615. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  616. return rtcp_parse_packet(s, buf, len);
  617. }
  618. if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
  619. /* First packet, or no reordering */
  620. return rtp_parse_packet_internal(s, pkt, buf, len);
  621. } else {
  622. uint16_t seq = AV_RB16(buf + 2);
  623. int16_t diff = seq - s->seq;
  624. if (diff < 0) {
  625. /* Packet older than the previously emitted one, drop */
  626. av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
  627. "RTP: dropping old packet received too late\n");
  628. return -1;
  629. } else if (diff <= 1) {
  630. /* Correct packet */
  631. rv = rtp_parse_packet_internal(s, pkt, buf, len);
  632. return rv;
  633. } else {
  634. /* Still missing some packet, enqueue this one. */
  635. enqueue_packet(s, buf, len);
  636. *bufptr = NULL;
  637. /* Return the first enqueued packet if the queue is full,
  638. * even if we're missing something */
  639. if (s->queue_len >= s->queue_size)
  640. return rtp_parse_queued_packet(s, pkt);
  641. return -1;
  642. }
  643. }
  644. }
  645. /**
  646. * Parse an RTP or RTCP packet directly sent as a buffer.
  647. * @param s RTP parse context.
  648. * @param pkt returned packet
  649. * @param bufptr pointer to the input buffer or NULL to read the next packets
  650. * @param len buffer len
  651. * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
  652. * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
  653. */
  654. int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  655. uint8_t **bufptr, int len)
  656. {
  657. int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
  658. s->prev_ret = rv;
  659. while (rv == AVERROR(EAGAIN) && has_next_packet(s))
  660. rv = rtp_parse_queued_packet(s, pkt);
  661. return rv ? rv : has_next_packet(s);
  662. }
  663. void rtp_parse_close(RTPDemuxContext *s)
  664. {
  665. ff_rtp_reset_packet_queue(s);
  666. if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
  667. ff_mpegts_parse_close(s->ts);
  668. }
  669. av_free(s);
  670. }
  671. int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
  672. int (*parse_fmtp)(AVStream *stream,
  673. PayloadContext *data,
  674. char *attr, char *value))
  675. {
  676. char attr[256];
  677. char *value;
  678. int res;
  679. int value_size = strlen(p) + 1;
  680. if (!(value = av_malloc(value_size))) {
  681. av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
  682. return AVERROR(ENOMEM);
  683. }
  684. // remove protocol identifier
  685. while (*p && *p == ' ') p++; // strip spaces
  686. while (*p && *p != ' ') p++; // eat protocol identifier
  687. while (*p && *p == ' ') p++; // strip trailing spaces
  688. while (ff_rtsp_next_attr_and_value(&p,
  689. attr, sizeof(attr),
  690. value, value_size)) {
  691. res = parse_fmtp(stream, data, attr, value);
  692. if (res < 0 && res != AVERROR_PATCHWELCOME) {
  693. av_free(value);
  694. return res;
  695. }
  696. }
  697. av_free(value);
  698. return 0;
  699. }