source: roaraudio/libroar/simple.c @ 3875:8731d0c3e04a

Last change on this file since 3875:8731d0c3e04a was 3857:8ed6d81a32d6, checked in by phi, 14 years ago

use roar_debug_warn_obsolete() and roar_libroar_*warn()

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