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
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->getScalarInt("ebuffer");
203 }
204
205protected:
208
218 [[nodiscard]] bool startEvent([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
219 if (!event_data) { log->error(ERR_PUBLISH_ERROR, "eventData is null in GStreamer::startEvent"); }
220 if (!event_data->getHeader()) {
221 log->error(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) { log->error(ERR_PUBLISH_ERROR, "event header is null in GStreamer::publishEventHeader"); }
270 log->debug(NORMAL, "GStreamer::publishEventHeader");
271 return publishEventHeaderImpl(gevent_header);
272 }
273
280 virtual bool publishEventHeaderImpl([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header) {
281 return false;
282 }
283
294 [[nodiscard]] bool publishEventTrueInfoData([[maybe_unused]] const std::string& detectorName,
295 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
296 log->debug(NORMAL, "GStreamer::publishEventTrueInfoData for detector ", detectorName);
297 return publishEventTrueInfoDataImpl(detectorName, trueInfoData);
298 }
299
307 virtual bool publishEventTrueInfoDataImpl([[maybe_unused]] const std::string& detectorName,
308 [[maybe_unused]] const std::vector<const GTrueInfoData*>& trueInfoData) {
309 return false;
310 }
311
322 [[nodiscard]] bool publishEventDigitizedData([[maybe_unused]] const std::string& detectorName,
323 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
324 log->debug(NORMAL, "GStreamer::publishEventDigitizedData for detector ", detectorName);
325 return publishEventDigitizedDataImpl(detectorName, digitizedData);
326 }
327
328 [[nodiscard]] bool publishEventGeneratedParticles([[maybe_unused]] const std::string& bankName,
329 [[maybe_unused]] const GGeneratedParticleBank& particles) {
330 log->debug(NORMAL, "GStreamer::publishEventGeneratedParticles for bank ", bankName);
331 return publishEventGeneratedParticlesImpl(bankName, particles);
332 }
333
340 [[nodiscard]] bool publishEventAncestors(const GAncestorBank& ancestors) {
341 log->debug(NORMAL, "GStreamer::publishEventAncestors");
342 return publishEventAncestorsImpl(ancestors);
343 }
344
359 virtual bool publishEventGeneratedParticlesImpl([[maybe_unused]] const std::string& bankName,
360 [[maybe_unused]] const GGeneratedParticleBank& particles) {
361 return true;
362 }
363
365 virtual bool publishEventAncestorsImpl([[maybe_unused]] const GAncestorBank& ancestors) { return true; }
366
374 virtual bool publishEventDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
375 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
376 return false;
377 }
378
385 [[nodiscard]] bool startRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
386 if (!run_data) { log->error(ERR_PUBLISH_ERROR, "run_data is null in GStreamer::startRun"); }
387 if (!run_data->getHeader()) {
388 log->error(ERR_PUBLISH_ERROR, "run header is null in GStreamer::startRun");
389 }
390
391 log->debug(NORMAL, "GStreamer::startRun");
392 return startRunImpl(run_data);
393 }
394
401 virtual bool startRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
402 return false;
403 }
404
411 [[nodiscard]] bool endRun([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
412 log->debug(NORMAL, "GStreamer::endRun");
413 return endRunImpl(run_data);
414 }
415
422 virtual bool endRunImpl([[maybe_unused]] const std::shared_ptr<GRunDataCollection>& run_data) {
423 return false;
424 }
425
432 bool publishRunHeader([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
433 log->debug(NORMAL, "GStreamer::publishRunHeader");
434 return publishRunHeaderImpl(run_header);
435 }
436
443 virtual bool publishRunHeaderImpl([[maybe_unused]] const std::unique_ptr<GRunHeader>& run_header) {
444 return false;
445 }
446
454 bool publishRunDigitizedData([[maybe_unused]] const std::string& detectorName,
455 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
456 log->debug(NORMAL, "GStreamer::publishRunDigitizedData for detector ", detectorName);
457 return publishRunDigitizedDataImpl(detectorName, digitizedData);
458 }
459
467 virtual bool publishRunDigitizedDataImpl([[maybe_unused]] const std::string& detectorName,
468 [[maybe_unused]] const std::vector<const GDigitizedData*>& digitizedData) {
469 return false;
470 }
471
481 [[nodiscard]] bool startStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
483 log->debug(NORMAL, "GStreamer::startStream");
484 return startStreamImpl(frameRunData);
485 }
486
493 virtual bool startStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
494
501 [[nodiscard]] bool publishFrameHeader([[maybe_unused]] const GFrameHeader* gframeHeader) {
502 log->debug(NORMAL, "GStreamer::publishFrameHeader");
503 return publishFrameHeaderImpl(gframeHeader);
504 }
505
512 virtual bool publishFrameHeaderImpl([[maybe_unused]] const GFrameHeader* gframeHeader) { return false; }
513
520 [[nodiscard]] bool publishPayload([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) {
521 log->debug(NORMAL, "GStreamer::publishPayload");
522 return publishPayloadImpl(payload);
523 }
524
531 virtual bool publishPayloadImpl([[maybe_unused]] const std::vector<GIntegralPayload*>* payload) { return false; }
532
539 [[nodiscard]] bool endStream([[maybe_unused]] const GFrameDataCollection* frameRunData) {
540 log->debug(NORMAL, "GStreamer::endStream");
541 return endStreamImpl(frameRunData);
542 }
543
550 virtual bool endStreamImpl([[maybe_unused]] const GFrameDataCollection* frameRunData) { return false; }
551
559 void flushEventBuffer();
560
561private:
570 [[nodiscard]] virtual std::string filename() const = 0;
571
573 std::vector<std::shared_ptr<GEventDataCollection>> eventBuffer;
574
576 size_t bufferFlushLimit = 10;
577
578public:
589 static GStreamer* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
590 if (!h) return nullptr;
591 using fptr = GStreamer* (*)(std::shared_ptr<GOptions>);
592
593 auto sym = dlsym(h, "GStreamerFactory");
594 if (!sym) return nullptr;
595
596 auto func = reinterpret_cast<fptr>(sym);
597 return func(g);
598 }
599};
600
601
602namespace gstreamer {
603
604 using gstreamersMap = std::unordered_map<std::string, std::shared_ptr<GStreamer>>;
605
621 inline std::shared_ptr<const gstreamersMap> gstreamersMapPtr(const std::shared_ptr<GOptions>& gopts,
622 int thread_id = -1) {
623 auto log = std::make_shared<GLogger>(gopts, "gstreamersMap worker for thread id" + std::to_string(thread_id),
625
626 GManager manager(gopts);
627
628 auto gstreamers = std::make_shared<gstreamersMap>();
629
630 for (const auto& gstreamer_def : gstreamer::getGStreamerDefinition(gopts)) {
631 auto gstreamer_def_thread = GStreamerDefinition(gstreamer_def, thread_id);
632 std::string gstreamer_plugin = gstreamer_def_thread.gstreamerPluginName();
633 // Key the map by the format-derived plugin name combined with the per-output
634 // rootname so that neither multiple same-format outputs (e.g. two csv files)
635 // nor multiple same-rootname outputs of different formats (e.g. dc.csv and
636 // dc.hipo) collide; the plugin library is still loaded by the plugin name.
637 const std::string output_key = gstreamer_plugin + ":" + gstreamer_def_thread.rootname;
638
639 // Load the plugin object for this configured output. Each call returns a
640 // fresh GStreamer instance, so same-format outputs stay independent.
641 auto streamer = manager.LoadAndRegisterObjectFromLibrary<GStreamer>(gstreamer_plugin, gopts);
642 auto [it, inserted] = gstreamers->emplace(output_key, streamer);
643 if (!inserted) {
644 log->warning("duplicate gstreamer output name '", output_key,
645 "' - ignoring later definition");
646 continue;
647 }
648
649 // Bind the thread-specialized definition to this (freshly created) plugin instance.
650 it->second->define_gstreamer(gstreamer_def_thread);
651 }
652
653 return gstreamers;
654 }
655
667 inline std::shared_ptr<const gstreamersMap> preloadGStreamerPlugins(const std::shared_ptr<GOptions>& gopts) {
668 if (gstreamer::getGStreamerDefinition(gopts).empty()) { return std::make_shared<gstreamersMap>(); }
669 return gstreamersMapPtr(gopts);
670 }
671
672} // namespace gstreamer
std::shared_ptr< GLogger > log
void debug(debug_type type, Args &&... args) const
void error(int exit_code, Args &&... args) const
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:531
bool publishFrameHeader(const GFrameHeader *gframeHeader)
Publish one frame header.
Definition gstreamer.h:501
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:493
bool publishEventDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish the digitized hit bank for one detector.
Definition gstreamer.h:322
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:481
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:454
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:307
bool endRun(const std::shared_ptr< GRunDataCollection > &run_data)
End publishing one run-level collection.
Definition gstreamer.h:411
virtual bool endStreamImpl(const GFrameDataCollection *frameRunData)
Plugin-specific implementation hook called at the end of a frame stream record.
Definition gstreamer.h:550
void flushEventBuffer()
Flush all buffered events to the backend in publish order.
Definition gstreamer.cc:73
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:359
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:385
void publishRunData(const std::shared_ptr< GRunDataCollection > &run_data)
Publish one run-level data collection immediately.
Definition gstreamer.cc:45
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:467
virtual bool publishRunHeaderImpl(const std::unique_ptr< GRunHeader > &run_header)
Plugin-specific implementation hook for serializing one run header.
Definition gstreamer.h:443
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:432
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:512
bool endStream(const GFrameDataCollection *frameRunData)
End publishing one frame stream record.
Definition gstreamer.h:539
virtual bool publishEventHeaderImpl(const std::unique_ptr< GEventHeader > &gevent_header)
Plugin-specific implementation hook for serializing one event header.
Definition gstreamer.h:280
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:365
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:589
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:340
bool publishPayload(const std::vector< GIntegralPayload * > *payload)
Publish one frame payload.
Definition gstreamer.h:520
bool publishEventTrueInfoData(const std::string &detectorName, const std::vector< const GTrueInfoData * > &trueInfoData)
Publish the true-information hit bank for one detector.
Definition gstreamer.h:294
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:374
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:401
bool publishEventGeneratedParticles(const std::string &bankName, const GGeneratedParticleBank &particles)
Definition gstreamer.h:328
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:422
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:621
std::shared_ptr< const gstreamersMap > preloadGStreamerPlugins(const std::shared_ptr< GOptions > &gopts)
Preload configured streamer plugins before worker threads are started.
Definition gstreamer.h:667
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.
#define ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
Option and configuration helpers for the gstreamer module.
constexpr const char * GSTREAMER_LOGGER
Logger category name used by gstreamer components.
std::unordered_map< std::string, std::shared_ptr< GStreamer > > gstreamersMap
Definition gstreamer.h:604
Lightweight description of one configured gstreamer output.
std::string type
Semantic output type, typically "event" or "stream".