Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 35091 | Differences between
and this patch

Collapse All | Expand All

(-)vanilla/main/configure (+13 lines)
Lines 3901-3909 Link Here
3901
int main(void) { return (!(SND_LIB_MAJOR==0 && SND_LIB_MINOR==9)); }
3901
int main(void) { return (!(SND_LIB_MAJOR==0 && SND_LIB_MINOR==9)); }
3902
EOF
3902
EOF
3903
  cc_check -lasound $_ld_dl $_ld_pthread && $TMPO && _alsaver='0.9.x-alsa'
3903
  cc_check -lasound $_ld_dl $_ld_pthread && $TMPO && _alsaver='0.9.x-alsa'
3904
  cat > $TMPC << EOF
3905
#include <alsa/asoundlib.h>
3906
int main(void) { return (!(SND_LIB_MAJOR==1)); }
3907
EOF
3908
  cc_check -lasound $_ld_dl $_ld_pthread && $TMPO && _alsaver='1.x-alsa'
3904
fi
3909
fi
3905
_def_alsa5='#undef HAVE_ALSA5'
3910
_def_alsa5='#undef HAVE_ALSA5'
3906
_def_alsa9='#undef HAVE_ALSA9'
3911
_def_alsa9='#undef HAVE_ALSA9'
3912
_def_alsa1x='#undef HAVE_ALSA1X'
3907
_def_sys_asoundlib_h='#undef HAVE_SYS_ASOUNDLIB_H'
3913
_def_sys_asoundlib_h='#undef HAVE_SYS_ASOUNDLIB_H'
3908
_def_alsa_asoundlib_h='#undef HAVE_ALSA_ASOUNDLIB_H'
3914
_def_alsa_asoundlib_h='#undef HAVE_ALSA_ASOUNDLIB_H'
3909
if test "$_alsaver" ; then
3915
if test "$_alsaver" ; then
Lines 3925-3930 Link Here
3925
    _def_alsa9='#define HAVE_ALSA9 1'
3931
    _def_alsa9='#define HAVE_ALSA9 1'
3926
    _def_alsa_asoundlib_h='#define HAVE_ALSA_ASOUNDLIB_H 1'
3932
    _def_alsa_asoundlib_h='#define HAVE_ALSA_ASOUNDLIB_H 1'
3927
    echores "yes (using alsa 0.9.x and alsa/asoundlib.h)"
3933
    echores "yes (using alsa 0.9.x and alsa/asoundlib.h)"
3934
  elif test "$_alsaver" = '1.x-alsa' ; then
3935
    _aosrc="$_aosrc ao_alsa1x.c"
3936
    _aomodules="alsa1x $_aomodules"
3937
    _def_alsa1x='#define HAVE_ALSA1X 1'
3938
    _def_alsa_asoundlib_h='#define HAVE_ALSA_ASOUNDLIB_H 1'
3939
    echores "yes (using alsa 1.x and alsa/asoundlib.h)"
3928
  fi
3940
  fi
3929
  _ld_alsa="-lasound $_ld_dl $_ld_pthread"
3941
  _ld_alsa="-lasound $_ld_dl $_ld_pthread"
3930
else
3942
else
Lines 6190-6195 Link Here
6190
$_def_ossaudio_devmixer
6202
$_def_ossaudio_devmixer
6191
$_def_alsa5
6203
$_def_alsa5
6192
$_def_alsa9
6204
$_def_alsa9
6205
$_def_alsa1x
6193
$_def_arts
6206
$_def_arts
6194
$_def_esd
6207
$_def_esd
6195
$_def_esd_latency
6208
$_def_esd_latency
(-)vanilla/main/libao2/ao_alsa1x.c (+1173 lines)
Line 0 Link Here
1
/*
2
  ao_alsa1x - ALSA-1.x output plugin for MPlayer
3
4
  (C) Alex Beregszaszi
5
  
6
  modified for real alsa-0.9.0-support by Joy Winter <joy at pingfm.org>
7
  additional AC3 passthrough support by Andy Lo A Foe <andy at alsaplayer.org>  
8
  08/22/2002 iec958-init rewritten and merged with common init, joy
9
  
10
  Trivial port to ALSA 1.x API by Jindrich Makovicka
11
  
12
  Any bugreports regarding to this driver are welcome.
13
*/
14
15
#include <errno.h>
16
#include <sys/time.h>
17
#include <stdlib.h>
18
#include <math.h>
19
#include <string.h>
20
#include <sys/poll.h>
21
22
#include "../config.h"
23
24
#if HAVE_SYS_ASOUNDLIB_H
25
#include <sys/asoundlib.h>
26
#elif HAVE_ALSA_ASOUNDLIB_H
27
#include <alsa/asoundlib.h>
28
#else
29
#error "asoundlib.h is not in sys/ or alsa/ - please bugreport"
30
#endif
31
32
#include "audio_out.h"
33
#include "audio_out_internal.h"
34
#include "afmt.h"
35
36
extern int verbose;
37
38
static ao_info_t info = 
39
{
40
    "ALSA-1.x audio output",
41
    "alsa1x",
42
    "Alex Beregszaszi, Joy Winter <joy at pingfm.org>",
43
    "under developement"
44
};
45
46
LIBAO_EXTERN(alsa1x)
47
48
49
static snd_pcm_t *alsa_handler;
50
static snd_pcm_format_t alsa_format;
51
static snd_pcm_hw_params_t *alsa_hwparams;
52
static snd_pcm_sw_params_t *alsa_swparams;
53
static char *alsa_device;
54
55
/* possible 4096, original 8192 
56
 * was only needed for calculating chunksize? */
57
static int alsa_fragsize = 4096;
58
/* 16 sets buffersize to 16 * chunksize is as default 1024
59
 * which seems to be good avarge for most situations 
60
 * so buffersize is 16384 frames by default */
