Libav
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 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 #include "libavformat/avformat.h"
42 
43 #include "alsa-audio.h"
44 
46 {
47  AlsaData *s = s1->priv_data;
48  AVStream *st;
49  unsigned int sample_rate;
50  enum AVCodecID codec_id;
51  int res;
52 
53  st = s1->streams[0];
54  sample_rate = st->codec->sample_rate;
55  codec_id = st->codec->codec_id;
56  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
57  st->codec->channels, &codec_id);
58  if (sample_rate != st->codec->sample_rate) {
59  av_log(s1, AV_LOG_ERROR,
60  "sample rate %d not available, nearest is %d\n",
61  st->codec->sample_rate, sample_rate);
62  goto fail;
63  }
64 
65  return res;
66 
67 fail:
68  snd_pcm_close(s->h);
69  return AVERROR(EIO);
70 }
71 
73 {
74  AlsaData *s = s1->priv_data;
75  int res;
76  int size = pkt->size;
77  uint8_t *buf = pkt->data;
78 
79  size /= s->frame_size;
80  if (s->reorder_func) {
81  if (size > s->reorder_buf_size)
82  if (ff_alsa_extend_reorder_buf(s, size))
83  return AVERROR(ENOMEM);
84  s->reorder_func(buf, s->reorder_buf, size);
85  buf = s->reorder_buf;
86  }
87  while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
88  if (res == -EAGAIN) {
89 
90  return AVERROR(EAGAIN);
91  }
92 
93  if (ff_alsa_xrun_recover(s1, res) < 0) {
94  av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
95  snd_strerror(res));
96 
97  return AVERROR(EIO);
98  }
99  }
100 
101  return 0;
102 }
103 
105  .name = "alsa",
106  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
107  .priv_data_size = sizeof(AlsaData),
108  .audio_codec = DEFAULT_CODEC_ID,
109  .video_codec = AV_CODEC_ID_NONE,
113  .flags = AVFMT_NOFILE,
114 };
int size
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int size
Definition: avcodec.h:974
Format I/O context.
Definition: avformat.h:922
uint8_t
#define av_cold
Definition: attributes.h:66
void(* reorder_func)(const void *, void *, int)
Definition: alsa-audio.h:52
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
enum AVCodecID codec_id
Definition: mov_chan.c:432
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
const char * name
Definition: avformat.h:446
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)
Stream structure.
Definition: avformat.h:699
void * reorder_buf
Definition: alsa-audio.h:53
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVOutputFormat ff_alsa_muxer
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Main libavformat public API header.
#define DEFAULT_CODEC_ID
Definition: alsa-audio.h:41
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
snd_pcm_t * h
Definition: alsa-audio.h:47
int frame_size
preferred size for reads and writes
Definition: alsa-audio.h:48
ALSA input and output: definitions and structures.
This structure stores compressed data.
Definition: avcodec.h:950
int reorder_buf_size
in frames
Definition: alsa-audio.h:54