MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FlowSynchronizationGroup.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 <mxl/time.h>
9
10namespace mxl::lib
11{
13 : reader{&reader}
17 {
18 auto const configInfo = reader.getFlowConfigInfo();
19 grainRate = configInfo.common.grainRate;
20 }
21
27
31
32 void FlowSynchronizationGroup::addReader(DiscreteFlowReader const& reader, std::uint16_t minValidSlices)
33 {
34 auto prev = _readers.before_begin();
35 for (auto current = std::next(prev); current != _readers.end(); ++current)
36 {
37 if (current->reader == &reader)
38 {
39 current->minValidSlices = minValidSlices;
40 return;
41 }
42 prev = current;
43 }
44
45 _readers.emplace_after(prev, reader, minValidSlices);
46 }
47
49 {
50 auto prev = _readers.before_begin();
51 for (auto current = std::next(prev); current != _readers.end(); ++current)
52 {
53 if (current->reader == &reader)
54 {
55 return;
56 }
57 prev = current;
58 }
59
60 _readers.emplace_after(prev, reader);
61 }
62
64 {
65 auto prev = _readers.before_begin();
66 for (auto current = std::next(prev); current != _readers.end(); ++current)
67 {
68 if (current->reader == &reader)
69 {
70 _readers.erase_after(prev);
71 return;
72 }
73 prev = current;
74 }
75 }
76
78 {
79 auto prev = _readers.before_begin();
80 for (auto it = _readers.begin(); it != _readers.end(); /* Nothing */)
81 {
82 auto const current = it++;
83
84 auto const expectedIndex = timestampToIndex(current->grainRate, originTime);
85 auto const runtimeInfo = current->reader->getFlowRuntimeInfo();
86 if (expectedIndex > runtimeInfo.headIndex)
87 {
88 auto result = mxlStatus{MXL_ERR_UNKNOWN};
89 switch (current->variant)
90 {
92 result =
93 static_cast<DiscreteFlowReader const*>(current->reader)->waitForGrain(expectedIndex, current->minValidSlices, deadline);
94 break;
95
97 result = static_cast<ContinuousFlowReader const*>(current->reader)->waitForSamples(expectedIndex, deadline);
98 break;
99 }
100
101 if (result == MXL_STATUS_OK)
102 {
103 // If the current source delay of this flow exceeds any previously observed source delay of
104 // this flow we update the cached maximum and if this new maximum turns out to be bigger than
105 // the maximum source delay observed for the flow at the head of the list, we move this flow
106 // to the front, hoping that we can save blocking waits in the future.
107 auto const expectedArrivalTime = indexToTimestamp(current->grainRate, expectedIndex);
108 auto const currentTaiTime = currentTime(Clock::TAI);
109 if (currentTaiTime > expectedArrivalTime)
110 {
111 auto const sourceDelay = (currentTaiTime - expectedArrivalTime).value;
112 if (sourceDelay > current->maxObservedSourceDelay)
113 {
114 current->maxObservedSourceDelay = sourceDelay;
115 if (current->maxObservedSourceDelay > _readers.begin()->maxObservedSourceDelay)
116 {
117 // `splice_after` moves the element *following* the iterator it is given.
118 _readers.splice_after(_readers.before_begin(), _readers, prev);
119 continue; // `prev` already precedes `it` after the splice.
120 }
121 }
122 }
123 }
124 else
125 {
126 return result;
127 }
128 }
129 prev = current;
130 }
131 return MXL_STATUS_OK;
132 }
133}
void addReader(DiscreteFlowReader const &reader, std::uint16_t minValidSlices)
void removeReader(FlowReader const &reader)
mxlStatus waitForDataAt(Timepoint originTime, Timepoint deadline) const
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_STATUS_OK
Definition mxl.h:23
constexpr Timepoint indexToTimestamp(mxlRational const &editRate, std::uint64_t index) noexcept
constexpr std::uint64_t timestampToIndex(mxlRational const &editRate, Timepoint timestamp) noexcept
ListEntry(DiscreteFlowReader const &reader, std::uint16_t minValidSlices)
mxlFlowConfigInfo configInfo
mxlFlowReader reader
auto const currentTime
Definition test_time.cpp:58