61
static int alsa_fragcount = 16;
62
static int chunk_size = 1024; //is alsa_fragsize / 4
63
64
#define MIN_CHUNK_SIZE 1024
65
66
static size_t bits_per_sample, bytes_per_sample, bits_per_frame;
67
static size_t chunk_bytes;
68
69
int ao_mmap = 0;
70
int ao_noblock = 0;
71
int first = 1;
72
73
static int open_mode;
74
static int set_block_mode;
75
static int alsa_can_pause = 0;
76
77
#define ALSA_DEVICE_SIZE 48
78
79
#undef BUFFERTIME
80
#define SET_CHUNKSIZE
81
#undef USE_POLL
82
83
84
/* to set/get/query special features/parameters */
85
static int control(int cmd, void *arg)
86
{
87
  switch(cmd) {
88
  case AOCONTROL_QUERY_FORMAT:
89
    return CONTROL_TRUE;
90
#ifndef WORDS_BIGENDIAN 
91
  case AOCONTROL_GET_VOLUME:
92
  case AOCONTROL_SET_VOLUME:
93
    {
94
      ao_control_vol_t *vol = (ao_control_vol_t *)arg;
95
96
      int err;
97
      snd_mixer_t *handle;
98
      snd_mixer_elem_t *elem;
99
      snd_mixer_selem_id_t *sid;
100
101
      const char *mix_name = "PCM";
102
      char *card = "default";
103
104
      long pmin, pmax;
105
      long get_vol, set_vol;
106
      float calc_vol, diff, f_multi;
107
108
      if(ao_data.format == AFMT_AC3)
109
	return CONTROL_TRUE;
110
111
      //allocate simple id
112
      snd_mixer_selem_id_alloca(&sid);
113
	
114
      //sets simple-mixer index and name
115
      snd_mixer_selem_id_set_index(sid, 0);
116
      snd_mixer_selem_id_set_name(sid, mix_name);
117
118
      if ((err = snd_mixer_open(&handle, 0)) < 0) {
119
	printf("alsa-control: mixer open error: %s\n", snd_strerror(err));
120
	return CONTROL_ERROR;
121
      }
122
123
      if ((err = snd_mixer_attach(handle, card)) < 0) {
124
	printf("alsa-control: mixer attach %s error: %s", card, snd_strerror(err));
125
	snd_mixer_close(handle);
126
	return CONTROL_ERROR;
127
      }
128
129
      if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
130
	printf("alsa-control: mixer register error: %s", snd_strerror(err));
131
	snd_mixer_close(handle);
132
	return CONTROL_ERROR;
133
      }
134
      err = snd_mixer_load(handle);
135
      if (err < 0) {
136
	printf("alsa-control: mixer load error: %s", snd_strerror(err));
137
	snd_mixer_close(handle);
138
	return CONTROL_ERROR;
139
      }
140
141
      elem = snd_mixer_find_selem(handle, sid);
142
      if (!elem) {
143
	printf("alsa-control: unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
144
	snd_mixer_close(handle);
145
	return CONTROL_ERROR;
146
	}
147
148
      snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
149
      f_multi = (100 / (float)pmax);
150
151
      if (cmd == AOCONTROL_SET_VOLUME) {
152
153
	diff = (vol->left+vol->right) / 2;
154
	set_vol = rint(diff / f_multi);
155
	
156
	if (set_vol < 0)
157
	  set_vol = 0;
158
	else if (set_vol > pmax)
159
	  set_vol = pmax;
160
161
	//setting channels
162
	if ((err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol)) < 0) {
163
	  printf("alsa-control: error setting left channel, %s",snd_strerror(err));
164
	  return CONTROL_ERROR;
165
	}
166
	if ((err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol)) < 0) {
167
	  printf("alsa-control: error setting right channel, %s",snd_strerror(err));
168
	  return CONTROL_ERROR;
169
	}
170
171
	//printf("diff=%f, set_vol=%i, pmax=%i, mult=%f\n", diff, set_vol, pmax, f_multi);
172
      }
173
      else {
174
	snd_mixer_selem_get_playback_volume(elem, 0, &get_vol);
175
	calc_vol = get_vol;
176
	calc_vol = rintf(calc_vol * f_multi);
177
178
	vol->left = vol->right = (int)calc_vol;
179
180
	//printf("get_vol = %i, calc=%i\n",get_vol, calc_vol);
181
      }
182
      snd_mixer_close(handle);
183
      return CONTROL_OK;
184
    }
185
#endif
186
    
187
  } //end switch
188
  return(CONTROL_UNKNOWN);
189
}
190
191
192
/*
193
    open & setup audio device
194
    return: 1=success 0=fail
195
*/
196
static int init(int rate_hz, int channels, int format, int flags)
197
{
198
    int err;
199
    int cards = -1;
200
    int period_val;
201
    snd_pcm_uframes_t bufsize;
202
    snd_pcm_info_t *alsa_info;
203
    char *str_block_mode;
204
    int device_set = 0;
205
206
    printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz,
207
	channels, audio_out_format_name(format));
208
209
    alsa_handler = NULL;
210
211
    if (verbose>0)
