oss_audio: Split muxer and demuxer
[libav.git] / libavdevice / oss_audio.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux audio play and grab interface
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
8be1c656 21
4f786086 22#include "config.h"
d6e1d371 23
de6d9b64 24#include <string.h>
d6e1d371 25
b250f9c6 26#if HAVE_SOUNDCARD_H
8c802695
JM
27#include <soundcard.h>
28#else
b13788c5 29#include <sys/soundcard.h>
8c802695 30#endif
d6e1d371 31
de6d9b64
FB
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/ioctl.h>
de6d9b64 35
245976da 36#include "libavutil/log.h"
d6e1d371 37
245976da 38#include "libavcodec/avcodec.h"
4f786086 39
d6e1d371 40#include "libavformat/avformat.h"
4972b26f 41
d6e1d371 42#include "oss_audio.h"
de6d9b64 43
d6e1d371
NM
44int ff_oss_audio_open(AVFormatContext *s1, int is_output,
45 const char *audio_device)
de6d9b64 46{
d6e1d371 47 OSSAudioData *s = s1->priv_data;
4972b26f 48 int audio_fd;
de6d9b64 49 int tmp, err;
1de1cce2 50 char *flip = getenv("AUDIO_FLIP_LEFT");
de6d9b64 51
4972b26f 52 if (is_output)
71bf6b41 53 audio_fd = avpriv_open(audio_device, O_WRONLY);
de6d9b64 54 else
71bf6b41 55 audio_fd = avpriv_open(audio_device, O_RDONLY);
de6d9b64 56 if (audio_fd < 0) {
0c26e964 57 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
6f3e0b21 58 return AVERROR(EIO);
de6d9b64
FB
59 }
60
1de1cce2
PG
61 if (flip && *flip == '1') {
62 s->flip_left = 1;
63 }
64
de6d9b64 65 /* non blocking mode */
4364a3e0
FB
66 if (!is_output)
67 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
de6d9b64 68
d6e1d371 69 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
de6d9b64 70
4972b26f
FB
71 /* select format : favour native format */
72 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
115329f1 73
63613fe6 74#if HAVE_BIGENDIAN
4972b26f
FB
75 if (tmp & AFMT_S16_BE) {
76 tmp = AFMT_S16_BE;
77 } else if (tmp & AFMT_S16_LE) {
78 tmp = AFMT_S16_LE;
79 } else {
80 tmp = 0;
81 }
82#else
83 if (tmp & AFMT_S16_LE) {
84 tmp = AFMT_S16_LE;
85 } else if (tmp & AFMT_S16_BE) {
86 tmp = AFMT_S16_BE;
87 } else {
88 tmp = 0;
89 }
90#endif
91
92 switch(tmp) {
93 case AFMT_S16_LE:
36ef5369 94 s->codec_id = AV_CODEC_ID_PCM_S16LE;
4972b26f
FB
95 break;
96 case AFMT_S16_BE:
36ef5369 97 s->codec_id = AV_CODEC_ID_PCM_S16BE;
4972b26f
FB
98 break;
99 default:
0c26e964 100 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
4972b26f 101 close(audio_fd);
6f3e0b21 102 return AVERROR(EIO);
4972b26f
FB
103 }
104 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
de6d9b64 105 if (err < 0) {
0c26e964 106 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
de6d9b64
FB
107 goto fail;
108 }
115329f1 109
4972b26f
FB
110 tmp = (s->channels == 2);
111 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
de6d9b64 112 if (err < 0) {
0c26e964 113 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
de6d9b64
FB
114 goto fail;
115 }
115329f1 116
4972b26f
FB
117 tmp = s->sample_rate;
118 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
de6d9b64 119 if (err < 0) {
0c26e964 120 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
de6d9b64
FB
121 goto fail;
122 }
4972b26f 123 s->sample_rate = tmp; /* store real sample rate */
de6d9b64
FB
124 s->fd = audio_fd;
125
126 return 0;
127 fail:
128 close(audio_fd);
6f3e0b21 129 return AVERROR(EIO);
de6d9b64
FB
130}
131
d6e1d371 132int ff_oss_audio_close(OSSAudioData *s)
de6d9b64 133{
de6d9b64 134 close(s->fd);
4972b26f
FB
135 return 0;
136}