source: roaraudio/libroar/simple.c @ 1359:6a59a3cbc91a

Last change on this file since 1359:6a59a3cbc91a was 1359:6a59a3cbc91a, checked in by phi, 15 years ago

make IPv4 optional in most libroar files

File size: 8.6 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  shutdown(con.fh, SHUT_RD);
91 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
92  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 struct roar_message    mes;
122 char file[80] = {0};
123 int fh = -1, listen = -1;
124 static int count = 0;
125 int    type = ROAR_SOCKET_TYPE_UNIX;
126 int    port = 0;
127 int    opt  = 1;
128 struct sockaddr_in   socket_addr;
129 socklen_t            len            = sizeof(struct sockaddr_in);
130 fd_set fds;
131 struct timeval timeout = {10, 0};
132 int socks[2]; // for socketpair()
133
134 if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
135  return -1;
136 }
137
138 if ( len == 0 ) {
139#ifdef ROAR_OS_OPENBSD
140  ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD");
141  socket_addr.sin_family = AF_UNIX;
142#else
143  return -1;
144#endif
145 }
146
147 switch (socket_addr.sin_family) {
148#ifdef ROAR_HAVE_UNIX
149  case AF_UNIX:   type = ROAR_SOCKET_TYPE_UNIX; break;
150#endif
151#ifdef ROAR_HAVE_IPV4
152  case AF_INET:   type = ROAR_SOCKET_TYPE_INET; break;
153#endif
154#ifdef ROAR_HAVE_LIBDNET
155  case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break;
156#endif
157  default:
158    return -1;
159   break;
160 }
161
162 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
163  if ( roar_socket_get_local_nodename() ) {
164   snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
165  } else {
166   return -1;
167  }
168 } else {
169  strncpy(file, inet_ntoa(socket_addr.sin_addr), 79);
170 }
171
172 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
173  if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
174   return -1;
175  }
176 }
177
178 if ( type == ROAR_SOCKET_TYPE_INET ) {
179  len = sizeof(struct sockaddr_in);
180  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
181
182  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
183   return -1;
184  }
185  port = ROAR_NET2HOST16(socket_addr.sin_port);
186  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
187 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
188  len = sizeof(struct sockaddr_in);
189  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
190 }
191
192 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
193  return -1;
194 }
195
196 if ( roar_stream_connect(con, s, dir) == -1 ) {
197  return -1;
198 }
199
200 if ( type != ROAR_SOCKET_TYPE_UNIX ) {
201  if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
202
203   FD_ZERO(&fds);
204   FD_SET(listen, &fds);
205
206   if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) {
207    close(listen);
208
209    // we don't need to check the content as we know it failed...
210    if ( roar_recv_message(con, &mes, NULL) == -1 )
211     return -1;
212
213    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
214     return -1;
215
216    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir);
217   }
218
219   if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
220    /* errr, do we need we any error handling here? */
221   }
222   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
223    close(fh);
224    fh = -1;
225   } else if ( mes.cmd != ROAR_CMD_OK ) {
226    close(fh);
227    fh = -1;
228   }
229  }
230
231  close(listen);
232 } else { // this is type == ROAR_SOCKET_TYPE_UNIX
233  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
234   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
235                                          // as we return -1 in both whys
236   return -1;
237  }
238
239  if ( roar_stream_passfh(con, s, socks[0]) == -1 ) {
240   roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors
241                                          // as we return -1 in both whys
242   return -1;
243  }
244
245  close(socks[0]);
246  fh = socks[1];
247 }
248
249/*
250 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
251  unlink(file);
252 }
253*/
254
255 if ( dir == ROAR_DIR_PLAY ) {
256  shutdown(fh, SHUT_RD);
257 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
258  shutdown(fh, SHUT_WR);
259 }
260
261 s->fh = fh;
262
263 return fh;
264}
265
266
267int roar_simple_play_file(char * file, char * server, char * name) {
268 struct roar_connection con;
269
270 if ( roar_simple_connect(&con, server, name) == -1 ) {
271  return -1;
272 }
273
274 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
275}
276
277int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
278 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
279}
280
281int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
282 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
283}
284
285int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
286 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
287}
288
289int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
290 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
291}
292
293
294int roar_simple_close(int fh) {
295 return close(fh);
296}
297
298int roar_simple_get_standby (int fh) {
299 struct roar_connection con;
300
301 con.fh = fh;
302
303 return roar_get_standby(&con);
304}
305
306//ll
Note: See TracBrowser for help on using the repository browser.