Libav 0.7.1
|
00001 /* 00002 * ALSA input and output 00003 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) 00004 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00031 #include <alsa/asoundlib.h> 00032 #include "libavformat/avformat.h" 00033 00034 #include "alsa-audio.h" 00035 00036 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id) 00037 { 00038 switch(codec_id) { 00039 case CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE; 00040 case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE; 00041 case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE; 00042 case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE; 00043 case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE; 00044 case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE; 00045 case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE; 00046 case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE; 00047 case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE; 00048 case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE; 00049 case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE; 00050 case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE; 00051 case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE; 00052 case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE; 00053 case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE; 00054 case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE; 00055 case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8; 00056 case CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8; 00057 case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW; 00058 case CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW; 00059 default: return SND_PCM_FORMAT_UNKNOWN; 00060 } 00061 } 00062 00063 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, 00064 unsigned int *sample_rate, 00065 int channels, enum CodecID *codec_id) 00066 { 00067 AlsaData *s = ctx->priv_data; 00068 const char *audio_device; 00069 int res, flags = 0; 00070 snd_pcm_format_t format; 00071 snd_pcm_t *h; 00072 snd_pcm_hw_params_t *hw_params; 00073 snd_pcm_uframes_t buffer_size, period_size; 00074 00075 if (ctx->filename[0] == 0) audio_device = "default"; 00076 else audio_device = ctx->filename; 00077 00078 if (*codec_id == CODEC_ID_NONE) 00079 *codec_id = DEFAULT_CODEC_ID; 00080 format = codec_id_to_pcm_format(*codec_id); 00081 if (format == SND_PCM_FORMAT_UNKNOWN) { 00082 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id); 00083 return AVERROR(ENOSYS); 00084 } 00085 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels; 00086 00087 if (ctx->flags & AVFMT_FLAG_NONBLOCK) { 00088 flags = SND_PCM_NONBLOCK; 00089 } 00090 res = snd_pcm_open(&h, audio_device, mode, flags); 00091 if (res < 0) { 00092 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n", 00093 audio_device, snd_strerror(res)); 00094 return AVERROR(EIO); 00095 } 00096 00097 res = snd_pcm_hw_params_malloc(&hw_params); 00098 if (res < 0) { 00099 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n", 00100 snd_strerror(res)); 00101 goto fail1; 00102 } 00103 00104 res = snd_pcm_hw_params_any(h, hw_params); 00105 if (res < 0) { 00106 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n", 00107 snd_strerror(res)); 00108 goto fail; 00109 } 00110 00111 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); 00112 if (res < 0) { 00113 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n", 00114 snd_strerror(res)); 00115 goto fail; 00116 } 00117 00118 res = snd_pcm_hw_params_set_format(h, hw_params, format); 00119 if (res < 0) { 00120 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n", 00121 *codec_id, format, snd_strerror(res)); 00122 goto fail; 00123 } 00124 00125 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0); 00126 if (res < 0) { 00127 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n", 00128 snd_strerror(res)); 00129 goto fail; 00130 } 00131 00132 res = snd_pcm_hw_params_set_channels(h, hw_params, channels); 00133 if (res < 0) { 00134 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n", 00135 channels, snd_strerror(res)); 00136 goto fail; 00137 } 00138 00139 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size); 00140 buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX); 00141 /* TODO: maybe use ctx->max_picture_buffer somehow */ 00142 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size); 00143 if (res < 0) { 00144 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n", 00145 snd_strerror(res)); 00146 goto fail; 00147 } 00148 00149 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL); 00150 if (!period_size) 00151 period_size = buffer_size / 4; 00152 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL); 00153 if (res < 0) { 00154 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n", 00155 snd_strerror(res)); 00156 goto fail; 00157 } 00158 s->period_size = period_size; 00159 00160 res = snd_pcm_hw_params(h, hw_params); 00161 if (res < 0) { 00162 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n", 00163 snd_strerror(res)); 00164 goto fail; 00165 } 00166 00167 snd_pcm_hw_params_free(hw_params); 00168 00169 s->h = h; 00170 return 0; 00171 00172 fail: 00173 snd_pcm_hw_params_free(hw_params); 00174 fail1: 00175 snd_pcm_close(h); 00176 return AVERROR(EIO); 00177 } 00178 00179 av_cold int ff_alsa_close(AVFormatContext *s1) 00180 { 00181 AlsaData *s = s1->priv_data; 00182 00183 snd_pcm_close(s->h); 00184 return 0; 00185 } 00186 00187 int ff_alsa_xrun_recover(AVFormatContext *s1, int err) 00188 { 00189 AlsaData *s = s1->priv_data; 00190 snd_pcm_t *handle = s->h; 00191 00192 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n"); 00193 if (err == -EPIPE) { 00194 err = snd_pcm_prepare(handle); 00195 if (err < 0) { 00196 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err)); 00197 00198 return AVERROR(EIO); 00199 } 00200 } else if (err == -ESTRPIPE) { 00201 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n"); 00202 00203 return -1; 00204 } 00205 return err; 00206 }