Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 4d1d6f7a

History | View | Annotate | Download (8.98 KB)

1
/*
2
 * ALSA input and output
3
 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4
 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file
25
 * ALSA input and output: common code
26
 * @author Luca Abeni ( lucabe72 email it )
27
 * @author Benoit Fouet ( benoit fouet free fr )
28
 * @author Nicolas George ( nicolas george normalesup org )
29
 */
30

    
31
#include <alsa/asoundlib.h>
32
#include "libavformat/avformat.h"
33

    
34
#include "alsa-audio.h"
35

    
36
static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
37
{
38
    switch(codec_id) {
39
        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
40
        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
41
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
42
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
43
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
44
        default:                 return SND_PCM_FORMAT_UNKNOWN;
45
    }
46
}
47

    
48
#define REORDER_OUT_51(NAME, TYPE) \
49
static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
50
{ \
51
    const TYPE *in = in_v; \
52
    TYPE * out = out_v; \
53
\
54
    while (n-- > 0) { \
55
        out[0] = in[0]; \
56
        out[1] = in[1]; \
57
        out[2] = in[4]; \
58
        out[3] = in[5]; \
59
        out[4] = in[2]; \
60
        out[5] = in[3]; \
61
        in  += 6; \
62
        out += 6; \
63
    } \
64
}
65

    
66
#define REORDER_OUT_71(NAME, TYPE) \
67
static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
68
{ \
69
    const TYPE *in = in_v; \
70
    TYPE * out = out_v; \
71
\
72
    while (n-- > 0) { \
73
        out[0] = in[0]; \
74
        out[1] = in[1]; \
75
        out[2] = in[4]; \
76
        out[3] = in[5]; \
77
        out[4] = in[2]; \
78
        out[5] = in[3]; \
79
        out[6] = in[6]; \
80
        out[7] = in[7]; \
81
        in  += 8; \
82
        out += 8; \
83
    } \
84
}
85

    
86
REORDER_OUT_51(s16, int16_t)
87
REORDER_OUT_71(s16, int16_t)
88
REORDER_OUT_51(s32, int32_t)
89
REORDER_OUT_71(s32, int32_t)
90

    
91
#define REORDER_DUMMY ((void *)1)
92

    
93
static av_cold ff_reorder_func find_reorder_func(int codec_id,
94
                                                 int64_t layout,
95
                                                 int out)
96
{
97
    return
98
    codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
99
        layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
100
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
101
            out ? alsa_reorder_s16_out_51 : NULL :
102
        layout == AV_CH_LAYOUT_7POINT1 ?
103
            out ? alsa_reorder_s16_out_71 : NULL :
104
            NULL :
105
    codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
106
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
107
            out ? alsa_reorder_s32_out_51 : NULL :
108
        layout == AV_CH_LAYOUT_7POINT1 ?
109
            out ? alsa_reorder_s32_out_71 : NULL :
110
           NULL :
111
        NULL;
112
}
113

    
114
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
115
                         unsigned int *sample_rate,
116
                         int channels, enum CodecID *codec_id)
117
{
118
    AlsaData *s = ctx->priv_data;
119
    const char *audio_device;
120
    int res, flags = 0;
121
    snd_pcm_format_t format;
122
    snd_pcm_t *h;
123
    snd_pcm_hw_params_t *hw_params;
124
    snd_pcm_uframes_t buffer_size, period_size;
125
    int64_t layout = ctx->streams[0]->codec->channel_layout;
126

    
127
    if (ctx->filename[0] == 0) audio_device = "default";
128
    else                       audio_device = ctx->filename;
129

    
130
    if (*codec_id == CODEC_ID_NONE)
131
        *codec_id = DEFAULT_CODEC_ID;
132
    format = codec_id_to_pcm_format(*codec_id);
133
    if (format == SND_PCM_FORMAT_UNKNOWN) {
134
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
135
        return AVERROR(ENOSYS);
136
    }
137
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
138

    
139
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
140
        flags = SND_PCM_NONBLOCK;
141
    }
