]> git.sesse.net Git - ffmpeg/blob - libavdevice/sndio_dec.c
sndio support for playback and record
[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
27 #include "sndio_common.h"
28
29 static av_cold int audio_read_header(AVFormatContext *s1,
30                                      AVFormatParameters *ap)
31 {
32     SndioData *s = s1->priv_data;
33     AVStream *st;
34     int ret;
35
36     if (ap->sample_rate <= 0 || ap->channels <= 0)
37         return AVERROR(EINVAL);
38
39     st = av_new_stream(s1, 0);
40     if (!st)
41         return AVERROR(ENOMEM);
42
43     s->sample_rate = ap->sample_rate;
44     s->channels    = ap->channels;
45
46     ret = ff_sndio_open(s1, 0, s1->filename);
47     if (ret < 0)
48         return ret;
49
50     /* take real parameters */
51     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
52     st->codec->codec_id    = s->codec_id;
53     st->codec->sample_rate = s->sample_rate;
54     st->codec->channels    = s->channels;
55
56     av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
57
58     return 0;
59 }
60
61 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
62 {
63     SndioData *s = s1->priv_data;
64     int64_t bdelay, cur_time;
65     int ret;
66
67     if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
68         return ret;
69
70     ret = sio_read(s->hdl, pkt->data, pkt->size);
71     if (ret == 0 || sio_eof(s->hdl)) {
72         av_free_packet(pkt);
73         return AVERROR_EOF;
74     }
75
76     pkt->size   = ret;
77     s->softpos += ret;
78
79     /* compute pts of the start of the packet */
80     cur_time = av_gettime();
81
82     bdelay = ret + s->hwpos - s->softpos;
83
84     /* convert to pts */
85     pkt->pts = cur_time - ((bdelay * 1000000) /
86         (s->bps * s->channels * s->sample_rate));
87
88     return 0;
89 }
90
91 static av_cold int audio_read_close(AVFormatContext *s1)
92 {
93     SndioData *s = s1->priv_data;
94
95     ff_sndio_close(s);
96
97     return 0;
98 }
99
100 AVInputFormat ff_sndio_demuxer = {
101     .name           = "sndio",
102     .long_name      = NULL_IF_CONFIG_SMALL("sndio audio capture"),
103     .priv_data_size = sizeof(SndioData),
104     .read_header    = audio_read_header,
105     .read_packet    = audio_read_packet,
106     .read_close     = audio_read_close,
107     .flags          = AVFMT_NOFILE,
108 };