OSDN Git Service

6df7f34d7ee09ee49561c4eec088b67e91712926
[coroid/libav_saccubus.git] / 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
84     /* select format : favour native format */
85     err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
86
87 #if HAVE_BIGENDIAN
88     if (tmp & AFMT_S16_BE) {
89         tmp = AFMT_S16_BE;
90     } else if (tmp & AFMT_S16_LE) {
91         tmp = AFMT_S16_LE;
92     } else {
93         tmp = 0;
94     }
95 #else
96     if (tmp & AFMT_S16_LE) {
97         tmp = AFMT_S16_LE;
98     } else if (tmp & AFMT_S16_BE) {
99         tmp = AFMT_S16_BE;
100     } else {
101         tmp = 0;
102     }
103 #endif
104
105     switch(tmp) {
106     case AFMT_S16_LE:
107         s->codec_id = CODEC_ID_PCM_S16LE;
108         break;
109     case AFMT_S16_BE:
110         s->codec_id = CODEC_ID_PCM_S16BE;
111         break;
112     default:
113         av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
114         close(audio_fd);
115         return AVERROR(EIO);
116     }
117     err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
118     if (err < 0) {
119         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
120         goto fail;
121     }
122
123     tmp = (s->channels == 2);
124     err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
125     if (err < 0) {
126         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
127         goto fail;
128     }
129
130     tmp = s->sample_rate;
131     err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
132     if (err < 0) {
133         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
134         goto fail;
135     }
136     s->sample_rate = tmp; /* store real sample rate */
137     s->fd = audio_fd;
138
139     return 0;
140  fail:
141     close(audio_fd);
142     return AVERROR(EIO);
143 }
144
145 static int audio_close(AudioData *s)
146 {
147     close(s->fd);
148     return 0;
149 }
150
151 /* sound output support */
152 static int audio_write_header(AVFormatContext *s1)
153 {
154     AudioData *s = s1->priv_data;
155     AVStream *st;
156     int ret;
157
158     st = s1->streams[0];
159     s->sample_rate = st->codec->sample_rate;
160     s->channels = st->codec->channels;
161     ret = audio_open(s1, 1, s1->filename);
162     if (ret < 0) {
163         return AVERROR(EIO);
164     } else {
165         return 0;
166     }
167 }
168
169 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
170 {
171     AudioData *s = s1->priv_data;
172     int len, ret;
173     int size= pkt->size;
174     uint8_t *buf= pkt->data;
175
176     while (size > 0) {
177         len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
178         memcpy(s->buffer + s->buffer_ptr, buf, len);
179         s->buffer_ptr += len;
180         if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
181             for(;;) {
182                 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
183                 if (ret > 0)
184                     break;
185                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
186                     return AVERROR(EIO);
187             }
188             s->buffer_ptr = 0;
189         }
190         buf += len;
191         size -= len;
192     }
193     return 0;
194 }
195
196 static int audio_write_trailer(AVFormatContext *s1)
197 {
198     AudioData *s = s1->priv_data;
199
200     audio_close(s);
201     return 0;
202 }
203
204 /* grab support */
205
206 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
207 {
208     AudioData *s = s1->priv_data;
209     AVStream *st;
210     int ret;
211
212     st = av_new_stream(s1, 0);
213     if (!st) {
214         return AVERROR(ENOMEM);
215     }
216
217     ret = audio_open(s1, 0, s1->filename);
218     if (ret < 0) {
219         return AVERROR(EIO);
220     }
221
222     /* take real parameters */
223     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
224     st->codec->codec_id = s->codec_id;
225     st->codec->sample_rate = s->sample_rate;
226     st->codec->channels = s->channels;
227
228     av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
229     return 0;
230 }
231
232 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
233 {
234     AudioData *s = s1->priv_data;
235     int ret, bdelay;
236     int64_t cur_time;
237     struct audio_buf_info abufi;
238
239     if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
240         return ret;
241
242     ret = read(s->fd, pkt->data, pkt->size);
243     if (ret <= 0){
244         av_free_packet(pkt);
245         pkt->size = 0;
246         if (ret<0)  return AVERROR(errno);
247         else        return AVERROR_EOF;
248     }
249     pkt->size = ret;
250
251     /* compute pts of the start of the packet */
252     cur_time = av_gettime();
253     bdelay = ret;
254     if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
255         bdelay += abufi.bytes;
256     }
257     /* subtract time represented by the number of bytes in the audio fifo */
258     cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
259
260     /* convert to wanted units */
261     pkt->pts = cur_time;
262
263     if (s->flip_left && s->channels == 2) {
264         int i;
265         short *p = (short *) pkt->data;
266
267         for (i = 0; i < ret; i += 4) {
268             *p = ~*p;
269             p += 2;
270         }
271     }
272     return 0;
273 }
274
275 static int audio_read_close(AVFormatContext *s1)
276 {
277     AudioData *s = s1->priv_data;
278
279     audio_close(s);
280     return 0;
281 }
282
283 #if CONFIG_OSS_INDEV
284 static const AVOption options[] = {
285     { "sample_rate", "", offsetof(AudioData, sample_rate), FF_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
286     { "channels",    "", offsetof(AudioData, channels),    FF_OPT_TYPE_INT, {.dbl = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
287     { NULL },
288 };
289
290 static const AVClass oss_demuxer_class = {
291     .class_name     = "OSS demuxer",
292     .item_name      = av_default_item_name,
293     .option         = options,
294     .version        = LIBAVUTIL_VERSION_INT,
295 };
296
297 AVInputFormat ff_oss_demuxer = {
298     "oss",
299     NULL_IF_CONFIG_SMALL("Open Sound System capture"),
300     sizeof(AudioData),
301     NULL,
302     audio_read_header,
303     audio_read_packet,
304     audio_read_close,
305     .flags = AVFMT_NOFILE,
306     .priv_class = &oss_demuxer_class,
307 };
308 #endif
309
310 #if CONFIG_OSS_OUTDEV
311 AVOutputFormat ff_oss_muxer = {
312     "oss",
313     NULL_IF_CONFIG_SMALL("Open Sound System playback"),
314     "",
315     "",
316     sizeof(AudioData),
317     /* XXX: we make the assumption that the soundcard accepts this format */
318     /* XXX: find better solution with "preinit" method, needed also in
319        other formats */
320     AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
321     CODEC_ID_NONE,
322     audio_write_header,
323     audio_write_packet,
324     audio_write_trailer,
325     .flags = AVFMT_NOFILE,
326 };
327 #endif