212
	printf("alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
213
214
    if ((err = snd_card_next(&cards)) < 0 || cards < 0)
215
    {
216
	printf("alsa-init: no soundcards found: %s\n", snd_strerror(err));
217
	return(0);
218
    }
219
220
    ao_data.samplerate = rate_hz;
221
    ao_data.bps = channels * rate_hz;
222
    ao_data.format = format;
223
    ao_data.channels = channels;
224
    ao_data.outburst = OUTBURST;
225
    //ao_data.buffersize = MAX_OUTBURST; // was 16384
226
227
    switch (format)
228
      {
229
      case AFMT_S8:
230
	alsa_format = SND_PCM_FORMAT_S8;
231
	break;
232
      case AFMT_U8:
233
	alsa_format = SND_PCM_FORMAT_U8;
234
	break;
235
      case AFMT_U16_LE:
236
	alsa_format = SND_PCM_FORMAT_U16_LE;
237
	break;
238
      case AFMT_U16_BE:
239
	alsa_format = SND_PCM_FORMAT_U16_BE;
240
	break;
241
#ifndef WORDS_BIGENDIAN
242
      case AFMT_AC3:
243
#endif
244
      case AFMT_S16_LE:
245
	alsa_format = SND_PCM_FORMAT_S16_LE;
246
	break;
247
#ifdef WORDS_BIGENDIAN
248
      case AFMT_AC3:
249
#endif
250
      case AFMT_S16_BE:
251
	alsa_format = SND_PCM_FORMAT_S16_BE;
252
	break;
253
      case AFMT_S32_LE:
254
	alsa_format = SND_PCM_FORMAT_S32_LE;
255
	break;
256
      case AFMT_S32_BE:
257
	alsa_format = SND_PCM_FORMAT_S32_BE;
258
	break;
259
260
      default:
261
	alsa_format = SND_PCM_FORMAT_MPEG;
262
	break;
263
      }
264
    
265
    switch(alsa_format)
266
      {
267
      case SND_PCM_FORMAT_S16_LE:
268
      case SND_PCM_FORMAT_U16_LE:
269
	ao_data.bps *= 2;
270
	break;
271
      case SND_PCM_FORMAT_S32_LE:
272
      case SND_PCM_FORMAT_S32_BE:
273
	ao_data.bps *= 4;
274
	break;
275
      case -1:
276
	printf("alsa-init: invalid format (%s) requested - output disabled\n",
277
	       audio_out_format_name(format));
278
	return(0);
279
      default:
280
	break;	    
281
      }
282
283
    if (ao_subdevice) {
284
      //start parsing ao_subdevice, ugly and not thread safe!
285
      //maybe there's a better way?
286
      int i2 = 1;
287
      int i3 = 0;
288
      char *sub_str;
289
290
      char *token_str[3];
291
      char* test_str = strdup(ao_subdevice);
292
293
294
      if ((strcspn(ao_subdevice, ":")) > 0) {
295
296
	sub_str = strtok(test_str, ":");
297
	*(token_str) = sub_str;
298
299
	while (((sub_str = strtok(NULL, ":")) != NULL) && (i2 <= 3)) {
300
	  *(token_str+i2) = sub_str;
301
	  i2 += 1;
302
	}
303
304
	for (i3=0; i3 <= i2-1; i3++) {
305
	  if (strcmp(*(token_str + i3), "mmap") == 0) {
306
	    ao_mmap = 1;
307
	  }
308
	  else if (strcmp(*(token_str+i3), "noblock") == 0) {
309
	    ao_noblock = 1;
310
	  }
311
	  else if (strcmp(*(token_str+i3), "hw") == 0) {
312
	    if ((i3 < i2-1) && (strcmp(*(token_str+i3+1), "noblock") != 0) && (strcmp(*(token_str+i3+1), "mmap") != 0)) {
313
              char *tmp;
314
315
	      alsa_device = alloca(ALSA_DEVICE_SIZE);
316
	      snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%s", *(token_str+(i3+1)));
317
	      if ((tmp = strrchr(alsa_device, '.')) && isdigit(*(tmp+1)))
318
                *tmp = ',';
319
	      device_set = 1;
320
	    }
321
		else {
322
		  alsa_device = *(token_str+i3);
323
		  device_set = 1;
324
		}
325
	  }
326
	  else if (device_set == 0 && (!ao_mmap || !ao_noblock)) {
327
	    alsa_device = *(token_str+i3);
328
	    device_set = 1;
329
	  }
330
	}
331
      }
332
    } else { //end parsing ao_subdevice
333
        /* in any case for multichannel playback we should select
334
         * appropriate device
335
         */
336
        char devstr[128];
337
338
        switch (channels) {
339
          case 4:
340
            strcpy(devstr, "surround40");
341
            alsa_device = devstr;
342
           break;
343
          case 6:
344
            strcpy(devstr, "surround51");
345
            alsa_device = devstr;
346
           break;
347
          default:
348
	   break;
349
        }
350
    }
351
352
353
    /* switch for spdif
354
     * sets opening sequence for SPDIF
355
     * sets also the playback and other switches 'on the fly'
356
     * while opening the abstract alias for the spdif subdevice
357
     * 'iec958'
358
     */
359
    if (format == AFMT_AC3) {
360
      char devstr[128];
361
      unsigned char s[4];
362
      int err, c;
363
364
      switch (channels) {
365
      case 1:
366
      case 2:
367
368
	s[0] = IEC958_AES0_NONAUDIO | 
369
	  IEC958_AES0_CON_EMPHASIS_NONE;
370
	s[1] = IEC958_AES1_CON_ORIGINAL | 
371
	  IEC958_AES1_CON_PCM_CODER;
372
	s[2] = 0;
373
	s[3] = IEC958_AES3_CON_FS_48000;
374
375
	sprintf(devstr, "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x", 
376
		s[0], s[1], s[2], s[3]);
377
378
	if (verbose>0)
379
	  printf("alsa-spdif-init: playing AC3, %i channels\n", channels);
380
	break;
381
      case 4:
382
	strcpy(devstr, "surround40");
383
	break;
384
    
385
      case 6:
386
	strcpy(devstr, "surround51");
387
	break;
388
389
      default:
390
	fprintf(stderr, "%d channels are not supported\n", channels);
391
	return(0);
392
      }
393
394
      alsa_device = devstr;
395
    }
396
397
    if (alsa_device == NULL)
398
      {
399
	int tmp_device, tmp_subdevice, err;
400
401
	if ((err = snd_pcm_info_malloc(&alsa_info)) < 0)
402
	  {
403
	    printf("alsa-init: memory allocation error: %s\n", snd_strerror(err));
404
	    return(0);
405
	  }
406
	
407
	if ((alsa_device = alloca(ALSA_DEVICE_SIZE)) == NULL)
408
	  {
409
	    printf("alsa-init: memory allocation error: %s\n", strerror(errno));
410
	    return(0);
411
	  }
412
413
	if ((tmp_device = snd_pcm_info_get_device(alsa_info)) < 0)
414
	  {
415
	    printf("alsa-init: can't get device\n");
416
	    return(0);
417
	  }
418
419
	if ((tmp_subdevice = snd_pcm_info_get_subdevice(alsa_info)) < 0)
420
	  {
421
	    printf("alsa-init: can't get subdevice\n");
422
	    return(0);
423
	  }
424
	
425
	if (verbose>0)
426
	  printf("alsa-init: got device=%i, subdevice=%i\n", tmp_device, tmp_subdevice);
427
428
	if ((err = snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%1d,%1d", tmp_device, tmp_subdevice)) <= 0)
429
	  {
430
	    printf("alsa-init: can't write device-id\n");
431
	  }
432
433
	snd_pcm_info_free(alsa_info);
434
	printf("alsa-init: %d soundcard%s found, using: %s\n", cards+1,
435
	       (cards >= 0) ? "" : "s", alsa_device);
436
      } else if (strcmp(alsa_device, "help") == 0) {
437
	printf("alsa-help: available options are:\n");
438
	printf("           mmap: sets mmap-mode\n");
439
	printf("           noblock: sets noblock-mode\n");
440
	printf("           device-name: sets device name (change comma to point)\n");
441
	printf("           example -ao alsa1x:mmap:noblock:hw:0.3 sets noblock-mode,\n");
442
	printf("           mmap-mode and the device-name as first card fourth device\n");
443
	return(0);
444
      } else {
445
		printf("alsa-init: soundcard set to %s\n", alsa_device);
446
      }
447
448
    //setting modes for block or nonblock-mode
449
    if (ao_noblock) {
450
      open_mode = SND_PCM_NONBLOCK;
451
      set_block_mode = 1;
452
      str_block_mode = "nonblock-mode";
453
    }
454
    else {
455
      open_mode = 0;
456
      set_block_mode = 0;
457
      str_block_mode = "block-mode";
458
    }
459
460
    //sets buff/chunksize if its set manually
461
    if (ao_data.buffersize) {
462
      switch (ao_data.buffersize)
463
	{
464
	case 1:
465
	  alsa_fragcount = 16;
466
	  chunk_size = 512;
467
	  if (verbose>0) {
468
	    printf("alsa-init: buffersize set manually to 8192\n");
469
	    printf("alsa-init: chunksize set manually to 512\n");
470
	  }
471
	  break;
472
	case 2:
473
	  alsa_fragcount = 8;
474
	  chunk_size = 1024;
475
	  if (verbose>0) {
476
	    printf("alsa-init: buffersize set manually to 8192\n");
477
	    printf("alsa-init: chunksize set manually to 1024\n");
478
	  }
479
	  break;
480
	case 3:
481
	  alsa_fragcount = 32;
482
	  chunk_size = 512;
483
	  if (verbose>0) {
484
	    printf("alsa-init: buffersize set manually to 16384\n");
485
	    printf("alsa-init: chunksize set manually to 512\n");
486
	  }
487
	  break;
488
	case 4:
489
	  alsa_fragcount = 16;
490
	  chunk_size = 1024;
491
	  if (verbose>0) {
492
	    printf("alsa-init: buffersize set manually to 16384\n");
493
	    printf("alsa-init: chunksize set manually to 1024\n");
494
	  }
495
	  break;
496
	default:
497
	  alsa_fragcount = 16;
498
	  if (ao_mmap)
499
	    chunk_size = 512;
500
	  else
501
	    chunk_size = 1024;
502
	  break;
503
	}
504
    }
505
506
    if (!alsa_handler) {
507
      //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
508
      if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, open_mode)) < 0)
509
	{
510
	  if (err != -EBUSY && ao_noblock) {
511
	    printf("alsa-init: open in nonblock-mode failed, trying to open in block-mode\n");
512
	    if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
513
	      printf("alsa-init: playback open error: %s\n", snd_strerror(err));
514
	      return(0);
515
	    } else {
516
	      set_block_mode = 0;
517
	      str_block_mode = "block-mode";
518
	    }
519
	  } else {
520
	    printf("alsa-init: playback open error: %s\n", snd_strerror(err));
521
	    return(0);
522
	  }
523
	}
524
525
      if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) {
526
	printf("alsa-init: error set block-mode %s\n", snd_strerror(err));
527
      }
528
      else if (verbose>0) {
529
	printf("alsa-init: pcm opend in %s\n", str_block_mode);
530
      }
531
      
532
      snd_pcm_hw_params_alloca(&alsa_hwparams);
533
      snd_pcm_sw_params_alloca(&alsa_swparams);
534
535
      // setting hw-parameters
536
      if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
537
	{
538
	  printf("alsa-init: unable to get initial parameters: %s\n",
539
		 snd_strerror(err));
540
	  return(0);
541
	}
