source: roaraudio/libroar/simple.c @ 5225:2cedb218a048

Last change on this file since 5225:2cedb218a048 was 5225:2cedb218a048, checked in by phi, 12 years ago

added const keywords to roar_simple_play_file()

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