00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <stdint.h>
00026 #include <string.h>
00027 #include <errno.h>
00028 #if HAVE_SOUNDCARD_H
00029 #include <soundcard.h>
00030 #else
00031 #include <sys/soundcard.h>
00032 #endif
00033 #include <unistd.h>
00034 #include <fcntl.h>
00035 #include <sys/ioctl.h>
00036 #include <sys/time.h>
00037 #include <sys/select.h>
00038
00039 #include "libavutil/log.h"
00040 #include "libavcodec/avcodec.h"
00041 #include "libavformat/avformat.h"
00042
00043 #define AUDIO_BLOCK_SIZE 4096
00044
00045 typedef struct {
00046 int fd;
00047 int sample_rate;
00048 int channels;
00049 int frame_size;
00050 enum CodecID codec_id;
00051 unsigned int flip_left : 1;
00052 uint8_t buffer[AUDIO_BLOCK_SIZE];
00053 int buffer_ptr;
00054 } AudioData;
00055
00056 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
00057 {
00058 AudioData *s = s1->priv_data;
00059 int audio_fd;
00060 int tmp, err;
00061 char *flip = getenv("AUDIO_FLIP_LEFT");
00062
00063 if (is_output)
00064 audio_fd = open(audio_device, O_WRONLY);
00065 else
00066 audio_fd = open(audio_device, O_RDONLY);
00067 if (audio_fd < 0) {
00068 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
00069 return AVERROR(EIO);
00070 }
00071
00072 if (flip && *flip == '1') {
00073 s->flip_left = 1;
00074 }
00075
00076
00077 if (!is_output)
00078 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
00079
00080 s->frame_size = AUDIO_BLOCK_SIZE;
00081 #if 0
00082 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
00083 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
00084 if (err < 0) {
00085 perror("SNDCTL_DSP_SETFRAGMENT");
00086 }
00087 #endif
00088
00089
00090 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
00091
00092 #ifdef WORDS_BIGENDIAN
00093 if (tmp & AFMT_S16_BE) {
00094 tmp = AFMT_S16_BE;
00095 } else if (tmp & AFMT_S16_LE) {
00096 tmp = AFMT_S16_LE;
00097 } else {
00098 tmp = 0;
00099 }
00100 #else
00101 if (tmp & AFMT_S16_LE) {
00102 tmp = AFMT_S16_LE;
00103 } else if (tmp & AFMT_S16_BE) {
00104 tmp = AFMT_S16_BE;
00105 } else {
00106 tmp = 0;
00107 }
00108 #endif
00109
00110 switch(tmp) {
00111 case AFMT_S16_LE:
00112 s->codec_id = CODEC_ID_PCM_S16LE;
00113 break;
00114 case AFMT_S16_BE:
00115 s->codec_id = CODEC_ID_PCM_S16BE;
00116 break;
00117 default:
00118 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
00119 close(audio_fd);
00120 return AVERROR(EIO);
00121 }
00122 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
00123 if (err < 0) {
00124 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
00125 goto fail;
00126 }
00127
00128 tmp = (s->channels == 2);
00129 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
00130 if (err < 0) {
00131 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
00132 goto fail;
00133 }
00134
00135 tmp = s->sample_rate;
00136 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
00137 if (err < 0) {
00138 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
00139 goto fail;
00140 }
00141 s->sample_rate = tmp;
00142 s->fd = audio_fd;
00143
00144 return 0;
00145 fail:
00146 close(audio_fd);
00147 return AVERROR(EIO);
00148 }
00149
00150 static int audio_close(AudioData *s)
00151 {
00152 close(s->fd);
00153 return 0;
00154 }
00155
00156
00157 static int audio_write_header(AVFormatContext *s1)
00158 {
00159 AudioData *s = s1->priv_data;
00160 AVStream *st;
00161 int ret;
00162
00163 st = s1->streams[0];
00164 s->sample_rate = st->codec->sample_rate;
00165 s->channels = st->codec->channels;
00166 ret = audio_open(s1, 1, s1->filename);
00167 if (ret < 0) {
00168 return AVERROR(EIO);
00169 } else {
00170 return 0;
00171 }
00172 }
00173
00174 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
00175 {
00176 AudioData *s = s1->priv_data;
00177 int len, ret;
00178 int size= pkt->size;
00179 uint8_t *buf= pkt->data;
00180
00181 while (size > 0) {
00182 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
00183 if (len > size)
00184 len = size;
00185 memcpy(s->buffer + s->buffer_ptr, buf, len);
00186 s->buffer_ptr += len;
00187 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
00188 for(;;) {
00189 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
00190 if (ret > 0)
00191 break;
00192 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
00193 return AVERROR(EIO);
00194 }
00195 s->buffer_ptr = 0;
00196 }
00197 buf += len;
00198 size -= len;
00199 }
00200 return 0;
00201 }
00202
00203 static int audio_write_trailer(AVFormatContext *s1)
00204 {
00205 AudioData *s = s1->priv_data;
00206
00207 audio_close(s);
00208 return 0;
00209 }
00210
00211
00212
00213 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00214 {
00215 AudioData *s = s1->priv_data;
00216 AVStream *st;
00217 int ret;
00218
00219 if (ap->sample_rate <= 0 || ap->channels <= 0)
00220 return -1;
00221
00222 st = av_new_stream(s1, 0);
00223 if (!st) {
00224 return AVERROR(ENOMEM);
00225 }
00226 s->sample_rate = ap->sample_rate;
00227 s->channels = ap->channels;
00228
00229 ret = audio_open(s1, 0, s1->filename);
00230 if (ret < 0) {
00231 av_free(st);
00232 return AVERROR(EIO);
00233 }
00234
00235
00236 st->codec->codec_type = CODEC_TYPE_AUDIO;
00237 st->codec->codec_id = s->codec_id;
00238 st->codec->sample_rate = s->sample_rate;
00239 st->codec->channels = s->channels;
00240
00241 av_set_pts_info(st, 64, 1, 1000000);
00242 return 0;
00243 }
00244
00245 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
00246 {
00247 AudioData *s = s1->priv_data;
00248 int ret, bdelay;
00249 int64_t cur_time;
00250 struct audio_buf_info abufi;
00251
00252 if (av_new_packet(pkt, s->frame_size) < 0)
00253 return AVERROR(EIO);
00254 for(;;) {
00255 struct timeval tv;
00256 fd_set fds;
00257
00258 tv.tv_sec = 0;
00259 tv.tv_usec = 30 * 1000;
00260
00261 FD_ZERO(&fds);
00262 FD_SET(s->fd, &fds);
00263
00264
00265 (void) select(s->fd + 1, &fds, 0, 0, &tv);
00266
00267 ret = read(s->fd, pkt->data, pkt->size);
00268 if (ret > 0)
00269 break;
00270 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
00271 av_free_packet(pkt);
00272 pkt->size = 0;
00273 pkt->pts = av_gettime();
00274 return 0;
00275 }
00276 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
00277 av_free_packet(pkt);
00278 return AVERROR(EIO);
00279 }
00280 }
00281 pkt->size = ret;
00282
00283
00284 cur_time = av_gettime();
00285 bdelay = ret;
00286 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
00287 bdelay += abufi.bytes;
00288 }
00289
00290 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
00291
00292
00293 pkt->pts = cur_time;
00294
00295 if (s->flip_left && s->channels == 2) {
00296 int i;
00297 short *p = (short *) pkt->data;
00298
00299 for (i = 0; i < ret; i += 4) {
00300 *p = ~*p;
00301 p += 2;
00302 }
00303 }
00304 return 0;
00305 }
00306
00307 static int audio_read_close(AVFormatContext *s1)
00308 {
00309 AudioData *s = s1->priv_data;
00310
00311 audio_close(s);
00312 return 0;
00313 }
00314
00315 #if CONFIG_OSS_DEMUXER
00316 AVInputFormat oss_demuxer = {
00317 "oss",
00318 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
00319 sizeof(AudioData),
00320 NULL,
00321 audio_read_header,
00322 audio_read_packet,
00323 audio_read_close,
00324 .flags = AVFMT_NOFILE,
00325 };
00326 #endif
00327
00328 #if CONFIG_OSS_MUXER
00329 AVOutputFormat oss_muxer = {
00330 "oss",
00331 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
00332 "",
00333 "",
00334 sizeof(AudioData),
00335
00336
00337
00338 #ifdef WORDS_BIGENDIAN
00339 CODEC_ID_PCM_S16BE,
00340 #else
00341 CODEC_ID_PCM_S16LE,
00342 #endif
00343 CODEC_ID_NONE,
00344 audio_write_header,
00345 audio_write_packet,
00346 audio_write_trailer,
00347 .flags = AVFMT_NOFILE,
00348 };
00349 #endif