alsa-audio-enc.c
Go to the documentation of this file.
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
25  * ALSA input and output: output
26  * @author Luca Abeni ( lucabe72 email it )
27  * @author Benoit Fouet ( benoit fouet free fr )
28  *
29  * This avdevice encoder allows to play audio to an ALSA (Advanced Linux
30  * Sound Architecture) device.
31  *
32  * The filename parameter is the name of an ALSA PCM device capable of
33  * capture, for example "default" or "plughw:1"; see the ALSA documentation
34  * for naming conventions. The empty string is equivalent to "default".
35  *
36  * The playback period is set to the lower value available for the device,
37  * which gives a low latency suitable for real-time playback.
38  */
39 
40 #include <alsa/asoundlib.h>
41 
42 #include "libavutil/time.h"
43 #include "libavformat/internal.h"
44 #include "avdevice.h"
45 #include "alsa-audio.h"
46 
48 {
49  AlsaData *s = s1->priv_data;
50  AVStream *st;
51  unsigned int sample_rate;
52  enum AVCodecID codec_id;
53  int res;
54 
55  st = s1->streams[0];
56  sample_rate = st->codec->sample_rate;
57  codec_id = st->codec->codec_id;
58  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
59  st->codec->channels, &codec_id);
60  if (sample_rate != st->codec->sample_rate) {
61  av_log(s1, AV_LOG_ERROR,
62  "sample rate %d not available, nearest is %d\n",
63  st->codec->sample_rate, sample_rate);
64  goto fail;
65  }
66  avpriv_set_pts_info(st, 64, 1, sample_rate);
67 
68  return res;
69 
70 fail:
71  snd_pcm_close(s->h);
72  return AVERROR(EIO);
73 }
74 
76 {
77  AlsaData *s = s1->priv_data;
78  int res;
79  int size = pkt->size;
80  uint8_t *buf = pkt->data;
81 
82  size /= s->frame_size;
83  if (s->reorder_func) {
84  if (size > s->reorder_buf_size)
85  if (ff_alsa_extend_reorder_buf(s, size))
86  return AVERROR(ENOMEM);
87  s->reorder_func(buf, s->reorder_buf, size);
88  buf = s->reorder_buf;
89  }
90  while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
91  if (res == -EAGAIN) {
92 
93  return AVERROR(EAGAIN);
94  }
95 
96  if (ff_alsa_xrun_recover(s1, res) < 0) {
97  av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
98  snd_strerror(res));
99 
100  return AVERROR(EIO);
101  }
102  }
103 
104  return 0;
105 }
106 
107 static void
109  int64_t *dts, int64_t *wall)
110 {
111  AlsaData *s = s1->priv_data;
112  snd_pcm_sframes_t delay = 0;
113  *wall = av_gettime();
114  snd_pcm_delay(s->h, &delay);
115  *dts = s1->streams[0]->cur_dts - delay;
116 }
117 
119  .name = "alsa",
120  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
121  .priv_data_size = sizeof(AlsaData),
122  .audio_codec = DEFAULT_CODEC_ID,
123  .video_codec = AV_CODEC_ID_NONE,
127  .get_output_timestamp = audio_get_output_timestamp,
128  .flags = AVFMT_NOFILE,
129 };
const char * s
Definition: avisynth_c.h:668
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Format I/O context.
Definition: avformat.h:944
int64_t cur_dts
Definition: avformat.h:785
uint8_t
#define av_cold
Definition: attributes.h:78
static AVPacket pkt
Definition: demuxing.c:56
AVStream ** streams
Definition: avformat.h:992
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
uint8_t * data
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
static void audio_get_output_timestamp(AVFormatContext *s1, int stream, int64_t *dts, int64_t *wall)
static int write_trailer(AVFormatContext *s)
Main libavdevice API header.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
enum AVCodecID codec_id
Definition: mov_chan.c:433
int size
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
void(* reorder_func)(const void *, void *, int)
Definition: alsa-audio.h:57
const char * name
Definition: avformat.h:378
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, unsigned int *sample_rate, int channels, enum AVCodecID *codec_id)
Open an ALSA PCM.
static av_cold int audio_write_header(AVFormatContext *s1)
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:643
sample_rate
void * reorder_buf
Definition: alsa-audio.h:58
enum AVCodecID codec_id
int sample_rate
samples per second
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
AVOutputFormat ff_alsa_muxer
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
#define s1
Definition: regdef.h:38
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
struct AlsaData AlsaData
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
static int flags
Definition: cpu.c:23
#define DEFAULT_CODEC_ID
Definition: alsa-audio.h:42
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:345
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
snd_pcm_t * h
Definition: alsa-audio.h:50
int frame_size
bytes per sample * channels
Definition: alsa-audio.h:51
ALSA input and output: definitions and structures.
This structure stores compressed data.
int reorder_buf_size
in frames
Definition: alsa-audio.h:59