rtpenc_xiph.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * RTP packetization for Xiph audio and video
  3. * Copyright (c) 2010 Josh Allmann
  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. #include "libavutil/avassert.h"
  22. #include "avformat.h"
  23. #include "rtpenc.h"
  24. /**
  25. * Packetize Xiph frames into RTP according to
  26. * RFC 5215 (Vorbis) and the Theora RFC draft.
  27. * (http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt)
  28. */
  29. void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
  30. {
  31. RTPMuxContext *s = s1->priv_data;
  32. int max_pkt_size, xdt, frag;
  33. uint8_t *q;
  34. max_pkt_size = s->max_payload_size;
  35. // set xiph data type
  36. switch (*buff) {
  37. case 0x01: // vorbis id
  38. case 0x05: // vorbis setup
  39. case 0x80: // theora header
  40. case 0x82: // theora tables
  41. xdt = 1; // packed config payload
  42. break;
  43. case 0x03: // vorbis comments
  44. case 0x81: // theora comments
  45. xdt = 2; // comment payload
  46. break;
  47. default:
  48. xdt = 0; // raw data payload
  49. break;
  50. }
  51. // Set ident.
  52. // Probably need a non-fixed way of generating
  53. // this, but it has to be done in SDP and passed in from there.
  54. q = s->buf;
  55. *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
  56. *q++ = (RTP_XIPH_IDENT >> 8) & 0xff;
  57. *q++ = (RTP_XIPH_IDENT ) & 0xff;
  58. // set fragment
  59. // 0 - whole frame (possibly multiple frames)
  60. // 1 - first fragment
  61. // 2 - fragment continuation
  62. // 3 - last fragmement
  63. frag = size <= max_pkt_size ? 0 : 1;
  64. if (!frag && !xdt) { // do we have a whole frame of raw data?
  65. uint8_t *end_ptr = s->buf + 6 + max_pkt_size; // what we're allowed to write
  66. uint8_t *ptr = s->buf_ptr + 2 + size; // what we're going to write
  67. int remaining = end_ptr - ptr;
  68. av_assert1(s->num_frames <= s->max_frames_per_packet);
  69. if ((s->num_frames > 0 && remaining < 0) ||
  70. s->num_frames == s->max_frames_per_packet) {
  71. // send previous packets now; no room for new data
  72. ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
  73. s->num_frames = 0;
  74. }
  75. // buffer current frame to send later
  76. if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
  77. s->num_frames++;
  78. // Set packet header. Normally, this is OR'd with frag and xdt,
  79. // but those are zero, so omitted here
  80. *q++ = s->num_frames;
  81. if (s->num_frames > 1) q = s->buf_ptr; // jump ahead if needed
  82. *q++ = (size >> 8) & 0xff;
  83. *q++ = size & 0xff;
  84. memcpy(q, buff, size);
  85. q += size;
  86. s->buf_ptr = q;
  87. return;
  88. } else if (s->num_frames) {
  89. // immediately send buffered frames if buffer is not raw data,
  90. // or if current frame is fragmented.
  91. ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
  92. }
  93. s->timestamp = s->cur_timestamp;
  94. s->num_frames = 0;
  95. s->buf_ptr = q;
  96. while (size > 0) {
  97. int len = (!frag || frag == 3) ? size : max_pkt_size;
  98. q = s->buf_ptr;
  99. // set packet headers
  100. *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
  101. *q++ = (len >> 8) & 0xff;
  102. *q++ = len & 0xff;
  103. // set packet body
  104. memcpy(q, buff, len);
  105. q += len;
  106. buff += len;
  107. size -= len;
  108. ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
  109. frag = size <= max_pkt_size ? 3 : 2;
  110. }
  111. }