Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
40 #include <alsa/asoundlib.h>
41 
42 #include "libavutil/internal.h"
43 
44 #include "libavformat/avformat.h"
45 
46 #include "alsa.h"
47 
49 {
50  AlsaData *s = s1->priv_data;
51  AVStream *st;
52  unsigned int sample_rate;
53  enum AVCodecID codec_id;
54  int res;
55 
56  st = s1->streams[0];
57  sample_rate = st->codecpar->sample_rate;
58  codec_id = st->codecpar->codec_id;
59  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
60  st->codecpar->channels, &codec_id);
61  if (sample_rate != st->codecpar->sample_rate) {
62  av_log(s1, AV_LOG_ERROR,
63  "sample rate %d not available, nearest is %d\n",
64  st->codecpar->sample_rate, sample_rate);
65  goto fail;
66  }
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 
108  .name = "alsa",
109  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
110  .priv_data_size = sizeof(AlsaData),
111  .audio_codec = DEFAULT_CODEC_ID,
112  .video_codec = AV_CODEC_ID_NONE,
116  .flags = AVFMT_NOFILE,
117 };
int size
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVOutputFormat ff_alsa_muxer
Definition: alsa_enc.c:107
ALSA input and output: definitions and structures.
Format I/O context.
Definition: avformat.h:940
uint8_t
#define av_cold
Definition: attributes.h:66
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
static av_cold int audio_write_header(AVFormatContext *s1)
Definition: alsa_enc.c:48
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
Definition: alsa.c:348
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define DEFAULT_CODEC_ID
Definition: alsa.h:41
#define AVERROR(e)
Definition: error.h:43
Definition: alsa.h:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: alsa_enc.c:75
#define fail()
Definition: checkasm.h:80
common internal API header
void(* reorder_func)(const void *, void *, int)
Definition: alsa.h:52
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
const char * name
Definition: avformat.h:450
Stream structure.
Definition: avformat.h:705
void * reorder_buf
Definition: alsa.h:53
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
Definition: alsa.c:327
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
Definition: alsa.c:318
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
void * priv_data
Format private data.
Definition: avformat.h:968
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
snd_pcm_t * h
Definition: alsa.h:47
int channels
Audio only.
Definition: avcodec.h:3560
int frame_size
preferred size for reads and writes
Definition: alsa.h:48
AVCodecParameters * codecpar
Definition: avformat.h:831
This structure stores compressed data.
Definition: avcodec.h:1323
int reorder_buf_size
in frames
Definition: alsa.h:54
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:186