2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #include <soundcard.h>
29 #include <sys/soundcard.h>
36 #include <sys/ioctl.h>
38 #include "libavutil/log.h"
40 #include "libavcodec/avcodec.h"
43 #include "oss_audio.h"
45 int ff_oss_audio_open(AVFormatContext *s1, int is_output,
46 const char *audio_device)
48 OSSAudioData *s = s1->priv_data;
51 char *flip = getenv("AUDIO_FLIP_LEFT");
54 audio_fd = avpriv_open(audio_device, O_WRONLY);
56 audio_fd = avpriv_open(audio_device, O_RDONLY);
58 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
62 if (flip && *flip == '1') {
66 /* non blocking mode */
68 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
69 av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
73 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
75 #define CHECK_IOCTL_ERROR(event) \
77 av_log(s1, AV_LOG_ERROR, #event ": %s\n", strerror(errno)); \
81 /* select format : favour native format
82 * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
83 * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
84 * fail anyway. `err =` kept to eliminate compiler warning. */
85 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
87 av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", strerror(errno));
91 if (tmp & AFMT_S16_BE) {
93 } else if (tmp & AFMT_S16_LE) {
99 if (tmp & AFMT_S16_LE) {
101 } else if (tmp & AFMT_S16_BE) {
110 s->codec_id = AV_CODEC_ID_PCM_S16LE;
113 s->codec_id = AV_CODEC_ID_PCM_S16BE;
116 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
120 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
121 CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
123 tmp = (s->channels == 2);
124 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
125 CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
127 tmp = s->sample_rate;
128 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
129 CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
130 s->sample_rate = tmp; /* store real sample rate */
137 #undef CHECK_IOCTL_ERROR
140 int ff_oss_audio_close(OSSAudioData *s)