Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(game/five): adhere to 'mute audio on focus loss' for Citizen cod…
…e too
  • Loading branch information
blattersturm committed Jan 13, 2021
1 parent 1ef4e76 commit c17da3a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 1 deletion.
7 changes: 7 additions & 0 deletions code/components/extra-natives-five/src/NuiAudioSink.cpp
Expand Up @@ -28,6 +28,8 @@
#include <ScriptEngine.h>
#include <audDspEffect.h>

#include <GameAudioState.h>

static concurrency::concurrent_queue<std::function<void()>> g_mainQueue;

namespace rage
Expand Down Expand Up @@ -1778,6 +1780,11 @@ static InitFunction initFunction([]()
active = true;
}

if (ShouldMuteGameAudio())
{
active = false;
}

#if SMTEST
static uint8_t mySubmix = 0;

Expand Down
11 changes: 11 additions & 0 deletions code/components/glue/src/GtaNui.cpp
Expand Up @@ -15,6 +15,10 @@

#include <wrl.h>

#if __has_include(<GameAudioState.h>)
#include <GameAudioState.h>
#endif

namespace WRL = Microsoft::WRL;

using nui::GITexture;
Expand Down Expand Up @@ -634,6 +638,13 @@ static GtaNuiInterface nuiGi;

static InitFunction initFunction([]()
{
#if __has_include(<GameAudioState.h>)
nuiGi.QueryShouldMute.Connect([](bool& shouldMute)
{
shouldMute = shouldMute || ShouldMuteGameAudio();
});
#endif

OnGrcCreateDevice.Connect([]()
{
nuiGi.OnInitRenderer();
Expand Down
10 changes: 10 additions & 0 deletions code/components/gta-core-five/include/GameAudioState.h
@@ -0,0 +1,10 @@
#pragma once

// Returns whether or not any game-related audio should be muted as a result of 'mute on focus loss'
bool
#ifdef COMPILING_GTA_CORE_FIVE
DLL_EXPORT
#else
DLL_IMPORT
#endif
ShouldMuteGameAudio();
17 changes: 17 additions & 0 deletions code/components/gta-core-five/src/GameAudioState.cpp
@@ -0,0 +1,17 @@
#include <StdInc.h>
#include <Hooking.h>

static bool* audioNotFocused;
static int* muteOnFocusLoss;

bool DLL_EXPORT ShouldMuteGameAudio()
{
return *audioNotFocused && *muteOnFocusLoss;
}

static HookFunction hookFunction([]()
{
auto location = hook::get_pattern<char>("75 17 40 38 2D ? ? ? ? 74 0E 39 2D", 5);
audioNotFocused = hook::get_address<bool*>(location);
muteOnFocusLoss = hook::get_address<int*>(location + 8);
});
15 changes: 14 additions & 1 deletion code/components/gta-net-five/src/MumbleVoice.cpp
Expand Up @@ -30,6 +30,10 @@

#include <CrossBuildRuntime.h>

#if __has_include(<GameAudioState.h>)
#include <GameAudioState.h>
#endif

class FxNativeInvoke
{
private:
Expand Down Expand Up @@ -253,7 +257,16 @@ static void Mumble_RunFrame()

g_mumbleClient->SetActivationMode(activationMode);

g_mumbleClient->SetOutputVolume(g_preferenceArray[PREF_VOICE_OUTPUT_VOLUME] * 0.1f);
#if __has_include(<GameAudioState.h>)
if (ShouldMuteGameAudio())
{
g_mumbleClient->SetOutputVolume(0.0f);
}
else
#endif
{
g_mumbleClient->SetOutputVolume(g_preferenceArray[PREF_VOICE_OUTPUT_VOLUME] * 0.1f);
}

float cameraFront[3];
float cameraTop[3];
Expand Down
2 changes: 2 additions & 0 deletions code/components/nui-core/include/CefOverlay.h
Expand Up @@ -159,6 +159,8 @@ namespace nui
fwEvent<> OnInitRenderer;

fwEvent<> OnRender;

fwEvent<bool&> QueryShouldMute;
};

void OVERLAY_DECL Initialize(nui::GameInterface* gi);
Expand Down
2 changes: 2 additions & 0 deletions code/components/nui-core/include/NUIWindow.h
Expand Up @@ -142,6 +142,8 @@ class
private:
CefString m_initUrl;

bool m_isMuted = false;

public:
~NUIWindow();

Expand Down
23 changes: 23 additions & 0 deletions code/components/nui-core/src/NUIWindow.cpp
Expand Up @@ -587,6 +587,29 @@ void NUIWindow::TouchMessage()

void NUIWindow::UpdateFrame()
{
if (m_client)
{
auto browser = ((NUIClient*)m_client.get())->GetBrowser();

if (browser)
{
// the CEF API has a 'is muted' getter but it doesn't work
bool shouldMute = false;
g_nuiGi->QueryShouldMute(shouldMute);

if (!m_isMuted && shouldMute)
{
browser->GetHost()->SetAudioMuted(true);
m_isMuted = true;
}
else if (m_isMuted && !shouldMute)
{
browser->GetHost()->SetAudioMuted(false);
m_isMuted = false;
}
}
}

#ifndef IS_RDR3
if (GetPaintType() != NUIPaintTypePostRender)
#endif
Expand Down

0 comments on commit c17da3a

Please sign in to comment.