• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Linux audio play interface
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 
24 #include <stdint.h>
25 
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/soundcard.h>
32 
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/time.h"
36 
37 #include "avdevice.h"
38 #include "libavformat/internal.h"
39 
40 #include "oss.h"
41 
audio_read_header(AVFormatContext * s1)42 static int audio_read_header(AVFormatContext *s1)
43 {
44     OSSAudioData *s = s1->priv_data;
45     AVStream *st;
46     int ret;
47 
48     st = avformat_new_stream(s1, NULL);
49     if (!st) {
50         return AVERROR(ENOMEM);
51     }
52 
53     ret = ff_oss_audio_open(s1, 0, s1->url);
54     if (ret < 0) {
55         return AVERROR(EIO);
56     }
57 
58     /* take real parameters */
59     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
60     st->codecpar->codec_id = s->codec_id;
61     st->codecpar->sample_rate = s->sample_rate;
62     st->codecpar->ch_layout.nb_channels = s->channels;
63 
64     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
65     return 0;
66 }
67 
audio_read_packet(AVFormatContext * s1,AVPacket * pkt)68 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
69 {
70     OSSAudioData *s = s1->priv_data;
71     int ret, bdelay;
72     int64_t cur_time;
73     struct audio_buf_info abufi;
74 
75     if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
76         return ret;
77 
78     ret = read(s->fd, pkt->data, pkt->size);
79     if (ret <= 0){
80         av_packet_unref(pkt);
81         pkt->size = 0;
82         if (ret<0)  return AVERROR(errno);
83         else        return AVERROR_EOF;
84     }
85     pkt->size = ret;
86 
87     /* compute pts of the start of the packet */
88     cur_time = av_gettime();
89     bdelay = ret;
90     if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
91         bdelay += abufi.bytes;
92     }
93     /* subtract time represented by the number of bytes in the audio fifo */
94     cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->sample_size * s->channels);
95 
96     /* convert to wanted units */
97     pkt->pts = cur_time;
98 
99     if (s->flip_left && s->channels == 2) {
100         int i;
101         short *p = (short *) pkt->data;
102 
103         for (i = 0; i < ret; i += 4) {
104             *p = ~*p;
105             p += 2;
106         }
107     }
108     return 0;
109 }
110 
audio_read_close(AVFormatContext * s1)111 static int audio_read_close(AVFormatContext *s1)
112 {
113     OSSAudioData *s = s1->priv_data;
114 
115     ff_oss_audio_close(s);
116     return 0;
117 }
118 
119 static const AVOption options[] = {
120     { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
121     { "channels",    "", offsetof(OSSAudioData, channels),    AV_OPT_TYPE_INT, {.i64 = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
122     { NULL },
123 };
124 
125 static const AVClass oss_demuxer_class = {
126     .class_name     = "OSS indev",
127     .item_name      = av_default_item_name,
128     .option         = options,
129     .version        = LIBAVUTIL_VERSION_INT,
130     .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
131 };
132 
133 const AVInputFormat ff_oss_demuxer = {
134     .name           = "oss",
135     .long_name      = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
136     .priv_data_size = sizeof(OSSAudioData),
137     .read_header    = audio_read_header,
138     .read_packet    = audio_read_packet,
139     .read_close     = audio_read_close,
140     .flags          = AVFMT_NOFILE,
141     .priv_class     = &oss_demuxer_class,
142 };
143