rtpenc_xiph.c 4.0 KB

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