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