source: roaraudio/plugins/xmms/roar.c @ 720:a5c6f3514048

Last change on this file since 720:a5c6f3514048 was 720:a5c6f3514048, checked in by phi, 16 years ago

reset connection if we can not connect a new stream, maybe it helps

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