gstreamer
Loading...
Searching...
No Matches
gstreamer.h
Go to the documentation of this file.
1#pragma once
2
3// gstreamer
4#include "gstreamer_options.h"
6
7// gemc
8#include <gemc/gdata/event/gEventDataCollection.h>
9#include <gemc/gdata/run/gRunDataCollection.h>
10#include <gemc/gdata/frame/gFrameDataCollection.h>
11#include <gemc/gfactory/gfactory.h>
12#include <gemc/gbase/gbase.h>
13
14// c++
15#include <string>
16#include <vector>
17#include <map>
18
24
77class GStreamer : public GBase<GStreamer>
78{
79public:
85 explicit GStreamer(const std::shared_ptr<GOptions>& g) : GBase(g, GSTREAMER_LOGGER) {
86 }
87
94 virtual ~GStreamer() = default;
95
104 [[nodiscard]] virtual bool openConnection() { return false; }
105
114 [[nodiscard]] bool closeConnection() {
116 return closeConnectionImpl();
117 }
118
127 [[nodiscard]] virtual bool closeConnectionImpl() { return false; }
128
138 void publishEventData(const std::shared_ptr<GEventDataCollection>& event_data);
139
148 void publishRunData(const std::shared_ptr<GRunDataCollection>& run_data);
149
158 [[nodiscard]] inline std::string getStreamType() const { return gstreamer_definitions.type; }
159
170 inline void define_gstreamer(const GStreamerDefinition& gstreamerDefinition, int tid = -1) {
171 gstreamer_definitions = GStreamerDefinition(gstreamerDefinition, tid);
172 }
173
182 static const std::vector<std::string>& supported_formats();
183
192 static bool is_valid_format(const std::string& format);
193
201 void set_loggers(const std::shared_ptr<GOptions>& g) {
202 bufferFlushLimit = g->getRequiredScalarInt("ebuffer");
203 }
204
205protected:
208
218 [[nodiscard]] bool startEvent([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
219 if (!event_data) {
220 log->error(gstreamer::ERR_PUBLISH_ERROR, "eventData is null in GStreamer::startEvent");
221 }
222 if (!event_data->getHeader()) {
223 log->error(gstreamer::ERR_PUBLISH_ERROR, "event header is null in GStreamer::startEvent");
224 }
225
226 log->debug(NORMAL, "GStreamer::startEvent");
227 return startEventImpl(event_data);
228 }
229
236 virtual bool startEventImpl([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
237 return false;
238 }
239
246 [[nodiscard]] bool endEvent([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
247 log->debug(NORMAL, "GStreamer::endEvent");
248 return endEventImpl(event_data);
249 }
250
257 virtual bool endEventImpl([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
258 return false;
259 }
260
270 [[nodiscard]] bool publishEventHeader([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header) {
271 if (!gevent_header) {
272 log->error(gstreamer::ERR_PUBLISH_ERROR, "event header is null in GStreamer::publishEventHeader");
273 }
274 log->debug(NORMAL, "GStreamer::publishEventHeader");
275 return publishEventHeaderImpl(gevent_header);
276 }
277
284 virtual bool publishEventHeaderImpl([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header) {
285 return false;
286 }
287
298 [[nodiscard]] bool publishEventTrueInfoData([[maybe_unused]] const std::string& detectorName,
299 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
300 log->debug(NORMAL, "GStreamer::publishEventTrueInfoData for detector %1", detectorName);
301 return publishEventTrueInfoDataImpl(detectorName, trueInfoData);
302 }
303
311 virtual bool publishEventTrueInfoDataImpl([[maybe_unused]] const std::string& detectorName,
312 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
313 return false;
314 }
315
326 [[nodiscard]] bool publishEventDigitizedData([[maybe_unused]] const std::string& detectorName,
327 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
328 log->debug(NORMAL, "GStreamer::publishEventDigitizedData for detector %1", detectorName);
329 return publishEventDigitizedDataImpl(detectorName, digitizedData);
330 }
331
332 [[nodiscard]] bool publishEventGeneratedParticles([[maybe_unused]] const std::string& bankName,
333 [[maybe_unused]] const GGeneratedParticleBank& particles) {
334 log->debug(NORMAL, "GStreamer::publishEventGeneratedParticles for bank %1", bankName);
335 return publishEventGeneratedParticlesImpl(bankName, particles);
336 }
337
344 [[nodiscard]] bool publishEventAncestors(const GAncestorBank& ancestors) {
345 log->debug(NORMAL, "GStreamer::publishEventAncestors");
346 return publishEventAncestorsImpl(ancestors);
347 }
348
363 virtual bool publishEventGeneratedParticlesImpl([[maybe_unused]] const std::string& bankName,
364 [[maybe_unused]] const GGeneratedParticleBank& particles) {
365 return true;
366 }
367
369 virtual bool publishEventAncestorsImpl([[maybe_unused]] const GAncestorBank& ancestors) { return true; }
370
378 virtual bool publishEventDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
379 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
380 return false;
381 }
382
389 [[nodiscard]] bool startRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
390 if (!run_data) { log->error(gstreamer::ERR_PUBLISH_ERROR, "run_data is null in GStreamer::startRun"); }
391 if (!run_data->getHeader()) {
392 log->error(gstreamer::ERR_PUBLISH_ERROR, "run header is null in GStreamer::startRun");
393 }
394
395 log->debug(NORMAL, "GStreamer::startRun");
396 return startRunImpl(run_data);
397 }
398
405 virtual bool startRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
406 return false;
407 }
408
415 [[nodiscard]] bool endRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
416 log->debug(NORMAL, "GStreamer::endRun");
417 return endRunImpl(run_data);
418 }
419
426 virtual bool endRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
427 return false;
428 }
429
436 bool publishRunHeader([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
437 log->debug(NORMAL, "GStreamer::publishRunHeader");
438 return publishRunHeaderImpl(run_header);
439 }
440
447 virtual bool publishRunHeaderImpl([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
448 return false;
449 }
450
458 bool publishRunDigitizedData([[maybe_unused]] const std::string& detectorName,
459 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
460 log->debug(NORMAL, "GStreamer::publishRunDigitizedData for detector %1", detectorName);
461 return publishRunDigitizedDataImpl(detectorName, digitizedData);
462 }
463
471 virtual bool publishRunDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
472 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
473 return false;
474 }
475
485 [[nodiscard]] bool startStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
487 log->debug(NORMAL, "GStreamer::startStream");
488 return startStreamImpl(frameRunData);
489 }
490
497 virtual bool startStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
498
505 [[nodiscard]] bool publishFrameHeader([[maybe_unused]] const GFrameHeader* gframeHeader) {
506 log->debug(NORMAL, "GStreamer::publishFrameHeader");
507 return publishFrameHeaderImpl(gframeHeader);
508 }
509
516 virtual bool publishFrameHeaderImpl([[maybe_unused]] const GFrameHeader* gframeHeader) { return false; }
517
524 [[nodiscard]] bool publishPayload([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) {
525 log->debug(NORMAL, "GStreamer::publishPayload");
526 return publishPayloadImpl(payload);
527 }
528
535 virtual bool publishPayloadImpl([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) { return false; }
536
543 [[nodiscard]] bool endStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
544 log->debug(NORMAL, "GStreamer::endStream");
545 return endStreamImpl(frameRunData);
546 }
547
554 virtual bool endStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
555
563 void flushEventBuffer();
564
565private:
574 [[nodiscard]] virtual std::string filename() const = 0;
575
577 std::vector<std::shared_ptr<GEventDataCollection>> eventBuffer;
578
580 size_t bufferFlushLimit = 10;
581
582public:
593 static GStreamer* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
594 if (!h) return nullptr;
595 using fptr = GStreamer* (*)(std::shared_ptr<GOptions>);
596
597 auto sym = dlsym(h, "GStreamerFactory");
598 if (!sym) return nullptr;
599
600 auto func = reinterpret_cast<fptr>(sym);
601 return func(g);
602 }
603};
604
605
606namespace gstreamer {
607
608 using gstreamersMap = std::unordered_map<std::string, std::shared_ptr<GStreamer>>;
609
625 inline std::shared_ptr<const gstreamersMap> gstreamersMapPtr(const std::shared_ptr<GOptions>& gopts,
626 int thread_id = -1) {
627 auto log = std::make_shared<GLogger>(gopts, "gstreamersMap worker for thread id" + std::to_string(thread_id),
629
630 GManager manager(gopts);
631
632 auto gstreamers = std::make_shared<gstreamersMap>();
633
634 for (const auto& gstreamer_def : gstreamer::getGStreamerDefinition(gopts)) {
635 if (gstreamer_def.format == "sro") { continue; } // Shared GSROFactory owns crate output.
636 auto gstreamer_def_thread = GStreamerDefinition(gstreamer_def, thread_id);
637 std::string gstreamer_plugin = gstreamer_def_thread.gstreamerPluginName();
638 // Key the map by the format-derived plugin name combined with the per-output
639 // rootname so that neither multiple same-format outputs (e.g. two csv files)
640 // nor multiple same-rootname outputs of different formats (e.g. dc.csv and
641 // dc.hipo) collide; the plugin library is still loaded by the plugin name.
642 const std::string output_key = gstreamer_plugin + ":" + gstreamer_def_thread.rootname;
643
644 // Load the plugin object for this configured output. Each call returns a
645 // fresh GStreamer instance, so same-format outputs stay independent.
646 auto streamer = manager.LoadAndRegisterObjectFromLibrary<GStreamer>(gstreamer_plugin, gopts);
647 auto [it, inserted] = gstreamers->emplace(output_key, streamer);
648 if (!inserted) {
649 log->warning("duplicate gstreamer output name '%1' - ignoring later definition", output_key);
650 continue;
651 }
652
653 // Bind the thread-specialized definition to this (freshly created) plugin instance.
654 it->second->define_gstreamer(gstreamer_def_thread);
655 }
656
657 return gstreamers;
658 }
659
671 inline std::shared_ptr<const gstreamersMap> preloadGStreamerPlugins(const std::shared_ptr<GOptions>& gopts) {
672 if (gstreamer::getGStreamerDefinition(gopts).empty()) { return std::make_shared<gstreamersMap>(); }
673 return gstreamersMapPtr(gopts);
674 }
675
676} // namespace gstreamer
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
std::shared_ptr< T > LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr< GOptions > &gopts)
Abstract base class for all gstreamer output plugins.
Definition gstreamer.h:78
virtual bool publishPayloadImpl(const std::vector< GIntegralPayload * > *payload)
Plugin-specific implementation hook for serializing one frame payload.
Definition gstreamer.h:535
bool publishFrameHeader(const GFrameHeader *gframeHeader)
Publish one frame header.
Definition gstreamer.h:505
static const std::vector< std::string > & supported_formats()
Return the list of output format tokens supported by the module.
Definition gstreamer.cc:11
virtual bool startStreamImpl(const GFrameDataCollection *frameRunData)
Plugin-specific implementation hook called at the start of a frame stream record.
Definition gstreamer.h:497
bool publishEventDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish the digitized hit bank for one detector.
Definition gstreamer.h:326
std::string getStreamType() const
Return the semantic stream type associated with this streamer instance.
Definition gstreamer.h:158
bool startStream(const GFrameDataCollection *frameRunData)
Begin publishing one frame stream record.
Definition gstreamer.h:485
virtual ~GStreamer()=default
Virtual destructor.
void publishEventData(const std::shared_ptr< GEventDataCollection > &event_data)
Queue one event for publication.
Definition gstreamer.cc:25
virtual bool closeConnectionImpl()
Plugin-specific close implementation hook.
Definition gstreamer.h:127
bool publishRunDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish run-level digitized data for one detector.
Definition gstreamer.h:458
virtual bool publishEventTrueInfoDataImpl(const std::string &detectorName, const std::vector< const GTrueInfoData * > &trueInfoData)
Plugin-specific implementation hook for one detector true-information collection.
Definition gstreamer.h:311
bool endRun(const std::shared_ptr< GRunDataCollection > &run_data)
End publishing one run-level collection.
Definition gstreamer.h:415
virtual bool endStreamImpl(const GFrameDataCollection *frameRunData)
Plugin-specific implementation hook called at the end of a frame stream record.
Definition gstreamer.h:554
void flushEventBuffer()
Flush all buffered events to the backend in publish order.
Definition gstreamer.cc:75
static bool is_valid_format(const std::string &format)
Validate whether a format token is supported.
Definition gstreamer.cc:17
virtual bool publishEventGeneratedParticlesImpl(const std::string &bankName, const GGeneratedParticleBank &particles)
Plugin-specific implementation hook for a generated-particle event bank.
Definition gstreamer.h:363
bool endEvent(const std::shared_ptr< GEventDataCollection > &event_data)
End publishing one buffered event.
Definition gstreamer.h:246
bool publishEventHeader(const std::unique_ptr< GEventHeader > &gevent_header)
Publish the event header for the current event sequence.
Definition gstreamer.h:270
void define_gstreamer(const GStreamerDefinition &gstreamerDefinition, int tid=-1)
Assign the output definition used by this streamer instance.
Definition gstreamer.h:170
GStreamer(const std::shared_ptr< GOptions > &g)
Construct the streamer base and initialize module logging.
Definition gstreamer.h:85
bool startRun(const std::shared_ptr< GRunDataCollection > &run_data)
Begin publishing one run-level collection.
Definition gstreamer.h:389
void publishRunData(const std::shared_ptr< GRunDataCollection > &run_data)
Publish one run-level data collection immediately.
Definition gstreamer.cc:47
virtual bool publishRunDigitizedDataImpl(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Plugin-specific implementation hook for one detector run-level digitized collection.
Definition gstreamer.h:471
virtual bool publishRunHeaderImpl(const std::unique_ptr< GRunHeader > &run_header)
Plugin-specific implementation hook for serializing one run header.
Definition gstreamer.h:447
GStreamerDefinition gstreamer_definitions
Output definition currently bound to this streamer instance.
Definition gstreamer.h:207
bool publishRunHeader(const std::unique_ptr< GRunHeader > &run_header)
Publish the run header for the current run sequence.
Definition gstreamer.h:436
virtual bool endEventImpl(const std::shared_ptr< GEventDataCollection > &event_data)
Plugin-specific implementation hook called at the end of one event publish sequence.
Definition gstreamer.h:257
virtual bool publishFrameHeaderImpl(const GFrameHeader *gframeHeader)
Plugin-specific implementation hook for serializing one frame header.
Definition gstreamer.h:516
bool endStream(const GFrameDataCollection *frameRunData)
End publishing one frame stream record.
Definition gstreamer.h:543
virtual bool publishEventHeaderImpl(const std::unique_ptr< GEventHeader > &gevent_header)
Plugin-specific implementation hook for serializing one event header.
Definition gstreamer.h:284
bool startEvent(const std::shared_ptr< GEventDataCollection > &event_data)
Begin publishing one buffered event.
Definition gstreamer.h:218
virtual bool publishEventAncestorsImpl(const GAncestorBank &ancestors)
Plugin-specific event ancestor-bank serialization hook.
Definition gstreamer.h:369
virtual bool startEventImpl(const std::shared_ptr< GEventDataCollection > &event_data)
Plugin-specific implementation hook called at the start of one event publish sequence.
Definition gstreamer.h:236
static GStreamer * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Instantiate a streamer plugin from a dynamic library handle.
Definition gstreamer.h:593
virtual bool openConnection()
Open the output medium used by this streamer.
Definition gstreamer.h:104
bool publishEventAncestors(const GAncestorBank &ancestors)
Publishes the event-local ancestor bank.
Definition gstreamer.h:344
bool publishPayload(const std::vector< GIntegralPayload * > *payload)
Publish one frame payload.
Definition gstreamer.h:524
bool publishEventTrueInfoData(const std::string &detectorName, const std::vector< const GTrueInfoData * > &trueInfoData)
Publish the true-information hit bank for one detector.
Definition gstreamer.h:298
virtual bool publishEventDigitizedDataImpl(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Plugin-specific implementation hook for one detector digitized collection.
Definition gstreamer.h:378
virtual bool startRunImpl(const std::shared_ptr< GRunDataCollection > &run_data)
Plugin-specific implementation hook called at the start of a run publish sequence.
Definition gstreamer.h:405
bool publishEventGeneratedParticles(const std::string &bankName, const GGeneratedParticleBank &particles)
Definition gstreamer.h:332
virtual bool endRunImpl(const std::shared_ptr< GRunDataCollection > &run_data)
Plugin-specific implementation hook called at the end of a run publish sequence.
Definition gstreamer.h:426
bool closeConnection()
Close the output medium after flushing buffered events.
Definition gstreamer.h:114
void set_loggers(const std::shared_ptr< GOptions > &g)
Load streamer runtime settings from the parsed options container.
Definition gstreamer.h:201
std::vector< GAncestorData > GAncestorBank
std::vector< GGeneratedParticleData > GGeneratedParticleBank
void * dlhandle
NORMAL
std::shared_ptr< const gstreamersMap > gstreamersMapPtr(const std::shared_ptr< GOptions > &gopts, int thread_id=-1)
Create a per-thread map of configured streamer instances.
Definition gstreamer.h:625
std::shared_ptr< const gstreamersMap > preloadGStreamerPlugins(const std::shared_ptr< GOptions > &gopts)
Preload configured streamer plugins before worker threads are started.
Definition gstreamer.h:671
vector< GStreamerDefinition > getGStreamerDefinition(const std::shared_ptr< GOptions > &gopts)
Parse all configured gstreamer output definitions from the options container.
Shared constants and error codes for the gstreamer module.
Option and configuration helpers for the gstreamer module.
constexpr const char * GSTREAMER_LOGGER
Logger category name used by gstreamer components.
constexpr int ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
std::unordered_map< std::string, std::shared_ptr< GStreamer > > gstreamersMap
Definition gstreamer.h:608
Lightweight description of one configured gstreamer output.