542
    
543
      if (ao_mmap) {
544
	snd_pcm_access_mask_t *mask = alloca(snd_pcm_access_mask_sizeof());
545
	snd_pcm_access_mask_none(mask);
546
	snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
547
	snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
548
	snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX);
549
	err = snd_pcm_hw_params_set_access_mask(alsa_handler, alsa_hwparams, mask);
550
	printf("alsa-init: mmap set\n");
551
      } else {
552
	err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,SND_PCM_ACCESS_RW_INTERLEAVED);
553
      }
554
      if (err < 0) {
555
	printf("alsa-init: unable to set access type: %s\n", snd_strerror(err));
556
	return (0);
557
      }
558
559
      /* workaround for nonsupported formats
560
	 sets default format to S16_LE if the given formats aren't supported */
561
      if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams,
562
                                             alsa_format)) < 0)
563
      {
564
         printf("alsa-init: format %s are not supported by hardware, trying default\n",
565
                 audio_out_format_name(format));
566
         alsa_format = SND_PCM_FORMAT_S16_LE;
567
         ao_data.format = AFMT_S16_LE;
568
         ao_data.bps = channels * rate_hz * 2;
569
      }
570
571
      bytes_per_sample = ao_data.bps / ao_data.samplerate; //it should be here
572
573
    
574
      if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
575
					      alsa_format)) < 0)
576
	{
577
	  printf("alsa-init: unable to set format: %s\n",
578
		 snd_strerror(err));
579
	  return(0);
580
	}
581
582
      if ((err = snd_pcm_hw_params_set_channels(alsa_handler, alsa_hwparams,
583
						ao_data.channels)) < 0)
584
	{
585
	  printf("alsa-init: unable to set channels: %s\n",
586
		 snd_strerror(err));
587
	  return(0);
588
	}
589
590
      if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, &ao_data.samplerate, 0)) < 0) 
591
        {
592
	  printf("alsa-init: unable to set samplerate-2: %s\n",
593
		 snd_strerror(err));
594
	  return(0);
595
        }
596
597
#ifdef BUFFERTIME
598
      {
599
	int alsa_buffer_time = 500000; /* original 60 */
600
601
	if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, alsa_buffer_time, 0)) < 0)
602
	  {
603
	    printf("alsa-init: unable to set buffer time near: %s\n",
604
		   snd_strerror(err));
605
	    return(0);
606
	  } else
607
	    alsa_buffer_time = err;
608
609
	if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams, alsa_buffer_time/4, 0)) < 0)
610
	  /* original: alsa_buffer_time/ao_data.bps */
611
	  {
612
	    printf("alsa-init: unable to set period time: %s\n",
613
		   snd_strerror(err));
614
	    return(0);
615
	  }
616
	if (verbose>0)
617
	  printf("alsa-init: buffer_time: %d, period_time :%d\n",alsa_buffer_time, err);
618
      }
619
#endif
620
621
#ifdef SET_CHUNKSIZE
622
      {
623
	//set chunksize
624
	if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, chunk_size, 0)) < 0)
625
	  {
626
	    printf("alsa-init: unable to set periodsize: %s\n", snd_strerror(err));
627
	    return(0);
628
	  }
629
	else if (verbose>0) {
630
	  printf("alsa-init: chunksize set to %i\n", chunk_size);
631
	}
632
633
	//set period_count
634
	snd_pcm_hw_params_get_periods_max(alsa_hwparams, &period_val, 0);
635
	if (period_val < alsa_fragcount)
636
	    alsa_fragcount = period_val;
637
638
	if (verbose>0)
639
	  printf("alsa-init: current val=%i, fragcount=%i\n", period_val, alsa_fragcount);
640
641
	if ((err = snd_pcm_hw_params_set_periods(alsa_handler, alsa_hwparams, alsa_fragcount, 0)) < 0) {
642
	  printf("alsa-init: unable to set periods: %s\n", snd_strerror(err));
643
	}
644
      }
645
#endif
646
647
      /* finally install hardware parameters */
648
      if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
649
	{
650
	  printf("alsa-init: unable to set hw-parameters: %s\n",
651
		 snd_strerror(err));
652
	  return(0);
653
	}
654
      // end setting hw-params
655
656
657
      // gets buffersize for control
658
      if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0)
659
	{
660
	  printf("alsa-init: unable to get buffersize: %s\n", snd_strerror(err));
661
	  return(0);
662
	}
663
      else {
664
	ao_data.buffersize = bufsize * bytes_per_sample;
665
	if (verbose>0)
666
	  printf("alsa-init: got buffersize=%i\n", ao_data.buffersize);
667
      }
668
669
      // setting sw-params (only avail-min) if noblocking mode was choosed
670
      if (ao_noblock)
671
	{
672
673
	  if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0)
674
	    {
675
	      printf("alsa-init: unable to get parameters: %s\n",snd_strerror(err));
676
	      return(0);
677
	    }
678
679
	  //set min available frames to consider pcm ready (4)
680
	  //increased for nonblock-mode should be set dynamically later
681
	  if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0)
682
	    {
683
	      printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err));
684
	      return(0);
685
	    }
686
687
	  if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
688
	    {
689
	      printf("alsa-init: unable to install sw-params\n");
690
	      return(0);
691
	    }
692
693
	  bits_per_sample = snd_pcm_format_physical_width(alsa_format);
694
	  bits_per_frame = bits_per_sample * channels;
695
	  chunk_bytes = chunk_size * bits_per_frame / 8;
696
697
	  if (verbose>0) {
698
	    printf("alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);}
699
700
	}//end swparams
701
702
      if ((err = snd_pcm_prepare(alsa_handler)) < 0)
703
	{
704
	  printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
705
	  return(0);
706
	}
707
708
      printf("alsa1x: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
709
	     ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize,
710
	     snd_pcm_format_description(alsa_format));
711
712
    } // end switch alsa_handler (spdif)
713
    alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
714
    return(1);
715
} // end init
716
717
718
/* close audio device */
719
static void uninit()
720
{
721
722
  if (alsa_handler) {
723
    int err;
724
725
    if (!ao_noblock) {
726
      if ((err = snd_pcm_drop(alsa_handler)) < 0)
727
	{
728
	  printf("alsa-uninit: pcm drop error: %s\n", snd_strerror(err));
729
	  return;
730
	}
731
    }
732
733
    if ((err = snd_pcm_close(alsa_handler)) < 0)
734
      {
735
	printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
736
	return;
737
      }
738
    else {
739
      alsa_handler = NULL;
740
      alsa_device = NULL;
741
      printf("alsa-uninit: pcm closed\n");
742
    }
743
  }
744
  else {
745
    printf("alsa-uninit: no handler defined!\n");
746
  }
747
}
748
749
static void audio_pause()
750
{
751
    int err;
752
753
    if (alsa_can_pause) {
754
        if ((err = snd_pcm_pause(alsa_handler, 1)) < 0)
755
        {
756
            printf("alsa-pause: pcm pause error: %s\n", snd_strerror(err));
757
            return;
758
        }
759
        if (verbose)
760
          printf("alsa-pause: pause supported by hardware\n");
761
    } else {
762
        if ((err = snd_pcm_drop(alsa_handler)) < 0)
763
        {
764
            printf("alsa-pause: pcm drop error: %s\n", snd_strerror(err));
765
            return;
766
        }
767
    }
768
}
769
770
static void audio_resume()
771
{
772
    int err;
773
774
    if (alsa_can_pause) {
775
        if ((err = snd_pcm_pause(alsa_handler, 0)) < 0)
776
        {
777
            printf("alsa-resume: pcm resume error: %s\n", snd_strerror(err));
778
            return;
779
        }
780
        if (verbose)
781
          printf("alsa-resume: resume supported by hardware\n");
782
    } else {
783
        if ((err = snd_pcm_prepare(alsa_handler)) < 0)
784
        {
785
           printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err));
786
            return;
787
        }
