Pay attention to cache behavior, resulting in fast_scale_y being about
[xiph/unicode.git] / xiph-rtp / vorbisrtp.c
blob4d4794e8af07fce13f8c8445494a270d9c602eb0
1 /*
2 File: Vorbis RTP Server
3 Authors: Phil Kerr, Luca Barbato
4 Date: 05/01/2005
5 Platform: Linux
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.
35 */
37 /* System includes */
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <time.h>
45 /* Codec includes */
47 #include <vorbis/codec.h>
49 /* Network includes */
51 #include <arpa/inet.h>
52 #include <netinet/in.h>
53 #include <netdb.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
57 /* Common include */
59 #define HAVE_VORBIS
61 #include "xiph_rtp.h"
63 #define BUFFER_SIZE 4096
66 int main (int argc, char **argv)
68 xiph_rtp_t xr;
70 char *buffer;
71 int bytes;
73 char *filename;
74 FILE *file;
76 ogg_sync_init (&xr.oy);
78 int i = 0;
79 int opt;
81 char *ip = "227.0.0.1";
82 unsigned int port = 4044;
83 unsigned int ttl = 1;
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 */
91 if (argc < 2) {
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]);
94 exit (1);
97 while ((opt = getopt (argc, argv, "i:p:t:")) != -1) {
98 switch (opt)
101 /* Set IP address */
102 case 'i':
103 ip = optarg;
104 break;
106 /* Set port */
107 case 'p':
108 port = atoi (optarg);
109 break;
111 /* Set TTL value */
112 case 't':
113 ttl = atoi (optarg);
114 break;
116 /* Unknown option */
117 case '?':
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]);
120 return 1;
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");
141 if (file == NULL) {
142 fprintf (stderr, " Could not open file %s\n", filename);
143 exit (1);
146 fprintf (stdout, " Vorbis setup\n");
147 fprintf (stdout, " Filename: %s\n", filename);
149 int eos = 0;
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");
160 exit (0);
163 fprintf (stderr, "\n Input does not appear to be an Ogg bitstream.\n");
164 exit (1);
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");
174 exit (1);
177 if (ogg_stream_packetout (&xr.os, &xr.op) != 1) {
178 fprintf (stderr, " Error reading initial header packet.\n");
179 exit (1);
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");
184 exit (1);
187 ogg_copy_packet(&(xr.header[i]), &xr.op); //FIXME change it?
189 /*===========================================================================*/
190 /* Process comment and codebook headers */
191 /*===========================================================================*/
193 while (i < 2) {
194 while (i < 2) {
195 int result = ogg_sync_pageout (&xr.oy, &xr.og);
197 if (result == 0) break; /* Need more data */
199 if (result == 1) {
200 ogg_stream_pagein (&xr.os, &xr.og);
202 while(i < 2) {
203 result = ogg_stream_packetout (&xr.os, &xr.op);
205 if (result == 0) break;
207 if (result < 0) {
208 fprintf (stderr, " Corrupt secondary header. Exiting.\n");
209 exit (1);
212 vorbis_synthesis_headerin (&xr.vi, &xr.vc, &xr.op);
213 i++;
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");
225 exit (1);
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 /*===========================================================================*/
237 /* Print details */
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,
256 xr.header[2].packet,
257 xr.header[2].bytes);
258 creatertp(&xr, conf_packet, conf_bytes, 0, 0, 1, 0);
260 free(conf_packet);
263 /*===========================================================================*/
264 /* Read raw Vorbis data and send RTP packet */
265 /*===========================================================================*/
267 while (!eos) {
268 while (!eos) {
269 int result = ogg_sync_pageout (&xr.oy, &xr.og);
271 if (result == 0) break; /* need more data */
273 if (result < 0) {
274 fprintf (stderr, "\n Corrupt or missing data in bitstream; continuing....\n ");
275 } else {
276 ogg_stream_pagein (&xr.os, &xr.og);
278 while (1) {
279 result = ogg_stream_packetout (&xr.os, &xr.op);
281 if (result == 0) break; /* need more data */
283 if (result < 0) {
284 /* no reason to complain; already complained above */
285 } else {
286 #ifdef DEBUG
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);
288 #endif
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;
301 if (!eos) {
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);
323 fclose (file);
325 fprintf (stdout, "Done.\n");
326 return (0);