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");
55 audio_fd = avpriv_open(audio_device, O_WRONLY);
57 audio_fd = avpriv_open(audio_device, O_RDONLY);
59 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
63 if (flip && *flip == '1') {
67 /* non blocking mode */
69 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
70 av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
74 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
76 #define CHECK_IOCTL_ERROR(event) \
78 av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
79 av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
83 /* select format : favour native format
84 * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
85 * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
87 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
89 av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", strerror(errno));
93 if (tmp & AFMT_S16_BE) {
95 } else if (tmp & AFMT_S16_LE) {
101 if (tmp & AFMT_S16_LE) {
103 } else if (tmp & AFMT_S16_BE) {
112 s->codec_id = AV_CODEC_ID_PCM_S16LE;
115 s->codec_id = AV_CODEC_ID_PCM_S16BE;
118 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
122 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
123 CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
125 tmp = (s->channels == 2);
126 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
127 CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
129 tmp = s->sample_rate;
130 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
131 CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
132 s->sample_rate = tmp; /* store real sample rate */
139 #undef CHECK_IOCTL_ERROR
142 int ff_oss_audio_close(OSSAudioData *s)