788
    }
789
}
790
791
/* stop playing and empty buffers (for seeking/pause) */
792
static void reset()
793
{
794
    int err;
795
796
    if ((err = snd_pcm_drop(alsa_handler)) < 0)
797
    {
798
	printf("alsa-reset: pcm drop error: %s\n", snd_strerror(err));
799
	return;
800
    }
801
    if ((err = snd_pcm_prepare(alsa_handler)) < 0)
802
    {
803
	printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err));
804
	return;
805
    }
806
    return;
807
}
808
809
#ifdef USE_POLL
810
static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
811
{
812
  unsigned short revents;
813
814
  while (1) {
815
    poll(ufds, count, -1);
816
    snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
817
    if (revents & POLLERR)
818
      return -EIO;
819
    if (revents & POLLOUT)
820
      return 0;
821
  }
822
} 
823
#endif
824
825
#ifndef timersub
826
#define timersub(a, b, result) \
827
do { \
828
	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
829
  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
830
  if ((result)->tv_usec < 0) { \
831
		--(result)->tv_sec; \
832
		(result)->tv_usec += 1000000; \
833
	} \
834
} while (0)
835
#endif
836
837
/* I/O error handler */
838
static int xrun(u_char *str_mode)
839
{
840
  int err;
841
  snd_pcm_status_t *status;
842
843
  snd_pcm_status_alloca(&status);
844
  
845
  if ((err = snd_pcm_status(alsa_handler, status))<0) {
846
    printf("status error: %s", snd_strerror(err));
847
    return(0);
848
  }
849
850
  if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
851
    struct timeval now, diff, tstamp;
852
    gettimeofday(&now, 0);
853
    snd_pcm_status_get_trigger_tstamp(status, &tstamp);
854
    timersub(&now, &tstamp, &diff);
855
    printf("alsa-%s: xrun of at least %.3f msecs. resetting stream\n",
856
	   str_mode,
857
	   diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
858
  }
859
860
  if ((err = snd_pcm_prepare(alsa_handler))<0) {
861
    printf("xrun: prepare error: %s", snd_strerror(err));
862
    return(0);
863
  }
864
865
  return(1); /* ok, data should be accepted again */
866
}
867
868
static int play_normal(void* data, int len);
869
static int play_mmap(void* data, int len);
870
871
static int play(void* data, int len, int flags)
872
{
873
  int result;
874
  if (ao_mmap)
875
    result = play_mmap(data, len);
876
  else
877
    result = play_normal(data, len);
878
879
  return result;
880
}
881
882
/*
883
    plays 'len' bytes of 'data'
884
    returns: number of bytes played
885
    modified last at 29.06.02 by jp
886
    thanxs for marius <marius at rospot.com> for giving us the light ;)
887
*/
888
889
static int play_normal(void* data, int len)
890
{
891
892
  //bytes_per_sample is always 4 for 2 chn S16_LE
893
  int num_frames = len / bytes_per_sample;
894
  char *output_samples = (char *)data;
895
  snd_pcm_sframes_t res = 0;
896
897
  //fprintf(stderr,"alsa-play: frames=%i, len=%i\n",num_frames,len);
898
899
  if (!alsa_handler) {
900
    printf("alsa-play: device configuration error");
901
    return 0;
902
  }
903
904
  while (num_frames > 0) {
905
906
    res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames);
907
908
      if (res == -EAGAIN) {
909
	snd_pcm_wait(alsa_handler, 1000);
910
      }
911
      else if (res == -EPIPE) {  /* underrun */
912
	if (xrun("play") <= 0) {
913
	  printf("alsa-play: xrun reset error");
914
	  return(0);
915
	}
916
      }
917
      else if (res == -ESTRPIPE) {	/* suspend */
918
	printf("alsa-play: pcm in suspend mode. trying to resume\n");
919
	while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
920
	  sleep(1);
921
      }
922
      else if (res < 0) {
923
	printf("alsa-play: unknown status, trying to reset soundcard\n");
924
	if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
925
	  printf("alsa-play: snd prepare error");
926
	  return(0);
927
	  break;
928
	}
929
      }
930
931
      if (res > 0) {
932
933
	/* output_samples += ao_data.channels * res; */
934
	output_samples += res * bytes_per_sample;
935
936
	num_frames -= res;
937
      }
938
939
  } //end while
940
941
  if (res < 0) {
942
    printf("alsa-play: write error %s", snd_strerror(res));
943
    return 0;
944
  }
945
  return res < 0 ? (int)res : len - len % bytes_per_sample;
946
}
947
948
/* mmap-mode mainly based on descriptions by Joshua Haberman <joshua at haberman.com>
949
 * 'An overview of the ALSA API' http://people.debian.org/~joshua/x66.html
950
 * and some help by Paul Davis <pbd at op.net> */
951
952
static int play_mmap(void* data, int len)
953
{
954
  snd_pcm_sframes_t commitres, frames_available;
955
  snd_pcm_uframes_t frames_transmit, size, offset;
956
  const snd_pcm_channel_area_t *area;
957
  void *outbuffer;
958
  int err, result;
959
960
#ifdef USE_POLL //seems not really be needed
961
  struct pollfd *ufds;
962
  int count;
963
964
  count = snd_pcm_poll_descriptors_count (alsa_handler);
965
  ufds = malloc(sizeof(struct pollfd) * count);
966
  snd_pcm_poll_descriptors(alsa_handler, ufds, count);
967
968
  //first wait_for_poll
969
    if (err = (wait_for_poll(alsa_handler, ufds, count) < 0)) {
970
      if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || 
971
	  snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
972
        xrun("play");
973
      }
974
    }
975
#endif
976
977
  outbuffer = alloca(ao_data.buffersize);
978
979
  //don't trust get_space() ;)
980
  frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
981
  if (frames_available < 0)
982
    xrun("play");
983
984
  if (frames_available < 4) {
985
    if (first) {
986
      first = 0;
987
      snd_pcm_start(alsa_handler);
988
    }
989
    else { //FIXME should break and return 0?
990
      snd_pcm_wait(alsa_handler, -1);
991
      first = 1;
992
    }
993
  }
994
995
  /* len is simply the available bufferspace got by get_space() 
996
   * but real avail_buffer in frames is ab/bytes_per_sample */
997
  size = len / bytes_per_sample;
998
999
  //if (verbose)
1000
  //printf("len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample);
1001
1002
  frames_transmit = size;
1003
1004
  /* prepare areas and set sw-pointers
1005
   * frames_transmit returns the real available buffer-size
1006
   * sometimes != frames_available cause of ringbuffer 'emulation' */
1007
  snd_pcm_mmap_begin(alsa_handler, &area, &offset, &frames_transmit);
1008
1009
  /* this is specific to interleaved streams (or non-interleaved
1010
   * streams with only one channel) */
1011
  outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8
1012
1013
  //write data
1014
  memcpy(outbuffer, data, (frames_transmit * bytes_per_sample));
1015
1016
  commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit);
1017
1018
  if (commitres < 0 || commitres != frames_transmit) {
1019
    if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || 
1020
	snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
1021
      xrun("play");
1022
    }
