rogue
Loading...
Searching...
No Matches
StreamWriter.h
Go to the documentation of this file.
1
33#ifndef __ROGUE_UTILITIES_FILEIO_STREAM_WRITER_H__
34#define __ROGUE_UTILITIES_FILEIO_STREAM_WRITER_H__
35#include "rogue/Directives.h"
36
37#include <stdint.h>
38
39#include <chrono>
40#include <condition_variable>
41#include <deque>
42#include <map>
43#include <memory>
44#include <mutex>
45#include <string>
46#include <thread>
47#include <utility>
48
50#include "rogue/Logging.h"
52
53namespace rogue {
54namespace utilities {
55namespace fileio {
56
57class StreamWriterChannel;
58
76class StreamWriter : public rogue::EnableSharedFromThis<rogue::utilities::fileio::StreamWriter> {
77 friend class StreamWriterChannel;
78
79 protected:
80 // Log
81 std::shared_ptr<rogue::Logging> log_;
82
83 // Active output file descriptor.
84 int32_t fd_;
85
86 // Base output file path.
87 std::string baseName_;
88
89 // True when writer is currently open for output.
90 bool isOpen_;
91
92 // Current file index for rollover mode.
93 uint32_t fdIdx_;
94
95 // Auto-rollover size limit in bytes. Zero disables rollover.
96 uint64_t sizeLimit_;
97
98 // Bytes written to current file.
99 uint64_t currSize_;
100
101 // Total bytes written across all files since open().
102 uint64_t totSize_;
103
104 // Write-buffer size in bytes. Zero disables buffering.
105 uint32_t buffSize_;
106
107 // Write buffer storage.
108 uint8_t* buffer_;
109
110 // Number of bytes currently staged in write buffer.
111 uint32_t currBuffer_;
112
113 // Drop frames with non-zero error field when true.
115
116 // Lock for writer state and counters.
117 std::mutex mtx_;
118
119 // Number of frames written since open().
120 uint32_t frameCount_;
121
122 // Byte total in sliding one-second bandwidth window.
124
125 // Time-tagged write samples for bandwidth calculation.
126 std::deque<std::pair<std::chrono::steady_clock::time_point, uint32_t>> bandwidthHistory_;
127
128 // Writes bytes with optional staging buffer.
129 void intWrite(void* data, uint32_t size);
130
131 // Applies max-size rollover checks before write.
132 void checkSize(uint32_t size);
133
134 // Flushes staged bytes to file descriptor.
135 void flush();
136
137 // Adds a write sample to bandwidth statistics.
138 void recordBandwidth(uint32_t size);
139
140 // Removes samples older than one second.
141 void pruneBandwidth(std::chrono::steady_clock::time_point now);
142
143 // Writes payload-only format when true.
144 bool raw_;
145
146 // Condition variable for frame-count waiters.
147 std::condition_variable cond_;
148
149 std::map<uint32_t, std::shared_ptr<rogue::utilities::fileio::StreamWriterChannel>> channelMap_;
150
151 // Writes one frame to file. Called by StreamWriterChannel.
152 virtual void writeFile(uint8_t channel, std::shared_ptr<rogue::interfaces::stream::Frame> frame);
153
154 public:
165 static std::shared_ptr<rogue::utilities::fileio::StreamWriter> create();
166
168 static void setup_python();
169
177 StreamWriter();
178
180 virtual ~StreamWriter();
181
192 void open(std::string file);
193
195 void close();
196
201 bool isOpen();
202
212 void setRaw(bool raw);
213
218 bool getRaw();
219
229 void setBufferSize(uint32_t size);
230
240 void setMaxSize(uint64_t size);
241
250 void setDropErrors(bool drop);
251
262 std::shared_ptr<rogue::utilities::fileio::StreamWriterChannel> getChannel(uint8_t channel);
263
268 uint64_t getTotalSize();
269
274 uint64_t getCurrentSize();
275
280 double getBandwidth();
281
286 uint32_t getFrameCount();
287
294 bool waitFrameCount(uint32_t count, uint64_t timeout);
295};
296
297// Convenience
298typedef std::shared_ptr<rogue::utilities::fileio::StreamWriter> StreamWriterPtr;
299} // namespace fileio
300} // namespace utilities
301} // namespace rogue
302#endif
Typed shared-from-this helper for Rogue classes.
Stream sink that writes incoming frames into a tagged writer channel.
Coordinates channelized frame capture into Rogue stream data files.
bool waitFrameCount(uint32_t count, uint64_t timeout)
Blocks until a target frame count is reached or timeout expires.
void setMaxSize(uint64_t size)
Sets automatic file rollover size.
bool getRaw()
Gets raw output mode state.
void close()
Closes the currently open data file.
StreamWriter()
Constructs a stream writer.
void pruneBandwidth(std::chrono::steady_clock::time_point now)
bool isOpen()
Returns whether a data file is open.
void setBufferSize(uint32_t size)
Sets write buffering size.
uint64_t getTotalSize()
Returns cumulative bytes written across all files.
virtual void writeFile(uint8_t channel, std::shared_ptr< rogue::interfaces::stream::Frame > frame)
Write data to file. Called from StreamWriterChannel.
void setDropErrors(bool drop)
Configures whether errored frames are dropped.
virtual ~StreamWriter()
Destroys the stream writer and closes open resources.
void intWrite(void *data, uint32_t size)
Internal method for file writing with buffer and auto close and reopen.
std::deque< std::pair< std::chrono::steady_clock::time_point, uint32_t > > bandwidthHistory_
void open(std::string file)
Opens a data file.
static std::shared_ptr< rogue::utilities::fileio::StreamWriter > create()
Creates a stream writer instance.
static void setup_python()
Registers Python bindings for this class.
std::shared_ptr< rogue::utilities::fileio::StreamWriterChannel > getChannel(uint8_t channel)
Gets or creates a channel writer endpoint.
uint32_t getFrameCount()
Returns number of frames written to the current file session.
uint64_t getCurrentSize()
Returns current output file size.
std::map< uint32_t, std::shared_ptr< rogue::utilities::fileio::StreamWriterChannel > > channelMap_
double getBandwidth()
Returns recent write bandwidth estimate.
void checkSize(uint32_t size)
Check file size for next write.
void setRaw(bool raw)
Sets raw output mode.
std::shared_ptr< rogue::Logging > log_
std::shared_ptr< rogue::utilities::fileio::StreamWriter > StreamWriterPtr