source: roaraudio/libroar/simple.c @ 4806:988c888752c2

Last change on this file since 4806:988c888752c2 was 4806:988c888752c2, checked in by phi, 13 years ago

Started with support for non-blocking server locating attempts.
Speed up SLP lookup without DA by about 6 sec.

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