1023
  }
1024
1025
  //if (verbose)
1026
  //printf("mmap ft: %i, cres: %i\n", frames_transmit, commitres);
1027
1028
  /* 	err = snd_pcm_area_copy(&area, offset, &data, offset, len, alsa_format); */
1029
  /* 	if (err < 0) { */
1030
  /* 	  printf("area-copy-error\n"); */
1031
  /* 	  return 0; */
1032
  /* 	} */
1033
1034
1035
  //calculate written frames!
1036
  result = commitres * bytes_per_sample;
1037
1038
1039
  /* if (verbose) { */
1040
  /* if (len == result) */
1041
  /* printf("result: %i, frames written: %i ...\n", result, frames_transmit); */
1042
  /* else */
1043
  /* printf("result: %i, frames written: %i, result != len ...\n", result, frames_transmit); */
1044
  /* } */
1045
1046
  //mplayer doesn't like -result
1047
  if (result < 0)
1048
    result = 0;
1049
1050
#ifdef USE_POLL
1051
  free(ufds);
1052
#endif
1053
1054
  return result;
1055
}
1056
1057
/* how many byes are free in the buffer */
1058
static int get_space()
1059
{
1060
    snd_pcm_status_t *status;
1061
    int ret;
1062
    char *str_status;
1063
1064
    //snd_pcm_sframes_t avail_frames = 0;
1065
    
1066
    if ((ret = snd_pcm_status_malloc(&status)) < 0)
1067
    {
1068
	printf("alsa-space: memory allocation error: %s\n", snd_strerror(ret));
1069
	return(0);
1070
    }
1071
    
1072
    if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
1073
    {
1074
	printf("alsa-space: cannot get pcm status: %s\n", snd_strerror(ret));
1075
	return(0);
1076
    }
1077
    
1078
    switch(snd_pcm_status_get_state(status))
1079
    {
1080
    case SND_PCM_STATE_OPEN:
1081
      str_status = "open";
1082
    case SND_PCM_STATE_PREPARED:
1083
      if (str_status != "open") {
1084
	str_status = "prepared";
1085
	first = 1;
1086
	ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1087
	if (ret == 0) //ugly workaround for hang in mmap-mode
1088
	  ret = 10;
1089
	break;
1090
      }
1091
    case SND_PCM_STATE_RUNNING:
1092
      ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1093
      //avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
1094
      if (str_status != "open" && str_status != "prepared")
1095
	str_status = "running";
1096
      break;
1097
    case SND_PCM_STATE_PAUSED:
1098
      if (verbose>0) printf("alsa-space: paused");
1099
      str_status = "paused";
1100
      ret = 0;
1101
      break;
1102
    case SND_PCM_STATE_XRUN:
1103
      xrun("space");
1104
      str_status = "xrun";
1105
      first = 1;
1106
      ret = 0;
1107
      break;
1108
    default:
1109
      str_status = "undefined";
1110
      ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1111
      if (ret <= 0) {
1112
	xrun("space");
1113
      }
1114
    }
1115
1116
    if (verbose>0 && str_status != "running")
1117
      printf("alsa-space: free space = %i, status=%i, %s --\n", ret, status, str_status);
1118
    snd_pcm_status_free(status);
1119
    
1120
    if (ret < 0) {
1121
      printf("negative value!!\n");
1122
      ret = 0;
1123
    }
1124
 
1125
    // workaround for too small value returned
1126
    if (ret < MIN_CHUNK_SIZE)
1127
      ret = 0;
1128
1129
    return(ret);
1130
}
1131
1132
/* delay in seconds between first and last sample in buffer */
1133
static float get_delay()
1134
{
1135
1136
  if (alsa_handler) {
1137
1138
    snd_pcm_status_t *status;
1139
    float ret;
1140
    
1141
    if ((ret = snd_pcm_status_malloc(&status)) < 0)
1142
    {
1143
	printf("alsa-delay: memory allocation error: %s\n", snd_strerror(ret));
1144
	return(0);
1145
    }
1146
    
1147
    if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
1148
    {
1149
	printf("alsa-delay: cannot get pcm status: %s\n", snd_strerror(ret));
1150
	return(0);
1151
    }
1152
    
1153
    switch(snd_pcm_status_get_state(status))
1154
    {
1155
	case SND_PCM_STATE_OPEN:
1156
	case SND_PCM_STATE_PREPARED:
1157
	case SND_PCM_STATE_RUNNING:
1158
	    ret = (float)snd_pcm_status_get_delay(status)/(float)ao_data.samplerate;
1159
	    break;
1160
	default:
1161
	    ret = 0;
1162
    }
1163
    
1164
    snd_pcm_status_free(status);
1165
1166
    if (ret < 0)
1167
      ret = 0;
1168
    return(ret);
1169
    
1170
  } else {
1171
    return(0);
1172
  }
1173
}
(-)vanilla/main/libao2/audio_out.c (+6 lines)
Lines 29-34 Link Here
29
#ifdef HAVE_ALSA9
29
#ifdef HAVE_ALSA9
30
 extern ao_functions_t audio_out_alsa9;
30
 extern ao_functions_t audio_out_alsa9;
31
#endif
31
#endif
32
#ifdef HAVE_ALSA1X
33
 extern ao_functions_t audio_out_alsa1x;
34
#endif
32
#ifdef HAVE_NAS
35
#ifdef HAVE_NAS
33
extern ao_functions_t audio_out_nas;
36
extern ao_functions_t audio_out_nas;
34
#endif
37
#endif
Lines 75-80 Link Here
75
#ifdef HAVE_ALSA9
78
#ifdef HAVE_ALSA9
76
	&audio_out_alsa9,
79
	&audio_out_alsa9,
77
#endif
80
#endif
81
#ifdef HAVE_ALSA1X
82
	&audio_out_alsa1x,
83
#endif
78
#ifdef HAVE_ALSA5
84
#ifdef HAVE_ALSA5
79
	&audio_out_alsa5,
85
	&audio_out_alsa5,
