~ubuntu-branches/ubuntu/raring/libav/raring-security

« back to all changes in this revision

Viewing changes to .pc/post-0.7.1/0016-alsa-limit-buffer_size-to-32768-frames.patch/libavdevice/alsa-audio-common.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2011-09-28 09:18:34 UTC
  • mfrom: (1.3.7 sid)
  • Revision ID: package-import@ubuntu.com-20110928091834-w415mnuh06h4zpvc
Tags: 4:0.7.1-7ubuntu2
Revert "Convert package to include multiarch support."

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Libav.
 
7
 *
 
8
 * Libav 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
 * Libav 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 Libav; 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_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
 
40
        case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
 
41
        case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
 
42
        case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
 
43
        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
 
44
        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
 
45
        case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
 
46
        case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
 
47
        case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
 
48
        case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
 
49
        case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
 
50
        case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
 
51
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
 
52
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
 
53
        case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
 
54
        case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
 
55
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
 
56
        case CODEC_ID_PCM_U8:    return SND_PCM_FORMAT_U8;
 
57
        case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
 
58
        case CODEC_ID_PCM_ALAW:  return SND_PCM_FORMAT_A_LAW;
 
59
        default:                 return SND_PCM_FORMAT_UNKNOWN;
 
60
    }
 
61
}
 
62
 
 
63
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
 
64
                         unsigned int *sample_rate,
 
65
                         int channels, enum CodecID *codec_id)
 
66
{
 
67
    AlsaData *s = ctx->priv_data;
 
68
    const char *audio_device;
 
69
    int res, flags = 0;
 
70
    snd_pcm_format_t format;
 
71
    snd_pcm_t *h;
 
72
    snd_pcm_hw_params_t *hw_params;
 
73
    snd_pcm_uframes_t buffer_size, period_size;
 
74
 
 
75
    if (ctx->filename[0] == 0) audio_device = "default";
 
76
    else                       audio_device = ctx->filename;
 
77
 
 
78
    if (*codec_id == CODEC_ID_NONE)
 
79
        *codec_id = DEFAULT_CODEC_ID;
 
80
    format = codec_id_to_pcm_format(*codec_id);
 
81
    if (format == SND_PCM_FORMAT_UNKNOWN) {
 
82
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
 
83
        return AVERROR(ENOSYS);
 
84
    }
 
85
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
 
86
 
 
87
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
 
88
        flags = SND_PCM_NONBLOCK;
 
89
    }
 
90
    res = snd_pcm_open(&h, audio_device, mode, flags);
 
91
    if (res < 0) {
 
92
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
 
93
               audio_device, snd_strerror(res));
 
94
        return AVERROR(EIO);
 
95
    }
 
96
 
 
97
    res = snd_pcm_hw_params_malloc(&hw_params);
 
98
    if (res < 0) {
 
99
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
 
100
               snd_strerror(res));
 
101
        goto fail1;
 
102
    }
 
103
 
 
104
    res = snd_pcm_hw_params_any(h, hw_params);
 
105
    if (res < 0) {
 
106
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
 
107
               snd_strerror(res));
 
108
        goto fail;
 
109
    }
 
110
 
 
111
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
 
112
    if (res < 0) {
 
113
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
 
114
               snd_strerror(res));
 
115
        goto fail;
 
116
    }
 
117
 
 
118
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
 
119
    if (res < 0) {
 
120
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
 
121
               *codec_id, format, snd_strerror(res));
 
122
        goto fail;
 
123
    }
 
124
 
 
125
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
 
126
    if (res < 0) {
 
127
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
 
128
               snd_strerror(res));
 
129
        goto fail;
 
130
    }
 
131
 
 
132
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
 
133
    if (res < 0) {
 
134
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
 
135
               channels, snd_strerror(res));
 
136
        goto fail;
 
137
    }
 
138
 
 
139
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
 
140
    /* TODO: maybe use ctx->max_picture_buffer somehow */
 
141
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
 
142
    if (res < 0) {
 
143
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
 
144
               snd_strerror(res));
 
145
        goto fail;
 
146
    }
 
147
 
 
148
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
 
149
    if (!period_size)
 
150
        period_size = buffer_size / 4;
 
151
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
 
152
    if (res < 0) {
 
153
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
 
154
               snd_strerror(res));
 
155
        goto fail;
 
156
    }
 
157
    s->period_size = period_size;
 
158
 
 
159
    res = snd_pcm_hw_params(h, hw_params);
 
160
    if (res < 0) {
 
161
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
 
162
               snd_strerror(res));
 
163
        goto fail;
 
164
    }
 
165
 
 
166
    snd_pcm_hw_params_free(hw_params);
 
167
 
 
168
    s->h = h;
 
169
    return 0;
 
170
 
 
171
fail:
 
172
    snd_pcm_hw_params_free(hw_params);
 
173
fail1:
 
174
    snd_pcm_close(h);
 
175
    return AVERROR(EIO);
 
176
}
 
177
 
 
178
av_cold int ff_alsa_close(AVFormatContext *s1)
 
179
{
 
180
    AlsaData *s = s1->priv_data;
 
181
 
 
182
    snd_pcm_close(s->h);
 
183
    return 0;
 
184
}
 
185
 
 
186
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
 
187
{
 
188
    AlsaData *s = s1->priv_data;
 
189
    snd_pcm_t *handle = s->h;
 
190
 
 
191
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
 
192
    if (err == -EPIPE) {
 
193
        err = snd_pcm_prepare(handle);
 
194
        if (err < 0) {
 
195
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
 
196
 
 
197
            return AVERROR(EIO);
 
198
        }
 
199
    } else if (err == -ESTRPIPE) {
 
200
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
 
201
 
 
202
        return -1;
 
203
    }
 
204
    return err;
 
205
}