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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avformat.h"
23 #include "rtpenc.h"
24 
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  // Set ident.
56  // 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  *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
60  *q++ = (RTP_XIPH_IDENT >> 8) & 0xff;
61  *q++ = (RTP_XIPH_IDENT ) & 0xff;
62 
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  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  int remaining = end_ptr - ptr;
74 
75  assert(s->num_frames <= s->max_frames_per_packet);
76  if ((s->num_frames > 0 && remaining < 0) ||
78  // 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 }