2 File: Vorbis RTP Server
3 Authors: Phil Kerr, Luca Barbato
7 Copyright (c) 2006, Fluendo / Xiph.Org
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 * Neither the name of the Xiph.Org nor the names of its contributors may
20 be used to endorse or promote products derived from this software without
21 specific prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
27 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 THE POSSIBILITY OF SUCH DAMAGE.
47 #include <vorbis/codec.h>
49 /* Network includes */
51 #include <arpa/inet.h>
52 #include <netinet/in.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
63 #define BUFFER_SIZE 4096
66 int main (int argc
, char **argv
)
76 ogg_sync_init (&xr
.oy
);
81 char *ip
= "227.0.0.1";
82 unsigned int port
= 4044;
84 long timestamp
= 0, prev
= 0;
86 fprintf (stderr
, "Vorbis RTP Server (draft-ietf-avt-rtp-vorbis-00)\n");
87 memset (&xr
,0,sizeof(xiph_rtp_t
));
89 /* Command-line args processing */
92 fprintf (stderr
, "\n\tNo Vorbis file specified.\n");
93 fprintf (stderr
, "\tUsage: %s [-i ip address] [-p port] [-t ttl] filename\n\n",argv
[0]);
97 while ((opt
= getopt (argc
, argv
, "i:p:t:")) != -1) {
108 port
= atoi (optarg
);
118 fprintf (stderr
, "\n Unknown option `-%c'.\n", optopt
);
119 fprintf (stderr
, "\tUsage: %s [-i ip address] [-p port] [-t ttl] filename\n\n",argv
[0]);
124 /* Init RTP socket */
126 if ( createsocket (&xr
, ip
, port
, ttl
)<0) return 1;
128 /* Print network details */
130 fprintf (stdout
, "\n Network setup\n");
131 fprintf (stdout
, " IP Address= %s\n", ip
);
132 fprintf (stdout
, " Port = %d\n", port
);
133 fprintf (stdout
, " TTL = %d\n", ttl
);
134 fprintf (stdout
, "\n");
136 /* Open Vorbis file */
138 filename
= argv
[argc
- 1];
139 file
= fopen (filename
, "rb");
142 fprintf (stderr
, " Could not open file %s\n", filename
);
146 fprintf (stdout
, " Vorbis setup\n");
147 fprintf (stdout
, " Filename: %s\n", filename
);
151 buffer
= ogg_sync_buffer (&xr
.oy
, BUFFER_SIZE
);
153 bytes
= fread (buffer
, 1, BUFFER_SIZE
, file
);
155 ogg_sync_wrote (&xr
.oy
, bytes
);
157 if (ogg_sync_pageout (&xr
.oy
, &xr
.og
) != 1) {
158 if (bytes
< BUFFER_SIZE
) {
159 fprintf (stdout
, " Done\n");
163 fprintf (stderr
, "\n Input does not appear to be an Ogg bitstream.\n");
167 ogg_stream_init (&xr
.os
, ogg_page_serialno (&xr
.og
));
169 vorbis_info_init (&xr
.vi
);
170 vorbis_comment_init (&xr
.vc
);
172 if (ogg_stream_pagein (&xr
.os
, &xr
.og
) < 0) {
173 fprintf (stderr
, " Error reading first page of Ogg bitstream data.\n");
177 if (ogg_stream_packetout (&xr
.os
, &xr
.op
) != 1) {
178 fprintf (stderr
, " Error reading initial header packet.\n");
182 if (vorbis_synthesis_headerin (&xr
.vi
, &xr
.vc
, &xr
.op
) < 0) {
183 fprintf (stderr
, " This Ogg bitstream does not contain Vorbis audio data.\n");
187 ogg_copy_packet(&(xr
.header
[i
]), &xr
.op
); //FIXME change it?
189 /*===========================================================================*/
190 /* Process comment and codebook headers */
191 /*===========================================================================*/
195 int result
= ogg_sync_pageout (&xr
.oy
, &xr
.og
);
197 if (result
== 0) break; /* Need more data */
200 ogg_stream_pagein (&xr
.os
, &xr
.og
);
203 result
= ogg_stream_packetout (&xr
.os
, &xr
.op
);
205 if (result
== 0) break;
208 fprintf (stderr
, " Corrupt secondary header. Exiting.\n");
212 vorbis_synthesis_headerin (&xr
.vi
, &xr
.vc
, &xr
.op
);
215 ogg_copy_packet(&(xr
.header
[i
]), &xr
.op
);
220 buffer
= ogg_sync_buffer (&xr
.oy
, BUFFER_SIZE
);
221 bytes
= fread (buffer
, 1, BUFFER_SIZE
, file
);
223 if (bytes
== 0 && i
< 2) {
224 fprintf (stderr
, "|| End of file before finding all Vorbis headers!\n");
228 ogg_sync_wrote (&xr
.oy
, bytes
);
231 xr
.bitfield
.cbident
= rand ();
233 vorbis_synthesis_init (&xr
.vd
, &xr
.vi
);
234 vorbis_block_init (&xr
.vd
, &xr
.vb
);
236 /*===========================================================================*/
238 /*===========================================================================*/
240 fprintf (stdout
, " Bitstream is %d channel, %ldHz\n", xr
.vi
.channels
, xr
.vi
.rate
);
241 fprintf (stdout
, " Encoded by: %s\n", xr
.vc
.vendor
);
242 fprintf (stdout
, " Bitrates: min=%ld - nom=%ld - max=%ld\n", xr
.vi
.bitrate_lower
, xr
.vi
.bitrate_nominal
, xr
.vi
.bitrate_upper
);
243 fprintf (stdout
, " Decode setup ident is 0x%06x\n", xr
.bitfield
.cbident
);
244 fprintf (stdout
, "\n");
245 fprintf (stdout
, "|| Processing\n");
247 /*===========================================================================*/
248 /* Send the three headers inline */
249 /*===========================================================================*/
251 int conf_bytes
= xr
.header
[0].bytes
+ xr
.header
[2].bytes
;
252 unsigned char *conf_packet
= malloc(conf_bytes
);
254 memcpy (conf_packet
, xr
.header
[0].packet
, xr
.header
[0].bytes
);
255 memcpy (conf_packet
+ xr
.header
[0].bytes
,
258 creatertp(&xr
, conf_packet
, conf_bytes
, 0, 0, 1, 0);
263 /*===========================================================================*/
264 /* Read raw Vorbis data and send RTP packet */
265 /*===========================================================================*/
269 int result
= ogg_sync_pageout (&xr
.oy
, &xr
.og
);
271 if (result
== 0) break; /* need more data */
274 fprintf (stderr
, "\n Corrupt or missing data in bitstream; continuing....\n ");
276 ogg_stream_pagein (&xr
.os
, &xr
.og
);
279 result
= ogg_stream_packetout (&xr
.os
, &xr
.op
);
281 if (result
== 0) break; /* need more data */
284 /* no reason to complain; already complained above */
287 printf(" bytes %ld bos %ld eos %ld gp %lld pno %lld\n", xr
.op
.bytes
, xr
.op
.b_o_s
, xr
.op
.e_o_s
, xr
.op
.granulepos
, xr
.op
.packetno
);
289 creatertp ( &xr
, xr
.op
.packet
, xr
.op
.bytes
,
290 timestamp
, timestamp
, 0, xr
.op
.e_o_s
);
291 timestamp
= ( vorbis_packet_blocksize(&xr
.vi
,&xr
.op
) +
292 prev
) / 4*1000000L / xr
.vi
.rate
;
293 prev
= vorbis_packet_blocksize(&xr
.vi
,&xr
.op
);
297 if (ogg_page_eos (&xr
.og
)) eos
= 1;
302 buffer
= ogg_sync_buffer (&xr
.oy
, BUFFER_SIZE
);
303 bytes
= fread (buffer
, 1, BUFFER_SIZE
, file
);
305 ogg_sync_wrote (&xr
.oy
, bytes
);
307 if (bytes
== 0) eos
= 1;
310 //FIXME make it a cleanup function!
311 ogg_packet_clear (&(xr
.header
[0]));
312 ogg_packet_clear (&(xr
.header
[1]));
313 ogg_packet_clear (&(xr
.header
[2]));
315 ogg_stream_clear (&xr
.os
);
317 vorbis_block_clear (&xr
.vb
);
318 vorbis_dsp_clear (&xr
.vd
);
319 vorbis_comment_clear (&xr
.vc
);
320 vorbis_info_clear (&xr
.vi
);
322 ogg_sync_clear (&xr
.oy
);
325 fprintf (stdout
, "Done.\n");