FFmpeg
alsa_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 can 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/internal.h"
43 #include "libavutil/time.h"
44 
45 
46 #include "libavformat/internal.h"
47 #include "libavformat/mux.h"
48 #include "avdevice.h"
49 #include "alsa.h"
50 
52 {
53  AlsaData *s = s1->priv_data;
54  AVStream *st = NULL;
55  unsigned int sample_rate;
56  enum AVCodecID codec_id;
57  int res;
58 
59  if (s1->nb_streams != 1 || s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
60  av_log(s1, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
61  return AVERROR(EINVAL);
62  }
63  st = s1->streams[0];
64 
66  codec_id = st->codecpar->codec_id;
67  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
69  if (sample_rate != st->codecpar->sample_rate) {
71  "sample rate %d not available, nearest is %d\n",
73  goto fail;
74  }
75  avpriv_set_pts_info(st, 64, 1, sample_rate);
76 
77  return res;
78 
79 fail:
80  snd_pcm_close(s->h);
81  return AVERROR(EIO);
82 }
83 
85 {
86  AlsaData *s = s1->priv_data;
87  int res;
88  int size = pkt->size;
89  const uint8_t *buf = pkt->data;
90 
91  size /= s->frame_size;
92  if (pkt->dts != AV_NOPTS_VALUE)
93  s->timestamp = pkt->dts;
94  s->timestamp += pkt->duration ? pkt->duration : size;
95 
96  if (s->reorder_func) {
97  if (size > s->reorder_buf_size)
99  return AVERROR(ENOMEM);
100  s->reorder_func(buf, s->reorder_buf, size);
101  buf = s->reorder_buf;
102  }
103  while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
104  if (res == -EAGAIN) {
105 
106  return AVERROR(EAGAIN);
107  }
108 
109  if (ff_alsa_xrun_recover(s1, res) < 0) {
110  av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
111  snd_strerror(res));
112 
113  return AVERROR(EIO);
114  }
115  }
116 
117  return 0;
118 }
119 
120 static int audio_write_frame(AVFormatContext *s1, int stream_index,
121  AVFrame **frame, unsigned flags)
122 {
123  AlsaData *s = s1->priv_data;
124  AVPacket pkt;
125 
126  /* ff_alsa_open() should have accepted only supported formats */
128  return av_sample_fmt_is_planar(s1->streams[stream_index]->codecpar->format) ?
129  AVERROR(EINVAL) : 0;
130  /* set only used fields */
131  pkt.data = (*frame)->data[0];
132  pkt.size = (*frame)->nb_samples * s->frame_size;
133  pkt.dts = (*frame)->pkt_dts;
134  pkt.duration = (*frame)->pkt_duration;
135  return audio_write_packet(s1, &pkt);
136 }
137 
138 static void
140  int64_t *dts, int64_t *wall)
141 {
142  AlsaData *s = s1->priv_data;
143  snd_pcm_sframes_t delay = 0;
144  *wall = av_gettime();
145  snd_pcm_delay(s->h, &delay);
146  *dts = s->timestamp - delay;
147 }
148 
150 {
151  return ff_alsa_get_device_list(device_list, SND_PCM_STREAM_PLAYBACK);
152 }
153 
154 static const AVClass alsa_muxer_class = {
155  .class_name = "ALSA outdev",
156  .item_name = av_default_item_name,
157  .version = LIBAVUTIL_VERSION_INT,
159 };
160 
162  .name = "alsa",
163  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
164  .priv_data_size = sizeof(AlsaData),
165  .audio_codec = DEFAULT_CODEC_ID,
166  .video_codec = AV_CODEC_ID_NONE,
170  .write_uncoded_frame = audio_write_frame,
171  .get_device_list = audio_get_device_list,
172  .get_output_timestamp = audio_get_output_timestamp,
173  .flags = AVFMT_NOFILE,
174  .priv_class = &alsa_muxer_class,
175 };
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
audio_write_frame
static int audio_write_frame(AVFormatContext *s1, int stream_index, AVFrame **frame, unsigned flags)
Definition: alsa_enc.c:120
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
audio_get_output_timestamp
static void audio_get_output_timestamp(AVFormatContext *s1, int stream, int64_t *dts, int64_t *wall)
Definition: alsa_enc.c:139
DEFAULT_CODEC_ID
#define DEFAULT_CODEC_ID
Definition: alsa.h:42
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:697
fail
#define fail()
Definition: checkasm.h:131
audio_write_packet
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: alsa_enc.c:84
alsa_muxer_class
static const AVClass alsa_muxer_class
Definition: alsa_enc.c:154
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
s1
#define s1
Definition: regdef.h:38
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
ff_alsa_open
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.
Definition: alsa.c:172
ff_alsa_extend_reorder_buf
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
Definition: alsa.c:345
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:371
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:100
AlsaData
Definition: alsa.h:48
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
time.h
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:212
ff_alsa_close
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
Definition: alsa.c:308
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
avdevice.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:509
internal.h
AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
Definition: log.h:42
alsa.h
AVStream
Stream structure.
Definition: avformat.h:948
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVDeviceInfoList
List of devices.
Definition: avdevice.h:473
AV_WRITE_UNCODED_FRAME_QUERY
@ AV_WRITE_UNCODED_FRAME_QUERY
Query whether the feature is possible on this stream.
Definition: mux.h:95
ff_alsa_xrun_recover
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
Definition: alsa.c:324
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
ff_alsa_get_device_list
int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
Definition: alsa.c:362
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:92
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
audio_write_header
static av_cold int audio_write_header(AVFormatContext *s1)
Definition: alsa_enc.c:51
mux.h
ff_alsa_muxer
const AVOutputFormat ff_alsa_muxer
Definition: alsa_enc.c:161
audio_get_device_list
static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
Definition: alsa_enc.c:149