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.

739 lines
20KB

  1. /*
  2. * RTP input/output format
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "mpegts.h"
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #ifndef __BEOS__
  26. # include <arpa/inet.h>
  27. #else
  28. # include "barpainet.h"
  29. #endif
  30. #include <netdb.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. #define RTP_VERSION 2
  40. #define RTP_MAX_SDES 256 /* maximum text length for SDES */
  41. /* RTCP paquets use 0.5 % of the bandwidth */
  42. #define RTCP_TX_RATIO_NUM 5
  43. #define RTCP_TX_RATIO_DEN 1000
  44. typedef enum {
  45. RTCP_SR = 200,
  46. RTCP_RR = 201,
  47. RTCP_SDES = 202,
  48. RTCP_BYE = 203,
  49. RTCP_APP = 204
  50. } rtcp_type_t;
  51. typedef enum {
  52. RTCP_SDES_END = 0,
  53. RTCP_SDES_CNAME = 1,
  54. RTCP_SDES_NAME = 2,
  55. RTCP_SDES_EMAIL = 3,
  56. RTCP_SDES_PHONE = 4,
  57. RTCP_SDES_LOC = 5,
  58. RTCP_SDES_TOOL = 6,
  59. RTCP_SDES_NOTE = 7,
  60. RTCP_SDES_PRIV = 8,
  61. RTCP_SDES_IMG = 9,
  62. RTCP_SDES_DOOR = 10,
  63. RTCP_SDES_SOURCE = 11
  64. } rtcp_sdes_type_t;
  65. struct RTPDemuxContext {
  66. AVFormatContext *ic;
  67. AVStream *st;
  68. int payload_type;
  69. uint32_t ssrc;
  70. uint16_t seq;
  71. uint32_t timestamp;
  72. uint32_t base_timestamp;
  73. uint32_t cur_timestamp;
  74. int max_payload_size;
  75. MpegTSContext *ts; /* only used for RTP_PT_MPEG2TS payloads */
  76. int read_buf_index;
  77. int read_buf_size;
  78. /* rtcp sender statistics receive */
  79. int64_t last_rtcp_ntp_time;
  80. int64_t first_rtcp_ntp_time;
  81. uint32_t last_rtcp_timestamp;
  82. /* rtcp sender statistics */
  83. unsigned int packet_count;
  84. unsigned int octet_count;
  85. unsigned int last_octet_count;
  86. int first_packet;
  87. /* buffer for output */
  88. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  89. uint8_t *buf_ptr;
  90. };
  91. int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
  92. {
  93. switch(payload_type) {
  94. case RTP_PT_ULAW:
  95. codec->codec_type = CODEC_TYPE_AUDIO;
  96. codec->codec_id = CODEC_ID_PCM_MULAW;
  97. codec->channels = 1;
  98. codec->sample_rate = 8000;
  99. break;
  100. case RTP_PT_ALAW:
  101. codec->codec_type = CODEC_TYPE_AUDIO;
  102. codec->codec_id = CODEC_ID_PCM_ALAW;
  103. codec->channels = 1;
  104. codec->sample_rate = 8000;
  105. break;
  106. case RTP_PT_S16BE_STEREO:
  107. codec->codec_type = CODEC_TYPE_AUDIO;
  108. codec->codec_id = CODEC_ID_PCM_S16BE;
  109. codec->channels = 2;
  110. codec->sample_rate = 44100;
  111. break;
  112. case RTP_PT_S16BE_MONO:
  113. codec->codec_type = CODEC_TYPE_AUDIO;
  114. codec->codec_id = CODEC_ID_PCM_S16BE;
  115. codec->channels = 1;
  116. codec->sample_rate = 44100;
  117. break;
  118. case RTP_PT_MPEGAUDIO:
  119. codec->codec_type = CODEC_TYPE_AUDIO;
  120. codec->codec_id = CODEC_ID_MP2;
  121. break;
  122. case RTP_PT_JPEG:
  123. codec->codec_type = CODEC_TYPE_VIDEO;
  124. codec->codec_id = CODEC_ID_MJPEG;
  125. break;
  126. case RTP_PT_MPEGVIDEO:
  127. codec->codec_type = CODEC_TYPE_VIDEO;
  128. codec->codec_id = CODEC_ID_MPEG1VIDEO;
  129. break;
  130. case RTP_PT_MPEG2TS:
  131. codec->codec_type = CODEC_TYPE_DATA;
  132. codec->codec_id = CODEC_ID_MPEG2TS;
  133. break;
  134. default:
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. /* return < 0 if unknown payload type */
  140. int rtp_get_payload_type(AVCodecContext *codec)
  141. {
  142. int payload_type;
  143. /* compute the payload type */
  144. payload_type = -1;
  145. switch(codec->codec_id) {
  146. case CODEC_ID_PCM_MULAW:
  147. payload_type = RTP_PT_ULAW;
  148. break;
  149. case CODEC_ID_PCM_ALAW:
  150. payload_type = RTP_PT_ALAW;
  151. break;
  152. case CODEC_ID_PCM_S16BE:
  153. if (codec->channels == 1) {
  154. payload_type = RTP_PT_S16BE_MONO;
  155. } else if (codec->channels == 2) {
  156. payload_type = RTP_PT_S16BE_STEREO;
  157. }
  158. break;
  159. case CODEC_ID_MP2:
  160. case CODEC_ID_MP3:
  161. payload_type = RTP_PT_MPEGAUDIO;
  162. break;
  163. case CODEC_ID_MJPEG:
  164. payload_type = RTP_PT_JPEG;
  165. break;
  166. case CODEC_ID_MPEG1VIDEO:
  167. payload_type = RTP_PT_MPEGVIDEO;
  168. break;
  169. case CODEC_ID_MPEG2TS:
  170. payload_type = RTP_PT_MPEG2TS;
  171. break;
  172. default:
  173. break;
  174. }
  175. return payload_type;
  176. }
  177. static inline uint32_t decode_be32(const uint8_t *p)
  178. {
  179. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  180. }
  181. static inline uint64_t decode_be64(const uint8_t *p)
  182. {
  183. return ((uint64_t)decode_be32(p) << 32) | decode_be32(p + 4);
  184. }
  185. static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
  186. {
  187. if (buf[1] != 200)
  188. return -1;
  189. s->last_rtcp_ntp_time = decode_be64(buf + 8);
  190. if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
  191. s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
  192. s->last_rtcp_timestamp = decode_be32(buf + 16);
  193. return 0;
  194. }
  195. /**
  196. * open a new RTP parse context for stream 'st'. 'st' can be NULL for
  197. * MPEG2TS streams to indicate that they should be demuxed inside the
  198. * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
  199. */
  200. RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type)
  201. {
  202. RTPDemuxContext *s;
  203. s = av_mallocz(sizeof(RTPDemuxContext));
  204. if (!s)
  205. return NULL;
  206. s->payload_type = payload_type;
  207. s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
  208. s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
  209. s->ic = s1;
  210. s->st = st;
  211. if (payload_type == RTP_PT_MPEG2TS) {
  212. s->ts = mpegts_parse_open(s->ic);
  213. if (s->ts == NULL) {
  214. av_free(s);
  215. return NULL;
  216. }
  217. }
  218. return s;
  219. }
  220. /**
  221. * Parse an RTP or RTCP packet directly sent as a buffer.
  222. * @param s RTP parse context.
  223. * @param pkt returned packet
  224. * @param buf input buffer or NULL to read the next packets
  225. * @param len buffer len
  226. * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
  227. * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
  228. */
  229. int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  230. const uint8_t *buf, int len)
  231. {
  232. unsigned int ssrc, h;
  233. int payload_type, seq, delta_timestamp, ret;
  234. AVStream *st;
  235. uint32_t timestamp;
  236. if (!buf) {
  237. /* return the next packets, if any */
  238. if (s->read_buf_index >= s->read_buf_size)
  239. return -1;
  240. ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
  241. s->read_buf_size - s->read_buf_index);
  242. if (ret < 0)
  243. return -1;
  244. s->read_buf_index += ret;
  245. if (s->read_buf_index < s->read_buf_size)
  246. return 1;
  247. else
  248. return 0;
  249. }
  250. if (len < 12)
  251. return -1;
  252. if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
  253. return -1;
  254. if (buf[1] >= 200 && buf[1] <= 204) {
  255. rtcp_parse_packet(s, buf, len);
  256. return -1;
  257. }
  258. payload_type = buf[1] & 0x7f;
  259. seq = (buf[2] << 8) | buf[3];
  260. timestamp = decode_be32(buf + 4);
  261. ssrc = decode_be32(buf + 8);
  262. /* NOTE: we can handle only one payload type */
  263. if (s->payload_type != payload_type)
  264. return -1;
  265. #if defined(DEBUG) || 1
  266. if (seq != ((s->seq + 1) & 0xffff)) {
  267. printf("RTP: PT=%02x: bad cseq %04x expected=%04x\n",
  268. payload_type, seq, ((s->seq + 1) & 0xffff));
  269. }
  270. s->seq = seq;
  271. #endif
  272. len -= 12;
  273. buf += 12;
  274. st = s->st;
  275. if (!st) {
  276. /* specific MPEG2TS demux support */
  277. ret = mpegts_parse_packet(s->ts, pkt, buf, len);
  278. if (ret < 0)
  279. return -1;
  280. if (ret < len) {
  281. s->read_buf_size = len - ret;
  282. memcpy(s->buf, buf + ret, s->read_buf_size);
  283. s->read_buf_index = 0;
  284. return 1;
  285. }
  286. } else {
  287. switch(st->codec.codec_id) {
  288. case CODEC_ID_MP2:
  289. /* better than nothing: skip mpeg audio RTP header */
  290. if (len <= 4)
  291. return -1;
  292. h = decode_be32(buf);
  293. len -= 4;
  294. buf += 4;
  295. av_new_packet(pkt, len);
  296. memcpy(pkt->data, buf, len);
  297. break;
  298. case CODEC_ID_MPEG1VIDEO:
  299. /* better than nothing: skip mpeg audio RTP header */
  300. if (len <= 4)
  301. return -1;
  302. h = decode_be32(buf);
  303. buf += 4;
  304. len -= 4;
  305. if (h & (1 << 26)) {
  306. /* mpeg2 */
  307. if (len <= 4)
  308. return -1;
  309. buf += 4;
  310. len -= 4;
  311. }
  312. av_new_packet(pkt, len);
  313. memcpy(pkt->data, buf, len);
  314. break;
  315. default:
  316. av_new_packet(pkt, len);
  317. memcpy(pkt->data, buf, len);
  318. break;
  319. }
  320. switch(st->codec.codec_id) {
  321. case CODEC_ID_MP2:
  322. case CODEC_ID_MPEG1VIDEO:
  323. if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
  324. int64_t addend;
  325. /* XXX: is it really necessary to unify the timestamp base ? */
  326. /* compute pts from timestamp with received ntp_time */
  327. delta_timestamp = timestamp - s->last_rtcp_timestamp;
  328. /* convert to 90 kHz without overflow */
  329. addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14;
  330. addend = (addend * 5625) >> 14;
  331. pkt->pts = addend + delta_timestamp;
  332. }
  333. break;
  334. default:
  335. /* no timestamp info yet */
  336. break;
  337. }
  338. pkt->stream_index = s->st->index;
  339. }
  340. return 0;
  341. }
  342. void rtp_parse_close(RTPDemuxContext *s)
  343. {
  344. if (s->payload_type == RTP_PT_MPEG2TS) {
  345. mpegts_parse_close(s->ts);
  346. }
  347. av_free(s);
  348. }
  349. /* rtp output */
  350. static int rtp_write_header(AVFormatContext *s1)
  351. {
  352. RTPDemuxContext *s = s1->priv_data;
  353. int payload_type, max_packet_size, n;
  354. AVStream *st;
  355. if (s1->nb_streams != 1)
  356. return -1;
  357. st = s1->streams[0];
  358. payload_type = rtp_get_payload_type(&st->codec);
  359. if (payload_type < 0)
  360. payload_type = RTP_PT_PRIVATE; /* private payload type */
  361. s->payload_type = payload_type;
  362. s->base_timestamp = random();
  363. s->timestamp = s->base_timestamp;
  364. s->ssrc = random();
  365. s->first_packet = 1;
  366. max_packet_size = url_fget_max_packet_size(&s1->pb);
  367. if (max_packet_size <= 12)
  368. return AVERROR_IO;
  369. s->max_payload_size = max_packet_size - 12;
  370. switch(st->codec.codec_id) {
  371. case CODEC_ID_MP2:
  372. case CODEC_ID_MP3:
  373. s->buf_ptr = s->buf + 4;
  374. s->cur_timestamp = 0;
  375. break;
  376. case CODEC_ID_MPEG1VIDEO:
  377. s->cur_timestamp = 0;
  378. break;
  379. case CODEC_ID_MPEG2TS:
  380. n = s->max_payload_size / TS_PACKET_SIZE;
  381. if (n < 1)
  382. n = 1;
  383. s->max_payload_size = n * TS_PACKET_SIZE;
  384. s->buf_ptr = s->buf;
  385. break;
  386. default:
  387. s->buf_ptr = s->buf;
  388. break;
  389. }
  390. return 0;
  391. }
  392. /* send an rtcp sender report packet */
  393. static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
  394. {
  395. RTPDemuxContext *s = s1->priv_data;
  396. #if defined(DEBUG)
  397. printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp);
  398. #endif
  399. put_byte(&s1->pb, (RTP_VERSION << 6));
  400. put_byte(&s1->pb, 200);
  401. put_be16(&s1->pb, 6); /* length in words - 1 */
  402. put_be32(&s1->pb, s->ssrc);
  403. put_be64(&s1->pb, ntp_time);
  404. put_be32(&s1->pb, s->timestamp);
  405. put_be32(&s1->pb, s->packet_count);
  406. put_be32(&s1->pb, s->octet_count);
  407. put_flush_packet(&s1->pb);
  408. }
  409. /* send an rtp packet. sequence number is incremented, but the caller
  410. must update the timestamp itself */
  411. static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len)
  412. {
  413. RTPDemuxContext *s = s1->priv_data;
  414. #ifdef DEBUG
  415. printf("rtp_send_data size=%d\n", len);
  416. #endif
  417. /* build the RTP header */
  418. put_byte(&s1->pb, (RTP_VERSION << 6));
  419. put_byte(&s1->pb, s->payload_type & 0x7f);
  420. put_be16(&s1->pb, s->seq);
  421. put_be32(&s1->pb, s->timestamp);
  422. put_be32(&s1->pb, s->ssrc);
  423. put_buffer(&s1->pb, buf1, len);
  424. put_flush_packet(&s1->pb);
  425. s->seq++;
  426. s->octet_count += len;
  427. s->packet_count++;
  428. }
  429. /* send an integer number of samples and compute time stamp and fill
  430. the rtp send buffer before sending. */
  431. static void rtp_send_samples(AVFormatContext *s1,
  432. const uint8_t *buf1, int size, int sample_size)
  433. {
  434. RTPDemuxContext *s = s1->priv_data;
  435. int len, max_packet_size, n;
  436. max_packet_size = (s->max_payload_size / sample_size) * sample_size;
  437. /* not needed, but who nows */
  438. if ((size % sample_size) != 0)
  439. av_abort();
  440. while (size > 0) {
  441. len = (max_packet_size - (s->buf_ptr - s->buf));
  442. if (len > size)
  443. len = size;
  444. /* copy data */
  445. memcpy(s->buf_ptr, buf1, len);
  446. s->buf_ptr += len;
  447. buf1 += len;
  448. size -= len;
  449. n = (s->buf_ptr - s->buf);
  450. /* if buffer full, then send it */
  451. if (n >= max_packet_size) {
  452. rtp_send_data(s1, s->buf, n);
  453. s->buf_ptr = s->buf;
  454. /* update timestamp */
  455. s->timestamp += n / sample_size;
  456. }
  457. }
  458. }
  459. /* NOTE: we suppose that exactly one frame is given as argument here */
  460. /* XXX: test it */
  461. static void rtp_send_mpegaudio(AVFormatContext *s1,
  462. const uint8_t *buf1, int size)
  463. {
  464. RTPDemuxContext *s = s1->priv_data;
  465. AVStream *st = s1->streams[0];
  466. int len, count, max_packet_size;
  467. max_packet_size = s->max_payload_size;
  468. /* test if we must flush because not enough space */
  469. len = (s->buf_ptr - s->buf);
  470. if ((len + size) > max_packet_size) {
  471. if (len > 4) {
  472. rtp_send_data(s1, s->buf, s->buf_ptr - s->buf);
  473. s->buf_ptr = s->buf + 4;
  474. /* 90 KHz time stamp */
  475. s->timestamp = s->base_timestamp +
  476. (s->cur_timestamp * 90000LL) / st->codec.sample_rate;
  477. }
  478. }
  479. /* add the packet */
  480. if (size > max_packet_size) {
  481. /* big packet: fragment */
  482. count = 0;
  483. while (size > 0) {
  484. len = max_packet_size - 4;
  485. if (len > size)
  486. len = size;
  487. /* build fragmented packet */
  488. s->buf[0] = 0;
  489. s->buf[1] = 0;
  490. s->buf[2] = count >> 8;
  491. s->buf[3] = count;
  492. memcpy(s->buf + 4, buf1, len);
  493. rtp_send_data(s1, s->buf, len + 4);
  494. size -= len;
  495. buf1 += len;
  496. count += len;
  497. }
  498. } else {
  499. if (s->buf_ptr == s->buf + 4) {
  500. /* no fragmentation possible */
  501. s->buf[0] = 0;
  502. s->buf[1] = 0;
  503. s->buf[2] = 0;
  504. s->buf[3] = 0;
  505. }
  506. memcpy(s->buf_ptr, buf1, size);
  507. s->buf_ptr += size;
  508. }
  509. s->cur_timestamp += st->codec.frame_size;
  510. }
  511. /* NOTE: a single frame must be passed with sequence header if
  512. needed. XXX: use slices. */
  513. static void rtp_send_mpegvideo(AVFormatContext *s1,
  514. const uint8_t *buf1, int size)
  515. {
  516. RTPDemuxContext *s = s1->priv_data;
  517. AVStream *st = s1->streams[0];
  518. int len, h, max_packet_size;
  519. uint8_t *q;
  520. max_packet_size = s->max_payload_size;
  521. while (size > 0) {
  522. /* XXX: more correct headers */
  523. h = 0;
  524. if (st->codec.sub_id == 2)
  525. h |= 1 << 26; /* mpeg 2 indicator */
  526. q = s->buf;
  527. *q++ = h >> 24;
  528. *q++ = h >> 16;
  529. *q++ = h >> 8;
  530. *q++ = h;
  531. if (st->codec.sub_id == 2) {
  532. h = 0;
  533. *q++ = h >> 24;
  534. *q++ = h >> 16;
  535. *q++ = h >> 8;
  536. *q++ = h;
  537. }
  538. len = max_packet_size - (q - s->buf);
  539. if (len > size)
  540. len = size;
  541. memcpy(q, buf1, len);
  542. q += len;
  543. /* 90 KHz time stamp */
  544. s->timestamp = s->base_timestamp +
  545. av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate);
  546. rtp_send_data(s1, s->buf, q - s->buf);
  547. buf1 += len;
  548. size -= len;
  549. }
  550. s->cur_timestamp++;
  551. }
  552. static void rtp_send_raw(AVFormatContext *s1,
  553. const uint8_t *buf1, int size)
  554. {
  555. RTPDemuxContext *s = s1->priv_data;
  556. AVStream *st = s1->streams[0];
  557. int len, max_packet_size;
  558. max_packet_size = s->max_payload_size;
  559. while (size > 0) {
  560. len = max_packet_size;
  561. if (len > size)
  562. len = size;
  563. /* 90 KHz time stamp */
  564. s->timestamp = s->base_timestamp +
  565. av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate);
  566. rtp_send_data(s1, buf1, len);
  567. buf1 += len;
  568. size -= len;
  569. }
  570. s->cur_timestamp++;
  571. }
  572. /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
  573. static void rtp_send_mpegts_raw(AVFormatContext *s1,
  574. const uint8_t *buf1, int size)
  575. {
  576. RTPDemuxContext *s = s1->priv_data;
  577. int len, out_len;
  578. while (size >= TS_PACKET_SIZE) {
  579. len = s->max_payload_size - (s->buf_ptr - s->buf);
  580. if (len > size)
  581. len = size;
  582. memcpy(s->buf_ptr, buf1, len);
  583. buf1 += len;
  584. size -= len;
  585. s->buf_ptr += len;
  586. out_len = s->buf_ptr - s->buf;
  587. if (out_len >= s->max_payload_size) {
  588. rtp_send_data(s1, s->buf, out_len);
  589. s->buf_ptr = s->buf;
  590. }
  591. }
  592. }
  593. /* write an RTP packet. 'buf1' must contain a single specific frame. */
  594. static int rtp_write_packet(AVFormatContext *s1, int stream_index,
  595. const uint8_t *buf1, int size, int64_t pts)
  596. {
  597. RTPDemuxContext *s = s1->priv_data;
  598. AVStream *st = s1->streams[0];
  599. int rtcp_bytes;
  600. int64_t ntp_time;
  601. #ifdef DEBUG
  602. printf("%d: write len=%d\n", stream_index, size);
  603. #endif
  604. /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
  605. rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
  606. RTCP_TX_RATIO_DEN;
  607. if (s->first_packet || rtcp_bytes >= 28) {
  608. /* compute NTP time */
  609. /* XXX: 90 kHz timestamp hardcoded */
  610. ntp_time = (pts << 28) / 5625;
  611. rtcp_send_sr(s1, ntp_time);
  612. s->last_octet_count = s->octet_count;
  613. s->first_packet = 0;
  614. }
  615. switch(st->codec.codec_id) {
  616. case CODEC_ID_PCM_MULAW:
  617. case CODEC_ID_PCM_ALAW:
  618. case CODEC_ID_PCM_U8:
  619. case CODEC_ID_PCM_S8:
  620. rtp_send_samples(s1, buf1, size, 1 * st->codec.channels);
  621. break;
  622. case CODEC_ID_PCM_U16BE:
  623. case CODEC_ID_PCM_U16LE:
  624. case CODEC_ID_PCM_S16BE:
  625. case CODEC_ID_PCM_S16LE:
  626. rtp_send_samples(s1, buf1, size, 2 * st->codec.channels);
  627. break;
  628. case CODEC_ID_MP2:
  629. case CODEC_ID_MP3:
  630. rtp_send_mpegaudio(s1, buf1, size);
  631. break;
  632. case CODEC_ID_MPEG1VIDEO:
  633. rtp_send_mpegvideo(s1, buf1, size);
  634. break;
  635. case CODEC_ID_MPEG2TS:
  636. rtp_send_mpegts_raw(s1, buf1, size);
  637. break;
  638. default:
  639. /* better than nothing : send the codec raw data */
  640. rtp_send_raw(s1, buf1, size);
  641. break;
  642. }
  643. return 0;
  644. }
  645. static int rtp_write_trailer(AVFormatContext *s1)
  646. {
  647. // RTPDemuxContext *s = s1->priv_data;
  648. return 0;
  649. }
  650. AVOutputFormat rtp_mux = {
  651. "rtp",
  652. "RTP output format",
  653. NULL,
  654. NULL,
  655. sizeof(RTPDemuxContext),
  656. CODEC_ID_PCM_MULAW,
  657. CODEC_ID_NONE,
  658. rtp_write_header,
  659. rtp_write_packet,
  660. rtp_write_trailer,
  661. };
  662. int rtp_init(void)
  663. {
  664. av_register_output_format(&rtp_mux);
  665. return 0;
  666. }