[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) { |
---|
[67] | 46 | if ( strcmp(driver, "raw") == 0 ) { |
---|
[65] | 47 | return sources_add_raw(driver, device, container, options, primary); |
---|
[67] | 48 | } else if ( strcmp(driver, "wav") == 0 ) { |
---|
| 49 | return sources_add_wav(driver, device, container, options, primary); |
---|
[542] | 50 | } else if ( strcmp(driver, "cf") == 0 ) { |
---|
| 51 | return sources_add_cf(driver, device, container, options, primary); |
---|
[1022] | 52 | } else if ( strcmp(driver, "roar") == 0 ) { |
---|
| 53 | return sources_add_roar(driver, device, container, options, primary); |
---|
[67] | 54 | } |
---|
[65] | 55 | |
---|
[0] | 56 | return -1; |
---|
| 57 | } |
---|
| 58 | |
---|
[65] | 59 | int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 60 | int stream; |
---|
| 61 | int fh; |
---|
| 62 | struct roar_stream * s; |
---|
| 63 | |
---|
[566] | 64 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
| 65 | |
---|
[65] | 66 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 67 | return -1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | if ( (stream = streams_new()) == -1 ) { |
---|
| 71 | close(fh); |
---|
| 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 76 | |
---|
| 77 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 78 | |
---|
| 79 | s->dir = ROAR_DIR_PLAY; |
---|
| 80 | s->pos_rel_id = -1; |
---|
| 81 | |
---|
| 82 | streams_set_fh(stream, fh); |
---|
| 83 | |
---|
[1031] | 84 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[65] | 85 | client_stream_add(g_source_client, stream); |
---|
| 86 | |
---|
| 87 | return 0; |
---|
| 88 | } |
---|
| 89 | |
---|
[67] | 90 | int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 91 | int stream; |
---|
| 92 | int fh; |
---|
| 93 | char buf[44]; |
---|
| 94 | struct roar_stream * s; |
---|
| 95 | |
---|
[566] | 96 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
| 97 | |
---|
[67] | 98 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (read(fh, buf, 44) != 44) { |
---|
| 103 | close(fh); |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if ( (stream = streams_new()) == -1 ) { |
---|
| 108 | close(fh); |
---|
| 109 | return -1; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 113 | |
---|
| 114 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 115 | |
---|
| 116 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
| 117 | memcpy(&(s->info.channels), buf+22, 2); |
---|
| 118 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
| 119 | |
---|
| 120 | s->dir = ROAR_DIR_PLAY; |
---|
| 121 | s->pos_rel_id = -1; |
---|
| 122 | |
---|
| 123 | streams_set_fh(stream, fh); |
---|
| 124 | |
---|
[1031] | 125 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[67] | 126 | client_stream_add(g_source_client, stream); |
---|
| 127 | |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
[542] | 130 | |
---|
| 131 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
[555] | 132 | int stream; |
---|
| 133 | int fh; |
---|
| 134 | int codec; |
---|
| 135 | int len; |
---|
| 136 | char buf[64]; |
---|
[542] | 137 | struct roar_stream * s; |
---|
| 138 | |
---|
| 139 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
[556] | 143 | // TODO: finy out a better way of doing auto detetion without need for seek! |
---|
[555] | 144 | if ( !options ) { |
---|
| 145 | if ( (len = read(fh, buf, 64)) < 1 ) { |
---|
| 146 | close(fh); |
---|
| 147 | return -1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | if ( lseek(fh, -len, SEEK_CUR) == (off_t)-1 ) { |
---|
| 151 | close(fh); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) { |
---|
| 156 | close(fh); |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | } else { |
---|
| 160 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 161 | close(fh); |
---|
| 162 | return -1; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
[542] | 166 | if ( (stream = streams_new()) == -1 ) { |
---|
| 167 | close(fh); |
---|
| 168 | return -1; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 172 | |
---|
| 173 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 174 | |
---|
| 175 | s->dir = ROAR_DIR_PLAY; |
---|
| 176 | s->pos_rel_id = -1; |
---|
| 177 | s->info.codec = codec; |
---|
| 178 | |
---|
[566] | 179 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 180 | |
---|
[542] | 181 | streams_set_fh(stream, fh); |
---|
[644] | 182 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
| 183 | |
---|
| 184 | if ( primary ) |
---|
| 185 | streams_mark_primary(stream); |
---|
[542] | 186 | |
---|
[1031] | 187 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[542] | 188 | client_stream_add(g_source_client, stream); |
---|
| 189 | |
---|
| 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[1022] | 193 | int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 194 | int stream; |
---|
| 195 | int fh; |
---|
| 196 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 197 | struct roar_stream * s; |
---|
| 198 | |
---|
| 199 | if ( options != NULL && *options ) { |
---|
| 200 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 201 | return -1; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) { |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if ( (stream = streams_new()) == -1 ) { |
---|
| 210 | close(fh); |
---|
| 211 | return -1; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 215 | |
---|
| 216 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 217 | |
---|
| 218 | s->dir = ROAR_DIR_PLAY; |
---|
| 219 | s->pos_rel_id = -1; |
---|
| 220 | s->info.codec = codec; |
---|
| 221 | |
---|
| 222 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 223 | |
---|
| 224 | streams_set_fh(stream, fh); |
---|
| 225 | |
---|
| 226 | if ( primary ) |
---|
| 227 | streams_mark_primary(stream); |
---|
| 228 | |
---|
[1031] | 229 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[1022] | 230 | client_stream_add(g_source_client, stream); |
---|
| 231 | |
---|
| 232 | return 0; |
---|
| 233 | } |
---|
| 234 | |
---|
[0] | 235 | //ll |
---|