2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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>
34 #include <sys/ioctl.h>
36 #include "libavutil/log.h"
38 #include "libavcodec/avcodec.h"
40 #include "libavformat/avformat.h"
44 int ff_oss_audio_open(AVFormatContext *s1, int is_output,
45 const char *audio_device)
47 OSSAudioData *s = s1->priv_data;
50 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 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
70 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
72 #define CHECK_IOCTL_ERROR(event) \
74 av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
75 av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
79 /* select format : favour native format
80 * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
81 * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
83 (void) ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
86 if (tmp & AFMT_S16_BE) {
88 } else if (tmp & AFMT_S16_LE) {
94 if (tmp & AFMT_S16_LE) {
96 } else if (tmp & AFMT_S16_BE) {
105 s->codec_id = AV_CODEC_ID_PCM_S16LE;
108 s->codec_id = AV_CODEC_ID_PCM_S16BE;
111 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
115 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
116 CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
118 tmp = (s->channels == 2);
119 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
120 CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
122 tmp = s->sample_rate;
123 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
124 CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
125 s->sample_rate = tmp; /* store real sample rate */
132 #undef CHECK_IOCTL_ERROR
135 int ff_oss_audio_close(OSSAudioData *s)