Skip to content

Commit

Permalink
ApplicationVolumeHandling: move to ApplicationComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
notspiff authored and akva2 committed Oct 15, 2022
1 parent 7a923e4 commit 93ad075
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 97 deletions.
56 changes: 36 additions & 20 deletions xbmc/application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "application/ApplicationPlayer.h"
#include "application/ApplicationPowerHandling.h"
#include "application/ApplicationSkinHandling.h"
#include "application/ApplicationVolumeHandling.h"
#include "cores/AudioEngine/Engines/ActiveAE/ActiveAE.h"
#include "cores/IPlayer.h"
#include "cores/playercorefactory/PlayerCoreFactory.h"
Expand Down Expand Up @@ -219,8 +220,7 @@ using namespace std::chrono_literals;
#define MAX_FFWD_SPEED 5

CApplication::CApplication(void)
: CApplicationPlayerCallback(m_stackHelper),
CApplicationSettingsHandling(static_cast<CApplicationVolumeHandling&>(*this))
: CApplicationPlayerCallback(m_stackHelper)
#ifdef HAS_DVD_DRIVE
,
m_Autorun(new CAutorun())
Expand All @@ -240,10 +240,12 @@ CApplication::CApplication(void)
RegisterComponent(std::make_shared<CApplicationPlayer>());
RegisterComponent(std::make_shared<CApplicationPowerHandling>());
RegisterComponent(std::make_shared<CApplicationSkinHandling>(this, this, m_bInitializing));
RegisterComponent(std::make_shared<CApplicationVolumeHandling>());
}

