[vlc-devel] [PATCH] pulse: save audio volume

Ludovic Fauvet etix at videolan.org
Mon Oct 21 11:13:05 CEST 2013


On 20/10/2013 05:18, Edward Wang wrote:
> The fix in 2a823309242446ed50190b082cf10a2499724a19 is missing many aouts like pulse and others.
> 
> Ref #8503
> ---
>  modules/audio_output/pulse.c                      | 85 +++++++++++++++--------
>  modules/gui/qt4/components/simple_preferences.cpp |  2 +
>  2 files changed, 58 insertions(+), 29 deletions(-)
> 
> diff --git a/modules/audio_output/pulse.c b/modules/audio_output/pulse.c
> index 39b9898..978ca6c 100644
> --- a/modules/audio_output/pulse.c
> +++ b/modules/audio_output/pulse.c
> @@ -37,6 +37,9 @@
>  # include <vlc_xlib.h>
>  #endif
>  
> +#define VOLUME_TEXT N_("Audio volume")
> +#define VOLUME_LONGTEXT VOLUME_TEXT
> +
>  static int  Open        ( vlc_object_t * );
>  static void Close       ( vlc_object_t * );
>  
> @@ -48,6 +51,8 @@ vlc_module_begin ()
>      set_subcategory( SUBCAT_AUDIO_AOUT )
>      add_shortcut( "pulseaudio", "pa" )
>      set_callbacks( Open, Close )
> +    add_float("pulse-volume", 1.0f,
> +        VOLUME_TEXT, VOLUME_LONGTEXT, true)
>  vlc_module_end ()
>  
>  /* NOTE:
> @@ -77,8 +82,37 @@ struct aout_sys_t
>      char *sink_force; /**< Forced sink name (stream must be NULL) */
>  
>      struct sink *sinks; /**< Locally-cached list of sinks */
> +
> +    float f_volume;
>  };
>  
> +/**
> + * Sets the volume.
> + * @note PulseAudio lock required.
> + */
> +static void stream_set_volume(aout_sys_t* sys, float vol)
> +{
> +    /* VLC provides the software volume so convert directly to PulseAudio
> +     * software volume, pa_volume_t. This is not a linear amplification factor
> +     * so do not use PulseAudio linear amplification! */
> +    vol *= PA_VOLUME_NORM;
> +    if (unlikely(vol > PA_VOLUME_MAX))
> +        vol = PA_VOLUME_MAX;
> +    pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
> +
> +    /* Preserve the balance (VLC does not support it). */
> +    pa_cvolume cvolume = sys->cvolume;
> +    pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
> +    pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
> +    assert(pa_cvolume_valid(&cvolume));
> +
> +    pa_operation *op;
> +    uint32_t idx = pa_stream_get_index(sys->stream);
> +    op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
> +                                          NULL, NULL);
> +    if (likely(op != NULL))
> +        pa_operation_unref(op);
> +}
>  
>  /*** Sink ***/
>  static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
> @@ -115,6 +149,10 @@ static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
>          return;
>      (void) ctx;
>  
> +    aout_sys_t *sys = aout->sys;
> +    if(sys->stream)
> +        stream_set_volume(sys, sys->f_volume);

Don't you need a lock around here?

> +
>      msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
>              i->description);
>      aout_HotplugReport(aout, i->name, i->description);
> @@ -356,14 +394,6 @@ static void stream_overflow_cb(pa_stream *s, void *userdata)
>      sys->first_pts = VLC_TS_INVALID;
>  }
>  
> -static void stream_started_cb(pa_stream *s, void *userdata)
> -{
> -    audio_output_t *aout = userdata;
> -
> -    msg_Dbg(aout, "started");
> -    (void) s;
> -}
> -
>  static void stream_suspended_cb(pa_stream *s, void *userdata)
>  {
>      audio_output_t *aout = userdata;
> @@ -594,33 +624,19 @@ static void Flush(audio_output_t *aout, bool wait)
>  static int VolumeSet(audio_output_t *aout, float vol)
>  {
>      aout_sys_t *sys = aout->sys;
> +    if (var_InheritBool(aout, "volume-save"))
> +    {
> +        config_PutFloat(aout, "pulse-volume", vol);
> +        sys->f_volume = vol;
> +    }
> +
>      if (sys->stream == NULL)
>      {
>          msg_Err (aout, "cannot change volume while not playing");

I would probably discard this message or at least change it to debug.

>          return -1;
>      }
> -
> -    /* VLC provides the software volume so convert directly to PulseAudio
> -     * software volume, pa_volume_t. This is not a linear amplification factor
> -     * so do not use PulseAudio linear amplification! */
> -    vol *= PA_VOLUME_NORM;
> -    if (unlikely(vol >= PA_VOLUME_MAX))
> -        vol = PA_VOLUME_MAX;
> -    pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
> -
> -    /* Preserve the balance (VLC does not support it). */
> -    pa_cvolume cvolume = sys->cvolume;
> -    pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
> -    pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
> -    assert(pa_cvolume_valid(&cvolume));
> -
> -    pa_operation *op;
> -    uint32_t idx = pa_stream_get_index(sys->stream);
>      pa_threaded_mainloop_lock(sys->mainloop);
> -    op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
> -                                          NULL, NULL);
> -    if (likely(op != NULL))
> -        pa_operation_unref(op);
> +    stream_set_volume(sys, vol);
>      pa_threaded_mainloop_unlock(sys->mainloop);
>  
>      return 0;
> @@ -650,6 +666,14 @@ static int MuteSet(audio_output_t *aout, bool mute)
>      return 0;
>  }
>  
> +static void stream_started_cb(pa_stream *s, void *userdata)
> +{
> +    audio_output_t *aout = userdata;
> +
> +    msg_Dbg(aout, "started");
> +    (void) s;
> +}
> +
>  static int StreamMove(audio_output_t *aout, const char *name)
>  {
>      aout_sys_t *sys = aout->sys;
> @@ -989,6 +1013,9 @@ static int Open(vlc_object_t *obj)
>      aout->mute_set = MuteSet;
>      aout->device_select = StreamMove;
>  
> +    sys->f_volume = var_InheritFloat(aout, "pulse-volume");
> +    aout_VolumeReport(aout, sys->f_volume);
> +
>      pa_threaded_mainloop_lock(sys->mainloop);
>      /* Sinks (output devices) list */
>      op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
> diff --git a/modules/gui/qt4/components/simple_preferences.cpp b/modules/gui/qt4/components/simple_preferences.cpp
> index 2d1fea9..7e7754f 100644
> --- a/modules/gui/qt4/components/simple_preferences.cpp
> +++ b/modules/gui/qt4/components/simple_preferences.cpp
> @@ -1093,6 +1093,8 @@ void SPrefsPanel::apply()
>              config_PutFloat( p_intf, "alsa-gain", f_gain );
>          if( save_vol_aout( "jack" ) )
>              config_PutFloat( p_intf, "jack-gain", f_gain );
> +        if( save_vol_aout( "pulse" ) )
> +            config_PutFloat( p_intf, "pulse-volume", i_volume / 100.f );

Ok.

>  #endif
>  #undef save_vol_aout
>  
> 

I'm not a PA expert but else that that it looks good to me.

-- 
Ludovic Fauvet
www.videolan.org



More information about the vlc-devel mailing list