mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-08-29 12:45:28 -05:00
Compare commits
1 Commits
play-mono
...
now-playin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a13a0774b4 |
@@ -904,9 +904,19 @@ AdvSceneSwitcher.condition.streamDeck.stopListen="Stop listening"
|
||||
AdvSceneSwitcher.condition.streamDeck.pluginDownload="<html><head/><body><p>The Stream Deck plugin can be found <a href=\"https://github.com/WarmUpTill/advanced-scene-switcher-streamdeck-plugin/releases\"><span style=\" text-decoration: underline; color:#268bd2;\">here on GitHub</span></a>.</p></body></html>"
|
||||
AdvSceneSwitcher.condition.gameCapture="Game capture"
|
||||
AdvSceneSwitcher.condition.gameCapture.entry="{{sources}}hooked a game."
|
||||
|
||||
AdvSceneSwitcher.condition.screenshot="Screenshot"
|
||||
AdvSceneSwitcher.condition.screenshot.entry="A screenshot was taken"
|
||||
AdvSceneSwitcher.condition.nowPlaying="Now Playing"
|
||||
AdvSceneSwitcher.condition.nowPlaying.checkType.playbackState="Playback state"
|
||||
AdvSceneSwitcher.condition.nowPlaying.checkType.title="Title"
|
||||
AdvSceneSwitcher.condition.nowPlaying.checkType.artist="Artist"
|
||||
AdvSceneSwitcher.condition.nowPlaying.checkType.album="Album"
|
||||
AdvSceneSwitcher.condition.nowPlaying.checkType.appName="App name"
|
||||
AdvSceneSwitcher.condition.nowPlaying.playbackState.playing="Playing"
|
||||
AdvSceneSwitcher.condition.nowPlaying.playbackState.paused="Paused"
|
||||
AdvSceneSwitcher.condition.nowPlaying.playbackState.stopped="Stopped"
|
||||
AdvSceneSwitcher.condition.nowPlaying.playbackState.opening="Opening"
|
||||
AdvSceneSwitcher.condition.nowPlaying.playbackState.changing="Changing"
|
||||
AdvSceneSwitcher.condition.mqtt="MQTT"
|
||||
AdvSceneSwitcher.condition.mqtt.layout.match="Message was received from{{connection}} which matches{{regex}}:"
|
||||
AdvSceneSwitcher.condition.mqtt.layout.listen="Set message selection to incoming message:{{listenButton}}"
|
||||
@@ -973,7 +983,6 @@ AdvSceneSwitcher.action.playAudio.monitorUnavailable="Audio monitoring not avail
|
||||
AdvSceneSwitcher.action.playAudio.tracks="Tracks"
|
||||
AdvSceneSwitcher.action.playAudio.layout.startOffset="{{useStartOffset}}Start at{{startOffset}}"
|
||||
AdvSceneSwitcher.action.playAudio.layout.playbackDuration="{{useDuration}}Play for{{playbackDuration}}(0 = until end)"
|
||||
AdvSceneSwitcher.action.playAudio.mono="Mono"
|
||||
AdvSceneSwitcher.action.recording="Recording"
|
||||
AdvSceneSwitcher.action.recording.type.stop="Stop recording"
|
||||
AdvSceneSwitcher.action.recording.type.start="Start recording"
|
||||
@@ -2600,6 +2609,12 @@ AdvSceneSwitcher.tempVar.gameCapture.class.description="Window class of the appl
|
||||
AdvSceneSwitcher.tempVar.gameCapture.executable="Executable"
|
||||
AdvSceneSwitcher.tempVar.gameCapture.executable.description="Executable name of the application captured by the source."
|
||||
|
||||
AdvSceneSwitcher.tempVar.nowPlaying.title="Title"
|
||||
AdvSceneSwitcher.tempVar.nowPlaying.artist="Artist"
|
||||
AdvSceneSwitcher.tempVar.nowPlaying.album="Album"
|
||||
AdvSceneSwitcher.tempVar.nowPlaying.appName="App name"
|
||||
AdvSceneSwitcher.tempVar.nowPlaying.playbackStatus="Playback status"
|
||||
|
||||
AdvSceneSwitcher.tempVar.http.status="Status code"
|
||||
AdvSceneSwitcher.tempVar.http.error="Error"
|
||||
AdvSceneSwitcher.tempVar.http.error.description="Empty when no error occurred.\nOther possible values:\n\n * Could not establish connection\n * Failed to bind IP address\n * Failed to read connection\n * Failed to write connection\n * Maximum redirect count exceeded\n * Connection handling canceled\n * SSL connection failed\n * SSL certificate loading failed\n * SSL server verification failed\n * Unsupported HTTP multipart boundary characters\n * Compression failed\n * Connection timed out\n * Proxy connection failed\n * Unknown"
|
||||
|
||||
@@ -42,6 +42,7 @@ add_plugin(twitch)
|
||||
add_plugin(usb)
|
||||
add_plugin(video)
|
||||
add_plugin(speech)
|
||||
add_plugin(now-playing)
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
@@ -2,64 +2,18 @@
|
||||
#include "audio-helpers.hpp"
|
||||
#include "layout-helpers.hpp"
|
||||
#include "macro-helpers.hpp"
|
||||
#include "plugin-state-helpers.hpp"
|
||||
#include "sync-helpers.hpp"
|
||||
|
||||
#include <obs-frontend-api.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <QFileInfo>
|
||||
#include <QLabel>
|
||||
|
||||
namespace advss {
|
||||
|
||||
// Use a high output channel index that is unlikely to be claimed by OBS or
|
||||
// other plugins. OBS supports channels 0-63; channel 0 is the main scene.
|
||||
static constexpr uint32_t kPlaybackOutputChannel = 63;
|
||||
|
||||
// Tracks rawSource handles held by background cleanup threads so they can be
|
||||
// stopped early when OBS begins shutdown, before audio is freed.
|
||||
static std::mutex g_activeSourcesMutex;
|
||||
static std::set<obs_source_t *> g_activeSources;
|
||||
|
||||
static void registerActiveSource(obs_source_t *source)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_activeSourcesMutex);
|
||||
g_activeSources.insert(source);
|
||||
}
|
||||
|
||||
static void unregisterActiveSource(obs_source_t *source)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_activeSourcesMutex);
|
||||
g_activeSources.erase(source);
|
||||
}
|
||||
|
||||
static void stopAllActiveSources()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_activeSourcesMutex);
|
||||
for (auto *source : g_activeSources) {
|
||||
obs_source_media_stop(source);
|
||||
}
|
||||
}
|
||||
|
||||
static void handleObsEvent(enum obs_frontend_event event, void *)
|
||||
{
|
||||
if (event == OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN ||
|
||||
event == OBS_FRONTEND_EVENT_EXIT) {
|
||||
stopAllActiveSources();
|
||||
}
|
||||
}
|
||||
|
||||
static bool setup()
|
||||
{
|
||||
AddPluginPostLoadStep([]() {
|
||||
obs_frontend_add_event_callback(handleObsEvent, nullptr);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool setupDone = setup();
|
||||
|
||||
const std::string MacroActionPlayAudio::id = "play_audio";
|
||||
|
||||
bool MacroActionPlayAudio::_registered = MacroActionFactory::Register(
|
||||
@@ -69,7 +23,6 @@ bool MacroActionPlayAudio::_registered = MacroActionFactory::Register(
|
||||
|
||||
static void deactivatePlayback(obs_source_t *source, bool wantsOutput)
|
||||
{
|
||||
obs_source_media_stop(source);
|
||||
if (wantsOutput) {
|
||||
obs_set_output_source(kPlaybackOutputChannel, nullptr);
|
||||
} else {
|
||||
@@ -85,7 +38,8 @@ static void waitForPlaybackToEnd(Macro *macro, obs_source_t *source,
|
||||
std::unique_lock<std::mutex> lock(*GetMutex());
|
||||
SetMacroAbortWait(false);
|
||||
|
||||
// Poll until the source starts playing or the macro is stopped.
|
||||
// The media source needs time to open and decode before reaching
|
||||
// PLAYING state. Poll until it starts (or the macro is stopped).
|
||||
while (!MacroWaitShouldAbort() && !MacroIsStopped(macro)) {
|
||||
if (obs_source_media_get_state(source) ==
|
||||
OBS_MEDIA_STATE_PLAYING) {
|
||||
@@ -94,8 +48,9 @@ static void waitForPlaybackToEnd(Macro *macro, obs_source_t *source,
|
||||
GetMacroWaitCV().wait_for(lock, 10ms);
|
||||
}
|
||||
|
||||
// Wait for playback to end. Two consecutive non-playing samples are
|
||||
// required to avoid false positives on brief state transitions.
|
||||
// Now wait for playback to end. Require two consecutive non-playing
|
||||
// samples to avoid false positives on brief state transitions.
|
||||
// If maxMs > 0, also stop once that many milliseconds have elapsed.
|
||||
const auto playbackStart = std::chrono::steady_clock::now();
|
||||
static const int kStopThreshold = 2;
|
||||
int stoppedCount = 0;
|
||||
@@ -140,6 +95,7 @@ bool MacroActionPlayAudio::PerformAction()
|
||||
obs_data_set_string(settings, "local_file", path.c_str());
|
||||
obs_data_set_bool(settings, "is_local_file", true);
|
||||
obs_data_set_bool(settings, "looping", false);
|
||||
// Disable automatic restart on activate so we control start explicitly.
|
||||
obs_data_set_bool(settings, "restart_on_activate", false);
|
||||
obs_data_set_bool(settings, "close_when_inactive", true);
|
||||
|
||||
@@ -157,11 +113,10 @@ bool MacroActionPlayAudio::PerformAction()
|
||||
DecibelToPercent(static_cast<float>(_volumeDB.GetValue()));
|
||||
obs_source_set_volume(source, vol);
|
||||
obs_source_set_monitoring_type(source, _monitorType);
|
||||
if (_mono) {
|
||||
obs_source_set_flags(source, OBS_SOURCE_FLAG_FORCE_MONO);
|
||||
}
|
||||
|
||||
// Fall back to monitor-only if all output tracks are deselected.
|
||||
// Fall back to monitor-only if all output tracks are deselected —
|
||||
// there is no point routing through the output channel if the mixer
|
||||
// mask would silence every track.
|
||||
const bool wantsOutput =
|
||||
(_monitorType != OBS_MONITORING_TYPE_MONITOR_ONLY) &&
|
||||
(_audioMixers != 0);
|
||||
@@ -173,8 +128,10 @@ bool MacroActionPlayAudio::PerformAction()
|
||||
|
||||
if (wantsOutput) {
|
||||
obs_source_set_audio_mixers(source, _audioMixers);
|
||||
// Route through a private scene positioned off-screen so the
|
||||
// audio mixes into the output without video appearing on screen.
|
||||
// Route through a private scene so we can position the scene
|
||||
// item far off-screen. This keeps the item "visible" (so audio
|
||||
// is still mixed) while ensuring its video never intersects the
|
||||
// output frame.
|
||||
OBSSceneAutoRelease audioScene =
|
||||
obs_scene_create_private("advss_audio_scene");
|
||||
obs_sceneitem_t *item = obs_scene_add(audioScene, source);
|
||||
@@ -184,7 +141,8 @@ bool MacroActionPlayAudio::PerformAction()
|
||||
}
|
||||
obs_set_output_source(kPlaybackOutputChannel,
|
||||
obs_scene_get_source(audioScene));
|
||||
// audioScene is released here; the output channel keeps the scene alive.
|
||||
// audioScene released here; the output channel holds the
|
||||
// remaining reference and keeps the scene alive.
|
||||
} else {
|
||||
obs_source_set_audio_mixers(source, 0);
|
||||
obs_source_inc_active(source);
|
||||
@@ -209,15 +167,15 @@ bool MacroActionPlayAudio::PerformAction()
|
||||
return true;
|
||||
}
|
||||
|
||||
// Keep the source alive until playback ends via a background thread.
|
||||
// Keep the source alive in a background thread that cleans up
|
||||
// once playback finishes. Grab an extra strong reference so the
|
||||
// source survives beyond this stack frame.
|
||||
auto rawSource = obs_source_get_ref(source);
|
||||
registerActiveSource(rawSource);
|
||||
auto macro = GetMacro();
|
||||
|
||||
std::thread cleanupThread([rawSource, wantsOutput, macro, maxMs]() {
|
||||
waitForPlaybackToEnd(macro, rawSource, maxMs);
|
||||
deactivatePlayback(rawSource, wantsOutput);
|
||||
unregisterActiveSource(rawSource);
|
||||
obs_source_release(rawSource);
|
||||
});
|
||||
AddMacroHelperThread(macro, std::move(cleanupThread));
|
||||
@@ -246,7 +204,6 @@ bool MacroActionPlayAudio::Save(obs_data_t *obj) const
|
||||
obs_data_set_bool(obj, "useDuration", _useDuration);
|
||||
_playbackDuration.Save(obj, "playbackDuration");
|
||||
obs_data_set_bool(obj, "waitForCompletion", _waitForCompletion);
|
||||
obs_data_set_bool(obj, "mono", _mono);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -264,7 +221,6 @@ bool MacroActionPlayAudio::Load(obs_data_t *obj)
|
||||
_useDuration = obs_data_get_bool(obj, "useDuration");
|
||||
_playbackDuration.Load(obj, "playbackDuration");
|
||||
_waitForCompletion = obs_data_get_bool(obj, "waitForCompletion");
|
||||
_mono = obs_data_get_bool(obj, "mono");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -306,9 +262,7 @@ MacroActionPlayAudioEdit::MacroActionPlayAudioEdit(
|
||||
_useDuration(new QCheckBox(this)),
|
||||
_playbackDuration(new DurationSelection(this, true, 0.0)),
|
||||
_waitForCompletion(new QCheckBox(
|
||||
obs_module_text("AdvSceneSwitcher.action.playAudio.wait"))),
|
||||
_mono(new QCheckBox(
|
||||
obs_module_text("AdvSceneSwitcher.action.playAudio.mono")))
|
||||
obs_module_text("AdvSceneSwitcher.action.playAudio.wait")))
|
||||
{
|
||||
_volumeDB->setMinimum(-100.0);
|
||||
_volumeDB->setMaximum(0.0);
|
||||
@@ -349,8 +303,6 @@ MacroActionPlayAudioEdit::MacroActionPlayAudioEdit(
|
||||
SLOT(PlaybackDurationChanged(const Duration &)));
|
||||
QWidget::connect(_waitForCompletion, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(WaitChanged(int)));
|
||||
QWidget::connect(_mono, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(MonoChanged(int)));
|
||||
|
||||
auto tracksLayout = new QHBoxLayout;
|
||||
tracksLayout->setContentsMargins(0, 0, 0, 0);
|
||||
@@ -394,7 +346,6 @@ MacroActionPlayAudioEdit::MacroActionPlayAudioEdit(
|
||||
mainLayout->addLayout(startOffsetLayout);
|
||||
mainLayout->addLayout(playbackDurationLayout);
|
||||
mainLayout->addWidget(_waitForCompletion);
|
||||
mainLayout->addWidget(_mono);
|
||||
setLayout(mainLayout);
|
||||
|
||||
_entryData = entryData;
|
||||
@@ -424,7 +375,6 @@ void MacroActionPlayAudioEdit::UpdateEntryData()
|
||||
_playbackDuration->SetDuration(_entryData->_playbackDuration);
|
||||
_playbackDuration->setEnabled(_entryData->_useDuration);
|
||||
_waitForCompletion->setChecked(_entryData->_waitForCompletion);
|
||||
_mono->setChecked(_entryData->_mono);
|
||||
}
|
||||
|
||||
void MacroActionPlayAudioEdit::FilePathChanged(const QString &path)
|
||||
@@ -493,10 +443,4 @@ void MacroActionPlayAudioEdit::WaitChanged(int value)
|
||||
_entryData->_waitForCompletion = value;
|
||||
}
|
||||
|
||||
void MacroActionPlayAudioEdit::MonoChanged(int value)
|
||||
{
|
||||
GUARD_LOADING_AND_LOCK();
|
||||
_entryData->_mono = value;
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
|
||||
@@ -37,7 +37,6 @@ public:
|
||||
bool _useDuration = false;
|
||||
Duration _playbackDuration;
|
||||
bool _waitForCompletion = false;
|
||||
bool _mono = false;
|
||||
|
||||
static bool _registered;
|
||||
static const std::string id;
|
||||
@@ -69,7 +68,6 @@ private slots:
|
||||
void UseDurationChanged(int);
|
||||
void PlaybackDurationChanged(const Duration &);
|
||||
void WaitChanged(int value);
|
||||
void MonoChanged(int value);
|
||||
|
||||
signals:
|
||||
void HeaderInfoChanged(const QString &);
|
||||
@@ -85,7 +83,6 @@ private:
|
||||
QCheckBox *_useDuration;
|
||||
DurationSelection *_playbackDuration;
|
||||
QCheckBox *_waitForCompletion;
|
||||
QCheckBox *_mono;
|
||||
|
||||
std::shared_ptr<MacroActionPlayAudio> _entryData;
|
||||
bool _loading = true;
|
||||
|
||||
17
plugins/now-playing/CMakeLists.txt
Normal file
17
plugins/now-playing/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(advanced-scene-switcher-now-playing)
|
||||
|
||||
if(NOT WIN32)
|
||||
message(WARNING "Now playing condition is only supported on Windows.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
add_library(${PROJECT_NAME} MODULE)
|
||||
target_sources(${PROJECT_NAME} PRIVATE macro-condition-now-playing.cpp
|
||||
macro-condition-now-playing.hpp)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE windowsapp)
|
||||
|
||||
setup_advss_plugin(${PROJECT_NAME})
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
|
||||
install_advss_plugin(${PROJECT_NAME})
|
||||
338
plugins/now-playing/macro-condition-now-playing.cpp
Normal file
338
plugins/now-playing/macro-condition-now-playing.cpp
Normal file
@@ -0,0 +1,338 @@
|
||||
#include "macro-condition-now-playing.hpp"
|
||||
#include "layout-helpers.hpp"
|
||||
#include "sync-helpers.hpp"
|
||||
|
||||
#include <util/base.h>
|
||||
|
||||
#include <roapi.h>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Media.Control.h>
|
||||
|
||||
namespace advss {
|
||||
|
||||
using namespace winrt::Windows::Media::Control;
|
||||
|
||||
const std::string MacroConditionNowPlaying::id = "now_playing";
|
||||
|
||||
bool MacroConditionNowPlaying::_registered = MacroConditionFactory::Register(
|
||||
MacroConditionNowPlaying::id,
|
||||
{MacroConditionNowPlaying::Create, MacroConditionNowPlayingEdit::Create,
|
||||
"AdvSceneSwitcher.condition.nowPlaying"});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct SessionInfo {
|
||||
bool hasSession = false;
|
||||
std::string title;
|
||||
std::string artist;
|
||||
std::string album;
|
||||
std::string appName;
|
||||
GlobalSystemMediaTransportControlsSessionPlaybackStatus playbackStatus =
|
||||
GlobalSystemMediaTransportControlsSessionPlaybackStatus::Closed;
|
||||
};
|
||||
|
||||
static bool initWinRT()
|
||||
{
|
||||
HRESULT hr = RoInitialize(RO_INIT_MULTITHREADED);
|
||||
// S_OK: initialized, S_FALSE: already initialized with same type,
|
||||
// RPC_E_CHANGED_MODE: thread is STA (OBS UI thread) - WinRT still usable
|
||||
return SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE;
|
||||
}
|
||||
|
||||
static SessionInfo getCurrentSessionInfo()
|
||||
{
|
||||
thread_local bool initialized = initWinRT();
|
||||
(void)initialized;
|
||||
|
||||
SessionInfo info;
|
||||
|
||||
try {
|
||||
auto manager =
|
||||
GlobalSystemMediaTransportControlsSessionManager::
|
||||
RequestAsync()
|
||||
.get();
|
||||
auto session = manager.GetCurrentSession();
|
||||
if (!session) {
|
||||
return info;
|
||||
}
|
||||
|
||||
info.hasSession = true;
|
||||
info.appName = winrt::to_string(session.SourceAppUserModelId());
|
||||
|
||||
auto playbackInfo = session.GetPlaybackInfo();
|
||||
info.playbackStatus = playbackInfo.PlaybackStatus();
|
||||
|
||||
auto props = session.TryGetMediaPropertiesAsync().get();
|
||||
if (props) {
|
||||
info.title = winrt::to_string(props.Title());
|
||||
info.artist = winrt::to_string(props.Artist());
|
||||
info.album = winrt::to_string(props.AlbumTitle());
|
||||
}
|
||||
} catch (const winrt::hresult_error &e) {
|
||||
blog(LOG_WARNING, "now playing: WinRT error 0x%08X",
|
||||
static_cast<uint32_t>(e.code()));
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
static std::string PlaybackStatusToString(
|
||||
GlobalSystemMediaTransportControlsSessionPlaybackStatus status)
|
||||
{
|
||||
switch (status) {
|
||||
case GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing:
|
||||
return "Playing";
|
||||
case GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused:
|
||||
return "Paused";
|
||||
case GlobalSystemMediaTransportControlsSessionPlaybackStatus::Stopped:
|
||||
return "Stopped";
|
||||
case GlobalSystemMediaTransportControlsSessionPlaybackStatus::Opened:
|
||||
return "Opening";
|
||||
case GlobalSystemMediaTransportControlsSessionPlaybackStatus::Changing:
|
||||
return "Changing";
|
||||
default:
|
||||
return "Closed";
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool MacroConditionNowPlaying::CheckCondition()
|
||||
{
|
||||
const auto info = getCurrentSessionInfo();
|
||||
|
||||
SetTempVarValue("title", info.title);
|
||||
SetTempVarValue("artist", info.artist);
|
||||
SetTempVarValue("album", info.album);
|
||||
SetTempVarValue("appName", info.appName);
|
||||
SetTempVarValue("playbackStatus",
|
||||
PlaybackStatusToString(info.playbackStatus));
|
||||
|
||||
if (!info.hasSession) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto textMatches = [this](const std::string &field) -> bool {
|
||||
const QString pattern = QString::fromStdString(_matchText);
|
||||
const QString value = QString::fromStdString(field);
|
||||
if (_regex.Enabled()) {
|
||||
return _regex.Matches(value, pattern);
|
||||
}
|
||||
return value == pattern;
|
||||
};
|
||||
|
||||
switch (_checkType) {
|
||||
case CheckType::PLAYBACK_STATE: {
|
||||
using S =
|
||||
GlobalSystemMediaTransportControlsSessionPlaybackStatus;
|
||||
switch (_playbackState) {
|
||||
case PlaybackState::PLAYING:
|
||||
return info.playbackStatus == S::Playing;
|
||||
case PlaybackState::PAUSED:
|
||||
return info.playbackStatus == S::Paused;
|
||||
case PlaybackState::STOPPED:
|
||||
return info.playbackStatus == S::Stopped;
|
||||
case PlaybackState::OPENING:
|
||||
return info.playbackStatus == S::Opened;
|
||||
case PlaybackState::CHANGING:
|
||||
return info.playbackStatus == S::Changing;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CheckType::TITLE:
|
||||
return textMatches(info.title);
|
||||
case CheckType::ARTIST:
|
||||
return textMatches(info.artist);
|
||||
case CheckType::ALBUM:
|
||||
return textMatches(info.album);
|
||||
case CheckType::APP_NAME:
|
||||
return textMatches(info.appName);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MacroConditionNowPlaying::Save(obs_data_t *obj) const
|
||||
{
|
||||
MacroCondition::Save(obj);
|
||||
obs_data_set_int(obj, "checkType", static_cast<int>(_checkType));
|
||||
obs_data_set_int(obj, "playbackState",
|
||||
static_cast<int>(_playbackState));
|
||||
_matchText.Save(obj, "matchText");
|
||||
_regex.Save(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MacroConditionNowPlaying::Load(obs_data_t *obj)
|
||||
{
|
||||
MacroCondition::Load(obj);
|
||||
_checkType = static_cast<CheckType>(obs_data_get_int(obj, "checkType"));
|
||||
_playbackState = static_cast<PlaybackState>(
|
||||
obs_data_get_int(obj, "playbackState"));
|
||||
_matchText.Load(obj, "matchText");
|
||||
_regex.Load(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
void MacroConditionNowPlaying::SetupTempVars()
|
||||
{
|
||||
AddTempvar(
|
||||
"title",
|
||||
obs_module_text("AdvSceneSwitcher.tempVar.nowPlaying.title"));
|
||||
AddTempvar(
|
||||
"artist",
|
||||
obs_module_text("AdvSceneSwitcher.tempVar.nowPlaying.artist"));
|
||||
AddTempvar(
|
||||
"album",
|
||||
obs_module_text("AdvSceneSwitcher.tempVar.nowPlaying.album"));
|
||||
AddTempvar(
|
||||
"appName",
|
||||
obs_module_text("AdvSceneSwitcher.tempVar.nowPlaying.appName"));
|
||||
AddTempvar(
|
||||
"playbackStatus",
|
||||
obs_module_text(
|
||||
"AdvSceneSwitcher.tempVar.nowPlaying.playbackStatus"));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static void populateCheckTypeSelection(QComboBox *list)
|
||||
{
|
||||
static const std::map<MacroConditionNowPlaying::CheckType, std::string> checkTypes = {
|
||||
{MacroConditionNowPlaying::CheckType::PLAYBACK_STATE,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.checkType.playbackState"},
|
||||
{MacroConditionNowPlaying::CheckType::TITLE,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.checkType.title"},
|
||||
{MacroConditionNowPlaying::CheckType::ARTIST,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.checkType.artist"},
|
||||
{MacroConditionNowPlaying::CheckType::ALBUM,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.checkType.album"},
|
||||
{MacroConditionNowPlaying::CheckType::APP_NAME,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.checkType.appName"},
|
||||
};
|
||||
|
||||
for (const auto &[type, name] : checkTypes) {
|
||||
list->addItem(obs_module_text(name.c_str()),
|
||||
static_cast<int>(type));
|
||||
}
|
||||
}
|
||||
|
||||
static void populatePlaybackStateSelection(QComboBox *list)
|
||||
{
|
||||
static const std::map<MacroConditionNowPlaying::PlaybackState, std::string> playbackStates = {
|
||||
{MacroConditionNowPlaying::PlaybackState::PLAYING,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.playbackState.playing"},
|
||||
{MacroConditionNowPlaying::PlaybackState::PAUSED,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.playbackState.paused"},
|
||||
{MacroConditionNowPlaying::PlaybackState::STOPPED,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.playbackState.stopped"},
|
||||
{MacroConditionNowPlaying::PlaybackState::OPENING,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.playbackState.opening"},
|
||||
{MacroConditionNowPlaying::PlaybackState::CHANGING,
|
||||
"AdvSceneSwitcher.condition.nowPlaying.playbackState.changing"},
|
||||
};
|
||||
|
||||
for (const auto &[state, name] : playbackStates) {
|
||||
list->addItem(obs_module_text(name.c_str()),
|
||||
static_cast<int>(state));
|
||||
}
|
||||
}
|
||||
|
||||
MacroConditionNowPlayingEdit::MacroConditionNowPlayingEdit(
|
||||
QWidget *parent, std::shared_ptr<MacroConditionNowPlaying> entryData)
|
||||
: QWidget(parent),
|
||||
_checkType(new QComboBox(this)),
|
||||
_playbackState(new QComboBox(this)),
|
||||
_matchText(new VariableLineEdit(this)),
|
||||
_regex(new RegexConfigWidget(this))
|
||||
{
|
||||
populateCheckTypeSelection(_checkType);
|
||||
populatePlaybackStateSelection(_playbackState);
|
||||
|
||||
connect(_checkType, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &MacroConditionNowPlayingEdit::CheckTypeChanged);
|
||||
connect(_playbackState,
|
||||
QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
||||
&MacroConditionNowPlayingEdit::PlaybackStateChanged);
|
||||
connect(_matchText, &VariableLineEdit::textEdited, this,
|
||||
&MacroConditionNowPlayingEdit::MatchTextChanged);
|
||||
connect(_regex, &RegexConfigWidget::RegexConfigChanged, this,
|
||||
&MacroConditionNowPlayingEdit::RegexChanged);
|
||||
|
||||
_layout = new QHBoxLayout();
|
||||
_layout->addWidget(_checkType);
|
||||
_layout->addWidget(_playbackState);
|
||||
_layout->addWidget(_matchText);
|
||||
_layout->addWidget(_regex);
|
||||
setLayout(_layout);
|
||||
|
||||
_entryData = entryData;
|
||||
UpdateEntryData();
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::UpdateEntryData()
|
||||
{
|
||||
if (!_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
_checkType->setCurrentIndex(
|
||||
_checkType->findData(static_cast<int>(_entryData->_checkType)));
|
||||
_playbackState->setCurrentIndex(_playbackState->findData(
|
||||
static_cast<int>(_entryData->_playbackState)));
|
||||
_matchText->setText(QString::fromStdString(_entryData->_matchText));
|
||||
_regex->SetRegexConfig(_entryData->_regex);
|
||||
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::CheckTypeChanged(int)
|
||||
{
|
||||
GUARD_LOADING_AND_LOCK();
|
||||
_entryData->_checkType =
|
||||
static_cast<MacroConditionNowPlaying::CheckType>(
|
||||
_checkType->currentData().toInt());
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::PlaybackStateChanged(int)
|
||||
{
|
||||
GUARD_LOADING_AND_LOCK();
|
||||
_entryData->_playbackState =
|
||||
static_cast<MacroConditionNowPlaying::PlaybackState>(
|
||||
_playbackState->currentData().toInt());
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::MatchTextChanged(const QString &text)
|
||||
{
|
||||
GUARD_LOADING_AND_LOCK();
|
||||
_entryData->_matchText = text.toStdString();
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::RegexChanged(const RegexConfig ®ex)
|
||||
{
|
||||
GUARD_LOADING_AND_LOCK();
|
||||
_entryData->_regex = regex;
|
||||
}
|
||||
|
||||
void MacroConditionNowPlayingEdit::SetWidgetVisibility()
|
||||
{
|
||||
const bool isStateCheck =
|
||||
_entryData->_checkType ==
|
||||
MacroConditionNowPlaying::CheckType::PLAYBACK_STATE;
|
||||
_playbackState->setVisible(isStateCheck);
|
||||
_matchText->setVisible(!isStateCheck);
|
||||
_regex->setVisible(!isStateCheck);
|
||||
|
||||
if (isStateCheck) {
|
||||
AddStretchIfNecessary(_layout);
|
||||
} else {
|
||||
RemoveStretchIfPresent(_layout);
|
||||
}
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
86
plugins/now-playing/macro-condition-now-playing.hpp
Normal file
86
plugins/now-playing/macro-condition-now-playing.hpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
#include "macro-condition-edit.hpp"
|
||||
#include "regex-config.hpp"
|
||||
#include "variable-line-edit.hpp"
|
||||
|
||||
#include <QComboBox>
|
||||
|
||||
namespace advss {
|
||||
|
||||
class MacroConditionNowPlaying : public MacroCondition {
|
||||
public:
|
||||
enum class CheckType {
|
||||
PLAYBACK_STATE,
|
||||
TITLE,
|
||||
ARTIST,
|
||||
ALBUM,
|
||||
APP_NAME,
|
||||
};
|
||||
|
||||
enum class PlaybackState {
|
||||
PLAYING,
|
||||
PAUSED,
|
||||
STOPPED,
|
||||
OPENING,
|
||||
CHANGING,
|
||||
};
|
||||
|
||||
MacroConditionNowPlaying(Macro *m) : MacroCondition(m) {}
|
||||
bool CheckCondition();
|
||||
bool Save(obs_data_t *obj) const;
|
||||
bool Load(obs_data_t *obj);
|
||||
std::string GetId() const { return id; }
|
||||
static std::shared_ptr<MacroCondition> Create(Macro *m)
|
||||
{
|
||||
return std::make_shared<MacroConditionNowPlaying>(m);
|
||||
}
|
||||
|
||||
CheckType _checkType = CheckType::PLAYBACK_STATE;
|
||||
PlaybackState _playbackState = PlaybackState::PLAYING;
|
||||
StringVariable _matchText;
|
||||
RegexConfig _regex;
|
||||
|
||||
private:
|
||||
void SetupTempVars();
|
||||
|
||||
static bool _registered;
|
||||
static const std::string id;
|
||||
};
|
||||
|
||||
class MacroConditionNowPlayingEdit : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MacroConditionNowPlayingEdit(
|
||||
QWidget *parent,
|
||||
std::shared_ptr<MacroConditionNowPlaying> cond = nullptr);
|
||||
void UpdateEntryData();
|
||||
static QWidget *Create(QWidget *parent,
|
||||
std::shared_ptr<MacroCondition> cond)
|
||||
{
|
||||
return new MacroConditionNowPlayingEdit(
|
||||
parent,
|
||||
std::dynamic_pointer_cast<MacroConditionNowPlaying>(
|
||||
cond));
|
||||
}
|
||||
|
||||
private slots:
|
||||
void CheckTypeChanged(int);
|
||||
void PlaybackStateChanged(int);
|
||||
void MatchTextChanged(const QString &);
|
||||
void RegexChanged(const RegexConfig &);
|
||||
|
||||
private:
|
||||
void SetWidgetVisibility();
|
||||
|
||||
QHBoxLayout *_layout;
|
||||
QComboBox *_checkType;
|
||||
QComboBox *_playbackState;
|
||||
VariableLineEdit *_matchText;
|
||||
RegexConfigWidget *_regex;
|
||||
|
||||
std::shared_ptr<MacroConditionNowPlaying> _entryData;
|
||||
bool _loading = true;
|
||||
};
|
||||
|
||||
} // namespace advss
|
||||
Reference in New Issue
Block a user