source: roaraudio/plugins/xmms/roar.c @ 2331:c32fe06a0481

Last change on this file since 2331:c32fe06a0481 was 2331:c32fe06a0481, checked in by phi, 15 years ago

do simple /cleanmeta/

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