source: roaraudio/libroar/simple.c @ 5289:ddb3677af4d0

Last change on this file since 5289:ddb3677af4d0 was 5289:ddb3677af4d0, checked in by phi, 12 years ago

use support to set mixer ID up at least one API layer

File size: 11.6 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
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
40int roar_simple_connect (struct roar_connection * con, const char * server, const char * name) {
41 return roar_simple_connect2(con, server, name, 0, 0);
42}
43
44int roar_simple_connect2(struct roar_connection * con, const char * server, const char * name, int flags, uint_least32_t timeout) {
45
46 ROAR_DBG("roar_simple_connect(*): trying to connect...");
47
48 if ( roar_connect(con, server, flags, timeout) == -1 ) {
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
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) {
67 struct roar_connection con;
68 int ret;
69 int safe_error;
70
71 roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL);
72
73 if ( roar_simple_connect(&con, server, name) == -1 ) {
74  ROAR_DBG("roar_simple_stream_obj(*): roar_simple_connect() faild!");
75  ROAR_ERR("roar_simple_stream_obj(*): Can not connect to server");
76  return -1;
77 }
78
79 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
80  safe_error = roar_error;
81  roar_disconnect(&con);
82  roar_err_set(safe_error);
83  return -1;
84 }
85
86 if ( roar_stream_connect(&con, s, dir, mixer) == -1 ) {
87  safe_error = roar_error;
88  roar_disconnect(&con);
89  roar_err_set(safe_error);
90  return -1;
91 }
92
93 if ( roar_stream_exec(&con, s) == -1 ) {
94  safe_error = roar_error;
95  roar_disconnect(&con);
96  roar_err_set(safe_error);
97  return -1;
98 }
99
100 roar_libroar_nowarn();
101 if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
102  safe_error = roar_error;
103  roar_libroar_warn();
104  roar_disconnect(&con);
105  roar_err_set(safe_error);
106  return -1;
107 }
108 roar_libroar_warn();
109
110 if ( dir == ROAR_DIR_PLAY ) {
111  (void)ROAR_SHUTDOWN(ret, SHUT_RD);
112 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
113  (void)ROAR_SHUTDOWN(ret, SHUT_WR);
114 }
115
116 return ret;
117}
118
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) {
120 int fh;
121
122 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */,
123                                   dir, "libroar temp stream", mixer)) == -1 )
124  return -1;
125
126 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
127#ifdef ROAR_HAVE_IO_POSIX
128  close(fh);
129#endif /* no else as we return -1 anyway */
130  return -1;
131 }
132
133 s->fh = fh;
134
135 return fh;
136}
137
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) {
139 struct roar_libroar_config * config = roar_libroar_get_config();
140 char file[80] = {0};
141 int fh = -1, listen = -1;
142 static int count = 0;
143 int    type = ROAR_SOCKET_TYPE_UNIX;
144 int    port = 0;
145#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
146 int    opt  = 1;
147#endif
148#ifdef ROAR_HAVE_IPV4
149 struct sockaddr_in   socket_addr;
150 socklen_t            len            = sizeof(struct sockaddr_in);
151#else
152 struct sockaddr      socket_addr;
153 socklen_t            len            = sizeof(struct sockaddr);
154#endif
155#ifdef ROAR_HAVE_SELECT
156 int confh;
157 fd_set fds;
158 struct timeval timeout = {10, 0};
159 struct roar_message    mes;
160#endif
161#ifdef ROAR_HAVE_UNIX
162 int socks[2]; // for socketpair()
163#endif
164
165 // make valgrind happy
166 memset(&socket_addr, 0, sizeof(socket_addr));
167#ifdef ROAR_HAVE_SELECT
168 memset(&mes,         0, sizeof(mes));
169#endif
170
171 roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL);
172
173 if ( config != NULL ) {
174  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
175   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
176  }
177 }
178
179#ifdef ROAR_HAVE_BSDSOCKETS
180 roar_libroar_nowarn();
181 if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
182  roar_libroar_warn();
183  return -1;
184 }
185 roar_libroar_warn();
186#else
187 return -1;
188#endif
189
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");
193  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
194#else
195  return -1;
196#endif
197 }
198
199 switch (((struct sockaddr*)&socket_addr)->sa_family) {
200#ifdef ROAR_HAVE_UNIX
201  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
202#endif
203#ifdef ROAR_HAVE_IPV4
204  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
205#endif
206#ifdef ROAR_HAVE_LIBDNET
207  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
208#endif
209  default:
210    return -1;
211   break;
212 }
213
214 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
215  if ( roar_socket_get_local_nodename() ) {
216   snprintf(file, 24, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
217  } else {
218   return -1;
219  }
220#ifdef ROAR_HAVE_IPV4
221 } else {
222  strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1);
223#endif
224 }
225
226 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
227  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
228   return -1;
229  }
230 }
231
232 if ( type == ROAR_SOCKET_TYPE_INET ) {
233#ifdef ROAR_HAVE_IPV4
234  len = sizeof(struct sockaddr_in);
235  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
236
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);
242#else
243  return -1;
244#endif
245 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
246#ifdef ROAR_HAVE_LIBDNET
247  len = sizeof(struct sockaddr_in);
248  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
249#else
250  return -1;
251#endif
252 }
253
254 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
255  return -1;
256 }
257
258 if ( roar_stream_connect(con, s, dir, mixer) == -1 ) {
259  return -1;
260 }
261
262 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
263#ifdef ROAR_HAVE_SELECT
264  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
265
266   FD_ZERO(&fds);
267   FD_SET(listen, &fds);
268
269   confh = roar_get_connection_fh(con);
270
271   if ( confh != -1 ) {
272    FD_SET(confh, &fds);
273   }
274
275   if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
276    close(listen);
277
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;
281
282    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
283     return -1;
284
285    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
286   }
287
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    }
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    }
302   } else {
303    // we don't need to check the content as we know it failed...
304    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
305     close(listen);
306     return -1;
307    }
308
309    if ( mes.cmd != ROAR_CMD_OK ) {
310     close(listen);
311     if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
312      return -1;
313
314     return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
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
327       return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
328      }
329     }
330    }
331   }
332  }
333
334  close(listen);
335#else
336  return -1;
337#endif
338 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
339#ifdef ROAR_HAVE_UNIX
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
348                                          // as we return -1 anyway.
349   close(socks[0]);
350   close(socks[1]);
351   return -1;
352  }
353
354  close(socks[0]);
355  fh = socks[1];
356#else
357  roar_kick(con, ROAR_OT_STREAM, s->id);
358  return -1;
359#endif
360 }
361
362/*
363 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
364  unlink(file);
365 }
366*/
367
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  }
374 }
375
376 s->fh = fh;
377
378 return fh;
379}
380
381
382int roar_simple_play_file(const char * file, const char * server, const char * name) {
383 roar_vs_t * vss;
384 int err;
385
386 vss = roar_vs_new_from_file(server, name, file, &err);
387
388 if ( vss == NULL ) {
389  roar_err_set(err);
390  return -1;
391 }
392
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;
402}
403
404int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) {
405 struct roar_stream parent_stream;
406
407 if ( con == NULL || s == NULL || parent < 0 )
408  return -1;
409
410 if ( dir == -1 ) {
411  if ( roar_get_stream(con, &parent_stream, parent) == -1 )
412   return -1;
413
414  if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 )
415   return -1;
416 }
417
418 if ( roar_stream_set_rel_id(s, parent) == -1 )
419  return -1;
420
421 if ( roar_stream_connect(con, s, dir, -1) == -1 )
422  return -1;
423
424 if ( roar_stream_set_flags(con, s, ROAR_FLAG_VIRTUAL, ROAR_SET_FLAG) == -1 ) {
425  roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s));
426  return -1;
427 }
428
429 return 0;
430}
431
432//ll
Note: See TracBrowser for help on using the repository browser.