FFmpeg
Loading...
Searching...
No Matches
sndio_dec.c
Go to the documentation of this file.
1/*
2 * sndio play and grab interface
3 * Copyright (c) 2010 Jacob Meuser
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "libavutil/internal.h"
26#include "libavutil/opt.h"
27#include "libavutil/time.h"
28
30#include "libavformat/demux.h"
32
33#include "libavdevice/sndio.h"
34
36{
37 SndioData *s = s1->priv_data;
38 AVStream *st;
39 int ret;
40
41 st = avformat_new_stream(s1, NULL);
42 if (!st)
43 return AVERROR(ENOMEM);
44
45 ret = ff_sndio_open(s1, 0, s1->url);
46 if (ret < 0)
47 return ret;
48
49 /* take real parameters */
51 st->codecpar->codec_id = s->codec_id;
52 st->codecpar->sample_rate = s->sample_rate;
53 st->codecpar->ch_layout.nb_channels = s->channels;
54
55 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
56
57 return 0;
58}
59
61{
62 SndioData *s = s1->priv_data;
63 int64_t bdelay, cur_time;
64 int ret;
65
66 if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
67 return ret;
68
69 ret = sio_read(s->hdl, pkt->data, pkt->size);
70 if (ret == 0 || sio_eof(s->hdl)) {
72 return AVERROR_EOF;
73 }
74
75 pkt->size = ret;
76 s->softpos += ret;
77
78 /* compute pts of the start of the packet */
79 cur_time = av_gettime();
80
81 bdelay = ret + s->hwpos - s->softpos;
82
83 /* convert to pts */
84 pkt->pts = cur_time - ((bdelay * 1000000) /
85 (s->bps * s->channels * s->sample_rate));
86
87 return 0;
88}
89
91{
92 SndioData *s = s1->priv_data;
93
95
96 return 0;
97}
98
99static const AVOption options[] = {
100 { "sample_rate", "", offsetof(SndioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
101 { "channels", "", offsetof(SndioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
102 { NULL },
103};
104
106 .class_name = "sndio indev",
107 .item_name = av_default_item_name,
108 .option = options,
109 .version = LIBAVUTIL_VERSION_INT,
111};
112
114 .p.name = "sndio",
115 .p.long_name = NULL_IF_CONFIG_SMALL("sndio audio capture"),
116 .p.flags = AVFMT_NOFILE,
117 .p.priv_class = &sndio_demuxer_class,
118 .priv_data_size = sizeof(SndioData),
122};
const FFInputFormat ff_sndio_demuxer
Definition sndio_dec.c:113
static av_cold int audio_read_header(AVFormatContext *s1)
Definition alsa_dec.c:61
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition alsa_dec.c:106
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static int audio_read_close(AVFormatContext *context)
Definition jack.c:323
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition log.h:44
AVOptions.
av_cold int ff_sndio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition sndio.c:36
int ff_sndio_close(SndioData *s)
Definition sndio.c:112
static const AVClass sndio_demuxer_class
Definition sndio_dec.c:105
static av_cold int audio_read_header(AVFormatContext *s1)
Definition sndio_dec.c:35
static av_cold int audio_read_close(AVFormatContext *s1)
Definition sndio_dec.c:90
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition sndio_dec.c:60
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
char * url
input or output URL.
Definition avformat.h:1449
void * priv_data
Format private data.
Definition avformat.h:1361
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40