1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
/*
* Copyright (C) 2000-2002 the xine project
*
* This file is part of xine, a free video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: audio_esd_out.c,v 1.25 2002/12/27 17:49:17 jkeil Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <esd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <inttypes.h>
#include "xine_internal.h"
#include "xineutils.h"
#include "audio_out.h"
#include "metronom.h"
#define AO_OUT_ESD_IFACE_VERSION 7
#define REBLOCK 1 /* reblock output to ESD_BUF_SIZE blks */
#define GAP_TOLERANCE 5000
typedef struct esd_driver_s {
ao_driver_t ao_driver;
int audio_fd;
int capabilities;
int mode;
char *pname; /* Player name id for esd daemon */
int32_t output_sample_rate, input_sample_rate;
int32_t output_sample_k_rate;
double sample_rate_factor;
uint32_t num_channels;
uint32_t bytes_per_frame;
uint32_t bytes_in_buffer; /* number of bytes writen to esd */
int gap_tolerance, latency;
int server_sample_rate;
struct timeval start_time;
struct {
int source_id;
int volume;
int mute;
} mixer;
#if REBLOCK
/*
* Temporary sample buffer used to reblock the sample output stream
* to writes using buffer sizes of n*ESD_BUF_SIZE bytes.
*
* The reblocking avoids a bug with esd 0.2.18 servers and reduces
* cpu load with newer versions of the esd server.
*
* The esd 0.2.18 version zero fills "partial"/"incomplete" blocks.
* esd 0.2.28+ has fixed this problem, by performing a busy polling
* loop reading from a nonblocking socket to get the remainder of
* the partial block. This is wasting a lot of cpu cycles.
*/
char reblock_buf[ESD_BUF_SIZE];
int reblock_rem;
#endif
} esd_driver_t;
typedef struct {
audio_driver_class_t driver_class;
config_values_t *config;
} esd_class_t;
/*
* connect to esd
*/
static int ao_esd_open(ao_driver_t *this_gen,
uint32_t bits, uint32_t rate, int mode)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
esd_format_t format;
printf ("audio_esd_out: ao_open bits=%d rate=%d, mode=%d\n",
bits, rate, mode);
if ( (mode & this->capabilities) == 0 ) {
printf ("audio_esd_out: unsupported mode %08x\n", mode);
return 0;
}
if (this->audio_fd>=0) {
if ( (mode == this->mode) && (rate == this->input_sample_rate) )
return this->output_sample_rate;
esd_close (this->audio_fd);
}
this->mode = mode;
this->input_sample_rate = rate;
this->output_sample_rate = rate;
this->bytes_in_buffer = 0;
this->start_time.tv_sec = 0;
/*
* open stream to ESD server
*/
format = ESD_STREAM | ESD_PLAY | ESD_BITS16;
switch (mode) {
case AO_CAP_MODE_MONO:
format |= ESD_MONO;
this->num_channels = 1;
break;
case AO_CAP_MODE_STEREO:
format |= ESD_STEREO;
this->num_channels = 2;
break;
}
printf ("audio_esd_out: %d channels output\n",this->num_channels);
this->bytes_per_frame=(bits*this->num_channels)/8;
#if ESD_RESAMPLE
/* esd resamples (only for sample rates < the esd server's sample rate) */
if (this->output_sample_rate > this->server_sample_rate)
this->output_sample_rate = this->server_sample_rate;
#else
/* use xine's resample code */
this->output_sample_rate = this->server_sample_rate;
#endif
this->output_sample_k_rate = this->output_sample_rate / 1000;
this->audio_fd = esd_play_stream(format, this->output_sample_rate, NULL, this->pname);
if (this->audio_fd < 0) {
char *server = getenv("ESPEAKER");
printf("audio_esd_out: connecting to ESD server %s: %s\n",
server ? server : "<default>", strerror(errno));
return 0;
}
return this->output_sample_rate;
}
static int ao_esd_num_channels(ao_driver_t *this_gen)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
return this->num_channels;
}
static int ao_esd_bytes_per_frame(ao_driver_t *this_gen)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
return this->bytes_per_frame;
}
static int ao_esd_delay(ao_driver_t *this_gen)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
int bytes_left;
int frames;
struct timeval tv;
if (this->start_time.tv_sec == 0)
return 0;
gettimeofday(&tv, NULL);
frames = (tv.tv_usec - this->start_time.tv_usec)
* this->output_sample_k_rate / 1000;
frames += (tv.tv_sec - this->start_time.tv_sec)
* this->output_sample_rate;
frames -= this->latency;
if (frames < 0)
frames = 0;
/* calc delay */
bytes_left = this->bytes_in_buffer - frames * this->bytes_per_frame;
if (bytes_left<=0) /* buffer ran dry */
bytes_left = 0;
return bytes_left / this->bytes_per_frame;
}
static int ao_esd_write(ao_driver_t *this_gen,
int16_t* frame_buffer, uint32_t num_frames)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
int simulated_bytes_in_buffer, frames ;
struct timeval tv;
if (this->audio_fd<0)
return 1;
if (this->start_time.tv_sec == 0)
gettimeofday(&this->start_time, NULL);
/* check if simulated buffer ran dry */
gettimeofday(&tv, NULL);
frames = (tv.tv_usec - this->start_time.tv_usec)
* this->output_sample_k_rate / 1000;
frames += (tv.tv_sec - this->start_time.tv_sec)
* this->output_sample_rate;
frames -= this->latency;
if (frames < 0)
frames = 0;
/* calc delay */
simulated_bytes_in_buffer = frames * this->bytes_per_frame;
if (this->bytes_in_buffer < simulated_bytes_in_buffer)
this->bytes_in_buffer = simulated_bytes_in_buffer;
#if REBLOCK
{
struct iovec iov[2];
int iovcnt;
int num_bytes;
int nwritten;
int rem;
if (this->reblock_rem + num_frames*this->bytes_per_frame < ESD_BUF_SIZE) {
/*
* the stuff in the temporary reblocking buffer plus the new
* samples still do not give a complete ESD_BUF_SIZE block.
* just save the new samples in the reblocking buffer for later.
*/
memcpy(this->reblock_buf + this->reblock_rem,
frame_buffer,
num_frames * this->bytes_per_frame);
this->reblock_rem += num_frames * this->bytes_per_frame;
return 1;
}
/* OK, we have at least one complete ESD_BUF_SIZE block */
iovcnt = 0;
num_bytes = 0;
if (this->reblock_rem > 0) {
/* send any saved samples from the reblocking buffer first */
iov[iovcnt].iov_base = this->reblock_buf;
iov[iovcnt].iov_len = this->reblock_rem;
iovcnt++;
num_bytes += this->reblock_rem;
this->reblock_rem = 0;
}
rem = (num_bytes + num_frames * this->bytes_per_frame) % ESD_BUF_SIZE;
if (num_frames * this->bytes_per_frame > rem) {
/*
* add samples from caller, so that the total number of bytes is
* a multiple of ESD_BUF_SIZE
*/
iov[iovcnt].iov_base = frame_buffer;
iov[iovcnt].iov_len = num_frames * this->bytes_per_frame - rem;
num_bytes += num_frames * this->bytes_per_frame - rem;
iovcnt++;
}
nwritten = writev(this->audio_fd, iov, iovcnt);
if (nwritten != num_bytes) {
if (nwritten < 0)
printf("audio_esd_out: writev failed: %s\n", strerror(errno));
else
printf("audio_esd_out: warning, incomplete write: %d\n", nwritten);
}
if (nwritten > 0)
this->bytes_in_buffer += nwritten;
if (rem > 0) {
/* save the remaining bytes for the next ao_esd_write() */
memcpy(this->reblock_buf,
(char*)frame_buffer + iov[iovcnt-1].iov_len, rem);
this->reblock_rem = rem;
}
}
#else
this->bytes_in_buffer += num_frames * this->bytes_per_frame;
write(this->audio_fd, frame_buffer, num_frames * this->bytes_per_frame);
#endif
return 1;
}
static void ao_esd_close(ao_driver_t *this_gen)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
esd_close(this->audio_fd);
this->audio_fd = -1;
}
static uint32_t ao_esd_get_capabilities (ao_driver_t *this_gen) {
esd_driver_t *this = (esd_driver_t *) this_gen;
return this->capabilities;
}
static int ao_esd_get_gap_tolerance (ao_driver_t *this_gen) {
/* esd_driver_t *this = (esd_driver_t *) this_gen; */
return GAP_TOLERANCE;
}
static void ao_esd_exit(ao_driver_t *this_gen)
{
esd_driver_t *this = (esd_driver_t *) this_gen;
if (this->audio_fd != -1)
esd_close(this->audio_fd);
free(this->pname);
free (this);
}
static int ao_esd_get_property (ao_driver_t *this_gen, int property) {
esd_driver_t *this = (esd_driver_t *) this_gen;
int mixer_fd;
esd_player_info_t *esd_pi;
esd_info_t *esd_i;
switch(property) {
case AO_PROP_MIXER_VOL:
if((mixer_fd = esd_open_sound(NULL)) >= 0) {
if((esd_i = esd_get_all_info(mixer_fd)) != NULL) {
for(esd_pi = esd_i->player_list; esd_pi != NULL; esd_pi = esd_pi->next) {
if(!strcmp(this->pname, esd_pi->name)) {
this->mixer.source_id = esd_pi->source_id;
if(!this->mixer.mute)
this->mixer.volume = (((esd_pi->left_vol_scale * 100) / 256) +
((esd_pi->right_vol_scale * 100) / 256)) >> 1;
}
}
esd_free_all_info(esd_i);
}
esd_close(mixer_fd);
}
return this->mixer.volume;
break;
case AO_PROP_MUTE_VOL:
return this->mixer.mute;
break;
}
return 0;
}
static int ao_esd_set_property (ao_driver_t *this_gen, int property, int value) {
esd_driver_t *this = (esd_driver_t *) this_gen;
int mixer_fd;
switch(property) {
case AO_PROP_MIXER_VOL:
if(!this->mixer.mute) {
/* need this to get source_id */
(void) ao_esd_get_property(&this->ao_driver, AO_PROP_MIXER_VOL);
if((mixer_fd = esd_open_sound(NULL)) >= 0) {
int v = (value * 256) / 100;
esd_set_stream_pan(mixer_fd, this->mixer.source_id, v, v);
if(!this->mixer.mute)
this->mixer.volume = value;
esd_close(mixer_fd);
}
}
else
this->mixer.volume = value;
return this->mixer.volume;
break;
case AO_PROP_MUTE_VOL: {
int mute = (value) ? 1 : 0;
/* need this to get source_id */
(void) ao_esd_get_property(&this->ao_driver, AO_PROP_MIXER_VOL);
if(mute) {
if((mixer_fd = esd_open_sound(NULL)) >= 0) {
int v = 0;
esd_set_stream_pan(mixer_fd, this->mixer.source_id, v, v);
esd_close(mixer_fd);
}
}
else {
if((mixer_fd = esd_open_sound(NULL)) >= 0) {
int v = (this->mixer.volume * 256) / 100;
esd_set_stream_pan(mixer_fd, this->mixer.source_id, v, v);
esd_close(mixer_fd);
}
}
this->mixer.mute = mute;
return value;
}
break;
}
return ~value;
}
static int ao_esd_ctrl(ao_driver_t *this_gen, int cmd, ...) {
/* esd_driver_t *this = (esd_driver_t *) this_gen; */
switch (cmd) {
case AO_CTRL_PLAY_PAUSE:
break;
case AO_CTRL_PLAY_RESUME:
break;
case AO_CTRL_FLUSH_BUFFERS:
break;
}
return 0;
}
static ao_driver_t *open_plugin (audio_driver_class_t *class_gen,
const void *data) {
esd_class_t *class = (esd_class_t *) class_gen;
config_values_t *config = class->config;
esd_driver_t *this;
int audio_fd;
int err;
esd_server_info_t *esd_svinfo;
int server_sample_rate;
sigset_t vo_mask, vo_mask_orig;
/*
* open stream to ESD server
*
* esd_open_sound needs a working SIGALRM for detecting a failed
* attempt to autostart the esd daemon; esd notifies the process that
* attempts the esd daemon autostart with a SIGALRM (SIGUSR1) signal
* about a failure to open the audio device (successful daemon startup).
*
* Temporarily release the blocked SIGALRM, while esd_open_sound is active.
* (Otherwise xine hangs in esd_open_sound on a machine without sound)
*/
sigemptyset(&vo_mask);
sigaddset(&vo_mask, SIGALRM);
if (sigprocmask(SIG_UNBLOCK, &vo_mask, &vo_mask_orig))
printf("audio_esd_out: cannot unblock SIGALRM: %s\n", strerror(errno));
printf("audio_esd_out: connecting to esd server...\n");
audio_fd = esd_open_sound(NULL);
err = errno;
if (sigprocmask(SIG_SETMASK, &vo_mask_orig, NULL))
printf("audio_esd_out: cannot block SIGALRM: %s\n", strerror(errno));
if(audio_fd < 0) {
char *server = getenv("ESPEAKER");
/* print a message so the user knows why ESD failed */
printf("audio_esd_out: can't connect to %s ESD server: %s\n",
server ? server : "<default>", strerror(err));
return NULL;
}
esd_svinfo = esd_get_server_info(audio_fd);
if (esd_svinfo) {
server_sample_rate = esd_svinfo->rate;
esd_free_server_info(esd_svinfo);
} else
server_sample_rate = 44100;
esd_close(audio_fd);
this = (esd_driver_t *) xine_xmalloc (sizeof (esd_driver_t));
this->pname = strdup("xine esd audio output plugin");
this->output_sample_rate = 0;
this->server_sample_rate = server_sample_rate;
this->audio_fd = -1;
this->capabilities = AO_CAP_MODE_MONO | AO_CAP_MODE_STEREO | AO_CAP_MIXER_VOL | AO_CAP_MUTE_VOL;
this->latency = config->register_range (config, "audio.esd_latency", 0,
-30000, 90000,
_("esd audio output latency (adjust a/v sync)"),
NULL, 0, NULL, NULL);
this->ao_driver.get_capabilities = ao_esd_get_capabilities;
this->ao_driver.get_property = ao_esd_get_property;
this->ao_driver.set_property = ao_esd_set_property;
this->ao_driver.open = ao_esd_open;
this->ao_driver.num_channels = ao_esd_num_channels;
this->ao_driver.bytes_per_frame = ao_esd_bytes_per_frame;
this->ao_driver.get_gap_tolerance = ao_esd_get_gap_tolerance;
this->ao_driver.delay = ao_esd_delay;
this->ao_driver.write = ao_esd_write;
this->ao_driver.close = ao_esd_close;
this->ao_driver.exit = ao_esd_exit;
this->ao_driver.control = ao_esd_ctrl;
return this;
}
/*
* class functions
*/
static char* get_identifier (audio_driver_class_t *this_gen) {
return "esd";
}
static char* get_description (audio_driver_class_t *this_gen) {
return _("xine audio output plugin using esound");
}
static void dispose_class (audio_driver_class_t *this_gen) {
esd_class_t *this = (esd_class_t *) this_gen;
free (this);
}
static void *init_class (xine_t *xine, void *data) {
esd_class_t *this;
this = (esd_class_t *) malloc (sizeof (esd_class_t));
this->driver_class.open_plugin = open_plugin;
this->driver_class.get_identifier = get_identifier;
this->driver_class.get_description = get_description;
this->driver_class.dispose = dispose_class;
this->config = xine->config;
return this;
}
static ao_info_t ao_info_esd = {
4
};
/*
* exported plugin catalog entry
*/
plugin_info_t xine_plugin_info[] = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_AUDIO_OUT, AO_OUT_ESD_IFACE_VERSION, "esd", XINE_VERSION_CODE, &ao_info_esd, init_class },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};
|