source: roaraudio/roarclients/roarmonhttp.c @ 1017:055034719a76

Last change on this file since 1017:055034719a76 was 1017:055034719a76, checked in by phi, 15 years ago

print header, do the main loop as IO buffer

File size: 3.3 KB
Line 
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
29void 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 }
37
38 printf("Content-type: %s\r\n", mime);
39 printf("\r\n");
40
41 fflush(stdout);
42}
43
44int stream (int dest, int src) {
45 struct roar_buffer *ring = NULL, *cur;
46 ssize_t len;
47 size_t  todo;
48 fd_set fsi[1], fso[1];
49 struct timeval tv;
50 int alive = 1;
51 int maxfh = (dest > src ? dest : src) + 1;
52 void * data;
53
54 roar_socket_nonblock(src,  ROAR_SOCKET_NONBLOCK);
55 roar_socket_nonblock(dest, ROAR_SOCKET_NONBLOCK);
56
57 while (alive) {
58  FD_ZERO(fsi);
59  FD_ZERO(fso);
60  FD_SET(src, fsi);
61  FD_SET(dest, fso);
62
63  tv.tv_sec  = 0;
64  tv.tv_usec = 100000; // 100ms
65
66  if (select(maxfh, fsi, fso, NULL, &tv) > 0) {
67   if ( FD_ISSET(src, fsi) ) { // we can read!
68    if ( roar_buffer_new(&cur, BUFSIZE) == -1 )
69     return -1;
70
71    if ( roar_buffer_get_data(cur, &data) == -1 )
72     return -1;
73
74    len = read(src, data, BUFSIZE);
75
76    switch (len) {
77     case  0:
78     case -1:
79       roar_buffer_free(cur);
80
81       if ( ring != NULL )
82        roar_buffer_free(ring);
83
84       return -1;
85      break;
86    }
87
88    if ( ring == NULL ) {
89     ring = cur;
90    } else {
91     roar_buffer_add(ring, cur);
92    }
93   } else if ( FD_ISSET(dest, fso) && ring != NULL ) { // we can write!
94    if ( roar_buffer_get_data(ring, &data) == -1 )
95     return -1;
96
97    if ( roar_buffer_get_len(ring, &todo) == -1 )
98     return -1;
99
100    len = write(dest, data, todo);
101
102    if ( len < 1 ) {
103     if ( errno != EAGAIN ) {
104      roar_buffer_free(ring);
105      return -1;
106     }
107    }
108
109    if ( todo == len ) { // we wrote all of the pkg
110     if ( roar_buffer_next(&ring) == -1 )
111      return -1;
112    } else {
113     if ( roar_buffer_set_offset(ring, len) == -1 )
114      return -1;
115    }
116
117   }
118  }
119 }
120
121 return 0;
122}
123
124int main (int argc, char * argv[]) {
125 int    rate     = 44100;
126 int    bits     = 16;
127 int    channels = 2;
128// int    codec    = ROAR_CODEC_OGG_VORBIS;
129 int    codec    = ROAR_CODEC_DEFAULT;
130 char * server   = NULL;
131 int    fh;
132
133 if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarmon")) == -1 ) {
134  fprintf(stderr, "Error: can not start monitoring\n");
135  return 1;
136 }
137
138 print_header(codec);
139
140/*
141 while((i = read(fh, buf, BUFSIZE)))
142  if (write(out, buf, i) != i)
143   break;
144*/
145
146 stream(ROAR_STDOUT, fh);
147
148 roar_simple_close(fh);
149
150 return 0;
151}
152
153//ll
Note: See TracBrowser for help on using the repository browser.