MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Engine.cpp
Go to the documentation of this file.
1#include "Engine.hpp"
2
9
11
12#ifdef MAYAFLUX_PLATFORM_MACOS
13#include <CoreFoundation/CoreFoundation.h>
14#include <unistd.h>
15#endif
16
17namespace MayaFlux::Core {
18
19//-------------------------------------------------------------------------
20// Initialization and Lifecycle
21//-------------------------------------------------------------------------
22
24 : m_rng(new Nodes::Generator::Stochastics::Random())
25{
26}
27
29{
30 End();
31}
32
33Engine::Engine(Engine&& other) noexcept
34 : m_stream_info(other.m_stream_info)
35 , m_graphics_config(other.m_graphics_config)
36 , m_is_paused(other.m_is_paused)
37 , m_is_initialized(other.m_is_initialized)
38 , m_should_shutdown(other.m_should_shutdown.load())
39 , m_scheduler(std::move(other.m_scheduler))
40 , m_node_graph_manager(std::move(other.m_node_graph_manager))
41 , m_buffer_manager(std::move(other.m_buffer_manager))
42 , m_subsystem_manager(std::move(other.m_subsystem_manager))
43 , m_window_manager(std::move(other.m_window_manager))
44 , m_event_manager(std::move(other.m_event_manager))
45 , m_rng(std::move(other.m_rng))
46{
47 other.m_is_initialized = false;
48 other.m_is_paused = false;
49}
50
52{
53 if (this != &other) {
54 End();
55
56 m_stream_info = other.m_stream_info;
57 m_graphics_config = other.m_graphics_config;
58
59 m_subsystem_manager = std::move(other.m_subsystem_manager);
60 m_node_graph_manager = std::move(other.m_node_graph_manager);
61 m_buffer_manager = std::move(other.m_buffer_manager);
62 m_scheduler = std::move(other.m_scheduler);
63 m_window_manager = std::move(other.m_window_manager);
64 m_event_manager = std::move(other.m_event_manager);
65 m_rng = std::move(other.m_rng);
66
67 m_is_initialized = other.m_is_initialized;
68 m_is_paused = other.m_is_paused;
69 m_should_shutdown = other.m_should_shutdown.load();
70
71 other.m_is_initialized = false;
72 other.m_is_paused = false;
73 }
74 return *this;
75}
76
81
82void Engine::Init(const GlobalStreamInfo& streamInfo)
83{
84 Init(streamInfo, m_graphics_config);
85}
86
87void Engine::Init(const GlobalStreamInfo& streamInfo, const GlobalGraphicsConfig& graphics_config)
88{
90 m_stream_info = streamInfo;
91 m_graphics_config = graphics_config;
92
93 m_scheduler = std::make_shared<Vruta::TaskScheduler>(m_stream_info.sample_rate);
94 m_event_manager = std::make_shared<Vruta::EventManager>();
95
96 m_buffer_manager = std::make_shared<Buffers::BufferManager>(
100
101 m_node_graph_manager = std::make_shared<Nodes::NodeGraphManager>();
102
104 m_window_manager = std::make_shared<WindowManager>(m_graphics_config);
105 } else {
106 MF_WARN(Journal::Component::Core, Journal::Context::Init, "No windowing backend selected - running in audio-only mode");
107 }
108
109 m_subsystem_manager = std::make_shared<SubsystemManager>(
111
113
114 m_subsystem_manager->create_graphics_subsystem(m_graphics_config);
115
116 m_buffer_manager->initialize_buffer_service();
117
118 m_is_initialized = true;
119
120 MF_PRINT(Journal::Component::Core, Journal::Context::Init, "Audio backend: RtAudio, Sample rate: {}", m_stream_info.sample_rate);
121}
122
124{
125 if (!m_is_initialized) {
126 Init();
127 }
128 m_subsystem_manager->start_all_subsystems();
129}
130
132{
134 return;
135 }
136
137 m_subsystem_manager->pause_all_subsystems();
138 m_is_paused = true;
139}
140
142{
143 if (!m_is_paused || !m_is_initialized) {
144 return;
145 }
146
147 m_subsystem_manager->resume_all_subsystems();
148 m_is_paused = false;
149}
150
152{
154 return false;
155 }
156
157 if (m_is_initialized) {
158 auto status = m_subsystem_manager->query_subsystem_status();
159 for (const auto& [type, readiness] : status) {
160 const auto& [is_ready, is_running] = readiness;
161 if (is_ready && is_running) {
162 return true;
163 }
164 }
165 }
166 return false;
167}
168
170{
171#ifdef MAYAFLUX_PLATFORM_MACOS
172 run_macos_event_loop();
173#else
174 // Simple blocking wait on other platforms
175 std::cin.get();
176#endif
177
178 m_should_shutdown.store(true, std::memory_order_release);
179
181 "Shutdown requested, awaiting all subsystem termination ......");
182}
183
185{
186 m_should_shutdown.store(true, std::memory_order_release);
187
188#ifdef MAYAFLUX_PLATFORM_MACOS
189 CFRunLoopStop(CFRunLoopGetMain());
190#endif
191}
192
194{
195 return m_should_shutdown.load(std::memory_order_acquire);
196}
197
198#ifdef MAYAFLUX_PLATFORM_MACOS
199void Engine::run_macos_event_loop()
200{
201 CFRunLoopRef runLoop = CFRunLoopGetMain();
202
203 dispatch_source_t stdinSource = dispatch_source_create(
204 DISPATCH_SOURCE_TYPE_READ,
205 STDIN_FILENO,
206 0,
207 dispatch_get_main_queue());
208
209 dispatch_source_set_event_handler(stdinSource, ^{
210 char buf[1024];
211 ssize_t bytes_read = read(STDIN_FILENO, buf, sizeof(buf));
212 if (bytes_read > 0) {
214 }
215 });
216
217 dispatch_resume(stdinSource);
218
219 double timeout_seconds = 1.0 / static_cast<double>(m_graphics_config.target_frame_rate);
220
222 "Main thread event loop running (polling at {}fps)",
224
225 while (!is_shutdown_requested()) {
226 CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout_seconds, false);
227 }
228
229 dispatch_source_cancel(stdinSource);
230 dispatch_release(stdinSource);
231
233 "Main thread event loop exiting");
234}
235#endif
236
238{
239 if (!m_is_initialized)
240 return;
241
243 m_node_graph_manager->terminate_active_processing();
244 }
245
247 m_subsystem_manager->stop();
248 }
249
250 if (m_scheduler) {
251 m_scheduler->terminate_all_tasks();
252 }
253
254 if (m_buffer_manager) {
255 m_buffer_manager->terminate_active_buffers();
256 m_buffer_manager.reset();
257 }
258
259 if (m_window_manager) {
260 m_window_manager->set_terminate();
261 auto windows = m_window_manager->get_windows();
262 for (auto& window : windows) {
263 m_window_manager->destroy_window(window, true);
264 }
265 m_window_manager.reset();
266 }
267
269 m_node_graph_manager.reset();
270 }
271
273 m_subsystem_manager->shutdown();
274 }
275
276 m_is_initialized = false;
277 m_is_paused = false;
278}
279
280} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_PRINT(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
void Init()
Initializes all system components and prepares for processing.
Definition Engine.cpp:77
std::shared_ptr< SubsystemManager > m_subsystem_manager
Definition Engine.hpp:341
void await_shutdown()
Blocks until shutdown is requested (main thread event loop)
Definition Engine.cpp:169
GlobalGraphicsConfig m_graphics_config
Graphics/windowing configuration.
Definition Engine.hpp:327
void request_shutdown()
Request shutdown from any thread.
Definition Engine.cpp:184
void Resume()
Resumes processing from paused state.
Definition Engine.cpp:141
std::shared_ptr< Vruta::EventManager > m_event_manager
Event manager (currently only glfw events)
Definition Engine.hpp:343
std::shared_ptr< Buffers::BufferManager > m_buffer_manager
Buffer manager.
Definition Engine.hpp:340
GlobalStreamInfo m_stream_info
Stream configuration.
Definition Engine.hpp:326
bool is_running() const
Checks if the coordinated processing system is currently active.
Definition Engine.cpp:151
void Start()
Starts the coordinated processing of all subsystems.
Definition Engine.cpp:123
std::atomic< bool > m_should_shutdown
Definition Engine.hpp:332
void Pause()
Pauses all processing while maintaining system state.
Definition Engine.cpp:131
bool m_is_paused
Pause state flag.
Definition Engine.hpp:329
std::shared_ptr< WindowManager > m_window_manager
Window manager (Windowing subsystem)
Definition Engine.hpp:342
Engine()
Constructs a new Engine instance.
Definition Engine.cpp:23
std::shared_ptr< Vruta::TaskScheduler > m_scheduler
Task scheduler.
Definition Engine.hpp:338
~Engine()
Destroys the Engine instance and cleans up resources.
Definition Engine.cpp:28
bool is_shutdown_requested() const
Check if shutdown has been requested.
Definition Engine.cpp:193
void End()
Stops all processing and performs clean shutdown.
Definition Engine.cpp:237
std::shared_ptr< Nodes::NodeGraphManager > m_node_graph_manager
Node graph manager.
Definition Engine.hpp:339
Engine & operator=(const Engine &)=delete
Central lifecycle manager and component orchestrator for the MayaFlux processing system.
Definition Engine.hpp:77
@ Init
Engine/subsystem initialization.
@ Runtime
General runtime operations (default fallback)
@ Core
Core engine, backend, subsystems.
void End()
Stops and cleans up the default engine.
Definition Core.cpp:142
@ NONE
No windowing (offscreen rendering only)
uint32_t target_frame_rate
Target frame rate for visual processing (Hz)
WindowingBackend windowing_backend
Selected windowing backend.
uint32_t channels
Number of discrete channels in this set.
bool enabled
Whether this channel set is active in the stream.
uint32_t buffer_size
Number of samples per processing block.
ChannelConfig input
Configuration for input signal channels (disabled by default)
uint32_t sample_rate
Number of samples processed per second (Hz)
ChannelConfig output
Configuration for output signal channels.
Comprehensive configuration for digital audio stream processing.