FFmpeg: libavdevice/alsa_enc.c Source File

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 "avdevice.h"
48 #include "alsa.h"
49 
50  static av_cold int audio_write_header(AVFormatContext *s1)
51 {
52  AlsaData *s = s1->priv_data;
53  AVStream *st = NULL;
54  unsigned int sample_rate;
55  enum AVCodecID codec_id;
56  int res;
57 
58  if (s1->nb_streams != 1 || s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
59  av_log(s1, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
60  return AVERROR(EINVAL);
61  }
62  st = s1->streams[0];
63 
64  sample_rate = st->codecpar->sample_rate;
65  codec_id = st->codecpar->codec_id;
66  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
67  st->codecpar->channels, &codec_id);
68  if (sample_rate != st->codecpar->sample_rate) {
69  av_log(s1, AV_LOG_ERROR,
70  "sample rate %d not available, nearest is %d\n",
71  st->codecpar->sample_rate, sample_rate);
72  goto fail;
73  }
74  avpriv_set_pts_info(st, 64, 1, sample_rate);
75 
76  return res;
77 
78 fail:
79  snd_pcm_close(s->h);
80  return AVERROR(EIO);
81 }
82 
83  static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
84 {
85  AlsaData *s = s1->priv_data;
86  int res;
87  int size = pkt->size;
88  uint8_t *buf = pkt->data;
89 
90  size /= s->frame_size;
91  if (pkt->dts != AV_NOPTS_VALUE)
92  s->timestamp = pkt->dts;
93  s->timestamp += pkt->duration ? pkt->duration : size;
94 
95  if (s->reorder_func) {
96  if (size > s->reorder_buf_size)
97  if (ff_alsa_extend_reorder_buf(s, size))
98  return AVERROR(ENOMEM);
99  s->reorder_func(buf, s->reorder_buf, size);
100  buf = s->reorder_buf;
101  }
102  while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
103  if (res == -EAGAIN) {
104 
105  return AVERROR(EAGAIN);
106  }
107 
108  if (ff_alsa_xrun_recover(s1, res) < 0) {
109  av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
110  snd_strerror(res));
111 
112  return AVERROR(EIO);
113  }
114  }
115 
116  return 0;
117 }
118 
119  static int audio_write_frame(AVFormatContext *s1, int stream_index,
120  AVFrame **frame, unsigned flags)
121 {
122  AlsaData *s = s1->priv_data;
123  AVPacket pkt;
124 
125  /* ff_alsa_open() should have accepted only supported formats */
126  if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
127  return av_sample_fmt_is_planar(s1->streams[stream_index]->codecpar->format) ?
128  AVERROR(EINVAL) : 0;
129  /* set only used fields */
130  pkt.data = (*frame)->data[0];
131  pkt.size = (*frame)->nb_samples * s->frame_size;
132  pkt.dts = (*frame)->pkt_dts;
133  pkt.duration = (*frame)->pkt_duration;
134  return audio_write_packet(s1, &pkt);
135 }
136 
137 static void
138  audio_get_output_timestamp(AVFormatContext *s1, int stream,
139  int64_t *dts, int64_t *wall)
140 {
141  AlsaData *s = s1->priv_data;
142  snd_pcm_sframes_t delay = 0;
143  *wall = av_gettime();
144  snd_pcm_delay(s->h, &delay);
145  *dts = s->timestamp - delay;
146 }
147 
148  static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
149 {
150  return ff_alsa_get_device_list(device_list, SND_PCM_STREAM_PLAYBACK);
151 }
152 
153  static const AVClass alsa_muxer_class = {
154  .class_name = "ALSA muxer",
155  .item_name = av_default_item_name,
156  .version = LIBAVUTIL_VERSION_INT,
157  .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
158 };
159 
160  AVOutputFormat ff_alsa_muxer = {
161  .name = "alsa",
162  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
163  .priv_data_size = sizeof(AlsaData),
164  .audio_codec = DEFAULT_CODEC_ID,
165  .video_codec = AV_CODEC_ID_NONE,
166  .write_header = audio_write_header,
167  .write_packet = audio_write_packet,
168  .write_trailer = ff_alsa_close,
169  .write_uncoded_frame = audio_write_frame,
170  .get_device_list = audio_get_device_list,
171  .get_output_timestamp = audio_get_output_timestamp,
172  .flags = AVFMT_NOFILE,
173  .priv_class = &alsa_muxer_class,
174 };
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:679
NULL
#define NULL
Definition: coverity.c:32
s
const char * s
Definition: avisynth_c.h:768
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
avpriv_set_pts_info
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.
Definition: utils.c:4820
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
AVPacket::size
int size
Definition: avcodec.h:1431
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AlsaData::timestamp
int64_t timestamp
current timestamp, without latency applied.
Definition: alsa.h:60
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
ff_alsa_muxer
AVOutputFormat ff_alsa_muxer
Definition: alsa_enc.c:160
alsa.h
ALSA input and output: definitions and structures.
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
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:72
uint8_t
uint8_t
Definition: audio_convert.c:194
av_cold
#define av_cold
Definition: attributes.h:82
AlsaData::reorder_func
void(* reorder_func)(const void *, void *, int)
Definition: alsa.h:57
AV_WRITE_UNCODED_FRAME_QUERY
Query whether the feature is possible on this stream.
Definition: internal.h:628
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
frame
static AVFrame * frame
Definition: demuxing_decoding.c:53
audio_write_header
static av_cold int audio_write_header(AVFormatContext *s1)
Definition: alsa_enc.c:50
AVPacket::data
uint8_t * data
Definition: avcodec.h:1430
flags
static int flags
Definition: log.c:55
audio_write_frame
static int audio_write_frame(AVFormatContext *s1, int stream_index, AVFrame **frame, unsigned flags)
Definition: alsa_enc.c:119
size
ptrdiff_t size
Definition: opengl_enc.c:101
ff_alsa_extend_reorder_buf
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
Definition: alsa.c:333
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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:112
avdevice.h
Main libavdevice API header.
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
DEFAULT_CODEC_ID
#define DEFAULT_CODEC_ID
Definition: alsa.h:42
AVERROR
#define AVERROR(e)
Definition: error.h:43
AlsaData
Definition: alsa.h:48
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:186
audio_write_packet
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: alsa_enc.c:83
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
fail
#define fail()
Definition: checkasm.h:116
internal.h
common internal API header
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
AVOutputFormat::name
const char * name
Definition: avformat.h:507
alsa_muxer_class
static const AVClass alsa_muxer_class
Definition: alsa_enc.c:153
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:362
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
AVStream
Stream structure.
Definition: avformat.h:873
sample_rate
sample_rate
Definition: ffmpeg_filter.c:190
AlsaData::reorder_buf
void * reorder_buf
Definition: alsa.h:58
ff_alsa_xrun_recover
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
Definition: alsa.c:312
ff_alsa_close
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
Definition: alsa.c:299
audio_get_device_list
static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
Definition: alsa_enc.c:148
buf
void * buf
Definition: avisynth_c.h:690
audio_get_output_timestamp
static void audio_get_output_timestamp(AVFormatContext *s1, int stream, int64_t *dts, int64_t *wall)
Definition: alsa_enc.c:138
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
ff_alsa_get_device_list
int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
Definition: alsa.c:350
s1
#define s1
Definition: regdef.h:38
AVDeviceInfoList
List of devices.
Definition: avdevice.h:460
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:3994
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
AlsaData::h
snd_pcm_t * h
Definition: alsa.h:50
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:3990
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1429
AlsaData::frame_size
int frame_size
bytes per sample * channels
Definition: alsa.h:51
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1407
AlsaData::reorder_buf_size
int reorder_buf_size
in frames
Definition: alsa.h:59
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:167

Generated on Sun May 13 2018 02:03:58 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /