FFmpeg
rtpenc_xiph.c
Go to the documentation of this file.
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 
22 #include "libavutil/avassert.h"
23 #include "libavutil/intreadwrite.h"
24 
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;
36  AVStream *st = s1->streams[0];
37  int max_pkt_size, xdt, frag;
38  uint8_t *q;
39 
40  max_pkt_size = s->max_payload_size - 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
41  if (max_pkt_size <= 0) {
42  av_log(s1, AV_LOG_ERROR, "Max payload size too small for Xiph RTP\n");
43  return;
44  }
45 
46  // set xiph data type
47  switch (*buff) {
48  case 0x01: // vorbis id
49  case 0x05: // vorbis setup
50  case 0x80: // theora header
51  case 0x82: // theora tables
52  xdt = 1; // packed config payload
53  break;
54  case 0x03: // vorbis comments
55  case 0x81: // theora comments
56  xdt = 2; // comment payload
57  break;
58  default:
59  xdt = 0; // raw data payload
60  break;
61  }
62 
63  // Set ident.
64  // Probably need a non-fixed way of generating
65  // this, but it has to be done in SDP and passed in from there.
66  q = s->buf;
67  *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
68  *q++ = (RTP_XIPH_IDENT >> 8) & 0xff;
69  *q++ = (RTP_XIPH_IDENT ) & 0xff;
70 
71  // set fragment
72  // 0 - whole frame (possibly multiple frames)
73  // 1 - first fragment
74  // 2 - fragment continuation
75  // 3 - last fragment
76  frag = size <= max_pkt_size ? 0 : 1;
77 
78  if (!frag && !xdt) { // do we have a whole frame of raw data?
79  uint8_t *end_ptr = s->buf + 6 + max_pkt_size; // what we're allowed to write
80  uint8_t *ptr = s->buf_ptr + 2 + size; // what we're going to write
81  int remaining = end_ptr - ptr;
82 
83  av_assert1(s->num_frames <= s->max_frames_per_packet);
84  if (s->num_frames > 0 &&
85  (remaining < 0 ||
86  s->num_frames == s->max_frames_per_packet ||
87  av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
88  s1->max_delay, AV_TIME_BASE_Q) >= 0)) {
89  // send previous packets now; no room for new data, or too much delay
90  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
91  s->num_frames = 0;
92  }
93 
94  // buffer current frame to send later
95  if (0 == s->num_frames)
96  s->timestamp = s->cur_timestamp;
97  s->num_frames++;
98 
99  // Set packet header. Normally, this is OR'd with frag and xdt,
100  // but those are zero, so omitted here
101  *q++ = s->num_frames;
102 
103  if (s->num_frames > 1)
104  q = s->buf_ptr; // jump ahead if needed
105  AV_WB16(q, size);
106  q += 2;
107  memcpy(q, buff, size);
108  q += size;
109  s->buf_ptr = q;
110 
111  return;
112  } else if (s->num_frames) {
113  // immediately send buffered frames if buffer is not raw data,
114  // or if current frame is fragmented.
115  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
116  }
117 
118  s->timestamp = s->cur_timestamp;
119  s->num_frames = 0;
120  s->buf_ptr = q;
121  while (size > 0) {
122  int len = (!frag || frag == 3) ? size : max_pkt_size;
123  q = s->buf_ptr;
124 
125  // set packet headers
126  *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
127  AV_WB16(q, len);
128  q += 2;
129  // set packet body
130  memcpy(q, buff, len);
131  q += len;
132  buff += len;
133  size -= len;
134 
135  ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
136 
137  frag = size <= max_pkt_size ? 3 : 2;
138  }
139 }
av_compare_ts
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:263
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1352
ff_rtp_send_data
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:364
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVFormatContext
Format I/O context.
Definition: avformat.h:1284
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:786
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
RTPMuxContext
Definition: rtpenc.h:27
RTP_XIPH_IDENT
#define RTP_XIPH_IDENT
Definition: rtp.h:91
size
int size
Definition: twinvq_data.h:10344
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1429
len
int len
Definition: vorbis_enc_data.h:426
rtpenc.h
AVStream
Stream structure.
Definition: avformat.h:747
avformat.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_rtp_send_xiph
void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
Packetize Xiph frames into RTP according to RFC 5215 (Vorbis) and the Theora RFC draft.
Definition: rtpenc_xiph.c:33
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1312