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
00037 #include "libavutil/log.h"
00038 #include "libavutil/opt.h"
00039 #include "libavutil/time.h"
00040 #include "libavcodec/avcodec.h"
00041 #include "avdevice.h"
00042 #include "libavformat/internal.h"
00043
00044 #define AUDIO_BLOCK_SIZE 4096
00045
00046 typedef struct {
00047 AVClass *class;
00048 int fd;
00049 int sample_rate;
00050 int channels;
00051 int frame_size;
00052 enum AVCodecID codec_id;
00053 unsigned int flip_left : 1;
00054 uint8_t buffer[AUDIO_BLOCK_SIZE];
00055 int buffer_ptr;
00056 } AudioData;
00057
00058 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
00059 {
00060 AudioData *s = s1->priv_data;
00061 int audio_fd;
00062 int tmp, err;
00063 char *flip = getenv("AUDIO_FLIP_LEFT");
00064
00065 if (is_output)
00066 audio_fd = open(audio_device, O_WRONLY);
00067 else
00068 audio_fd = open(audio_device, O_RDONLY);
00069 if (audio_fd < 0) {
00070 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
00071 return AVERROR(EIO);
00072 }
00073
00074 if (flip && *flip == '1') {
00075 s->flip_left = 1;
00076 }
00077
00078
00079 if (!is_output) {
00080 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
00081 av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
00082 }
00083 }
00084
00085 s->frame_size = AUDIO_BLOCK_SIZE;
00086
00087
00088 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
00089
00090 #if HAVE_BIGENDIAN
00091 if (tmp & AFMT_S16_BE) {
00092 tmp = AFMT_S16_BE;
00093 } else if (tmp & AFMT_S16_LE) {
00094 tmp = AFMT_S16_LE;
00095 } else {
00096 tmp = 0;
00097 }
00098 #else
00099 if (tmp & AFMT_S16_LE) {
00100 tmp = AFMT_S16_LE;
00101 } else if (tmp & AFMT_S16_BE) {
00102 tmp = AFMT_S16_BE;
00103 } else {
00104 tmp = 0;
00105 }
00106 #endif
00107
00108 switch(tmp) {
00109 case AFMT_S16_LE:
00110 s->codec_id = AV_CODEC_ID_PCM_S16LE;
00111 break;
00112 case AFMT_S16_BE:
00113 s->codec_id = AV_CODEC_ID_PCM_S16BE;
00114 break;
00115 default:
00116 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
00117 close(audio_fd);
00118 return AVERROR(EIO);
00119 }
00120 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
00121 if (err < 0) {
00122 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
00123 goto fail;
00124 }
00125
00126 tmp = (s->channels == 2);
00127 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
00128 if (err < 0) {
00129 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
00130 goto fail;
00131 }
00132
00133 tmp = s->sample_rate;
00134 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
00135 if (err < 0) {
00136 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
00137 goto fail;
00138 }
00139 s->sample_rate = tmp;
00140 s->fd = audio_fd;
00141
00142 return 0;
00143 fail:
00144 close(audio_fd);
00145 return AVERROR(EIO);
00146 }
00147
00148 static int audio_close(AudioData *s)
00149 {
00150 close(s->fd);
00151 return 0;
00152 }
00153
00154
00155 static int audio_write_header(AVFormatContext *s1)
00156 {
00157 AudioData *s = s1->priv_data;
00158 AVStream *st;
00159 int ret;
00160
00161 st = s1->streams[0];
00162 s->sample_rate = st->codec->sample_rate;
00163 s->channels = st->codec->channels;
00164 ret = audio_open(s1, 1, s1->filename);
00165 if (ret < 0) {
00166 return AVERROR(EIO);
00167 } else {
00168 return 0;
00169 }
00170 }
00171
00172 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
00173 {
00174 AudioData *s = s1->priv_data;
00175 int len, ret;
00176 int size= pkt->size;
00177 uint8_t *buf= pkt->data;
00178
00179 while (size > 0) {
00180 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
00181 memcpy(s->buffer + s->buffer_ptr, buf, len);
00182 s->buffer_ptr += len;
00183 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
00184 for(;;) {
00185 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
00186 if (ret > 0)
00187 break;
00188 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
00189 return AVERROR(EIO);
00190 }
00191 s->buffer_ptr = 0;
00192 }
00193 buf += len;
00194 size -= len;
00195 }
00196 return 0;
00197 }
00198
00199 static int audio_write_trailer(AVFormatContext *s1)
00200 {
00201 AudioData *s = s1->priv_data;
00202
00203 audio_close(s);
00204 return 0;
00205 }
00206
00207
00208
00209 static int audio_read_header(AVFormatContext *s1)
00210 {
00211 AudioData *s = s1->priv_data;
00212 AVStream *st;
00213 int ret;
00214
00215 st = avformat_new_stream(s1, NULL);
00216 if (!st) {
00217 return AVERROR(ENOMEM);
00218 }
00219
00220 ret = audio_open(s1, 0, s1->filename);
00221 if (ret < 0) {
00222 return AVERROR(EIO);
00223 }
00224
00225
00226 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00227 st->codec->codec_id = s->codec_id;
00228 st->codec->sample_rate = s->sample_rate;
00229 st->codec->channels = s->channels;
00230
00231 avpriv_set_pts_info(st, 64, 1, 1000000);
00232 return 0;
00233 }
00234
00235 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
00236 {
00237 AudioData *s = s1->priv_data;
00238 int ret, bdelay;
00239 int64_t cur_time;
00240 struct audio_buf_info abufi;
00241
00242 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
00243 return ret;
00244
00245 ret = read(s->fd, pkt->data, pkt->size);
00246 if (ret <= 0){
00247 av_free_packet(pkt);
00248 pkt->size = 0;
00249 if (ret<0) return AVERROR(errno);
00250 else return AVERROR_EOF;
00251 }
00252 pkt->size = ret;
00253
00254
00255 cur_time = av_gettime();
00256 bdelay = ret;
00257 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
00258 bdelay += abufi.bytes;
00259 }
00260
00261 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
00262
00263
00264 pkt->pts = cur_time;
00265
00266 if (s->flip_left && s->channels == 2) {
00267 int i;
00268 short *p = (short *) pkt->data;
00269
00270 for (i = 0; i < ret; i += 4) {
00271 *p = ~*p;
00272 p += 2;
00273 }
00274 }
00275 return 0;
00276 }
00277
00278 static int audio_read_close(AVFormatContext *s1)
00279 {
00280 AudioData *s = s1->priv_data;
00281
00282 audio_close(s);
00283 return 0;
00284 }
00285
00286 #if CONFIG_OSS_INDEV
00287 static const AVOption options[] = {
00288 { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00289 { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00290 { NULL },
00291 };
00292
00293 static const AVClass oss_demuxer_class = {
00294 .class_name = "OSS demuxer",
00295 .item_name = av_default_item_name,
00296 .option = options,
00297 .version = LIBAVUTIL_VERSION_INT,
00298 };
00299
00300 AVInputFormat ff_oss_demuxer = {
00301 .name = "oss",
00302 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
00303 .priv_data_size = sizeof(AudioData),
00304 .read_header = audio_read_header,
00305 .read_packet = audio_read_packet,
00306 .read_close = audio_read_close,
00307 .flags = AVFMT_NOFILE,
00308 .priv_class = &oss_demuxer_class,
00309 };
00310 #endif
00311
00312 #if CONFIG_OSS_OUTDEV
00313 AVOutputFormat ff_oss_muxer = {
00314 .name = "oss",
00315 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
00316 .priv_data_size = sizeof(AudioData),
00317
00318
00319
00320 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
00321 .video_codec = AV_CODEC_ID_NONE,
00322 .write_header = audio_write_header,
00323 .write_packet = audio_write_packet,
00324 .write_trailer = audio_write_trailer,
00325 .flags = AVFMT_NOFILE,
00326 };
00327 #endif