[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 | |
---|
[82] | 98 | int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) { |
---|
| 99 | struct roar_stream s; |
---|
[119] | 100 | return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | 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] | 104 | struct roar_message mes; |
---|
| 105 | char file[80]; |
---|
| 106 | int fh = -1, listen; |
---|
| 107 | static int count = 0; |
---|
[451] | 108 | struct group * grp = NULL; |
---|
[487] | 109 | int type = ROAR_SOCKET_TYPE_UNIX; |
---|
| 110 | int port = 0; |
---|
[493] | 111 | int opt = 1; |
---|
[487] | 112 | struct sockaddr_in socket_addr; |
---|
| 113 | socklen_t len = sizeof(struct sockaddr_in); |
---|
[82] | 114 | |
---|
[487] | 115 | if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
[82] | 116 | return -1; |
---|
| 117 | } |
---|
| 118 | |
---|
[487] | 119 | if ( socket_addr.sin_family == AF_INET ) { |
---|
| 120 | type = ROAR_SOCKET_TYPE_INET; |
---|
[505] | 121 | } else if ( socket_addr.sin_family == AF_UNIX ) { |
---|
| 122 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
[526] | 123 | } else if ( socket_addr.sin_family == AF_DECnet ) { |
---|
| 124 | type = ROAR_SOCKET_TYPE_DECNET; |
---|
[505] | 125 | } else { |
---|
| 126 | return -1; |
---|
[487] | 127 | } |
---|
| 128 | |
---|
| 129 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[1067] | 130 | snprintf(file, 79, "/tmp/.libroar-simple-stream.%i-%i", getpid(), count++); |
---|
[526] | 131 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
| 132 | if ( roar_socket_get_local_nodename() ) { |
---|
[1067] | 133 | snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
[526] | 134 | } else { |
---|
| 135 | return -1; |
---|
| 136 | } |
---|
[487] | 137 | } else { |
---|
[1068] | 138 | strncpy(file, inet_ntoa(socket_addr.sin_addr), 79); |
---|
[487] | 139 | } |
---|
| 140 | |
---|
| 141 | if ( (listen = roar_socket_listen(type, file, port)) == -1 ) { |
---|
| 142 | return -1; |
---|
| 143 | } |
---|
[451] | 144 | |
---|
[487] | 145 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
| 146 | len = sizeof(struct sockaddr_in); |
---|
[493] | 147 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
| 148 | |
---|
[487] | 149 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
| 150 | return -1; |
---|
| 151 | } |
---|
| 152 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
| 153 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
[532] | 154 | } else if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[1080] | 155 | #ifndef ROAR_TARGET_WIN32 |
---|
[487] | 156 | chmod(file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 157 | |
---|
| 158 | grp = getgrnam(ROAR_DEFAULT_SOCKGRP); |
---|
| 159 | |
---|
| 160 | if ( grp ) |
---|
| 161 | chown(file, -1, grp->gr_gid); |
---|
[1080] | 162 | #else |
---|
| 163 | ROAR_ERR("roar_simple_new_stream_obj(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
| 164 | #endif |
---|
[532] | 165 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
| 166 | len = sizeof(struct sockaddr_in); |
---|
| 167 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
[487] | 168 | } |
---|
[451] | 169 | |
---|
[119] | 170 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
[82] | 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
[119] | 174 | if ( roar_stream_connect(con, s, dir) == -1 ) { |
---|
[82] | 175 | return -1; |
---|
| 176 | } |
---|
| 177 | |
---|
[487] | 178 | if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) { |
---|
[82] | 179 | |
---|
| 180 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
| 181 | if ( dir == ROAR_DIR_PLAY ) { |
---|
| 182 | shutdown(fh, SHUT_RD); |
---|
| 183 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
| 184 | shutdown(fh, SHUT_WR); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
| 188 | close(fh); |
---|
| 189 | fh = -1; |
---|
| 190 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
| 191 | close(fh); |
---|
| 192 | fh = -1; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | close(listen); |
---|
[487] | 197 | |
---|
| 198 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
| 199 | unlink(file); |
---|
| 200 | } |
---|
[82] | 201 | |
---|
[505] | 202 | s->fh = fh; |
---|
| 203 | |
---|
[82] | 204 | return fh; |
---|
| 205 | } |
---|
| 206 | |
---|
[235] | 207 | |
---|
| 208 | int roar_simple_play_file(char * file, char * server, char * name) { |
---|
| 209 | struct roar_connection con; |
---|
| 210 | |
---|
| 211 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
| 212 | return -1; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one. |
---|
| 216 | } |
---|
| 217 | |
---|
[0] | 218 | int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 219 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 223 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 227 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
| 231 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | int roar_simple_close(int fh) { |
---|
| 236 | return close(fh); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | int roar_simple_get_standby (int fh) { |
---|
| 240 | struct roar_connection con; |
---|
| 241 | |
---|
| 242 | con.fh = fh; |
---|
| 243 | |
---|
| 244 | return roar_get_standby(&con); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | //ll |
---|