~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to stream/ai_alsa1x.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of MPlayer.
3
 
 *
4
 
 * MPlayer is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * MPlayer is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 
 */
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <sys/time.h>
22
 
#include <alloca.h>
23
 
 
24
 
#include "config.h"
25
 
 
26
 
#include <alsa/asoundlib.h>
27
 
#include "audio_in.h"
28
 
#include "mp_msg.h"
29
 
#include "help_mp.h"
30
 
 
31
 
int ai_alsa_setup(audio_in_t *ai)
32
 
{
33
 
    snd_pcm_hw_params_t *params;
34
 
    snd_pcm_sw_params_t *swparams;
35
 
    snd_pcm_uframes_t buffer_size, period_size;
36
 
    int err;
37
 
    int dir;
38
 
    unsigned int rate;
39
 
 
40
 
    snd_pcm_hw_params_alloca(&params);
41
 
    snd_pcm_sw_params_alloca(&swparams);
42
 
 
43
 
    err = snd_pcm_hw_params_any(ai->alsa.handle, params);
44
 
    if (err < 0) {
45
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig);
46
 
        return -1;
47
 
    }
48
 
 
49
 
    err = snd_pcm_hw_params_set_access(ai->alsa.handle, params,
50
 
                                       SND_PCM_ACCESS_RW_INTERLEAVED);
51
 
    if (err < 0) {
52
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType);
53
 
        return -1;
54
 
    }
55
 
 
56
 
    err = snd_pcm_hw_params_set_format(ai->alsa.handle, params, SND_PCM_FORMAT_S16_LE);
57
 
    if (err < 0) {
58
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt);
59
 
        return -1;
60
 
    }
61
 
 
62
 
    err = snd_pcm_hw_params_set_channels(ai->alsa.handle, params, ai->req_channels);
63
 
    if (err < 0) {
64
 
        snd_pcm_hw_params_get_channels(params, &ai->channels);
65
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount,
66
 
               ai->channels);
67
 
    } else {
68
 
        ai->channels = ai->req_channels;
69
 
    }
70
 
 
71
 
    dir = 0;
72
 
    rate = ai->req_samplerate;
73
 
    err = snd_pcm_hw_params_set_rate_near(ai->alsa.handle, params, &rate, &dir);
74
 
    if (err < 0) {
75
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA1X_CannotSetSamplerate);
76
 
    }
77
 
    ai->samplerate = rate;
78
 
 
79
 
    dir = 0;
80
 
    ai->alsa.buffer_time = 1000000;
81
 
    err = snd_pcm_hw_params_set_buffer_time_near(ai->alsa.handle, params,
82
 
                                                 &ai->alsa.buffer_time, &dir);
83
 
    if (err < 0) {
84
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA1X_CannotSetBufferTime);
85
 
    }
86
 
 
87
 
    dir = 0;
88
 
    ai->alsa.period_time = ai->alsa.buffer_time / 4;
89
 
    err = snd_pcm_hw_params_set_period_time_near(ai->alsa.handle, params,
90
 
                                                 &ai->alsa.period_time, &dir);
91
 
    if (err < 0) {
92
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA1X_CannotSetPeriodTime);
93
 
    }
94
 
 
95
 
    err = snd_pcm_hw_params(ai->alsa.handle, params);
96
 
    if (err < 0) {
97
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams, snd_strerror(err));
98
 
        snd_pcm_hw_params_dump(params, ai->alsa.log);
99
 
        return -1;
100
 
    }
101
 
 
102
 
    dir = -1;
103
 
    snd_pcm_hw_params_get_period_size(params, &period_size, &dir);
104
 
    snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
105
 
    ai->alsa.chunk_size = period_size;
106
 
    if (period_size == buffer_size) {
107
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize, ai->alsa.chunk_size, (long)buffer_size);
108
 
        return -1;
109
 
    }
110
 
 
111
 
    snd_pcm_sw_params_current(ai->alsa.handle, swparams);
112
 
    err = snd_pcm_sw_params_set_sleep_min(ai->alsa.handle, swparams,0);
113
 
    err = snd_pcm_sw_params_set_avail_min(ai->alsa.handle, swparams, ai->alsa.chunk_size);
114
 
 
115
 
    err = snd_pcm_sw_params_set_start_threshold(ai->alsa.handle, swparams, 0);
116
 
    err = snd_pcm_sw_params_set_stop_threshold(ai->alsa.handle, swparams, buffer_size);
117
 
 
118
 
    if (snd_pcm_sw_params(ai->alsa.handle, swparams) < 0) {
119
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams);
120
 
        snd_pcm_sw_params_dump(swparams, ai->alsa.log);
121
 
        return -1;
122
 
    }
123
 
 
124
 
    if (mp_msg_test(MSGT_TV, MSGL_V)) {
125
 
        snd_pcm_dump(ai->alsa.handle, ai->alsa.log);
126
 
    }
127
 
 
128
 
    ai->alsa.bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
129
 
    ai->alsa.bits_per_frame = ai->alsa.bits_per_sample * ai->channels;
130
 
    ai->blocksize = ai->alsa.chunk_size * ai->alsa.bits_per_frame / 8;
131
 
    ai->samplesize = ai->alsa.bits_per_sample;
132
 
    ai->bytes_per_sample = ai->alsa.bits_per_sample/8;
133
 
 
134
 
    return 0;
135
 
}
136
 
 
137
 
int ai_alsa_init(audio_in_t *ai)
138
 
{
139
 
    int err;
140
 
 
141
 
    err = snd_pcm_open(&ai->alsa.handle, ai->alsa.device, SND_PCM_STREAM_CAPTURE, 0);
142
 
    if (err < 0) {
143
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio, snd_strerror(err));
144
 
        return -1;
145
 
    }
146
 
 
147
 
    err = snd_output_stdio_attach(&ai->alsa.log, stderr, 0);
148
 
 
149
 
    if (err < 0) {
150
 
        return -1;
151
 
    }
152
 
 
153
 
    err = ai_alsa_setup(ai);
154
 
 
155
 
    return err;
156
 
}
157
 
 
158
 
#ifndef timersub
159
 
#define timersub(a, b, result) \
160
 
do { \
161
 
        (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
162
 
        (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
163
 
        if ((result)->tv_usec < 0) { \
164
 
                --(result)->tv_sec; \
165
 
                (result)->tv_usec += 1000000; \
166
 
        } \
167
 
} while (0)
168
 
#endif
169
 
 
170
 
int ai_alsa_xrun(audio_in_t *ai)
171
 
{
172
 
    snd_pcm_status_t *status;
173
 
    int res;
174
 
 
175
 
    snd_pcm_status_alloca(&status);
176
 
    if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
177
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaStatusError, snd_strerror(res));
178
 
        return -1;
179
 
    }
180
 
    if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
181
 
        struct timeval now, diff, tstamp;
182
 
        gettimeofday(&now, 0);
183
 
        snd_pcm_status_get_trigger_tstamp(status, &tstamp);
184
 
        timersub(&now, &tstamp, &diff);
185
 
        mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUN,
186
 
               diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
187
 
        if (mp_msg_test(MSGT_TV, MSGL_V)) {
188
 
            mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaStatus);
189
 
            snd_pcm_status_dump(status, ai->alsa.log);
190
 
        }
191
 
        if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
192
 
            mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError, snd_strerror(res));
193
 
            return -1;
194
 
        }
195
 
        return 0;               /* ok, data should be accepted again */
196
 
    }
197
 
    mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError);
198
 
    return -1;
199
 
}