From 24e5f5cd3ec779c7d9ab9248762605099017975d Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Wed, 16 Apr 2014 15:24:33 +0200 Subject: lavd/pulse_audio_common: rename variables and enums Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_common.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_common.c b/libavdevice/pulse_audio_common.c index 696cb62b43..bffe9e3417 100644 --- a/libavdevice/pulse_audio_common.c +++ b/libavdevice/pulse_audio_common.c @@ -43,10 +43,10 @@ pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id) } } -enum PulseAudioLoopState { - PA_LOOP_INITIALIZING, - PA_LOOP_READY, - PA_LOOP_FINISHED +enum PulseAudioContextState { + PULSE_CONTEXT_INITIALIZING, + PULSE_CONTEXT_READY, + PULSE_CONTEXT_FINISHED }; typedef struct PulseAudioDeviceList { @@ -58,15 +58,15 @@ typedef struct PulseAudioDeviceList { static void pa_state_cb(pa_context *c, void *userdata) { - enum PulseAudioLoopState *loop_status = userdata; + enum PulseAudioContextState *context_state = userdata; switch (pa_context_get_state(c)) { case PA_CONTEXT_FAILED: case PA_CONTEXT_TERMINATED: - *loop_status = PA_LOOP_FINISHED; + *context_state = PULSE_CONTEXT_FINISHED; break; case PA_CONTEXT_READY: - *loop_status = PA_LOOP_READY; + *context_state = PULSE_CONTEXT_READY; break; default: break; @@ -142,7 +142,7 @@ int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, in pa_operation *pa_op = NULL; pa_context *pa_ctx = NULL; enum pa_operation_state op_state; - enum PulseAudioLoopState loop_state = PA_LOOP_INITIALIZING; + enum PulseAudioContextState loop_state = PULSE_CONTEXT_INITIALIZING; PulseAudioDeviceList dev_list = { 0 }; int i; @@ -168,9 +168,9 @@ int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, in goto fail; } - while (loop_state == PA_LOOP_INITIALIZING) + while (loop_state == PULSE_CONTEXT_INITIALIZING) pa_mainloop_iterate(pa_ml, 1, NULL); - if (loop_state == PA_LOOP_FINISHED) { + if (loop_state == PULSE_CONTEXT_FINISHED) { dev_list.error_code = AVERROR_EXTERNAL; goto fail; } -- cgit v1.2.3 From 176046d2b59c2042cd35a58848d4964563287f63 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Wed, 16 Apr 2014 15:23:46 +0200 Subject: lavd/pulse_audio_common: add context helper functions Functions allow to connect and disconnect from server. Helpfull to implement utility functions with nested loops. Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_common.c | 89 +++++++++++++++++++++++++++------------- libavdevice/pulse_audio_common.h | 5 +++ 2 files changed, 66 insertions(+), 28 deletions(-) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_common.c b/libavdevice/pulse_audio_common.c index bffe9e3417..5a2568b82b 100644 --- a/libavdevice/pulse_audio_common.c +++ b/libavdevice/pulse_audio_common.c @@ -24,6 +24,7 @@ #include "libavutil/attributes.h" #include "libavutil/avstring.h" #include "libavutil/mem.h" +#include "libavutil/avassert.h" pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id) { @@ -73,6 +74,64 @@ static void pa_state_cb(pa_context *c, void *userdata) } } +void ff_pulse_audio_disconnect_context(pa_mainloop **pa_ml, pa_context **pa_ctx) +{ + av_assert0(pa_ml); + av_assert0(pa_ctx); + + if (*pa_ctx) { + pa_context_set_state_callback(*pa_ctx, NULL, NULL); + pa_context_disconnect(*pa_ctx); + pa_context_unref(*pa_ctx); + } + if (*pa_ml) + pa_mainloop_free(*pa_ml); + *pa_ml = NULL; + *pa_ctx = NULL; +} + +int ff_pulse_audio_connect_context(pa_mainloop **pa_ml, pa_context **pa_ctx, + const char *server, const char *description) +{ + int ret; + pa_mainloop_api *pa_mlapi = NULL; + enum PulseAudioContextState context_state = PULSE_CONTEXT_INITIALIZING; + + av_assert0(pa_ml); + av_assert0(pa_ctx); + + *pa_ml = NULL; + *pa_ctx = NULL; + + if (!(*pa_ml = pa_mainloop_new())) + return AVERROR(ENOMEM); + if (!(pa_mlapi = pa_mainloop_get_api(*pa_ml))) { + ret = AVERROR_EXTERNAL; + goto fail; + } + if (!(*pa_ctx = pa_context_new(pa_mlapi, description))) { + ret = AVERROR(ENOMEM); + goto fail; + } + pa_context_set_state_callback(*pa_ctx, pa_state_cb, &context_state); + if (pa_context_connect(*pa_ctx, server, 0, NULL) < 0) { + ret = AVERROR_EXTERNAL; + goto fail; + } + + while (context_state == PULSE_CONTEXT_INITIALIZING) + pa_mainloop_iterate(*pa_ml, 1, NULL); + if (context_state == PULSE_CONTEXT_FINISHED) { + ret = AVERROR_EXTERNAL; + goto fail; + } + return 0; + + fail: + ff_pulse_audio_disconnect_context(pa_ml, pa_ctx); + return ret; +} + static void pulse_add_detected_device(PulseAudioDeviceList *info, const char *name, const char *description) { @@ -138,11 +197,9 @@ static void pulse_server_info_cb(pa_context *c, const pa_server_info *i, void *u int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output) { pa_mainloop *pa_ml = NULL; - pa_mainloop_api *pa_mlapi = NULL; pa_operation *pa_op = NULL; pa_context *pa_ctx = NULL; enum pa_operation_state op_state; - enum PulseAudioContextState loop_state = PULSE_CONTEXT_INITIALIZING; PulseAudioDeviceList dev_list = { 0 }; int i; @@ -152,28 +209,9 @@ int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, in return AVERROR(EINVAL); devices->nb_devices = 0; devices->devices = NULL; - if (!(pa_ml = pa_mainloop_new())) - return AVERROR(ENOMEM); - if (!(pa_mlapi = pa_mainloop_get_api(pa_ml))) { - dev_list.error_code = AVERROR_EXTERNAL; - goto fail; - } - if (!(pa_ctx = pa_context_new(pa_mlapi, "Query devices"))) { - dev_list.error_code = AVERROR(ENOMEM); - goto fail; - } - pa_context_set_state_callback(pa_ctx, pa_state_cb, &loop_state); - if (pa_context_connect(pa_ctx, server, 0, NULL) < 0) { - dev_list.error_code = AVERROR_EXTERNAL; - goto fail; - } - while (loop_state == PULSE_CONTEXT_INITIALIZING) - pa_mainloop_iterate(pa_ml, 1, NULL); - if (loop_state == PULSE_CONTEXT_FINISHED) { - dev_list.error_code = AVERROR_EXTERNAL; + if ((dev_list.error_code = ff_pulse_audio_connect_context(&pa_ml, &pa_ctx, server, "Query devices")) < 0) goto fail; - } if (output) pa_op = pa_context_get_sink_info_list(pa_ctx, pulse_audio_sink_device_cb, &dev_list); @@ -206,11 +244,6 @@ int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, in fail: av_free(dev_list.default_device); - if(pa_ctx) - pa_context_disconnect(pa_ctx); - if (pa_ctx) - pa_context_unref(pa_ctx); - if (pa_ml) - pa_mainloop_free(pa_ml); + ff_pulse_audio_disconnect_context(&pa_ml, &pa_ctx); return dev_list.error_code; } diff --git a/libavdevice/pulse_audio_common.h b/libavdevice/pulse_audio_common.h index b049cdea1a..02534f79a1 100644 --- a/libavdevice/pulse_audio_common.h +++ b/libavdevice/pulse_audio_common.h @@ -30,4 +30,9 @@ pa_sample_format_t ff_codec_id_to_pulse_format(enum AVCodecID codec_id); int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output); +int ff_pulse_audio_connect_context(pa_mainloop **pa_ml, pa_context **pa_ctx, + const char *server, const char *description); + +void ff_pulse_audio_disconnect_context(pa_mainloop **pa_ml, pa_context **pa_ctx); + #endif /* AVDEVICE_PULSE_AUDIO_COMMON_H */ -- cgit v1.2.3 From 10627e7e10f78951ea6edc0f5cf35387043fc448 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Fri, 18 Apr 2014 01:10:20 +0200 Subject: lavd/pulse_audio_enc: cosmetics --- libavdevice/pulse_audio_enc.c | 67 ++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index fa2a7cb0c5..885a1fb15a 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -35,8 +35,8 @@ typedef struct PulseData { const char *device; pa_simple *pa; int64_t timestamp; - int buffer_size; - int buffer_duration; + int buffer_size; /**< Buffer size in bytes */ + int buffer_duration; /**< Buffer size in ms, recalculated to buffer_size */ } PulseData; static av_cold int pulse_write_header(AVFormatContext *h) @@ -44,8 +44,8 @@ static av_cold int pulse_write_header(AVFormatContext *h) PulseData *s = h->priv_data; AVStream *st = NULL; int ret; - pa_sample_spec ss; - pa_buffer_attr attr = { -1, -1, -1, -1, -1 }; + pa_sample_spec sample_spec; + pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 }; const char *stream_name = s->stream_name; if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) { @@ -66,26 +66,30 @@ static av_cold int pulse_write_header(AVFormatContext *h) bytes *= st->codec->channels * st->codec->sample_rate * av_get_bytes_per_sample(st->codec->sample_fmt); bytes /= 1000; - attr.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1)); + buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1)); av_log(s, AV_LOG_DEBUG, "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n", s->buffer_duration, bytes); - av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", attr.tlength); + av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength); } else if (s->buffer_size) - attr.tlength = s->buffer_size; + buffer_attributes.tlength = s->buffer_size; - ss.format = ff_codec_id_to_pulse_format(st->codec->codec_id); - ss.rate = st->codec->sample_rate; - ss.channels = st->codec->channels; + sample_spec.format = ff_codec_id_to_pulse_format(st->codec->codec_id); + sample_spec.rate = st->codec->sample_rate; + sample_spec.channels = st->codec->channels; + if (!pa_sample_spec_valid(&sample_spec)) { + av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n"); + return AVERROR(EINVAL); + } s->pa = pa_simple_new(s->server, // Server s->name, // Application name PA_STREAM_PLAYBACK, s->device, // Device stream_name, // Description of a stream - &ss, // Sample format + &sample_spec, // Sample format NULL, // Use default channel map - &attr, // Buffering attributes + &buffer_attributes, // Buffering attributes &ret); // Result if (!s->pa) { @@ -175,19 +179,18 @@ static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_li #define OFFSET(a) offsetof(PulseData, a) #define E AV_OPT_FLAG_ENCODING_PARAM - static const AVOption options[] = { - { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, - { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E }, - { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, - { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, - { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E }, - { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E }, + { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, + { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E }, + { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, + { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, + { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E }, + { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E }, { NULL } }; static const AVClass pulse_muxer_class = { - .class_name = "Pulse muxer", + .class_name = "PulseAudio muxer", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, @@ -195,17 +198,17 @@ static const AVClass pulse_muxer_class = { }; AVOutputFormat ff_pulse_muxer = { - .name = "pulse", - .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"), - .priv_data_size = sizeof(PulseData), - .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), - .video_codec = AV_CODEC_ID_NONE, - .write_header = pulse_write_header, - .write_packet = pulse_write_packet, - .write_uncoded_frame = pulse_write_frame, - .write_trailer = pulse_write_trailer, + .name = "pulse", + .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"), + .priv_data_size = sizeof(PulseData), + .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), + .video_codec = AV_CODEC_ID_NONE, + .write_header = pulse_write_header, + .write_packet = pulse_write_packet, + .write_uncoded_frame = pulse_write_frame, + .write_trailer = pulse_write_trailer, .get_output_timestamp = pulse_get_output_timestamp, - .get_device_list = pulse_get_device_list, - .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH, - .priv_class = &pulse_muxer_class, + .get_device_list = pulse_get_device_list, + .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH, + .priv_class = &pulse_muxer_class, }; -- cgit v1.2.3 From eb9dee2d0d2d9fafb769c44b8d851fbd98263c34 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Sun, 6 Apr 2014 23:11:46 +0200 Subject: lavd/pulse_audio_enc: replace simple API with async API Async API allows to use full capabilites of PulseAudio. Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_enc.c | 347 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 311 insertions(+), 36 deletions(-) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index 885a1fb15a..779766e963 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -18,13 +18,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include #include #include "libavformat/avformat.h" #include "libavformat/internal.h" #include "libavutil/opt.h" #include "libavutil/time.h" #include "libavutil/log.h" +#include "libavutil/attributes.h" #include "pulse_audio_common.h" typedef struct PulseData { @@ -33,12 +34,203 @@ typedef struct PulseData { const char *name; const char *stream_name; const char *device; - pa_simple *pa; int64_t timestamp; int buffer_size; /**< Buffer size in bytes */ int buffer_duration; /**< Buffer size in ms, recalculated to buffer_size */ + int last_result; + pa_threaded_mainloop *mainloop; + pa_context *ctx; + pa_stream *stream; } PulseData; +static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata) +{ + AVFormatContext *h = userdata; + PulseData *s = h->priv_data; + + if (stream != s->stream) + return; + + pa_threaded_mainloop_signal(s->mainloop, 0); +} + +static void pulse_stream_state(pa_stream *stream, void *userdata) +{ + PulseData *s = userdata; + + if (stream != s->stream) + return; + + switch (pa_stream_get_state(s->stream)) { + case PA_STREAM_READY: + case PA_STREAM_FAILED: + case PA_STREAM_TERMINATED: + pa_threaded_mainloop_signal(s->mainloop, 0); + default: + break; + } +} + +static int pulse_stream_wait(PulseData *s) +{ + pa_stream_state_t state; + + while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) { + if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED) + return AVERROR_EXTERNAL; + pa_threaded_mainloop_wait(s->mainloop); + } + return 0; +} + +static void pulse_context_state(pa_context *ctx, void *userdata) +{ + PulseData *s = userdata; + + if (s->ctx != ctx) + return; + + switch (pa_context_get_state(ctx)) { + case PA_CONTEXT_READY: + case PA_CONTEXT_FAILED: + case PA_CONTEXT_TERMINATED: + pa_threaded_mainloop_signal(s->mainloop, 0); + default: + break; + } +} + +static int pulse_context_wait(PulseData *s) +{ + pa_context_state_t state; + + while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) { + if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED) + return AVERROR_EXTERNAL; + pa_threaded_mainloop_wait(s->mainloop); + } + return 0; +} + +static void pulse_stream_result(pa_stream *stream, int success, void *userdata) +{ + PulseData *s = userdata; + + if (stream != s->stream) + return; + + s->last_result = success ? 0 : AVERROR_EXTERNAL; + pa_threaded_mainloop_signal(s->mainloop, 0); +} + +static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name) +{ + if (!op) { + pa_threaded_mainloop_unlock(s->mainloop); + av_log(s, AV_LOG_ERROR, "%s failed.\n", name); + return AVERROR_EXTERNAL; + } + s->last_result = 2; + while (s->last_result == 2) + pa_threaded_mainloop_wait(s->mainloop); + pa_operation_unref(op); + pa_threaded_mainloop_unlock(s->mainloop); + if (s->last_result != 0) + av_log(s, AV_LOG_ERROR, "%s failed.\n", name); + return s->last_result; +} + +static int pulse_flash_stream(PulseData *s) +{ + pa_operation *op; + pa_threaded_mainloop_lock(s->mainloop); + op = pa_stream_flush(s->stream, pulse_stream_result, s); + return pulse_finish_stream_operation(s, op, "pa_stream_flush"); +} + +static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map) +{ + channel_map->channels = 0; + if (channel_layout & AV_CH_FRONT_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT; + if (channel_layout & AV_CH_FRONT_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT; + if (channel_layout & AV_CH_FRONT_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER; + if (channel_layout & AV_CH_LOW_FREQUENCY) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE; + if (channel_layout & AV_CH_BACK_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT; + if (channel_layout & AV_CH_BACK_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT; + if (channel_layout & AV_CH_FRONT_LEFT_OF_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; + if (channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; + if (channel_layout & AV_CH_BACK_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER; + if (channel_layout & AV_CH_SIDE_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT; + if (channel_layout & AV_CH_SIDE_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT; + if (channel_layout & AV_CH_TOP_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER; + if (channel_layout & AV_CH_TOP_FRONT_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT; + if (channel_layout & AV_CH_TOP_FRONT_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER; + if (channel_layout & AV_CH_TOP_FRONT_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT; + if (channel_layout & AV_CH_TOP_BACK_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT; + if (channel_layout & AV_CH_TOP_BACK_CENTER) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER; + if (channel_layout & AV_CH_TOP_BACK_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT; + if (channel_layout & AV_CH_STEREO_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT; + if (channel_layout & AV_CH_STEREO_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT; + if (channel_layout & AV_CH_WIDE_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0; + if (channel_layout & AV_CH_WIDE_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1; + if (channel_layout & AV_CH_SURROUND_DIRECT_LEFT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2; + if (channel_layout & AV_CH_SURROUND_DIRECT_RIGHT) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3; + if (channel_layout & AV_CH_LOW_FREQUENCY_2) + channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE; +} + +static av_cold int pulse_write_trailer(AVFormatContext *h) +{ + PulseData *s = h->priv_data; + + if (s->mainloop) { + pa_threaded_mainloop_lock(s->mainloop); + if (s->stream) { + pa_stream_disconnect(s->stream); + pa_stream_set_state_callback(s->stream, NULL, NULL); + pa_stream_set_write_callback(s->stream, NULL, NULL); + pa_stream_unref(s->stream); + s->stream = NULL; + } + if (s->ctx) { + pa_context_disconnect(s->ctx); + pa_context_set_state_callback(s->ctx, NULL, NULL); + pa_context_unref(s->ctx); + s->ctx = NULL; + } + pa_threaded_mainloop_unlock(s->mainloop); + pa_threaded_mainloop_stop(s->mainloop); + pa_threaded_mainloop_free(s->mainloop); + s->mainloop = NULL; + } + + return 0; +} + static av_cold int pulse_write_header(AVFormatContext *h) { PulseData *s = h->priv_data; @@ -46,7 +238,12 @@ static av_cold int pulse_write_header(AVFormatContext *h) int ret; pa_sample_spec sample_spec; pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 }; + pa_channel_map channel_map; + pa_mainloop_api *mainloop_api; const char *stream_name = s->stream_name; + static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING | + PA_STREAM_AUTO_TIMING_UPDATE | + PA_STREAM_NOT_MONOTONIC; if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) { av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n"); @@ -82,47 +279,109 @@ static av_cold int pulse_write_header(AVFormatContext *h) return AVERROR(EINVAL); } - s->pa = pa_simple_new(s->server, // Server - s->name, // Application name - PA_STREAM_PLAYBACK, - s->device, // Device - stream_name, // Description of a stream - &sample_spec, // Sample format - NULL, // Use default channel map - &buffer_attributes, // Buffering attributes - &ret); // Result + if (sample_spec.channels == 1) { + channel_map.channels = 1; + channel_map.map[0] = PA_CHANNEL_POSITION_MONO; + } else if (st->codec->channel_layout) { + if (av_get_channel_layout_nb_channels(st->codec->channel_layout) != st->codec->channels) + return AVERROR(EINVAL); + pulse_map_channels_to_pulse(st->codec->channel_layout, &channel_map); + /* Unknown channel is present in channel_layout, let PulseAudio use its default. */ + if (channel_map.channels != sample_spec.channels) { + av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n"); + channel_map.channels = 0; + } + } else + channel_map.channels = 0; - if (!s->pa) { - av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret)); - return AVERROR(EIO); + if (!channel_map.channels) + av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n"); + else if (!pa_channel_map_valid(&channel_map)) { + av_log(s, AV_LOG_ERROR, "Invalid channel map.\n"); + return AVERROR(EINVAL); } - avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ + /* start main loop */ + s->mainloop = pa_threaded_mainloop_new(); + if (!s->mainloop) { + av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n"); + return AVERROR(ENOMEM); + } + if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) { + av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret)); + pa_threaded_mainloop_free(s->mainloop); + s->mainloop = NULL; + return AVERROR_EXTERNAL; + } - return 0; -} + pa_threaded_mainloop_lock(s->mainloop); + + mainloop_api = pa_threaded_mainloop_get_api(s->mainloop); + if (!mainloop_api) { + av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n"); + ret = AVERROR_EXTERNAL; + goto fail; + } + + s->ctx = pa_context_new(mainloop_api, s->name); + if (!s->ctx) { + av_log(s, AV_LOG_ERROR, "Cannot create context.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + pa_context_set_state_callback(s->ctx, pulse_context_state, s); + + if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) { + av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret)); + ret = AVERROR_EXTERNAL; + goto fail; + } + + if ((ret = pulse_context_wait(s)) < 0) { + av_log(s, AV_LOG_ERROR, "Context failed.\n"); + goto fail; + } + + s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec, + channel_map.channels ? &channel_map : NULL); + if (!s->stream) { + av_log(s, AV_LOG_ERROR, "Cannot create stream.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + pa_stream_set_state_callback(s->stream, pulse_stream_state, s); + pa_stream_set_write_callback(s->stream, pulse_stream_writable, h); + + if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes, + stream_flags, NULL, NULL)) < 0) { + av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret)); + ret = AVERROR_EXTERNAL; + goto fail; + } + + if ((ret = pulse_stream_wait(s)) < 0) { + av_log(s, AV_LOG_ERROR, "Stream failed.\n"); + goto fail; + } + + pa_threaded_mainloop_unlock(s->mainloop); + + avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ -static av_cold int pulse_write_trailer(AVFormatContext *h) -{ - PulseData *s = h->priv_data; - pa_simple_flush(s->pa, NULL); - pa_simple_free(s->pa); - s->pa = NULL; return 0; + fail: + pa_threaded_mainloop_unlock(s->mainloop); + pulse_write_trailer(h); + return ret; } static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt) { PulseData *s = h->priv_data; - int error; + int ret; - if (!pkt) { - if (pa_simple_flush(s->pa, &error) < 0) { - av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error)); - return AVERROR(EIO); - } - return 1; - } + if (!pkt) + return pulse_flash_stream(s); if (pkt->dts != AV_NOPTS_VALUE) s->timestamp = pkt->dts; @@ -137,12 +396,24 @@ static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt) s->timestamp += av_rescale_q(samples, r, st->time_base); } - if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) { - av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error)); - return AVERROR(EIO); + pa_threaded_mainloop_lock(s->mainloop); + if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) { + av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n"); + goto fail; + } + while (!pa_stream_writable_size(s->stream)) + pa_threaded_mainloop_wait(s->mainloop); + + if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) { + av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret)); + goto fail; } + pa_threaded_mainloop_unlock(s->mainloop); return 0; + fail: + pa_threaded_mainloop_unlock(s->mainloop); + return AVERROR_EXTERNAL; } static int pulse_write_frame(AVFormatContext *h, int stream_index, @@ -166,9 +437,13 @@ static int pulse_write_frame(AVFormatContext *h, int stream_index, static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall) { PulseData *s = h->priv_data; - pa_usec_t latency = pa_simple_get_latency(s->pa, NULL); + pa_usec_t latency; + int neg; + pa_threaded_mainloop_lock(s->mainloop); + pa_stream_get_latency(s->stream, &latency, &neg); + pa_threaded_mainloop_unlock(s->mainloop); *wall = av_gettime(); - *dts = s->timestamp - latency; + *dts = s->timestamp - (neg ? -latency : latency); } static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list) -- cgit v1.2.3 From bc31a57902b83ae09221d53861c35dcb0109c4f7 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Sun, 6 Apr 2014 23:12:44 +0200 Subject: lavd/pulse_audio_enc: add nonblocking mode Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_enc.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index 779766e963..6c02399e84 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -41,6 +41,7 @@ typedef struct PulseData { pa_threaded_mainloop *mainloop; pa_context *ctx; pa_stream *stream; + int nonblocking; } PulseData; static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata) @@ -257,6 +258,7 @@ static av_cold int pulse_write_header(AVFormatContext *h) else stream_name = "Playback"; } + s->nonblocking = (h->flags & AVFMT_FLAG_NONBLOCK); if (s->buffer_duration) { int64_t bytes = s->buffer_duration; @@ -401,8 +403,13 @@ static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt) av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n"); goto fail; } - while (!pa_stream_writable_size(s->stream)) - pa_threaded_mainloop_wait(s->mainloop); + while (!pa_stream_writable_size(s->stream)) { + if (s->nonblocking) { + pa_threaded_mainloop_unlock(s->mainloop); + return AVERROR(EAGAIN); + } else + pa_threaded_mainloop_wait(s->mainloop); + } if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) { av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret)); -- cgit v1.2.3 From cef118032910230dc6ce845f76bfaf82277297f4 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Fri, 11 Apr 2014 02:05:49 +0200 Subject: lavd/pulse_audio_enc: add buffer fullness messages Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_enc.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index 6c02399e84..b05e12680e 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -48,13 +48,27 @@ static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userda { AVFormatContext *h = userdata; PulseData *s = h->priv_data; + int64_t val = nbytes; if (stream != s->stream) return; + avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &val, sizeof(val)); pa_threaded_mainloop_signal(s->mainloop, 0); } +static void pulse_overflow(pa_stream *stream, void *userdata) +{ + AVFormatContext *h = userdata; + avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_OVERFLOW, NULL, 0); +} + +static void pulse_underflow(pa_stream *stream, void *userdata) +{ + AVFormatContext *h = userdata; + avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_UNDERFLOW, NULL, 0); +} + static void pulse_stream_state(pa_stream *stream, void *userdata) { PulseData *s = userdata; @@ -214,6 +228,8 @@ static av_cold int pulse_write_trailer(AVFormatContext *h) pa_stream_disconnect(s->stream); pa_stream_set_state_callback(s->stream, NULL, NULL); pa_stream_set_write_callback(s->stream, NULL, NULL); + pa_stream_set_overflow_callback(s->stream, NULL, NULL); + pa_stream_set_underflow_callback(s->stream, NULL, NULL); pa_stream_unref(s->stream); s->stream = NULL; } @@ -353,6 +369,8 @@ static av_cold int pulse_write_header(AVFormatContext *h) } pa_stream_set_state_callback(s->stream, pulse_stream_state, s); pa_stream_set_write_callback(s->stream, pulse_stream_writable, h); + pa_stream_set_overflow_callback(s->stream, pulse_overflow, h); + pa_stream_set_underflow_callback(s->stream, pulse_underflow, h); if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes, stream_flags, NULL, NULL)) < 0) { -- cgit v1.2.3 From 4c0f36412abca964efe675599054a0cdd84fce29 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Tue, 15 Apr 2014 22:06:17 +0200 Subject: lavd/avdevice: add volume messages Signed-off-by: Lukasz Marek --- libavdevice/avdevice.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'libavdevice') diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h index 6085eb4a04..92678ebd45 100644 --- a/libavdevice/avdevice.h +++ b/libavdevice/avdevice.h @@ -154,6 +154,41 @@ enum AVAppToDevMessageType { AV_APP_TO_DEV_PAUSE = MKBETAG('P', 'A', 'U', ' '), AV_APP_TO_DEV_PLAY = MKBETAG('P', 'L', 'A', 'Y'), AV_APP_TO_DEV_TOGGLE_PAUSE = MKBETAG('P', 'A', 'U', 'T'), + + /** + * Volume control message. + * + * Set volume level. It may be device-dependent if volume + * is changed per stream or system wide. Per stream volume + * change is expected when possible. + * + * data: double: new volume with range of 0.0 - 1.0. + */ + AV_APP_TO_DEV_SET_VOLUME = MKBETAG('S', 'V', 'O', 'L'), + + /** + * Mute control messages. + * + * Change mute state. It may be device-dependent if mute status + * is changed per stream or system wide. Per stream mute status + * change is expected when possible. + * + * data: NULL. + */ + AV_APP_TO_DEV_MUTE = MKBETAG(' ', 'M', 'U', 'T'), + AV_APP_TO_DEV_UNMUTE = MKBETAG('U', 'M', 'U', 'T'), + AV_APP_TO_DEV_TOGGLE_MUTE = MKBETAG('T', 'M', 'U', 'T'), + + /** + * Get volume/mute messages. + * + * Force the device to send AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED or + * AV_DEV_TO_APP_MUTE_STATE_CHANGED command respectively. + * + * data: NULL. + */ + AV_APP_TO_DEV_GET_VOLUME = MKBETAG('G', 'V', 'O', 'L'), + AV_APP_TO_DEV_GET_MUTE = MKBETAG('G', 'M', 'U', 'T'), }; /** @@ -237,6 +272,24 @@ enum AVDevToAppMessageType { */ AV_DEV_TO_APP_BUFFER_READABLE = MKBETAG('B','R','D',' '), AV_DEV_TO_APP_BUFFER_WRITABLE = MKBETAG('B','W','R',' '), + + /** + * Mute state change message. + * + * Device informs that mute state has changed. + * + * data: int: 0 for not muted state, non-zero for muted state. + */ + AV_DEV_TO_APP_MUTE_STATE_CHANGED = MKBETAG('C','M','U','T'), + + /** + * Volume level change message. + * + * Device informs that volume level has changed. + * + * data: double: new volume with range of 0.0 - 1.0. + */ + AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED = MKBETAG('C','V','O','L'), }; /** -- cgit v1.2.3 From eb9136a7d60e3a528f05ae9f9be34e4607b327bc Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Tue, 15 Apr 2014 22:07:09 +0200 Subject: lavd/pulse_audio_enc: implement volume messages Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_enc.c | 246 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index b05e12680e..473421474e 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include #include "libavformat/avformat.h" @@ -42,8 +43,123 @@ typedef struct PulseData { pa_context *ctx; pa_stream *stream; int nonblocking; + int mute; + pa_volume_t base_volume; + pa_volume_t last_volume; } PulseData; +static void pulse_audio_sink_device_cb(pa_context *ctx, const pa_sink_info *dev, + int eol, void *userdata) +{ + PulseData *s = userdata; + + if (s->ctx != ctx) + return; + + if (eol) { + pa_threaded_mainloop_signal(s->mainloop, 0); + } else { + if (dev->flags & PA_SINK_FLAT_VOLUME) + s->base_volume = dev->base_volume; + else + s->base_volume = PA_VOLUME_NORM; + av_log(s, AV_LOG_DEBUG, "base volume: %u\n", s->base_volume); + } +} + +/* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */ +static int pulse_update_sink_info(AVFormatContext *h) +{ + PulseData *s = h->priv_data; + pa_operation *op; + if (!(op = pa_context_get_sink_info_by_name(s->ctx, s->device, + pulse_audio_sink_device_cb, s))) { + av_log(s, AV_LOG_ERROR, "pa_context_get_sink_info_by_name failed.\n"); + return AVERROR_EXTERNAL; + } + while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(s->mainloop); + pa_operation_unref(op); + return 0; +} + +static void pulse_audio_sink_input_cb(pa_context *ctx, const pa_sink_input_info *i, + int eol, void *userdata) +{ + AVFormatContext *h = userdata; + PulseData *s = h->priv_data; + + if (s->ctx != ctx) + return; + + if (!eol) { + double val; + pa_volume_t vol = pa_cvolume_avg(&i->volume); + if (s->mute < 0 || (s->mute && !i->mute) || (!s->mute && i->mute)) { + s->mute = i->mute; + avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_MUTE_STATE_CHANGED, &s->mute, sizeof(s->mute)); + } + + vol = pa_sw_volume_divide(vol, s->base_volume); + if (s->last_volume != vol) { + val = (double)vol / PA_VOLUME_NORM; + avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED, &val, sizeof(val)); + s->last_volume = vol; + } + } +} + +/* This function creates new loop so may be called from PA callbacks. + Mainloop must be locked before calling this function as it operates on streams. */ +static int pulse_update_sink_input_info(AVFormatContext *h) +{ + PulseData *s = h->priv_data; + pa_operation *op; + enum pa_operation_state op_state; + pa_mainloop *ml = NULL; + pa_context *ctx = NULL; + int ret = 0; + + if ((ret = ff_pulse_audio_connect_context(&ml, &ctx, s->server, "Update sink input information")) < 0) + return ret; + + if (!(op = pa_context_get_sink_input_info(ctx, pa_stream_get_index(s->stream), + pulse_audio_sink_input_cb, h))) { + ret = AVERROR_EXTERNAL; + goto fail; + } + + while ((op_state = pa_operation_get_state(op)) == PA_OPERATION_RUNNING) + pa_mainloop_iterate(ml, 1, NULL); + pa_operation_unref(op); + if (op_state != PA_OPERATION_DONE) { + ret = AVERROR_EXTERNAL; + goto fail; + } + + fail: + ff_pulse_audio_disconnect_context(&ml, &ctx); + if (ret) + av_log(s, AV_LOG_ERROR, "pa_context_get_sink_input_info failed.\n"); + return ret; +} + +static void pulse_event(pa_context *ctx, pa_subscription_event_type_t t, + uint32_t idx, void *userdata) +{ + AVFormatContext *h = userdata; + PulseData *s = h->priv_data; + + if (s->ctx != ctx) + return; + + if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) { + if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE) + // Calling from mainloop callback. No need to lock mainloop. + pulse_update_sink_input_info(h); + } +} + static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata) { AVFormatContext *h = userdata; @@ -163,6 +279,68 @@ static int pulse_flash_stream(PulseData *s) return pulse_finish_stream_operation(s, op, "pa_stream_flush"); } +static void pulse_context_result(pa_context *ctx, int success, void *userdata) +{ + PulseData *s = userdata; + + if (s->ctx != ctx) + return; + + s->last_result = success ? 0 : AVERROR_EXTERNAL; + pa_threaded_mainloop_signal(s->mainloop, 0); +} + +static int pulse_finish_context_operation(PulseData *s, pa_operation *op, const char *name) +{ + if (!op) { + pa_threaded_mainloop_unlock(s->mainloop); + av_log(s, AV_LOG_ERROR, "%s failed.\n", name); + return AVERROR_EXTERNAL; + } + s->last_result = 2; + while (s->last_result == 2) + pa_threaded_mainloop_wait(s->mainloop); + pa_operation_unref(op); + pa_threaded_mainloop_unlock(s->mainloop); + if (s->last_result != 0) + av_log(s, AV_LOG_ERROR, "%s failed.\n", name); + return s->last_result; +} + +static int pulse_set_mute(PulseData *s) +{ + pa_operation *op; + pa_threaded_mainloop_lock(s->mainloop); + op = pa_context_set_sink_input_mute(s->ctx, pa_stream_get_index(s->stream), + s->mute, pulse_context_result, s); + return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_mute"); +} + +static int pulse_set_volume(PulseData *s, double volume) +{ + pa_operation *op; + pa_cvolume cvol; + pa_volume_t vol; + const pa_sample_spec *ss = pa_stream_get_sample_spec(s->stream); + + vol = pa_sw_volume_multiply(lround(volume * PA_VOLUME_NORM), s->base_volume); + pa_cvolume_set(&cvol, ss->channels, PA_VOLUME_NORM); + pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol); + pa_threaded_mainloop_lock(s->mainloop); + op = pa_context_set_sink_input_volume(s->ctx, pa_stream_get_index(s->stream), + &cvol, pulse_context_result, s); + return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_volume"); +} + +static int pulse_subscribe_events(PulseData *s) +{ + pa_operation *op; + + pa_threaded_mainloop_lock(s->mainloop); + op = pa_context_subscribe(s->ctx, PA_SUBSCRIPTION_MASK_SINK_INPUT, pulse_context_result, s); + return pulse_finish_context_operation(s, op, "pa_context_subscribe"); +} + static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map) { channel_map->channels = 0; @@ -236,6 +414,7 @@ static av_cold int pulse_write_trailer(AVFormatContext *h) if (s->ctx) { pa_context_disconnect(s->ctx); pa_context_set_state_callback(s->ctx, NULL, NULL); + pa_context_set_subscribe_callback(s->ctx, NULL, NULL); pa_context_unref(s->ctx); s->ctx = NULL; } @@ -348,6 +527,7 @@ static av_cold int pulse_write_header(AVFormatContext *h) goto fail; } pa_context_set_state_callback(s->ctx, pulse_context_state, s); + pa_context_set_subscribe_callback(s->ctx, pulse_event, h); if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) { av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret)); @@ -362,6 +542,12 @@ static av_cold int pulse_write_header(AVFormatContext *h) s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec, channel_map.channels ? &channel_map : NULL); + + if ((ret = pulse_update_sink_info(h)) < 0) { + av_log(s, AV_LOG_ERROR, "Updating sink info failed.\n"); + goto fail; + } + if (!s->stream) { av_log(s, AV_LOG_ERROR, "Cannot create stream.\n"); ret = AVERROR(ENOMEM); @@ -386,6 +572,23 @@ static av_cold int pulse_write_header(AVFormatContext *h) pa_threaded_mainloop_unlock(s->mainloop); + if ((ret = pulse_subscribe_events(s)) < 0) { + av_log(s, AV_LOG_ERROR, "Event subscription failed.\n"); + /* a bit ugly but the simplest to lock here*/ + pa_threaded_mainloop_lock(s->mainloop); + goto fail; + } + + /* force control messages */ + s->mute = -1; + s->last_volume = PA_VOLUME_INVALID; + pa_threaded_mainloop_lock(s->mainloop); + if ((ret = pulse_update_sink_input_info(h)) < 0) { + av_log(s, AV_LOG_ERROR, "Updating sink input info failed.\n"); + goto fail; + } + pa_threaded_mainloop_unlock(s->mainloop); + avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ return 0; @@ -477,6 +680,48 @@ static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_li return ff_pulse_audio_get_devices(device_list, s->server, 1); } +static int pulse_control_message(AVFormatContext *h, int type, + void *data, size_t data_size) +{ + PulseData *s = h->priv_data; + int ret; + + switch(type) { + case AV_APP_TO_DEV_MUTE: + if (!s->mute) { + s->mute = 1; + return pulse_set_mute(s); + } + return 0; + case AV_APP_TO_DEV_UNMUTE: + if (s->mute) { + s->mute = 0; + return pulse_set_mute(s); + } + return 0; + case AV_APP_TO_DEV_TOGGLE_MUTE: + s->mute = !s->mute; + return pulse_set_mute(s); + case AV_APP_TO_DEV_SET_VOLUME: + return pulse_set_volume(s, *(double *)data); + case AV_APP_TO_DEV_GET_VOLUME: + s->last_volume = PA_VOLUME_INVALID; + pa_threaded_mainloop_lock(s->mainloop); + ret = pulse_update_sink_input_info(h); + pa_threaded_mainloop_unlock(s->mainloop); + return ret; + case AV_APP_TO_DEV_GET_MUTE: + s->mute = -1; + pa_threaded_mainloop_lock(s->mainloop); + ret = pulse_update_sink_input_info(h); + pa_threaded_mainloop_unlock(s->mainloop); + return ret; + default: + break; + } + return AVERROR(ENOSYS); +} + #define OFFSET(a) offsetof(PulseData, a) #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { @@ -509,6 +754,7 @@ AVOutputFormat ff_pulse_muxer = { .write_trailer = pulse_write_trailer, .get_output_timestamp = pulse_get_output_timestamp, .get_device_list = pulse_get_device_list, + .control_message = pulse_control_message, .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH, .priv_class = &pulse_muxer_class, }; -- cgit v1.2.3 From 35bf7a491e4df3399a36588e1377c39e8d5a7e64 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Wed, 16 Apr 2014 20:16:20 +0200 Subject: lavd/pulse_audio_enc: implement pause messages Signed-off-by: Lukasz Marek --- libavdevice/pulse_audio_enc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'libavdevice') diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c index 473421474e..92e0697aeb 100644 --- a/libavdevice/pulse_audio_enc.c +++ b/libavdevice/pulse_audio_enc.c @@ -271,6 +271,14 @@ static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const c return s->last_result; } +static int pulse_set_pause(PulseData *s, int pause) +{ + pa_operation *op; + pa_threaded_mainloop_lock(s->mainloop); + op = pa_stream_cork(s->stream, pause, pulse_stream_result, s); + return pulse_finish_stream_operation(s, op, "pa_stream_cork"); +} + static int pulse_flash_stream(PulseData *s) { pa_operation *op; @@ -687,6 +695,12 @@ static int pulse_control_message(AVFormatContext *h, int type, int ret; switch(type) { + case AV_APP_TO_DEV_PAUSE: + return pulse_set_pause(s, 1); + case AV_APP_TO_DEV_PLAY: + return pulse_set_pause(s, 0); + case AV_APP_TO_DEV_TOGGLE_PAUSE: + return pulse_set_pause(s, !pa_stream_is_corked(s->stream)); case AV_APP_TO_DEV_MUTE: if (!s->mute) { s->mute = 1; -- cgit v1.2.3