2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/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 ! */
42 UINT8 buffer
[AUDIO_BLOCK_SIZE
];
46 static int audio_open(AudioData
*s
, int is_output
)
50 char *flip
= getenv("AUDIO_FLIP_LEFT");
52 /* open linux audio device */
54 audio_fd
= open(audio_device
, O_WRONLY
);
56 audio_fd
= open(audio_device
, O_RDONLY
);
62 if (flip
&& *flip
== '1') {
66 /* non blocking mode */
67 fcntl(audio_fd
, F_SETFL
, O_NONBLOCK
);
69 s
->frame_size
= AUDIO_BLOCK_SIZE
;
71 tmp
= (NB_FRAGMENTS
<< 16) | FRAGMENT_BITS
;
72 err
= ioctl(audio_fd
, SNDCTL_DSP_SETFRAGMENT
, &tmp
);
74 perror("SNDCTL_DSP_SETFRAGMENT");
78 /* select format : favour native format */
79 err
= ioctl(audio_fd
, SNDCTL_DSP_GETFMTS
, &tmp
);
81 #ifdef WORDS_BIGENDIAN
82 if (tmp
& AFMT_S16_BE
) {
84 } else if (tmp
& AFMT_S16_LE
) {
90 if (tmp
& AFMT_S16_LE
) {
92 } else if (tmp
& AFMT_S16_BE
) {
101 s
->codec_id
= CODEC_ID_PCM_S16LE
;
104 s
->codec_id
= CODEC_ID_PCM_S16BE
;
107 fprintf(stderr
, "Soundcard does not support 16 bit sample format\n");
111 err
=ioctl(audio_fd
, SNDCTL_DSP_SETFMT
, &tmp
);
113 perror("SNDCTL_DSP_SETFMT");
117 tmp
= (s
->channels
== 2);
118 err
= ioctl(audio_fd
, SNDCTL_DSP_STEREO
, &tmp
);
120 perror("SNDCTL_DSP_STEREO");
126 tmp
= s
->sample_rate
;
127 err
= ioctl(audio_fd
, SNDCTL_DSP_SPEED
, &tmp
);
129 perror("SNDCTL_DSP_SPEED");
132 s
->sample_rate
= tmp
; /* store real sample rate */
141 static int audio_close(AudioData
*s
)
147 /* sound output support */
148 static int audio_write_header(AVFormatContext
*s1
)
150 AudioData
*s
= s1
->priv_data
;
155 s
->sample_rate
= st
->codec
.sample_rate
;
156 s
->channels
= st
->codec
.channels
;
157 ret
= audio_open(s
, 1);
165 static int audio_write_packet(AVFormatContext
*s1
, int stream_index
,
166 UINT8
*buf
, int size
, int force_pts
)
168 AudioData
*s
= s1
->priv_data
;
172 len
= AUDIO_BLOCK_SIZE
- s
->buffer_ptr
;
175 memcpy(s
->buffer
+ s
->buffer_ptr
, buf
, len
);
176 s
->buffer_ptr
+= len
;
177 if (s
->buffer_ptr
>= AUDIO_BLOCK_SIZE
) {
179 ret
= write(s
->fd
, s
->buffer
, AUDIO_BLOCK_SIZE
);
182 if (ret
< 0 && (errno
!= EAGAIN
&& errno
!= EINTR
))
193 static int audio_write_trailer(AVFormatContext
*s1
)
195 AudioData
*s
= s1
->priv_data
;
203 static int audio_read_header(AVFormatContext
*s1
, AVFormatParameters
*ap
)
205 AudioData
*s
= s1
->priv_data
;
209 if (!ap
|| ap
->sample_rate
<= 0 || ap
->channels
<= 0)
212 st
= av_new_stream(s1
, 0);
216 s
->sample_rate
= ap
->sample_rate
;
217 s
->channels
= ap
->channels
;
219 ret
= audio_open(s
, 0);
224 /* take real parameters */
225 st
->codec
.codec_type
= CODEC_TYPE_AUDIO
;
226 st
->codec
.codec_id
= s
->codec_id
;
227 st
->codec
.sample_rate
= s
->sample_rate
;
228 st
->codec
.channels
= s
->channels
;
233 static int audio_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
235 AudioData
*s
= s1
->priv_data
;
238 if (av_new_packet(pkt
, s
->frame_size
) < 0)
241 ret
= read(s
->fd
, pkt
->data
, pkt
->size
);
244 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
249 if (!(ret
== 0 || (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)))) {
255 if (s
->flip_left
&& s
->channels
== 2) {
257 short *p
= (short *) pkt
->data
;
259 for (i
= 0; i
< ret
; i
+= 4) {
267 static int audio_read_close(AVFormatContext
*s1
)
269 AudioData
*s
= s1
->priv_data
;
275 AVInputFormat audio_in_format
= {
277 "audio grab and output",
286 AVOutputFormat audio_out_format
= {
288 "audio grab and output",
292 /* XXX: we make the assumption that the soundcard accepts this format */
293 /* XXX: find better solution with "preinit" method, needed also in
295 #ifdef WORDS_BIGENDIAN
309 av_register_input_format(&audio_in_format
);
310 av_register_output_format(&audio_out_format
);