avdevice: Add missing header for NULL_IF_CONFIG_SMALL
[libav.git] / libavdevice / sndio_dec.c
CommitLineData
15d59d2c
B
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
c201069f 25#include "libavutil/internal.h"
a1a15a99 26#include "libavutil/opt.h"
fdaacc59 27#include "libavutil/time.h"
15d59d2c 28
c201069f
DB
29#include "libavformat/avformat.h"
30#include "libavformat/internal.h"
31
15d59d2c
B
32#include "sndio_common.h"
33
6e9651d1 34static av_cold int audio_read_header(AVFormatContext *s1)
15d59d2c
B
35{
36 SndioData *s = s1->priv_data;
37 AVStream *st;
38 int ret;
39
3b3bbdd3 40 st = avformat_new_stream(s1, NULL);
15d59d2c
B
41 if (!st)
42 return AVERROR(ENOMEM);
43
15d59d2c
B
44 ret = ff_sndio_open(s1, 0, s1->filename);
45 if (ret < 0)
46 return ret;
47
48 /* take real parameters */
49 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
50 st->codec->codec_id = s->codec_id;
51 st->codec->sample_rate = s->sample_rate;
52 st->codec->channels = s->channels;
53
c3f9ebf7 54 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
15d59d2c
B
55
56 return 0;
57}
58
59static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
60{
61 SndioData *s = s1->priv_data;
62 int64_t bdelay, cur_time;
63 int ret;
64
65 if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
66 return ret;
67
68 ret = sio_read(s->hdl, pkt->data, pkt->size);
69 if (ret == 0 || sio_eof(s->hdl)) {
70 av_free_packet(pkt);
71 return AVERROR_EOF;
72 }
73
74 pkt->size = ret;
75 s->softpos += ret;
76
77 /* compute pts of the start of the packet */
78 cur_time = av_gettime();
79
80 bdelay = ret + s->hwpos - s->softpos;
81
82 /* convert to pts */
83 pkt->pts = cur_time - ((bdelay * 1000000) /
84 (s->bps * s->channels * s->sample_rate));
85
86 return 0;
87}
88
89static av_cold int audio_read_close(AVFormatContext *s1)
90{
91 SndioData *s = s1->priv_data;
92
93 ff_sndio_close(s);
94
95 return 0;
96}
97
a1a15a99 98static const AVOption options[] = {
e6153f17
MS
99 { "sample_rate", "", offsetof(SndioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
100 { "channels", "", offsetof(SndioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
a1a15a99
AK
101 { NULL },
102};
103
104static const AVClass sndio_demuxer_class = {
105 .class_name = "sndio indev",
106 .item_name = av_default_item_name,
107 .option = options,
108 .version = LIBAVUTIL_VERSION_INT,
109};
110
15d59d2c
B
111AVInputFormat ff_sndio_demuxer = {
112 .name = "sndio",
113 .long_name = NULL_IF_CONFIG_SMALL("sndio audio capture"),
114 .priv_data_size = sizeof(SndioData),
115 .read_header = audio_read_header,
116 .read_packet = audio_read_packet,
117 .read_close = audio_read_close,
118 .flags = AVFMT_NOFILE,
a1a15a99 119 .priv_class = &sndio_demuxer_class,
15d59d2c 120};