MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PosixContinuousFlowReader.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 <atomic>
6#include <filesystem>
7#include <system_error>
8#include <sys/stat.h>
10#include "mxl-internal/Sync.hpp"
11
12namespace mxl::lib
13{
15 std::unique_ptr<ContinuousFlowData>&& data)
17 , _flowData{std::move(data)}
18 , _channelCount{_flowData->channelCount()}
19 , _bufferLength{_flowData->channelBufferLength()}
20 {
21 if (!checkPermissions())
22 {
23 throw std::filesystem::filesystem_error{"Flow is not accessible due to insufficient permissions.",
24 makeFlowDescriptorFilePath(getDomain(), uuids::to_string(getId())),
25 std::make_error_code(std::errc::permission_denied)};
26 }
27 }
28
30 {
31 if (_flowData)
32 {
33 return *_flowData;
34 }
35 throw std::runtime_error("No open flow.");
36 }
37
42
47
52
54 {
55 return _bufferLength / 2U;
56 }
57
59 {
60 if (_flowData)
61 {
62 auto const result = getSamplesImpl(index, 0U, deadline, nullptr);
63
64 // If we were ultimately too early, even with blocking for a
65 // certain amount of time it could very well be that we're
66 // operating on a stale flow, so we use the opportunity to check
67 // whether it's valid.
68 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
69 }
70
71 return MXL_ERR_UNKNOWN;
72 }
73
75 mxlWrappedMultiBufferSlice& payloadBuffersSlices)
76 {
77 if (_flowData)
78 {
79 auto const result = getSamplesImpl(index, count, deadline, &payloadBuffersSlices);
80
81 // If we were ultimately too early, even with blocking for a
82 // certain amount of time it could very well be that we're
83 // operating on a stale flow, so we use the opportunity to check
84 // whether it's valid.
85 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
86 }
87
88 return MXL_ERR_UNKNOWN;
89 }
90
92 {
93 if (_flowData)
94 {
95 auto const result = getSamplesImpl(index, count, &payloadBuffersSlices);
96
97 // If we were too early it could very well be that we're operating
98 // on a stale flow, so we use the opportunity to check whether it's
99 // valid.
100 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
101 }
102
103 return MXL_ERR_UNKNOWN;
104 }
105
107 {
108 return _flowData && isFlowValidImpl();
109 }
110
112 {
113 auto const flowState = _flowData->flowState();
114 auto const flowDataPath = makeFlowDataFilePath(getDomain(), to_string(getId()));
115
116 struct stat st;
117 if (::stat(flowDataPath.string().c_str(), &st) != 0)
118 {
119 return false;
120 }
121 return (st.st_ino == flowState->inode);
122 }
123
125 mxlWrappedMultiBufferSlice* payloadBuffersSlices) const
126 {
127 if (auto const headIndex = _flowData->flowInfo()->runtime.headIndex; index <= headIndex)
128 {
129 auto const minIndex = (headIndex >= (_bufferLength / 2U)) ? (headIndex - (_bufferLength / 2U)) : std::uint64_t{0};
130
131 if ((index >= minIndex) && ((index - minIndex) >= count))
132 {
133 if (payloadBuffersSlices != nullptr)
134 {
135 auto const startOffset = (index + _bufferLength - count) % _bufferLength;
136 auto const endOffset = (index % _bufferLength);
137
138 auto const firstLength = (startOffset < endOffset) ? count : _bufferLength - startOffset;
139 auto const secondLength = count - firstLength;
140
141 auto const baseBufferPtr = static_cast<std::uint8_t const*>(_flowData->channelData());
142 auto const sampleWordSize = _flowData->sampleWordSize();
143
144 payloadBuffersSlices->base.fragments[0].pointer = baseBufferPtr + sampleWordSize * startOffset;
145 payloadBuffersSlices->base.fragments[0].size = sampleWordSize * firstLength;
146
147 payloadBuffersSlices->base.fragments[1].pointer = baseBufferPtr;
148 payloadBuffersSlices->base.fragments[1].size = sampleWordSize * secondLength;
149
150 payloadBuffersSlices->stride = sampleWordSize * _bufferLength;
151 payloadBuffersSlices->count = _channelCount;
152 }
153
154 return MXL_STATUS_OK;
155 }
156
158 }
159
161 }
162
164 mxlWrappedMultiBufferSlice* payloadBuffersSlices) const
165 {
166 auto const flow = _flowData->flow();
167 auto const syncObject = std::atomic_ref{flow->state.syncCounter};
168 while (true)
169 {
170 auto const previousSyncCounter = syncObject.load(std::memory_order_acquire);
171 auto const result = getSamplesImpl(index, count, payloadBuffersSlices);
172 // NOTE: Before C++26 there is no way to access the address of the object wrapped
173 // by an atomic_ref. If there were it would be much more appropriate to pass
174 // syncObject by reference here and only unwrap the underlying integer in the
175 // implementation of waitUntilChanged.
176 if ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || !waitUntilChanged(&flow->state.syncCounter, previousSyncCounter, deadline))
177 {
178 return result;
179 }
180 }
181 }
182}
Paths and filenames utilities used by the MXL SDK.
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
bool checkPermissions() const
uuids::uuid const & getId() const
std::filesystem::path const & getDomain() const
virtual mxlFlowConfigInfo getFlowConfigInfo() const override
std::unique_ptr< ContinuousFlowData > _flowData
virtual mxlStatus getSamples(std::uint64_t index, std::size_t count, Timepoint deadline, mxlWrappedMultiBufferSlice &payloadBuffersSlices) override
virtual FlowData const & getFlowData() const override
PosixContinuousFlowReader(FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< ContinuousFlowData > &&data)
mxlStatus getSamplesImpl(std::uint64_t index, std::size_t count, mxlWrappedMultiBufferSlice *payloadBuffersSlices) const
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
virtual mxlFlowInfo getFlowInfo() const override
virtual mxlStatus waitForSamples(std::uint64_t index, Timepoint deadline) const override
virtual std::size_t getMaxReadLength() const override
struct mxlWrappedMultiBufferSlice_t mxlWrappedMultiBufferSlice
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_FLOW_INVALID
Definition mxl.h:41
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_OUT_OF_RANGE_TOO_EARLY
Definition mxl.h:31
@ MXL_STATUS_OK
Definition mxl.h:23
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30
std::filesystem::path makeFlowDescriptorFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:23
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
size_t size
Definition flow.h:55
void const * pointer
Definition flow.h:53
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229
mxlFlowConfigInfo config
Definition flowinfo.h:227
mxlBufferSlice fragments[2]
Definition flow.h:77
mxlWrappedBufferSlice base
Definition flow.h:97
auto const flowId
uint64_t index