[452] | 1 | //roar.c: |
---|
| 2 | |
---|
| 3 | #include <roaraudio.h> |
---|
| 4 | |
---|
[453] | 5 | //#include "xmms/i18n.h" |
---|
| 6 | #include <gtk/gtk.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <string.h> |
---|
| 9 | |
---|
| 10 | #include "xmms/plugin.h" |
---|
| 11 | #include "xmms/xmmsctrl.h" |
---|
| 12 | #include "xmms/dirbrowser.h" |
---|
| 13 | #include "xmms/configfile.h" |
---|
| 14 | #include "xmms/util.h" |
---|
| 15 | |
---|
[456] | 16 | #define _(x) (x) |
---|
| 17 | |
---|
| 18 | gint ctrlsocket_get_session_id(void); |
---|
| 19 | |
---|
[453] | 20 | void roar_init(void); |
---|
| 21 | void roar_about(void); |
---|
| 22 | void roar_configure(void); |
---|
| 23 | |
---|
| 24 | void roar_get_volume(int *l, int *r); |
---|
| 25 | void roar_fetch_volume(int *l, int *r); |
---|
| 26 | void roar_set_volume(int l, int r); |
---|
| 27 | void roar_mixer_init(void); |
---|
| 28 | void roar_mixer_init_vol(int l, int r); |
---|
| 29 | |
---|
| 30 | int roar_playing(void); |
---|
| 31 | int roar_free(void); |
---|
| 32 | void roar_write(void *ptr, int length); |
---|
| 33 | void roar_close(void); |
---|
| 34 | void roar_flush(int time); |
---|
| 35 | void roar_pause(short p); |
---|
| 36 | int roar_open(AFormat fmt, int rate, int nch); |
---|
| 37 | int roar_get_output_time(void); |
---|
| 38 | int roar_get_written_time(void); |
---|
| 39 | |
---|
[456] | 40 | int roar_update_metadata(void); |
---|
| 41 | int roar_chk_metadata(void); |
---|
[453] | 42 | |
---|
[454] | 43 | OutputPlugin roar_op = { |
---|
[453] | 44 | NULL, |
---|
| 45 | NULL, |
---|
| 46 | "RoarAudio XMMS Plugin", /* Description */ |
---|
| 47 | roar_init, |
---|
[456] | 48 | roar_about, |
---|
[454] | 49 | NULL, //roar_configure, |
---|
| 50 | NULL, //roar_get_volume, |
---|
| 51 | NULL, //roar_set_volume, |
---|
[453] | 52 | roar_open, |
---|
| 53 | roar_write, |
---|
| 54 | roar_close, |
---|
[454] | 55 | NULL, //roar_flush, |
---|
[457] | 56 | roar_pause, |
---|
[453] | 57 | roar_free, |
---|
| 58 | roar_playing, |
---|
[455] | 59 | roar_get_output_time, |
---|
| 60 | roar_get_written_time, |
---|
[453] | 61 | }; |
---|
| 62 | |
---|
[454] | 63 | #define STATE_CONNECTED 1 |
---|
| 64 | #define STATE_PLAYING 2 |
---|
| 65 | |
---|
| 66 | struct xmms_roar_out { |
---|
| 67 | int state; |
---|
| 68 | char * server; |
---|
| 69 | struct roar_connection con; |
---|
| 70 | struct roar_stream stream; |
---|
| 71 | int data_fh; |
---|
[456] | 72 | long unsigned int written; |
---|
| 73 | long unsigned int bps; |
---|
| 74 | int session; |
---|
[457] | 75 | int pause; |
---|
[454] | 76 | } g_inst; |
---|
| 77 | |
---|
| 78 | OutputPlugin *get_oplugin_info(void) { |
---|
| 79 | return &roar_op; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void roar_init(void) { |
---|
| 83 | g_inst.state = 0; |
---|
| 84 | g_inst.server = NULL; |
---|
[456] | 85 | g_inst.session = ctrlsocket_get_session_id(); |
---|
| 86 | ROAR_DBG("roar_init(*) = (void)"); |
---|
[454] | 87 | } |
---|
| 88 | |
---|
| 89 | int roar_playing(void) { |
---|
| 90 | return g_inst.state & STATE_PLAYING ? TRUE : FALSE; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void roar_write(void *ptr, int length) { |
---|
[455] | 94 | int r; |
---|
[457] | 95 | |
---|
| 96 | if ( g_inst.pause ) |
---|
| 97 | return; |
---|
[456] | 98 | |
---|
| 99 | while (length) { |
---|
[457] | 100 | if ( (r = write(g_inst.data_fh, ptr, length >= 17640 ? 17640 : length)) != -1 ) { |
---|
[456] | 101 | g_inst.written += r; |
---|
| 102 | ptr += r; |
---|
| 103 | length -= r; |
---|
| 104 | } else { |
---|
[458] | 105 | return; |
---|
[456] | 106 | } |
---|
| 107 | } |
---|
[454] | 108 | } |
---|
| 109 | |
---|
| 110 | int roar_open(AFormat fmt, int rate, int nch) { |
---|
| 111 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 112 | int bits; |
---|
| 113 | |
---|
| 114 | if ( !(g_inst.state & STATE_CONNECTED) ) { |
---|
| 115 | if ( roar_simple_connect(&(g_inst.con), g_inst.server, "XMMS") == -1 ) { |
---|
| 116 | return FALSE; |
---|
| 117 | } |
---|
| 118 | g_inst.state |= STATE_CONNECTED; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | bits = 16; |
---|
| 122 | switch (fmt) { |
---|
| 123 | case FMT_S8: |
---|
| 124 | bits = 8; |
---|
| 125 | codec = ROAR_CODEC_DEFAULT; |
---|
| 126 | break; |
---|
| 127 | case FMT_U8: |
---|
| 128 | bits = 8; |
---|
| 129 | codec = ROAR_CODEC_PCM_S_LE; // _LE, _BE, _PDP,... all the same for 8 bit output |
---|
| 130 | break; |
---|
| 131 | case FMT_U16_LE: |
---|
| 132 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 133 | break; |
---|
| 134 | case FMT_U16_BE: |
---|
| 135 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 136 | break; |
---|
| 137 | case FMT_U16_NE: |
---|
| 138 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 139 | codec = ROAR_CODEC_PCM_U_BE; |
---|
| 140 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
| 141 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 142 | #else |
---|
| 143 | codec = ROAR_CODEC_PCM_U_PDP; |
---|
| 144 | #endif |
---|
| 145 | break; |
---|
| 146 | case FMT_S16_LE: |
---|
| 147 | codec = ROAR_CODEC_PCM_S_LE; |
---|
| 148 | break; |
---|
| 149 | case FMT_S16_BE: |
---|
| 150 | codec = ROAR_CODEC_PCM_S_BE; |
---|
| 151 | break; |
---|
| 152 | case FMT_S16_NE: |
---|
| 153 | codec = ROAR_CODEC_DEFAULT; |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | |
---|
[456] | 157 | g_inst.bps = nch * rate * bits / 8; |
---|
[455] | 158 | |
---|
[454] | 159 | if ( (g_inst.data_fh = roar_simple_new_stream_obj(&(g_inst.con), &(g_inst.stream), |
---|
| 160 | rate, nch, bits, codec, ROAR_DIR_PLAY)) == -1) { |
---|
| 161 | return FALSE; |
---|
| 162 | } |
---|
| 163 | g_inst.state |= STATE_PLAYING; |
---|
| 164 | |
---|
[455] | 165 | g_inst.written = 0; |
---|
[457] | 166 | g_inst.pause = 0; |
---|
[455] | 167 | |
---|
[456] | 168 | roar_update_metadata(); |
---|
| 169 | |
---|
[454] | 170 | return TRUE; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void roar_close(void) { |
---|
| 174 | close(g_inst.data_fh); |
---|
| 175 | g_inst.state |= STATE_PLAYING; |
---|
| 176 | g_inst.state -= STATE_PLAYING; |
---|
[455] | 177 | g_inst.written = 0; |
---|
[454] | 178 | } |
---|
| 179 | |
---|
[457] | 180 | void roar_pause(short p) { |
---|
| 181 | g_inst.pause = p; |
---|
| 182 | } |
---|
[454] | 183 | |
---|
| 184 | int roar_free(void) { |
---|
| 185 | return 1000000; // ??? |
---|
| 186 | } |
---|
| 187 | |
---|
[455] | 188 | int roar_get_output_time(void) { |
---|
| 189 | return roar_get_written_time(); |
---|
| 190 | } |
---|
[456] | 191 | |
---|
[455] | 192 | int roar_get_written_time(void) { |
---|
[456] | 193 | gint64 r; |
---|
| 194 | |
---|
| 195 | if ( !g_inst.bps ) { |
---|
| 196 | ROAR_DBG("roar_get_written_time(void) = 0"); |
---|
| 197 | return 0; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | r = g_inst.written; |
---|
| 201 | r *= 1000; // sec -> msec |
---|
| 202 | r /= g_inst.bps; |
---|
| 203 | ROAR_DBG("roar_get_written_time(void) = %lu", r); |
---|
| 204 | |
---|
| 205 | return r; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | // ABOUT: |
---|
| 210 | |
---|
| 211 | void roar_about(void) { |
---|
| 212 | static GtkWidget *dialog; |
---|
| 213 | |
---|
| 214 | if (dialog != NULL) |
---|
| 215 | return; |
---|
| 216 | |
---|
| 217 | dialog = xmms_show_message( |
---|
| 218 | _("About RoarAudio Plugin"), |
---|
| 219 | _("RoarAudio XMMS Plugin..." |
---|
| 220 | ), _("OK"), FALSE, NULL, NULL); |
---|
| 221 | gtk_signal_connect(GTK_OBJECT(dialog), "destroy", |
---|
| 222 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), |
---|
| 223 | &dialog); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | // CONFIG: |
---|
| 227 | |
---|
| 228 | // META DATA: |
---|
| 229 | |
---|
| 230 | int roar_update_metadata(void) { |
---|
| 231 | struct roar_meta meta; |
---|
| 232 | char empty = 0; |
---|
| 233 | char * info; |
---|
| 234 | int pos; |
---|
| 235 | |
---|
| 236 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 237 | |
---|
| 238 | meta.value = ∅ |
---|
| 239 | meta.key[0] = 0; |
---|
| 240 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 241 | |
---|
| 242 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta); |
---|
| 243 | |
---|
| 244 | info = xmms_remote_get_playlist_file(g_inst.session, pos); |
---|
| 245 | |
---|
| 246 | if ( info ) { |
---|
| 247 | if ( strncmp(info, "http:", 5) == 0 ) |
---|
| 248 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
| 249 | else |
---|
| 250 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
| 251 | |
---|
| 252 | meta.value = info; |
---|
| 253 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 254 | |
---|
| 255 | free(info); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | info = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 259 | |
---|
| 260 | if ( info ) { |
---|
| 261 | meta.type = ROAR_META_TYPE_TITLE; |
---|
| 262 | |
---|
| 263 | meta.value = info; |
---|
| 264 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
| 265 | |
---|
| 266 | free(info); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | return 0; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | int roar_chk_metadata(void) { |
---|
| 273 | static int old_pos = -1; |
---|
| 274 | static char * old_title = "NEW TITLE"; |
---|
| 275 | int pos; |
---|
| 276 | char * title; |
---|
| 277 | int need_update = 0; |
---|
| 278 | |
---|
| 279 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
| 280 | |
---|
| 281 | if ( pos != old_pos ) { |
---|
| 282 | old_pos = pos; |
---|
| 283 | need_update = 1; |
---|
| 284 | } else { |
---|
| 285 | title = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
| 286 | |
---|
| 287 | if ( strcmp(title, old_title) ) { |
---|
| 288 | free(old_title); |
---|
| 289 | old_title = title; |
---|
| 290 | need_update = 1; |
---|
| 291 | } else { |
---|
| 292 | free(title); |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | if ( need_update ) { |
---|
| 297 | roar_update_metadata(); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | return 0; |
---|
[455] | 301 | } |
---|
| 302 | |
---|
[452] | 303 | //ll |
---|