source: roaraudio/plugins/xmms/roar.c @ 3623:20fccd6c8d90

Last change on this file since 3623:20fccd6c8d90 was 3623:20fccd6c8d90, checked in by phi, 14 years ago

give the server 10 extra ms, maybe that makes the plugin more stable.

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