ADTF File Library
Loading...
Searching...
No Matches
adtf_file_writer.h
Go to the documentation of this file.
1
16
17#ifndef ADTF_FILE_FILE_WRITER_INCLUDED
18#define ADTF_FILE_FILE_WRITER_INCLUDED
19
20#include <memory>
21#include <string>
22#include <chrono>
23#include <unordered_map>
24#include <queue>
25#include <list>
26
27#include <ifhd/ifhd.h>
28#include "sample.h"
29#include "stream_type.h"
30#include "object.h"
31
32namespace adtf_file
33{
34
39{
40 public:
46 virtual void write(const void* data, size_t data_size) = 0;
47
54 template <typename T>
55 OutputStream& operator<<(const T& value)
56 {
57 static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types and std::string are allowed. Alternatively, provide a specialization for adtf_file::OutputStream& operator<<(T& value).");
58 write(&value, sizeof(T));
59 return *this;
60 }
61};
62
68template <>
69OutputStream& OutputStream::operator<<(const std::string& value);
70
75{
76 public:
81 virtual std::string getMetaType() const = 0;
87 virtual void serialize(const StreamType& stream_type, OutputStream& stream) const = 0;
88};
89
94{
95 public:
100 virtual std::string getTypeId() const = 0;
101};
102
106class StreamTypeSerializers: private std::unordered_map<std::string, std::shared_ptr<StreamTypeSerializer>>
107{
108 public:
114 void add(const std::shared_ptr<StreamTypeSerializer>& serializer)
115 {
116 emplace(serializer->getMetaType(), serializer);
117 }
118
126 void serialize(const StreamType& stream_type, OutputStream& stream) const
127 {
128 auto& property_type = dynamic_cast<const PropertyStreamType&>(stream_type);
129 auto serializer = find(property_type.getMetaType());
130 if (serializer == end())
131 {
132 // try to find a default serializer
133 serializer = find("");
134 if (serializer == end())
135 {
136 throw std::runtime_error("No serializer for stream type " + property_type.getMetaType());
137 }
138 }
139
140 serializer->second->serialize(stream_type, stream);
141 }
142
149 std::string getAdtf2TypeId(const StreamType& stream_type)
150 {
151 auto& property_type = dynamic_cast<const PropertyStreamType&>(stream_type);
152 auto serializer = find(property_type.getMetaType());
153 if (serializer != end())
154 {
155 auto adtf2_serializer = std::dynamic_pointer_cast<ADTF2StreamTypeSerializer>(serializer->second);
156 if (adtf2_serializer)
157 {
158 return adtf2_serializer->getTypeId();
159 }
160 }
161
162 return "";
163 }
164};
165
171{
172 public:
177 virtual std::string getId() const = 0;
182 virtual void setStreamType(const StreamType& stream_type) = 0;
189 virtual void serialize(const WriteSample& sample, OutputStream& stream) = 0;
190};
191
196{
197 public:
202 virtual std::string getId() const = 0;
207 virtual std::shared_ptr<SampleSerializer> build() const = 0;
208};
209
214template <typename SERIALIZER>
216{
217 public:
218 std::string getId() const override
219 {
220 return SERIALIZER::id;
221 }
222
223 std::shared_ptr<SampleSerializer> build() const override
224 {
225 return std::make_shared<SERIALIZER>();
226 }
227};
228
232class SampleSerializerFactories: private std::unordered_map<std::string, std::shared_ptr<SampleSerializerFactory>>
233{
234 public:
240 void add(const std::shared_ptr<SampleSerializerFactory>& serializer_factory)
241 {
242 emplace(serializer_factory->getId(), serializer_factory);
243 }
244
251 std::shared_ptr<SampleSerializer> build(const std::string& id) const
252 {
253 try
254 {
255 return at(id)->build();
256 }
257 catch (...)
258 {
259 throw std::runtime_error("no sample serializer factory with id '" + id + "' available.");
260 }
261 }
262};
263
267class ADTFDatFileWriter: private ifhd::v400::IndexedFileWriter::ChunkDroppedCallback
268{
269 private:
270 class Buffer: public std::vector<uint8_t>, public OutputStream
271 {
272
273 public:
274 void write(const void* data, size_t data_size) override
275 {
276 this->insert(end(), static_cast<const uint8_t*>(data), static_cast<const uint8_t*>(data) + data_size);
277 }
278 };
279
280 class Chunk: public Buffer
281 {
282 public:
283 size_t stream_id;
284 std::chrono::nanoseconds time_stamp;
285 uint32_t flags;
286
287 public:
288 void reset(size_t stream_id_to_set,
289 std::chrono::nanoseconds time_stamp_to_set,
290 uint32_t flags_to_set)
291 {
292 this->stream_id = stream_id_to_set;
293 this->time_stamp = time_stamp_to_set;
294 this->flags = flags_to_set;
295 clear();
296 }
297 };
298
299 public:
305 {
310 adtf2 = 2,
315 adtf3 = 3,
321 };
322
323 public:
324 ADTFDatFileWriter() = delete;
339 ADTFDatFileWriter(const std::string& file_name,
340 std::chrono::nanoseconds history_duration,
341 StreamTypeSerializers type_serializers,
342 TargetADTFVersion target_adtf_version = adtf3ns,
343 uint32_t writer_flags = 0);
344 ~ADTFDatFileWriter();
345
346 ADTFDatFileWriter(const ADTFDatFileWriter&) = delete;
350 ADTFDatFileWriter(ADTFDatFileWriter&&) = default;
351
359 size_t createStream(const std::string& name,
360 const StreamType& initial_type,
361 const std::shared_ptr<SampleSerializer>& serializer);
368 void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType& type);
375 void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample& sample);
381 void writeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp);
382
387
396 std::shared_ptr<OutputStream> getExtensionStream(const std::string& name,
397 uint32_t user_id,
398 uint32_t type_id,
399 uint32_t file_version_id);
404 void setFileDescription(const std::string& description);
405 private:
406 void onChunkDropped(uint64_t index, uint16_t stream_id, uint16_t flags, timestamp_t time_stamp) override;
407
408 const Chunk& serialize(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType& type);
409 const Chunk& serialize(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample& sample);
410 const Chunk& serializeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp);
411
412 void write(const Chunk& chunk);
413
414 void closeAdtf2();
415 void closeAdtf3();
416
417 private:
418 std::unique_ptr<ifhd::v500::IndexedFileWriter> _file;
419 StreamTypeSerializers _type_serializers;
420 TargetADTFVersion _target_adtf_version;
421 size_t _stream_id_counter = 0;
422 bool _history_active;
423
424 struct Stream
425 {
426 std::string name;
427 Buffer initial_type;
428 std::string adtf2_initial_type_id;
429 std::shared_ptr<SampleSerializer> sample_serializer;
430 std::list<Buffer> type_queue;
431 Chunk chunk_buffer;
432 bool has_samples = false;
433 };
434
435 std::vector<Stream> _streams;
436};
437
438}
439
440#endif
ADTF2 stream type serializer interface class to write ADTF 2 stream types.
Definition adtf_file_writer.h:94
virtual std::string getTypeId() const =0
Get the Type Id of the serializer.
Default ADTF DAT File Writer to write an ADTF DAT File (IFHD file with ADTF type and sample content).
Definition adtf_file_writer.h:268
size_t createStream(const std::string &name, const StreamType &initial_type, const std::shared_ptr< SampleSerializer > &serializer)
Create a Stream and retrieve the stream id for it.
void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample &sample)
Writes a sample to the file or the history.
void setFileDescription(const std::string &description)
Set the File Description.
void quitHistory()
quits the history.
std::shared_ptr< OutputStream > getExtensionStream(const std::string &name, uint32_t user_id, uint32_t type_id, uint32_t file_version_id)
Creates and gets a extension stream to write information into an extension.
ADTFDatFileWriter(const std::string &file_name, std::chrono::nanoseconds history_duration, StreamTypeSerializers type_serializers, TargetADTFVersion target_adtf_version=adtf3ns, uint32_t writer_flags=0)
CTOR for a new ADTF file for writing.
void writeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp)
Writes a trigger to the file or the history.
TargetADTFVersion
Target Verion of the ADTF file to write.
Definition adtf_file_writer.h:305
@ adtf3ns
target version is ADTF 3 file, ontaining samples, triggers and stream types with nanosecond resolutio...
Definition adtf_file_writer.h:320
@ adtf3
target version is ADTF 3 file, containing samples, triggers and stream types.
Definition adtf_file_writer.h:315
@ adtf2
target version is ADTF 2 file, containing samples only.
Definition adtf_file_writer.h:310
void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType &type)
Writes a type to the file or the history.
ADTFDatFileWriter(ADTFDatFileWriter &&)=default
move CTOR
base interface class for all available objects of the ADTF File Library that can be added to the obje...
Definition object.h:38
Interface class for an output stream.
Definition adtf_file_writer.h:39
OutputStream & operator<<(const T &value)
template operator to write a standard layout type to the output stream
Definition adtf_file_writer.h:55
virtual void write(const void *data, size_t data_size)=0
Writes the data to the output stream.
The default interface class for stream types.
Definition stream_type.h:32
Container class for SampleSerializerFactory.
Definition adtf_file_writer.h:233
void add(const std::shared_ptr< SampleSerializerFactory > &serializer_factory)
Adds a SampleSerializerFactory instance to the container.
Definition adtf_file_writer.h:240
std::shared_ptr< SampleSerializer > build(const std::string &id) const
Create an instance of a SampleSerializer for the given id.
Definition adtf_file_writer.h:251
interface class for sample serializer
Definition adtf_file_writer.h:196
virtual std::string getId() const =0
Get the id of the serializer.
virtual std::shared_ptr< SampleSerializer > build() const =0
Create an instance of a SampleSerializer.
Interface class for a sample serializer. Usually the implementaion has a serialization state dependin...
Definition adtf_file_writer.h:171
virtual void serialize(const WriteSample &sample, OutputStream &stream)=0
Serializes the sample into the given stream. These samples usually are described by the previous set ...
virtual void setStreamType(const StreamType &stream_type)=0
Set the Stream Type and the serialization state of the serializer.
virtual std::string getId() const =0
Get the Id of the sample serializer.
interface class for stream type serializers.
Definition adtf_file_writer.h:75
virtual std::string getMetaType() const =0
Get the Meta Type name for which the serializer serializes stream types.
virtual void serialize(const StreamType &stream_type, OutputStream &stream) const =0
serializes the given stream type into the output stream
Container class for StreamTypeSerializer.
Definition adtf_file_writer.h:107
void add(const std::shared_ptr< StreamTypeSerializer > &serializer)
Adds a StreamTypeSerializer instance to the container.
Definition adtf_file_writer.h:114
std::string getAdtf2TypeId(const StreamType &stream_type)
Get the ADTF 2 type id of the stream_type.
Definition adtf_file_writer.h:149
void serialize(const StreamType &stream_type, OutputStream &stream) const
Serialzes the given stream type into the given stream. It will use the StreamTypeSerializer supportin...
Definition adtf_file_writer.h:126
Stream Type base class.
Definition stream_item.h:43
Interface class for samples that are written.
Definition sample.h:116
default implementation template for SampleSerializerFactory to create an instance of the serialzer ty...
Definition adtf_file_writer.h:216
std::string getId() const override
Get the id of the serializer.
Definition adtf_file_writer.h:218
std::shared_ptr< SampleSerializer > build() const override
Create an instance of a SampleSerializer.
Definition adtf_file_writer.h:223
namespace for ADTF File library
Definition adtf2_adtf_core_media_sample_deserializer.h:25