22#include <pulse/pulseaudio.h>
23#include <pulse/error.h>
59 int eol,
void *userdata)
67 pa_threaded_mainloop_signal(
s->mainloop, 0);
69 if (dev->flags & PA_SINK_FLAT_VOLUME)
70 s->base_volume = dev->base_volume;
72 s->base_volume = PA_VOLUME_NORM;
82 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
87 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
88 pa_threaded_mainloop_wait(
s->mainloop);
89 pa_operation_unref(
op);
94 int eol,
void *userdata)
104 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
105 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
110 vol = pa_sw_volume_divide(vol,
s->base_volume);
111 if (
s->last_volume != vol) {
114 s->last_volume = vol;
125 enum pa_operation_state op_state;
126 pa_mainloop *ml =
NULL;
133 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
139 while ((op_state = pa_operation_get_state(
op)) == PA_OPERATION_RUNNING)
140 pa_mainloop_iterate(ml, 1,
NULL);
141 pa_operation_unref(
op);
142 if (op_state != PA_OPERATION_DONE) {
155 uint32_t idx,
void *userdata)
163 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
164 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
176 if (stream !=
s->stream)
180 pa_threaded_mainloop_signal(
s->mainloop, 0);
199 if (stream !=
s->stream)
202 switch (pa_stream_get_state(
s->stream)) {
203 case PA_STREAM_READY:
204 case PA_STREAM_FAILED:
205 case PA_STREAM_TERMINATED:
206 pa_threaded_mainloop_signal(
s->mainloop, 0);
214 pa_stream_state_t
state;
216 while ((
state = pa_stream_get_state(
s->stream)) != PA_STREAM_READY) {
217 if (
state == PA_STREAM_FAILED ||
state == PA_STREAM_TERMINATED)
219 pa_threaded_mainloop_wait(
s->mainloop);
231 switch (pa_context_get_state(
ctx)) {
232 case PA_CONTEXT_READY:
233 case PA_CONTEXT_FAILED:
234 case PA_CONTEXT_TERMINATED:
235 pa_threaded_mainloop_signal(
s->mainloop, 0);
243 pa_context_state_t
state;
245 while ((
state = pa_context_get_state(
s->ctx)) != PA_CONTEXT_READY) {
246 if (
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED)
248 pa_threaded_mainloop_wait(
s->mainloop);
257 if (stream !=
s->stream)
261 pa_threaded_mainloop_signal(
s->mainloop, 0);
267 pa_threaded_mainloop_unlock(
s->mainloop);
272 while (
s->last_result == 2)
273 pa_threaded_mainloop_wait(
s->mainloop);
274 pa_operation_unref(
op);
275 pa_threaded_mainloop_unlock(
s->mainloop);
276 if (
s->last_result != 0)
278 return s->last_result;
284 pa_threaded_mainloop_lock(
s->mainloop);
292 pa_threaded_mainloop_lock(
s->mainloop);
305 pa_threaded_mainloop_signal(
s->mainloop, 0);
311 pa_threaded_mainloop_unlock(
s->mainloop);
316 while (
s->last_result == 2)
317 pa_threaded_mainloop_wait(
s->mainloop);
318 pa_operation_unref(
op);
319 pa_threaded_mainloop_unlock(
s->mainloop);
320 if (
s->last_result != 0)
322 return s->last_result;
328 pa_threaded_mainloop_lock(
s->mainloop);
329 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
339 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
341 vol = pa_sw_volume_multiply(
lrint(volume * PA_VOLUME_NORM),
s->base_volume);
342 pa_cvolume_set(&cvol,
ss->channels, PA_VOLUME_NORM);
343 pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
344 pa_threaded_mainloop_lock(
s->mainloop);
345 op = pa_context_set_sink_input_volume(
s->ctx, pa_stream_get_index(
s->stream),
354 pa_threaded_mainloop_lock(
s->mainloop);
419 pa_threaded_mainloop_lock(
s->mainloop);
421 pa_stream_disconnect(
s->stream);
422 pa_stream_set_state_callback(
s->stream,
NULL,
NULL);
423 pa_stream_set_write_callback(
s->stream,
NULL,
NULL);
424 pa_stream_set_overflow_callback(
s->stream,
NULL,
NULL);
425 pa_stream_set_underflow_callback(
s->stream,
NULL,
NULL);
426 pa_stream_unref(
s->stream);
430 pa_context_disconnect(
s->ctx);
431 pa_context_set_state_callback(
s->ctx,
NULL,
NULL);
432 pa_context_set_subscribe_callback(
s->ctx,
NULL,
NULL);
433 pa_context_unref(
s->ctx);
436 pa_threaded_mainloop_unlock(
s->mainloop);
437 pa_threaded_mainloop_stop(
s->mainloop);
438 pa_threaded_mainloop_free(
s->mainloop);
450 pa_sample_spec sample_spec;
451 pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
453 pa_mainloop_api *mainloop_api;
454 const char *stream_name =
s->stream_name;
455 static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
456 PA_STREAM_AUTO_TIMING_UPDATE |
457 PA_STREAM_NOT_MONOTONIC;
467 stream_name =
h->url;
469 stream_name =
"Playback";
473 if (
s->buffer_duration) {
479 buffer_attributes.tlength =
FFMAX(
s->buffer_size,
av_clip64(bytes, 0, UINT32_MAX - 1));
481 "Buffer duration: %ums recalculated into %"PRId64
" bytes buffer.\n",
482 s->buffer_duration, bytes);
484 }
else if (
s->buffer_size)
485 buffer_attributes.tlength =
s->buffer_size;
487 buffer_attributes.prebuf =
s->prebuf;
489 buffer_attributes.minreq =
s->minreq;
494 if (!pa_sample_spec_valid(&sample_spec)) {
499 if (sample_spec.channels == 1) {
507 if (
channel_map.channels != sample_spec.channels) {
522 s->mainloop = pa_threaded_mainloop_new();
527 if ((ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
529 pa_threaded_mainloop_free(
s->mainloop);
534 pa_threaded_mainloop_lock(
s->mainloop);
536 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
543 s->ctx = pa_context_new(mainloop_api,
s->name);
552 if ((ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
563 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
581 if ((ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
594 buffer_attributes = *pa_stream_get_buffer_attr(
s->stream);
595 s->buffer_size = buffer_attributes.tlength;
596 s->prebuf = buffer_attributes.prebuf;
597 s->minreq = buffer_attributes.minreq;
599 s->buffer_size,
s->prebuf,
s->minreq);
601 pa_threaded_mainloop_unlock(
s->mainloop);
606 pa_threaded_mainloop_lock(
s->mainloop);
612 s->last_volume = PA_VOLUME_INVALID;
613 pa_threaded_mainloop_lock(
s->mainloop);
618 pa_threaded_mainloop_unlock(
s->mainloop);
624 pa_threaded_mainloop_unlock(
s->mainloop);
639 s->timestamp =
pkt->dts;
642 s->timestamp +=
pkt->duration;
650 pa_threaded_mainloop_lock(
s->mainloop);
651 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
655 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
656 if (
s->nonblocking) {
657 pa_threaded_mainloop_unlock(
s->mainloop);
660 pa_threaded_mainloop_wait(
s->mainloop);
663 if ((ret = pa_stream_write(
s->stream,
pkt->data,
pkt->size,
NULL, 0, PA_SEEK_RELATIVE)) < 0) {
667 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
670 pa_threaded_mainloop_unlock(
s->mainloop);
674 pa_threaded_mainloop_unlock(
s->mainloop);
688 pkt.data = (*frame)->data[0];
690 pkt.dts = (*frame)->pkt_dts;
691 pkt.duration = (*frame)->duration;
701 pa_threaded_mainloop_lock(
s->mainloop);
702 pa_stream_get_latency(
s->stream, &latency, &neg);
703 pa_threaded_mainloop_unlock(
s->mainloop);
707 *dts =
s->timestamp - (neg ? -latency : latency);
717 void *
data,
size_t data_size)
747 s->last_volume = PA_VOLUME_INVALID;
748 pa_threaded_mainloop_lock(
s->mainloop);
750 pa_threaded_mainloop_unlock(
s->mainloop);
754 pa_threaded_mainloop_lock(
s->mainloop);
756 pa_threaded_mainloop_unlock(
s->mainloop);
764#define OFFSET(a) offsetof(PulseData, a)
765#define E AV_OPT_FLAG_ENCODING_PARAM
771 {
"buffer_size",
"set buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
772 {
"buffer_duration",
"set buffer duration in millisecs",
OFFSET(buffer_duration),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
779 .class_name =
"PulseAudio outdev",
static double val(void *priv, double ch)
const FFOutputFormat ff_pulse_muxer
static AVFormatContext * ctx
static const uint8_t channel_map[8][8]
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
#define ss(width, name, subs,...)
Public libavutil channel layout APIs header.
static struct @346255127015250356166251341105367306144006377143 state
reference-counted frame API
@ 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...
int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type, void *data, size_t data_size)
Send control message from device to application.
@ AV_APP_TO_DEV_GET_VOLUME
Get volume/mute messages.
@ AV_APP_TO_DEV_TOGGLE_PAUSE
@ AV_APP_TO_DEV_PAUSE
Request pause/play.
@ AV_APP_TO_DEV_MUTE
Mute control messages.
@ AV_APP_TO_DEV_SET_VOLUME
Volume control message.
@ AV_APP_TO_DEV_TOGGLE_MUTE
@ AV_DEV_TO_APP_BUFFER_OVERFLOW
Buffer fullness status messages.
@ AV_DEV_TO_APP_BUFFER_WRITABLE
@ AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED
Volume level change message.
@ AV_DEV_TO_APP_MUTE_STATE_CHANGED
Mute state change message.
@ AV_DEV_TO_APP_BUFFER_UNDERFLOW
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_TOP_BACK_CENTER
@ AV_CHAN_SURROUND_DIRECT_RIGHT
@ AV_CHAN_STEREO_RIGHT
See above.
@ AV_CHAN_SURROUND_DIRECT_LEFT
@ AV_CHAN_STEREO_LEFT
Stereo downmix.
@ AV_CHAN_TOP_FRONT_CENTER
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#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.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define LIBAVUTIL_VERSION_INT
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Macro definitions for various function/variable attributes.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
@ AV_WRITE_UNCODED_FRAME_QUERY
Query whether the feature is possible on this stream.
pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
static void pulse_audio_sink_device_cb(pa_context *c, const pa_sink_info *dev, int eol, void *userdata)
int ff_pulse_audio_connect_context(pa_mainloop **pa_ml, pa_context **pa_ctx, const char *server, const char *description)
int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
void ff_pulse_audio_disconnect_context(pa_mainloop **pa_ml, pa_context **pa_ctx)
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
static int pulse_context_wait(PulseData *s)
static int pulse_subscribe_events(PulseData *s)
static int pulse_stream_wait(PulseData *s)
static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
static void pulse_map_channels_to_pulse(const AVChannelLayout *channel_layout, pa_channel_map *channel_map)
static void pulse_overflow(pa_stream *stream, void *userdata)
static void pulse_stream_result(pa_stream *stream, int success, void *userdata)
static av_cold int pulse_write_header(AVFormatContext *h)
static void pulse_context_result(pa_context *ctx, int success, void *userdata)
static int pulse_set_volume(PulseData *s, double volume)
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
static int pulse_flash_stream(PulseData *s)
static int pulse_write_frame(AVFormatContext *h, int stream_index, AVFrame **frame, unsigned flags)
static void pulse_underflow(pa_stream *stream, void *userdata)
static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name)
static void pulse_audio_sink_device_cb(pa_context *ctx, const pa_sink_info *dev, int eol, void *userdata)
static int pulse_update_sink_info(AVFormatContext *h)
static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
static int pulse_control_message(AVFormatContext *h, int type, void *data, size_t data_size)
static int pulse_set_pause(PulseData *s, int pause)
static void pulse_stream_state(pa_stream *stream, void *userdata)
static void pulse_context_state(pa_context *ctx, void *userdata)
static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
static av_cold int pulse_write_trailer(AVFormatContext *h)
static void pulse_event(pa_context *ctx, pa_subscription_event_type_t t, uint32_t idx, void *userdata)
static const AVClass pulse_muxer_class
static int pulse_finish_context_operation(PulseData *s, pa_operation *op, const char *name)
static int pulse_update_sink_input_info(AVFormatContext *h)
static void pulse_audio_sink_input_cb(pa_context *ctx, const pa_sink_input_info *i, int eol, void *userdata)
static int pulse_set_mute(PulseData *s)
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
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 AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
The number of audio samples per second.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
Rational number (pair of numerator and denominator).
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int buffer_duration
Buffer size in ms, recalculated to buffer_size.
pa_threaded_mainloop * mainloop
int buffer_size
Buffer size in bytes.
int64_t av_gettime(void)
Get the current time in microseconds.