gstreamer
Loading...
Searching...
No Matches
gstreamerJSONFactory.h
Go to the documentation of this file.
1#pragma once
2
3// gstreamer
4#include "gstreamer.h"
6
7// c++
8#include <fstream>
9#include <sstream>
10#include <string>
11#include <vector>
12
53public:
56
62
63private:
70 bool openConnection() override;
71
80 bool closeConnectionImpl() override;
81
91 bool startEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) override;
92
99 bool endEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) override;
100
107 bool publishEventHeaderImpl(const std::unique_ptr<GEventHeader>& gevent_header) override;
108
116 bool publishEventTrueInfoDataImpl(const std::string& detectorName,
117 const std::vector<const GTrueInfoData*>& trueInfoData) override;
118
126 bool publishEventDigitizedDataImpl(const std::string& detectorName,
127 const std::vector<const GDigitizedData*>& digitizedData) override;
128
139 bool startStreamImpl(const GFrameDataCollection* frameRunData) override;
140
146 bool endStreamImpl(const GFrameDataCollection* frameRunData) override;
147
153 bool publishFrameHeaderImpl(const GFrameHeader* gframeHeader) override;
154
160 bool publishPayloadImpl(const std::vector<GIntegralPayload*>* payload) override;
161
162private:
163 // Output stream (one instance per worker thread).
164 std::ofstream ofile;
165
166 // True if the file header (opening braces) has been written.
167 bool is_file_initialized = false;
168
169 // Tracks whether we've already written at least one entry inside the current top-level array.
170 bool wrote_first_top_level_entry = false;
171
172 // Active top-level mode: "event" or "stream".
173 std::string top_level_type;
174
175 // Event assembly state.
176 bool is_building_event = false;
177 std::ostringstream current_event;
178 bool current_event_has_header = false;
179 bool current_event_has_any_detector = false;
180
181 // Frame assembly state (minimal support).
182 bool is_building_frame = false;
183 std::ostringstream current_frame;
184 bool current_frame_has_header = false;
185 bool current_frame_has_payload = false;
186
187 // Cached header fields (used by event writers).
188 std::string timestamp;
189 int event_number = -1;
190 int thread_id = -1;
191
192private:
193 [[nodiscard]] std::string filename() const override { return gstreamer_definitions.rootname + ".json"; }
194
195private:
196 // Helper utilities (kept private and not cross-referenced).
197 static std::string jsonEscape(const std::string& s);
198 void ensureFileInitializedForType(const std::string& type);
199 void writeTopLevelEntry(const std::string& entry_json);
200 void closeTopLevelObjectIfNeeded();
201};
Abstract base class for streaming GEMC event or frame data to output media.
Definition gstreamer.h:66
GStreamer(const std::shared_ptr< GOptions > &g)
Construct a streamer and bind it to module logging.
Definition gstreamer.h:72
GStreamerDefinition gstreamer_definitions
Output definition used by this streamer (format, base name, type, thread id).
Definition gstreamer.h:173
Writes gstreamer output to a JSON file.
GstreamerJsonFactory(GstreamerJsonFactory &&)=delete
GstreamerJsonFactory(const GstreamerJsonFactory &)=delete
One instance per thread: forbid copy/move to prevent accidental sharing of output streams.
GstreamerJsonFactory & operator=(GstreamerJsonFactory &&)=delete
GstreamerJsonFactory & operator=(const GstreamerJsonFactory &)=delete
Shared constants and error codes for the gstreamer module.
Core streaming interface for gstreamer output plugins.
std::string rootname
Base filename (without extension), possibly specialized by thread id.