annotate audio.c @ 1461:23f88db38eb5 libavformat

allow spaces in source and build directory names out of tree builds from a source dir with spaces is impossible due to how make handles vpath
author mru
date Wed, 08 Nov 2006 00:02:15 +0000
parents 0899bfe4105c
children 2f59a73884af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * Linux audio play and grab interface
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 887
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #include <stdlib.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 #include <stdio.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 #include <string.h>
754
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
26 #ifdef __OpenBSD__
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
27 #include <soundcard.h>
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
28 #else
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 #include <sys/soundcard.h>
754
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
30 #endif
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 #include <fcntl.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 #include <sys/ioctl.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 #include <sys/mman.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 #include <sys/time.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 #define AUDIO_BLOCK_SIZE 4096
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 int fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 int sample_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 int channels;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 int frame_size; /* in bytes ! */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 int codec_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 int flip_left : 1;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 56
diff changeset
46 uint8_t buffer[AUDIO_BLOCK_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 int buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 } AudioData;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
50 static int audio_open(AudioData *s, int is_output, const char *audio_device)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 int audio_fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 int tmp, err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 char *flip = getenv("AUDIO_FLIP_LEFT");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 /* open linux audio device */
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
57 if (!audio_device)
754
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
58 #ifdef __OpenBSD__
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
59 audio_device = "/dev/sound";
754
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
60 #else
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
61 audio_device = "/dev/dsp";
754
aa52767bb802 OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents: 482
diff changeset
62 #endif
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
63
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 if (is_output)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 audio_fd = open(audio_device, O_WRONLY);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 audio_fd = open(audio_device, O_RDONLY);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 if (audio_fd < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 perror(audio_device);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
70 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 if (flip && *flip == '1') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 s->flip_left = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 /* non blocking mode */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 if (!is_output)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 s->frame_size = AUDIO_BLOCK_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 if (err < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 perror("SNDCTL_DSP_SETFRAGMENT");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 /* select format : favour native format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
92
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 #ifdef WORDS_BIGENDIAN
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 if (tmp & AFMT_S16_BE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 tmp = AFMT_S16_BE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 } else if (tmp & AFMT_S16_LE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 tmp = AFMT_S16_LE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 tmp = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 if (tmp & AFMT_S16_LE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 tmp = AFMT_S16_LE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 } else if (tmp & AFMT_S16_BE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 tmp = AFMT_S16_BE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 tmp = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 switch(tmp) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 case AFMT_S16_LE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 s->codec_id = CODEC_ID_PCM_S16LE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 case AFMT_S16_BE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 s->codec_id = CODEC_ID_PCM_S16BE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 default:
370
845f9de2c883 av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 241
diff changeset
119 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 close(audio_fd);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
121 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 if (err < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 perror("SNDCTL_DSP_SETFMT");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
128
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 tmp = (s->channels == 2);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 if (err < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132 perror("SNDCTL_DSP_STEREO");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 if (tmp)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 s->channels = 2;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
137
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 tmp = s->sample_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 if (err < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 perror("SNDCTL_DSP_SPEED");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 s->sample_rate = tmp; /* store real sample rate */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 s->fd = audio_fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 close(audio_fd);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
150 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 static int audio_close(AudioData *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 close(s->fd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 /* sound output support */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 static int audio_write_header(AVFormatContext *s1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 int ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 st = s1->streams[0];
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
167 s->sample_rate = st->codec->sample_rate;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
168 s->channels = st->codec->channels;
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
169 ret = audio_open(s, 1, NULL);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 if (ret < 0) {
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
171 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
177 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 int len, ret;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
181 int size= pkt->size;
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
182 uint8_t *buf= pkt->data;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 memcpy(s->buffer + s->buffer_ptr, buf, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 s->buffer_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 if (ret > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
196 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 s->buffer_ptr = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 static int audio_write_trailer(AVFormatContext *s1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 audio_close(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 /* grab support */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 int ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221
1003
2d57ce58f576 simplify AVFormatParameters NULL checks
michael
parents: 921
diff changeset
222 if (ap->sample_rate <= 0 || ap->channels <= 0)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 st = av_new_stream(s1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 if (!st) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 return -ENOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 s->sample_rate = ap->sample_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 s->channels = ap->channels;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231
30
90fd30dd68b3 grab device is in AVFormatParameter (at least better than global variable)
bellard
parents: 0
diff changeset
232 ret = audio_open(s, 0, ap->device);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 if (ret < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 av_free(st);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
235 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 /* take real parameters */
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
239 st->codec->codec_type = CODEC_TYPE_AUDIO;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
240 st->codec->codec_id = s->codec_id;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
241 st->codec->sample_rate = s->sample_rate;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 754
diff changeset
242 st->codec->channels = s->channels;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243
921
547675092e98 video4linux2 input support
diego
parents: 896
diff changeset
244 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 int ret, bdelay;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 int64_t cur_time;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 struct audio_buf_info abufi;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
254
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 if (av_new_packet(pkt, s->frame_size) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
256 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 for(;;) {
56
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
258 struct timeval tv;
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
259 fd_set fds;
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
260
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
261 tv.tv_sec = 0;
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
262 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
263
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
264 FD_ZERO(&fds);
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
265 FD_SET(s->fd, &fds);
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
266
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
267 /* This will block until data is available or we get a timeout */
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
268 (void) select(s->fd + 1, &fds, 0, 0, &tv);
01d48dc59dab Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents: 30
diff changeset
269
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 ret = read(s->fd, pkt->data, pkt->size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 if (ret > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 av_free_packet(pkt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 pkt->size = 0;
921
547675092e98 video4linux2 input support
diego
parents: 896
diff changeset
276 pkt->pts = av_gettime();
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 av_free_packet(pkt);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 481
diff changeset
281 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 pkt->size = ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 /* compute pts of the start of the packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 cur_time = av_gettime();
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 bdelay = ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 bdelay += abufi.bytes;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 /* substract time represented by the number of bytes in the audio fifo */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 /* convert to wanted units */
921
547675092e98 video4linux2 input support
diego
parents: 896
diff changeset
296 pkt->pts = cur_time;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 if (s->flip_left && s->channels == 2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 short *p = (short *) pkt->data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 for (i = 0; i < ret; i += 4) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 *p = ~*p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 p += 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 static int audio_read_close(AVFormatContext *s1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 AudioData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 audio_close(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
318 #ifdef CONFIG_AUDIO_DEMUXER
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
319 AVInputFormat audio_demuxer = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 "audio_device",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 "audio grab and output",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 sizeof(AudioData),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324 audio_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 audio_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 audio_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 .flags = AVFMT_NOFILE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 };
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
329 #endif
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
331 #ifdef CONFIG_AUDIO_MUXER
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
332 AVOutputFormat audio_muxer = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 "audio_device",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 "audio grab and output",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 "",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 "",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 sizeof(AudioData),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338 /* XXX: we make the assumption that the soundcard accepts this format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 /* XXX: find better solution with "preinit" method, needed also in
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 other formats */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 #ifdef WORDS_BIGENDIAN
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342 CODEC_ID_PCM_S16BE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 CODEC_ID_PCM_S16LE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 CODEC_ID_NONE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 audio_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 audio_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 audio_write_trailer,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350 .flags = AVFMT_NOFILE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 };
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
352 #endif