summaryrefslogtreecommitdiff
path: root/src/audio_out/audio_esd_out.c
blob: 259364a5537ed7571d27bc4fcb162951f30a0713 (plain)
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
/* 
 * Copyright (C) 2000 the xine project
 * 
 * This file is part of xine, a unix 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
 */

#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 <sys/time.h>
#include <inttypes.h>

#include "xine/xine.h"
#include "xine/monitor.h"
#include "xine/audio_out.h"
#include "resample.h"
#include "xine/metronom.h"
#include "xine/ac3.h"
#include "xine/utils.h"

#define GAP_TOLERANCE        15000
#define MAX_MASTER_CLOCK_DIV  5000

extern uint32_t xine_debug;

typedef struct _audio_esd_globals {

  int            audio_fd;

  int32_t        output_sample_rate, input_sample_rate;
  int32_t        output_rate_correction;
  double         sample_rate_factor;
  uint32_t       num_channels;

  uint32_t       bytes_in_buffer;      /* number of bytes writen to audio hardware   */
  uint32_t       last_vpts;            /* vpts at which last written package ends    */

  uint32_t       sync_vpts;            /* this syncpoint is used as a starting point */
  uint32_t       sync_bytes_in_buffer; /* for vpts <-> samplecount assoc             */

  int            audio_step;           /* pts per 32 768 samples (sample = #bytes/2) */
  int32_t        bytes_per_kpts;       /* bytes per 1024/90000 sec                   */

  int16_t       *zero_space;
  
  int            audio_started;

} audio_esd_globals_t;

static audio_esd_globals_t gAudioESD;

/*
 * open the audio device for writing to
 */
static int ao_open(uint32_t bits, uint32_t rate, int mode)
{
  esd_format_t format;

  printf ("audio_esd_out: ao_open rate=%d, mode=%d\n", rate, mode);

  if ((mode != AO_MODE_STEREO) && (mode != AO_MODE_MONO)) {
    printf ("ESD Driver only supports mono/stereo output modes at the moment\n");
    return -1;
  }

  if (gAudioESD.audio_fd > -1) {

    if (rate == gAudioESD.input_sample_rate)
      return 1;

    close (gAudioESD.audio_fd);
  }

  gAudioESD.input_sample_rate      = rate;
  gAudioESD.bytes_in_buffer        = 0;
  gAudioESD.last_vpts              = 0;
  gAudioESD.output_rate_correction = 0;
  gAudioESD.sync_vpts              = 0;
  gAudioESD.sync_bytes_in_buffer   = 0;
  gAudioESD.audio_started          = 0;

  /*
   * open stream to ESD server
   */

  format = ESD_STREAM | ESD_PLAY | ESD_BITS16;
  if (mode == AO_MODE_STEREO) {
    format |= ESD_STEREO;
    gAudioESD.num_channels = 2;
  } else {
    format |= ESD_MONO;
    gAudioESD.num_channels = 1;
  }
  gAudioESD.output_sample_rate = gAudioESD.input_sample_rate;
  if (gAudioESD.output_sample_rate > 44100)
    gAudioESD.output_sample_rate = 44100;

  gAudioESD.audio_fd=esd_play_stream(format, gAudioESD.output_sample_rate, NULL, NULL);
  if(gAudioESD.audio_fd < 0) {
    printf("audio_esd_out: Connecting to ESD server %s: %s\n",
	   getenv("ESPEAKER"), strerror(errno));
    return -1;
  }

  xprintf (VERBOSE|AUDIO, "audio_esd_out: %d channels\n",gAudioESD.num_channels);
  
  gAudioESD.sample_rate_factor = (double) gAudioESD.output_sample_rate / (double) gAudioESD.input_sample_rate;
  gAudioESD.audio_step         = (uint32_t) 90000 * (uint32_t) 32768 
                                 / gAudioESD.input_sample_rate;
  gAudioESD.bytes_per_kpts     = gAudioESD.output_sample_rate * gAudioESD.num_channels * 2 * 1024 / 90000;

  xprintf (VERBOSE|AUDIO, "audio_out : audio_step %d pts per 32768 samples\n", gAudioESD.audio_step);

  metronom_set_audio_rate (gAudioESD.audio_step);

  return 1;
}

static uint32_t ao_get_current_vpts (void) {

  int32_t  diff ;
  uint32_t vpts ;

  if (gAudioESD.audio_started)
    diff = 0;
  else
    diff = gAudioESD.sync_bytes_in_buffer;
  
  vpts = gAudioESD.sync_vpts - diff * 1024 / gAudioESD.bytes_per_kpts;

//  xprintf (AUDIO|VERBOSE,"audio_esd_out: get_current_vpts pos=%d diff=%d vpts=%d sync_vpts=%d\n",
//	   pos, diff, vpts, gAudioESD.sync_vpts);

  return vpts;
}

