source: roaraudio/libroar/simple.c @ 1476:bcb075f757c8

Last change on this file since 1476:bcb075f757c8 was 1476:bcb075f757c8, checked in by phi, 15 years ago

do not try BSD Socket interface/POSIX IO if the arch/os does not support that

File size: 9.3 KB
RevLine 
[0]1//simple.c:
2
[690]3/*
[1187]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
[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
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
[0]35#include "libroar.h"
36
37int roar_simple_connect (struct roar_connection * con, char * server, char * name) {
38
39 ROAR_DBG("roar_simple_connect(*): trying to connect...");
40
41 if ( roar_connect(con, server) == -1 ) {
42  ROAR_DBG("roar_simple_connect(*): roar_connect() faild!");
43  return -1;
44 }
45
46 if ( roar_identify(con, name) == -1 ) {
47  ROAR_DBG("roar_simple_connect(*): roar_identify() faild!");
48  return -1;
49 }
50
51 if ( roar_auth(con) == -1 ) {
52  ROAR_DBG("roar_simple_connect(*): roar_auth() faild!");
53  return -1;
54 }
55
56 return 0;
57}
58
59int roar_simple_stream(int rate, int channels, int bits, int codec, char * server, int dir, char * name) {
[1158]60 struct roar_stream     s;
61
62 return roar_simple_stream_obj(&s, rate, channels, bits, codec, server, dir, name);
63}
64
65int roar_simple_stream_obj  (struct roar_stream * s, int rate, int channels, int bits, int codec, char * server, int dir, char * name) {
[0]66 struct roar_connection con;
67
68 if ( roar_simple_connect(&con, server, name) == -1 ) {
69  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
[1094]70  ROAR_ERR("roar_simple_stream(*): Can not connect to server");
[0]71  return -1;
72 }
73
[1158]74 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[0]75  roar_disconnect(&con);
76  return -1;
77 }
78
[1158]79 if ( roar_stream_connect(&con, s, dir) == -1 ) {
[0]80  roar_disconnect(&con);
81  return -1;
82 }
83
[1158]84 if ( roar_stream_exec(&con, s) == -1 ) {
[0]85  roar_disconnect(&con);
86  return -1;
87 }
88
[54]89 if ( dir == ROAR_DIR_PLAY ) {
[1470]90  ROAR_SHUTDOWN(con.fh, SHUT_RD);
[54]91 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
[1470]92  ROAR_SHUTDOWN(con.fh, SHUT_WR);
[54]93 }
94
[0]95 return con.fh;
96}
97
[1163]98int roar_simple_new_stream_attachexeced_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
99 int fh;
100
[1476]101 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */,
[1163]102                                   dir, "libroar temp stream")) == -1 )
103  return -1;
104
105 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
[1476]106#ifdef ROAR_HAVE_IO_POSIX
[1163]107  close(fh);
[1476]108#endif /* no else as we return -1 anyway */
[1163]109  return -1;
110 }
111
[1166]112 s->fh = fh;
113
[1163]114 return fh;
115}
116
[82]117int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) {
118 struct roar_stream     s;
[119]119 return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir);
120}
121
122int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
[1168]123 char file[80] = {0};
124 int fh = -1, listen = -1;
[82]125 static int count = 0;
[487]126 int    type = ROAR_SOCKET_TYPE_UNIX;
127 int    port = 0;
[1396]128#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
[493]129 int    opt  = 1;
[1396]130#endif
[1466]131#ifdef ROAR_HAVE_IPV4
[487]132 struct sockaddr_in   socket_addr;
133 socklen_t            len            = sizeof(struct sockaddr_in);
[1466]134#else
135 struct sockaddr      socket_addr;
136 socklen_t            len            = sizeof(struct sockaddr);
137#endif
[1465]138#ifdef ROAR_HAVE_SELECT
[1161]139 fd_set fds;
140 struct timeval timeout = {10, 0};
[1465]141 struct roar_message    mes;
142#endif
[1396]143#ifdef ROAR_HAVE_UNIX
[1168]144 int socks[2]; // for socketpair()
[1396]145#endif
[82]146
[1476]147#ifdef ROAR_HAVE_BSDSOCKETS
[487]148 if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
[82]149  return -1;
150 }
[1476]151#else
152 return -1;
153#endif
[82]154
[1178]155 if ( len == 0 ) {
156#ifdef ROAR_OS_OPENBSD
157  ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD");
[1466]158  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
[1178]159#else
[505]160  return -1;
[1178]161#endif
162 }
163
[1466]164 switch (((struct sockaddr*)&socket_addr)->sa_family) {
[1359]165#ifdef ROAR_HAVE_UNIX
[1178]166  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
[1359]167#endif
168#ifdef ROAR_HAVE_IPV4
[1178]169  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
[1359]170#endif
171#ifdef ROAR_HAVE_LIBDNET
[1178]172  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
[1359]173#endif
[1178]174  default:
175    return -1;
176   break;
[487]177 }
178
[1187]179 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[526]180  if ( roar_socket_get_local_nodename() ) {
[1067]181   snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
[526]182  } else {
183   return -1;
184  }
[1380]185#ifdef ROAR_HAVE_IPV4
[487]186 } else {
[1068]187  strncpy(file, inet_ntoa(socket_addr.sin_addr), 79);
[1380]188#endif
[487]189 }
190
[1168]191 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
192  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
193   return -1;
194  }
[487]195 }
[451]196
[487]197 if ( type == ROAR_SOCKET_TYPE_INET ) {
[1380]198#ifdef ROAR_HAVE_IPV4
[487]199  len = sizeof(struct sockaddr_in);
[493]200  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
201
[487]202  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
203   return -1;
204  }
205  port = ROAR_NET2HOST16(socket_addr.sin_port);
206  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
[1380]207#else
208  return -1;
209#endif
[532]210 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
[1395]211#ifdef ROAR_HAVE_LIBDNET
[532]212  len = sizeof(struct sockaddr_in);
213  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
[1395]214#else
215  return -1;
216#endif
[487]217 }
[451]218
[119]219 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[82]220  return -1;
221 }
222
[119]223 if ( roar_stream_connect(con, s, dir) == -1 ) {
[82]224  return -1;
225 }
226
[1168]227 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
[1465]228#ifdef ROAR_HAVE_SELECT
[1168]229  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
[1161]230
[1168]231   FD_ZERO(&fds);
232   FD_SET(listen, &fds);
[1163]233
[1168]234   if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) {
235    close(listen);
[1163]236
[1168]237    // we don't need to check the content as we know it failed...
238    if ( roar_recv_message(con, &mes, NULL) == -1 )
239     return -1;
[1161]240
[1168]241    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
242     return -1;
[1161]243
[1168]244    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
[82]245   }
[1168]246
247   if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
248    /* errr, do we need we any error handling here? */
249   }
[82]250   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
251    close(fh);
252    fh = -1;
253   } else if ( mes.cmd != ROAR_CMD_OK ) {
254    close(fh);
255    fh = -1;
256   }
[1168]257  }
258
259  close(listen);
[1465]260#else
261  return -1;
262#endif
[1168]263 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
[1395]264#ifdef ROAR_HAVE_UNIX
[1168]265  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
266   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
267                                          // as we return -1 in both whys
268   return -1;
269  }
270
271  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
272   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
273                                          // as we return -1 in both whys
274   return -1;
275  }
276
277  close(socks[0]);
278  fh = socks[1];
[1395]279#else
280  roar_kick(con, ROAR_OT_STREAM, s->id);
281  return -1;
282#endif
[82]283 }
284
[1168]285/*
[487]286 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
287  unlink(file);
288 }
[1168]289*/
290
291 if ( dir == ROAR_DIR_PLAY ) {
[1470]292  ROAR_SHUTDOWN(fh, SHUT_RD);
[1168]293 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
[1470]294  ROAR_SHUTDOWN(fh, SHUT_WR);
[1168]295 }
[82]296
[505]297 s->fh = fh;
298
[82]299 return fh;
300}
301
[235]302
303int roar_simple_play_file(char * file, char * server, char * name) {
304 struct roar_connection con;
305
306 if ( roar_simple_connect(&con, server, name) == -1 ) {
307  return -1;
308 }
309
310 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
311}
312
[0]313int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
314 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
315}
316
317int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
318 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
319}
320
321int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
322 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
323}
324
325int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
326 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
327}
328
329
330int roar_simple_close(int fh) {
[1476]331#ifdef ROAR_HAVE_IO_POSIX
[0]332 return close(fh);
[1476]333#else
334 return -1;
335#endif
[0]336}
337
338int roar_simple_get_standby (int fh) {
339 struct roar_connection con;
340
341 con.fh = fh;
342
343 return roar_get_standby(&con);
344}
345
346//ll
Note: See TracBrowser for help on using the repository browser.