Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 813dbb44

History | View | Annotate | Download (8.27 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_S16LE: return SND_PCM_FORMAT_S16_LE;
40
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
41
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
42
        default:                 return SND_PCM_FORMAT_UNKNOWN;
43
    }
44
}
45

    
46
static void alsa_reorder_s16_out_51(const void *in_v, void *out_v, int n)
47
{
48
    const int16_t *in = in_v;
49
    int16_t *out = out_v;
50

    
51
    while (n-- > 0) {
52
        out[0] = in[0];
53
        out[1] = in[1];
54
        out[2] = in[4];
55
        out[3] = in[5];
56
        out[4] = in[2];
57
        out[5] = in[3];
58
        in  += 6;
59
        out += 6;
60
    }
61
}
62

    
63
static void alsa_reorder_s16_out_71(const void *in_v, void *out_v, int n)
64
{
65
    const int16_t *in = in_v;
66
    int16_t *out = out_v;
67

    
68
    while (n-- > 0) {
69
        out[0] = in[0];
70
        out[1] = in[1];
71
        out[2] = in[4];
72
        out[3] = in[5];
73
        out[4] = in[2];
74
        out[5] = in[3];
75
        out[6] = in[6];
76
        out[7] = in[7];
77
        in  += 8;
78
        out += 8;
79
    }
80
}
81

    
82
#define REORDER_DUMMY ((void *)1)
83

    
84
static av_cold ff_reorder_func find_reorder_func(int codec_id,
85
                                                 int64_t layout,
86
                                                 int out)
87
{
88
    return
89
    codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
90
        layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
91
        layout == AV_CH_LAYOUT_5POINT1_BACK ?
92
            out ? alsa_reorder_s16_out_51 : NULL :
93
        layout == AV_CH_LAYOUT_7POINT1 ?
94
            out ? alsa_reorder_s16_out_71 : NULL :
95
            NULL :
96
        NULL;
97
}
98

    
99
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
100
                         unsigned int *sample_rate,
101
                         int channels, enum CodecID *codec_id)
102
{
103
    AlsaData *s = ctx->priv_data;
104
    const char *audio_device;
105
    int res, flags = 0;
106
    snd_pcm_format_t format;
107
    snd_pcm_t *h;
108
    snd_pcm_hw_params_t *hw_params;
109
    snd_pcm_uframes_t buffer_size, period_size;
110
    int64_t layout = ctx->streams[0]->codec->channel_layout;
111

    
112
    if (ctx->filename[0] == 0) audio_device = "default";
113
    else                       audio_device = ctx->filename;
114

    
115
    if (*codec_id == CODEC_ID_NONE)
116
        *codec_id = DEFAULT_CODEC_ID;
117
    format = codec_id_to_pcm_format(*codec_id);
118
    if (format == SND_PCM_FORMAT_UNKNOWN) {
119
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
120
        return AVERROR(ENOSYS);
121
    }
122
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
123

    
124
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
125
        flags = SND_PCM_NONBLOCK;
126
    }
127
    res = snd_pcm_open(&h, audio_device, mode, flags);
128
    if (res < 0) {
129
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
130
               audio_device, snd_strerror(res));
131
        return AVERROR(EIO);
132
    }
133

    
134
    res = snd_pcm_hw_params_malloc(&hw_params);
135
    if (res < 0) {
136
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
137
               snd_strerror(res));
138
        goto fail1;
139
    }
140

    
141
    res = snd_pcm_hw_params_any(h, hw_params);
142
    if (res < 0) {
143
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
144
               snd_strerror(res));
145
        goto fail;
146
    }
147

    
148
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
149
    if (res < 0) {
150
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
151
               snd_strerror(res));
152
        goto fail;
153
    }
154

    
155
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
156
    if (res < 0) {
157
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
158
               *codec_id, format, snd_strerror(res));
159
        goto fail;
160
    }
161

    
162
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
163
    if (res < 0) {
164
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
165
               snd_strerror(res));
166
        goto fail;
167
    }
168

    
169
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
170
    if (res < 0) {
171
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
172
               channels, snd_strerror(res));
173
        goto fail;
174
    }
175

    
176
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
177
    /* TODO: maybe use ctx->max_picture_buffer somehow */
178
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
179
    if (res < 0) {
180
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
181
               snd_strerror(res));
182
        goto fail;
183
    }
184

    
185
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
186
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
187
    if (res < 0) {
188
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
189
               snd_strerror(res));
190
        goto fail;
191
    }
192
    s->period_size = period_size;
193

    
194
    res = snd_pcm_hw_params(h, hw_params);
195
    if (res < 0) {
196
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
197
               snd_strerror(res));
198
        goto fail;
199
    }
200

    
201
    snd_pcm_hw_params_free(hw_params);
202

    
203
    if (channels > 2 && layout) {
204
        s->reorder_func = find_reorder_func(*codec_id, layout,
205
                                            mode == SND_PCM_STREAM_PLAYBACK);
206
        if (s->reorder_func == REORDER_DUMMY) {
207
            s->reorder_func = NULL;
208
        } else if (s->reorder_func) {
209
            s->reorder_buf_size = buffer_size;
210
            s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
211
            if (!s->reorder_buf)
212
                goto fail1;
213
        } else {
214
            char name[16];
215
            av_get_channel_layout_string(name, sizeof(name), channels, layout);
216
            av_log(ctx, AV_LOG_WARNING,
217
                   "ALSA channel layout unknown or unimplemented for %s %s.\n",
218
                   name,
219
                   mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
220
        }
221
    }
222

    
223
    s->h = h;
224
    return 0;
225

    
226
fail:
227
    snd_pcm_hw_params_free(hw_params);
228
fail1:
229
    snd_pcm_close(h);
230
    return AVERROR(EIO);
231
}
232

    
233
av_cold int ff_alsa_close(AVFormatContext *s1)
234
{
235
    AlsaData *s = s1->priv_data;
236

    
237
    av_freep(s->reorder_buf);
238
    snd_pcm_close(s->h);
239
    return 0;
240
}
241

    
242
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
243
{
244
    AlsaData *s = s1->priv_data;
245
    snd_pcm_t *handle = s->h;
246

    
247
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
248
    if (err == -EPIPE) {
249
        err = snd_pcm_prepare(handle);
250
        if (err < 0) {
251
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
252

    
253
            return AVERROR(EIO);
254
        }
255
    } else if (err == -ESTRPIPE) {
256
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
257

    
258
        return -1;
259
    }
260
    return err;
261
}
262

    
263
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
264
{
265
    int size = s->reorder_buf_size;
266
    void *r;
267

    
268
    while (size < min_size)
269
        size *= 2;
270
    r = av_realloc(s->reorder_buf, size * s->frame_size);
271
    if (!r)
272
        return AVERROR(ENOMEM);
273
    s->reorder_buf = r;
274
    s->reorder_buf_size = size;
275
    return 0;
276
}