FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavformat/internal.h"
43 #include "avdevice.h"
44 #include "alsa-audio.h"
45 
47 {
48  AlsaData *s = s1->priv_data;
49  AVStream *st;
50  unsigned int sample_rate;
51  enum AVCodecID codec_id;
52  int res;
53 
54  st = s1->streams[0];
55  sample_rate = st->codec->sample_rate;
56  codec_id = st->codec->codec_id;
57  res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
58  st->codec->channels, &codec_id);
59  if (sample_rate != st->codec->sample_rate) {
60  av_log(s1, AV_LOG_ERROR,
61  "sample rate %d not available, nearest is %d\n",
62  st->codec->sample_rate, sample_rate);
63  goto fail;
64  }
65  avpriv_set_pts_info(st, 64, 1, sample_rate);
66 
67  return res;
68 
69 fail:
70  snd_pcm_close(s->h);
71  return AVERROR(EIO);
72 }
73 
75 {
76  AlsaData *s = s1->priv_data;
77  int res;
78  int size = pkt->size;
79  uint8_t *buf = pkt->data;
80 
81  size /= s->frame_size;
82  if (s->reorder_func) {
83  if (size > s->reorder_buf_size)
84  if (ff_alsa_extend_reorder_buf(s, size))
85  return AVERROR(ENOMEM);
86  s->reorder_func(buf, s->reorder_buf, size);
87  buf = s->reorder_buf;
88  }
89  while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
90  if (res == -EAGAIN) {
91 
92  return AVERROR(EAGAIN);
93  }
94 
95  if (ff_alsa_xrun_recover(s1, res) < 0) {
96  av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
97  snd_strerror(res));
98 
99  return AVERROR(EIO);
100  }
101  }
102 
103  return 0;
104 }
105 
106 static void
108  int64_t *dts, int64_t *wall)
109 {
110  AlsaData *s = s1->priv_data;
111  snd_pcm_sframes_t delay = 0;
112  *wall = av_gettime();
113  snd_pcm_delay(s->h, &delay);
114  *dts = s1->streams[0]->cur_dts - delay;
115 }
116 
118  .name = "alsa",
119  .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
120  .priv_data_size = sizeof(AlsaData),
121  .audio_codec = DEFAULT_CODEC_ID,
122  .video_codec = AV_CODEC_ID_NONE,
126  .get_output_timestamp = audio_get_output_timestamp,
127  .flags = AVFMT_NOFILE,
128 };