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) { log->error(gstreamer::ERR_PUBLISH_ERROR, "eventData is null in GStreamer::startEvent"); }
220 if (!event_data->getHeader()) {
221 log->error(gstreamer::ERR_PUBLISH_ERROR, "event header is null in GStreamer::startEvent");
222 }
223
224 log->debug(NORMAL, "GStreamer::startEvent");
225 return startEventImpl(event_data);
226 }
227
234 virtual bool startEventImpl([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
235 return false;
236 }
237
244 [[nodiscard]] bool endEvent([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
245 log->debug(NORMAL, "GStreamer::endEvent");
246 return endEventImpl(event_data);
247 }
248
255 virtual bool endEventImpl([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
256 return false;
257 }
258
268 [[nodiscard]] bool publishEventHeader([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header) {
269 if (!gevent_header) {
270 log->error(gstreamer::ERR_PUBLISH_ERROR, "event header is null in GStreamer::publishEventHeader");
271 }
272 log->debug(NORMAL, "GStreamer::publishEventHeader");
273 return publishEventHeaderImpl(gevent_header);
274 }
275
282 virtual bool publishEventHeaderImpl([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header) {
283 return false;
284 }
285
296 [[nodiscard]] bool publishEventTrueInfoData([[maybe_unused]] const std::string& detectorName,
297 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
298 log->debug(NORMAL, "GStreamer::publishEventTrueInfoData for detector ", detectorName);
299 return publishEventTrueInfoDataImpl(detectorName, trueInfoData);
300 }
301
309 virtual bool publishEventTrueInfoDataImpl([[maybe_unused]] const std::string& detectorName,
310 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
311 return false;
312 }
313
324 [[nodiscard]] bool publishEventDigitizedData([[maybe_unused]] const std::string& detectorName,
325 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
326 log->debug(NORMAL, "GStreamer::publishEventDigitizedData for detector ", detectorName);
327 return publishEventDigitizedDataImpl(detectorName, digitizedData);
328 }
329
330 [[nodiscard]] bool publishEventGeneratedParticles([[maybe_unused]] const std::string& bankName,
331 [[maybe_unused]] const GGeneratedParticleBank& particles) {
332 log->debug(NORMAL, "GStreamer::publishEventGeneratedParticles for bank ", bankName);
333 return publishEventGeneratedParticlesImpl(bankName, particles);
334 }
335
342 [[nodiscard]] bool publishEventAncestors(const GAncestorBank& ancestors) {
343 log->debug(NORMAL, "GStreamer::publishEventAncestors");
344 return publishEventAncestorsImpl(ancestors);
345 }
346
361 virtual bool publishEventGeneratedParticlesImpl([[maybe_unused]] const std::string& bankName,
362 [[maybe_unused]] const GGeneratedParticleBank& particles) {
363 return true;
364 }
365
367 virtual bool publishEventAncestorsImpl([[maybe_unused]] const GAncestorBank& ancestors) { return true; }
368
376 virtual bool publishEventDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
377 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
378 return false;
379 }
380
387 [[nodiscard]] bool startRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
388 if (!run_data) { log->error(gstreamer::ERR_PUBLISH_ERROR, "run_data is null in GStreamer::startRun"); }
389 if (!run_data->getHeader()) {
390 log->error(gstreamer::ERR_PUBLISH_ERROR, "run header is null in GStreamer::startRun");
391 }
392
393 log->debug(NORMAL, "GStreamer::startRun");
394 return startRunImpl(run_data);
395 }
396
403 virtual bool startRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
404 return false;
405 }
406
413 [[nodiscard]] bool endRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
414 log->debug(NORMAL, "GStreamer::endRun");
415 return endRunImpl(run_data);
416 }
417
424 virtual bool endRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
425 return false;
426 }
427
434 bool publishRunHeader([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
435 log->debug(NORMAL, "GStreamer::publishRunHeader");
436 return publishRunHeaderImpl(run_header);
437 }
438
445 virtual bool publishRunHeaderImpl([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
446 return false;
447 }
448
456 bool publishRunDigitizedData([[maybe_unused]] const std::string& detectorName,
457 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
458 log->debug(NORMAL, "GStreamer::publishRunDigitizedData for detector ", detectorName);
459 return publishRunDigitizedDataImpl(detectorName, digitizedData);
460 }
461
469 virtual bool publishRunDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
470 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
471 return false;
472 }
473
483 [[nodiscard]] bool startStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
485 log->debug(NORMAL, "GStreamer::startStream");
486 return startStreamImpl(frameRunData);
487 }
488
495 virtual bool startStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
496
503 [[nodiscard]] bool publishFrameHeader([[maybe_unused]] const GFrameHeader* gframeHeader) {
504 log->debug(NORMAL, "GStreamer::publishFrameHeader");
505 return publishFrameHeaderImpl(gframeHeader);
506 }
507
514 virtual bool publishFrameHeaderImpl([[maybe_unused]] const GFrameHeader* gframeHeader) { return false; }
515
522 [[nodiscard]] bool publishPayload([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) {
523 log->debug(NORMAL, "GStreamer::publishPayload");
524 return publishPayloadImpl(payload);
525 }
526
533 virtual bool publishPayloadImpl([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) { return false; }
534
541 [[nodiscard]] bool endStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
542 log->debug(NORMAL, "GStreamer::endStream");
543 return endStreamImpl(frameRunData);
544 }
545
552 virtual bool endStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
553
561 void flushEventBuffer();
562
563private:
572 [[nodiscard]] virtual std::string filename() const = 0;
573
575 std::vector<std::shared_ptr<GEventDataCollection>> eventBuffer;
576
578 size_t bufferFlushLimit = 10;
579
580public:
591 static GStreamer* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
592 if (!h) return nullptr;
593 using fptr = GStreamer* (*)(std::shared_ptr<GOptions>);
594
595 auto sym = dlsym(h, "GStreamerFactory");
596 if (!sym) return nullptr;
597
598 auto func = reinterpret_cast<fptr>(sym);
599 return func(g);
600 }
601};
602
603
604namespace gstreamer {
605
606 using gstreamersMap = std::unordered_map<std::string, std::shared_ptr<GStreamer>>;
607
623 inline std::shared_ptr<const gstreamersMap> gstreamersMapPtr(const std::shared_ptr<GOptions>& gopts,
624 int thread_id = -1) {
625 auto log = std::make_shared<GLogger>(gopts, "gstreamersMap worker for thread id" + std::to_string(thread_id),
627
628 GManager manager(gopts);
629
630 auto gstreamers = std::make_shared<gstreamersMap>();
631
632 for (const auto& gstreamer_def : gstreamer::getGStreamerDefinition(gopts)) {
633 auto gstreamer_def_thread = GStreamerDefinition(gstreamer_def, thread_id);
634 std::string gstreamer_plugin = gstreamer_def_thread.gstreamerPluginName();
635 // Key the map by the format-derived plugin name combined with the per-output
636 // rootname so that neither multiple same-format outputs (e.g. two csv files)
637 // nor multiple same-rootname outputs of different formats (e.g. dc.csv and
638 // dc.hipo) collide; the plugin library is still loaded by the plugin name.
639 const std::string output_key = gstreamer_plugin + ":" + gstreamer_def_thread.rootname;
640
641 // Load the plugin object for this configured output. Each call returns a
642 // fresh GStreamer instance, so same-format outputs stay independent.
643 auto streamer = manager.LoadAndRegisterObjectFromLibrary<GStreamer>(gstreamer_plugin, gopts);
644 auto [it, inserted] = gstreamers->emplace(output_key, streamer);
645 if (!inserted) {
646 log->warning("duplicate gstreamer output name '", output_key,
647 "' - ignoring later definition");
648 continue;
649 }
650
651 // Bind the thread-specialized definition to this (freshly created) plugin instance.
652 it->second->define_gstreamer(gstreamer_def_thread);
653 }
654
655 return gstreamers;
656 }
657
669 inline std::shared_ptr<const gstreamersMap> preloadGStreamerPlugins(const std::shared_ptr<GOptions>& gopts) {
670 if (gstreamer::getGStreamerDefinition(gopts).empty()) { return std::make_shared<gstreamersMap>(); }
671 return gstreamersMapPtr(gopts);
672 }
673
674} // 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:533
bool publishFrameHeader(const GFrameHeader *gframeHeader)
Publish one frame header.
Definition gstreamer.h:503
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:495
bool publishEventDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish the digitized hit bank for one detector.
Definition gstreamer.h:324
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:483
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:456
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:309
bool endRun(const std::shared_ptr< GRunDataCollection > &run_data)
End publishing one run-level collection.
Definition gstreamer.h:413
virtual bool endStreamImpl(const GFrameDataCollection *frameRunData)
Plugin-specific implementation hook called at the end of a frame stream record.
Definition gstreamer.h:552
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:361
bool endEvent(const std::shared_ptr< GEventDataCollection > &event_data)
End publishing one buffered event.
Definition gstreamer.h:244
bool publishEventHeader(const std::unique_ptr< GEventHeader > &gevent_header)
Publish the event header for the current event sequence.
Definition gstreamer.h:268
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:387
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:469
virtual bool publishRunHeaderImpl(const std::unique_ptr< GRunHeader > &run_header)
Plugin-specific implementation hook for serializing one run header.
Definition gstreamer.h:445
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:434
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:255
virtual bool publishFrameHeaderImpl(const GFrameHeader *gframeHeader)
Plugin-specific implementation hook for serializing one frame header.
Definition gstreamer.h:514
bool endStream(const GFrameDataCollection *frameRunData)
End publishing one frame stream record.
Definition gstreamer.h:541
virtual bool publishEventHeaderImpl(const std::unique_ptr< GEventHeader > &gevent_header)
Plugin-specific implementation hook for serializing one event header.
Definition gstreamer.h:282
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:367
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:234
static GStreamer * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Instantiate a streamer plugin from a dynamic library handle.
Definition gstreamer.h:591
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:342
bool publishPayload(const std::vector< GIntegralPayload * > *payload)
Publish one frame payload.
Definition gstreamer.h:522
bool publishEventTrueInfoData(const std::string &detectorName, const std::vector< const GTrueInfoData * > &trueInfoData)
Publish the true-information hit bank for one detector.
Definition gstreamer.h:296
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:376
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:403
bool publishEventGeneratedParticles(const std::string &bankName, const GGeneratedParticleBank &particles)
Definition gstreamer.h:330
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:424
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:623
std::shared_ptr< const gstreamersMap > preloadGStreamerPlugins(const std::shared_ptr< GOptions > &gopts)
Preload configured streamer plugins before worker threads are started.
Definition gstreamer.h:669
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:606
Lightweight description of one configured gstreamer output.