]> git.sesse.net Git - ffmpeg/blob - libavdevice/oss_audio.c
vorbis: Remove pointless DEBUG #ifdef around debug output macros.
[ffmpeg] / libavdevice / oss_audio.c
1 /*
2  * Linux audio play and grab interface
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28 #if HAVE_SOUNDCARD_H
29 #include <soundcard.h>
30 #else
31 #include <sys/soundcard.h>
32 #endif
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/time.h>
37 #include <sys/select.h>
38
39 #include "libavutil/log.h"
40 #include "libavutil/opt.h"
41 #include "libavcodec/avcodec.h"
42 #include "libavformat/avformat.h"
43
44 #define AUDIO_BLOCK_SIZE 4096
45
46 typedef struct {
47     AVClass *class;
48     int fd;
49     int sample_rate;
50     int channels;
51     int frame_size; /* in bytes ! */
52     enum CodecID codec_id;
53     unsigned int flip_left : 1;
54     uint8_t buffer[AUDIO_BLOCK_SIZE];
55     int buffer_ptr;
56 } AudioData;
57
58 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
59 {
60     AudioData *s = s1->priv_data;
61     int audio_fd;
62     int tmp, err;
63     char *flip = getenv("AUDIO_FLIP_LEFT");
64
65     if (is_output)
66         audio_fd = open(audio_device, O_WRONLY);
67     else
68         audio_fd = open(audio_device, O_RDONLY);
69     if (audio_fd < 0) {
70         av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
71         return AVERROR(EIO);
72     }
73
74     if (flip && *flip == '1') {
75         s->flip_left = 1;
76     }
77
78     /* non blocking mode */
79     if (!is_output)
80         fcntl(audio_fd, F_SETFL, O_NONBLOCK);
81
82     s->frame_size = AUDIO_BLOCK_SIZE;
83 #if 0
84     tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
85     err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
86     if (err < 0) {
87         perror("SNDCTL_DSP_SETFRAGMENT");
88     }
89 #endif
90
91     /* select format : favour native format */
92     err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
93
94 #if HAVE_BIGENDIAN
95     if (tmp & AFMT_S16_BE) {
96         tmp = AFMT_S16_BE;
97     } else if (tmp & AFMT_S16_LE) {
98         tmp = AFMT_S16_LE;
99     } else {
100         tmp = 0;
101     }
102 #else
103     if (tmp & AFMT_S16_LE) {
104         tmp = AFMT_S16_LE;
105     } else if (tmp & AFMT_S16_BE) {
106         tmp = AFMT_S16_BE;
107     } else {
108         tmp = 0;
109     }
110 #endif
111
112     switch(tmp) {
113     case AFMT_S16_LE:
114         s->codec_id = CODEC_ID_PCM_S16LE;
115         break;
116     case AFMT_S16_BE:
117         s->codec_id = CODEC_ID_PCM_S16BE;
118         break;
119     default:
120         av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
121         close(audio_fd);
122         return AVERROR(EIO);
123     }
124     err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
125     if (err < 0) {
126         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
127         goto fail;
128     }
129
130     tmp = (s->channels == 2);
131     err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
132     if (err < 0) {
133         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
134         goto fail;
135     }
136
137     tmp = s->sample_rate;
138     err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
139     if (err < 0) {
140         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
141         goto fail;
142     }
143     s->sample_rate = tmp; /* store real sample rate */
144     s->fd = audio_fd;
145
146     return 0;
147  fail:
148     close(audio_fd);
149     return AVERROR(EIO);
150 }
151
152 static int audio_close(AudioData *s)
153 {
154     close(s->fd);
155     return 0;
156 }
157
158 /* sound output support */
159 static int audio_write_header(AVFormatContext *s1)
160 {
161     AudioData *s = s1->priv_data;
162     AVStream *st;
163     int ret;
164
165     st = s1->streams[0];
166     s->sample_rate = st->codec->sample_rate;
167     s->channels = st->codec->channels;
168     ret = audio_open(s1, 1, s1->filename);
169     if (ret < 0) {
170         return AVERROR(EIO);
171     } else {
172         return 0;
173     }
174 }
175
176 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
177 {
178     AudioData *s = s1->priv_data;
179     int len, ret;
180     int size= pkt->size;
181     uint8_t *buf= pkt->data;
182
183     while (size > 0) {
184         len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
185         if (len > size)
186             len = size;
187         memcpy(s->buffer + s->buffer_ptr, buf, len);
188         s->buffer_ptr += len;
189         if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
190             for(;;) {
191                 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
192                 if (ret > 0)
193                     break;
194                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
195                     return AVERROR(EIO);
196             }
197             s->buffer_ptr = 0;
198         }
199         buf += len;
200         size -= len;
201     }
202     return 0;
203 }
204
205 static int audio_write_trailer(AVFormatContext *s1)
206 {
207     AudioData *s = s1->priv_data;
208
209     audio_close(s);
210     return 0;
211 }
212
213 /* grab support */
214
215 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
216 {
217     AudioData *s = s1->priv_data;
218     AVStream *st;
219     int ret;
220
221 #if FF_API_FORMAT_PARAMETERS
222     if (ap->sample_rate > 0)
223         s->sample_rate = ap->sample_rate;
224     if (ap->channels > 0)
225         s->channels = ap->channels;
226 #endif
227
228     st = av_new_stream(s1, 0);
229     if (!st) {
230         return AVERROR(ENOMEM);
231     }
232
233     ret = audio_open(s1, 0, s1->filename);
234     if (ret < 0) {
235         return AVERROR(EIO);
236     }
237
238     /* take real parameters */
239     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
240     st->codec->codec_id = s->codec_id;
241     st->codec->sample_rate = s->sample_rate;
242     st->codec->channels = s->channels;
243
244     av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
245     return 0;
246 }
247
248 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
249 {
250     AudioData *s = s1->priv_data;
251     int ret, bdelay;
252     int64_t cur_time;
253     struct audio_buf_info abufi;
254
255     if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
256         return ret;
257
258     ret = read(s->fd, pkt->data, pkt->size);
259     if (ret <= 0){
260         av_free_packet(pkt);
261         pkt->size = 0;
262         if (ret<0)  return AVERROR(errno);
263         else        return AVERROR_EOF;
264     }
265     pkt->size = ret;
266
267     /* compute pts of the start of the packet */
268     cur_time = av_gettime();
269     bdelay = ret;
270     if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
271         bdelay += abufi.bytes;
272     }
273     /* subtract time represented by the number of bytes in the audio fifo */
274     cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
275
276     /* convert to wanted units */
277     pkt->pts = cur_time;
278
279     if (s->flip_left && s->channels == 2) {
280         int i;
281         short *p = (short *) pkt->data;
282
283         for (i = 0; i < ret; i += 4) {
284             *p = ~*p;
285             p += 2;
286         }
287     }
288     return 0;
289 }
290
291 static int audio_read_close(AVFormatContext *s1)
292 {
293     AudioData *s = s1->priv_data;
294
295     audio_close(s);
296     return 0;
297 }
298
299 #if CONFIG_OSS_INDEV
300 static const AVOption options[] = {
301     { "sample_rate", "", offsetof(AudioData, sample_rate), FF_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
302     { "channels",    "", offsetof(AudioData, channels),    FF_OPT_TYPE_INT, {.dbl = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
303     { NULL },
304 };
305
306 static const AVClass oss_demuxer_class = {
307     .class_name     = "OSS demuxer",
308     .item_name      = av_default_item_name,
309     .option         = options,
310     .version        = LIBAVUTIL_VERSION_INT,
311 };
312
313 AVInputFormat ff_oss_demuxer = {
314     "oss",
315     NULL_IF_CONFIG_SMALL("Open Sound System capture"),
316     sizeof(AudioData),
317     NULL,
318     audio_read_header,
319     audio_read_packet,
320     audio_read_close,
321     .flags = AVFMT_NOFILE,
322     .priv_class = &oss_demuxer_class,
323 };
324 #endif
325
326 #if CONFIG_OSS_OUTDEV
327 AVOutputFormat ff_oss_muxer = {
328     "oss",
329     NULL_IF_CONFIG_SMALL("Open Sound System playback"),
330     "",
331     "",
332     sizeof(AudioData),
333     /* XXX: we make the assumption that the soundcard accepts this format */
334     /* XXX: find better solution with "preinit" method, needed also in
335        other formats */
336     AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
337     CODEC_ID_NONE,
338     audio_write_header,
339     audio_write_packet,
340     audio_write_trailer,
341     .flags = AVFMT_NOFILE,
342 };
343 #endif