annotate ffmpeg/libavdevice/alsa-audio-enc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * ALSA input and output
yading@10 3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
yading@10 4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * ALSA input and output: output
yading@10 26 * @author Luca Abeni ( lucabe72 email it )
yading@10 27 * @author Benoit Fouet ( benoit fouet free fr )
yading@10 28 *
yading@10 29 * This avdevice encoder allows to play audio to an ALSA (Advanced Linux
yading@10 30 * Sound Architecture) device.
yading@10 31 *
yading@10 32 * The filename parameter is the name of an ALSA PCM device capable of
yading@10 33 * capture, for example "default" or "plughw:1"; see the ALSA documentation
yading@10 34 * for naming conventions. The empty string is equivalent to "default".
yading@10 35 *
yading@10 36 * The playback period is set to the lower value available for the device,
yading@10 37 * which gives a low latency suitable for real-time playback.
yading@10 38 */
yading@10 39
yading@10 40 #include <alsa/asoundlib.h>
yading@10 41
yading@10 42 #include "libavutil/time.h"
yading@10 43 #include "libavformat/internal.h"
yading@10 44 #include "avdevice.h"
yading@10 45 #include "alsa-audio.h"
yading@10 46
yading@10 47 static av_cold int audio_write_header(AVFormatContext *s1)
yading@10 48 {
yading@10 49 AlsaData *s = s1->priv_data;
yading@10 50 AVStream *st;
yading@10 51 unsigned int sample_rate;
yading@10 52 enum AVCodecID codec_id;
yading@10 53 int res;
yading@10 54
yading@10 55 st = s1->streams[0];
yading@10 56 sample_rate = st->codec->sample_rate;
yading@10 57 codec_id = st->codec->codec_id;
yading@10 58 res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
yading@10 59 st->codec->channels, &codec_id);
yading@10 60 if (sample_rate != st->codec->sample_rate) {
yading@10 61 av_log(s1, AV_LOG_ERROR,
yading@10 62 "sample rate %d not available, nearest is %d\n",
yading@10 63 st->codec->sample_rate, sample_rate);
yading@10 64 goto fail;
yading@10 65 }
yading@10 66 avpriv_set_pts_info(st, 64, 1, sample_rate);
yading@10 67
yading@10 68 return res;
yading@10 69
yading@10 70 fail:
yading@10 71 snd_pcm_close(s->h);
yading@10 72 return AVERROR(EIO);
yading@10 73 }
yading@10 74
yading@10 75 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
yading@10 76 {
yading@10 77 AlsaData *s = s1->priv_data;
yading@10 78 int res;
yading@10 79 int size = pkt->size;
yading@10 80 uint8_t *buf = pkt->data;
yading@10 81
yading@10 82 size /= s->frame_size;
yading@10 83 if (s->reorder_func) {
yading@10 84 if (size > s->reorder_buf_size)
yading@10 85 if (ff_alsa_extend_reorder_buf(s, size))
yading@10 86 return AVERROR(ENOMEM);
yading@10 87 s->reorder_func(buf, s->reorder_buf, size);
yading@10 88 buf = s->reorder_buf;
yading@10 89 }
yading@10 90 while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
yading@10 91 if (res == -EAGAIN) {
yading@10 92
yading@10 93 return AVERROR(EAGAIN);
yading@10 94 }
yading@10 95
yading@10 96 if (ff_alsa_xrun_recover(s1, res) < 0) {
yading@10 97 av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
yading@10 98 snd_strerror(res));
yading@10 99
yading@10 100 return AVERROR(EIO);
yading@10 101 }
yading@10 102 }
yading@10 103
yading@10 104 return 0;
yading@10 105 }
yading@10 106
yading@10 107 static void
yading@10 108 audio_get_output_timestamp(AVFormatContext *s1, int stream,
yading@10 109 int64_t *dts, int64_t *wall)
yading@10 110 {
yading@10 111 AlsaData *s = s1->priv_data;
yading@10 112 snd_pcm_sframes_t delay = 0;
yading@10 113 *wall = av_gettime();
yading@10 114 snd_pcm_delay(s->h, &delay);
yading@10 115 *dts = s1->streams[0]->cur_dts - delay;
yading@10 116 }
yading@10 117
yading@10 118 AVOutputFormat ff_alsa_muxer = {
yading@10 119 .name = "alsa",
yading@10 120 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
yading@10 121 .priv_data_size = sizeof(AlsaData),
yading@10 122 .audio_codec = DEFAULT_CODEC_ID,
yading@10 123 .video_codec = AV_CODEC_ID_NONE,
yading@10 124 .write_header = audio_write_header,
yading@10 125 .write_packet = audio_write_packet,
yading@10 126 .write_trailer = ff_alsa_close,
yading@10 127 .get_output_timestamp = audio_get_output_timestamp,
yading@10 128 .flags = AVFMT_NOFILE,
yading@10 129 };