Skip to content

Commit

Permalink
Reduce dependency on ConsoleHandle in infra.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Jan 15, 2025
1 parent 7bbd96d commit e77a266
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef PokemonAutomation_OperationFailedException_H
#define PokemonAutomation_OperationFailedException_H

#include "CommonFramework/Tools/ConsoleHandle.h"
#include "CommonFramework/VideoPipeline/VideoStream.h"
#include "ScreenshotException.h"

namespace PokemonAutomation{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "ProgramFinishedException.h"

namespace PokemonAutomation{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ImageRGB32;
class EventNotificationOption;
struct ProgramInfo;
class ProgramEnvironment;
class ConsoleHandle;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "CommonFramework/ErrorReports/ErrorReports.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "CommonFramework/VideoPipeline/VideoStream.h"
#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "ScreenshotException.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace PokemonAutomation{
class ImageViewRGB32;
class ImageRGB32;
class EventNotificationOption;
struct VideoStream;
class VideoStream;
struct ProgramInfo;
class ProgramEnvironment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/AudioPipeline/AudioFeed.h"
#include "CommonFramework/Inference/SpectrogramMatcher.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "AudioPerSpectrumDetectorBase.h"

#include <iostream>
Expand All @@ -21,13 +20,16 @@ namespace PokemonAutomation{


AudioPerSpectrumDetectorBase::AudioPerSpectrumDetectorBase(
std::string label, std::string audio_name, Color detection_color,
ConsoleHandle& console, DetectedCallback detected_callback
Logger& logger,
std::string label,
std::string audio_name,
Color detection_color,
DetectedCallback detected_callback
)
: AudioInferenceCallback(std::move(label))
, m_audio_name(std::move(audio_name))
, m_detection_color(detection_color)
, m_console(console)
, m_logger(logger)
, m_detected_callback(std::move(detected_callback))
, m_start_timestamp(current_time())
, m_spectrums_processed(0)
Expand All @@ -47,14 +49,14 @@ void AudioPerSpectrumDetectorBase::throw_if_no_sound(std::chrono::milliseconds m
if (m_spectrums_processed > 0){
return;
}
throw UserSetupError(m_console, "No sound detected.");
throw UserSetupError(m_logger, "No sound detected.");
}

void AudioPerSpectrumDetectorBase::log_results(){
if (m_last_timestamp != WallClock::min()){
m_console.log(m_audio_name + " detected! Error Coefficient = " + tostr_default(m_lowest_error), COLOR_BLUE);
m_logger.log(m_audio_name + " detected! Error Coefficient = " + tostr_default(m_lowest_error), COLOR_BLUE);
}else{
m_console.log(m_audio_name + " not detected. Error Coefficient = " + tostr_default(m_lowest_error), COLOR_PURPLE);
m_logger.log(m_audio_name + " not detected. Error Coefficient = " + tostr_default(m_lowest_error), COLOR_PURPLE);
}

#if 0
Expand All @@ -66,7 +68,7 @@ void AudioPerSpectrumDetectorBase::log_results(){
for (const auto& error : m_errors){
ss << "[" << error.first << ":" << error.second << "]";
}
// m_console.log(ss.str(), COLOR_RED);
// m_logger.log(ss.str(), COLOR_RED);
cout << ss.str() << endl;
}
#endif
Expand Down Expand Up @@ -95,7 +97,7 @@ bool AudioPerSpectrumDetectorBase::process_spectrums(
const size_t sample_rate = new_spectrums[0].sample_rate;
// Lazy intialization of the spectrogram matcher.
if (m_matcher == nullptr || m_matcher->sample_rate() != sample_rate){
m_console.log("Loading spectrogram...");
m_logger.log("Loading spectrogram...");
m_matcher = build_spectrogram_matcher(sample_rate);
}

Expand Down Expand Up @@ -156,7 +158,7 @@ bool AudioPerSpectrumDetectorBase::process_spectrums(

std::ostringstream os;
os << m_audio_name << " found, score " << matcher_score << "/" << threshold << ", scale: " << m_matcher->lastMatchedScale();
m_console.log(os.str(), COLOR_BLUE);
m_logger.log(os.str(), COLOR_BLUE);
audio_feed.add_overlay(curStamp+1-m_matcher->numMatchedWindows(), curStamp+1, m_detection_color);

// Since the target audio is found, no need to check detection on the rest of the spectrums in `new_spectrums`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace PokemonAutomation{

class ConsoleHandle;
class Logger;
class SpectrogramMatcher;

// A virtual base class for audio detectors to match an audio template starting at each incoming
Expand All @@ -56,8 +56,11 @@ class AudioPerSpectrumDetectorBase : public AudioInferenceCallback{
// function. If it returns true, the inference session will stop (by returning true from
// AudioPerSpectrumDetectorBase::process_spectrums()).
AudioPerSpectrumDetectorBase(
std::string label, std::string audio_name, Color detection_color,
ConsoleHandle& console, DetectedCallback detected_callback
Logger& logger,
std::string label,
std::string audio_name,
Color detection_color,
DetectedCallback detected_callback
);

virtual ~AudioPerSpectrumDetectorBase();
Expand Down Expand Up @@ -93,7 +96,7 @@ class AudioPerSpectrumDetectorBase : public AudioInferenceCallback{
// Color of the box to visualize the detection in the audio spectrogram UI.
Color m_detection_color;

ConsoleHandle& m_console;
Logger& m_logger;
// Callback function to determine whether to stop the inference session when the target audio
// is detected.
DetectedCallback m_detected_callback;
Expand Down
12 changes: 6 additions & 6 deletions SerialPrograms/Source/CommonFramework/Tools/BlackBorderCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
//#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
Expand All @@ -15,26 +15,26 @@
namespace PokemonAutomation{


void start_program_video_check(ConsoleHandle& console, FeedbackType feedback){
void start_program_video_check(VideoStream& stream, FeedbackType feedback){
if (feedback == FeedbackType::NONE){
return;
}

VideoSnapshot screen = console.video().snapshot();
VideoSnapshot screen = stream.video.snapshot();

if (!screen){
if (feedback == FeedbackType::REQUIRED || feedback == FeedbackType::VIDEO_AUDIO){
throw UserSetupError(console, "This program requires video feedback. Please make sure the video is working.");
throw UserSetupError(stream.logger, "This program requires video feedback. Please make sure the video is working.");
}
return;
}

BlackBorderDetector detector;
VideoOverlaySet set(console);
VideoOverlaySet set(stream.overlay);
detector.make_overlays(set);

if (detector.detect(screen)){
throw UserSetupError(console, "Black border detected! Please set your screen size to 100% in the TV Settings on your Nintendo Switch.");
throw UserSetupError(stream.logger, "Black border detected! Please set your screen size to 100% in the TV Settings on your Nintendo Switch.");
}
}
void start_program_video_check(FixedLimitVector<ConsoleHandle>& consoles, FeedbackType feedback){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

namespace PokemonAutomation{

class VideoStream;
class ConsoleHandle;


void start_program_video_check(ConsoleHandle& console, FeedbackType feedback);
void start_program_video_check(VideoStream& stream, FeedbackType feedback);
void start_program_video_check(FixedLimitVector<ConsoleHandle>& consoles, FeedbackType feedback);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ConsoleHandle::ConsoleHandle(
AudioFeed& audio,
const StreamHistorySession& history
)
: VideoStream(logger, audio, video, history)
: VideoStream({logger, audio, video, history, overlay})
, m_index(index)
// , m_logger(logger)
, m_botbase(botbase)
Expand Down
5 changes: 2 additions & 3 deletions SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "CommonFramework/ErrorReports/ErrorReports.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
//#include "CommonFramework/VideoPipeline/VideoOverlay.h"
#include "ConsoleHandle.h"
#include "ErrorDumper.h"
//#include "ProgramEnvironment.h"
namespace PokemonAutomation{
Expand Down Expand Up @@ -68,15 +67,15 @@ void dump_image(

void dump_image_and_throw_recoverable_exception(
const ProgramInfo& program_info,
ConsoleHandle& console,
VideoStream& stream,
const std::string& error_name,
const std::string& error_message,
const ImageViewRGB32& screenshot
){
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
error_message,
console
stream
);
}

Expand Down
21 changes: 2 additions & 19 deletions SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace PokemonAutomation{

class VideoFeed;
class StreamHistorySession;
class ConsoleHandle;
class VideoStream;
class EventNotificationOption;
class ImageViewRGB32;
class Logger;
Expand Down Expand Up @@ -44,30 +44,13 @@ void dump_image(
const std::string& label
);

#if 0
// dump a screenshot to ./ErrorDumps/ folder and throw an OperationFailedException.
// Also send image as telemetry if user allows.
// notification_error: the notification option used to set whether user wants to receive notifiction for
// recoverable error.
// error_name: the error name, used as the image name and show up on video overlay log. Typical format example:
// "NoHatchEnd", "NoYCommFound".
// error_message: the exception mesage.
[[noreturn]] void dump_image_and_throw_recoverable_exception(
ProgramEnvironment& env,
ConsoleHandle& console,
EventNotificationOption& notification_error,
const std::string& error_name,
const std::string& error_message
);
#endif

// dump a screenshot to ./ErrorDumps/ folder and throw an OperationFailedException.
// error_name: the error name, used as the image name and show up on video overlay log. Typical format example:
// "NoHatchEnd", "NoYCommFound".
// error_message: the exception mesage.
[[noreturn]] void dump_image_and_throw_recoverable_exception(
const ProgramInfo& program_info,
ConsoleHandle& console,
VideoStream& stream,
const std::string& error_name,
const std::string& error_message,
const ImageViewRGB32& screenshot = ImageViewRGB32()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
*/

#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "MultiConsoleErrors.h"

namespace PokemonAutomation{



void MultiConsoleErrorState::report_unrecoverable_error(ConsoleHandle& console, std::string msg){
console.log(msg, COLOR_RED);
void MultiConsoleErrorState::report_unrecoverable_error(VideoStream& stream, std::string msg){
stream.logger.log(msg, COLOR_RED);
bool expected = false;
if (m_unrecoverable_error.compare_exchange_strong(expected, true)){
m_message = msg;
}
OperationFailedException::fire(ErrorReport::NO_ERROR_REPORT, std::move(msg), console);
OperationFailedException::fire(ErrorReport::NO_ERROR_REPORT, std::move(msg), stream);
}
void MultiConsoleErrorState::check_unrecoverable_error(Logger& logger){
if (m_unrecoverable_error.load(std::memory_order_acquire)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
namespace PokemonAutomation{

class Logger;
class ConsoleHandle;
class VideoStream;


class MultiConsoleErrorState{
public:
void report_unrecoverable_error(ConsoleHandle& console, std::string msg);
void report_unrecoverable_error(VideoStream& stream, std::string msg);
void check_unrecoverable_error(Logger& logger);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "VideoResolutionCheck.h"

namespace PokemonAutomation{
Expand All @@ -25,8 +24,8 @@ void assert_16_9_720p_min(Logger& logger, const ImageViewRGB32& frame){
throw UserSetupError(logger, "Video aspect ratio must be 16:9.");
}
}
void assert_16_9_720p_min(Logger& logger, ConsoleHandle& console){
assert_16_9_720p_min(logger, console.video().snapshot());
void assert_16_9_720p_min(Logger& logger, VideoFeed& video){
assert_16_9_720p_min(logger, video.snapshot());
}

void assert_16_9_1080p_min(Logger& logger, const ImageViewRGB32& frame){
Expand All @@ -41,8 +40,8 @@ void assert_16_9_1080p_min(Logger& logger, const ImageViewRGB32& frame){
throw UserSetupError(logger, "Video aspect ratio must be 16:9.");
}
}
void assert_16_9_1080p_min(Logger& logger, ConsoleHandle& console){
assert_16_9_1080p_min(logger, console.video().snapshot());
void assert_16_9_1080p_min(Logger& logger, VideoFeed& video){
assert_16_9_1080p_min(logger, video.snapshot());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace PokemonAutomation{

class Logger;
class ImageViewRGB32;
class ConsoleHandle;
class VideoFeed;


void assert_16_9_720p_min(Logger& logger, const ImageViewRGB32& frame);
void assert_16_9_720p_min(Logger& logger, ConsoleHandle& console);
void assert_16_9_720p_min(Logger& logger, VideoFeed& video);

void assert_16_9_1080p_min(Logger& logger, const ImageViewRGB32& frame);
void assert_16_9_1080p_min(Logger& logger, ConsoleHandle& console);
void assert_16_9_1080p_min(Logger& logger, VideoFeed& video);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ class Logger;
class AudioFeed;
class VideoFeed;
class StreamHistorySession;
class VideoOverlay;


struct VideoStream{
class VideoStream{
public:
Logger& logger;
AudioFeed& audio;
VideoFeed& video;
const StreamHistorySession& video_history;
VideoOverlay& overlay;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ namespace PokemonBDSP{

ShinySoundDetector::ShinySoundDetector(ConsoleHandle& console, DetectedCallback detected_callback)
// Use a yellow as the detection color because the shiny animation is yellow.
: AudioPerSpectrumDetectorBase("ShinySoundDetector", "Shiny sound", COLOR_YELLOW, console, detected_callback)
: AudioPerSpectrumDetectorBase(
console,
"ShinySoundDetector",
"Shiny sound",
COLOR_YELLOW,
detected_callback
)
{}


Expand Down
Loading

0 comments on commit e77a266

Please sign in to comment.