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 #define AUDIO_BLOCK_SIZE 4096
37 int frame_size
; /* in bytes ! */
40 uint8_t buffer
[AUDIO_BLOCK_SIZE
];
44 static int audio_open(AudioData
*s
, int is_output
, const char *audio_device
)
48 char *flip
= getenv("AUDIO_FLIP_LEFT");
50 /* open linux audio device */
52 audio_device
= "/dev/dsp";
55 audio_fd
= open(audio_device
, O_WRONLY
);
57 audio_fd
= open(audio_device
, O_RDONLY
);
63 if (flip
&& *flip
== '1') {
67 /* non blocking mode */
69 fcntl(audio_fd
, F_SETFL
, O_NONBLOCK
);
71 s
->frame_size
= AUDIO_BLOCK_SIZE
;
73 tmp
= (NB_FRAGMENTS
<< 16) | FRAGMENT_BITS
;
74 err
= ioctl(audio_fd
, SNDCTL_DSP_SETFRAGMENT
, &tmp
);
76 perror("SNDCTL_DSP_SETFRAGMENT");
80 /* select format : favour native format */
81 err
= ioctl(audio_fd
, SNDCTL_DSP_GETFMTS
, &tmp
);
83 #ifdef WORDS_BIGENDIAN
84 if (tmp
& AFMT_S16_BE
) {
86 } else if (tmp
& AFMT_S16_LE
) {
92 if (tmp
& AFMT_S16_LE
) {
94 } else if (tmp
& AFMT_S16_BE
) {
103 s
->codec_id
= CODEC_ID_PCM_S16LE
;
106 s
->codec_id
= CODEC_ID_PCM_S16BE
;
109 av_log(NULL
, AV_LOG_ERROR
, "Soundcard does not support 16 bit sample format\n");
113 err
=ioctl(audio_fd
, SNDCTL_DSP_SETFMT
, &tmp
);
115 perror("SNDCTL_DSP_SETFMT");
119 tmp
= (s
->channels
== 2);
120 err
= ioctl(audio_fd
, SNDCTL_DSP_STEREO
, &tmp
);
122 perror("SNDCTL_DSP_STEREO");
128 tmp
= s
->sample_rate
;
129 err
= ioctl(audio_fd
, SNDCTL_DSP_SPEED
, &tmp
);
131 perror("SNDCTL_DSP_SPEED");
134 s
->sample_rate
= tmp
; /* store real sample rate */
143 static int audio_close(AudioData
*s
)
149 /* sound output support */
150 static int audio_write_header(AVFormatContext
*s1
)
152 AudioData
*s
= s1
->priv_data
;
157 s
->sample_rate
= st
->codec
.sample_rate
;
158 s
->channels
= st
->codec
.channels
;
159 ret
= audio_open(s
, 1, NULL
);
167 static int audio_write_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
169 AudioData
*s
= s1
->priv_data
;
172 uint8_t *buf
= pkt
->data
;
175 len
= AUDIO_BLOCK_SIZE
- s
->buffer_ptr
;
178 memcpy(s
->buffer
+ s
->buffer_ptr
, buf
, len
);
179 s
->buffer_ptr
+= len
;
180 if (s
->buffer_ptr
>= AUDIO_BLOCK_SIZE
) {
182 ret
= write(s
->fd
, s
->buffer
, AUDIO_BLOCK_SIZE
);
185 if (ret
< 0 && (errno
!= EAGAIN
&& errno
!= EINTR
))
196 static int audio_write_trailer(AVFormatContext
*s1
)
198 AudioData
*s
= s1
->priv_data
;
206 static int audio_read_header(AVFormatContext
*s1
, AVFormatParameters
*ap
)
208 AudioData
*s
= s1
->priv_data
;
212 if (!ap
|| ap
->sample_rate
<= 0 || ap
->channels
<= 0)
215 st
= av_new_stream(s1
, 0);
219 s
->sample_rate
= ap
->sample_rate
;
220 s
->channels
= ap
->channels
;
222 ret
= audio_open(s
, 0, ap
->device
);
228 /* take real parameters */
229 st
->codec
.codec_type
= CODEC_TYPE_AUDIO
;
230 st
->codec
.codec_id
= s
->codec_id
;
231 st
->codec
.sample_rate
= s
->sample_rate
;
232 st
->codec
.channels
= s
->channels
;
234 av_set_pts_info(st
, 48, 1, 1000000); /* 48 bits pts in us */
238 static int audio_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
240 AudioData
*s
= s1
->priv_data
;
243 struct audio_buf_info abufi
;
245 if (av_new_packet(pkt
, s
->frame_size
) < 0)
252 tv
.tv_usec
= 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
257 /* This will block until data is available or we get a timeout */
258 (void) select(s
->fd
+ 1, &fds
, 0, 0, &tv
);
260 ret
= read(s
->fd
, pkt
->data
, pkt
->size
);
263 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
266 pkt
->pts
= av_gettime() & ((1LL << 48) - 1);
269 if (!(ret
== 0 || (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)))) {
276 /* compute pts of the start of the packet */
277 cur_time
= av_gettime();
279 if (ioctl(s
->fd
, SNDCTL_DSP_GETISPACE
, &abufi
) == 0) {
280 bdelay
+= abufi
.bytes
;
282 /* substract time represented by the number of bytes in the audio fifo */
283 cur_time
-= (bdelay
* 1000000LL) / (s
->sample_rate
* s
->channels
);
285 /* convert to wanted units */
286 pkt
->pts
= cur_time
& ((1LL << 48) - 1);
288 if (s
->flip_left
&& s
->channels
== 2) {
290 short *p
= (short *) pkt
->data
;
292 for (i
= 0; i
< ret
; i
+= 4) {
300 static int audio_read_close(AVFormatContext
*s1
)
302 AudioData
*s
= s1
->priv_data
;
308 static AVInputFormat audio_in_format
= {
310 "audio grab and output",
316 .flags
= AVFMT_NOFILE
,
319 static AVOutputFormat audio_out_format
= {
321 "audio grab and output",
325 /* XXX: we make the assumption that the soundcard accepts this format */
326 /* XXX: find better solution with "preinit" method, needed also in
328 #ifdef WORDS_BIGENDIAN
337 .flags
= AVFMT_NOFILE
,
342 av_register_input_format(&audio_in_format
);
343 av_register_output_format(&audio_out_format
);