[452] | 1 | //roar.c: |
---|
| 2 | |
---|
[703] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of the XMMS RoarAudio output plugin 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 | |
---|
[452] | 25 | #include <roaraudio.h> |
---|
| 26 | |
---|
[453] | 27 | //#include "xmms/i18n.h" |
---|
| 28 | #include <gtk/gtk.h> |
---|
| 29 | #include <stdio.h> |
---|
| 30 | #include <string.h> |
---|
| 31 | |
---|
| 32 | #include "xmms/plugin.h" |
---|
| 33 | #include "xmms/xmmsctrl.h" |
---|
| 34 | #include "xmms/dirbrowser.h" |
---|
| 35 | #include "xmms/configfile.h" |
---|
| 36 | #include "xmms/util.h" |
---|
| 37 | |
---|
[456] | 38 | #define _(x) (x) |
---|
| 39 | |
---|
| 40 | gint ctrlsocket_get_session_id(void); |
---|
| 41 | |
---|
[453] | 42 | void roar_init(void); |
---|
| 43 | void roar_about(void); |
---|
| 44 | void roar_configure(void); |
---|
| 45 | |
---|
| 46 | void roar_get_volume(int *l, int *r); |
---|
| 47 | void roar_set_volume(int l, int r); |
---|
| 48 | void roar_mixer_init(void); |
---|
| 49 | void roar_mixer_init_vol(int l, int r); |
---|
| 50 | |
---|
| 51 | int roar_playing(void); |
---|
| 52 | int roar_free(void); |
---|
| 53 | void roar_write(void *ptr, int length); |
---|
| 54 | void roar_close(void); |
---|
| 55 | void roar_flush(int time); |
---|
| 56 | void roar_pause(short p); |
---|
| 57 | int roar_open(AFormat fmt, int rate, int nch); |
---|
| 58 | int roar_get_output_time(void); |
---|
| 59 | int roar_get_written_time(void); |
---|
| 60 | |
---|
[456] | 61 | int roar_update_metadata(void); |
---|
| 62 | int roar_chk_metadata(void); |
---|
[453] | 63 | |
---|
[454] | 64 | OutputPlugin roar_op = { |
---|
[453] | 65 | NULL, |
---|
| 66 | NULL, |
---|
| 67 | "RoarAudio XMMS Plugin", /* Description */ |
---|
| 68 | roar_init, |
---|
[456] | 69 | roar_about, |
---|
[454] | 70 | NULL, //roar_configure, |
---|
[459] | 71 | roar_get_volume, |
---|
| 72 | roar_set_volume, |
---|
[453] | 73 | roar_open, |
---|
| 74 | roar_write, |
---|
| 75 | roar_close, |
---|
[460] | 76 | roar_flush, |
---|
[457] | 77 | roar_pause, |
---|
[453] | 78 | roar_free, |
---|
| 79 | roar_playing, |
---|
[455] | 80 | roar_get_output_time, |
---|
| 81 | roar_get_written_time, |
---|
[453] | 82 | }; |
---|
| 83 | |
---|
[1036] | 84 | #define STATE_CONNECTED 1 |
---|
| 85 | #define STATE_PLAYING 2 |
---|
| 86 | #define STATE_NORECONNECT 4 |
---|
[454] | 87 | |
---|
| 88 | struct xmms_roar_out { |
---|
[1106] | 89 | int state; |
---|
| 90 | char * server; |
---|
[454] | 91 | struct roar_connection con; |
---|
| 92 | struct roar_stream stream; |
---|
[1106] | 93 | int data_fh; |
---|
| 94 | long unsigned int written; |
---|
| 95 | long unsigned int bps; |
---|
| 96 | int session; |
---|
| 97 | int pause; |
---|
| 98 | struct { |
---|
| 99 | int server_type; |
---|
| 100 | int port; |
---|
| 101 | int * proxy_type; |
---|
| 102 | char * proxy; |
---|
| 103 | char * player_name; |
---|
| 104 | } cfg; |
---|
[454] | 105 | } g_inst; |
---|
| 106 | |
---|
| 107 | OutputPlugin *get_oplugin_info(void) { |
---|
| 108 | return &roar_op; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void roar_init(void) { |
---|
[1106] | 112 | ConfigFile * cfgfile; |
---|
| 113 | |
---|
| 114 | cfgfile = xmms_cfg_open_default_file(); |
---|
| 115 | |
---|
[454] | 116 | g_inst.state = 0; |
---|
| 117 | g_inst.server = NULL; |
---|
[456] | 118 | g_inst.session = ctrlsocket_get_session_id(); |
---|
[1106] | 119 | |
---|
| 120 | xmms_cfg_read_string(cfgfile, "ROAR", "server", &g_inst.server); |
---|
| 121 | |
---|
| 122 | xmms_cfg_read_string(cfgfile, "ROAR", "player_name", &g_inst.cfg.player_name); |
---|
| 123 | |
---|
| 124 | xmms_cfg_free(cfgfile); |
---|
| 125 | |
---|
| 126 | if ( g_inst.cfg.player_name == NULL ) |
---|
| 127 | g_inst.cfg.player_name = "XMMS"; |
---|
| 128 | |
---|
[456] | 129 | ROAR_DBG("roar_init(*) = (void)"); |
---|
[454] | 130 | } |
---|
| 131 | |
---|
| 132 | int roar_playing(void) { |
---|
[461] | 133 | return FALSE; |
---|
[454] | 134 | } |
---|
| 135 | |
---|
| 136 | void roar_write(void *ptr, int length) { |
---|
[455] | 137 | int r; |
---|
[457] | 138 | |
---|
| 139 | if ( g_inst.pause ) |
---|
| 140 | return; |
---|
[456] | 141 | |
---|
[461] | 142 | ROAR_DBG("roar_write(ptr=%p, length=%i) = (void)", ptr, length); |
---|
| 143 | |
---|
[456] | 144 | while (length) { |
---|
[457] | 145 | if ( (r = write(g_inst.data_fh, ptr, length >= 17640 ? 17640 : length)) != -1 ) { |
---|
[456] | 146 | g_inst.written += r; |
---|
| 147 | ptr += r; |
---|
| 148 | length -= r; |
---|
| 149 | } else { |
---|
[458] | 150 | return; |
---|
[456] | 151 | } |
---|
| 152 | } |
---|
[454] | 153 | } |
---|
| 154 | |
---|
| 155 | int roar_open(AFormat fmt, int rate, int nch) { |
---|
| 156 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 157 | int bits; |
---|
| 158 | |
---|
| 159 | if ( !(g_inst.state & STATE_CONNECTED) ) { |
---|
[1106] | 160 | if ( roar_simple_connect(&(g_inst.con), g_inst.server, g_inst.cfg.player_name) == -1 ) { |
---|
[454] | 161 | return FALSE; |
---|
| 162 | } |
---|
| 163 | g_inst.state |= STATE_CONNECTED; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | bits = 16; |
---|
| 167 | switch (fmt) { |
---|
| 168 | case FMT_S8: |
---|
| 169 | bits = 8; |
---|
| 170 | codec = ROAR_CODEC_DEFAULT; |
---|
| 171 | break; |
---|
| 172 | case FMT_U8: |
---|
| 173 | bits = 8; |
---|
[464] | 174 | codec = ROAR_CODEC_PCM_U_LE; // _LE, _BE, _PDP,... all the same for 8 bit output |
---|
[454] | 175 | break; |
---|
| 176 | case FMT_U16_LE: |
---|
| 177 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 178 | break; |
---|
| 179 | case FMT_U16_BE: |
---|
| 180 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 181 | break; |
---|
| 182 | case FMT_U16_NE: |
---|
| 183 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 184 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 185 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
| 186 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 187 | #else |
---|
| 188 | codec = ROAR_CODEC_PCM_U_PDP; |
---|
| 189 | #endif |
---|
| 190 | break; |
---|
| 191 | case FMT_S16_LE: |
---|
| 192 | codec = ROAR_CODEC_PCM_S_LE; |
---|
| 193 | break; |
---|
| 194 | case FMT_S16_BE: |
---|
| 195 | codec = ROAR_CODEC_PCM_S_BE; |
---|
| 196 | break; |
---|
| 197 | case FMT_S16_NE: |
---|
| 198 | codec = ROAR_CODEC_DEFAULT; |
---|
| 199 | break; |
---|
| 200 | } |
---|
| 201 | |
---|
[456] | 202 | g_inst.bps = nch * rate * bits / 8; |
---|
[455] | 203 | |
---|
[721] | 204 | roar_close(); |
---|
| 205 | |
---|
[454] | 206 | if ( (g_inst.data_fh = roar_simple_new_stream_obj(&(g_inst.con), &(g_inst.stream), |
---|
| 207 | rate, nch, bits, codec, ROAR_DIR_PLAY)) == -1) { |
---|
[720] | 208 | roar_disconnect(&(g_inst.con)); |
---|
| 209 | g_inst.state |= STATE_CONNECTED; |
---|
| 210 | g_inst.state -= STATE_CONNECTED; |
---|
[1036] | 211 | if ( !(g_inst.state & STATE_NORECONNECT) ) { |
---|
| 212 | g_inst.state |= STATE_NORECONNECT; |
---|
| 213 | return roar_open(fmt, rate, nch); |
---|
| 214 | } else { |
---|
| 215 | g_inst.state -= STATE_NORECONNECT; |
---|
| 216 | return FALSE; |
---|
| 217 | } |
---|
[454] | 218 | } |
---|
| 219 | g_inst.state |= STATE_PLAYING; |
---|
| 220 | |
---|
[455] | 221 | g_inst.written = 0; |
---|
[457] | 222 | g_inst.pause = 0; |
---|
[455] | 223 | |
---|
[456] | 224 | roar_update_metadata(); |
---|
| 225 | |
---|
[454] | 226 | return TRUE; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | void roar_close(void) { |
---|
[721] | 230 | if ( g_inst.data_fh != -1 ) |
---|
| 231 | close(g_inst.data_fh); |
---|
[461] | 232 | g_inst.data_fh = -1; |
---|
[454] | 233 | g_inst.state |= STATE_PLAYING; |
---|
| 234 | g_inst.state -= STATE_PLAYING; |
---|
[455] | 235 | g_inst.written = 0; |
---|
[461] | 236 | ROAR_DBG("roar_close(void) = (void)"); |
---|
[454] | 237 | } |
---|
| 238 | |
---|
[457] | 239 | void roar_pause(short p) { |
---|
| 240 | g_inst.pause = p; |
---|
| 241 | } |
---|
[454] | 242 | |
---|
| 243 | int roar_free(void) { |
---|
[460] | 244 | if ( g_inst.pause ) |
---|
| 245 | return 0; |
---|
| 246 | else |
---|
| 247 | return 1000000; // ??? |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | void roar_flush(int time) { |
---|
| 251 | gint64 r = time; |
---|
| 252 | |
---|
| 253 | r *= g_inst.bps; |
---|
| 254 | r /= 1000; |
---|
| 255 | |
---|
| 256 | g_inst.written = r; |
---|
[454] | 257 | } |
---|
| 258 | |
---|
[455] | 259 | int roar_get_output_time(void) { |
---|
| 260 | return roar_get_written_time(); |
---|
| 261 | } |
---|
[456] | 262 | |
---|
[455] | 263 | int roar_get_written_time(void) { |
---|
[456] | 264 | gint64 r; |
---|
| 265 | |
---|
| 266 | if ( !g_inst.bps ) { |
---|
| 267 | ROAR_DBG("roar_get_written_time(void) = 0"); |
---|
| 268 | return 0; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | r = g_inst.written; |
---|
| 272 | r *= 1000; // sec -> msec |
---|
| 273 | r /= g_inst.bps; |
---|
| 274 | ROAR_DBG("roar_get_written_time(void) = %lu", r); |
---|
| 275 | |
---|
| 276 | return r; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | // ABOUT: |
---|
| 281 | |
---|
| 282 | void roar_about(void) { |
---|
| 283 | static GtkWidget *dialog; |
---|
| 284 | |
---|
| 285 | if (dialog != NULL) |
---|
| 286 | return; |
---|
| 287 | |
---|
| 288 | dialog = xmms_show_message( |
---|
| 289 | _("About RoarAudio Plugin"), |
---|
| 290 | _("RoarAudio XMMS Plugin..." |
---|
| 291 | ), _("OK"), FALSE, NULL, NULL); |
---|
| 292 | gtk_signal_connect(GTK_OBJECT(dialog), "destroy", |
---|
| 293 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), |
---|
| 294 | &dialog); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // CONFIG: |
---|
| 298 | |
---|
| 299 | // META DATA: |
---|
| 300 | |
---|
| 301 | int roar_update_metadata(void) { |
---|
| 302 | struct roar_meta meta; |
---|
| 303 | char empty = 0; |
---|
| 304 | char * info; |
---|
| 305 | int pos; |
---|
| 306 | |
---|
| 307 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 308 | |
---|
| 309 | meta.value = ∅ |
---|
| 310 | meta.key[0] = 0; |
---|
| 311 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 312 | |
---|
| 313 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta); |
---|
| 314 | |
---|
| 315 | info = xmms_remote_get_playlist_file(g_inst.session, pos); |
---|
| 316 | |
---|
| 317 | if ( info ) { |
---|
| 318 | if ( strncmp(info, "http:", 5) == 0 ) |
---|
| 319 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
| 320 | else |
---|
| 321 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
| 322 | |
---|
| 323 | meta.value = info; |
---|
[1036] | 324 | ROAR_DBG("roar_update_metadata(*): setting meta data: type=%i, strlen(value)=%i", meta.type, strlen(info)); |
---|
[456] | 325 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 326 | |
---|
| 327 | free(info); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | info = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 331 | if ( info ) { |
---|
| 332 | meta.type = ROAR_META_TYPE_TITLE; |
---|
| 333 | |
---|
| 334 | meta.value = info; |
---|
| 335 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 336 | |
---|
| 337 | free(info); |
---|
| 338 | } |
---|
| 339 | |
---|
[1039] | 340 | meta.value = ∅ |
---|
| 341 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 342 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_FINALIZE, &meta); |
---|
| 343 | |
---|
[456] | 344 | return 0; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | int roar_chk_metadata(void) { |
---|
| 348 | static int old_pos = -1; |
---|
| 349 | static char * old_title = "NEW TITLE"; |
---|
| 350 | int pos; |
---|
| 351 | char * title; |
---|
| 352 | int need_update = 0; |
---|
| 353 | |
---|
| 354 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 355 | |
---|
| 356 | if ( pos != old_pos ) { |
---|
| 357 | old_pos = pos; |
---|
| 358 | need_update = 1; |
---|
| 359 | } else { |
---|
| 360 | title = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 361 | |
---|
| 362 | if ( strcmp(title, old_title) ) { |
---|
| 363 | free(old_title); |
---|
| 364 | old_title = title; |
---|
| 365 | need_update = 1; |
---|
| 366 | } else { |
---|
| 367 | free(title); |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | if ( need_update ) { |
---|
| 372 | roar_update_metadata(); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | return 0; |
---|
[455] | 376 | } |
---|
| 377 | |
---|
[459] | 378 | // MIXER: |
---|
| 379 | |
---|
| 380 | void roar_get_volume(int *l, int *r) { |
---|
| 381 | int channels; |
---|
| 382 | struct roar_mixer_settings mixer; |
---|
| 383 | |
---|
| 384 | if ( !(g_inst.state & STATE_CONNECTED) ) |
---|
| 385 | return; |
---|
| 386 | |
---|
| 387 | if ( roar_get_vol(&(g_inst.con), g_inst.stream.id, &mixer, &channels) == -1 ) { |
---|
| 388 | *l = *r = 100; |
---|
| 389 | return; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | if ( channels == 1 ) { |
---|
| 393 | *l = *r = mixer.mixer[0]/655.35; |
---|
| 394 | } else { |
---|
| 395 | *l = mixer.mixer[0]/655.35; |
---|
| 396 | *r = mixer.mixer[1]/655.35; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | void roar_set_volume(int l, int r) { |
---|
| 401 | struct roar_mixer_settings mixer; |
---|
| 402 | |
---|
| 403 | if ( !(g_inst.state & STATE_CONNECTED) ) |
---|
| 404 | return; |
---|
| 405 | |
---|
| 406 | mixer.mixer[0] = l * 655.35; |
---|
| 407 | mixer.mixer[1] = r * 655.35; |
---|
| 408 | |
---|
| 409 | roar_set_vol(&(g_inst.con), g_inst.stream.id, &mixer, 2); |
---|
| 410 | } |
---|
| 411 | |
---|
[452] | 412 | //ll |
---|