24#include <pulse/rtclock.h>
25#include <pulse/error.h>
38#define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
61#define CHECK_SUCCESS_GOTO(rerror, expression, label) \
63 if (!(expression)) { \
64 rerror = AVERROR_EXTERNAL; \
69#define CHECK_DEAD_GOTO(p, rerror, label) \
71 if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
72 !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
73 rerror = AVERROR_EXTERNAL; \
81 switch (pa_context_get_state(
c)) {
82 case PA_CONTEXT_READY:
83 case PA_CONTEXT_TERMINATED:
84 case PA_CONTEXT_FAILED:
85 pa_threaded_mainloop_signal(p->mainloop, 0);
93 switch (pa_stream_get_state(
s)) {
95 case PA_STREAM_FAILED:
96 case PA_STREAM_TERMINATED:
97 pa_threaded_mainloop_signal(p->mainloop, 0);
105 pa_threaded_mainloop_signal(p->mainloop, 0);
111 pa_threaded_mainloop_signal(p->mainloop, 0);
119 pa_threaded_mainloop_stop(pd->
mainloop);
122 pa_stream_unref(pd->
stream);
126 pa_context_disconnect(pd->
context);
132 pa_threaded_mainloop_free(pd->
mainloop);
153 pa_buffer_attr attr = { -1 };
155 const pa_buffer_attr *queried_attr;
157 pa_channel_map_init_extend(&cmap, pd->
channels, PA_CHANNEL_MAP_WAVEEX);
159 if (!pa_sample_spec_valid(&
ss)) {
178 if (
s->url[0] !=
'\0' && strcmp(
s->url,
"default"))
181 if (!(pd->
mainloop = pa_threaded_mainloop_new())) {
186 if (!(pd->
context = pa_context_new(pa_threaded_mainloop_get_api(pd->
mainloop), pd->
name))) {
198 pa_threaded_mainloop_lock(pd->
mainloop);
200 if (pa_threaded_mainloop_start(pd->
mainloop) < 0) {
202 goto unlock_and_fail;
206 pa_context_state_t
state;
210 if (
state == PA_CONTEXT_READY)
213 if (!PA_CONTEXT_IS_GOOD(
state)) {
215 goto unlock_and_fail;
219 pa_threaded_mainloop_wait(pd->
mainloop);
224 goto unlock_and_fail;
232 ret = pa_stream_connect_record(pd->
stream, device, &attr,
233 PA_STREAM_INTERPOLATE_TIMING
234 |PA_STREAM_ADJUST_LATENCY
235 |PA_STREAM_AUTO_TIMING_UPDATE);
239 goto unlock_and_fail;
243 pa_stream_state_t
state;
247 if (
state == PA_STREAM_READY)
250 if (!PA_STREAM_IS_GOOD(
state)) {
252 goto unlock_and_fail;
256 pa_threaded_mainloop_wait(pd->
mainloop);
260 queried_attr = pa_stream_get_buffer_attr(pd->
stream);
261 if (!queried_attr || queried_attr->fragsize > INT_MAX/100) {
263 goto unlock_and_fail;
268 pa_threaded_mainloop_unlock(pd->
mainloop);
288 pa_threaded_mainloop_unlock(pd->
mainloop);
305 pa_threaded_mainloop_lock(pd->
mainloop);
315 if (read_length <= 0) {
316 pa_threaded_mainloop_wait(pd->
mainloop);
321 r = pa_stream_drop(pd->
stream);
327 goto unlock_and_fail;
331 pa_operation_unref(pa_stream_update_timing_info(pd->
stream,
NULL,
NULL));
333 if (pa_stream_get_latency(pd->
stream, &latency, &negative) >= 0) {
342 if (
pkt->size -
pos < read_length) {
345 pa_stream_drop(pd->
stream);
348 goto unlock_and_fail;
352 pa_stream_drop(pd->
stream);
356 pa_threaded_mainloop_unlock(pd->
mainloop);
368 pa_threaded_mainloop_unlock(pd->
mainloop);
378#define OFFSET(a) offsetof(PulseData, a)
379#define D AV_OPT_FLAG_DECODING_PARAM
380#define DEPR AV_OPT_FLAG_DEPRECATED
386 {
"sample_rate",
"set sample rate in Hz",
OFFSET(sample_rate),
AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX,
D },
389 {
"fragment_size",
"set buffering size, affects latency and cpu usage",
OFFSET(fragment_size),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D },
390 {
"wallclock",
"set the initial pts using the current time",
OFFSET(wallclock),
AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1,
D },
395 .class_name =
"Pulse indev",
const FFInputFormat ff_pulse_demuxer
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define ss(width, name, subs,...)
static int read_data(void *opaque, uint8_t *buf, int buf_size)
static struct @346255127015250356166251341105367306144006377143 state
static int read_header(FFV1Context *f, RangeCoder *c)
static const uint8_t frame_size[4]
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
AVCodecID
Identify the syntax and semantics of the bitstream.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_cold int read_close(AVFormatContext *ctx)
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
static av_cold int pulse_read_header(AVFormatContext *s)
static void stream_state_cb(pa_stream *s, void *userdata)
static av_cold int pulse_close(AVFormatContext *s)
#define CHECK_DEAD_GOTO(p, rerror, label)
static void context_state_cb(pa_context *c, void *userdata)
static const AVClass pulse_demuxer_class
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
#define CHECK_SUCCESS_GOTO(rerror, expression, label)
static void stream_request_cb(pa_stream *s, size_t length, void *userdata)
static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
static void stream_latency_update_cb(pa_stream *s, void *userdata)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
AVChannelLayout ch_layout
The channel layout and number of channels.
enum AVMediaType codec_type
General type of the encoded data.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
The number of audio samples per second.
This structure stores compressed data.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
pa_threaded_mainloop * mainloop
Opaque type representing a time filter state.
int64_t av_gettime(void)
Get the current time in microseconds.
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
TimeFilter * ff_timefilter_new(double time_base, double period, double bandwidth)
Create a new Delay Locked Loop time filter.