Libav
|
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 FFmpeg. 00007 * 00008 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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_S16LE: return SND_PCM_FORMAT_S16_LE; 00040 case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE; 00041 case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8; 00042 default: return SND_PCM_FORMAT_UNKNOWN; 00043 } 00044 } 00045 00046 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, 00047 unsigned int *sample_rate, 00048 int channels, enum CodecID *codec_id) 00049 { 00050 AlsaData *s = ctx->priv_data; 00051 const char *audio_device; 00052 int res, flags = 0; 00053 snd_pcm_format_t format; 00054 snd_pcm_t *h; 00055 snd_pcm_hw_params_t *hw_params; 00056 snd_pcm_uframes_t buffer_size, period_size; 00057 00058 if (ctx->filename[0] == 0) audio_device = "default"; 00059 else audio_device = ctx->filename; 00060 00061 if (*codec_id == CODEC_ID_NONE) 00062 *codec_id = DEFAULT_CODEC_ID; 00063 format = codec_id_to_pcm_format(*codec_id); 00064 if (format == SND_PCM_FORMAT_UNKNOWN) { 00065 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id); 00066 return AVERROR(ENOSYS); 00067 } 00068 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels; 00069 00070 if (ctx->flags & AVFMT_FLAG_NONBLOCK) { 00071 flags = SND_PCM_NONBLOCK; 00072 } 00073 res = snd_pcm_open(&h, audio_device, mode, flags); 00074 if (res < 0) { 00075 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n", 00076 audio_device, snd_strerror(res)); 00077 return AVERROR(EIO); 00078 } 00079 00080 res = snd_pcm_hw_params_malloc(&hw_params); 00081 if (res < 0) { 00082 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n", 00083 snd_strerror(res)); 00084 goto fail1; 00085 } 00086 00087 res = snd_pcm_hw_params_any(h, hw_params); 00088 if (res < 0) { 00089 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n", 00090 snd_strerror(res)); 00091 goto fail; 00092 } 00093 00094 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); 00095 if (res < 0) { 00096 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n", 00097 snd_strerror(res)); 00098 goto fail; 00099 } 00100 00101 res = snd_pcm_hw_params_set_format(h, hw_params, format); 00102 if (res < 0) { 00103 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n", 00104 *codec_id, format, snd_strerror(res)); 00105 goto fail; 00106 } 00107 00108 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0); 00109 if (res < 0) { 00110 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n", 00111 snd_strerror(res)); 00112 goto fail; 00113 } 00114 00115 res = snd_pcm_hw_params_set_channels(h, hw_params, channels); 00116 if (res < 0) { 00117 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n", 00118 channels, snd_strerror(res)); 00119 goto fail; 00120 } 00121 00122 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size); 00123 /* TODO: maybe use ctx->max_picture_buffer somehow */ 00124 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size); 00125 if (res < 0) { 00126 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n", 00127 snd_strerror(res)); 00128 goto fail; 00129 } 00130 00131 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL); 00132 if (!period_size) 00133 period_size = buffer_size / 4; 00134 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL); 00135 if (res < 0) { 00136 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n", 00137 snd_strerror(res)); 00138 goto fail; 00139 } 00140 s->period_size = period_size; 00141 00142 res = snd_pcm_hw_params(h, hw_params); 00143 if (res < 0) { 00144 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n", 00145 snd_strerror(res)); 00146 goto fail; 00147 } 00148 00149 snd_pcm_hw_params_free(hw_params); 00150 00151 s->h = h; 00152 return 0; 00153 00154 fail: 00155 snd_pcm_hw_params_free(hw_params); 00156 fail1: 00157 snd_pcm_close(h); 00158 return AVERROR(EIO); 00159 } 00160 00161 av_cold int ff_alsa_close(AVFormatContext *s1) 00162 { 00163 AlsaData *s = s1->priv_data; 00164 00165 snd_pcm_close(s->h); 00166 return 0; 00167 } 00168 00169 int ff_alsa_xrun_recover(AVFormatContext *s1, int err) 00170 { 00171 AlsaData *s = s1->priv_data; 00172 snd_pcm_t *handle = s->h; 00173 00174 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n"); 00175 if (err == -EPIPE) { 00176 err = snd_pcm_prepare(handle); 00177 if (err < 0) { 00178 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err)); 00179 00180 return AVERROR(EIO); 00181 } 00182 } else if (err == -ESTRPIPE) { 00183 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n"); 00184 00185 return -1; 00186 } 00187 return err; 00188 }