source: roaraudio/plugins/xmms/roar.c @ 6052:d48765b2475e

Last change on this file since 6052:d48765b2475e was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 8.0 KB
RevLine 
[452]1//roar.c:
2
[703]3/*
[6052]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015
[703]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[703]23 *
24 */
25
[1107]26#include "all.h"
[453]27
[454]28OutputPlugin roar_op = {
[453]29        NULL,
30        NULL,
31        "RoarAudio XMMS Plugin", /* Description */
32        roar_init,
[456]33        roar_about,
[1107]34        roar_configure,
[459]35        roar_get_volume,
36        roar_set_volume,
[453]37        roar_open,
38        roar_write,
39        roar_close,
[460]40        roar_flush,
[457]41        roar_pause,
[453]42        roar_free,
43        roar_playing,
[455]44        roar_get_output_time,
45        roar_get_written_time,
[453]46};
47
[454]48OutputPlugin *get_oplugin_info(void) {
49 return &roar_op;
50}
51
52void roar_init(void) {
[1106]53 ConfigFile * cfgfile;
54
55 cfgfile = xmms_cfg_open_default_file();
56
[454]57 g_inst.state = 0;
58 g_inst.server = NULL;
[456]59 g_inst.session = ctrlsocket_get_session_id();
[2287]60 g_inst.mixer.l = -1;
61 g_inst.mixer.r = -1;
[1106]62
63 xmms_cfg_read_string(cfgfile, "ROAR", "server", &g_inst.server);
64
65 xmms_cfg_read_string(cfgfile, "ROAR", "player_name", &g_inst.cfg.player_name);
66
67 xmms_cfg_free(cfgfile);
68
69 if ( g_inst.cfg.player_name == NULL )
70  g_inst.cfg.player_name = "XMMS";
71
[456]72 ROAR_DBG("roar_init(*) = (void)");
[454]73}
74
75int roar_playing(void) {
[461]76 return FALSE;
[454]77}
78
79void roar_write(void *ptr, int length) {
[3622]80 ssize_t r;
[457]81
82 if ( g_inst.pause )
83  return;
[456]84
[461]85 ROAR_DBG("roar_write(ptr=%p, length=%i) = (void)", ptr, length);
86
[456]87 while (length) {
[3625]88  if ( (r = roar_vio_write(&(g_inst.vio), ptr, length >= 1764*4 ? 1764*4 : length)) != -1 ) {
[456]89   g_inst.written   += r;
90   ptr              += r;
91   length           -= r;
92  } else {
[3625]93   if ( errno == EAGAIN ) {
94    xmms_usleep(1000);
95   } else {
96    return;
97   }
[456]98  }
99 }
[454]100}
101
102int roar_open(AFormat fmt, int rate, int nch) {
103 int codec = ROAR_CODEC_DEFAULT;
104 int bits;
105
106 if ( !(g_inst.state & STATE_CONNECTED) ) {
[1106]107  if ( roar_simple_connect(&(g_inst.con), g_inst.server, g_inst.cfg.player_name) == -1 ) {
[454]108   return FALSE;
109  }
110  g_inst.state |= STATE_CONNECTED;
111 }
112
113  bits = 16;
114  switch (fmt) {
115   case FMT_S8:
116     bits = 8;
117     codec = ROAR_CODEC_DEFAULT;
118    break;
119   case FMT_U8:
120     bits = 8;
[464]121     codec = ROAR_CODEC_PCM_U_LE; // _LE, _BE, _PDP,... all the same for 8 bit output
[454]122    break;
123   case FMT_U16_LE:
124     codec = ROAR_CODEC_PCM_U_LE;
125    break;
126   case FMT_U16_BE:
127     codec = ROAR_CODEC_PCM_U_BE;
128    break;
129   case FMT_U16_NE:
130#if BYTE_ORDER == BIG_ENDIAN
131     codec = ROAR_CODEC_PCM_U_BE;
132#elif BYTE_ORDER == LITTLE_ENDIAN
133     codec = ROAR_CODEC_PCM_U_LE;
134#else
135     codec = ROAR_CODEC_PCM_U_PDP;
136#endif
137    break;
138   case FMT_S16_LE:
139     codec = ROAR_CODEC_PCM_S_LE;
140    break;
141   case FMT_S16_BE:
142     codec = ROAR_CODEC_PCM_S_BE;
143    break;
144   case FMT_S16_NE:
145     codec = ROAR_CODEC_DEFAULT;
146    break;
147 }
148
[456]149 g_inst.bps       = nch * rate * bits / 8;
[455]150
[721]151 roar_close();
152
[3622]153 if ( roar_vio_simple_new_stream_obj(&(g_inst.vio), &(g_inst.con), &(g_inst.stream),
[5289]154                                     rate, nch, bits, codec, ROAR_DIR_PLAY, -1) == -1 ) {
[720]155  roar_disconnect(&(g_inst.con));
156  g_inst.state |= STATE_CONNECTED;
157  g_inst.state -= STATE_CONNECTED;
[1036]158  if ( !(g_inst.state & STATE_NORECONNECT) ) {
159   g_inst.state |= STATE_NORECONNECT;
[3624]160   xmms_usleep(100000);
[1036]161   return roar_open(fmt, rate, nch);
162  } else {
163   g_inst.state -= STATE_NORECONNECT;
164   return FALSE;
165  }
[454]166 }
167 g_inst.state |= STATE_PLAYING;
168
[455]169 g_inst.written = 0;
[457]170 g_inst.pause   = 0;
[455]171
[5235]172 _LIBROAR_IGNORE_RET(roar_stream_set_role(&(g_inst.con), &(g_inst.stream), ROAR_ROLE_MUSIC));
[456]173 roar_update_metadata();
[2287]174 roar_set_volume(g_inst.mixer.l, g_inst.mixer.r);
[456]175
[454]176 return TRUE;
177}
178
179void roar_close(void) {
[3622]180 int was_playing = g_inst.state & STATE_PLAYING;
181
[454]182 g_inst.state |= STATE_PLAYING;
183 g_inst.state -= STATE_PLAYING;
[3622]184
[3623]185 if ( was_playing ) {
[3622]186  roar_vio_close(&(g_inst.vio));
[3624]187  xmms_usleep(10000); // give the server 10ms of extra time
[3623]188 }
[3622]189
[455]190 g_inst.written = 0;
[461]191 ROAR_DBG("roar_close(void) = (void)");
[454]192}
193
[457]194void roar_pause(short p) {
195 g_inst.pause = p;
196}
[454]197
198int roar_free(void) {
[460]199 if ( g_inst.pause )
200  return 0;
201 else
202  return 1000000; // ???
203}
204
205void roar_flush(int time) {
206 gint64 r = time;
207
208 r *= g_inst.bps;
209 r /= 1000;
210
211 g_inst.written = r;
[454]212}
213
[455]214int roar_get_output_time(void) {
215 return roar_get_written_time();
216}
[456]217
[455]218int roar_get_written_time(void) {
[456]219 gint64 r;
220
221 if ( !g_inst.bps ) {
222  ROAR_DBG("roar_get_written_time(void) = 0");
223  return 0;
224 }
225
226 r  = g_inst.written;
227 r *= 1000; // sec -> msec
228 r /= g_inst.bps;
229 ROAR_DBG("roar_get_written_time(void) = %lu", r);
230
231 return r;
232}
233
234
235// META DATA:
236
237int roar_update_metadata(void) {
238 struct roar_meta   meta;
239 char empty = 0;
240 char * info;
[2331]241 char * delm;
[456]242 int pos;
243
244 pos     = xmms_remote_get_playlist_pos(g_inst.session);
245
246 meta.value = ∅
247 meta.key[0] = 0;
248 meta.type = ROAR_META_TYPE_NONE;
249
250 roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta);
251
252 info = xmms_remote_get_playlist_file(g_inst.session, pos);
253
254 if ( info ) {
255  if ( strncmp(info, "http:", 5) == 0 )
256   meta.type = ROAR_META_TYPE_FILEURL;
257  else
258   meta.type = ROAR_META_TYPE_FILENAME;
259
260  meta.value = info;
[1036]261  ROAR_DBG("roar_update_metadata(*): setting meta data: type=%i, strlen(value)=%i", meta.type, strlen(info));
[456]262  roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
263
264  free(info);
265 }
266
267 info = xmms_remote_get_playlist_title(g_inst.session, pos);
268 if ( info ) {
[2331]269
270  if ( (delm = strstr(info, " - ")) != NULL ) {
271   *delm = 0;
272   meta.value  = info;
273   meta.type   = ROAR_META_TYPE_ARTIST;
274   roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
275
276   meta.value = delm + 3;
277  } else {
278   meta.value = info;
279  }
280
[456]281  meta.type = ROAR_META_TYPE_TITLE;
282
283  roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
284
285  free(info);
286 }
287
[1039]288 meta.value = ∅
289 meta.type = ROAR_META_TYPE_NONE;
290 roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_FINALIZE, &meta);
291
[456]292 return 0;
293}
294
295int roar_chk_metadata(void) {
296 static int    old_pos = -1;
297 static char * old_title = "NEW TITLE";
298 int pos;
299 char * title;
300 int need_update = 0;
301
302 pos     = xmms_remote_get_playlist_pos(g_inst.session);
303
304 if ( pos != old_pos ) {
305  old_pos = pos;
306  need_update = 1;
307 } else {
308  title = xmms_remote_get_playlist_title(g_inst.session, pos);
309
310  if ( strcmp(title, old_title) ) {
311   free(old_title);
312   old_title = title;
313   need_update = 1;
314  } else {
315   free(title);
316  }
317 }
318
319 if ( need_update ) {
320  roar_update_metadata();
321 }
322
323 return 0;
[455]324}
325
[459]326// MIXER:
327
328void roar_get_volume(int *l, int *r) {
329 int channels;
330 struct roar_mixer_settings mixer;
[3532]331 float fs;
[459]332
[3620]333 if ( !(g_inst.state & STATE_CONNECTED) || !(g_inst.state & STATE_PLAYING) ) {
334  *l = g_inst.mixer.l;
335  *r = g_inst.mixer.r;
[459]336  return;
[3620]337 }
[459]338
339 if ( roar_get_vol(&(g_inst.con), g_inst.stream.id, &mixer, &channels) == -1 ) {
340  *l = *r = 100;
341  return;
342 }
343
[3532]344 fs = (float)mixer.scale/100.;
345
[459]346 if ( channels == 1 ) {
[3532]347  *l = *r = mixer.mixer[0]/fs;
[459]348 } else {
[3532]349  *l = mixer.mixer[0]/fs;
350  *r = mixer.mixer[1]/fs;
[459]351 }
352}
353
354void roar_set_volume(int l, int r) {
355 struct roar_mixer_settings mixer;
356
357 if ( !(g_inst.state & STATE_CONNECTED) )
358  return;
359
[2287]360 if ( l == -1 ) l = 100;
361 if ( r == -1 ) r = 100;
362
363 g_inst.mixer.l = l;
364 g_inst.mixer.r = r;
365
[3620]366 if ( !(g_inst.state & STATE_PLAYING) )
367  return;
368
[3532]369 mixer.mixer[0] = l;
370 mixer.mixer[1] = r;
371 mixer.scale    = 100;
[459]372
[5236]373 _LIBROAR_IGNORE_RET(roar_set_vol(&(g_inst.con), g_inst.stream.id, &mixer, 2, ROAR_SET_VOL_UNMAPPED));
[459]374}
375
[452]376//ll
Note: See TracBrowser for help on using the repository browser.