80
#endif
86
#endif
(-)vanilla/main/libmpdemux/ai_alsa1x.c (+165 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/time.h>
4
5
#include "config.h"
6
7
#if defined(USE_TV) && (defined(HAVE_TV_V4L) || defined(HAVE_TV_V4L2)) && defined(HAVE_ALSA1X)
8
9
#include <alsa/asoundlib.h>
10
#include "audio_in.h"
11
#include "mp_msg.h"
12
13
int ai_alsa_setup(audio_in_t *ai)
14
{
15
    snd_pcm_hw_params_t *params;
16
    snd_pcm_sw_params_t *swparams;
17
    snd_pcm_uframes_t buffer_size;
18
    int err;
19
20
    snd_pcm_hw_params_alloca(&params);
21
    snd_pcm_sw_params_alloca(&swparams);
22
23
    err = snd_pcm_hw_params_any(ai->alsa.handle, params);
24
    if (err < 0) {
25
	mp_msg(MSGT_TV, MSGL_ERR, "Broken configuration for this PCM: no configurations available\n");
26
	return -1;
27
    }
28
    err = snd_pcm_hw_params_set_access(ai->alsa.handle, params,
29
				       SND_PCM_ACCESS_RW_INTERLEAVED);
30
    if (err < 0) {
31
	mp_msg(MSGT_TV, MSGL_ERR, "Access type not available\n");
32
	return -1;
33
    }
34
    err = snd_pcm_hw_params_set_format(ai->alsa.handle, params, SND_PCM_FORMAT_S16_LE);
35
    if (err < 0) {
36
	mp_msg(MSGT_TV, MSGL_ERR, "Sample format not available\n");
37
	return -1;
38
    }
39
    err = snd_pcm_hw_params_set_channels(ai->alsa.handle, params, ai->req_channels);
40
    if (err < 0) {
41
	snd_pcm_hw_params_get_channels(params, &ai->channels);
42
	mp_msg(MSGT_TV, MSGL_ERR, "Channel count not available - reverting to default: %d\n",
43
	       ai->channels);
44
    } else {
45
	ai->channels = ai->req_channels;
46
    }
47
48
    snd_pcm_hw_params_set_rate_near(ai->alsa.handle, params, &ai->req_samplerate, 0);
49
50
    ai->alsa.buffer_time = 1000000;
51
    snd_pcm_hw_params_set_buffer_time_near(ai->alsa.handle, params,
52
					   &ai->alsa.buffer_time, 0);
53
    assert(ai->alsa.buffer_time >= 0);
54
    ai->alsa.period_time = ai->alsa.buffer_time / 4;
55
    snd_pcm_hw_params_set_period_time_near(ai->alsa.handle, params,
56
					   &ai->alsa.period_time, 0);
57
    assert(ai->alsa.period_time >= 0);
58
    err = snd_pcm_hw_params(ai->alsa.handle, params);
59
    if (err < 0) {
60
	mp_msg(MSGT_TV, MSGL_ERR, "Unable to install hw params:");
61
	snd_pcm_hw_params_dump(params, ai->alsa.log);
62
	return -1;
63
    }
64
    snd_pcm_hw_params_get_period_size(params, 0, &ai->alsa.chunk_size);
65
    snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
66
    if (ai->alsa.chunk_size == buffer_size) {
67
	mp_msg(MSGT_TV, MSGL_ERR, "Can't use period equal to buffer size (%u == %lu)\n", ai->alsa.chunk_size, (long)buffer_size);
68
	return -1;
69
    }
70
    snd_pcm_sw_params_current(ai->alsa.handle, swparams);
71
    err = snd_pcm_sw_params_set_sleep_min(ai->alsa.handle, swparams,0);
72
    assert(err >= 0);
73
    err = snd_pcm_sw_params_set_avail_min(ai->alsa.handle, swparams, ai->alsa.chunk_size);
74
    assert(err >= 0);
75
76
    err = snd_pcm_sw_params_set_start_threshold(ai->alsa.handle, swparams, 0);
77
    assert(err >= 0);
78
    err = snd_pcm_sw_params_set_stop_threshold(ai->alsa.handle, swparams, buffer_size);
79
    assert(err >= 0);
80
81
    assert(err >= 0);
82
    if (snd_pcm_sw_params(ai->alsa.handle, swparams) < 0) {
83
	mp_msg(MSGT_TV, MSGL_ERR, "unable to install sw params:\n");
84
	snd_pcm_sw_params_dump(swparams, ai->alsa.log);
85
	return -1;
86
    }
87
88
    if (mp_msg_test(MSGT_TV, MSGL_V)) {
89
	snd_pcm_dump(ai->alsa.handle, ai->alsa.log);
90
    }
91
92
    ai->alsa.bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
93
    ai->alsa.bits_per_frame = ai->alsa.bits_per_sample * ai->channels;
94
    ai->blocksize = ai->alsa.chunk_size * ai->alsa.bits_per_frame / 8;
95
    ai->samplesize = ai->alsa.bits_per_sample;
96
    ai->bytes_per_sample = ai->alsa.bits_per_sample/8;
97
98
    return 0;
99
}
100
101
int ai_alsa_init(audio_in_t *ai)
102
{
103
    int err;
104
    
105
    err = snd_pcm_open(&ai->alsa.handle, ai->alsa.device, SND_PCM_STREAM_CAPTURE, 0);
106
    if (err < 0) {
107
	mp_msg(MSGT_TV, MSGL_ERR, "Error opening audio: %s\n", snd_strerror(err));
108
	return -1;
109
    }
110
    
111
    err = snd_output_stdio_attach(&ai->alsa.log, stderr, 0);
112
    
113
    if (err < 0) {
114
	return -1;
115
    }
116
    
117
    err = ai_alsa_setup(ai);
118
119
    return err;
120
}
121
122
#ifndef timersub
123
#define	timersub(a, b, result) \
124
do { \
125
	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
126
	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
127
	if ((result)->tv_usec < 0) { \
128
		--(result)->tv_sec; \
129
		(result)->tv_usec += 1000000; \
130
	} \
131
} while (0)
132
#endif
133
134
int ai_alsa_xrun(audio_in_t *ai)
135
{
136
    snd_pcm_status_t *status;
137
    int res;
138
	
139
    snd_pcm_status_alloca(&status);
140
    if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
141
	mp_msg(MSGT_TV, MSGL_ERR, "ALSA status error: %s", snd_strerror(res));
142
	return -1;
143
    }
144
    if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
145
	struct timeval now, diff, tstamp;
146
	gettimeofday(&now, 0);
147
	snd_pcm_status_get_trigger_tstamp(status, &tstamp);
148
	timersub(&now, &tstamp, &diff);
149
	mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun!!! (at least %.3f ms long)\n",
150
	       diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
151
	if (mp_msg_test(MSGT_TV, MSGL_V)) {
152
	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA Status:\n");
153
	    snd_pcm_status_dump(status, ai->alsa.log);
154
	}
155
	if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
156
	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun: prepare error: %s", snd_strerror(res));
157
	    return -1;
158
	}
159
	return 0;		/* ok, data should be accepted again */
160
    }
161
    mp_msg(MSGT_TV, MSGL_ERR, "ALSA read/write error");
162
    return -1;
163
}
164
165
#endif /* HAVE_ALSA1X */
(-)vanilla/main/libmpdemux/audio_in.c (-9 / +9 lines)
Lines 24-30 Link Here
24
    ai->samplesize = -1;
24
    ai->samplesize = -1;
