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