libavdevice/oss_audio.c
Go to the documentation of this file.
00001 /*
00002  * Linux audio play and grab interface
00003  * Copyright (c) 2000, 2001 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 "libavutil/opt.h"
00041 #include "libavcodec/avcodec.h"
00042 #include "avdevice.h"
00043 #include "libavformat/internal.h"
00044 
00045 #define AUDIO_BLOCK_SIZE 4096
00046 
00047 typedef struct {
00048     AVClass *class;
00049     int fd;
00050     int sample_rate;
00051     int channels;
00052     int frame_size; /* in bytes ! */
00053     enum CodecID codec_id;
00054     unsigned int flip_left : 1;
00055     uint8_t buffer[AUDIO_BLOCK_SIZE];
00056     int buffer_ptr;
00057 } AudioData;
00058 
00059 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
00060 {
00061     AudioData *s = s1->priv_data;
00062     int audio_fd;
00063     int tmp, err;
00064     char *flip = getenv("AUDIO_FLIP_LEFT");
00065 
00066     if (is_output)
00067         audio_fd = open(audio_device, O_WRONLY);
00068     else
00069         audio_fd = open(audio_device, O_RDONLY);
00070     if (audio_fd < 0) {
00071         av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
00072         return AVERROR(EIO);
00073     }
00074 
00075     if (flip && *flip == '1') {
00076         s->flip_left = 1;
00077     }
00078 
00079     /* non blocking mode */
00080     if (!is_output)
00081         fcntl(audio_fd, F_SETFL, O_NONBLOCK);
00082 
00083     s->frame_size = AUDIO_BLOCK_SIZE;
00084 
00085     /* select format : favour native format */
00086     err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
00087 
00088 #if HAVE_BIGENDIAN
00089     if (tmp & AFMT_S16_BE) {
00090         tmp = AFMT_S16_BE;
00091     } else if (tmp & AFMT_S16_LE) {
00092         tmp = AFMT_S16_LE;
00093     } else {
00094         tmp = 0;
00095     }
00096 #else
00097     if (tmp & AFMT_S16_LE) {
00098         tmp = AFMT_S16_LE;
00099     } else if (tmp & AFMT_S16_BE) {
00100         tmp = AFMT_S16_BE;
00101     } else {
00102         tmp = 0;
00103     }
00104 #endif
00105 
00106     switch(tmp) {
00107     case AFMT_S16_LE:
00108         s->codec_id = CODEC_ID_PCM_S16LE;
00109         break;
00110     case AFMT_S16_BE:
00111         s->codec_id = CODEC_ID_PCM_S16BE;
00112         break;
00113     default:
00114         av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
00115         close(audio_fd);
00116         return AVERROR(EIO);
00117     }
00118     err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
00119     if (err < 0) {
00120         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
00121         goto fail;
00122     }
00123 
00124     tmp = (s->channels == 2);
00125     err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
00126     if (err < 0) {
00127         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
00128         goto fail;
00129     }
00130 
00131     tmp = s->sample_rate;
00132     err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
00133     if (err < 0) {
00134         av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
00135         goto fail;
00136     }
00137     s->sample_rate = tmp; /* store real sample rate */
00138     s->fd = audio_fd;
00139 
00140     return 0;
00141  fail:
00142     close(audio_fd);
00143     return AVERROR(EIO);
00144 }
00145 
00146 static int audio_close(AudioData *s)
00147 {
00148     close(s->fd);
00149     return 0;
00150 }
00151 
00152 /* sound output support */
00153 static int audio_write_header(AVFormatContext *s1)
00154 {
00155     AudioData *s = s1->priv_data;
00156     AVStream *st;
00157     int ret;
00158 
00159     st = s1->streams[0];
00160     s->sample_rate = st->codec->sample_rate;
00161     s->channels = st->codec->channels;
00162     ret = audio_open(s1, 1, s1->filename);
00163     if (ret < 0) {
00164         return AVERROR(EIO);
00165     } else {
00166         return 0;
00167     }
00168 }
00169 
00170 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
00171 {
00172     AudioData *s = s1->priv_data;
00173     int len, ret;
00174     int size= pkt->size;
00175     uint8_t *buf= pkt->data;
00176 
00177     while (size > 0) {
00178         len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
00179         memcpy(s->buffer + s->buffer_ptr, buf, len);
00180         s->buffer_ptr += len;
00181         if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
00182             for(;;) {
00183                 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
00184                 if (ret > 0)
00185                     break;
00186                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
00187                     return AVERROR(EIO);
00188             }
00189             s->buffer_ptr = 0;
00190         }
00191         buf += len;
00192         size -= len;
00193     }
00194     return 0;
00195 }
00196 
00197 static int audio_write_trailer(AVFormatContext *s1)
00198 {
00199     AudioData *s = s1->priv_data;
00200 
00201     audio_close(s);
00202     return 0;
00203 }
00204 
00205 /* grab support */
00206 
00207 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00208 {
00209     AudioData *s = s1->priv_data;
00210     AVStream *st;
00211     int ret;
00212 
00213     st = avformat_new_stream(s1, NULL);
00214     if (!st) {
00215         return AVERROR(ENOMEM);
00216     }
00217 
00218     ret = audio_open(s1, 0, s1->filename);
00219     if (ret < 0) {
00220         return AVERROR(EIO);
00221     }
00222 
00223     /* take real parameters */
00224     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00225     st->codec->codec_id = s->codec_id;
00226     st->codec->sample_rate = s->sample_rate;
00227     st->codec->channels = s->channels;
00228 
00229     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
00230     return 0;
00231 }
00232 
00233 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
00234 {
00235     AudioData *s = s1->priv_data;
00236     int ret, bdelay;
00237     int64_t cur_time;
00238     struct audio_buf_info abufi;
00239 
00240     if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
00241         return ret;
00242 
00243     ret = read(s->fd, pkt->data, pkt->size);
00244     if (ret <= 0){
00245         av_free_packet(pkt);
00246         pkt->size = 0;
00247         if (ret<0)  return AVERROR(errno);
00248         else        return AVERROR_EOF;
00249     }
00250     pkt->size = ret;
00251 
00252     /* compute pts of the start of the packet */
00253     cur_time = av_gettime();
00254     bdelay = ret;
00255     if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
00256         bdelay += abufi.bytes;
00257     }
00258     /* subtract time represented by the number of bytes in the audio fifo */
00259     cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
00260 
00261     /* convert to wanted units */
00262     pkt->pts = cur_time;
00263 
00264     if (s->flip_left && s->channels == 2) {
00265         int i;
00266         short *p = (short *) pkt->data;
00267 
00268         for (i = 0; i < ret; i += 4) {
00269             *p = ~*p;
00270             p += 2;
00271         }
00272     }
00273     return 0;
00274 }
00275 
00276 static int audio_read_close(AVFormatContext *s1)
00277 {
00278     AudioData *s = s1->priv_data;
00279 
00280     audio_close(s);
00281     return 0;
00282 }
00283 
00284 #if CONFIG_OSS_INDEV
00285 static const AVOption options[] = {
00286     { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00287     { "channels",    "", offsetof(AudioData, channels),    AV_OPT_TYPE_INT, {.dbl = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00288     { NULL },
00289 };
00290 
00291 static const AVClass oss_demuxer_class = {
00292     .class_name     = "OSS demuxer",
00293     .item_name      = av_default_item_name,
00294     .option         = options,
00295     .version        = LIBAVUTIL_VERSION_INT,
00296 };
00297 
00298 AVInputFormat ff_oss_demuxer = {
00299     .name           = "oss",
00300     .long_name      = NULL_IF_CONFIG_SMALL("Open Sound System capture"),
00301     .priv_data_size = sizeof(AudioData),
00302     .read_header    = audio_read_header,
00303     .read_packet    = audio_read_packet,
00304     .read_close     = audio_read_close,
00305     .flags          = AVFMT_NOFILE,
00306     .priv_class     = &oss_demuxer_class,
00307 };
00308 #endif
00309 
00310 #if CONFIG_OSS_OUTDEV
00311 AVOutputFormat ff_oss_muxer = {
00312     .name           = "oss",
00313     .long_name      = NULL_IF_CONFIG_SMALL("Open Sound System playback"),
00314     .priv_data_size = sizeof(AudioData),
00315     /* XXX: we make the assumption that the soundcard accepts this format */
00316     /* XXX: find better solution with "preinit" method, needed also in
00317        other formats */
00318     .audio_codec    = AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
00319     .video_codec    = CODEC_ID_NONE,
00320     .write_header   = audio_write_header,
00321     .write_packet   = audio_write_packet,
00322     .write_trailer  = audio_write_trailer,
00323     .flags          = AVFMT_NOFILE,
00324 };
00325 #endif