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"
42 #include "oss_audio.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");
53 audio_fd = avpriv_open(audio_device, O_WRONLY);
55 audio_fd = avpriv_open(audio_device, O_RDONLY);
57 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
61 if (flip && *flip == '1') {
65 /* non blocking mode */
67 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
69 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
71 /* select format : favour native format */
72 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
75 if (tmp & AFMT_S16_BE) {
77 } else if (tmp & AFMT_S16_LE) {
83 if (tmp & AFMT_S16_LE) {
85 } else if (tmp & AFMT_S16_BE) {
94 s->codec_id = AV_CODEC_ID_PCM_S16LE;
97 s->codec_id = AV_CODEC_ID_PCM_S16BE;
100 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
104 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
106 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
110 tmp = (s->channels == 2);
111 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
113 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
117 tmp = s->sample_rate;
118 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
120 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
123 s->sample_rate = tmp; /* store real sample rate */
132 int ff_oss_audio_close(OSSAudioData *s)