]> git.sesse.net Git - ffmpeg/blob - libavdevice/sndio_dec.c
lavf: deprecate AVFormatParameters.{channels,sample_rate}.
[ffmpeg] / libavdevice / sndio_dec.c
1 /*
2  * sndio play and grab interface
3  * Copyright (c) 2010 Jacob Meuser
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdint.h>
23 #include <sndio.h>
24
25 #include "libavformat/avformat.h"
26 #include "libavutil/opt.h"
27
28 #include "sndio_common.h"
29
30 static av_cold int audio_read_header(AVFormatContext *s1,
31                                      AVFormatParameters *ap)
32 {
33     SndioData *s = s1->priv_data;
34     AVStream *st;
35     int ret;
36
37 #if FF_API_FORMAT_PARAMETERS
38     if (ap->sample_rate > 0)
39         s->sample_rate = ap->sample_rate;
40     if (ap->channels > 0)
41         s->channels = ap->channels;
42 #endif
43
44     st = av_new_stream(s1, 0);
45     if (!st)
46         return AVERROR(ENOMEM);
47
48     ret = ff_sndio_open(s1, 0, s1->filename);
49     if (ret < 0)
50         return ret;
51
52     /* take real parameters */
53     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
54     st->codec->codec_id    = s->codec_id;
55     st->codec->sample_rate = s->sample_rate;
56     st->codec->channels    = s->channels;
57
58     av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
59
60     return 0;
61 }
62
63 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
64 {
65     SndioData *s = s1->priv_data;
66     int64_t bdelay, cur_time;
67     int ret;
68
69     if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
70         return ret;
71
72     ret = sio_read(s->hdl, pkt->data, pkt->size);
73     if (ret == 0 || sio_eof(s->hdl)) {
74         av_free_packet(pkt);
75         return AVERROR_EOF;
76     }
77
78     pkt->size   = ret;
79     s->softpos += ret;
80
81     /* compute pts of the start of the packet */
82     cur_time = av_gettime();
83
84     bdelay = ret + s->hwpos - s->softpos;
85
86     /* convert to pts */
87     pkt->pts = cur_time - ((bdelay * 1000000) /
88         (s->bps * s->channels * s->sample_rate));
89
90     return 0;
91 }
92
93 static av_cold int audio_read_close(AVFormatContext *s1)
94 {
95     SndioData *s = s1->priv_data;
96
97     ff_sndio_close(s);
98
99     return 0;
100 }
101
102 static const AVOption options[] = {
103     { "sample_rate", "", offsetof(SndioData, sample_rate), FF_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
104     { "channels",    "", offsetof(SndioData, channels),    FF_OPT_TYPE_INT, {.dbl = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
105     { NULL },
106 };
107
108 static const AVClass sndio_demuxer_class = {
109     .class_name     = "sndio indev",
110     .item_name      = av_default_item_name,
111     .option         = options,
112     .version        = LIBAVUTIL_VERSION_INT,
113 };
114
115 AVInputFormat ff_sndio_demuxer = {
116     .name           = "sndio",
117     .long_name      = NULL_IF_CONFIG_SMALL("sndio audio capture"),
118     .priv_data_size = sizeof(SndioData),
119     .read_header    = audio_read_header,
120     .read_packet    = audio_read_packet,
121     .read_close     = audio_read_close,
122     .flags          = AVFMT_NOFILE,
123     .priv_class     = &sndio_demuxer_class,
124 };