]> git.sesse.net Git - ffmpeg/blob - libav/audio.c
* some minor modification by Philip Gladston
[ffmpeg] / libav / audio.c
1 /*
2  * Linux audio play and grab interface
3  * Copyright (c) 2000, 2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include "avformat.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <linux/soundcard.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/time.h>
30
31 const char *audio_device = "/dev/dsp";
32
33 #define AUDIO_BLOCK_SIZE 4096
34
35 typedef struct {
36     int fd;
37     int sample_rate;
38     int channels;
39     int frame_size; /* in bytes ! */
40     int codec_id;
41     UINT8 buffer[AUDIO_BLOCK_SIZE];
42     int buffer_ptr;
43 } AudioData;
44
45 static int audio_open(AudioData *s, int is_output)
46 {
47     int audio_fd;
48     int tmp, err;
49
50     /* open linux audio device */
51     if (is_output)
52         audio_fd = open(audio_device, O_WRONLY);
53     else
54         audio_fd = open(audio_device, O_RDONLY);
55     if (audio_fd < 0) {
56         perror(audio_device);
57         return -EIO;
58     }
59
60     /* non blocking mode */
61     fcntl(audio_fd, F_SETFL, O_NONBLOCK);
62
63     s->frame_size = AUDIO_BLOCK_SIZE;
64 #if 0
65     tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
66     err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
67     if (err < 0) {
68         perror("SNDCTL_DSP_SETFRAGMENT");
69     }
70 #endif
71
72     /* select format : favour native format */
73     err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
74     
75 #ifdef WORDS_BIGENDIAN
76     if (tmp & AFMT_S16_BE) {
77         tmp = AFMT_S16_BE;
78     } else if (tmp & AFMT_S16_LE) {
79         tmp = AFMT_S16_LE;
80     } else {
81         tmp = 0;
82     }
83 #else
84     if (tmp & AFMT_S16_LE) {
85         tmp = AFMT_S16_LE;
86     } else if (tmp & AFMT_S16_BE) {
87         tmp = AFMT_S16_BE;
88     } else {
89         tmp = 0;
90     }
91 #endif
92
93     switch(tmp) {
94     case AFMT_S16_LE:
95         s->codec_id = CODEC_ID_PCM_S16LE;
96         break;
97     case AFMT_S16_BE:
98         s->codec_id = CODEC_ID_PCM_S16BE;
99         break;
100     default:
101         fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
102         close(audio_fd);
103         return -EIO;
104     }
105     err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
106     if (err < 0) {
107         perror("SNDCTL_DSP_SETFMT");
108         goto fail;
109     }
110     
111     tmp = (s->channels == 2);
112     err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
113     if (err < 0) {
114         perror("SNDCTL_DSP_STEREO");
115         goto fail;
116     }
117     
118     tmp = s->sample_rate;
119     err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
120     if (err < 0) {
121         perror("SNDCTL_DSP_SPEED");
122         goto fail;
123     }
124     s->sample_rate = tmp; /* store real sample rate */
125     s->fd = audio_fd;
126
127     return 0;
128  fail:
129     close(audio_fd);
130     return -EIO;
131 }
132
133 static int audio_close(AudioData *s)
134 {
135     close(s->fd);
136     return 0;
137 }
138
139 /* sound output support */
140 static int audio_write_header(AVFormatContext *s1)
141 {
142     AudioData *s;
143     AVStream *st;
144     int ret;
145
146     s = av_mallocz(sizeof(AudioData));
147     if (!s)
148         return -ENOMEM;
149     s1->priv_data = s;
150
151     st = s1->streams[0];
152     s->sample_rate = st->codec.sample_rate;
153     s->channels = st->codec.channels;
154     ret = audio_open(s, 1);
155     if (ret < 0) {
156         free(s);
157         return -EIO;
158     } else {
159         return 0;
160     }
161 }
162
163 static int audio_write_packet(AVFormatContext *s1, int stream_index,
164                               UINT8 *buf, int size, int force_pts)
165 {
166     AudioData *s = s1->priv_data;
167     int len, ret;
168
169     while (size > 0) {
170         len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
171         if (len > size)
172             len = size;
173         memcpy(s->buffer + s->buffer_ptr, buf, len);
174         s->buffer_ptr += len;
175         if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
176             for(;;) {
177                 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
178                 if (ret != 0)
179                     break;
180                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
181                     return -EIO;
182             }
183             s->buffer_ptr = 0;
184         }
185         buf += len;
186         size -= len;
187     }
188     return 0;
189 }
190
191 static int audio_write_trailer(AVFormatContext *s1)
192 {
193     AudioData *s = s1->priv_data;
194
195     audio_close(s);
196     free(s);
197     return 0;
198 }
199
200 /* grab support */
201
202 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
203 {
204     AudioData *s;
205     AVStream *st;
206     int ret;
207
208     if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
209         return -1;
210
211     s = av_mallocz(sizeof(AudioData));
212     if (!s)
213         return -ENOMEM;
214     st = av_mallocz(sizeof(AVStream));
215     if (!st) {
216         free(s);
217         return -ENOMEM;
218     }
219     s1->priv_data = s;
220     s1->nb_streams = 1;
221     s1->streams[0] = st;
222     s->sample_rate = ap->sample_rate;
223     s->channels = ap->channels;
224
225     ret = audio_open(s, 0);
226     if (ret < 0) {
227         free(st);
228         free(s);
229         return -EIO;
230     } else {
231         /* take real parameters */
232         st->codec.codec_type = CODEC_TYPE_AUDIO;
233         st->codec.codec_id = s->codec_id;
234         st->codec.sample_rate = s->sample_rate;
235         st->codec.channels = s->channels;
236         return 0;
237     }
238 }
239
240 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
241 {
242     AudioData *s = s1->priv_data;
243     int ret;
244
245     if (av_new_packet(pkt, s->frame_size) < 0)
246         return -EIO;
247     for(;;) {
248         ret = read(s->fd, pkt->data, pkt->size);
249         if (ret > 0)
250             break;
251         if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
252             av_free_packet(pkt);
253             pkt->size = 0;
254             return 0;
255         }
256         if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
257             av_free_packet(pkt);
258             return -EIO;
259         }
260     }
261     pkt->size = ret;
262     return 0;
263 }
264
265 static int audio_read_close(AVFormatContext *s1)
266 {
267     AudioData *s = s1->priv_data;
268
269     audio_close(s);
270     free(s);
271     return 0;
272 }
273
274 AVFormat audio_device_format = {
275     "audio_device",
276     "audio grab and output",
277     "",
278     "",
279     /* XXX: we make the assumption that the soundcard accepts this format */
280     /* XXX: find better solution with "preinit" method, needed also in
281        other formats */
282 #ifdef WORDS_BIGENDIAN
283     CODEC_ID_PCM_S16BE,
284 #else
285     CODEC_ID_PCM_S16LE,
286 #endif
287     CODEC_ID_NONE,
288     audio_write_header,
289     audio_write_packet,
290     audio_write_trailer,
291
292     audio_read_header,
293     audio_read_packet,
294     audio_read_close,
295     NULL,
296     AVFMT_NOFILE,
297 };