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 libavdevice/alsa-audio-common.c 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 "libavformat/avformat.h" 32#include <alsa/asoundlib.h> 33 34#include "alsa-audio.h" 35 36static 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 46av_cold int ff_alsa_open(AVFormatContext *ctx, int mode, 47 unsigned int *sample_rate, 48 int channels, int *codec_id) 49{ 50 AlsaData *s = ctx->priv_data; 51 const char *audio_device; 52 int res, flags = 0; 53 snd_pcm_format_t format; 54 snd_pcm_t *h; 55 snd_pcm_hw_params_t *hw_params; 56 snd_pcm_uframes_t buffer_size, period_size; 57 58 if (ctx->filename[0] == 0) audio_device = "default"; 59 else audio_device = ctx->filename; 60 61 if (*codec_id == CODEC_ID_NONE) 62 *codec_id = DEFAULT_CODEC_ID; 63 format = codec_id_to_pcm_format(*codec_id); 64 if (format == SND_PCM_FORMAT_UNKNOWN) { 65 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id); 66 return AVERROR(ENOSYS); 67 } 68 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels; 69 70 if (ctx->flags & AVFMT_FLAG_NONBLOCK) { 71 flags = O_NONBLOCK; 72 } 73 res = snd_pcm_open(&h, audio_device, mode, flags); 74 if (res < 0) { 75 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n", 76 audio_device, snd_strerror(res)); 77 return AVERROR_IO; 78 } 79 80 res = snd_pcm_hw_params_malloc(&hw_params); 81 if (res < 0) { 82 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n", 83 snd_strerror(res)); 84 goto fail1; 85 } 86 87 res = snd_pcm_hw_params_any(h, hw_params); 88 if (res < 0) { 89 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n", 90 snd_strerror(res)); 91 goto fail; 92 } 93 94 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); 95 if (res < 0) { 96 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n", 97 snd_strerror(res)); 98 goto fail; 99 } 100 101 res = snd_pcm_hw_params_set_format(h, hw_params, format); 102 if (res < 0) { 103 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n", 104 *codec_id, format, snd_strerror(res)); 105 goto fail; 106 } 107 108 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0); 109 if (res < 0) { 110 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n", 111 snd_strerror(res)); 112 goto fail; 113 } 114 115 res = snd_pcm_hw_params_set_channels(h, hw_params, channels); 116 if (res < 0) { 117 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n", 118 channels, snd_strerror(res)); 119 goto fail; 120 } 121 122 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size); 123 /* TODO: maybe use ctx->max_picture_buffer somehow */ 124 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size); 125 if (res < 0) { 126 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n", 127 snd_strerror(res)); 128 goto fail; 129 } 130 131 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL); 132 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL); 133 if (res < 0) { 134 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n", 135 snd_strerror(res)); 136 goto fail; 137 } 138 s->period_size = period_size; 139 140 res = snd_pcm_hw_params(h, hw_params); 141 if (res < 0) { 142 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n", 143 snd_strerror(res)); 144 goto fail; 145 } 146 147 snd_pcm_hw_params_free(hw_params); 148 149 s->h = h; 150 return 0; 151 152fail: 153 snd_pcm_hw_params_free(hw_params); 154fail1: 155 snd_pcm_close(h); 156 return AVERROR_IO; 157} 158 159av_cold int ff_alsa_close(AVFormatContext *s1) 160{ 161 AlsaData *s = s1->priv_data; 162 163 snd_pcm_close(s->h); 164 return 0; 165} 166 167int ff_alsa_xrun_recover(AVFormatContext *s1, int err) 168{ 169 AlsaData *s = s1->priv_data; 170 snd_pcm_t *handle = s->h; 171 172 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n"); 173 if (err == -EPIPE) { 174 err = snd_pcm_prepare(handle); 175 if (err < 0) { 176 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err)); 177 178 return AVERROR_IO; 179 } 180 } else if (err == -ESTRPIPE) { 181 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n"); 182 183 return -1; 184 } 185 return err; 186} 187