source: roaraudio/libroar/simple.c @ 3479:67e03a930fb2

Last change on this file since 3479:67e03a930fb2 was 3479:67e03a930fb2, checked in by phi, 14 years ago

make valgrind happy

File size: 11.2 KB
Line 
1//simple.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
5 *
6 *  This file is part of libroar 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 *  libroar 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 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_simple_connect (struct roar_connection * con, char * server, char * name) {
38
39 ROAR_DBG("roar_simple_connect(*): trying to connect...");
40
41 if ( roar_connect(con, server) == -1 ) {
42  ROAR_DBG("roar_simple_connect(*): roar_connect() faild!");
43  return -1;
44 }
45
46 if ( roar_identify(con, name) == -1 ) {
47  ROAR_DBG("roar_simple_connect(*): roar_identify() faild!");
48  return -1;
49 }
50
51 if ( roar_auth(con) == -1 ) {
52  ROAR_DBG("roar_simple_connect(*): roar_auth() faild!");
53  return -1;
54 }
55
56 return 0;
57}
58
59int roar_simple_stream(int rate, int channels, int bits, int codec, char * server, int dir, char * name) {
60 struct roar_stream     s;
61
62 roar_debug_warn_sysio("roar_simple_stream", "roar_vio_simple_stream", NULL);
63
64 return roar_simple_stream_obj(&s, rate, channels, bits, codec, server, dir, name);
65}
66
67int roar_simple_stream_obj  (struct roar_stream * s, int rate, int channels, int bits, int codec, char * server, int dir, char * name) {
68 struct roar_connection con;
69 int ret;
70
71 roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL);
72
73 if ( roar_simple_connect(&con, server, name) == -1 ) {
74  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
75  ROAR_ERR("roar_simple_stream(*): Can not connect to server");
76  return -1;
77 }
78
79 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
80  roar_disconnect(&con);
81  return -1;
82 }
83
84 if ( roar_stream_connect(&con, s, dir) == -1 ) {
85  roar_disconnect(&con);
86  return -1;
87 }
88
89 if ( roar_stream_exec(&con, s) == -1 ) {
90  roar_disconnect(&con);
91  return -1;
92 }
93
94 if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
95  roar_disconnect(&con);
96  return -1;
97 }
98
99 if ( dir == ROAR_DIR_PLAY ) {
100  ROAR_SHUTDOWN(ret, SHUT_RD);
101 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
102  ROAR_SHUTDOWN(ret, SHUT_WR);
103 }
104
105 return ret;
106}
107
108int roar_simple_new_stream_attachexeced_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
109 int fh;
110
111 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */,
112                                   dir, "libroar temp stream")) == -1 )
113  return -1;
114
115 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
116#ifdef ROAR_HAVE_IO_POSIX
117  close(fh);
118#endif /* no else as we return -1 anyway */
119  return -1;
120 }
121
122 s->fh = fh;
123
124 return fh;
125}
126
127int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) {
128 struct roar_stream     s;
129 return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir);
130}
131
132int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
133 struct roar_libroar_config * config = roar_libroar_get_config();
134 char file[80] = {0};
135 int fh = -1, listen = -1;
136 static int count = 0;
137 int    type = ROAR_SOCKET_TYPE_UNIX;
138 int    port = 0;
139#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
140 int    opt  = 1;
141#endif
142#ifdef ROAR_HAVE_IPV4
143 struct sockaddr_in   socket_addr;
144 socklen_t            len            = sizeof(struct sockaddr_in);
145#else
146 struct sockaddr      socket_addr;
147 socklen_t            len            = sizeof(struct sockaddr);
148#endif
149#ifdef ROAR_HAVE_SELECT
150 fd_set fds;
151 struct timeval timeout = {10, 0};
152 struct roar_message    mes;
153#endif
154#ifdef ROAR_HAVE_UNIX
155 int socks[2]; // for socketpair()
156#endif
157
158 // make valgrind happy
159 memset(&socket_addr, 0, sizeof(socket_addr));
160 memset(&mes,         0, sizeof(mes));
161
162 roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL);
163
164 if ( config != NULL ) {
165  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
166   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
167  }
168 }
169
170#ifdef ROAR_HAVE_BSDSOCKETS
171 if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
172  return -1;
173 }
174#else
175 return -1;
176#endif
177
178 if ( len == 0 ) {
179#ifdef ROAR_OS_OPENBSD
180  ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD");
181  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
182#else
183  return -1;
184#endif
185 }
186
187 switch (((struct sockaddr*)&socket_addr)->sa_family) {
188#ifdef ROAR_HAVE_UNIX
189  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
190#endif
191#ifdef ROAR_HAVE_IPV4
192  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
193#endif
194#ifdef ROAR_HAVE_LIBDNET
195  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
196#endif
197  default:
198    return -1;
199   break;
200 }
201
202 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
203  if ( roar_socket_get_local_nodename() ) {
204   snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
205  } else {
206   return -1;
207  }
208#ifdef ROAR_HAVE_IPV4
209 } else {
210  strncpy(file, inet_ntoa(socket_addr.sin_addr), 79);
211#endif
212 }
213
214 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
215  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
216   return -1;
217  }
218 }
219
220 if ( type == ROAR_SOCKET_TYPE_INET ) {
221#ifdef ROAR_HAVE_IPV4
222  len = sizeof(struct sockaddr_in);
223  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
224
225  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
226   return -1;
227  }
228  port = ROAR_NET2HOST16(socket_addr.sin_port);
229  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
230#else
231  return -1;
232#endif
233 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
234#ifdef ROAR_HAVE_LIBDNET
235  len = sizeof(struct sockaddr_in);
236  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
237#else
238  return -1;
239#endif
240 }
241
242 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
243  return -1;
244 }
245
246 if ( roar_stream_connect(con, s, dir) == -1 ) {
247  return -1;
248 }
249
250 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
251#ifdef ROAR_HAVE_SELECT
252  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
253
254   FD_ZERO(&fds);
255   FD_SET(listen, &fds);
256
257   if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) {
258    close(listen);
259
260    // we don't need to check the content as we know it failed...
261    if ( roar_recv_message(con, &mes, NULL) == -1 )
262     return -1;
263
264    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
265     return -1;
266
267    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
268   }
269
270   if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
271    /* errr, do we need we any error handling here? */
272   }
273   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
274    close(fh);
275    fh = -1;
276   } else if ( mes.cmd != ROAR_CMD_OK ) {
277    close(fh);
278    fh = -1;
279   }
280  }
281
282  close(listen);
283#else
284  return -1;
285#endif
286 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
287#ifdef ROAR_HAVE_UNIX
288  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
289   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
290                                          // as we return -1 in both whys
291   return -1;
292  }
293
294  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
295   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
296                                          // as we return -1 in both whys
297   return -1;
298  }
299
300  close(socks[0]);
301  fh = socks[1];
302#else
303  roar_kick(con, ROAR_OT_STREAM, s->id);
304  return -1;
305#endif
306 }
307
308/*
309 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
310  unlink(file);
311 }
312*/
313
314 if ( dir == ROAR_DIR_PLAY ) {
315  ROAR_SHUTDOWN(fh, SHUT_RD);
316 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
317  ROAR_SHUTDOWN(fh, SHUT_WR);
318 }
319
320 s->fh = fh;
321
322 return fh;
323}
324
325
326int roar_simple_play_file(char * file, char * server, char * name) {
327 struct roar_connection con;
328
329 if ( roar_simple_connect(&con, server, name) == -1 ) {
330  return -1;
331 }
332
333 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
334}
335
336int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
337 roar_debug_warn_sysio("roar_simple_play", "roar_vio_simple_stream", NULL);
338
339 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
340}
341
342int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
343 roar_debug_warn_sysio("roar_simple_monitor", "roar_vio_simple_stream", NULL);
344
345 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
346}
347
348int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
349 roar_debug_warn_sysio("roar_simple_record", "roar_vio_simple_stream", NULL);
350
351 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
352}
353
354int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
355 roar_debug_warn_sysio("roar_simple_filter", "roar_vio_simple_stream", NULL);
356
357 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
358}
359
360int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) {
361 struct roar_stream parent_stream;
362
363 if ( con == NULL || s == NULL || parent < 0 )
364  return -1;
365
366 if ( dir == -1 ) {
367  if ( roar_get_stream(con, &parent_stream, parent) == -1 )
368   return -1;
369
370  if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 )
371   return -1;
372 }
373
374 if ( roar_stream_set_rel_id(s, parent) == -1 )
375  return -1;
376
377 if ( roar_stream_connect(con, s, dir) == -1 )
378  return -1;
379
380 if ( roar_stream_set_flags(con, s, ROAR_FLAG_VIRTUAL, 0) == -1 ) {
381  roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s));
382  return -1;
383 }
384
385 return 0;
386}
387
388int roar_simple_close(int fh) {
389 roar_debug_warn_sysio("roar_simple_close", "roar_vio_close", NULL);
390
391#ifdef ROAR_TARGET_WIN32
392 closesocket(fh);
393 return 0;
394#else
395
396#ifdef ROAR_HAVE_IO_POSIX
397 return close(fh);
398#else
399 return -1;
400#endif
401
402#endif
403}
404
405int roar_simple_get_standby (int fh) {
406 struct roar_connection con;
407
408 roar_debug_warn_sysio("roar_simple_get_standby", "roar_get_standby", NULL);
409
410 if ( roar_connect_fh(&con, fh) == -1 )
411  return -1;
412
413 return roar_get_standby(&con);
414}
415
416//ll
Note: See TracBrowser for help on using the repository browser.