1 | //roarmonhttp.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roarclients a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <roaraudio.h> |
---|
26 | |
---|
27 | #define BUFSIZE 1024 |
---|
28 | |
---|
29 | void print_header (int codec) { |
---|
30 | char * mime = "application/octet-stream"; |
---|
31 | |
---|
32 | switch (codec) { |
---|
33 | case ROAR_CODEC_OGG_VORBIS: |
---|
34 | mime = "application/ogg"; |
---|
35 | break; |
---|
36 | case ROAR_CODEC_RIFF_WAVE: |
---|
37 | mime = "audio/x-wav"; |
---|
38 | break; |
---|
39 | } |
---|
40 | |
---|
41 | printf("Content-type: %s\r\n", mime); |
---|
42 | printf("\r\n"); |
---|
43 | |
---|
44 | fflush(stdout); |
---|
45 | } |
---|
46 | |
---|
47 | int stream (int dest, int src) { |
---|
48 | struct roar_buffer *ring = NULL, *cur; |
---|
49 | ssize_t len; |
---|
50 | size_t todo; |
---|
51 | fd_set fsi[1], fso[1]; |
---|
52 | struct timeval tv; |
---|
53 | int alive = 1; |
---|
54 | int maxfh = (dest > src ? dest : src) + 1; |
---|
55 | void * data; |
---|
56 | |
---|
57 | roar_socket_nonblock(src, ROAR_SOCKET_NONBLOCK); |
---|
58 | roar_socket_nonblock(dest, ROAR_SOCKET_NONBLOCK); |
---|
59 | |
---|
60 | while (alive) { |
---|
61 | FD_ZERO(fsi); |
---|
62 | FD_ZERO(fso); |
---|
63 | FD_SET(src, fsi); |
---|
64 | if ( ring != NULL ) { |
---|
65 | FD_SET(dest, fso); |
---|
66 | } |
---|
67 | |
---|
68 | tv.tv_sec = 0; |
---|
69 | tv.tv_usec = 100000; // 100ms |
---|
70 | |
---|
71 | if (select(maxfh, fsi, fso, NULL, &tv) > 0) { |
---|
72 | if ( FD_ISSET(src, fsi) ) { // we can read! |
---|
73 | if ( roar_buffer_new(&cur, BUFSIZE) == -1 ) |
---|
74 | return -1; |
---|
75 | |
---|
76 | if ( roar_buffer_get_data(cur, &data) == -1 ) |
---|
77 | return -1; |
---|
78 | |
---|
79 | len = read(src, data, BUFSIZE); |
---|
80 | |
---|
81 | switch (len) { |
---|
82 | case 0: |
---|
83 | case -1: |
---|
84 | roar_buffer_free(cur); |
---|
85 | |
---|
86 | if ( ring != NULL ) |
---|
87 | roar_buffer_free(ring); |
---|
88 | |
---|
89 | return -1; |
---|
90 | break; |
---|
91 | } |
---|
92 | |
---|
93 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
94 | return -1; |
---|
95 | |
---|
96 | if ( ring == NULL ) { |
---|
97 | ring = cur; |
---|
98 | } else { |
---|
99 | roar_buffer_add(ring, cur); |
---|
100 | } |
---|
101 | } else if ( FD_ISSET(dest, fso) && ring != NULL ) { // we can write! |
---|
102 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
103 | return -1; |
---|
104 | |
---|
105 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
106 | return -1; |
---|
107 | |
---|
108 | len = write(dest, data, todo); |
---|
109 | |
---|
110 | if ( len < 1 ) { |
---|
111 | if ( errno != EAGAIN ) { |
---|
112 | roar_buffer_free(ring); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | if ( todo == len ) { // we wrote all of the pkg |
---|
118 | if ( roar_buffer_next(&ring) == -1 ) |
---|
119 | return -1; |
---|
120 | } else { |
---|
121 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | int main (int argc, char * argv[]) { |
---|
133 | int rate = 44100; |
---|
134 | int bits = 16; |
---|
135 | int channels = 2; |
---|
136 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
137 | // int codec = ROAR_CODEC_DEFAULT; |
---|
138 | char * server = NULL; |
---|
139 | int fh; |
---|
140 | char * c, * k, * v; |
---|
141 | char * sp0, * sp1; |
---|
142 | |
---|
143 | |
---|
144 | c = strtok_r(getenv("QUERY_STRING"), "&", &sp0); |
---|
145 | |
---|
146 | while (c != NULL) { |
---|
147 | k = strtok_r(c, "=", &sp1); |
---|
148 | v = strtok_r(NULL, "=", &sp1); |
---|
149 | |
---|
150 | if ( !strcmp(k, "codec") ) { |
---|
151 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
152 | return 1; |
---|
153 | } else { |
---|
154 | return 1; |
---|
155 | } |
---|
156 | |
---|
157 | c = strtok_r(NULL, "&", &sp0); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarmon")) == -1 ) { |
---|
162 | // fprintf(stderr, "Error: can not start monitoring\n"); |
---|
163 | return 1; |
---|
164 | } |
---|
165 | |
---|
166 | print_header(codec); |
---|
167 | |
---|
168 | /* |
---|
169 | while((i = read(fh, buf, BUFSIZE))) |
---|
170 | if (write(out, buf, i) != i) |
---|
171 | break; |
---|
172 | */ |
---|
173 | |
---|
174 | stream(ROAR_STDOUT, fh); |
---|
175 | |
---|
176 | roar_simple_close(fh); |
---|
177 | |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | //ll |
---|