FFmpeg  2.1.1
oss_audio.c
Go to the documentation of this file.
1 /*
2  * Linux audio play and grab interface
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28 #if HAVE_SOUNDCARD_H
29 #include <soundcard.h>
30 #else
31 #include <sys/soundcard.h>
32 #endif
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 
37 #include "libavutil/internal.h"
38 #include "libavutil/log.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/time.h"
41 #include "libavcodec/avcodec.h"
42 #include "avdevice.h"
43 #include "libavformat/internal.h"
44 
45 #define AUDIO_BLOCK_SIZE 4096
46 
47 typedef struct {
48  AVClass *class;
49  int fd;
51  int channels;
52  int frame_size; /* in bytes ! */
54  unsigned int flip_left : 1;
57 } AudioData;
58 
59 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
60 {
61  AudioData *s = s1->priv_data;
62  int audio_fd;
63  int tmp, err;
64  char *flip = getenv("AUDIO_FLIP_LEFT");
65 
66  if (is_output)
67  audio_fd = avpriv_open(audio_device, O_WRONLY);
68  else
69  audio_fd = avpriv_open(audio_device, O_RDONLY);
70  if (audio_fd < 0) {
71  av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
72  return AVERROR(EIO);
73  }
74 
75  if (flip && *flip == '1') {
76  s->flip_left = 1;
77  }
78 
79  /* non blocking mode */
80  if (!is_output) {
81  if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
82  av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
83  }
84  }
85 
87 
88  /* select format : favour native format */
89  err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
90 
91 #if HAVE_BIGENDIAN
92  if (tmp & AFMT_S16_BE) {
93  tmp = AFMT_S16_BE;
94  } else if (tmp & AFMT_S16_LE) {
95  tmp = AFMT_S16_LE;
96  } else {
97  tmp = 0;
98  }
99 #else
100  if (tmp & AFMT_S16_LE) {
101  tmp = AFMT_S16_LE;
102  } else if (tmp & AFMT_S16_BE) {
103  tmp = AFMT_S16_BE;
104  } else {
105  tmp = 0;
106  }
107 #endif
108 
109  switch(tmp) {
110  case AFMT_S16_LE:
112  break;
113  case AFMT_S16_BE:
115  break;
116  default:
117  av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
118  close(audio_fd);
119  return AVERROR(EIO);
120  }
121  err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
122  if (err < 0) {
123  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
124  goto fail;
125  }
126 
127  tmp = (s->channels == 2);
128  err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129  if (err < 0) {
130  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
131  goto fail;
132  }
133 
134  tmp = s->sample_rate;
135  err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
136  if (err < 0) {
137  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
138  goto fail;
139  }
140  s->sample_rate = tmp; /* store real sample rate */
141  s->fd = audio_fd;
142 
143  return 0;
144  fail:
145  close(audio_fd);
146  return AVERROR(EIO);
147 }
148 
149 static int audio_close(AudioData *s)
150 {
151  close(s->fd);
152  return 0;
153 }
154 
155 /* sound output support */
157 {
158  AudioData *s = s1->priv_data;
159  AVStream *st;
160  int ret;
161 
162  st = s1->streams[0];
163  s->sample_rate = st->codec->sample_rate;
164  s->channels = st->codec->channels;
165  ret = audio_open(s1, 1, s1->filename);
166  if (ret < 0) {
167  return AVERROR(EIO);
168  } else {
169  return 0;
170  }
171 }
172 
174 {
175  AudioData *s = s1->priv_data;
176  int len, ret;
177  int size= pkt->size;
178  uint8_t *buf= pkt->data;
179 
180  while (size > 0) {
181  len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
182  memcpy(s->buffer + s->buffer_ptr, buf, len);
183  s->buffer_ptr += len;
184  if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
185  for(;;) {
186  ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
187  if (ret > 0)
188  break;
189  if (ret < 0 && (errno != EAGAIN && errno != EINTR))
190  return AVERROR(EIO);
191  }
192  s->buffer_ptr = 0;
193  }
194  buf += len;
195  size -= len;
196  }
197  return 0;
198 }
199 
201 {
202  AudioData *s = s1->priv_data;
203 
204  audio_close(s);
205  return 0;
206 }
207 
208 /* grab support */
209 
211 {
212  AudioData *s = s1->priv_data;
213  AVStream *st;
214  int ret;
215 
216  st = avformat_new_stream(s1, NULL);
217  if (!st) {
218  return AVERROR(ENOMEM);
219  }
220 
221  ret = audio_open(s1, 0, s1->filename);
222  if (ret < 0) {
223  return AVERROR(EIO);
224  }
225 
226  /* take real parameters */
228  st->codec->codec_id = s->codec_id;
229  st->codec->sample_rate = s->sample_rate;
230  st->codec->channels = s->channels;
231 
232  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
233  return 0;
234 }
235 
237 {
238  AudioData *s = s1->priv_data;
239  int ret, bdelay;
240  int64_t cur_time;
241  struct audio_buf_info abufi;
242 
243  if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
244  return ret;
245 
246  ret = read(s->fd, pkt->data, pkt->size);
247  if (ret <= 0){
248  av_free_packet(pkt);
249  pkt->size = 0;
250  if (ret<0) return AVERROR(errno);
251  else return AVERROR_EOF;
252  }
253  pkt->size = ret;
254 
255  /* compute pts of the start of the packet */
256  cur_time = av_gettime();
257  bdelay = ret;
258  if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
259  bdelay += abufi.bytes;
260  }
261  /* subtract time represented by the number of bytes in the audio fifo */
262  cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
263 
264  /* convert to wanted units */
265  pkt->pts = cur_time;
266 
267  if (s->flip_left && s->channels == 2) {
268  int i;
269  short *p = (short *) pkt->data;
270 
271  for (i = 0; i < ret; i += 4) {
272  *p = ~*p;
273  p += 2;
274  }
275  }
276  return 0;
277 }
278 
280 {
281  AudioData *s = s1->priv_data;
282 
283  audio_close(s);
284  return 0;
285 }
286 
287 #if CONFIG_OSS_INDEV
288 static const AVOption options[] = {
289  { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
290  { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
291  { NULL },
292 };
293 
294 static const AVClass oss_demuxer_class = {
295  .class_name = "OSS demuxer",
296  .item_name = av_default_item_name,
297  .option = options,
298  .version = LIBAVUTIL_VERSION_INT,
299 };
300 
301 AVInputFormat ff_oss_demuxer = {
302  .name = "oss",
303  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
304  .priv_data_size = sizeof(AudioData),
308  .flags = AVFMT_NOFILE,
309  .priv_class = &oss_demuxer_class,
310 };
311 #endif
312 
313 #if CONFIG_OSS_OUTDEV
314 AVOutputFormat ff_oss_muxer = {
315  .name = "oss",
316  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
317  .priv_data_size = sizeof(AudioData),
318  /* XXX: we make the assumption that the soundcard accepts this format */
319  /* XXX: find better solution with "preinit" method, needed also in
320  other formats */
322  .video_codec = AV_CODEC_ID_NONE,
323  .write_header = audio_write_header,
324  .write_packet = audio_write_packet,
325  .write_trailer = audio_write_trailer,
326  .flags = AVFMT_NOFILE,
327 };
328 #endif
const char * s
Definition: avisynth_c.h:668
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
AVOption.
Definition: opt.h:253
Audio buffer used for intermediate storage between conversion phases.
Definition: oss_audio.c:47
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
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:3922
static int64_t cur_time
Definition: ffserver.c:324
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Format I/O context.
Definition: avformat.h:968
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
uint8_t
static int audio_write_header(AVFormatContext *s1)
Definition: oss_audio.c:156
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:55
int buffer_ptr
Definition: oss_audio.c:56
static int audio_write_trailer(AVFormatContext *s1)
Definition: oss_audio.c:200
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:71
static int audio_read_close(AVFormatContext *s1)
Definition: oss_audio.c:279
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:347
static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss_audio.c:59
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
const OptionDef options[]
Definition: ffserver.c:4682
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
char filename[1024]
input or output filename
Definition: avformat.h:1018
int channels
channel count
Definition: oss_audio.c:51
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
enum AVCodecID codec_id
Definition: mov_chan.c:433
enum AVCodecID codec_id
Definition: oss_audio.c:53
goto fail
Definition: avfilter.c:963
common internal API header
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
ret
Definition: avfilter.c:961
AVStream ** streams
Definition: avformat.h:1016
static int audio_close(AudioData *s)
Definition: oss_audio.c:149
#define FFMIN(a, b)
Definition: avcodec.h:925
unsigned int flip_left
Definition: oss_audio.c:54
const char * name
Definition: avformat.h:395
static char buffer[20]
Definition: seek-test.c:31
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
sample_rate
uint8_t buffer[AUDIO_BLOCK_SIZE]
Definition: oss_audio.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
static int audio_read_header(AVFormatContext *s1)
Definition: oss_audio.c:210
int sample_rate
samples per second
Definition: avcodec.h:1873
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
Main libavdevice API header.
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AUDIO_BLOCK_SIZE
Definition: oss_audio.c:45
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_audio.c:173
Describe the class of an AVClass context structure.
Definition: log.h:50
uint8_t * data
Definition: avcodec.h:1063
#define AV_NE(be, le)
Definition: avcodec.h:908
#define s1
Definition: regdef.h:38
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:284
int sample_rate
Definition: oss_audio.c:50
static void flip(AVCodecContext *avctx, AVPicture *picture)
Definition: rawdec.c:162
int fd
Definition: oss_audio.c:49
int len
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
int frame_size
Definition: oss_audio.c:52
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_audio.c:236
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
for(j=16;j >0;--j)