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