source: roaraudio/libroar/simple.c @ 1080:79a7f3ec66a7

Last change on this file since 1080:79a7f3ec66a7 was 1080:79a7f3ec66a7, checked in by phi, 15 years ago

no stat() and struct stat in win32...

File size: 6.8 KB
Line 
1//simple.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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_connection con;
61 struct roar_stream     s;
62
63 if ( roar_simple_connect(&con, server, name) == -1 ) {
64  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
65  return -1;
66 }
67
68 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
69  roar_disconnect(&con);
70  return -1;
71 }
72
73 if ( roar_stream_connect(&con, &s, dir) == -1 ) {
74  roar_disconnect(&con);
75  return -1;
76 }
77
78 if ( roar_stream_exec(&con, &s) == -1 ) {
79  roar_disconnect(&con);
80  return -1;
81 }
82
83 if ( dir == ROAR_DIR_PLAY ) {
84  shutdown(con.fh, SHUT_RD);
85 } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
86  shutdown(con.fh, SHUT_WR);
87 }
88
89 return con.fh;
90}
91
92int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) {
93 struct roar_stream     s;
94 return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir);
95}
96
97int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) {
98 struct roar_message    mes;
99 char file[80];
100 int fh = -1, listen;
101 static int count = 0;
102 struct group   * grp  = NULL;
103 int    type = ROAR_SOCKET_TYPE_UNIX;
104 int    port = 0;
105 int    opt  = 1;
106 struct sockaddr_in   socket_addr;
107 socklen_t            len            = sizeof(struct sockaddr_in);
108
109 if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
110  return -1;
111 }
112
113 if ( socket_addr.sin_family == AF_INET ) {
114  type = ROAR_SOCKET_TYPE_INET;
115 } else if ( socket_addr.sin_family == AF_UNIX ) {
116  type = ROAR_SOCKET_TYPE_UNIX;
117 } else if ( socket_addr.sin_family == AF_DECnet ) {
118  type = ROAR_SOCKET_TYPE_DECNET;
119 } else {
120  return -1;
121 }
122
123 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
124  snprintf(file, 79, "/tmp/.libroar-simple-stream.%i-%i", getpid(), count++);
125 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
126  if ( roar_socket_get_local_nodename() ) {
127   snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++);
128  } else {
129   return -1;
130  }
131 } else {
132  strncpy(file, inet_ntoa(socket_addr.sin_addr), 79);
133 }
134
135 if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
136  return -1;
137 }
138
139 if ( type == ROAR_SOCKET_TYPE_INET ) {
140  len = sizeof(struct sockaddr_in);
141  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
142
143  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
144   return -1;
145  }
146  port = ROAR_NET2HOST16(socket_addr.sin_port);
147  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
148 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
149#ifndef ROAR_TARGET_WIN32
150  chmod(file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
151
152  grp = getgrnam(ROAR_DEFAULT_SOCKGRP);
153
154  if ( grp )
155   chown(file, -1, grp->gr_gid);
156#else
157  ROAR_ERR("roar_simple_new_stream_obj(*): There is no UNIX Domain Socket support in win32, download a real OS.");
158#endif
159 } else if ( type == ROAR_SOCKET_TYPE_DECNET ) {
160  len = sizeof(struct sockaddr_in);
161  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
162 }
163
164 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
165  return -1;
166 }
167
168 if ( roar_stream_connect(con, s, dir) == -1 ) {
169  return -1;
170 }
171
172 if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
173
174  if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
175   if ( dir == ROAR_DIR_PLAY ) {
176    shutdown(fh, SHUT_RD);
177   } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
178    shutdown(fh, SHUT_WR);
179   }
180  }
181   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
182    close(fh);
183    fh = -1;
184   } else if ( mes.cmd != ROAR_CMD_OK ) {
185    close(fh);
186    fh = -1;
187   }
188 }
189
190 close(listen);
191
192 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
193  unlink(file);
194 }
195
196 s->fh = fh;
197
198 return fh;
199}
200
201
202int roar_simple_play_file(char * file, char * server, char * name) {
203 struct roar_connection con;
204
205 if ( roar_simple_connect(&con, server, name) == -1 ) {
206  return -1;
207 }
208
209 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
210}
211
212int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
213 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
214}
215
216int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
217 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
218}
219
220int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
221 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
222}
223
224int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
225 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
226}
227
228
229int roar_simple_close(int fh) {
230 return close(fh);
231}
232
233int roar_simple_get_standby (int fh) {
234 struct roar_connection con;
235
236 con.fh = fh;
237
238 return roar_get_standby(&con);
239}
240
241//ll
Note: See TracBrowser for help on using the repository browser.