source: roaraudio/libroar/simple.c @ 5275:811818eb5b81

Last change on this file since 5275:811818eb5b81 was 5238:5117fb0e7ed4, checked in by phi, 12 years ago

Removed roar_stream_connect(), roar_stream_add_data(), roar_stream_send_data() and roar_stream_set_flags().

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