[0] | 1 | //sources.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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 | */ |
---|
| 24 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
| 27 | int sources_init (void) { |
---|
| 28 | g_source_client = -1; |
---|
| 29 | return 0; |
---|
| 30 | } |
---|
| 31 | |
---|
[64] | 32 | int sources_set_client (int client) { |
---|
| 33 | if ( client >= 0 ) { |
---|
| 34 | g_source_client = client; |
---|
| 35 | return 0; |
---|
| 36 | } else { |
---|
| 37 | return -1; |
---|
| 38 | } |
---|
| 39 | } |
---|
[0] | 40 | |
---|
| 41 | int sources_free (void) { |
---|
| 42 | return 0; |
---|
| 43 | } |
---|
| 44 | |
---|
[65] | 45 | int sources_add (char * driver, char * device, char * container, char * options, int primary) { |
---|
[1499] | 46 | if (0) { |
---|
| 47 | #ifdef ROAR_HAVE_IO_POSIX |
---|
| 48 | } else if ( strcmp(driver, "raw") == 0 ) { |
---|
[65] | 49 | return sources_add_raw(driver, device, container, options, primary); |
---|
[67] | 50 | } else if ( strcmp(driver, "wav") == 0 ) { |
---|
| 51 | return sources_add_wav(driver, device, container, options, primary); |
---|
[1499] | 52 | #endif |
---|
[542] | 53 | } else if ( strcmp(driver, "cf") == 0 ) { |
---|
| 54 | return sources_add_cf(driver, device, container, options, primary); |
---|
[1022] | 55 | } else if ( strcmp(driver, "roar") == 0 ) { |
---|
| 56 | return sources_add_roar(driver, device, container, options, primary); |
---|
[67] | 57 | } |
---|
[65] | 58 | |
---|
[0] | 59 | return -1; |
---|
| 60 | } |
---|
| 61 | |
---|
[1499] | 62 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[65] | 63 | int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 64 | int stream; |
---|
| 65 | int fh; |
---|
| 66 | struct roar_stream * s; |
---|
| 67 | |
---|
[566] | 68 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
| 69 | |
---|
[65] | 70 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if ( (stream = streams_new()) == -1 ) { |
---|
| 75 | close(fh); |
---|
| 76 | return -1; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 80 | |
---|
| 81 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 82 | |
---|
[1609] | 83 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 84 | streams_delete(stream); |
---|
| 85 | close(fh); |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | |
---|
[65] | 89 | s->pos_rel_id = -1; |
---|
| 90 | |
---|
| 91 | streams_set_fh(stream, fh); |
---|
| 92 | |
---|
[1031] | 93 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[65] | 94 | client_stream_add(g_source_client, stream); |
---|
| 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
[1499] | 98 | #endif |
---|
[65] | 99 | |
---|
[1499] | 100 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[67] | 101 | int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 102 | int stream; |
---|
| 103 | int fh; |
---|
| 104 | char buf[44]; |
---|
| 105 | struct roar_stream * s; |
---|
| 106 | |
---|
[566] | 107 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
| 108 | |
---|
[67] | 109 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | if (read(fh, buf, 44) != 44) { |
---|
| 114 | close(fh); |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if ( (stream = streams_new()) == -1 ) { |
---|
| 119 | close(fh); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 124 | |
---|
| 125 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 126 | |
---|
[1499] | 127 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
| 128 | memcpy(&(s->info.channels), buf+22, 2); |
---|
| 129 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
[67] | 130 | |
---|
[1609] | 131 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 132 | streams_delete(stream); |
---|
| 133 | close(fh); |
---|
| 134 | return -1; |
---|
| 135 | } |
---|
[67] | 136 | s->pos_rel_id = -1; |
---|
| 137 | |
---|
| 138 | streams_set_fh(stream, fh); |
---|
| 139 | |
---|
[1031] | 140 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[67] | 141 | client_stream_add(g_source_client, stream); |
---|
| 142 | |
---|
| 143 | return 0; |
---|
| 144 | } |
---|
[1499] | 145 | #endif |
---|
| 146 | |
---|
[1614] | 147 | #define _ret(x) streams_delete(stream); return (x) |
---|
[542] | 148 | |
---|
| 149 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
[555] | 150 | int stream; |
---|
| 151 | int codec; |
---|
| 152 | int len; |
---|
| 153 | char buf[64]; |
---|
[1499] | 154 | struct roar_stream * s; |
---|
| 155 | struct roar_vio_calls * vio; |
---|
[1614] | 156 | struct roar_vio_defaults def; |
---|
| 157 | |
---|
| 158 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 ) |
---|
| 159 | return -1; |
---|
[555] | 160 | |
---|
[542] | 161 | if ( (stream = streams_new()) == -1 ) { |
---|
| 162 | return -1; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 166 | |
---|
| 167 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 168 | |
---|
[1609] | 169 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 170 | streams_delete(stream); |
---|
| 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
[542] | 174 | s->pos_rel_id = -1; |
---|
[1499] | 175 | |
---|
| 176 | /* |
---|
| 177 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 178 | return -1; |
---|
| 179 | } |
---|
| 180 | */ |
---|
| 181 | |
---|
| 182 | vio = &(ROAR_STREAM_SERVER(s)->vio); |
---|
| 183 | |
---|
[1614] | 184 | //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) { |
---|
| 185 | if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) { |
---|
[1499] | 186 | _ret(-1); |
---|
| 187 | } |
---|
| 188 | |
---|
[1614] | 189 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
| 190 | |
---|
[1499] | 191 | // TODO: finy out a better way of doing auto detetion without need for seek! |
---|
| 192 | if ( options == NULL ) { |
---|
| 193 | if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) { |
---|
| 194 | _ret(-1); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) { |
---|
| 198 | _ret(-1); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) { |
---|
| 202 | _ret(-1); |
---|
| 203 | } |
---|
| 204 | } else { |
---|
| 205 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 206 | options += 6; |
---|
| 207 | |
---|
| 208 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 209 | _ret(-1); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
[542] | 213 | s->info.codec = codec; |
---|
| 214 | |
---|
[566] | 215 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 216 | |
---|
[1614] | 217 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[1504] | 218 | streams_set_fh(stream, -2); |
---|
[1614] | 219 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[644] | 220 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
| 221 | |
---|
| 222 | if ( primary ) |
---|
| 223 | streams_mark_primary(stream); |
---|
[542] | 224 | |
---|
[1031] | 225 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[542] | 226 | client_stream_add(g_source_client, stream); |
---|
| 227 | |
---|
| 228 | return 0; |
---|
| 229 | } |
---|
| 230 | |
---|
[1499] | 231 | #undef _ret |
---|
| 232 | |
---|
| 233 | |
---|
[1022] | 234 | int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 235 | int stream; |
---|
| 236 | int fh; |
---|
| 237 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 238 | struct roar_stream * s; |
---|
| 239 | |
---|
| 240 | if ( options != NULL && *options ) { |
---|
[1186] | 241 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 242 | options += 6; |
---|
| 243 | |
---|
[1022] | 244 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 245 | return -1; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) { |
---|
| 250 | return -1; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | if ( (stream = streams_new()) == -1 ) { |
---|
| 254 | close(fh); |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 259 | |
---|
| 260 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 261 | |
---|
[1609] | 262 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 263 | streams_delete(stream); |
---|
| 264 | close(fh); |
---|
| 265 | return -1; |
---|
| 266 | } |
---|
| 267 | |
---|
[1022] | 268 | s->pos_rel_id = -1; |
---|
| 269 | s->info.codec = codec; |
---|
| 270 | |
---|
| 271 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 272 | |
---|
| 273 | streams_set_fh(stream, fh); |
---|
| 274 | |
---|
| 275 | if ( primary ) |
---|
| 276 | streams_mark_primary(stream); |
---|
| 277 | |
---|
[1031] | 278 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[1022] | 279 | client_stream_add(g_source_client, stream); |
---|
| 280 | |
---|
| 281 | return 0; |
---|
| 282 | } |
---|
| 283 | |
---|
[0] | 284 | //ll |
---|