[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 | |
---|
[454] | 84 | #define STATE_CONNECTED 1 |
---|
| 85 | #define STATE_PLAYING 2 |
---|
| 86 | |
---|
| 87 | struct xmms_roar_out { |
---|
| 88 | int state; |
---|
| 89 | char * server; |
---|
| 90 | struct roar_connection con; |
---|
| 91 | struct roar_stream stream; |
---|
| 92 | int data_fh; |
---|
[456] | 93 | long unsigned int written; |
---|
| 94 | long unsigned int bps; |
---|
| 95 | int session; |
---|
[457] | 96 | int pause; |
---|
[454] | 97 | } g_inst; |
---|
| 98 | |
---|
| 99 | OutputPlugin *get_oplugin_info(void) { |
---|
| 100 | return &roar_op; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void roar_init(void) { |
---|
| 104 | g_inst.state = 0; |
---|
| 105 | g_inst.server = NULL; |
---|
[456] | 106 | g_inst.session = ctrlsocket_get_session_id(); |
---|
| 107 | ROAR_DBG("roar_init(*) = (void)"); |
---|
[454] | 108 | } |
---|
| 109 | |
---|
| 110 | int roar_playing(void) { |
---|
[461] | 111 | return FALSE; |
---|
[454] | 112 | } |
---|
| 113 | |
---|
| 114 | void roar_write(void *ptr, int length) { |
---|
[455] | 115 | int r; |
---|
[457] | 116 | |
---|
| 117 | if ( g_inst.pause ) |
---|
| 118 | return; |
---|
[456] | 119 | |
---|
[461] | 120 | ROAR_DBG("roar_write(ptr=%p, length=%i) = (void)", ptr, length); |
---|
| 121 | |
---|
[456] | 122 | while (length) { |
---|
[457] | 123 | if ( (r = write(g_inst.data_fh, ptr, length >= 17640 ? 17640 : length)) != -1 ) { |
---|
[456] | 124 | g_inst.written += r; |
---|
| 125 | ptr += r; |
---|
| 126 | length -= r; |
---|
| 127 | } else { |
---|
[458] | 128 | return; |
---|
[456] | 129 | } |
---|
| 130 | } |
---|
[454] | 131 | } |
---|
| 132 | |
---|
| 133 | int roar_open(AFormat fmt, int rate, int nch) { |
---|
| 134 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 135 | int bits; |
---|
| 136 | |
---|
| 137 | if ( !(g_inst.state & STATE_CONNECTED) ) { |
---|
| 138 | if ( roar_simple_connect(&(g_inst.con), g_inst.server, "XMMS") == -1 ) { |
---|
| 139 | return FALSE; |
---|
| 140 | } |
---|
| 141 | g_inst.state |= STATE_CONNECTED; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | bits = 16; |
---|
| 145 | switch (fmt) { |
---|
| 146 | case FMT_S8: |
---|
| 147 | bits = 8; |
---|
| 148 | codec = ROAR_CODEC_DEFAULT; |
---|
| 149 | break; |
---|
| 150 | case FMT_U8: |
---|
| 151 | bits = 8; |
---|
[464] | 152 | codec = ROAR_CODEC_PCM_U_LE; // _LE, _BE, _PDP,... all the same for 8 bit output |
---|
[454] | 153 | break; |
---|
| 154 | case FMT_U16_LE: |
---|
| 155 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 156 | break; |
---|
| 157 | case FMT_U16_BE: |
---|
| 158 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 159 | break; |
---|
| 160 | case FMT_U16_NE: |
---|
| 161 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 162 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 163 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
| 164 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 165 | #else |
---|
| 166 | codec = ROAR_CODEC_PCM_U_PDP; |
---|
| 167 | #endif |
---|
| 168 | break; |
---|
| 169 | case FMT_S16_LE: |
---|
| 170 | codec = ROAR_CODEC_PCM_S_LE; |
---|
| 171 | break; |
---|
| 172 | case FMT_S16_BE: |
---|
| 173 | codec = ROAR_CODEC_PCM_S_BE; |
---|
| 174 | break; |
---|
| 175 | case FMT_S16_NE: |
---|
| 176 | codec = ROAR_CODEC_DEFAULT; |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | |
---|
[456] | 180 | g_inst.bps = nch * rate * bits / 8; |
---|
[455] | 181 | |
---|
[721] | 182 | roar_close(); |
---|
| 183 | |
---|
[454] | 184 | if ( (g_inst.data_fh = roar_simple_new_stream_obj(&(g_inst.con), &(g_inst.stream), |
---|
| 185 | rate, nch, bits, codec, ROAR_DIR_PLAY)) == -1) { |
---|
[720] | 186 | roar_disconnect(&(g_inst.con)); |
---|
| 187 | g_inst.state |= STATE_CONNECTED; |
---|
| 188 | g_inst.state -= STATE_CONNECTED; |
---|
[454] | 189 | return FALSE; |
---|
| 190 | } |
---|
| 191 | g_inst.state |= STATE_PLAYING; |
---|
| 192 | |
---|
[455] | 193 | g_inst.written = 0; |
---|
[457] | 194 | g_inst.pause = 0; |
---|
[455] | 195 | |
---|
[456] | 196 | roar_update_metadata(); |
---|
| 197 | |
---|
[454] | 198 | return TRUE; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void roar_close(void) { |
---|
[721] | 202 | if ( g_inst.data_fh != -1 ) |
---|
| 203 | close(g_inst.data_fh); |
---|
[461] | 204 | g_inst.data_fh = -1; |
---|
[454] | 205 | g_inst.state |= STATE_PLAYING; |
---|
| 206 | g_inst.state -= STATE_PLAYING; |
---|
[455] | 207 | g_inst.written = 0; |
---|
[461] | 208 | ROAR_DBG("roar_close(void) = (void)"); |
---|
[454] | 209 | } |
---|
| 210 | |
---|
[457] | 211 | void roar_pause(short p) { |
---|
| 212 | g_inst.pause = p; |
---|
| 213 | } |
---|
[454] | 214 | |
---|
| 215 | int roar_free(void) { |
---|
[460] | 216 | if ( g_inst.pause ) |
---|
| 217 | return 0; |
---|
| 218 | else |
---|
| 219 | return 1000000; // ??? |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void roar_flush(int time) { |
---|
| 223 | gint64 r = time; |
---|
| 224 | |
---|
| 225 | r *= g_inst.bps; |
---|
| 226 | r /= 1000; |
---|
| 227 | |
---|
| 228 | g_inst.written = r; |
---|
[454] | 229 | } |
---|
| 230 | |
---|
[455] | 231 | int roar_get_output_time(void) { |
---|
| 232 | return roar_get_written_time(); |
---|
| 233 | } |
---|
[456] | 234 | |
---|
[455] | 235 | int roar_get_written_time(void) { |
---|
[456] | 236 | gint64 r; |
---|
| 237 | |
---|
| 238 | if ( !g_inst.bps ) { |
---|
| 239 | ROAR_DBG("roar_get_written_time(void) = 0"); |
---|
| 240 | return 0; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | r = g_inst.written; |
---|
| 244 | r *= 1000; // sec -> msec |
---|
| 245 | r /= g_inst.bps; |
---|
| 246 | ROAR_DBG("roar_get_written_time(void) = %lu", r); |
---|
| 247 | |
---|
| 248 | return r; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | // ABOUT: |
---|
| 253 | |
---|
| 254 | void roar_about(void) { |
---|
| 255 | static GtkWidget *dialog; |
---|
| 256 | |
---|
| 257 | if (dialog != NULL) |
---|
| 258 | return; |
---|
| 259 | |
---|
| 260 | dialog = xmms_show_message( |
---|
| 261 | _("About RoarAudio Plugin"), |
---|
| 262 | _("RoarAudio XMMS Plugin..." |
---|
| 263 | ), _("OK"), FALSE, NULL, NULL); |
---|
| 264 | gtk_signal_connect(GTK_OBJECT(dialog), "destroy", |
---|
| 265 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), |
---|
| 266 | &dialog); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | // CONFIG: |
---|
| 270 | |
---|
| 271 | // META DATA: |
---|
| 272 | |
---|
| 273 | int roar_update_metadata(void) { |
---|
| 274 | struct roar_meta meta; |
---|
| 275 | char empty = 0; |
---|
| 276 | char * info; |
---|
| 277 | int pos; |
---|
| 278 | |
---|
| 279 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 280 | |
---|
| 281 | meta.value = ∅ |
---|
| 282 | meta.key[0] = 0; |
---|
| 283 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 284 | |
---|
| 285 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta); |
---|
| 286 | |
---|
| 287 | info = xmms_remote_get_playlist_file(g_inst.session, pos); |
---|
| 288 | |
---|
| 289 | if ( info ) { |
---|
| 290 | if ( strncmp(info, "http:", 5) == 0 ) |
---|
| 291 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
| 292 | else |
---|
| 293 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
| 294 | |
---|
| 295 | meta.value = info; |
---|
| 296 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 297 | |
---|
| 298 | free(info); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | info = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 302 | |
---|
| 303 | if ( info ) { |
---|
| 304 | meta.type = ROAR_META_TYPE_TITLE; |
---|
| 305 | |
---|
| 306 | meta.value = info; |
---|
| 307 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 308 | |
---|
| 309 | free(info); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | int roar_chk_metadata(void) { |
---|
| 316 | static int old_pos = -1; |
---|
| 317 | static char * old_title = "NEW TITLE"; |
---|
| 318 | int pos; |
---|
| 319 | char * title; |
---|
| 320 | int need_update = 0; |
---|
| 321 | |
---|
| 322 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 323 | |
---|
| 324 | if ( pos != old_pos ) { |
---|
| 325 | old_pos = pos; |
---|
| 326 | need_update = 1; |
---|
| 327 | } else { |
---|
| 328 | title = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 329 | |
---|
| 330 | if ( strcmp(title, old_title) ) { |
---|
| 331 | free(old_title); |
---|
| 332 | old_title = title; |
---|
| 333 | need_update = 1; |
---|
| 334 | } else { |
---|
| 335 | free(title); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | if ( need_update ) { |
---|
| 340 | roar_update_metadata(); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | return 0; |
---|
[455] | 344 | } |
---|
| 345 | |
---|
[459] | 346 | // MIXER: |
---|
| 347 | |
---|
| 348 | void roar_get_volume(int *l, int *r) { |
---|
| 349 | int channels; |
---|
| 350 | struct roar_mixer_settings mixer; |
---|
| 351 | |
---|
| 352 | if ( !(g_inst.state & STATE_CONNECTED) ) |
---|
| 353 | return; |
---|
| 354 | |
---|
| 355 | if ( roar_get_vol(&(g_inst.con), g_inst.stream.id, &mixer, &channels) == -1 ) { |
---|
| 356 | *l = *r = 100; |
---|
| 357 | return; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | if ( channels == 1 ) { |
---|
| 361 | *l = *r = mixer.mixer[0]/655.35; |
---|
| 362 | } else { |
---|
| 363 | *l = mixer.mixer[0]/655.35; |
---|
| 364 | *r = mixer.mixer[1]/655.35; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | void roar_set_volume(int l, int r) { |
---|
| 369 | struct roar_mixer_settings mixer; |
---|
| 370 | |
---|
| 371 | if ( !(g_inst.state & STATE_CONNECTED) ) |
---|
| 372 | return; |
---|
| 373 | |
---|
| 374 | mixer.mixer[0] = l * 655.35; |
---|
| 375 | mixer.mixer[1] = r * 655.35; |
---|
| 376 | |
---|
| 377 | roar_set_vol(&(g_inst.con), g_inst.stream.id, &mixer, 2); |
---|
| 378 | } |
---|
| 379 | |
---|
[452] | 380 | //ll |
---|