annotate stream/ai_alsa.c @ 35314:eaace9a7e822

Simplify: remove useless casts, NULL checks etc.
author reimar
date Sat, 10 Nov 2012 14:38:49 +0000
parents 11ab37356dd1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30426
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
1 /*
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
2 * This file is part of MPlayer.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
3 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
5 * it under the terms of the GNU General Public License as published by
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
7 * (at your option) any later version.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
8 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
12 * GNU General Public License for more details.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
13 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
14 * You should have received a copy of the GNU General Public License along
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
17 */
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
18
7213
13fcab6fde41 audio in layer is used only bu tvi_v4l, don't compile for others.
arpi
parents: 7070
diff changeset
19 #include <stdio.h>
13fcab6fde41 audio in layer is used only bu tvi_v4l, don't compile for others.
arpi
parents: 7070
diff changeset
20 #include <stdlib.h>
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
21 #include <sys/time.h>
27281
e47193172586 Our ALSA code needs alloca, so check for it in configure and include alloca.h
reimar
parents: 19271
diff changeset
22 #include <alloca.h>
34258
11ab37356dd1 ai_alsa: set appropriate #defines to work with ALSA 0.9.x headers
diego
parents: 34247
diff changeset
23 #define ALSA_PCM_NEW_HW_PARAMS_API
11ab37356dd1 ai_alsa: set appropriate #defines to work with ALSA 0.9.x headers
diego
parents: 34247
diff changeset
24 #define ALSA_PCM_NEW_SW_PARAMS_API
11ab37356dd1 ai_alsa: set appropriate #defines to work with ALSA 0.9.x headers
diego
parents: 34247
diff changeset
25 #include <alsa/asoundlib.h>
7213
13fcab6fde41 audio in layer is used only bu tvi_v4l, don't compile for others.
arpi
parents: 7070
diff changeset
26
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
27 #include "config.h"
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
28 #include "audio_in.h"
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
29 #include "mp_msg.h"
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
30 #include "help_mp.h"
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
31
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
32 int ai_alsa_setup(audio_in_t *ai)
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
33 {
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
34 snd_pcm_hw_params_t *params;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
35 snd_pcm_sw_params_t *swparams;
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
36 snd_pcm_uframes_t buffer_size, period_size;
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
37 int err;
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
38 int dir;
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
39 unsigned int rate;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
40
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
41 snd_pcm_hw_params_alloca(&params);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
42 snd_pcm_sw_params_alloca(&swparams);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
43
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
44 err = snd_pcm_hw_params_any(ai->alsa.handle, params);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
45 if (err < 0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
46 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig);
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
47 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
48 }
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
49
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
50 err = snd_pcm_hw_params_set_access(ai->alsa.handle, params,
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
51 SND_PCM_ACCESS_RW_INTERLEAVED);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
52 if (err < 0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
53 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType);
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
54 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
55 }
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
56
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
57 err = snd_pcm_hw_params_set_format(ai->alsa.handle, params, SND_PCM_FORMAT_S16_LE);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
58 if (err < 0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
59 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt);
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
60 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
61 }
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
62
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
63 err = snd_pcm_hw_params_set_channels(ai->alsa.handle, params, ai->req_channels);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
64 if (err < 0) {
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
65 snd_pcm_hw_params_get_channels(params, &ai->channels);
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
66 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount,
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
67 ai->channels);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
68 } else {
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
69 ai->channels = ai->req_channels;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
70 }
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
71
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
72 dir = 0;
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
73 rate = ai->req_samplerate;
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
74 err = snd_pcm_hw_params_set_rate_near(ai->alsa.handle, params, &rate, &dir);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
75 if (err < 0) {
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
76 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotSetSamplerate);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
77 }
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
78 ai->samplerate = rate;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
79
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
80 dir = 0;
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
81 ai->alsa.buffer_time = 1000000;
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
82 err = snd_pcm_hw_params_set_buffer_time_near(ai->alsa.handle, params,
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
83 &ai->alsa.buffer_time, &dir);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
84 if (err < 0) {
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
85 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotSetBufferTime);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
86 }
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
87
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
88 dir = 0;
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
89 ai->alsa.period_time = ai->alsa.buffer_time / 4;
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
90 err = snd_pcm_hw_params_set_period_time_near(ai->alsa.handle, params,
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
91 &ai->alsa.period_time, &dir);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
92 if (err < 0) {
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
93 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotSetPeriodTime);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
94 }
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
95
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
96 err = snd_pcm_hw_params(ai->alsa.handle, params);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
97 if (err < 0) {
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
98 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams, snd_strerror(err));
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
99 snd_pcm_hw_params_dump(params, ai->alsa.log);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
100 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
101 }
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
102
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
103 dir = -1;
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
104 snd_pcm_hw_params_get_period_size(params, &period_size, &dir);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
105 snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
106 ai->alsa.chunk_size = period_size;
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
107 if (period_size == buffer_size) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
108 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize, ai->alsa.chunk_size, (long)buffer_size);
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
109 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
110 }
34247
1c35122fba8c Remove obsolete ALSA 0.5 audio output and ALSA 0.9 audio input.
diego
parents: 33827
diff changeset
111
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
112 snd_pcm_sw_params_current(ai->alsa.handle, swparams);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
113 err = snd_pcm_sw_params_set_sleep_min(ai->alsa.handle, swparams,0);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
114 err = snd_pcm_sw_params_set_avail_min(ai->alsa.handle, swparams, ai->alsa.chunk_size);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
115
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
116 err = snd_pcm_sw_params_set_start_threshold(ai->alsa.handle, swparams, 0);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
117 err = snd_pcm_sw_params_set_stop_threshold(ai->alsa.handle, swparams, buffer_size);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
118
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
119 if (snd_pcm_sw_params(ai->alsa.handle, swparams) < 0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
120 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams);
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
121 snd_pcm_sw_params_dump(swparams, ai->alsa.log);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
122 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
123 }
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
124
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
125 if (mp_msg_test(MSGT_TV, MSGL_V)) {
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
126 snd_pcm_dump(ai->alsa.handle, ai->alsa.log);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
127 }
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
128
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
129 ai->alsa.bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
130 ai->alsa.bits_per_frame = ai->alsa.bits_per_sample * ai->channels;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
131 ai->blocksize = ai->alsa.chunk_size * ai->alsa.bits_per_frame / 8;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
132 ai->samplesize = ai->alsa.bits_per_sample;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
133 ai->bytes_per_sample = ai->alsa.bits_per_sample/8;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
134
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
135 return 0;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
136 }
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
137
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
138 int ai_alsa_init(audio_in_t *ai)
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
139 {
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
140 int err;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27281
diff changeset
141
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
142 err = snd_pcm_open(&ai->alsa.handle, ai->alsa.device, SND_PCM_STREAM_CAPTURE, 0);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
143 if (err < 0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
144 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio, snd_strerror(err));
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
145 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
146 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27281
diff changeset
147
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
148 err = snd_output_stdio_attach(&ai->alsa.log, stderr, 0);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27281
diff changeset
149
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
150 if (err < 0) {
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
151 return -1;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
152 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27281
diff changeset
153
7060
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
154 err = ai_alsa_setup(ai);
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
155
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
156 return err;
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
157 }
b14880a6cccb new v4l capture patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>:
arpi
parents:
diff changeset
158
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
159 #ifndef timersub
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
160 #define timersub(a, b, result) \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
161 do { \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
162 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
163 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
164 if ((result)->tv_usec < 0) { \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
165 --(result)->tv_sec; \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
166 (result)->tv_usec += 1000000; \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
167 } \
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
168 } while (0)
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
169 #endif
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
170
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
171 int ai_alsa_xrun(audio_in_t *ai)
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
172 {
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
173 snd_pcm_status_t *status;
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
174 int res;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27281
diff changeset
175
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
176 snd_pcm_status_alloca(&status);
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
177 if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
178 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaStatusError, snd_strerror(res));
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
179 return -1;
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
180 }
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
181 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
182 struct timeval now, diff, tstamp;
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
183 gettimeofday(&now, 0);
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
184 snd_pcm_status_get_trigger_tstamp(status, &tstamp);
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
185 timersub(&now, &tstamp, &diff);
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
186 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUN,
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
187 diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
188 if (mp_msg_test(MSGT_TV, MSGL_V)) {
33827
277ec491a8a7 Do not translate console messages of verbosity level MSGL_V and above.
diego
parents: 30426
diff changeset
189 mp_msg(MSGT_TV, MSGL_ERR, "ALSA Status:\n");
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
190 snd_pcm_status_dump(status, ai->alsa.log);
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
191 }
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
192 if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
193 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError, snd_strerror(res));
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
194 return -1;
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
195 }
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
196 return 0; /* ok, data should be accepted again */
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
197 }
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 10537
diff changeset
198 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError);
7586
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
199 return -1;
d12421dd1265 this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
alex
parents: 7213
diff changeset
200 }