source: roaraudio/libroar/simple.c @ 2647:812b8d564396

Last change on this file since 2647:812b8d564396 was 2647:812b8d564396, checked in by phi, 15 years ago

added dummy for roar_simple_connect_virtual()

File size: 9.9 KB
Line 
1//simple.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
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
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) {
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) {
66 struct roar_connection con;
67 int ret;
68
69 if ( roar_simple_connect(&con, server, name) == -1 ) {
70  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
71  ROAR_ERR("roar_simple_stream(*): Can not connect to server");
72  return -1;
73 }
74
75 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
76  roar_disconnect(&con);
77  return -1;
78 }
79
80 if ( roar_stream_connect(&con, s, dir) == -1 ) {
81  roar_disconnect(&con);
82  return -1;
83 }
84
85 if ( roar_stream_exec(&con, s) == -1 ) {
86  roar_disconnect(&con);
87  return -1;
88 }
89
90 if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
91  roar_disconnect(&con);
92  return -1;
93 }
94
95 if ( dir == ROAR_DIR_PLAY ) {
96  ROAR_SHUTDOWN(ret, SHUT_RD);
97 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
98  ROAR_SHUTDOWN(ret, SHUT_WR);
99 }
100
101 return ret;
102}
103
104int roar_simple_new_stream_attachexeced_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
105 int fh;
106
107 if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */,
108                                   dir, "libroar temp stream")) == -1 )
109  return -1;
110
111 if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) {
112#ifdef ROAR_HAVE_IO_POSIX
113  close(fh);
114#endif /* no else as we return -1 anyway */
115  return -1;
116 }
117
118 s->fh = fh;
119
120 return fh;
121}
122
123int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) {
124 struct roar_stream     s;
125 return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir);
126}
127
128int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
129 struct roar_libroar_config * config = roar_libroar_get_config();
130 char file[80] = {0};
131 int fh = -1, listen = -1;
132 static int count = 0;
133 int    type = ROAR_SOCKET_TYPE_UNIX;
134 int    port = 0;
135#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
136 int    opt  = 1;
137#endif
138#ifdef ROAR_HAVE_IPV4
139 struct sockaddr_in   socket_addr;
140 socklen_t            len            = sizeof(struct sockaddr_in);
141#else
142 struct sockaddr      socket_addr;
143 socklen_t            len            = sizeof(struct sockaddr);
144#endif
145#ifdef ROAR_HAVE_SELECT
146 fd_set fds;
147 struct timeval timeout = {10, 0};
148 struct roar_message    mes;
149#endif
150#ifdef ROAR_HAVE_UNIX
151 int socks[2]; // for socketpair()
152#endif
153
154 if ( config != NULL ) {
155  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
156   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
157  }
158 }
159
160#ifdef ROAR_HAVE_BSDSOCKETS
161 if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
162  return -1;
163 }
164#else
165 return -1;
166#endif
167
168 if ( len == 0 ) {
169#ifdef ROAR_OS_OPENBSD
170  ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD");
171  ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX;
172#else
173  return -1;
174#endif
175 }
176
177 switch (((struct sockaddr*)&socket_addr)->sa_family) {
178#ifdef ROAR_HAVE_UNIX
179  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
180#endif
181#ifdef ROAR_HAVE_IPV4
182  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
183#endif
184#ifdef ROAR_HAVE_LIBDNET
185  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
186#endif
187  default:
188    return -1;
189   break;
190 }
191
192 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
193  if ( roar_socket_get_local_nodename() ) {
194   snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
195  } else {
196   return -1;
197  }
198#ifdef ROAR_HAVE_IPV4
199 } else {
200  strncpy(file, inet_ntoa(socket_addr.sin_addr), 79);
201#endif
202 }
203
204 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
205  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
206   return -1;
207  }
208 }
209
210 if ( type == ROAR_SOCKET_TYPE_INET ) {
211#ifdef ROAR_HAVE_IPV4
212  len = sizeof(struct sockaddr_in);
213  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
214
215  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
216   return -1;
217  }
218  port = ROAR_NET2HOST16(socket_addr.sin_port);
219  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
220#else
221  return -1;
222#endif
223 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
224#ifdef ROAR_HAVE_LIBDNET
225  len = sizeof(struct sockaddr_in);
226  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
227#else
228  return -1;
229#endif
230 }
231
232 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
233  return -1;
234 }
235
236 if ( roar_stream_connect(con, s, dir) == -1 ) {
237  return -1;
238 }
239
240 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
241#ifdef ROAR_HAVE_SELECT
242  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
243
244   FD_ZERO(&fds);
245   FD_SET(listen, &fds);
246
247   if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) {
248    close(listen);
249
250    // we don't need to check the content as we know it failed...
251    if ( roar_recv_message(con, &mes, NULL) == -1 )
252     return -1;
253
254    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
255     return -1;
256
257    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
258   }
259
260   if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
261    /* errr, do we need we any error handling here? */
262   }
263   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
264    close(fh);
265    fh = -1;
266   } else if ( mes.cmd != ROAR_CMD_OK ) {
267    close(fh);
268    fh = -1;
269   }
270  }
271
272  close(listen);
273#else
274  return -1;
275#endif
276 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
277#ifdef ROAR_HAVE_UNIX
278  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
279   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
280                                          // as we return -1 in both whys
281   return -1;
282  }
283
284  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
285   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
286                                          // as we return -1 in both whys
287   return -1;
288  }
289
290  close(socks[0]);
291  fh = socks[1];
292#else
293  roar_kick(con, ROAR_OT_STREAM, s->id);
294  return -1;
295#endif
296 }
297
298/*
299 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
300  unlink(file);
301 }
302*/
303
304 if ( dir == ROAR_DIR_PLAY ) {
305  ROAR_SHUTDOWN(fh, SHUT_RD);
306 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
307  ROAR_SHUTDOWN(fh, SHUT_WR);
308 }
309
310 s->fh = fh;
311
312 return fh;
313}
314
315
316int roar_simple_play_file(char * file, char * server, char * name) {
317 struct roar_connection con;
318
319 if ( roar_simple_connect(&con, server, name) == -1 ) {
320  return -1;
321 }
322
323 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
324}
325
326int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
327 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
328}
329
330int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
331 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
332}
333
334int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
335 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
336}
337
338int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
339 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
340}
341
342int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent) {
343 return -1;
344}
345
346int roar_simple_close(int fh) {
347#ifdef ROAR_TARGET_WIN32
348 closesocket(fh);
349 return 0;
350#else
351
352#ifdef ROAR_HAVE_IO_POSIX
353 return close(fh);
354#else
355 return -1;
356#endif
357
358#endif
359}
360
361int roar_simple_get_standby (int fh) {
362 struct roar_connection con;
363
364 if ( roar_connect_fh(&con, fh) == -1 )
365  return -1;
366
367 return roar_get_standby(&con);
368}
369
370//ll
Note: See TracBrowser for help on using the repository browser.