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