25
25
26
    switch (ai->type) {
26
    switch (ai->type) {
27
#ifdef HAVE_ALSA9	  
27
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
28
    case AUDIO_IN_ALSA:
28
    case AUDIO_IN_ALSA:
29
	ai->alsa.handle = NULL;
29
	ai->alsa.handle = NULL;
30
	ai->alsa.log = NULL;
30
	ai->alsa.log = NULL;
Lines 46-52 Link Here
46
{
46
{
47
    
47
    
48
    switch (ai->type) {
48
    switch (ai->type) {
49
#ifdef HAVE_ALSA9	  
49
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
50
    case AUDIO_IN_ALSA:
50
    case AUDIO_IN_ALSA:
51
	if (ai_alsa_init(ai) < 0) return -1;
51
	if (ai_alsa_init(ai) < 0) return -1;
52
	ai->setup = 1;
52
	ai->setup = 1;
Lines 66-72 Link Here
66
int audio_in_set_samplerate(audio_in_t *ai, int rate)
66
int audio_in_set_samplerate(audio_in_t *ai, int rate)
67
{
67
{
68
    switch (ai->type) {
68
    switch (ai->type) {
69
#ifdef HAVE_ALSA9	  
69
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
70
    case AUDIO_IN_ALSA:
70
    case AUDIO_IN_ALSA:
71
	ai->req_samplerate = rate;
71
	ai->req_samplerate = rate;
72
	if (!ai->setup) return 0;
72
	if (!ai->setup) return 0;
Lines 88-94 Link Here
88
int audio_in_set_channels(audio_in_t *ai, int channels)
88
int audio_in_set_channels(audio_in_t *ai, int channels)
89
{
89
{
90
    switch (ai->type) {
90
    switch (ai->type) {
91
#ifdef HAVE_ALSA9	  
91
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
92
    case AUDIO_IN_ALSA:
92
    case AUDIO_IN_ALSA:
93
	ai->req_channels = channels;
93
	ai->req_channels = channels;
94
	if (!ai->setup) return 0;
94
	if (!ai->setup) return 0;
Lines 109-120 Link Here
109
109
110
int audio_in_set_device(audio_in_t *ai, char *device)
110
int audio_in_set_device(audio_in_t *ai, char *device)
111
{
111
{
112
#ifdef HAVE_ALSA9	  
112
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
113
    int i;
113
    int i;
114
#endif
114
#endif
115
    if (ai->setup) return -1;
115
    if (ai->setup) return -1;
116
    switch (ai->type) {
116
    switch (ai->type) {
117
#ifdef HAVE_ALSA9	  
117
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
118
    case AUDIO_IN_ALSA:
118
    case AUDIO_IN_ALSA:
119
	if (ai->alsa.device) free(ai->alsa.device);
119
	if (ai->alsa.device) free(ai->alsa.device);
120
	ai->alsa.device = strdup(device);
120
	ai->alsa.device = strdup(device);
Lines 139-145 Link Here
139
{
139
{
140
    if (ai->setup) {
140
    if (ai->setup) {
141
	switch (ai->type) {
141
	switch (ai->type) {
142
#ifdef HAVE_ALSA9	  
142
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
143
	case AUDIO_IN_ALSA:
143
	case AUDIO_IN_ALSA:
144
	    if (ai->alsa.log)
144
	    if (ai->alsa.log)
145
		snd_output_close(ai->alsa.log);
145
		snd_output_close(ai->alsa.log);
Lines 163-169 Link Here
163
int audio_in_start_capture(audio_in_t *ai)
163
int audio_in_start_capture(audio_in_t *ai)
164
{
164
{
165
    switch (ai->type) {
165
    switch (ai->type) {
166
#ifdef HAVE_ALSA9	  
166
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
167
    case AUDIO_IN_ALSA:
167
    case AUDIO_IN_ALSA:
168
	return snd_pcm_start(ai->alsa.handle);
168
	return snd_pcm_start(ai->alsa.handle);
169
#endif
169
#endif
Lines 181-187 Link Here
181
    int ret;
181
    int ret;
182
    
182
    
183
    switch (ai->type) {
183
    switch (ai->type) {
184
#ifdef HAVE_ALSA9	  
184
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
185
    case AUDIO_IN_ALSA:
185
    case AUDIO_IN_ALSA:
186
	ret = snd_pcm_readi(ai->alsa.handle, buffer, ai->alsa.chunk_size);
186
	ret = snd_pcm_readi(ai->alsa.handle, buffer, ai->alsa.chunk_size);
187
	if (ret != ai->alsa.chunk_size) {
187
	if (ret != ai->alsa.chunk_size) {
(-)vanilla/main/libmpdemux/audio_in.h (-3 / +3 lines)
Lines 6-12 Link Here
6
6
7
#include "config.h"
7
#include "config.h"
8
8
9
#ifdef HAVE_ALSA9
9
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
10
#include <alsa/asoundlib.h>
10
#include <alsa/asoundlib.h>
11
11
12
typedef struct {
12
typedef struct {
Lines 43-49 Link Here
43
    int bytes_per_sample;
43
    int bytes_per_sample;
44
    int samplesize;
44
    int samplesize;
45
    
45
    
46
#ifdef HAVE_ALSA9
46
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
47
    ai_alsa_t alsa;
47
    ai_alsa_t alsa;
48
#endif
48
#endif
49
#ifdef USE_OSS_AUDIO
49
#ifdef USE_OSS_AUDIO
Lines 60-66 Link Here
60
int audio_in_start_capture(audio_in_t *ai);
60
int audio_in_start_capture(audio_in_t *ai);
61
int audio_in_read_chunk(audio_in_t *ai, unsigned char *buffer);
61
int audio_in_read_chunk(audio_in_t *ai, unsigned char *buffer);
62
62
63
#ifdef HAVE_ALSA9
63
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
64
int ai_alsa_setup(audio_in_t *ai);
64
int ai_alsa_setup(audio_in_t *ai);
65
int ai_alsa_init(audio_in_t *ai);
65
int ai_alsa_init(audio_in_t *ai);
66
int ai_alsa_xrun(audio_in_t *ai);
66
int ai_alsa_xrun(audio_in_t *ai);
(-)vanilla/main/libmpdemux/Makefile (-1 / +1 lines)
Lines 3-9 Link Here
3
3
4
include ../config.mak
4
include ../config.mak
5
5
6
SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_ty.c demux_ty_osd.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c stream_ftp.c tv.c tvi_dummy.c tvi_v4l.c tvi_v4l2.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c url.c 
6
SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_ty.c demux_ty_osd.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c stream_ftp.c tv.c tvi_dummy.c tvi_v4l.c tvi_v4l2.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_alsa1x.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c url.c 
7
ifeq ($(XMMS_PLUGINS),yes)
7
ifeq ($(XMMS_PLUGINS),yes)
8
SRCS += demux_xmms.c
8
SRCS += demux_xmms.c
9
endif 
9
endif 
(-)vanilla/main/libmpdemux/tv.c (-1 / +1 lines)
Lines 70-76 Link Here
70
int tv_param_mjpeg = 0;
70
int tv_param_mjpeg = 0;
71
int tv_param_decimation = 2;
71
int tv_param_decimation = 2;
72
int tv_param_quality = 90;
72
int tv_param_quality = 90;
73
#ifdef HAVE_ALSA9
73
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
74
int tv_param_alsa = 0;
74
int tv_param_alsa = 0;
75
#endif
75
#endif
76
char* tv_param_adevice = NULL;
76
char* tv_param_adevice = NULL;
(-)vanilla/main/libmpdemux/tv.h (-1 / +1 lines)
Lines 40-46 Link Here
40
extern int tv_param_mjpeg;
40
extern int tv_param_mjpeg;
41
extern int tv_param_decimation;
41
extern int tv_param_decimation;
42
extern int tv_param_quality;
42
extern int tv_param_quality;
43
#ifdef HAVE_ALSA9
43
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
44
extern int tv_param_alsa;
44
extern int tv_param_alsa;
45
#endif
45
#endif
46
extern char* tv_param_adevice;
46
extern char* tv_param_adevice;
(-)vanilla/main/libmpdemux/tvi_v4l2.c (-1 / +1 lines)
Lines 1045-1051 Link Here
1045
    
1045
    
1046
    /* audio init */
1046
    /* audio init */
1047
    if (!tv_param_noaudio) {
1047
    if (!tv_param_noaudio) {
1048
#ifdef HAVE_ALSA9
1048
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
1049
	if (tv_param_alsa)
1049
	if (tv_param_alsa)
1050
	    audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
1050
	    audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
1051
	else
1051
	else
(-)vanilla/main/libmpdemux/tvi_v4l.c (-1 / +1 lines)
Lines 634-640 Link Here
634
    /* audio init */
634
    /* audio init */
635
    if (!tv_param_noaudio) {
635
    if (!tv_param_noaudio) {
636
	
636
	
637
#ifdef HAVE_ALSA9
637
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
638
	if (tv_param_alsa)
638
	if (tv_param_alsa)
639
	    audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
639
	    audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
640
	else
640
	else

Return to bug 35091