source: roaraudio/libroar/simple.c @ 5163:22c49b4be8cd

Last change on this file since 5163:22c49b4be8cd was 5163:22c49b4be8cd, checked in by phi, 13 years ago

fix for linux 3.0.*'s select()

File size: 13.4 KB
RevLine 
[0]1//simple.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]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
[0]36#include "libroar.h"
37
[5114]38int roar_simple_connect (struct roar_connection * con, const char * server, const char * name) {
[4806]39 return roar_simple_connect2(con, server, name, 0, 0);
40}
41
[5114]42int roar_simple_connect2(struct roar_connection * con, const char * server, const char * name, int flags, uint_least32_t timeout) {
[0]43
44 ROAR_DBG("roar_simple_connect(*): trying to connect...");
45
[4806]46 if ( roar_connect2(con, server, flags, timeout) == -1 ) {
[0]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
[5114]64int roar_simple_stream(int rate, int channels, int bits, int codec, const char * server, int dir, const char * name) {
[1158]65 struct roar_stream     s;
66
[2818]67 roar_debug_warn_sysio("roar_simple_stream", "roar_vio_simple_stream", NULL);
68
[1158]69 return roar_simple_stream_obj(&s, rate, channels, bits, codec, server, dir, name);
70}
71
[5118]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) {
[0]73 struct roar_connection con;
[1660]74 int ret;
[5148]75 int safe_error;
[0]76
[2818]77 roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL);
78
[0]79 if ( roar_simple_connect(&con, server, name) == -1 ) {
80  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
[1094]81  ROAR_ERR("roar_simple_stream(*): Can not connect to server");
[0]82  return -1;
83 }
84
[1158]85 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[5148]86  safe_error = roar_error;
[0]87  roar_disconnect(&con);
[5148]88  roar_err_set(safe_error);
[0]89  return -1;
90 }
91
[5118]92 if ( roar_stream_connect2(&con, s, dir, -1) == -1 ) {
[5148]93  safe_error = roar_error;
[0]94  roar_disconnect(&con);
[5148]95  roar_err_set(safe_error);
[0]96  return -1;
97 }
98
[1158]99 if ( roar_stream_exec(&con, s) == -1 ) {
[5148]100  safe_error = roar_error;
[0]101  roar_disconnect(&con);
[5148]102  roar_err_set(safe_error);
[0]103  return -1;
104 }
105
[3857]106 roar_libroar_nowarn();
[1660]107 if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
[5148]108  safe_error = roar_error;
[3857]109  roar_libroar_warn();
[1660]110  roar_disconnect(&con);
[5148]111  roar_err_set(safe_error);
[1660]112  return -1;
[54]113 }
[3857]114 roar_libroar_warn();
[54]115
[1660]116 if ( dir == ROAR_DIR_PLAY ) {
[5017]117  (void)ROAR_SHUTDOWN(ret, SHUT_RD);
[1660]118 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
[5017]119  (void)ROAR_SHUTDOWN(ret, SHUT_WR);
[1660]120 }
121
122 return ret;
[0]123}
124
[1163]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
[1476]128 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */,
[1163]129                                   dir, "libroar temp stream")) == -1 )
130  return -1;
131
132 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
[1476]133#ifdef ROAR_HAVE_IO_POSIX
[1163]134  close(fh);
[1476]135#endif /* no else as we return -1 anyway */
[1163]136  return -1;
137 }
138
[1166]139 s->fh = fh;
140
[1163]141 return fh;
142}
143
[82]144int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) {
145 struct roar_stream     s;
[3857]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;
[119]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) {
[2481]158 struct roar_libroar_config * config = roar_libroar_get_config();
[1168]159 char file[80] = {0};
160 int fh = -1, listen = -1;
[82]161 static int count = 0;
[487]162 int    type = ROAR_SOCKET_TYPE_UNIX;
163 int    port = 0;
[1396]164#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
[493]165 int    opt  = 1;
[1396]166#endif
[1466]167#ifdef ROAR_HAVE_IPV4
[487]168 struct sockaddr_in   socket_addr;
169 socklen_t            len            = sizeof(struct sockaddr_in);
[1466]170#else
171 struct sockaddr      socket_addr;
172 socklen_t            len            = sizeof(struct sockaddr);
173#endif
[1465]174#ifdef ROAR_HAVE_SELECT
[5061]175 int confh;
[1161]176 fd_set fds;
177 struct timeval timeout = {10, 0};
[1465]178 struct roar_message    mes;
179#endif
[1396]180#ifdef ROAR_HAVE_UNIX
[1168]181 int socks[2]; // for socketpair()
[1396]182#endif
[82]183
[3479]184 // make valgrind happy
185 memset(&socket_addr, 0, sizeof(socket_addr));
[3897]186#ifdef ROAR_HAVE_SELECT
[3479]187 memset(&mes,         0, sizeof(mes));
[3897]188#endif
[3479]189
[3012]190 roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL);
191
[2481]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
[1476]198#ifdef ROAR_HAVE_BSDSOCKETS
[3857]199 roar_libroar_nowarn();
[1660]200 if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
[3857]201  roar_libroar_warn();
[82]202  return -1;
203 }
[3857]204 roar_libroar_warn();
[1476]205#else
206 return -1;
207#endif
[82]208
[1178]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");
[1466]212  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
[1178]213#else
[505]214  return -1;
[1178]215#endif
216 }
217
[1466]218 switch (((struct sockaddr*)&socket_addr)->sa_family) {
[1359]219#ifdef ROAR_HAVE_UNIX
[1178]220  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
[1359]221#endif
222#ifdef ROAR_HAVE_IPV4
[1178]223  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
[1359]224#endif
225#ifdef ROAR_HAVE_LIBDNET
[1178]226  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
[1359]227#endif
[1178]228  default:
229    return -1;
230   break;
[487]231 }
232
[1187]233 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[526]234  if ( roar_socket_get_local_nodename() ) {
[5024]235   snprintf(file, 24, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
[526]236  } else {
237   return -1;
238  }
[1380]239#ifdef ROAR_HAVE_IPV4
[487]240 } else {
[5024]241  strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1);
[1380]242#endif
[487]243 }
244
[1168]245 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
246  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
247   return -1;
248  }
[487]249 }
[451]250
[487]251 if ( type == ROAR_SOCKET_TYPE_INET ) {
[1380]252#ifdef ROAR_HAVE_IPV4
[487]253  len = sizeof(struct sockaddr_in);
[3905]254  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
[493]255
[487]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);
[1380]261#else
262  return -1;
263#endif
[532]264 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[1395]265#ifdef ROAR_HAVE_LIBDNET
[532]266  len = sizeof(struct sockaddr_in);
267  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
[1395]268#else
269  return -1;
270#endif
[487]271 }
[451]272
[119]273 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[82]274  return -1;
275 }
276
[5118]277 if ( roar_stream_connect2(con, s, dir, -1) == -1 ) {
[82]278  return -1;
279 }
280
[1168]281 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
[1465]282#ifdef ROAR_HAVE_SELECT
[1168]283  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
[1161]284
[1168]285   FD_ZERO(&fds);
286   FD_SET(listen, &fds);
[1163]287
[5061]288   confh = roar_get_connection_fh(con);
289
290   if ( confh != -1 ) {
291    FD_SET(confh, &fds);
292   }
293
[5163]294   if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
[1168]295    close(listen);
[1163]296
[1168]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;
[1161]300
[1168]301    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
302     return -1;
[1161]303
[1168]304    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
[82]305   }
[1168]306
[5061]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    }
[5163]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    }
[5061]321   } else {
322    // we don't need to check the content as we know it failed...
[5163]323    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
324     close(listen);
[5061]325     return -1;
[5163]326    }
[5061]327
[5163]328    if ( mes.cmd != ROAR_CMD_OK ) {
329     close(listen);
330     if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
331      return -1;
[5061]332
[5163]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    }
[82]350   }
[1168]351  }
352
353  close(listen);
[1465]354#else
355  return -1;
356#endif
[1168]357 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
[1395]358#ifdef ROAR_HAVE_UNIX
[1168]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
[3651]367                                          // as we return -1 anyway.
368   close(socks[0]);
369   close(socks[1]);
[1168]370   return -1;
371  }
372
373  close(socks[0]);
374  fh = socks[1];
[1395]375#else
376  roar_kick(con, ROAR_OT_STREAM, s->id);
377  return -1;
378#endif
[82]379 }
380
[1168]381/*
[487]382 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
383  unlink(file);
384 }
[1168]385*/
386
[5061]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  }
[1168]393 }
[82]394
[505]395 s->fh = fh;
396
[82]397 return fh;
398}
399
[235]400
401int roar_simple_play_file(char * file, char * server, char * name) {
[5110]402 roar_vs_t * vss;
403 int err;
[235]404
[5110]405 vss = roar_vs_new_from_file(server, name, file, &err);
406
407 if ( vss == NULL ) {
408  roar_err_set(err);
[235]409  return -1;
410 }
411
[5110]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;
[235]421}
422
[0]423int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
[2655]424 roar_debug_warn_sysio("roar_simple_play", "roar_vio_simple_stream", NULL);
425
[0]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) {
[2655]430 roar_debug_warn_sysio("roar_simple_monitor", "roar_vio_simple_stream", NULL);
431
[0]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) {
[2655]436 roar_debug_warn_sysio("roar_simple_record", "roar_vio_simple_stream", NULL);
437
[0]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) {
[2655]442 roar_debug_warn_sysio("roar_simple_filter", "roar_vio_simple_stream", NULL);
443
[0]444 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
445}
446
[2697]447int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) {
[2656]448 struct roar_stream parent_stream;
449
450 if ( con == NULL || s == NULL || parent < 0 )
451  return -1;
452
[2697]453 if ( dir == -1 ) {
454  if ( roar_get_stream(con, &parent_stream, parent) == -1 )
455   return -1;
[2656]456
[2697]457  if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 )
458   return -1;
459 }
[2656]460
461 if ( roar_stream_set_rel_id(s, parent) == -1 )
462  return -1;
463
[5110]464 if ( roar_stream_connect2(con, s, dir, -1) == -1 )
[2656]465  return -1;
466
[5110]467 if ( roar_stream_set_flags2(con, s, ROAR_FLAG_VIRTUAL, ROAR_SET_FLAG) == -1 ) {
[2656]468  roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s));
469  return -1;
470 }
471
472 return 0;
[2647]473}
[0]474
475int roar_simple_close(int fh) {
[2655]476 roar_debug_warn_sysio("roar_simple_close", "roar_vio_close", NULL);
477
[1763]478#ifdef ROAR_TARGET_WIN32
479 closesocket(fh);
480 return 0;
481#else
482
[1476]483#ifdef ROAR_HAVE_IO_POSIX
[0]484 return close(fh);
[1476]485#else
486 return -1;
487#endif
[1763]488
489#endif
[0]490}
491
492int roar_simple_get_standby (int fh) {
493 struct roar_connection con;
494
[2809]495 roar_debug_warn_sysio("roar_simple_get_standby", "roar_get_standby", NULL);
[2655]496
[1660]497 if ( roar_connect_fh(&con, fh) == -1 )
498  return -1;
[0]499
500 return roar_get_standby(&con);
501}
502
503//ll
Note: See TracBrowser for help on using the repository browser.