[0] | 1 | //simple.c: |
---|
| 2 | |
---|
[690] | 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 | |
---|
[0] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int 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 | |
---|
| 59 | int 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 | |
---|
| 65 | int 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 ) { |
---|
| 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 | |
---|
[0] | 95 | return con.fh; |
---|
| 96 | } |
---|
| 97 | |
---|
[1163] | 98 | int 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 | |
---|
[1166] | 110 | s->fh = fh; |
---|
| 111 | |
---|
[1163] | 112 | return fh; |
---|
| 113 | } |
---|
| 114 | |
---|
[82] | 115 | int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) { |
---|
| 116 | struct roar_stream s; |
---|
[119] | 117 | return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) { |
---|
[82] | 121 | struct roar_message mes; |
---|
[1168] | 122 | char file[80] = {0}; |
---|
| 123 | int fh = -1, listen = -1; |
---|
[82] | 124 | static int count = 0; |
---|
[451] | 125 | struct group * grp = NULL; |
---|
[487] | 126 | int type = ROAR_SOCKET_TYPE_UNIX; |
---|
| 127 | int port = 0; |
---|
[493] | 128 | int opt = 1; |
---|
[487] | 129 | struct sockaddr_in socket_addr; |
---|
| 130 | socklen_t len = sizeof(struct sockaddr_in); |
---|
[1161] | 131 | fd_set fds; |
---|
| 132 | struct timeval timeout = {10, 0}; |
---|
[1168] | 133 | int socks[2]; // for socketpair() |
---|
[82] | 134 | |
---|
[487] | 135 | if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
[82] | 136 | return -1; |
---|
| 137 | } |
---|
| 138 | |
---|
[487] | 139 | if ( socket_addr.sin_family == AF_INET ) { |
---|
| 140 | type = ROAR_SOCKET_TYPE_INET; |
---|
[505] | 141 | } else if ( socket_addr.sin_family == AF_UNIX ) { |
---|
| 142 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
[526] | 143 | } else if ( socket_addr.sin_family == AF_DECnet ) { |
---|
| 144 | type = ROAR_SOCKET_TYPE_DECNET; |
---|
[505] | 145 | } else { |
---|
| 146 | return -1; |
---|
[487] | 147 | } |
---|
| 148 | |
---|
[1168] | 149 | /* |
---|
[487] | 150 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[1067] | 151 | snprintf(file, 79, "/tmp/.libroar-simple-stream.%i-%i", getpid(), count++); |
---|
[1168] | 152 | } else */ if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
[526] | 153 | if ( roar_socket_get_local_nodename() ) { |
---|
[1067] | 154 | snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
[526] | 155 | } else { |
---|
| 156 | return -1; |
---|
| 157 | } |
---|
[487] | 158 | } else { |
---|
[1068] | 159 | strncpy(file, inet_ntoa(socket_addr.sin_addr), 79); |
---|
[487] | 160 | } |
---|
| 161 | |
---|
[1168] | 162 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
| 163 | if ( (listen = roar_socket_listen(type, file, port)) == -1 ) { |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
[487] | 166 | } |
---|
[451] | 167 | |
---|
[487] | 168 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
| 169 | len = sizeof(struct sockaddr_in); |
---|
[493] | 170 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
| 171 | |
---|
[487] | 172 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
| 173 | return -1; |
---|
| 174 | } |
---|
| 175 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
| 176 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
[1168] | 177 | /* |
---|
[1172] | 178 | #if 0 |
---|
[532] | 179 | } else if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[1080] | 180 | #ifndef ROAR_TARGET_WIN32 |
---|
[487] | 181 | chmod(file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 182 | |
---|
| 183 | grp = getgrnam(ROAR_DEFAULT_SOCKGRP); |
---|
| 184 | |
---|
| 185 | if ( grp ) |
---|
| 186 | chown(file, -1, grp->gr_gid); |
---|
[1080] | 187 | #else |
---|
| 188 | ROAR_ERR("roar_simple_new_stream_obj(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
| 189 | #endif |
---|
[1172] | 190 | #endif |
---|
[1168] | 191 | */ |
---|
[532] | 192 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
| 193 | len = sizeof(struct sockaddr_in); |
---|
| 194 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
[487] | 195 | } |
---|
[451] | 196 | |
---|
[119] | 197 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
[82] | 198 | return -1; |
---|
| 199 | } |
---|
| 200 | |
---|
[119] | 201 | if ( roar_stream_connect(con, s, dir) == -1 ) { |
---|
[82] | 202 | return -1; |
---|
| 203 | } |
---|
| 204 | |
---|
[1168] | 205 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
| 206 | if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) { |
---|
[1161] | 207 | |
---|
[1168] | 208 | FD_ZERO(&fds); |
---|
| 209 | FD_SET(listen, &fds); |
---|
[1163] | 210 | |
---|
[1168] | 211 | if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) { |
---|
| 212 | close(listen); |
---|
[1163] | 213 | |
---|
[1168] | 214 | // we don't need to check the content as we know it failed... |
---|
| 215 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
| 216 | return -1; |
---|
[1161] | 217 | |
---|
[1168] | 218 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
| 219 | return -1; |
---|
[1161] | 220 | |
---|
[1168] | 221 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
[82] | 222 | } |
---|
[1168] | 223 | |
---|
| 224 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
| 225 | /* errr, do we need we any error handling here? */ |
---|
| 226 | } |
---|
[82] | 227 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
| 228 | close(fh); |
---|
| 229 | fh = -1; |
---|
| 230 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
| 231 | close(fh); |
---|
| 232 | fh = -1; |
---|
| 233 | } |
---|
[1168] | 234 | } |
---|
| 235 | |
---|
| 236 | close(listen); |
---|
| 237 | } else { // this is type == ROAR_SOCKET_TYPE_UNIX |
---|
| 238 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 239 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
| 240 | // as we return -1 in both whys |
---|
| 241 | return -1; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | if ( roar_stream_passfh(con, s, socks[0]) == -1 ) { |
---|
| 245 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
| 246 | // as we return -1 in both whys |
---|
| 247 | return -1; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | close(socks[0]); |
---|
| 251 | fh = socks[1]; |
---|
[82] | 252 | } |
---|
| 253 | |
---|
[1168] | 254 | /* |
---|
[487] | 255 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
| 256 | unlink(file); |
---|
| 257 | } |
---|
[1168] | 258 | */ |
---|
| 259 | |
---|
| 260 | if ( dir == ROAR_DIR_PLAY ) { |
---|
| 261 | shutdown(fh, SHUT_RD); |
---|
| 262 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
| 263 | shutdown(fh, SHUT_WR); |
---|
| 264 | } |
---|
[82] | 265 | |
---|
[505] | 266 | s->fh = fh; |
---|
| 267 | |
---|
[82] | 268 | return fh; |
---|
| 269 | } |
---|
| 270 | |
---|
[235] | 271 | |
---|
| 272 | int roar_simple_play_file(char * file, char * server, char * name) { |
---|
| 273 | struct roar_connection con; |
---|
| 274 | |
---|
| 275 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
| 276 | return -1; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one. |
---|
| 280 | } |
---|
| 281 | |
---|
[0] | 282 | int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 283 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 287 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 291 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 295 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | int roar_simple_close(int fh) { |
---|
| 300 | return close(fh); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | int roar_simple_get_standby (int fh) { |
---|
| 304 | struct roar_connection con; |
---|
| 305 | |
---|
| 306 | con.fh = fh; |
---|
| 307 | |
---|
| 308 | return roar_get_standby(&con); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | //ll |
---|