[MP1-4838] Volume control no longer functions properly when changing audio device

Discussion in 'General Support' started by Rick164, January 6, 2017.

Tags:
  1. Rick164
    • Super User

    Rick164 Super User

    Joined:
    January 7, 2006
    Messages:
    1,335
    Likes Received:
    918
    Ratings:
    +1,359 / 0
    Home Country:
    Netherlands Netherlands
    Show System Specs
    Hi,

    Been debugging an issue with @Virtual in the AudioSwitcher topic and is as follows:



    - Start Mediaportal with volume control set to master:

    [​IMG]
    [​IMG]

    - Test out volume control via keyboard for instance ( + -), will notice your audio device changing volume from sys tray.
    - Let Mediaportal run and change your default Windows audio device to something else either via AudioSwitcher plugin or Windows sound panel.
    - Test out volume control via keyboard, will notice your volume changing on your old Windows audio device instead and keep the current one as-is.

    Now so far it can be inconsistent and looking over the code below can't find any red flags, best guess is that we only set the audio device on startup and hook that up to volume control but don't check for changes afterwards (during volume events).
    While it works fine during playback you do lose Mediaportal volume control, anyone have any idea what is causing this?

    https://github.com/MediaPortal/MediaPortal-1/blob/master/mediaportal/Core/Player/VolumeHandler.cs

    Added Installer to Jira : https://issues.team-mediaportal.com/browse/MP1-4838 :p
    Thanks @Rick164
     
    Last edited by a moderator: January 11, 2017
    • Thank You! Thank You! x 1
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Stéphane Lenclud
    • Team MediaPortal

    Stéphane Lenclud Development Group

    Joined:
    April 29, 2013
    Messages:
    2,563
    Likes Received:
    657
    Gender:
    Male
    Ratings:
    +1,270 / 8
    Home Country:
    Germany Germany
    Show System Specs
    It seems so.
    That's unlikely to be an issue when using an HID remote or HID multimedia keyboard since we don't do anything other than letting the system do volume changes for us.
     
  4. Rick164
    • Super User

    Rick164 Super User

    Joined:
    January 7, 2006
    Messages:
    1,335
    Likes Received:
    918
    Ratings:
    +1,359 / 0
    Home Country:
    Netherlands Netherlands
    Show System Specs
  5. Sebastiii
    • Team MediaPortal

    Sebastiii Development Group

    Joined:
    November 12, 2007
    Messages:
    16,296
    Likes Received:
    6,248
    Gender:
    Male
    Location:
    France
    Ratings:
    +10,269 / 9
    Home Country:
    France France
    Show System Specs
    In mediaportal.cs we have that : I think it's here the init is done : line 3123

    VolumeHandler.CreateInstance();
    GUIGraphicsContext.VolumeHandler = VolumeHandler.Instance;

    If we are looking in \mediaportal\Core\DShowNET\Helper\DirectShowUtil.cs line 905

    Code (C#):
    1. try
    2.                   {
    3.                     // vh.Volume = 19660500 that means Audio endpoint device are not available.
    4.                     if (GUIGraphicsContext.VolumeHandler != null && GUIGraphicsContext.VolumeHandler.Volume == 19660500) // Check if new audio device is connected
    5.                     {
    6.                       Log.Debug("DirectShowUtil: need dispose volume handler value {0}", GUIGraphicsContext.VolumeHandler.Volume);
    7.                       VolumeHandler.Dispose();
    8.                       GUIGraphicsContext.VolumeHandler = VolumeHandler.Instance;
    9.                     }
    10.                     if (GUIGraphicsContext.VolumeHandler != null && GUIGraphicsContext.VolumeHandler.Volume != 19660500 && GUIGraphicsContext.DeviceAudioConnected > 0)
    11.                     {
    12.                       Log.Debug("DirectShowUtil: volume handler value {0}", GUIGraphicsContext.VolumeHandler.Volume);
    13.                       Log.Debug("DirectShowUtil: build the graph for PIN : {0}", pinName);
    14.                       hr = graphBuilder.Render(pins[0]);
    15.                     }
    16.                   }
    17.                   catch (Exception exception)
    18.                   {
    19.                     Log.Warn("DirectShowUtil: Could not initialize volume handler (don't connect Audio Pin) : {0}", exception.Message);
    20.                   }
    That part does a reinit, maybe it's possible to reinit differently elsewhere ?
     
  6. Rick164
    • Super User

    Rick164 Super User

    Joined:
    January 7, 2006
    Messages:
    1,335
    Likes Received:
    918
    Ratings:
    +1,359 / 0
    Home Country:
    Netherlands Netherlands
    Show System Specs
    Looked at that and seems we can create a new volume instance but does only help with OSD display but not for the actual volume, think we need a way to re-init it in DirectShowUtil or some event that will trigger if audio device changes (no longer primary).

    Ideally the volume control should always change it's default Windows audio device if set to "Default direct sound device" but not sure if that can be done easily.

    Code (Text):
    1.     private void SetPlaybackDevice(CoreAudioDevice device)
    2.     {
    3.       try
    4.       {
    5.         Log.Debug($"Audio Switcher - setting default playback device to: {device.FullName}");
    6.         device.SetAsDefault();
    7.         Log.Debug($"Device audio connected: {GUIGraphicsContext.DeviceAudioConnected}");
    8.  
    9.         if (Settings.LAVbitstreamPerDevice)
    10.         {
    11.           SetLavBitstreamSettings(device.FullName);
    12.         }
    13.  
    14.         Log.Debug("Resetting volume control");
    15.         Thread.Sleep(1000);
    16.         VolumeHandler.Dispose();
    17.         Thread.Sleep(1000);
    18.         VolumeHandler.CreateInstance();
    19.         Log.Debug("Volume control reset");
    20.       }
    21.       catch (Exception ex)
    22.       {
    23.         Log.Error("Error occured during SetPlaybackDevice()");
    24.         Log.Error(ex.Message);
    25.       }
    26.     }
     
    • Like Like x 1
  7. Sebastiii
    • Team MediaPortal

    Sebastiii Development Group

    Joined:
    November 12, 2007
    Messages:
    16,296
    Likes Received:
    6,248
    Gender:
    Male
    Location:
    France
    Ratings:
    +10,269 / 9
    Home Country:
    France France
    Show System Specs
    So ideally we should kept some volume instance value before the change and restore it on new instance :)
    But i think we need also to do : GUIGraphicsContext.VolumeHandler = VolumeHandler.Instance;
    What do you think ?

    Edit : we need to get and restore GUIGraphicsContext.VolumeHandler.Volume i think.
     
    Last edited: January 7, 2017
  8. Rick164
    • Super User

    Rick164 Super User

    Joined:
    January 7, 2006
    Messages:
    1,335
    Likes Received:
    918
    Ratings:
    +1,359 / 0
    Home Country:
    Netherlands Netherlands
    Show System Specs
    Agree, we need to tie up the new instance however requires:

    - A way of knowing if it has changed and update accordingly
    - Change volume on the default Windows device if user has set video audio device to Default or new option in GUI where you can select which device to use like with video audio renderer (default directsound / device X etc..)

    Now the Win32 audio API is pretty hard to deal with and couldn't find any clear examples for events so far, this was one of the reasons I went with AudioSwitcher core API which has easy control and can be events driven (also async for most parts in latest beta)
    Integration with that probably won't work well with Mediaportal but the developer is very helpful as he wrote the wrapper for the Win32 audio API so might possibly have a solution so we could ask him on Github :)
     
    • Like Like x 1
  9. Stéphane Lenclud
    • Team MediaPortal

    Stéphane Lenclud Development Group

    Joined:
    April 29, 2013
    Messages:
    2,563
    Likes Received:
    657
    Gender:
    Male
    Ratings:
    +1,270 / 8
    Home Country:
    Germany Germany
    Show System Specs
    • Like Like x 1
  10. Stéphane Lenclud
    • Team MediaPortal

    Stéphane Lenclud Development Group

    Joined:
    April 29, 2013
    Messages:
    2,563
    Likes Received:
    657
    Gender:
    Male
    Ratings:
    +1,270 / 8
    Home Country:
    Germany Germany
    Show System Specs
    I've just cleaned up my usage of CScore to handle volume changes and default device change notification.
    It's now implemented in that AudioManager class. That could be a useful reference if ever we need to implement something similar in MP1.
     
    • Thank You! Thank You! x 1
  11. mm1352000
    • Team MediaPortal

    mm1352000 Development Group

    Joined:
    September 1, 2008
    Messages:
    21,527
    Likes Received:
    4,738
    Ratings:
    +8,205 / 17
    Home Country:
    New Zealand New Zealand
    @Rick164
    If you're going down the path of investigating proper handling for audio device changes, I think our current code that tracks audio device connection/disconnection could do with a bit of love. Specifically:
    • Find an alternative way to enumerate audio devices at startup that doesn't require DirectShow (ie. replace -->this<--)
    • When an audio device is removed, detect whether that device was being used as the audio renderer for the playing media. Only stop playback if the device is being used. It might be possible to detect this better with a completely different method such as with DirectShow graph events (eg. this one).
     
    • Like Like x 2
    • Thank You! Thank You! x 1
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!