Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 2ed05f65

History | View | Annotate | Download (9.11 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_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
40
        case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
41
        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
42
        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
43
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
44
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
45
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
46
        default:                 return SND_PCM_FORMAT_UNKNOWN;
47
    }
48
}
49

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

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

    
88
REORDER_OUT_51(s16, int16_t)
89
REORDER_OUT_71(s16, int16_t)
90
REORDER_OUT_51(s32, int32_t)
91
REORDER_OUT_71(s32, int32_t)
92

    
93
#define REORDER_DUMMY ((void *)1)
94

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
218
    snd_pcm_hw_params_free(hw_params);
219

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

    
240
    s->h = h;
241
    return 0;
242

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

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

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

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

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

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

    
275
        return -1;
276
    }
277
    return err;
278
}
279

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

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