Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpenc_xiph.c @ 333e8943

History | View | Annotate | Download (3.99 KB)

1 91af5601 Josh Allmann
/*
2
 * RTP packetization for Xiph audio and video
3
 * Copyright (c) 2010 Josh Allmann
4
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 91af5601 Josh Allmann
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 91af5601 Josh Allmann
 * 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 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 91af5601 Josh Allmann
 * 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 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 91af5601 Josh Allmann
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "avformat.h"
23
#include "rtpenc.h"
24
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
    int max_pkt_size, xdt, frag;
34
    uint8_t *q;
35
36
    max_pkt_size = s->max_payload_size;
37
38
    // set xiph data type
39
    switch (*buff) {
40
    case 0x01:   // vorbis id
41
    case 0x05:   // vorbis setup
42
    case 0x80:   // theora header
43
    case 0x82:   // theora tables
44
        xdt = 1; // packed config payload
45
        break;
46
    case 0x03:   // vorbis comments
47
    case 0x81:   // theora comments
48
        xdt = 2; // comment payload
49
        break;
50
    default:
51
        xdt = 0; // raw data payload
52
        break;
53
    }
54
55 b5c4bb98 Martin Storsjö
    // Set ident.
56 91af5601 Josh Allmann
    // Probably need a non-fixed way of generating
57
    // this, but it has to be done in SDP and passed in from there.
58
    q = s->buf;
59 b5c4bb98 Martin Storsjö
    *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
60
    *q++ = (RTP_XIPH_IDENT >>  8) & 0xff;
61
    *q++ = (RTP_XIPH_IDENT      ) & 0xff;
62 91af5601 Josh Allmann
63
    // set fragment
64
    // 0 - whole frame (possibly multiple frames)
65
    // 1 - first fragment
66
    // 2 - fragment continuation
67
    // 3 - last fragmement
68
    frag = size <= max_pkt_size ? 0 : 1;
69
70
    if (!frag && !xdt) { // do we have a whole frame of raw data?
71 62c7e2c3 Martin Storsjö
        uint8_t *end_ptr = s->buf + 6 + max_pkt_size; // what we're allowed to write
72
        uint8_t *ptr     = s->buf_ptr + 2 + size; // what we're going to write
73 91af5601 Josh Allmann
        int remaining    = end_ptr - ptr;
74
75 f53b9a29 Martin Storsjö
        assert(s->num_frames <= s->max_frames_per_packet);
76 91af5601 Josh Allmann
        if ((s->num_frames > 0 && remaining < 0) ||
77 f53b9a29 Martin Storsjö
            s->num_frames == s->max_frames_per_packet) {
78 91af5601 Josh Allmann
            // send previous packets now; no room for new data
79
            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
80
            s->num_frames = 0;
81
        }
82
83
        // buffer current frame to send later
84
        if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
85
        s->num_frames++;
86
87
        // Set packet header. Normally, this is OR'd with frag and xdt,
88
        // but those are zero, so omitted here
89
        *q++ = s->num_frames;
90
91
        if (s->num_frames > 1) q = s->buf_ptr; // jump ahead if needed
92
        *q++ = (size >> 8) & 0xff;
93
        *q++ = size & 0xff;
94
        memcpy(q, buff, size);
95
        q += size;
96
        s->buf_ptr = q;
97
98
        return;
99
    } else if (s->num_frames) {
100
        // immediately send buffered frames if buffer is not raw data,
101
        // or if current frame is fragmented.
102
        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
103
    }
104
105
    s->timestamp = s->cur_timestamp;
106
    s->num_frames = 0;
107
    s->buf_ptr = q;
108
    while (size > 0) {
109
        int len = (!frag || frag == 3) ? size : max_pkt_size;
110
        q = s->buf_ptr;
111
112
        // set packet headers
113
        *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
114
        *q++ = (len >> 8) & 0xff;
115
        *q++ = len & 0xff;
116
        // set packet body
117
        memcpy(q, buff, len);
118
        q += len;
119
        buff += len;
120
        size -= len;
121
122
        ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
123
124
        frag = size <= max_pkt_size ? 3 : 2;
125
    }
126
}