MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PosixContinuousFlowWriter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
5#include <cstring>
6#include <algorithm>
7#include <filesystem>
8#include <stdexcept>
9#include <system_error>
10#include <mxl/time.h>
11#include "mxl-internal/Sync.hpp"
12
13namespace mxl::lib
14{
16 std::unique_ptr<ContinuousFlowData>&& data)
17 : ContinuousFlowWriter{flowId, manager.getDomain()}
18 , _flowData{std::move(data)}
19 , _channelCount{_flowData->channelCount()}
20 , _bufferLength{_flowData->channelBufferLength()}
22 , _currentCount{0U}
24 , _syncBatchSize{1U}
27 {
28 if (!checkPermissions())
29 {
30 throw std::filesystem::filesystem_error{
31 "Flow is not accessible due to insufficient permissions.", manager.getDomain(), std::make_error_code(std::errc::permission_denied)};
32 }
33 if (_flowData)
34 {
35 auto const& commonFlowConfigInfo = _flowData->flowInfo()->config.common;
36
37 auto const commitBatchSize = std::max(commonFlowConfigInfo.maxCommitBatchSizeHint, 1U);
38 _syncBatchSize = std::max(commonFlowConfigInfo.maxSyncBatchSizeHint, 1U);
39 _earlySyncThreshold = (_syncBatchSize >= commitBatchSize) ? (_syncBatchSize - commitBatchSize) : 0U;
40 }
41 }
42
44 {
45 if (_flowData)
46 {
47 return *_flowData;
48 }
49 throw std::runtime_error("No open flow.");
50 }
51
53 {
54 if (_flowData)
55 {
56 return *_flowData;
57 }
58 throw std::runtime_error("No open flow.");
59 }
60
65
70
75
77 {
78 return _bufferLength / 2U;
79 }
80
82 {
83 if (_flowData)
84 {
85 if (count <= (_bufferLength / 2))
86 {
87 if (index < count)
88 {
90 }
91
92 auto const writeStartIndex = index - count;
94 {
95 if ((index <= _lastCommittedIndex) || (writeStartIndex < _lastCommittedIndex))
96 {
98 }
99 }
100
101 auto const startOffset = (index + _bufferLength - count) % _bufferLength;
102 auto const endOffset = (index % _bufferLength);
103
104 auto const firstLength = (startOffset < endOffset) ? count : _bufferLength - startOffset;
105 auto const secondLength = count - firstLength;
106
107 auto const baseBufferPtr = static_cast<std::uint8_t*>(_flowData->channelData());
108 auto const sampleWordSize = _flowData->sampleWordSize();
109
110 payloadBufferSlices.base.fragments[0].pointer = baseBufferPtr + sampleWordSize * startOffset;
111 payloadBufferSlices.base.fragments[0].size = sampleWordSize * firstLength;
112
113 payloadBufferSlices.base.fragments[1].pointer = baseBufferPtr;
114 payloadBufferSlices.base.fragments[1].size = sampleWordSize * secondLength;
115
116 payloadBufferSlices.stride = sampleWordSize * _bufferLength;
117 payloadBufferSlices.count = _channelCount;
118
121
122 return MXL_STATUS_OK;
123 }
124
125 return MXL_ERR_INVALID_ARG;
126 }
127 return MXL_ERR_UNKNOWN;
128 }
129
131 {
132 if (_flowData)
133 {
135 {
136 return MXL_ERR_INVALID_ARG;
137 }
138
139 // Invalidate any skipped samples in the writable ring. This prevents readers from
140 // seeing stale payload data when the producer introduces a write gap.
141 auto const writeStartIndex = _currentIndex - _currentCount;
143 {
144 auto const skippedCount = writeStartIndex - _lastCommittedIndex;
145 auto const clearableLength = _bufferLength - _currentCount;
146 auto const clearedCount = std::min<std::uint64_t>(skippedCount, clearableLength);
147 auto const gapEndIndex = writeStartIndex;
148
149 auto const clearStartOffset = (gapEndIndex + _bufferLength - clearedCount) % _bufferLength;
150 auto const clearEndOffset = gapEndIndex % _bufferLength;
151 auto const firstClearLength = (clearStartOffset < clearEndOffset) ? clearedCount : _bufferLength - clearStartOffset;
152 auto const secondClearLength = clearedCount - firstClearLength;
153
154 auto const sampleWordSize = _flowData->sampleWordSize();
155 auto const channelStride = sampleWordSize * _bufferLength;
156 auto* const baseBufferPtr = static_cast<std::uint8_t*>(_flowData->channelData());
157
158 for (auto channel = std::size_t{0}; channel < _channelCount; ++channel)
159 {
160 auto* const channelBasePtr = baseBufferPtr + (channel * channelStride);
161 std::memset(channelBasePtr + (sampleWordSize * clearStartOffset), 0, sampleWordSize * firstClearLength);
162 std::memset(channelBasePtr, 0, sampleWordSize * secondClearLength);
163 }
164 }
165
166 auto const flow = _flowData->flow();
167 flow->info.runtime.headIndex = _currentIndex;
170 _currentCount = 0U;
171
173 {
174 // Let readers know that the head has moved
175 flow->state.syncCounter++;
176 wakeAll(&flow->state.syncCounter);
177 }
178
179 return MXL_STATUS_OK;
180 }
181 else
182 {
184 return MXL_ERR_UNKNOWN;
185 }
186 }
187
194
196 {
197 auto const flow = _flowData->flow();
198 auto const currentSyncSampleBatch = flow->info.runtime.headIndex / _syncBatchSize;
199 if (currentSyncSampleBatch < _lastSyncSampleBatch)
200 {
201 return false;
202 }
203 if (currentSyncSampleBatch == _lastSyncSampleBatch)
204 {
205 // Signal now before overshooting the maximum the next time around
206 if ((flow->info.runtime.headIndex % _syncBatchSize) > _earlySyncThreshold)
207 {
208 _lastSyncSampleBatch = currentSyncSampleBatch + 1U;
209 }
210 else
211 {
212 return false;
213 }
214 }
215 else
216 {
217 _lastSyncSampleBatch = currentSyncSampleBatch;
218 }
219
220 return true;
221 }
222
224 {
225 if (!_flowData)
226 {
227 throw std::runtime_error("Flow writer not initialized");
228 }
229
230 return _flowData->isExclusive();
231 }
232
234 {
235 if (!_flowData)
236 {
237 throw std::runtime_error("Flow writer not initialized");
238 }
239
240 return _flowData->makeExclusive();
241 }
242}
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
std::filesystem::path const & getDomain() const
bool checkPermissions() const
PosixContinuousFlowWriter(FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< ContinuousFlowData > &&data)
virtual mxlFlowInfo getFlowInfo() const override
virtual mxlStatus openSamples(std::uint64_t index, std::size_t count, mxlMutableWrappedMultiBufferSlice &payloadBufferSlices) override
std::unique_ptr< ContinuousFlowData > _flowData
virtual std::size_t getMaxWriteLength() const override
virtual mxlFlowConfigInfo getFlowConfigInfo() const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
struct mxlMutableWrappedMultiBufferSlice_t mxlMutableWrappedMultiBufferSlice
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_STATUS_OK
Definition mxl.h:23
void wakeAll(T const *in_addr)
Definition Sync.cpp:131
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229
mxlFlowConfigInfo config
Definition flowinfo.h:227
mxlMutableBufferSlice fragments[2]
Definition flow.h:87
mxlMutableWrappedBufferSlice base
Definition flow.h:117
auto const flowId
uint64_t index