source: roaraudio/plugins/xmms/roar.c @ 3625:2eacce017c57

Last change on this file since 3625:2eacce017c57 was 3625:2eacce017c57, checked in by phi, 14 years ago

better error handling, bit bigger write size

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*4 ? 1764*4 : length)) != -1 ) {
89   g_inst.written   += r;
90   ptr              += r;
91   length           -= r;
92  } else {
93   if ( errno == EAGAIN ) {
94    xmms_usleep(1000);
95   } else {
96    return;
97   }
98  }
99 }
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) ) {
107  if ( roar_simple_connect(&(g_inst.con), g_inst.server, g_inst.cfg.player_name) == -1 ) {
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;
121     codec = ROAR_CODEC_PCM_U_LE; // _LE, _BE, _PDP,... all the same for 8 bit output
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
149 g_inst.bps       = nch * rate * bits / 8;
150
151 roar_close();
152
153 if ( roar_vio_simple_new_stream_obj(&(g_inst.vio), &(g_inst.con), &(g_inst.stream),
154                                     rate, nch, bits, codec, ROAR_DIR_PLAY) == -1 ) {
155  roar_disconnect(&(g_inst.con));
156  g_inst.state |= STATE_CONNECTED;
157  g_inst.state -= STATE_CONNECTED;
158  if ( !(g_inst.state & STATE_NORECONNECT) ) {
159   g_inst.state |= STATE_NORECONNECT;
160   xmms_usleep(100000);
161   return roar_open(fmt, rate, nch);
162  } else {
163   g_inst.state -= STATE_NORECONNECT;
164   return FALSE;
165  }
166 }
167 g_inst.state |= STATE_PLAYING;
168
169 g_inst.written = 0;
170 g_inst.pause   = 0;
171
172 roar_update_metadata();
173 roar_set_volume(g_inst.mixer.l, g_inst.mixer.r);
174
175 return TRUE;
176}
177
178void roar_close(void) {
179 int was_playing = g_inst.state & STATE_PLAYING;
180
181 g_inst.state |= STATE_PLAYING;
182 g_inst.state -= STATE_PLAYING;
183
184 if ( was_playing ) {
185  roar_vio_close(&(g_inst.vio));
186  xmms_usleep(10000); // give the server 10ms of extra time
187 }
188
189 g_inst.written = 0;
190 ROAR_DBG("roar_close(void) = (void)");
191}
192
193void roar_pause(short p) {
194 g_inst.pause = p;
195}
196
197int roar_free(void) {
198 if ( g_inst.pause )
199  return 0;
200 else
201  return 1000000; // ???
202}
203
204void roar_flush(int time) {
205 gint64 r = time;
206
207 r *= g_inst.bps;
208 r /= 1000;
209
210 g_inst.written = r;
211}
212
213int roar_get_output_time(void) {
214 return roar_get_written_time();
215}
216
217int roar_get_written_time(void) {
218 gint64 r;
219
220 if ( !g_inst.bps ) {
221  ROAR_DBG("roar_get_written_time(void) = 0");
222  return 0;
223 }
224
225 r  = g_inst.written;
226 r *= 1000; // sec -> msec
227 r /= g_inst.bps;
228 ROAR_DBG("roar_get_written_time(void) = %lu", r);
229
230 return r;
231}
232
233
234// META DATA:
235
236int roar_update_metadata(void) {
237 struct roar_meta   meta;
238 char empty = 0;
239 char * info;
240 char * delm;
241 int pos;
242
243 pos     = xmms_remote_get_playlist_pos(g_inst.session);
244
245 meta.value = ∅
246 meta.key[0] = 0;
247 meta.type = ROAR_META_TYPE_NONE;
248
249 roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta);
250
251 info = xmms_remote_get_playlist_file(g_inst.session, pos);
252
253 if ( info ) {
254  if ( strncmp(info, "http:", 5) == 0 )
255   meta.type = ROAR_META_TYPE_FILEURL;
256  else
257   meta.type = ROAR_META_TYPE_FILENAME;
258
259  meta.value = info;
260  ROAR_DBG("roar_update_metadata(*): setting meta data: type=%i, strlen(value)=%i", meta.type, strlen(info));
261  roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
262
263  free(info);
264 }
265
266 info = xmms_remote_get_playlist_title(g_inst.session, pos);
267 if ( info ) {
268
269  if ( (delm = strstr(info, " - ")) != NULL ) {
270   *delm = 0;
271   meta.value  = info;
272   meta.type   = ROAR_META_TYPE_ARTIST;
273   roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
274
275   meta.value = delm + 3;
276  } else {
277   meta.value = info;
278  }
279
280  meta.type = ROAR_META_TYPE_TITLE;
281
282  roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta);
283
284  free(info);
285 }
286
287 meta.value = ∅
288 meta.type = ROAR_META_TYPE_NONE;
289 roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_FINALIZE, &meta);
290
291 return 0;
292}
293
294int roar_chk_metadata(void) {
295 static int    old_pos = -1;
296 static char * old_title = "NEW TITLE";
297 int pos;
298 char * title;
299 int need_update = 0;
300
301 pos     = xmms_remote_get_playlist_pos(g_inst.session);
302
303 if ( pos != old_pos ) {
304  old_pos = pos;
305  need_update = 1;
306 } else {
307  title = xmms_remote_get_playlist_title(g_inst.session, pos);
308
309  if ( strcmp(title, old_title) ) {
310   free(old_title);
311   old_title = title;
312   need_update = 1;
313  } else {
314   free(title);
315  }
316 }
317
318 if ( need_update ) {
319  roar_update_metadata();
320 }
321
322 return 0;
323}
324
325// MIXER:
326
327void roar_get_volume(int *l, int *r) {
328 int channels;
329 struct roar_mixer_settings mixer;
330 float fs;
331
332 if ( !(g_inst.state & STATE_CONNECTED) || !(g_inst.state & STATE_PLAYING) ) {
333  *l = g_inst.mixer.l;
334  *r = g_inst.mixer.r;
335  return;
336 }
337
338 if ( roar_get_vol(&(g_inst.con), g_inst.stream.id, &mixer, &channels) == -1 ) {
339  *l = *r = 100;
340  return;
341 }
342
343 fs = (float)mixer.scale/100.;
344
345 if ( channels == 1 ) {
346  *l = *r = mixer.mixer[0]/fs;
347 } else {
348  *l = mixer.mixer[0]/fs;
349  *r = mixer.mixer[1]/fs;
350 }
351}
352
353void roar_set_volume(int l, int r) {
354 struct roar_mixer_settings mixer;
355
356 if ( !(g_inst.state & STATE_CONNECTED) )
357  return;
358
359 if ( l == -1 ) l = 100;
360 if ( r == -1 ) r = 100;
361
362 g_inst.mixer.l = l;
363 g_inst.mixer.r = r;
364
365 if ( !(g_inst.state & STATE_PLAYING) )
366  return;
367
368 mixer.mixer[0] = l;
369 mixer.mixer[1] = r;
370 mixer.scale    = 100;
371
372 roar_set_vol(&(g_inst.con), g_inst.stream.id, &mixer, 2);
373}
374
375//ll
Note: See TracBrowser for help on using the repository browser.