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.

136 lines
4.3KB

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