• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavdevice/alsa-audio-common.c

Go to the documentation of this file.
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     res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
00133     if (res < 0) {
00134         av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
00135                snd_strerror(res));
00136         goto fail;
00137     }
00138     s->period_size = period_size;
00139 
00140     res = snd_pcm_hw_params(h, hw_params);
00141     if (res < 0) {
00142         av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
00143                snd_strerror(res));
00144         goto fail;
00145     }
00146 
00147     snd_pcm_hw_params_free(hw_params);
00148 
00149     s->h = h;
00150     return 0;
00151 
00152 fail:
00153     snd_pcm_hw_params_free(hw_params);
00154 fail1:
00155     snd_pcm_close(h);
00156     return AVERROR(EIO);
00157 }
00158 
00159 av_cold int ff_alsa_close(AVFormatContext *s1)
00160 {
00161     AlsaData *s = s1->priv_data;
00162 
00163     snd_pcm_close(s->h);
00164     return 0;
00165 }
00166 
00167 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
00168 {
00169     AlsaData *s = s1->priv_data;
00170     snd_pcm_t *handle = s->h;
00171 
00172     av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
00173     if (err == -EPIPE) {
00174         err = snd_pcm_prepare(handle);
00175         if (err < 0) {
00176             av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
00177 
00178             return AVERROR(EIO);
00179         }
00180     } else if (err == -ESTRPIPE) {
00181         av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
00182 
00183         return -1;
00184     }
00185     return err;
00186 }

Generated on Fri Sep 16 2011 17:17:47 for FFmpeg by  doxygen 1.7.1