source: roaraudio/libroar/simple.c @ 1470:9ada8abbf75d

Last change on this file since 1470:9ada8abbf75d was 1470:9ada8abbf75d, checked in by phi, 15 years ago

replaced shutdown() by ROAR_SHUTDOWN() to make it optional

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