|
1 | 1 | #include <obs-module.h> |
2 | 2 | #include <obs-frontend-api.h> |
| 3 | +#include <cstring> |
3 | 4 |
|
4 | 5 | #include "device/input_device.hpp" |
5 | 6 | #include "device/gamepad.hpp" |
6 | 7 | #include "writer/input_writer.hpp" |
7 | 8 | #include "writer/csv.hpp" |
8 | 9 | #include "writer/parquet.hpp" |
| 10 | +#include "writer/debug.hpp" |
9 | 11 | #include "obs_source.hpp" |
10 | 12 | #include "plugin-support.h" |
11 | 13 |
|
| 14 | +// Settings keys |
| 15 | +#define SETTING_INPUT_DEVICE "input_device" |
| 16 | +#define SETTING_OUTPUT_FORMAT "output_format" |
| 17 | + |
| 18 | +// Settings values |
| 19 | +#define DEVICE_GAMEPAD "gamepad" |
| 20 | +#define FORMAT_PARQUET "parquet" |
| 21 | +#define FORMAT_CSV "csv" |
| 22 | +#define FORMAT_DEBUG "debug" |
| 23 | + |
12 | 24 | class RecSource { |
13 | 25 | private: |
14 | 26 | obs_data_t *m_settings; |
15 | 27 | obs_source_t *m_source; |
16 | 28 | std::unique_ptr<InputWriter> m_input_writer; |
17 | 29 | obs_frontend_event_cb m_event_callback; |
18 | 30 |
|
19 | | -public: |
20 | | - RecSource(obs_data_t *settings, obs_source_t *source) |
21 | | - : m_settings{settings}, |
22 | | - m_source{source}, |
23 | | - m_input_writer{std::make_unique<ParquetWriter>(std::make_unique<GamepadDevice>())} |
| 31 | + static std::unique_ptr<InputDevice> create_device(const char *type) |
24 | 32 | { |
| 33 | + // Currently only gamepad is supported |
| 34 | + return std::make_unique<GamepadDevice>(); |
| 35 | + } |
| 36 | + |
| 37 | + static std::unique_ptr<InputWriter> create_writer(const char *format, std::unique_ptr<InputDevice> device) |
| 38 | + { |
| 39 | + if (strcmp(format, FORMAT_CSV) == 0) { |
| 40 | + return std::make_unique<CSVWriter>(std::move(device)); |
| 41 | + } |
| 42 | + if (strcmp(format, FORMAT_DEBUG) == 0) { |
| 43 | + return std::make_unique<DebugWriter>(std::move(device)); |
| 44 | + } |
| 45 | + // Default to parquet |
| 46 | + return std::make_unique<ParquetWriter>(std::move(device)); |
| 47 | + } |
25 | 48 |
|
| 49 | +public: |
| 50 | + RecSource(obs_data_t *settings, obs_source_t *source) : m_settings{settings}, m_source{source} |
| 51 | + { |
26 | 52 | m_event_callback = [](enum obs_frontend_event event, void *private_data) { |
27 | | - InputWriter *current_writer = static_cast<InputWriter *>(private_data); |
| 53 | + RecSource *rec_source = static_cast<RecSource *>(private_data); |
28 | 54 | switch (event) { |
29 | 55 | case OBS_FRONTEND_EVENT_RECORDING_STARTING: { |
30 | 56 | obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STARTING received"); |
31 | | - current_writer->prepare_recording(); |
| 57 | + rec_source->prepare_recording(); |
32 | 58 | break; |
33 | 59 | } |
34 | 60 | case OBS_FRONTEND_EVENT_RECORDING_STARTED: { |
35 | 61 | obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STARTED received"); |
36 | | - current_writer->start_recording(); |
| 62 | + rec_source->m_input_writer->start_recording(); |
37 | 63 | break; |
38 | 64 | } |
39 | 65 | case OBS_FRONTEND_EVENT_RECORDING_STOPPING: { |
40 | 66 | obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STOPPING received"); |
41 | | - current_writer->stop_recording(); |
| 67 | + rec_source->m_input_writer->stop_recording(); |
42 | 68 | break; |
43 | 69 | } |
44 | 70 | case OBS_FRONTEND_EVENT_RECORDING_STOPPED: { |
45 | 71 | obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STOPPED received"); |
46 | | - // Get last recording path |
47 | 72 | std::string recording_path{obs_frontend_get_last_recording()}; |
48 | | - current_writer->close_recording(recording_path); |
| 73 | + rec_source->m_input_writer->close_recording(recording_path); |
| 74 | + rec_source->m_input_writer.reset(); |
49 | 75 | break; |
50 | 76 | } |
51 | | - default: break; |
| 77 | + default: |
| 78 | + break; |
52 | 79 | } |
53 | 80 | }; |
54 | | - obs_frontend_add_event_callback(m_event_callback, m_input_writer.get()); |
| 81 | + obs_frontend_add_event_callback(m_event_callback, this); |
55 | 82 | } |
56 | 83 |
|
57 | | - ~RecSource() |
| 84 | + ~RecSource() { obs_frontend_remove_event_callback(m_event_callback, this); } |
| 85 | + |
| 86 | + void prepare_recording() |
58 | 87 | { |
59 | | - obs_frontend_remove_event_callback(m_event_callback, m_input_writer.get()); |
| 88 | + const char *device_type = obs_data_get_string(m_settings, SETTING_INPUT_DEVICE); |
| 89 | + const char *output_format = obs_data_get_string(m_settings, SETTING_OUTPUT_FORMAT); |
| 90 | + |
| 91 | + obs_log(LOG_INFO, "Creating writer with device=%s, format=%s", device_type, output_format); |
| 92 | + |
| 93 | + auto device = create_device(device_type); |
| 94 | + m_input_writer = create_writer(output_format, std::move(device)); |
| 95 | + m_input_writer->prepare_recording(); |
60 | 96 | } |
61 | 97 |
|
62 | 98 | void tick(float seconds) { UNUSED_PARAMETER(seconds); } |
| 99 | + |
| 100 | + bool is_recording() const { return m_input_writer != nullptr; } |
63 | 101 | }; |
64 | 102 |
|
| 103 | +static void rec_source_defaults(obs_data_t *settings) |
| 104 | +{ |
| 105 | + obs_data_set_default_string(settings, SETTING_INPUT_DEVICE, DEVICE_GAMEPAD); |
| 106 | + obs_data_set_default_string(settings, SETTING_OUTPUT_FORMAT, FORMAT_PARQUET); |
| 107 | +} |
| 108 | + |
| 109 | +static obs_properties_t *rec_source_properties(void *data) |
| 110 | +{ |
| 111 | + RecSource *source = static_cast<RecSource *>(data); |
| 112 | + obs_properties_t *props = obs_properties_create(); |
| 113 | + |
| 114 | + // Check if recording is active to disable options |
| 115 | + bool recording = source && source->is_recording(); |
| 116 | + |
| 117 | + // Input device dropdown |
| 118 | + obs_property_t *device_list = obs_properties_add_list(props, SETTING_INPUT_DEVICE, |
| 119 | + obs_module_text("InputDevice"), OBS_COMBO_TYPE_LIST, |
| 120 | + OBS_COMBO_FORMAT_STRING); |
| 121 | + obs_property_list_add_string(device_list, obs_module_text("Gamepad"), DEVICE_GAMEPAD); |
| 122 | + obs_property_set_enabled(device_list, !recording); |
| 123 | + |
| 124 | + // Output format dropdown |
| 125 | + obs_property_t *format_list = obs_properties_add_list(props, SETTING_OUTPUT_FORMAT, |
| 126 | + obs_module_text("OutputFormat"), OBS_COMBO_TYPE_LIST, |
| 127 | + OBS_COMBO_FORMAT_STRING); |
| 128 | + obs_property_list_add_string(format_list, "Parquet", FORMAT_PARQUET); |
| 129 | + obs_property_list_add_string(format_list, "CSV", FORMAT_CSV); |
| 130 | + obs_property_list_add_string(format_list, obs_module_text("DebugLog"), FORMAT_DEBUG); |
| 131 | + obs_property_set_enabled(format_list, !recording); |
| 132 | + |
| 133 | + return props; |
| 134 | +} |
| 135 | + |
65 | 136 | bool initialize_rec_source() |
66 | 137 | { |
67 | 138 | obs_source_info source_info = {}; |
68 | 139 | source_info.id = "input_recording_source"; |
69 | 140 | source_info.type = OBS_SOURCE_TYPE_INPUT; |
70 | 141 | source_info.output_flags = OBS_SOURCE_VIDEO; |
71 | 142 | source_info.icon_type = OBS_ICON_TYPE_GAME_CAPTURE; |
72 | | - source_info.get_name = [](void *) { |
73 | | - return obs_module_text("InputRecording"); |
74 | | - ; |
75 | | - }; |
| 143 | + source_info.get_name = [](void *) { return obs_module_text("InputRecording"); }; |
76 | 144 | source_info.get_width = [](void *) -> uint32_t { return 0; }; |
77 | 145 | source_info.get_height = [](void *) -> uint32_t { return 0; }; |
78 | 146 | source_info.create = [](obs_data_t *settings, obs_source_t *source) -> void * { |
79 | 147 | return static_cast<void *>(new RecSource(settings, source)); |
80 | 148 | }; |
81 | 149 | source_info.destroy = [](void *data) { delete static_cast<RecSource *>(data); }; |
82 | 150 | source_info.video_tick = [](void *data, float seconds) { static_cast<RecSource *>(data)->tick(seconds); }; |
| 151 | + source_info.get_defaults = rec_source_defaults; |
| 152 | + source_info.get_properties = rec_source_properties; |
83 | 153 | obs_register_source(&source_info); |
84 | 154 | return true; |
85 | 155 | } |
0 commit comments