CApplication::~CApplication(void)
{
DeregisterComponent(typeid(CApplicationVolumeHandling));
DeregisterComponent(typeid(CApplicationSkinHandling));
DeregisterComponent(typeid(CApplicationPowerHandling));
DeregisterComponent(typeid(CApplicationPlayer));
Expand Down Expand Up @@ -424,7 +426,7 @@ bool CApplication::Create()
CServiceBroker::RegisterAE(m_pActiveAE.get());

// initialize m_replayGainSettings
CacheReplayGainSettings(*settings);
GetComponent<CApplicationVolumeHandling>()->CacheReplayGainSettings(*settings);

// load the keyboard layouts
if (!keyboardLayoutManager->Load())
Expand Down Expand Up @@ -605,8 +607,12 @@ bool CApplication::Initialize()
{
m_pActiveAE->Start();
// restore AE's previous volume state
SetHardwareVolume(m_volumeLevel);
CServiceBroker::GetActiveAE()->SetMute(m_muted);

const auto appVolume = GetComponent<CApplicationVolumeHandling>();
const auto level = appVolume->GetVolumeRatio();
const auto muted = appVolume->IsMuted();
appVolume->SetHardwareVolume(level);
CServiceBroker::GetActiveAE()->SetMute(muted);

#if defined(HAS_DVD_DRIVE) && !defined(TARGET_WINDOWS) // somehow this throws an "unresolved external symbol" on win32
// turn off cdio logging
Expand Down Expand Up @@ -1355,8 +1361,9 @@ bool CApplication::OnAction(const CAction &action)

if (action.GetID() == ACTION_MUTE)
{
ToggleMute();
ShowVolumeBar(&action);
const auto appVolume = GetComponent<CApplicationVolumeHandling>();
appVolume->ToggleMute();
appVolume->ShowVolumeBar(&action);
return true;
}

Expand All @@ -1377,21 +1384,26 @@ bool CApplication::OnAction(const CAction &action)
// Check for global volume control
if ((action.GetAmount() && (action.GetID() == ACTION_VOLUME_UP || action.GetID() == ACTION_VOLUME_DOWN)) || action.GetID() == ACTION_VOLUME_SET)
{
const auto appVolume = GetComponent<CApplicationVolumeHandling>();
if (!appPlayer->IsPassthrough())
{
if (m_muted)
UnMute();
float volume = m_volumeLevel;
if (appVolume->IsMuted())
appVolume->UnMute();
float volume = appVolume->GetVolumeRatio();
int volumesteps = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_AUDIOOUTPUT_VOLUMESTEPS);
// sanity check
if (volumesteps == 0)
volumesteps = 90;

// Android has steps based on the max available volume level
#if defined(TARGET_ANDROID)
float step = (VOLUME_MAXIMUM - VOLUME_MINIMUM) / CXBMCApp::GetMaxSystemVolume();
float step = (CApplicationVolumeHandling::VOLUME_MAXIMUM -
CApplicationVolumeHandling::VOLUME_MINIMUM) /
CXBMCApp::GetMaxSystemVolume();
#else
float step = (VOLUME_MAXIMUM - VOLUME_MINIMUM) / volumesteps;
float step = (CApplicationVolumeHandling::VOLUME_MAXIMUM -
CApplicationVolumeHandling::VOLUME_MINIMUM) /
volumesteps;

if (action.GetRepeat())
step *= action.GetRepeat() * 50; // 50 fps
Expand All @@ -1402,13 +1414,14 @@ bool CApplication::OnAction(const CAction &action)
volume -= action.GetAmount() * action.GetAmount() * step;
else
volume = action.GetAmount() * step;
if (volume != m_volumeLevel)
SetVolume(volume, false);
if (volume != appVolume->GetVolumeRatio())
appVolume->SetVolume(volume, false);
}
// show visual feedback of volume or passthrough indicator
ShowVolumeBar(&action);
appVolume->ShowVolumeBar(&action);
return true;
}

if (action.GetID() == ACTION_GUIPROFILE_BEGIN)
{
CGUIControlProfiler::Instance().SetOutputFile(CSpecialProtocol::TranslatePath("special://home/guiprofiler.xml"));
Expand Down Expand Up @@ -1510,7 +1523,7 @@ void CApplication::OnApplicationMessage(ThreadMessage* pMsg)
case TMSG_VOLUME_SHOW:
{
CAction action(pMsg->param1);
ShowVolumeBar(&action);
GetComponent<CApplicationVolumeHandling>()->ShowVolumeBar(&action);
}
break;

Expand Down Expand Up @@ -2514,9 +2527,10 @@ bool CApplication::PlayFile(CFileItem item, const std::string& player, bool bRes
CLog::LogF(LOGDEBUG, "Ignored {} playback thread messages", dMsgCount);
}

const auto appVolume = GetComponent<CApplicationVolumeHandling>();
appPlayer->OpenFile(item, options, m_ServiceManager->GetPlayerCoreFactory(), player, *this);
appPlayer->SetVolume(m_volumeLevel);
appPlayer->SetMute(m_muted);
appPlayer->SetVolume(appVolume->GetVolumeRatio());
appPlayer->SetMute(appVolume->IsMuted());

#if !defined(TARGET_POSIX)
CGUIComponent *gui = CServiceBroker::GetGUI();
Expand Down Expand Up @@ -2654,8 +2668,10 @@ bool CApplication::OnMessage(CGUIMessage& message)
CServiceBroker::GetGUI()->GetWindowManager().Delete(WINDOW_SPLASH);

// show the volumebar if the volume is muted
if (IsMuted() || GetVolumeRatio() <= VOLUME_MINIMUM)
ShowVolumeBar();
const auto appVolume = GetComponent<CApplicationVolumeHandling>();
if (appVolume->IsMuted() ||
appVolume->GetVolumeRatio() <= CApplicationVolumeHandling::VOLUME_MINIMUM)
appVolume->ShowVolumeBar();

if (!m_incompatibleAddons.empty())
{
Expand Down
4 changes: 1 addition & 3 deletions xbmc/application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "application/ApplicationPlayerCallback.h"
#include "application/ApplicationSettingsHandling.h"
#include "application/ApplicationStackHelper.h"
#include "application/ApplicationVolumeHandling.h"
#include "guilib/IMsgTargetCallback.h"
#include "guilib/IWindowManagerCallback.h"
#include "messaging/IMessageTarget.h"
Expand Down Expand Up @@ -88,8 +87,7 @@ class CApplication : public IWindowManagerCallback,
public KODI::MESSAGING::IMessageTarget,
public CApplicationComponents,
public CApplicationPlayerCallback,
public CApplicationSettingsHandling,
public CApplicationVolumeHandling
public CApplicationSettingsHandling
{
friend class CAppInboundProtocol;

Expand Down
17 changes: 8 additions & 9 deletions xbmc/application/ApplicationSettingsHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ bool IsPlaying(const std::string& condition,
}
} // namespace

CApplicationSettingsHandling::CApplicationSettingsHandling(
CApplicationVolumeHandling& volumeHandling)
: m_volumeHandling(volumeHandling)
{
}

void CApplicationSettingsHandling::RegisterSettings()
{
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
Expand Down Expand Up @@ -118,7 +112,8 @@ void CApplicationSettingsHandling::OnSettingChanged(const std::shared_ptr<const
if (appSkin->OnSettingChanged(*setting))
return;

if (m_volumeHandling.OnSettingChanged(*setting))
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
if (appVolume->OnSettingChanged(*setting))
return;

const auto appPower = components.GetComponent<CApplicationPowerHandling>();
Expand Down Expand Up @@ -205,10 +200,14 @@ bool CApplicationSettingsHandling::OnSettingUpdate(const std::shared_ptr<CSettin

bool CApplicationSettingsHandling::Load(const TiXmlNode* settings)
{
return m_volumeHandling.Load(settings);
auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
return appVolume->Load(settings);
}

bool CApplicationSettingsHandling::Save(TiXmlNode* settings) const
{
return m_volumeHandling.Save(settings);
const auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
return appVolume->Save(settings);
}
7 changes: 0 additions & 7 deletions xbmc/application/ApplicationSettingsHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "settings/lib/ISettingCallback.h"
#include "settings/lib/ISettingsHandler.h"

class CApplicationVolumeHandling;

/*!
* \brief Class handling application support for settings.
*/
Expand All @@ -22,9 +20,6 @@ class CApplicationSettingsHandling : public ISettingCallback,
public ISettingsHandler,
public ISubSettings
{
public:
explicit CApplicationSettingsHandling(CApplicationVolumeHandling& volumeHandling);

protected:
void RegisterSettings();
void UnregisterSettings();
Expand All @@ -36,6 +31,4 @@ class CApplicationSettingsHandling : public ISettingCallback,
bool OnSettingUpdate(const std::shared_ptr<CSetting>& setting,
const char* oldSettingId,
const TiXmlNode* oldSettingNode) override;

CApplicationVolumeHandling& m_volumeHandling;
};
7 changes: 6 additions & 1 deletion xbmc/application/ApplicationVolumeHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@

#pragma once

#include "application/IApplicationComponent.h"

class CAction;
class CApplication;
class CSetting;
class CSettings;
class TiXmlNode;

/*!
* \brief Class handling application support for audio volume management.
*/
class CApplicationVolumeHandling
class CApplicationVolumeHandling : public IApplicationComponent
{
friend class CApplication;

public:
// replay gain settings struct for quick access by the player multiple
// times per second (saves doing settings lookup)
Expand Down
9 changes: 6 additions & 3 deletions xbmc/cores/AudioEngine/Sinks/AESinkPULSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "AESinkPULSE.h"

#include "ServiceBroker.h"
#include "application/Application.h"
#include "application/ApplicationComponents.h"
#include "application/ApplicationVolumeHandling.h"
#include "cores/AudioEngine/AESinkFactory.h"
#include "threads/SingleLock.h"
#include "utils/StringUtils.h"
Expand Down Expand Up @@ -1180,10 +1181,12 @@ void CAESinkPULSE::SetVolume(float volume)
m_volume_needs_update = false;
pa_volume_t n_vol = pa_cvolume_avg(&m_Volume);
n_vol = std::min(n_vol, PA_VOLUME_NORM);
per_cent_volume = (float) n_vol / PA_VOLUME_NORM;
per_cent_volume = static_cast<float>(n_vol) / PA_VOLUME_NORM;
// only update internal volume
pa_threaded_mainloop_unlock(m_MainLoop);
g_application.SetVolume(per_cent_volume, false);
auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
appVolume->SetVolume(per_cent_volume, false);
return;
}

Expand Down
12 changes: 9 additions & 3 deletions xbmc/cores/VideoPlayer/VideoPlayerRadioRDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "Interface/DemuxPacket.h"
#include "ServiceBroker.h"
#include "application/Application.h"
#include "application/ApplicationComponents.h"
#include "application/ApplicationVolumeHandling.h"
#include "cores/VideoPlayer/Interface/TimingConstants.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "guilib/GUIComponent.h"
Expand Down Expand Up @@ -882,10 +884,12 @@ unsigned int CDVDRadioRDSData::DecodeTA_TP(const uint8_t* msgElement)
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(19021), g_localizeStrings.Get(29930));
m_TA_TP_TrafficAdvisory = true;
m_TA_TP_TrafficVolume = g_application.GetVolumePercent();
auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
m_TA_TP_TrafficVolume = appVolume->GetVolumePercent();
float trafAdvVol = (float)CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt("pvrplayback.trafficadvisoryvolume");
if (trafAdvVol)
g_application.SetVolume(m_TA_TP_TrafficVolume+trafAdvVol);
appVolume->SetVolume(m_TA_TP_TrafficVolume + trafAdvVol);

CVariant data(CVariant::VariantTypeObject);
data["on"] = true;
Expand All @@ -895,7 +899,9 @@ unsigned int CDVDRadioRDSData::DecodeTA_TP(const uint8_t* msgElement)
if (!traffic_announcement && m_TA_TP_TrafficAdvisory && CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool("pvrplayback.trafficadvisory"))
{
m_TA_TP_TrafficAdvisory = false;
g_application.SetVolume(m_TA_TP_TrafficVolume);
auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
appVolume->SetVolume(m_TA_TP_TrafficVolume);

CVariant data(CVariant::VariantTypeObject);
data["on"] = false;
Expand Down
8 changes: 6 additions & 2 deletions xbmc/cores/paplayer/AudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include "FileItem.h"
#include "ICodec.h"
#include "ServiceBroker.h"
#include "application/Application.h"
#include "application/ApplicationComponents.h"
#include "application/ApplicationVolumeHandling.h"
#include "music/tags/MusicInfoTag.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
Expand Down Expand Up @@ -345,7 +346,10 @@ bool CAudioDecoder::CanSeek()
float CAudioDecoder::GetReplayGain(float &peakVal)
{
#define REPLAY_GAIN_DEFAULT_LEVEL 89.0f
const auto& replayGainSettings = g_application.GetReplayGainSettings();
auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();

const auto& replayGainSettings = appVolume->GetReplayGainSettings();
if (replayGainSettings.iType == ReplayGain::NONE)
return 1.0f;

Expand Down
12 changes: 8 additions & 4 deletions xbmc/dialogs/GUIDialogVolumeBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "GUIDialogVolumeBar.h"

#include "IGUIVolumeBarCallback.h"
#include "application/Application.h"
#include "application/ApplicationComponents.h"
#include "application/ApplicationVolumeHandling.h"
#include "guilib/GUIMessage.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"

Expand All @@ -29,8 +31,10 @@ bool CGUIDialogVolumeBar::OnAction(const CAction &action)
{
if (action.GetID() == ACTION_VOLUME_UP || action.GetID() == ACTION_VOLUME_DOWN || action.GetID() == ACTION_VOLUME_SET || action.GetID() == ACTION_MUTE)
{
if (g_application.IsMuted() ||
g_application.GetVolumeRatio() <= CApplicationVolumeHandling::VOLUME_MINIMUM)
const auto& components = CServiceBroker::GetAppComponents();
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
if (appVolume->IsMuted() ||
appVolume->GetVolumeRatio() <= CApplicationVolumeHandling::VOLUME_MINIMUM)
{ // cancel the timer, dialog needs to stay visible
CancelAutoClose();
}
Expand All @@ -46,7 +50,7 @@ bool CGUIDialogVolumeBar::OnAction(const CAction &action)

bool CGUIDialogVolumeBar::OnMessage(CGUIMessage& message)
{
switch ( message.GetMessage() )
switch (message.GetMessage())
{
case GUI_MSG_WINDOW_INIT:
case GUI_MSG_WINDOW_DEINIT:
Expand Down
Loading

0 comments on commit 93ad075

Please sign in to comment.