source: roaraudio/libroar/simple.c @ 5248:0133acb5ae31

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