source: roaraudio/libroar/simple.c @ 5162:85c55ccd12a9

Last change on this file since 5162:85c55ccd12a9 was 5148:73d76a74be61, checked in by phi, 13 years ago

improved error handling some more, marked some stuff as obsolete

File size: 12.7 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
294   if ( select((confh > listen ? confh : listen) + 1, &fds, &fds, &fds, &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    }
311   } else {
312    // we don't need to check the content as we know it failed...
313    if ( roar_recv_message(con, &mes, NULL) == -1 )
314     return -1;
315
316    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
317     return -1;
318
319    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
[1168]320   }
[82]321   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
322    close(fh);
323    fh = -1;
324   } else if ( mes.cmd != ROAR_CMD_OK ) {
325    close(fh);
326    fh = -1;
327   }
[1168]328  }
329
330  close(listen);
[1465]331#else
332  return -1;
333#endif
[1168]334 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
[1395]335#ifdef ROAR_HAVE_UNIX
[1168]336  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
337   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
338                                          // as we return -1 in both whys
339   return -1;
340  }
341
342  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
343   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
[3651]344                                          // as we return -1 anyway.
345   close(socks[0]);
346   close(socks[1]);
[1168]347   return -1;
348  }
349
350  close(socks[0]);
351  fh = socks[1];
[1395]352#else
353  roar_kick(con, ROAR_OT_STREAM, s->id);
354  return -1;
355#endif
[82]356 }
357
[1168]358/*
[487]359 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
360  unlink(file);
361 }
[1168]362*/
363
[5061]364 if ( fh != -1 ) {
365  if ( dir == ROAR_DIR_PLAY ) {
366   (void)ROAR_SHUTDOWN(fh, SHUT_RD);
367  } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
368   (void)ROAR_SHUTDOWN(fh, SHUT_WR);
369  }
[1168]370 }
[82]371
[505]372 s->fh = fh;
373
[82]374 return fh;
375}
376
[235]377
378int roar_simple_play_file(char * file, char * server, char * name) {
[5110]379 roar_vs_t * vss;
380 int err;
[235]381
[5110]382 vss = roar_vs_new_from_file(server, name, file, &err);
383
384 if ( vss == NULL ) {
385  roar_err_set(err);
[235]386  return -1;
387 }
388
[5110]389 if ( roar_vs_run(vss, &err) == -1 ) {
390  roar_err_set(err);
391  return -1;
392 }
393
394 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
395 roar_vs_close(vss, ROAR_VS_FALSE, NULL);
396
397 return 0;
[235]398}
399
[0]400int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
[2655]401 roar_debug_warn_sysio("roar_simple_play", "roar_vio_simple_stream", NULL);
402
[0]403 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
404}
405
406int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
[2655]407 roar_debug_warn_sysio("roar_simple_monitor", "roar_vio_simple_stream", NULL);
408
[0]409 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
410}
411
412int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
[2655]413 roar_debug_warn_sysio("roar_simple_record", "roar_vio_simple_stream", NULL);
414
[0]415 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
416}
417
418int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
[2655]419 roar_debug_warn_sysio("roar_simple_filter", "roar_vio_simple_stream", NULL);
420
[0]421 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
422}
423
[2697]424int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) {
[2656]425 struct roar_stream parent_stream;
426
427 if ( con == NULL || s == NULL || parent < 0 )
428  return -1;
429
[2697]430 if ( dir == -1 ) {
431  if ( roar_get_stream(con, &parent_stream, parent) == -1 )
432   return -1;
[2656]433
[2697]434  if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 )
435   return -1;
436 }
[2656]437
438 if ( roar_stream_set_rel_id(s, parent) == -1 )
439  return -1;
440
[5110]441 if ( roar_stream_connect2(con, s, dir, -1) == -1 )
[2656]442  return -1;
443
[5110]444 if ( roar_stream_set_flags2(con, s, ROAR_FLAG_VIRTUAL, ROAR_SET_FLAG) == -1 ) {
[2656]445  roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s));
446  return -1;
447 }
448
449 return 0;
[2647]450}
[0]451
452int roar_simple_close(int fh) {
[2655]453 roar_debug_warn_sysio("roar_simple_close", "roar_vio_close", NULL);
454
[1763]455#ifdef ROAR_TARGET_WIN32
456 closesocket(fh);
457 return 0;
458#else
459
[1476]460#ifdef ROAR_HAVE_IO_POSIX
[0]461 return close(fh);
[1476]462#else
463 return -1;
464#endif
[1763]465
466#endif
[0]467}
468
469int roar_simple_get_standby (int fh) {
470 struct roar_connection con;
471
[2809]472 roar_debug_warn_sysio("roar_simple_get_standby", "roar_get_standby", NULL);
[2655]473
[1660]474 if ( roar_connect_fh(&con, fh) == -1 )
475  return -1;
[0]476
477 return roar_get_standby(&con);
478}
479
480//ll
Note: See TracBrowser for help on using the repository browser.