FFmpeg
oss.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 
24 #include <string.h>
25 
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/soundcard.h>
32 
33 #include "libavutil/file_open.h"
34 #include "libavutil/log.h"
35 
36 #include "avdevice.h"
37 
38 #include "oss.h"
39 
40 int ff_oss_audio_open(AVFormatContext *s1, int is_output,
41  const char *audio_device)
42 {
43  OSSAudioData *s = s1->priv_data;
44  int audio_fd;
45  int tmp, err;
46  char *flip = getenv("AUDIO_FLIP_LEFT");
47 
48  if (is_output)
49  audio_fd = avpriv_open(audio_device, O_WRONLY);
50  else
51  audio_fd = avpriv_open(audio_device, O_RDONLY);
52  if (audio_fd < 0) {
53  av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, av_err2str(AVERROR(errno)));
54  return AVERROR(EIO);
55  }
56 
57  if (flip && *flip == '1') {
58  s->flip_left = 1;
59  }
60 
61  /* non blocking mode */
62  if (!is_output) {
63  if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
64  av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, av_err2str(AVERROR(errno)));
65  }
66  }
67 
68  s->frame_size = OSS_AUDIO_BLOCK_SIZE;
69 
70 #define CHECK_IOCTL_ERROR(event) \
71  if (err < 0) { \
72  av_log(s1, AV_LOG_ERROR, #event ": %s\n", av_err2str(AVERROR(errno)));\
73  goto fail; \
74  }
75 
76  /* select format : favour native format
77  * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
78  * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
79  * fail anyway. */
80  err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
81  if (err < 0) {
82  av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", av_err2str(AVERROR(errno)));
83  }
84 
85 #if HAVE_BIGENDIAN
86  if (tmp & AFMT_S16_BE) {
87  tmp = AFMT_S16_BE;
88  } else if (tmp & AFMT_S16_LE) {
89  tmp = AFMT_S16_LE;
90  } else {
91  tmp = 0;
92  }
93 #else
94  if (tmp & AFMT_S16_LE) {
95  tmp = AFMT_S16_LE;
96  } else if (tmp & AFMT_S16_BE) {
97  tmp = AFMT_S16_BE;
98  } else {
99  tmp = 0;
100  }
101 #endif
102 
103  switch(tmp) {
104  case AFMT_S16_LE:
105  s->codec_id = AV_CODEC_ID_PCM_S16LE;
106  s->sample_size = 2;
107  break;
108  case AFMT_S16_BE:
109  s->codec_id = AV_CODEC_ID_PCM_S16BE;
110  s->sample_size = 2;
111  break;
112  default:
113  av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
114  close(audio_fd);
115  return AVERROR(EIO);
116  }
117  err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
118  CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMT)
119 
120  tmp = (s->channels == 2);
121  err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
122  CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
123 
124  tmp = s->sample_rate;
125  err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
126  CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
127  s->sample_rate = tmp; /* store real sample rate */
128  s->fd = audio_fd;
129 
130  return 0;
131  fail:
132  close(audio_fd);
133  return AVERROR(EIO);
134 #undef CHECK_IOCTL_ERROR
135 }
136 
138 {
139  close(s->fd);
140  return 0;
141 }
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:326
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
ff_oss_audio_open
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss.c:40
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ff_oss_audio_close
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss.c:137
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:327
fail
#define fail()
Definition: checkasm.h:134
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:256
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
s1
#define s1
Definition: regdef.h:38
file_open.h
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
OSS_AUDIO_BLOCK_SIZE
#define OSS_AUDIO_BLOCK_SIZE
Definition: oss.h:27
OSSAudioData
Definition: oss.h:29
flip
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:132
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
avdevice.h
log.h
CHECK_IOCTL_ERROR
#define CHECK_IOCTL_ERROR(event)
oss.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27