source: roaraudio/libroar/vio_stream.c @ 6076:b81c397aee90

Last change on this file since 6076:b81c397aee90 was 6067:53fec3e5f6cf, checked in by phi, 9 years ago

updated copyright headers

File size: 13.5 KB
Line 
1//vio_stream.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2015
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
38#ifdef ROAR_TARGET_WIN32
39static inline int inet_aton(const char *s, struct in_addr *a) {
40 int b0, b1, b2, b3;
41
42 if (sscanf(s, "%d.%d.%d.%d", &b0, &b1, &b2, &b3) < 4)
43  return 0;
44
45 a->s_addr = inet_addr(s);
46
47 return a->s_addr != INADDR_NONE;
48}
49#endif
50
51static ssize_t _vio_stream_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
52 return roar_vio_read(roar_get_connection_vio2(vio->inst), buf, count);
53}
54
55static ssize_t _vio_stream_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
56 return roar_vio_write(roar_get_connection_vio2(vio->inst), buf, count);
57}
58
59static roar_off_t   _vio_stream_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
60 return roar_vio_lseek(roar_get_connection_vio2(vio->inst), offset, whence);
61}
62static int     _vio_stream_sync    (struct roar_vio_calls * vio) {
63 return roar_vio_sync(roar_get_connection_vio2(vio->inst));
64}
65static int     _vio_stream_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
66 if (vio == NULL) {
67  roar_err_set(ROAR_ERROR_FAULT);
68  return -1;
69 }
70
71 if (cmd == -1) {
72  roar_err_set(ROAR_ERROR_INVAL);
73  return -1;
74 }
75
76 switch (cmd) {
77  case ROAR_VIO_CTL_GET_NAME:
78    if ( data == NULL ) {
79     roar_err_set(ROAR_ERROR_FAULT);
80     return -1;
81    }
82
83    *(char**)data = "stream";
84    return 0;
85   break;
86  case ROAR_VIO_CTL_GET_NEXT:
87    *(struct roar_vio_calls **)data = roar_get_connection_vio2(vio->inst);
88    return 0;
89   break;
90  case ROAR_VIO_CTL_SET_NEXT:
91    roar_err_set(ROAR_ERROR_NOTSUP);
92    return -1;
93   break;
94  case ROAR_VIO_CTL_NONBLOCK:
95    return roar_vio_ctl(roar_get_connection_vio2(vio->inst), ROAR_VIO_CTL_NONBLOCK, data);
96   break;
97 }
98
99 return roar_vio_ctl(roar_get_connection_vio2(vio->inst), cmd, data);
100}
101
102static int     _vio_stream_close   (struct roar_vio_calls * vio) {
103 roar_vio_close(roar_get_connection_vio2(vio->inst));
104 roar_mm_free(vio->inst);
105
106 return 0;
107}
108
109int     roar_vio_simple_stream (struct roar_vio_calls * calls,
110                                uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec,
111                                const char * server, int dir, const char * name, int mixer) {
112 struct roar_connection * con = NULL;
113 struct roar_stream       stream;
114 int err;
115
116 if ( calls == NULL ) {
117  roar_err_set(ROAR_ERROR_FAULT);
118  return -1;
119 }
120
121 if ( roar_stream_new(&stream, rate, channels, bits, codec) == -1 )
122  return -1;
123
124 con = roar_mm_malloc(sizeof(struct roar_connection));
125 if ( con == NULL )
126  return -1;
127
128 memset(con, 0, sizeof(struct roar_connection));
129
130 if ( roar_simple_connect(con, server, name) == -1 ) {
131  roar_mm_free_noerror(con);
132  return -1;
133 }
134
135 if ( roar_stream_connect(con, &stream, dir, mixer) == -1 ) {
136  err = roar_error;
137  roar_disconnect(con);
138  roar_mm_free(con);
139  roar_error = err;
140  return -1;
141 }
142
143 if ( roar_stream_exec(con, &stream) == -1 ) {
144  err = roar_error;
145  roar_disconnect(con);
146  roar_mm_free(con);
147  roar_error = err;
148  return -1;
149 }
150
151 roar_vio_clear_calls(calls);
152
153 calls->inst       = con;
154 calls->read       = _vio_stream_read;
155 calls->write      = _vio_stream_write;
156 calls->lseek      = _vio_stream_lseek;
157 calls->sync       = _vio_stream_sync;
158 calls->ctl        = _vio_stream_ctl;
159 calls->close      = _vio_stream_close;
160
161 if ( dir == ROAR_DIR_PLAY ) {
162  roar_vio_shutdown(calls, SHUT_RD);
163 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
164  roar_vio_shutdown(calls, SHUT_WR);
165 }
166
167 return 0;
168}
169
170static inline int _roar_simple_new_stream_obj_try_select (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer, struct roar_sockname * sockname, int * fh, int * can_go_on) {
171#ifdef ROAR_HAVE_SELECT
172 int confh;
173 fd_set fds;
174 struct timeval timeout = {10, 0};
175 struct roar_message    mes;
176 int listen;
177 int    port = 0;
178 char file[80] = "";
179 char socketaddr[80];
180 ssize_t socketaddr_len = -1;
181#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
182 int    opt  = 1;
183#endif
184#ifdef ROAR_HAVE_LIBDNET
185 static int count = 0;
186 struct dn_naddr      *binaddr;
187#endif
188#ifdef ROAR_HAVE_IPV4
189 struct sockaddr_in   socket_addr;
190 socklen_t            len            = sizeof(struct sockaddr_in);
191#else
192 struct sockaddr      socket_addr;
193 socklen_t            len            = sizeof(struct sockaddr);
194#endif
195
196 memset(&mes, 0, sizeof(mes));
197 memset(&socket_addr, 0, sizeof(socket_addr));
198
199 *can_go_on = 0;
200
201 switch (sockname->type) {
202#ifdef ROAR_HAVE_LIBDNET
203  case ROAR_SOCKET_TYPE_DECNET:
204    if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) {
205     snprintf(socketaddr+3, sizeof(socketaddr)-3, "roar$TMP%04x%02x", getpid(), count++);
206     snprintf(file, sizeof(file), "%s::%s", roar_socket_get_local_nodename(), socketaddr+3);
207     memcpy(socketaddr, binaddr->a_addr, 2);
208     socketaddr[2] = 0; // object 0.
209     socketaddr_len = 3 + roar_mm_strlen(socketaddr+3);
210    } else {
211     return -1;
212    }
213   break;
214#endif
215#ifdef ROAR_HAVE_IPV4
216  case ROAR_SOCKET_TYPE_TCP:
217    strncpy(file, sockname->addr, sizeof(file) - 1);
218    roar_err_set(ROAR_ERROR_NONE);
219    if ( inet_aton(sockname->addr, &socket_addr.sin_addr) == 0 ) {
220     roar_err_update();
221     return -1;
222    }
223    memcpy(socketaddr, &socket_addr.sin_addr.s_addr, 4);
224    socketaddr_len = 6;
225   break;
226#endif
227  default:
228    roar_err_set(ROAR_ERROR_AFNOTSUP);
229    return -1;
230   break;
231 }
232
233 roar_libroar_nowarn();
234 if ( (listen = roar_socket_listen(sockname->type, file, port)) == -1 ) {
235  roar_libroar_warn();
236  return -1;
237 }
238 roar_libroar_warn();
239
240 switch (sockname->type) {
241#ifdef ROAR_HAVE_IPV4
242  case ROAR_SOCKET_TYPE_TCP:
243    setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
244
245    len = sizeof(struct sockaddr_in);
246    if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
247     return -1;
248    }
249    ((uint16_t*)socketaddr)[3] = socket_addr.sin_port;
250    port = ROAR_NET2HOST16(socket_addr.sin_port);
251    ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
252   break;
253#endif
254#ifdef ROAR_HAVE_LIBDNET
255  case ROAR_SOCKET_TYPE_DECNET:
256    setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
257   break;
258#endif
259 }
260
261 if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname->type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) == -1 ) {
262  close(listen);
263  *can_go_on = 1;
264  return -1;
265 } else {
266
267  FD_ZERO(&fds);
268  FD_SET(listen, &fds);
269
270  confh = roar_get_connection_fh(con);
271
272  if ( confh != -1 ) {
273   FD_SET(confh, &fds);
274  }
275
276  if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
277   close(listen);
278
279   // we don't need to check the content as we know it failed...
280   if ( roar_recv_message(con, &mes, NULL) == -1 )
281    return -1;
282
283   if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
284    return -1;
285
286   *can_go_on = 1;
287   return -1;
288  }
289
290  if ( FD_ISSET(listen, &fds) ) {
291   if ( (*fh = accept(listen, NULL, NULL)) != -1 ) {
292    /* TODO: FIXME: XXX: errr, do we need any error handling here? */
293   }
294
295   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
296    if ( *fh != -1 )
297     close(*fh);
298    *fh = -1;
299   } else if ( mes.cmd != ROAR_CMD_OK ) {
300    if ( *fh != -1 )
301     close(*fh);
302    *fh = -1;
303   }
304  } else {
305   // we don't need to check the content as we know it failed...
306   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
307    close(listen);
308    return -1;
309   }
310
311   if ( mes.cmd != ROAR_CMD_OK ) {
312    close(listen);
313    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
314     return -1;
315
316    *can_go_on = 1;
317    return -1;
318   } else { // seems like we have a positive reply. So we retry the listen socket:
319    FD_ZERO(&fds);
320    FD_SET(listen, &fds);
321    timeout.tv_sec = 0;
322    timeout.tv_usec = 128000L;
323    *fh = -1;
324    if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) {
325     if ( (*fh = accept(listen, NULL, NULL)) == -1 ) {
326      close(listen);
327      if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
328       return -1;
329
330      *can_go_on = 1;
331      return -1;
332     }
333    }
334   }
335  }
336  close(listen);
337  return 0;
338 }
339
340#else
341 roar_err_set(ROAR_ERROR_NOSYS);
342 return -1;
343#endif
344}
345
346static int _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) {
347 struct roar_libroar_config * config = roar_libroar_get_config();
348 int fh = -1;
349#ifdef ROAR_HAVE_UNIX
350 int socks[2]; // for socketpair()
351#endif
352 struct roar_sockname sockname;
353 int can_go_on = 0;
354
355 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
356
357 if ( config != NULL ) {
358  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
359   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
360  }
361 }
362
363 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
364
365 roar_libroar_nowarn();
366 if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) {
367  roar_libroar_warn();
368#ifdef ROAR_OS_OPENBSD
369  sockname.type = ROAR_SOCKET_TYPE_UNIX;
370#else
371  ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
372
373  return -1;
374#endif
375 }
376 roar_libroar_warn();
377
378 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
379
380 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
381  return -1;
382 }
383
384 if ( roar_stream_connect(con, s, dir, mixer) == -1 ) {
385  return -1;
386 }
387
388 if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) {
389  do {
390   if ( _roar_simple_new_stream_obj_try_select(con, s,
391                                               rate, channels, bits, codec, dir, mixer,
392                                               &sockname, &fh, &can_go_on) != -1 )
393    break;
394   if ( !can_go_on )
395    return -1;
396
397   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
398  } while (0);
399 } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX
400#ifdef ROAR_HAVE_UNIX
401  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
402   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
403                                          // as we return -1 in both whys
404   return -1;
405  }
406
407  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
408   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
409                                          // as we return -1 anyway.
410   close(socks[0]);
411   close(socks[1]);
412
413   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
414  }
415
416  close(socks[0]);
417  fh = socks[1];
418#else
419  roar_kick(con, ROAR_OT_STREAM, s->id);
420  return -1;
421#endif
422 }
423
424 if ( fh != -1 ) {
425  if ( dir == ROAR_DIR_PLAY ) {
426   (void)ROAR_SHUTDOWN(fh, SHUT_RD);
427  } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
428   (void)ROAR_SHUTDOWN(fh, SHUT_WR);
429  }
430 }
431
432 s->fh = fh;
433
434 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = %i", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer, fh);
435 return fh;
436}
437
438int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
439                                        struct roar_connection * con,
440                                        struct roar_stream * s,
441                                        uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec,
442                                        int dir, int mixer) {
443 struct roar_stream stream;
444 int fh;
445
446 ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?");
447
448 if ( calls == NULL ) {
449  roar_err_set(ROAR_ERROR_FAULT);
450  return -1;
451 }
452
453 if ( s == NULL )
454  s = &stream;
455
456 if ( (fh = _roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir, mixer)) == -1 ) {
457  ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1");
458  return -1;
459 }
460
461 ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh);
462
463 return roar_vio_open_fh_socket(calls, fh);
464}
465
466//ll
Note: See TracBrowser for help on using the repository browser.