61
|
1 /* BMP - Cross-platform multimedia player
|
|
2 * Copyright (C) 2003-2004 BMP development team.
|
|
3 *
|
|
4 * Based on XMMS:
|
|
5 * Copyright (C) 1998-2003 XMMS development team.
|
|
6 *
|
|
7 * This program is free software; you can redistribute it and/or modify
|
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with this program; if not, write to the Free Software
|
|
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
20 */
|
|
21
|
|
22 #include <glib.h>
|
|
23 #include <stdlib.h>
|
|
24 #include <string.h>
|
|
25 #include <esd.h>
|
|
26
|
|
27 #include <unistd.h>
|
|
28
|
|
29 #include <libaudacious/util.h>
|
|
30
|
|
31 #include "esdout.h"
|
|
32
|
|
33
|
|
34 static gint fd = 0;
|
|
35 static gpointer buffer;
|
|
36 static gboolean going = FALSE, paused = FALSE, prebuffer, remove_prebuffer;
|
|
37 static gint buffer_size, prebuffer_size, blk_size = 4096;
|
|
38 static gint rd_index = 0, wr_index = 0;
|
|
39 static gint output_time_offset = 0;
|
|
40 static guint64 written = 0, output_bytes = 0;
|
|
41 static gint bps, ebps;
|
|
42 static gint flush;
|
|
43 static gint format, channels, frequency, latency;
|
|
44 static esd_format_t esd_format;
|
|
45 static gint input_bps, input_format, input_frequency, input_channels;
|
|
46 static GThread *buffer_thread;
|
|
47 static gboolean realtime = FALSE;
|
|
48 static void *(*esd_translate) (void *, gint);
|
|
49
|
|
50 static gint
|
|
51 get_latency(void)
|
|
52 {
|
|
53 int fd, amount = 0;
|
|
54
|
|
55 #ifndef HAVE_ESD_GET_LATENCY
|
|
56 esd_server_info_t *info;
|
|
57 #endif
|
|
58
|
|
59 fd = esd_open_sound(esd_cfg.hostname);
|
|
60
|
|
61 if (fd == -1)
|
|
62 return 0;
|
|
63
|
|
64 #ifdef HAVE_ESD_GET_LATENCY
|
|
65 amount = esd_get_latency(fd);
|
|
66 #else
|
|
67 info = esd_get_server_info(fd);
|
|
68 if (info) {
|
|
69 if (info->format & ESD_STEREO) {
|
|
70 if (info->format & ESD_BITS16)
|
|
71 amount = (44100 * (ESD_BUF_SIZE + 64)) / info->rate;
|
|
72 else
|
|
73 amount = (44100 * (ESD_BUF_SIZE + 128)) / info->rate;
|
|
74 }
|
|
75 else {
|
|
76 if (info->format & ESD_BITS16)
|
|
77 amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / info->rate;
|
|
78 else
|
|
79 amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / info->rate;
|
|
80 }
|
|
81 free(info);
|
|
82 }
|
|
83 amount += ESD_BUF_SIZE * 2;
|
|
84 #endif
|
|
85 esd_close(fd);
|
|
86 return amount;
|
|
87 }
|
|
88
|
|
89 static void *
|
|
90 esd_stou8(void *data, gint length)
|
|
91 {
|
|
92 int len = length;
|
|
93 unsigned char *dat = (unsigned char *) data;
|
|
94 while (len-- > 0)
|
|
95 *dat++ ^= 0x80;
|
|
96 return data;
|
|
97 }
|
|
98
|
|
99 static void *
|
|
100 esd_utos16sw(void *data, gint length)
|
|
101 {
|
|
102 int len = length;
|
|
103 short *dat = data;
|
|
104 while (len > 0) {
|
|
105 *dat = GUINT16_SWAP_LE_BE(*dat) ^ 0x8000;
|
|
106 dat++;
|
|
107 len -= 2;
|
|
108 }
|
|
109 return data;
|
|
110 }
|
|
111
|
|
112 static void *
|
|
113 esd_utos16(void *data, gint length)
|
|
114 {
|
|
115 int len = length;
|
|
116 short *dat = data;
|
|
117 while (len > 0) {
|
|
118 *dat ^= 0x8000;
|
|
119 dat++;
|
|
120 len -= 2;
|
|
121 }
|
|
122 return data;
|
|
123 }
|
|
124
|
|
125 static void *
|
|
126 esd_16sw(void *data, gint length)
|
|
127 {
|
|
128 int len = length;
|
|
129 short *dat = data;
|
|
130 while (len > 0) {
|
|
131 *dat = GUINT16_SWAP_LE_BE(*dat);
|
|
132 dat++;
|
|
133 len -= 2;
|
|
134 }
|
|
135 return data;
|
|
136 }
|
|
137
|
|
138 static void
|
|
139 esdout_setup_format(AFormat fmt, gint rate, gint nch)
|
|
140 {
|
|
141 gboolean swap_sign = FALSE;
|
|
142 gboolean swap_16 = FALSE;
|
|
143
|
|
144 format = fmt;
|
|
145 frequency = rate;
|
|
146 channels = nch;
|
|
147 switch (fmt) {
|
|
148 case FMT_S8:
|
|
149 swap_sign = TRUE;
|
|
150 case FMT_U8:
|
|
151 esd_format = ESD_BITS8;
|
|
152 break;
|
|
153 case FMT_U16_LE:
|
|
154 case FMT_U16_BE:
|
|
155 case FMT_U16_NE:
|
|
156 swap_sign = TRUE;
|
|
157 case FMT_S16_LE:
|
|
158 case FMT_S16_BE:
|
|
159 case FMT_S16_NE:
|
|
160 esd_format = ESD_BITS16;
|
|
161 break;
|
|
162 }
|
|
163
|
|
164 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
|
|
165 if (fmt == FMT_U16_LE || fmt == FMT_S16_LE)
|
|
166 #else
|
|
167 if (fmt == FMT_U16_BE || fmt == FMT_S16_BE)
|
|
168 #endif
|
|
169 swap_16 = TRUE;
|
|
170
|
|
171 esd_translate = (void *(*)()) NULL;
|
|
172 if (esd_format == ESD_BITS8) {
|
|
173 if (swap_sign == TRUE)
|
|
174 esd_translate = esd_stou8;
|
|
175 }
|
|
176 else {
|
|
177 if (swap_sign == TRUE) {
|
|
178 if (swap_16 == TRUE)
|
|
179 esd_translate = esd_utos16sw;
|
|
180 else
|
|
181 esd_translate = esd_utos16;
|
|
182 }
|
|
183 else {
|
|
184 if (swap_16 == TRUE)
|
|
185 esd_translate = esd_16sw;
|
|
186 }
|
|
187 }
|
|
188
|
|
189 bps = rate * nch;
|
|
190 if (esd_format == ESD_BITS16)
|
|
191 bps *= 2;
|
|
192 if (nch == 1)
|
|
193 esd_format |= ESD_MONO;
|
|
194 else
|
|
195 esd_format |= ESD_STEREO;
|
|
196 esd_format |= ESD_STREAM | ESD_PLAY;
|
|
197
|
|
198 latency = ((get_latency() * frequency) / 44100) * channels;
|
|
199 if (format != FMT_U8 && format != FMT_S8)
|
|
200 latency *= 2;
|
|
201 }
|
|
202
|
|
203
|
|
204 gint
|
|
205 esdout_get_written_time(void)
|
|
206 {
|
|
207 if (!going)
|
|
208 return 0;
|
|
209 return (gint) ((written * 1000) / input_bps);
|
|
210 }
|
|
211
|
|
212 gint
|
|
213 esdout_get_output_time(void)
|
|
214 {
|
|
215 guint64 bytes;
|
|
216
|
|
217 if (!fd || !going)
|
|
218 return 0;
|
|
219
|
|
220 bytes = output_bytes;
|
|
221 if (!paused)
|
|
222 bytes -= (bytes < latency ? bytes : latency);
|
|
223
|
|
224 return output_time_offset + (gint) ((bytes * 1000) / ebps);
|
|
225 }
|
|
226
|
|
227 gint
|
|
228 esdout_used(void)
|
|
229 {
|
|
230 if (realtime)
|
|
231 return 0;
|
|
232 else {
|
|
233 if (wr_index >= rd_index)
|
|
234 return wr_index - rd_index;
|
|
235 return buffer_size - (rd_index - wr_index);
|
|
236 }
|
|
237 }
|
|
238
|
|
239 gint
|
|
240 esdout_playing(void)
|
|
241 {
|
|
242 if (!going)
|
|
243 return FALSE;
|
|
244 if (!esdout_used())
|
|
245 return FALSE;
|
|
246
|
|
247 return TRUE;
|
|
248 }
|
|
249
|
|
250 gint
|
|
251 esdout_free(void)
|
|
252 {
|
|
253 if (!realtime) {
|
|
254 if (remove_prebuffer && prebuffer) {
|
|
255 prebuffer = FALSE;
|
|
256 remove_prebuffer = FALSE;
|
|
257 }
|
|
258 if (prebuffer)
|
|
259 remove_prebuffer = TRUE;
|
|
260
|
|
261 if (rd_index > wr_index)
|
|
262 return (rd_index - wr_index) - 1;
|
|
263 return (buffer_size - (wr_index - rd_index)) - 1;
|
|
264 }
|
|
265 else {
|
|
266 if (paused)
|
|
267 return 0;
|
|
268 else
|
|
269 return 1000000;
|
|
270 }
|
|
271 }
|
|
272
|
|
273 static void
|
|
274 esdout_write_audio(gpointer data, gint length)
|
|
275 {
|
|
276 AFormat new_format;
|
|
277 gint new_frequency, new_channels;
|
|
278 EffectPlugin *ep;
|
|
279
|
|
280 new_format = input_format;
|
|
281 new_frequency = input_frequency;
|
|
282 new_channels = input_channels;
|
|
283
|
|
284 ep = get_current_effect_plugin();
|
|
285 if (effects_enabled() && ep && ep->query_format) {
|
|
286 ep->query_format(&new_format, &new_frequency, &new_channels);
|
|
287 }
|
|
288
|
|
289 if (new_format != format || new_frequency != frequency
|
|
290 || new_channels != channels) {
|
|
291 output_time_offset += (gint) ((output_bytes * 1000) / ebps);
|
|
292 output_bytes = 0;
|
|
293 esdout_setup_format(new_format, new_frequency, new_channels);
|
|
294 frequency = new_frequency;
|
|
295 channels = new_channels;
|
|
296 esd_close(fd);
|
|
297 esdout_set_audio_params();
|
|
298 }
|
|
299 if (effects_enabled() && ep && ep->mod_samples)
|
|
300 length =
|
|
301 ep->mod_samples(&data, length, input_format, input_frequency,
|
|
302 input_channels);
|
|
303 if (esd_translate)
|
|
304 output_bytes += write(fd, esd_translate(data, length), length);
|
|
305 else
|
|
306 output_bytes += write(fd, data, length);
|
|
307 }
|
|
308
|
|
309
|
|
310 void
|
|
311 esdout_write(gpointer ptr, gint length)
|
|
312 {
|
|
313 gint cnt, off = 0;
|
|
314
|
|
315 if (!realtime) {
|
|
316 remove_prebuffer = FALSE;
|
|
317
|
|
318 written += length;
|
|
319 while (length > 0) {
|
|
320 cnt = MIN(length, buffer_size - wr_index);
|
|
321 memcpy((gchar *) buffer + wr_index, (gchar *) ptr + off, cnt);
|
|
322 wr_index = (wr_index + cnt) % buffer_size;
|
|
323 length -= cnt;
|
|
324 off += cnt;
|
|
325
|
|
326 }
|
|
327 }
|
|
328 else {
|
|
329 if (paused)
|
|
330 return;
|
|
331 esdout_write_audio(ptr, length);
|
|
332 written += length;
|
|
333
|
|
334 }
|
|
335
|
|
336 }
|
|
337
|
|
338 void
|
|
339 esdout_close(void)
|
|
340 {
|
|
341 if (!going)
|
|
342 return;
|
|
343
|
|
344 going = 0;
|
|
345
|
|
346 if (!realtime)
|
|
347 g_thread_join(buffer_thread);
|
|
348 else
|
|
349 esd_close(fd);
|
|
350
|
|
351 wr_index = 0;
|
|
352 rd_index = 0;
|
|
353 g_free(esd_cfg.playername);
|
|
354 esd_cfg.playername = NULL;
|
|
355 }
|
|
356
|
|
357 void
|
|
358 esdout_flush(gint time)
|
|
359 {
|
|
360 if (!realtime) {
|
|
361 flush = time;
|
|
362 while (flush != -1)
|
|
363 g_usleep(10000);
|
|
364 }
|
|
365 else {
|
|
366 output_time_offset = time;
|
|
367 written = (guint64) (time / 10) * (guint64) (input_bps / 100);
|
|
368 output_bytes = 0;
|
|
369 }
|
|
370 }
|
|
371
|
|
372 void
|
|
373 esdout_pause(short p)
|
|
374 {
|
|
375 paused = p;
|
|
376 }
|
|
377
|
|
378 gpointer
|
|
379 esdout_loop(gpointer arg)
|
|
380 {
|
|
381 gint length, cnt;
|
|
382
|
|
383
|
|
384 while (going) {
|
|
385 if (esdout_used() > prebuffer_size)
|
|
386 prebuffer = FALSE;
|
|
387 if (esdout_used() > 0 && !paused && !prebuffer) {
|
|
388 length = MIN(blk_size, esdout_used());
|
|
389 while (length > 0) {
|
|
390 cnt = MIN(length, buffer_size - rd_index);
|
|
391 esdout_write_audio((gchar *) buffer + rd_index, cnt);
|
|
392 rd_index = (rd_index + cnt) % buffer_size;
|
|
393 length -= cnt;
|
|
394 }
|
|
395 }
|
|
396 else
|
|
397 g_usleep(10000);
|
|
398
|
|
399 if (flush != -1) {
|
|
400 output_time_offset = flush;
|
|
401 written = (guint64) (flush / 10) * (guint64) (input_bps / 100);
|
|
402 rd_index = wr_index = output_bytes = 0;
|
|
403 flush = -1;
|
|
404 prebuffer = TRUE;
|
|
405 }
|
|
406
|
|
407 }
|
|
408
|
|
409 esd_close(fd);
|
|
410 g_free(buffer);
|
|
411 return NULL;
|
|
412 }
|
|
413
|
|
414 void
|
|
415 esdout_set_audio_params(void)
|
|
416 {
|
|
417 fd = esd_play_stream(esd_format, frequency,
|
|
418 esd_cfg.hostname, esd_cfg.playername);
|
|
419 /* Set the stream's mixer */
|
|
420 if (fd != -1)
|
|
421 esdout_mixer_init();
|
|
422 ebps = frequency * channels;
|
|
423 if (format == FMT_U16_BE || format == FMT_U16_LE ||
|
|
424 format == FMT_S16_BE || format == FMT_S16_LE ||
|
|
425 format == FMT_S16_NE || format == FMT_U16_NE)
|
|
426 ebps *= 2;
|
|
427 }
|
|
428
|
|
429 gint
|
|
430 esdout_open(AFormat fmt, gint rate, gint nch)
|
|
431 {
|
|
432 esdout_setup_format(fmt, rate, nch);
|
|
433
|
|
434 input_format = format;
|
|
435 input_channels = channels;
|
|
436 input_frequency = frequency;
|
|
437 input_bps = bps;
|
|
438
|
|
439 realtime = xmms_check_realtime_priority();
|
|
440
|
|
441 if (!realtime) {
|
|
442 buffer_size = (esd_cfg.buffer_size * input_bps) / 1000;
|
|
443 if (buffer_size < 8192)
|
|
444 buffer_size = 8192;
|
|
445 prebuffer_size = (buffer_size * esd_cfg.prebuffer) / 100;
|
|
446 if (buffer_size - prebuffer_size < 4096)
|
|
447 prebuffer_size = buffer_size - 4096;
|
|
448
|
|
449 buffer = g_malloc0(buffer_size);
|
|
450 }
|
|
451 flush = -1;
|
|
452 prebuffer = 1;
|
|
453 wr_index = rd_index = output_time_offset = written = output_bytes = 0;
|
|
454 paused = FALSE;
|
|
455 remove_prebuffer = FALSE;
|
|
456
|
|
457 esd_cfg.playername = g_strdup_printf("xmms - plugin (%d)", getpid());
|
|
458
|
|
459 if (esd_cfg.hostname)
|
|
460 g_free(esd_cfg.hostname);
|
|
461 if (esd_cfg.use_remote)
|
|
462 esd_cfg.hostname =
|
|
463 g_strdup_printf("%s:%d", esd_cfg.server, esd_cfg.port);
|
|
464 else
|
|
465 esd_cfg.hostname = NULL;
|
|
466
|
|
467 esdout_set_audio_params();
|
|
468 if (fd == -1) {
|
|
469 g_free(esd_cfg.playername);
|
|
470 esd_cfg.playername = NULL;
|
|
471 g_free(buffer);
|
|
472 return 0;
|
|
473 }
|
|
474 going = 1;
|
|
475
|
|
476 if (!realtime)
|
|
477 buffer_thread = g_thread_create(esdout_loop, NULL, TRUE, NULL);
|
|
478 return 1;
|
|
479 }
|