2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/soundcard.h>
27 #include <sys/ioctl.h>
31 const char *audio_device = "/dev/dsp";
33 #define AUDIO_BLOCK_SIZE 4096
39 int frame_size; /* in bytes ! */
41 UINT8 buffer[AUDIO_BLOCK_SIZE];
45 static int audio_open(AudioData *s, int is_output)
50 /* open linux audio device */
52 audio_fd = open(audio_device, O_WRONLY);
54 audio_fd = open(audio_device, O_RDONLY);
60 /* non blocking mode */
61 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
63 s->frame_size = AUDIO_BLOCK_SIZE;
65 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
66 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
68 perror("SNDCTL_DSP_SETFRAGMENT");
72 /* select format : favour native format */
73 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
75 #ifdef WORDS_BIGENDIAN
76 if (tmp & AFMT_S16_BE) {
78 } else if (tmp & AFMT_S16_LE) {
84 if (tmp & AFMT_S16_LE) {
86 } else if (tmp & AFMT_S16_BE) {
95 s->codec_id = CODEC_ID_PCM_S16LE;
98 s->codec_id = CODEC_ID_PCM_S16BE;
101 fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
105 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
107 perror("SNDCTL_DSP_SETFMT");
111 tmp = (s->channels == 2);
112 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
114 perror("SNDCTL_DSP_STEREO");
118 tmp = s->sample_rate;
119 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
121 perror("SNDCTL_DSP_SPEED");
124 s->sample_rate = tmp; /* store real sample rate */
133 static int audio_close(AudioData *s)
139 /* sound output support */
140 static int audio_write_header(AVFormatContext *s1)
146 s = av_mallocz(sizeof(AudioData));
152 s->sample_rate = st->codec.sample_rate;
153 s->channels = st->codec.channels;
154 ret = audio_open(s, 1);
163 static int audio_write_packet(AVFormatContext *s1, int stream_index,
164 UINT8 *buf, int size, int force_pts)
166 AudioData *s = s1->priv_data;
170 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
173 memcpy(s->buffer + s->buffer_ptr, buf, len);
174 s->buffer_ptr += len;
175 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
177 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
180 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
191 static int audio_write_trailer(AVFormatContext *s1)
193 AudioData *s = s1->priv_data;
202 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
208 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
211 s = av_mallocz(sizeof(AudioData));
214 st = av_mallocz(sizeof(AVStream));
222 s->sample_rate = ap->sample_rate;
223 s->channels = ap->channels;
225 ret = audio_open(s, 0);
231 /* take real parameters */
232 st->codec.codec_type = CODEC_TYPE_AUDIO;
233 st->codec.codec_id = s->codec_id;
234 st->codec.sample_rate = s->sample_rate;
235 st->codec.channels = s->channels;
240 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
242 AudioData *s = s1->priv_data;
245 if (av_new_packet(pkt, s->frame_size) < 0)
248 ret = read(s->fd, pkt->data, pkt->size);
251 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
256 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
265 static int audio_read_close(AVFormatContext *s1)
267 AudioData *s = s1->priv_data;
274 AVFormat audio_device_format = {
276 "audio grab and output",
279 /* XXX: we make the assumption that the soundcard accepts this format */
280 /* XXX: find better solution with "preinit" method, needed also in
282 #ifdef WORDS_BIGENDIAN