00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00040 #include <alsa/asoundlib.h>
00041
00042 #include "libavformat/internal.h"
00043 #include "avdevice.h"
00044 #include "alsa-audio.h"
00045
00046 static av_cold int audio_write_header(AVFormatContext *s1)
00047 {
00048 AlsaData *s = s1->priv_data;
00049 AVStream *st;
00050 unsigned int sample_rate;
00051 enum CodecID codec_id;
00052 int res;
00053
00054 st = s1->streams[0];
00055 sample_rate = st->codec->sample_rate;
00056 codec_id = st->codec->codec_id;
00057 res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
00058 st->codec->channels, &codec_id);
00059 if (sample_rate != st->codec->sample_rate) {
00060 av_log(s1, AV_LOG_ERROR,
00061 "sample rate %d not available, nearest is %d\n",
00062 st->codec->sample_rate, sample_rate);
00063 goto fail;
00064 }
00065 avpriv_set_pts_info(st, 64, 1, sample_rate);
00066
00067 return res;
00068
00069 fail:
00070 snd_pcm_close(s->h);
00071 return AVERROR(EIO);
00072 }
00073
00074 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
00075 {
00076 AlsaData *s = s1->priv_data;
00077 int res;
00078 int size = pkt->size;
00079 uint8_t *buf = pkt->data;
00080
00081 size /= s->frame_size;
00082 if (s->reorder_func) {
00083 if (size > s->reorder_buf_size)
00084 if (ff_alsa_extend_reorder_buf(s, size))
00085 return AVERROR(ENOMEM);
00086 s->reorder_func(buf, s->reorder_buf, size);
00087 buf = s->reorder_buf;
00088 }
00089 while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
00090 if (res == -EAGAIN) {
00091
00092 return AVERROR(EAGAIN);
00093 }
00094
00095 if (ff_alsa_xrun_recover(s1, res) < 0) {
00096 av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
00097 snd_strerror(res));
00098
00099 return AVERROR(EIO);
00100 }
00101 }
00102
00103 return 0;
00104 }
00105
00106 static void
00107 audio_get_output_timestamp(AVFormatContext *s1, int stream,
00108 int64_t *dts, int64_t *wall)
00109 {
00110 AlsaData *s = s1->priv_data;
00111 snd_pcm_sframes_t delay = 0;
00112 *wall = av_gettime();
00113 snd_pcm_delay(s->h, &delay);
00114 *dts = s1->streams[0]->cur_dts - delay;
00115 }
00116
00117 AVOutputFormat ff_alsa_muxer = {
00118 .name = "alsa",
00119 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
00120 .priv_data_size = sizeof(AlsaData),
00121 .audio_codec = DEFAULT_CODEC_ID,
00122 .video_codec = CODEC_ID_NONE,
00123 .write_header = audio_write_header,
00124 .write_packet = audio_write_packet,
00125 .write_trailer = ff_alsa_close,
00126 .get_output_timestamp = audio_get_output_timestamp,
00127 .flags = AVFMT_NOFILE,
00128 };