142
    res = snd_pcm_open(&h, audio_device, mode, flags);
143
    if (res < 0) {
144
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
145
               audio_device, snd_strerror(res));
146
        return AVERROR(EIO);
147
    }
148

    
149
    res = snd_pcm_hw_params_malloc(&hw_params);
150
    if (res < 0) {
151
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
152
               snd_strerror(res));
153
        goto fail1;
154
    }
155

    
156
    res = snd_pcm_hw_params_any(h, hw_params);
157
    if (res < 0) {
158
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
159
               snd_strerror(res));
160
        goto fail;
161
    }
162

    
163
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
164
    if (res < 0) {
165
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
166
               snd_strerror(res));
167
        goto fail;
168
    }
169

    
170
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
171
    if (res < 0) {
172
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
173
               *codec_id, format, snd_strerror(res));
174
        goto fail;
175
    }
176

    
177
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
178
    if (res < 0) {
179
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
180
               snd_strerror(res));
181
        goto fail;
182
    }
183

    
184
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
185
    if (res < 0) {
186
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
187
               channels, snd_strerror(res));
188
        goto fail;
189
    }
190

    
191
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
192
    /* TODO: maybe use ctx->max_picture_buffer somehow */
193
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
194
    if (res < 0) {
195
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
196
               snd_strerror(res));
197
        goto fail;
198
    }
199

    
200
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
201
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
202
    if (res < 0) {
203
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
204
               snd_strerror(res));
205
        goto fail;
206
    }
207
    s->period_size = period_size;
208

    
209
    res = snd_pcm_hw_params(h, hw_params);
210
    if (res < 0) {
211
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
212
               snd_strerror(res));
213
        goto fail;
214
    }
215

    
216
    snd_pcm_hw_params_free(hw_params);
217

    
218
    if (channels > 2 && layout) {
219
        s->reorder_func = find_reorder_func(*codec_id, layout,
220
                                            mode == SND_PCM_STREAM_PLAYBACK);
221
        if (s->reorder_func == REORDER_DUMMY) {
222
            s->reorder_func = NULL;
223
        } else if (s->reorder_func) {
224
            s->reorder_buf_size = buffer_size;
225
            s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
226
            if (!s->reorder_buf)
227
                goto fail1;
228
        } else {
229
            char name[16];
230
            av_get_channel_layout_string(name, sizeof(name), channels, layout);
231
            av_log(ctx, AV_LOG_WARNING,
232
                   "ALSA channel layout unknown or unimplemented for %s %s.\n",
233
                   name,
234
                   mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
235
        }
236
    }
237

    
238
    s->h = h;
239
    return 0;
240

    
241
fail:
242
    snd_pcm_hw_params_free(hw_params);
243
fail1:
244
    snd_pcm_close(h);
245
    return AVERROR(EIO);
246
}
247

    
248
av_cold int ff_alsa_close(AVFormatContext *s1)
249
{
250
    AlsaData *s = s1->priv_data;
251

    
252
    av_freep(&s->reorder_buf);
253
    snd_pcm_close(s->h);
254
    return 0;
255
}
256

    
257
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
258
{
259
    AlsaData *s = s1->priv_data;
260
    snd_pcm_t *handle = s->h;
261

    
262
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
263
    if (err == -EPIPE) {
264
        err = snd_pcm_prepare(handle);
265
        if (err < 0) {
266
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
267

    
268
            return AVERROR(EIO);
269
        }
270
    } else if (err == -ESTRPIPE) {
271
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
272

    
273
        return -1;
274
    }
275
    return err;
276
}
277

    
278
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
279
{
280
    int size = s->reorder_buf_size;
281
    void *r;
282

    
283
    while (size < min_size)
284
        size *= 2;
285
    r = av_realloc(s->reorder_buf, size * s->frame_size);
286
    if (!r)
287
        return AVERROR(ENOMEM);
288
    s->reorder_buf = r;
289
    s->reorder_buf_size = size;
290
    return 0;
291
}