static void ao_fill_gap (uint32_t pts_len) {

  int num_bytes = pts_len * gAudioESD.bytes_per_kpts / 1024;
  
  num_bytes = (num_bytes / 4) * 4;

  printf ("audio_esd_out: inserting %d 0-bytes to fill a gap of %d pts\n",num_bytes, pts_len);
  
  gAudioESD.bytes_in_buffer += num_bytes;
  
  while (num_bytes>0) {
    if (num_bytes>8192) {
      write(gAudioESD.audio_fd, gAudioESD.zero_space, 8192);
      num_bytes -= 8192;
    } else {
      write(gAudioESD.audio_fd, gAudioESD.zero_space, num_bytes);
      num_bytes = 0;
    }
  }
  
  gAudioESD.last_vpts += pts_len;
}

static void ao_write_audio_data(int16_t* output_samples, uint32_t num_samples, 
				uint32_t pts_)
{

  uint32_t vpts,
           audio_vpts,
           master_vpts;
  int32_t  diff, gap;
  int      bDropPackage;
  uint16_t sample_buffer[8192];

  
  if (gAudioESD.audio_fd<0)
    return;

  vpts        = metronom_got_audio_samples (pts_, num_samples);

  xprintf (VERBOSE|AUDIO, "audio_esd_out: got %d samples, vpts=%d, last_vpts=%d\n",
	   num_samples, vpts, gAudioESD.last_vpts);

  /*
   * check if these samples "fit" in the audio output buffer
   * or do we have an audio "gap" here?
   */
  
  gap = vpts - gAudioESD.last_vpts ;
  
  /*
    printf ("audio_esd_out: gap = %d - %d + %d = %d\n",
    vpts, gAudioESD.last_vpts, diff, gap);
  */

  bDropPackage = 0;
  
  if (gap>GAP_TOLERANCE) {
    ao_fill_gap (gap);
  } else if (gap<-GAP_TOLERANCE) {
    bDropPackage = 1;
  }

  /*
   * sync on master clock
   */

  audio_vpts  = ao_get_current_vpts () ;
  master_vpts = metronom_get_current_time ();
  diff        = audio_vpts - master_vpts;

  xprintf (AUDIO|VERBOSE, "audio_esd_out: syncing on master clock: audio_vpts=%d master_vpts=%d\n",
	   audio_vpts, master_vpts);
  /*
  printf ("audio_esd_out: audio_vpts=%d <=> master_vpts=%d (diff=%d)\n",
	  audio_vpts, master_vpts, diff);
  */

  /*
   * adjust master clock
   */

  if (abs(diff)>MAX_MASTER_CLOCK_DIV) {
    printf ("master clock adjust time %d -> %d (diff: %d)\n", master_vpts, audio_vpts, diff); 
    metronom_adjust_clock (audio_vpts); 
  }

  /*
   * resample and output samples
   */

  if (!bDropPackage) {
    int num_output_samples = num_samples * (gAudioESD.output_sample_rate + gAudioESD.output_rate_correction) / gAudioESD.input_sample_rate;


    audio_out_resample_stereo (output_samples, num_samples,
			       sample_buffer, num_output_samples);
    
    write(gAudioESD.audio_fd, sample_buffer, num_output_samples * 2 * gAudioESD.num_channels);

    xprintf (AUDIO|VERBOSE, "audio_esd_out :audio package written\n");
    
    /*
     * remember vpts
     */
    
    gAudioESD.sync_vpts            = vpts;
    gAudioESD.sync_bytes_in_buffer = gAudioESD.bytes_in_buffer;

    /*
     * step values
     */
    
    gAudioESD.bytes_in_buffer += num_output_samples * 2 * gAudioESD.num_channels;
    gAudioESD.audio_started    = 1;
  } else {
    printf ("audio_esd_out: audio package (vpts = %d) dropped\n", vpts);
    gAudioESD.sync_vpts            = vpts;
  }
  
  gAudioESD.last_vpts        = vpts + num_samples * 90000 / gAudioESD.input_sample_rate ; 
}


static void ao_close(void)
{
  close(gAudioESD.audio_fd);
  gAudioESD.audio_fd = -1;
}

static int ao_is_mode_supported (int mode) {
  return ((mode == AO_MODE_STEREO) || (mode == AO_MODE_MONO));
}

static ao_functions_t audio_esdout = {
  ao_is_mode_supported,
  ao_open,
  ao_write_audio_data,
  ao_close,
};


ao_functions_t *audio_esdout_init (void) 
{
  int audio_fd;

  /*
   * open stream to ESD server
   */

  xprintf(VERBOSE|AUDIO, "Connecting to ESD server...");
  audio_fd = esd_open_sound(NULL);

  if(audio_fd < 0) 
  {
    char *server = getenv("ESPEAKER");

    // print a message so the user knows why ESD failed
    printf("Can't connect to %s ESD server: %s\n",
	   server ? server : "local", strerror(errno));

    return NULL;
  } // else
//    xprintf(VERBOSE|AUDIO, " %s\n", gAudioESD.audio_dev);

  close(audio_fd);

  gAudioESD.output_sample_rate = 0;
  gAudioESD.zero_space = xmalloc (8192);

  return &audio_esdout;
}