source: roaraudio/libroar/simple.c @ 5376:c8f94c6ad2a3

Last change on this file since 5376:c8f94c6ad2a3 was 5296:b7fd2c45243e, checked in by phi, 12 years ago

Updated connection object. (Closes: #188)

File size: 11.6 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
[5289]38static int roar_simple_stream_obj  (struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, const char * server, int dir, const char * name, int mixer) _LIBROAR_ATTR_DEPRECATED;
39
[5114]40int roar_simple_connect (struct roar_connection * con, const char * server, const char * name) {
[4806]41 return roar_simple_connect2(con, server, name, 0, 0);
42}
43
[5114]44int roar_simple_connect2(struct roar_connection * con, const char * server, const char * name, int flags, uint_least32_t timeout) {
[0]45
46 ROAR_DBG("roar_simple_connect(*): trying to connect...");
47
[5237]48 if ( roar_connect(con, server, flags, timeout) == -1 ) {
[0]49  ROAR_DBG("roar_simple_connect(*): roar_connect() faild!");
50  return -1;
51 }
52
53 if ( roar_identify(con, name) == -1 ) {
54  ROAR_DBG("roar_simple_connect(*): roar_identify() faild!");
55  return -1;
56 }
57
58 if ( roar_auth(con) == -1 ) {
59  ROAR_DBG("roar_simple_connect(*): roar_auth() faild!");
60  return -1;
61 }
62
63 return 0;
64}
65
[5289]66static int roar_simple_stream_obj  (struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, const char * server, int dir, const char * name, int mixer) {
[0]67 struct roar_connection con;
[1660]68 int ret;
[5148]69 int safe_error;
[0]70
[2818]71 roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL);
72
[0]73 if ( roar_simple_connect(&con, server, name) == -1 ) {
[5226]74  ROAR_DBG("roar_simple_stream_obj(*): roar_simple_connect() faild!");
75  ROAR_ERR("roar_simple_stream_obj(*): Can not connect to server");
[0]76  return -1;
77 }
78
[1158]79 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[5148]80  safe_error = roar_error;
[0]81  roar_disconnect(&con);
[5148]82  roar_err_set(safe_error);
[0]83  return -1;
84 }
85
[5289]86 if ( roar_stream_connect(&con, s, dir, mixer) == -1 ) {
[5148]87  safe_error = roar_error;
[0]88  roar_disconnect(&con);
[5148]89  roar_err_set(safe_error);
[0]90  return -1;
91 }
92
[1158]93 if ( roar_stream_exec(&con, s) == -1 ) {
[5148]94  safe_error = roar_error;
[0]95  roar_disconnect(&con);
[5148]96  roar_err_set(safe_error);
[0]97  return -1;
98 }
99
[3857]100 roar_libroar_nowarn();
[1660]101 if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
[5148]102  safe_error = roar_error;
[3857]103  roar_libroar_warn();
[1660]104  roar_disconnect(&con);
[5148]105  roar_err_set(safe_error);
[1660]106  return -1;
[54]107 }
[3857]108 roar_libroar_warn();
[54]109
[1660]110 if ( dir == ROAR_DIR_PLAY ) {
[5017]111  (void)ROAR_SHUTDOWN(ret, SHUT_RD);
[1660]112 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
[5017]113  (void)ROAR_SHUTDOWN(ret, SHUT_WR);
[1660]114 }
115
116 return ret;
[0]117}
118
[5289]119int roar_simple_new_stream_attachexeced_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) {
[1163]120 int fh;
121
[5296]122 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, roar_get_connection_server(con),
[5289]123                                   dir, "libroar temp stream", mixer)) == -1 )
[1163]124  return -1;
125
126 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
[1476]127#ifdef ROAR_HAVE_IO_POSIX
[1163]128  close(fh);
[1476]129#endif /* no else as we return -1 anyway */
[1163]130  return -1;
131 }
132
[1166]133 s->fh = fh;
134
[1163]135 return fh;
136}
137
[5289]138int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) {
[2481]139 struct roar_libroar_config * config = roar_libroar_get_config();
[1168]140 char file[80] = {0};
141 int fh = -1, listen = -1;
[82]142 static int count = 0;
[487]143 int    type = ROAR_SOCKET_TYPE_UNIX;
144 int    port = 0;
[1396]145#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
[493]146 int    opt  = 1;
[1396]147#endif
[1466]148#ifdef ROAR_HAVE_IPV4
[487]149 struct sockaddr_in   socket_addr;
150 socklen_t            len            = sizeof(struct sockaddr_in);
[1466]151#else
152 struct sockaddr      socket_addr;
153 socklen_t            len            = sizeof(struct sockaddr);
154#endif
[1465]155#ifdef ROAR_HAVE_SELECT
[5061]156 int confh;
[1161]157 fd_set fds;
158 struct timeval timeout = {10, 0};
[1465]159 struct roar_message    mes;
160#endif
[1396]161#ifdef ROAR_HAVE_UNIX
[1168]162 int socks[2]; // for socketpair()
[1396]163#endif
[82]164
[3479]165 // make valgrind happy
166 memset(&socket_addr, 0, sizeof(socket_addr));
[3897]167#ifdef ROAR_HAVE_SELECT
[3479]168 memset(&mes,         0, sizeof(mes));
[3897]169#endif
[3479]170
[3012]171 roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL);
172
[2481]173 if ( config != NULL ) {
174  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
[5289]175   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
[2481]176  }
177 }
178
[1476]179#ifdef ROAR_HAVE_BSDSOCKETS
[3857]180 roar_libroar_nowarn();
[1660]181 if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
[3857]182  roar_libroar_warn();
[82]183  return -1;
184 }
[3857]185 roar_libroar_warn();
[1476]186#else
187 return -1;
188#endif
[82]189
[1178]190 if ( len == 0 ) {
191#ifdef ROAR_OS_OPENBSD
192  ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD");
[1466]193  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
[1178]194#else
[505]195  return -1;
[1178]196#endif
197 }
198
[1466]199 switch (((struct sockaddr*)&socket_addr)->sa_family) {
[1359]200#ifdef ROAR_HAVE_UNIX
[1178]201  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
[1359]202#endif
203#ifdef ROAR_HAVE_IPV4
[1178]204  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
[1359]205#endif
206#ifdef ROAR_HAVE_LIBDNET
[1178]207  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
[1359]208#endif
[1178]209  default:
210    return -1;
211   break;
[487]212 }
213
[1187]214 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[526]215  if ( roar_socket_get_local_nodename() ) {
[5024]216   snprintf(file, 24, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
[526]217  } else {
218   return -1;
219  }
[1380]220#ifdef ROAR_HAVE_IPV4
[487]221 } else {
[5024]222  strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1);
[1380]223#endif
[487]224 }
225
[1168]226 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
227  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
228   return -1;
229  }
[487]230 }
[451]231
[487]232 if ( type == ROAR_SOCKET_TYPE_INET ) {
[1380]233#ifdef ROAR_HAVE_IPV4
[487]234  len = sizeof(struct sockaddr_in);
[3905]235  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
[493]236
[487]237  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
238   return -1;
239  }
240  port = ROAR_NET2HOST16(socket_addr.sin_port);
241  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
[1380]242#else
243  return -1;
244#endif
[532]245 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[1395]246#ifdef ROAR_HAVE_LIBDNET
[532]247  len = sizeof(struct sockaddr_in);
248  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
[1395]249#else
250  return -1;
251#endif
[487]252 }
[451]253
[119]254 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[82]255  return -1;
256 }
257
[5289]258 if ( roar_stream_connect(con, s, dir, mixer) == -1 ) {
[82]259  return -1;
260 }
261
[1168]262 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
[1465]263#ifdef ROAR_HAVE_SELECT
[1168]264  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
[1161]265
[1168]266   FD_ZERO(&fds);
267   FD_SET(listen, &fds);
[1163]268
[5061]269   confh = roar_get_connection_fh(con);
270
271   if ( confh != -1 ) {
272    FD_SET(confh, &fds);
273   }
274
[5163]275   if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
[1168]276    close(listen);
[1163]277
[1168]278    // we don't need to check the content as we know it failed...
279    if ( roar_recv_message(con, &mes, NULL) == -1 )
280     return -1;
[1161]281
[1168]282    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
283     return -1;
[1161]284
[5289]285    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
[82]286   }
[1168]287
[5061]288   if ( FD_ISSET(listen, &fds) ) {
289    if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
290     /* errr, do we need we any error handling here? */
291    }
[5163]292
293    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
294     if ( fh != -1 )
295      close(fh);
296     fh = -1;
297    } else if ( mes.cmd != ROAR_CMD_OK ) {
298     if ( fh != -1 )
299      close(fh);
300     fh = -1;
301    }
[5061]302   } else {
303    // we don't need to check the content as we know it failed...
[5163]304    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
305     close(listen);
[5061]306     return -1;
[5163]307    }
[5061]308
[5163]309    if ( mes.cmd != ROAR_CMD_OK ) {
310     close(listen);
311     if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
312      return -1;
[5061]313
[5289]314     return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
[5163]315    } else { // seems like we have a positive reply. So we retry the listen socket:
316     FD_ZERO(&fds);
317     FD_SET(listen, &fds);
318     timeout.tv_sec = 0;
319     timeout.tv_usec = 128000L;
320     fh = -1;
321     if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) {
322      if ( (fh = accept(listen, NULL, NULL)) == -1 ) {
323       close(listen);
324       if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
325        return -1;
326
[5289]327       return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
[5163]328      }
329     }
330    }
[82]331   }
[1168]332  }
333
334  close(listen);
[1465]335#else
336  return -1;
337#endif
[1168]338 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
[1395]339#ifdef ROAR_HAVE_UNIX
[1168]340  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
341   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
342                                          // as we return -1 in both whys
343   return -1;
344  }
345
346  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
347   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
[3651]348                                          // as we return -1 anyway.
349   close(socks[0]);
350   close(socks[1]);
[1168]351   return -1;
352  }
353
354  close(socks[0]);
355  fh = socks[1];
[1395]356#else
357  roar_kick(con, ROAR_OT_STREAM, s->id);
358  return -1;
359#endif
[82]360 }
361
[1168]362/*
[487]363 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
364  unlink(file);
365 }
[1168]366*/
367
[5061]368 if ( fh != -1 ) {
369  if ( dir == ROAR_DIR_PLAY ) {
370   (void)ROAR_SHUTDOWN(fh, SHUT_RD);
371  } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
372   (void)ROAR_SHUTDOWN(fh, SHUT_WR);
373  }
[1168]374 }
[82]375
[505]376 s->fh = fh;
377
[82]378 return fh;
379}
380
[235]381
[5225]382int roar_simple_play_file(const char * file, const char * server, const char * name) {
[5110]383 roar_vs_t * vss;
384 int err;
[235]385
[5110]386 vss = roar_vs_new_from_file(server, name, file, &err);
387
388 if ( vss == NULL ) {
389  roar_err_set(err);
[235]390  return -1;
391 }
392
[5110]393 if ( roar_vs_run(vss, &err) == -1 ) {
394  roar_err_set(err);
395  return -1;
396 }
397
398 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
399 roar_vs_close(vss, ROAR_VS_FALSE, NULL);
400
401 return 0;
[235]402}
403
[2697]404int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) {
[2656]405 struct roar_stream parent_stream;
406
407 if ( con == NULL || s == NULL || parent < 0 )
408  return -1;
409
[2697]410 if ( dir == -1 ) {
411  if ( roar_get_stream(con, &parent_stream, parent) == -1 )
412   return -1;
[2656]413
[2697]414  if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 )
415   return -1;
416 }
[2656]417
418 if ( roar_stream_set_rel_id(s, parent) == -1 )
419  return -1;
420
[5238]421 if ( roar_stream_connect(con, s, dir, -1) == -1 )
[2656]422  return -1;
423
[5238]424 if ( roar_stream_set_flags(con, s, ROAR_FLAG_VIRTUAL, ROAR_SET_FLAG) == -1 ) {
[2656]425  roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s));
426  return -1;
427 }
428
429 return 0;
[2647]430}
[0]431
432//ll
Note: See TracBrowser for help on using the repository browser.