[FFmpeg-devel] [PATCH] libavdevice/pulse_audio_enc: remove inevitable early return in mute state and volume change callback

Varun Dhar vdhar1 at protonmail.com
Sun Dec 18 23:30:07 EET 2022


This patch removes an inevitable early return in libavdevice/pulse_audio_enc.c that prevents the user from accessing the volume or mute state of a pulseaudio output device. This happens because pulse_update_sink_input_info() passes a new pulseaudio context to pulse_audio_sink_input_cb(), causing the early return condition s->ctx != ctx (pulse_audio_enc.c:98) to always be true as ctx is a new context whereas s->ctx is the old context. As the early return is inevitable, the user is unable to access the volume or mute state. With this patch, the user will be able to access the volume or mute state of a pulseaudio output device.

The following code is an example showing the issue. It creates a pulse output device, sets the volume, then gets the volume:

#include <stddef.h>
#include <assert.h>

#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include <libavcodec/avcodec.h>

//volume callback
int get_volume(AVFormatContext *ctx, int type, void *data, size_t size){
	if(type == AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED){
		*(double*)ctx->opaque = *(double*)data;
		return 0;
	}
	return AVERROR(ENOSYS);
}

int main(){
	avdevice_register_all();
	//set up pulse output stream
	AVFormatContext* ctx = NULL;
	avformat_alloc_output_context2(&ctx,NULL,"pulse",NULL);
	AVStream* stream = avformat_new_stream(ctx,NULL);
	AVCodecParameters* stream_params = stream->codecpar;
	stream_params->codec_type = AVMEDIA_TYPE_AUDIO;
	stream_params->codec_id = AV_CODEC_ID_PCM_S16LE;
	stream_params->sample_rate = 48000;
	stream_params->ch_layout.nb_channels = 2;
	stream_params->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
	avformat_write_header(ctx,NULL);

	//set volume to 50%
	double vol = 0.5;
	avdevice_app_to_dev_control_message(ctx,AV_APP_TO_DEV_SET_VOLUME,&vol,sizeof(vol));
	vol = 0;

	//get volume (should be 50% (0.5))
	ctx->opaque = &vol;
	ctx->control_message_cb = &get_volume;
	avdevice_app_to_dev_control_message(ctx,AV_APP_TO_DEV_GET_VOLUME,NULL,0);
	assert(vol == 0.5);

	av_write_trailer(ctx);
	avformat_free_context(ctx);
}




More information about the ffmpeg-devel mailing list