2 * sndio play and grab interface
3 * Copyright (c) 2010 Jacob Meuser
5 * This file is part of Libav.
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.
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.
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
25 #include "libavformat/avformat.h"
27 #include "sndio_common.h"
29 static av_cold int audio_read_header(AVFormatContext *s1,
30 AVFormatParameters *ap)
32 SndioData *s = s1->priv_data;
36 if (ap->sample_rate <= 0 || ap->channels <= 0)
37 return AVERROR(EINVAL);
39 st = av_new_stream(s1, 0);
41 return AVERROR(ENOMEM);
43 s->sample_rate = ap->sample_rate;
44 s->channels = ap->channels;
46 ret = ff_sndio_open(s1, 0, s1->filename);
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;
56 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
61 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
63 SndioData *s = s1->priv_data;
64 int64_t bdelay, cur_time;
67 if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
70 ret = sio_read(s->hdl, pkt->data, pkt->size);
71 if (ret == 0 || sio_eof(s->hdl)) {
79 /* compute pts of the start of the packet */
80 cur_time = av_gettime();
82 bdelay = ret + s->hwpos - s->softpos;
85 pkt->pts = cur_time - ((bdelay * 1000000) /
86 (s->bps * s->channels * s->sample_rate));
91 static av_cold int audio_read_close(AVFormatContext *s1)
93 SndioData *s = s1->priv_data;
100 AVInputFormat ff_sndio_demuxer = {
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,