yading@10: /* yading@10: * ALSA input and output yading@10: * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) yading@10: * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * ALSA input and output: output yading@10: * @author Luca Abeni ( lucabe72 email it ) yading@10: * @author Benoit Fouet ( benoit fouet free fr ) yading@10: * yading@10: * This avdevice encoder allows to play audio to an ALSA (Advanced Linux yading@10: * Sound Architecture) device. yading@10: * yading@10: * The filename parameter is the name of an ALSA PCM device capable of yading@10: * capture, for example "default" or "plughw:1"; see the ALSA documentation yading@10: * for naming conventions. The empty string is equivalent to "default". yading@10: * yading@10: * The playback period is set to the lower value available for the device, yading@10: * which gives a low latency suitable for real-time playback. yading@10: */ yading@10: yading@10: #include yading@10: yading@10: #include "libavutil/time.h" yading@10: #include "libavformat/internal.h" yading@10: #include "avdevice.h" yading@10: #include "alsa-audio.h" yading@10: yading@10: static av_cold int audio_write_header(AVFormatContext *s1) yading@10: { yading@10: AlsaData *s = s1->priv_data; yading@10: AVStream *st; yading@10: unsigned int sample_rate; yading@10: enum AVCodecID codec_id; yading@10: int res; yading@10: yading@10: st = s1->streams[0]; yading@10: sample_rate = st->codec->sample_rate; yading@10: codec_id = st->codec->codec_id; yading@10: res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate, yading@10: st->codec->channels, &codec_id); yading@10: if (sample_rate != st->codec->sample_rate) { yading@10: av_log(s1, AV_LOG_ERROR, yading@10: "sample rate %d not available, nearest is %d\n", yading@10: st->codec->sample_rate, sample_rate); yading@10: goto fail; yading@10: } yading@10: avpriv_set_pts_info(st, 64, 1, sample_rate); yading@10: yading@10: return res; yading@10: yading@10: fail: yading@10: snd_pcm_close(s->h); yading@10: return AVERROR(EIO); yading@10: } yading@10: yading@10: static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) yading@10: { yading@10: AlsaData *s = s1->priv_data; yading@10: int res; yading@10: int size = pkt->size; yading@10: uint8_t *buf = pkt->data; yading@10: yading@10: size /= s->frame_size; yading@10: if (s->reorder_func) { yading@10: if (size > s->reorder_buf_size) yading@10: if (ff_alsa_extend_reorder_buf(s, size)) yading@10: return AVERROR(ENOMEM); yading@10: s->reorder_func(buf, s->reorder_buf, size); yading@10: buf = s->reorder_buf; yading@10: } yading@10: while ((res = snd_pcm_writei(s->h, buf, size)) < 0) { yading@10: if (res == -EAGAIN) { yading@10: yading@10: return AVERROR(EAGAIN); yading@10: } yading@10: yading@10: if (ff_alsa_xrun_recover(s1, res) < 0) { yading@10: av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n", yading@10: snd_strerror(res)); yading@10: yading@10: return AVERROR(EIO); yading@10: } yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static void yading@10: audio_get_output_timestamp(AVFormatContext *s1, int stream, yading@10: int64_t *dts, int64_t *wall) yading@10: { yading@10: AlsaData *s = s1->priv_data; yading@10: snd_pcm_sframes_t delay = 0; yading@10: *wall = av_gettime(); yading@10: snd_pcm_delay(s->h, &delay); yading@10: *dts = s1->streams[0]->cur_dts - delay; yading@10: } yading@10: yading@10: AVOutputFormat ff_alsa_muxer = { yading@10: .name = "alsa", yading@10: .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"), yading@10: .priv_data_size = sizeof(AlsaData), yading@10: .audio_codec = DEFAULT_CODEC_ID, yading@10: .video_codec = AV_CODEC_ID_NONE, yading@10: .write_header = audio_write_header, yading@10: .write_packet = audio_write_packet, yading@10: .write_trailer = ff_alsa_close, yading@10: .get_output_timestamp = audio_get_output_timestamp, yading@10: .flags = AVFMT_NOFILE, yading@10: };