From 62f722318d82b7c88c33be6719380df845a9af14 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 8 Dec 2024 13:46:21 -0800 Subject: [PATCH] Add StreamHistory quality controls. --- SerialPrograms/CMakeLists.txt | 4 ++ SerialPrograms/SerialPrograms.pro | 4 ++ .../CommonFramework/GlobalSettingsPanel.cpp | 47 +--------------- .../CommonFramework/GlobalSettingsPanel.h | 22 +------- .../Options/ResolutionOption.cpp | 28 ++++++++++ .../Options/ResolutionOption.h | 31 +++++++++++ .../Recording/StreamHistoryOption.cpp | 55 +++++++++++++++++++ .../Recording/StreamHistoryOption.h | 29 ++++++++++ .../Recording/StreamRecorder.cpp | 10 +++- 9 files changed, 163 insertions(+), 67 deletions(-) create mode 100644 SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp create mode 100644 SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h create mode 100644 SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.cpp create mode 100644 SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.h diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index e74324364..e090a57a8 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -471,6 +471,8 @@ file(GLOB MAIN_SOURCES Source/CommonFramework/Options/LabelCellOption.h Source/CommonFramework/Options/LanguageOCROption.cpp Source/CommonFramework/Options/LanguageOCROption.h + Source/CommonFramework/Options/ResolutionOption.cpp + Source/CommonFramework/Options/ResolutionOption.h Source/CommonFramework/Options/ScreenWatchOption.cpp Source/CommonFramework/Options/ScreenWatchOption.h Source/CommonFramework/Options/ScreenshotFormatOption.h @@ -509,6 +511,8 @@ file(GLOB MAIN_SOURCES Source/CommonFramework/PersistentSettings.h Source/CommonFramework/ProgramSession.cpp Source/CommonFramework/ProgramSession.h + Source/CommonFramework/Recording/StreamHistoryOption.cpp + Source/CommonFramework/Recording/StreamHistoryOption.h Source/CommonFramework/Recording/StreamHistorySession.cpp Source/CommonFramework/Recording/StreamHistorySession.h Source/CommonFramework/Recording/StreamHistoryTracker_Null.h diff --git a/SerialPrograms/SerialPrograms.pro b/SerialPrograms/SerialPrograms.pro index 10a05008a..ed70d4820 100644 --- a/SerialPrograms/SerialPrograms.pro +++ b/SerialPrograms/SerialPrograms.pro @@ -254,6 +254,7 @@ SOURCES += \ Source/CommonFramework/Options/Environment/ThemeSelectorOption.cpp \ Source/CommonFramework/Options/LabelCellOption.cpp \ Source/CommonFramework/Options/LanguageOCROption.cpp \ + Source/CommonFramework/Options/ResolutionOption.cpp \ Source/CommonFramework/Options/ScreenWatchOption.cpp \ Source/CommonFramework/Options/StringSelectOption.cpp \ Source/CommonFramework/Options/UI/LabelCellWidget.cpp \ @@ -271,6 +272,7 @@ SOURCES += \ Source/CommonFramework/Panels/UI/SettingsPanelWidget.cpp \ Source/CommonFramework/PersistentSettings.cpp \ Source/CommonFramework/ProgramSession.cpp \ + Source/CommonFramework/Recording/StreamHistoryOption.cpp \ Source/CommonFramework/Recording/StreamHistorySession.cpp \ Source/CommonFramework/Recording/StreamRecorder.cpp \ Source/CommonFramework/Resources/SpriteDatabase.cpp \ @@ -1344,6 +1346,7 @@ HEADERS += \ Source/CommonFramework/Options/Environment/SleepSuppressOption.h \ Source/CommonFramework/Options/LabelCellOption.h \ Source/CommonFramework/Options/LanguageOCROption.h \ + Source/CommonFramework/Options/ResolutionOption.h \ Source/CommonFramework/Options/ScreenWatchOption.h \ Source/CommonFramework/Options/ScreenshotFormatOption.h \ Source/CommonFramework/Options/StringSelectOption.h \ @@ -1363,6 +1366,7 @@ HEADERS += \ Source/CommonFramework/Panels/UI/SettingsPanelWidget.h \ Source/CommonFramework/PersistentSettings.h \ Source/CommonFramework/ProgramSession.h \ + Source/CommonFramework/Recording/StreamHistoryOption.h \ Source/CommonFramework/Recording/StreamHistorySession.h \ Source/CommonFramework/Recording/StreamHistoryTracker_RecordOnTheFly.h \ Source/CommonFramework/Recording/StreamHistoryTracker_SaveFrames.h \ diff --git a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp index 4d193393d..9e85784d3 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp +++ b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp @@ -13,7 +13,6 @@ #include "Common/Cpp/Json/JsonObject.h" #include "CommonFramework/Globals.h" //#include "CommonFramework/Environment/Environment.h" -#include "CommonFramework/Windows/DpiScaler.h" #include "GlobalSettingsPanel.h" // #include @@ -37,50 +36,6 @@ const std::set TOKENS{ -ResolutionOption::ResolutionOption( - std::string label, std::string description, - int default_width, int default_height -) - : GroupOption(std::move(label), LockMode::LOCK_WHILE_RUNNING) - , DESCRIPTION(std::move(description)) - , WIDTH("Width:", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(default_width)) - , HEIGHT("Height:", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(default_height)) -{ - PA_ADD_STATIC(DESCRIPTION); - PA_ADD_OPTION(WIDTH); - PA_ADD_OPTION(HEIGHT); -} - -StreamHistoryOption::StreamHistoryOption() - : GroupOption( - "Stream History", - LockMode::LOCK_WHILE_RUNNING, - true, - IS_BETA_VERSION - ) - , DESCRIPTION( - "Keep a record of the recent video+audio streams. This will allow video capture " - "for unexpected events.

" - "Warning: This feature is computationally expensive and " - "will require a more powerful computer to run (especially for multi-Switch programs).
" - "Furthermore, the current implementation is inefficient as it will write a lot " - "of data to disk. This feature is still a work-in-progress." - "
" - ) - , HISTORY_SECONDS( - "History (seconds):
" - "Keep this many seconds of video and audio feed for video capture and debugging purposes.

" - "Do not set this too large as it will consume a lot of memory and may exceed the " - "attachment size limit for Discord notifications." - "", - LockMode::UNLOCK_WHILE_RUNNING, - 30 - ) -{ - PA_ADD_STATIC(DESCRIPTION); - PA_ADD_OPTION(HISTORY_SECONDS); -} - PreloadSettings::PreloadSettings(){} @@ -267,8 +222,8 @@ GlobalSettings::GlobalSettings() PA_ADD_OPTION(CHECK_FOR_UPDATES); PA_ADD_OPTION(STATS_FILE); PA_ADD_OPTION(ALL_STATS); - PA_ADD_OPTION(WINDOW_SIZE); PA_ADD_OPTION(THEME); + PA_ADD_OPTION(WINDOW_SIZE); #if (QT_VERSION_MAJOR == 6) && (QT_VERSION_MINOR >= 8) PA_ADD_OPTION(STREAM_HISTORY); #else diff --git a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h index 1c1aaf287..fc253b1a6 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h +++ b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h @@ -13,12 +13,14 @@ #include "Common/Cpp/Options/SimpleIntegerOption.h" #include "Common/Cpp/Options/FloatingPointOption.h" #include "Common/Cpp/Options/StringOption.h" +#include "CommonFramework/Options/ResolutionOption.h" #include "CommonFramework/Options/Environment/ProcessPriorityOption.h" #include "CommonFramework/Options/Environment/ProcessorLevelOption.h" #include "CommonFramework/Options/Environment/ThemeSelectorOption.h" #include "CommonFramework/Options/Environment/SleepSuppressOption.h" #include "CommonFramework/ErrorReports/ErrorReports.h" #include "CommonFramework/VideoPipeline/Backends/CameraImplementations.h" +#include "CommonFramework/Recording/StreamHistoryOption.h" #include "CommonFramework/Panels/SettingsPanel.h" #include "CommonFramework/Panels/PanelTools.h" #include "Integrations/DiscordSettingsOption.h" @@ -32,29 +34,11 @@ namespace PokemonAutomation{ -class ResolutionOption : public GroupOption{ -public: - ResolutionOption( - std::string label, std::string description, - int default_width, int default_height - ); - - StaticTextOption DESCRIPTION; - SimpleIntegerOption WIDTH; - SimpleIntegerOption HEIGHT; -}; struct DebugSettings{ bool COLOR_CHECK = false; bool IMAGE_TEMPLATE_MATCHING = false; bool IMAGE_DICTIONARY_MATCHING = false; }; -class StreamHistoryOption : public GroupOption{ -public: - StreamHistoryOption(); - - StaticTextOption DESCRIPTION; - SimpleIntegerOption HISTORY_SECONDS; -}; @@ -92,8 +76,8 @@ class GlobalSettings : public BatchOption, private ConfigOption::Listener{ StringOption STATS_FILE; BooleanCheckBoxOption ALL_STATS; - ResolutionOption WINDOW_SIZE; ThemeSelectorOption THEME; + ResolutionOption WINDOW_SIZE; StreamHistoryOption STREAM_HISTORY; SleepSuppressOptions SLEEP_SUPPRESS; diff --git a/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp b/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp new file mode 100644 index 000000000..9723edb9e --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp @@ -0,0 +1,28 @@ +/* Resolution Option + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#include "CommonFramework/Windows/DpiScaler.h" +#include "ResolutionOption.h" + +namespace PokemonAutomation{ + + +ResolutionOption::ResolutionOption( + std::string label, std::string description, + int default_width, int default_height +) + : GroupOption(std::move(label), LockMode::LOCK_WHILE_RUNNING) + , DESCRIPTION(std::move(description)) + , WIDTH("Width:", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(default_width)) + , HEIGHT("Height:", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(default_height)) +{ + PA_ADD_STATIC(DESCRIPTION); + PA_ADD_OPTION(WIDTH); + PA_ADD_OPTION(HEIGHT); +} + + +} diff --git a/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h b/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h new file mode 100644 index 000000000..074304402 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h @@ -0,0 +1,31 @@ +/* Resolution Option + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#ifndef PokemonAutomation_Options_ResolutionOption_H +#define PokemonAutomation_Options_ResolutionOption_H + +#include "Common/Cpp/Options/StaticTextOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "Common/Cpp/Options/GroupOption.h" + +namespace PokemonAutomation{ + + +class ResolutionOption : public GroupOption{ +public: + ResolutionOption( + std::string label, std::string description, + int default_width, int default_height + ); + + StaticTextOption DESCRIPTION; + SimpleIntegerOption WIDTH; + SimpleIntegerOption HEIGHT; +}; + + +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.cpp b/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.cpp new file mode 100644 index 000000000..5a7bb0044 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.cpp @@ -0,0 +1,55 @@ +/* Stream History Option + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#include "CommonFramework/Globals.h" +#include "StreamHistoryOption.h" + +namespace PokemonAutomation{ + + +StreamHistoryOption::StreamHistoryOption() + : GroupOption( + "Stream History", + LockMode::LOCK_WHILE_RUNNING, + true, + IS_BETA_VERSION + ) + , DESCRIPTION( + "Keep a record of the recent video+audio streams. This will allow video capture " + "for unexpected events.

" + "Warning: This feature is computationally expensive and " + "will require a more powerful computer to run (especially for multi-Switch programs).
" + "Furthermore, the current implementation is inefficient as it will write a lot " + "of data to disk. This feature is still a work-in-progress." + "
" + ) + , HISTORY_SECONDS( + "History (seconds):
" + "Keep this many seconds of video and audio feed for video capture and debugging purposes.

" + "Do not set this too large as it will consume a lot of memory and may exceed the " + "attachment size limit for Discord notifications." + "", + LockMode::UNLOCK_WHILE_RUNNING, + 30 + ) + , VIDEO_BITRATE( + "Video Bit-Rate (kbps):
" + "Lower = lower quality, smaller file size.
" + "Higher = high quality, larger file size.

" + "Large values can exceed the attachment size limit for Discord notifications." + "", + LockMode::UNLOCK_WHILE_RUNNING, + 5000 + ) +{ + PA_ADD_STATIC(DESCRIPTION); + PA_ADD_OPTION(HISTORY_SECONDS); + PA_ADD_OPTION(VIDEO_BITRATE); +} + + + +} diff --git a/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.h b/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.h new file mode 100644 index 000000000..9c131a55a --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Recording/StreamHistoryOption.h @@ -0,0 +1,29 @@ +/* Stream History Option + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#ifndef PokemonAutomation_StreamHistoryOption_H +#define PokemonAutomation_StreamHistoryOption_H + +#include "Common/Cpp/Options/StaticTextOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "Common/Cpp/Options/GroupOption.h" + +namespace PokemonAutomation{ + + + +class StreamHistoryOption : public GroupOption{ +public: + StreamHistoryOption(); + + StaticTextOption DESCRIPTION; + SimpleIntegerOption HISTORY_SECONDS; + SimpleIntegerOption VIDEO_BITRATE; +}; + + +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp b/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp index 58c073f87..552a1f1e0 100644 --- a/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp +++ b/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp @@ -18,6 +18,7 @@ #include #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Concurrency/SpinPause.h" +#include "CommonFramework/GlobalSettingsPanel.h" #include "CommonFramework/VideoPipeline/Backends/VideoFrameQt.h" #include "StreamRecorder.h" @@ -153,9 +154,13 @@ void StreamRecording::internal_run(){ session.setRecorder(&recorder); recorder.setMediaFormat(QMediaFormat::MPEG4); // recorder.setQuality(QMediaRecorder::NormalQuality); - recorder.setQuality(QMediaRecorder::LowQuality); +// recorder.setQuality(QMediaRecorder::LowQuality); // recorder.setQuality(QMediaRecorder::VeryLowQuality); + recorder.setVideoResolution(1280, 720); + recorder.setVideoBitRate(GlobalSettings::instance().STREAM_HISTORY.VIDEO_BITRATE * 1000); + recorder.setEncodingMode(QMediaRecorder::AverageBitRateEncoding); + #ifdef PA_STREAM_HISTORY_LOCAL_BUFFER recorder.setOutputDevice(&m_write_buffer); #else @@ -165,7 +170,8 @@ void StreamRecording::internal_run(){ ); #endif -// cout << "EncodingMode = " << (int)recorder.encodingMode() << endl; +// cout << "Encoding Mode = " << (int)recorder.encodingMode() << endl; +// cout << "Bit Rate = " << (int)recorder.videoBitRate() << endl; recorder.connect( &audio_input, &QAudioBufferInput::readyToSendAudioBuffer,