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.

587 lines
19KB

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