MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_flow_sync_groups.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
4#include <chrono>
5#include <cstdint>
6#include <thread>
7#include <catch2/catch_test_macros.hpp>
8#include <mxl/flow.h>
9#include <mxl/mxl.h>
10#include <mxl/time.h>
11#include "Utils.hpp"
12
13namespace
14{
15 constexpr auto VIDEO_FLOW_ID = "5fbec3b1-1b0f-417d-9059-8b94a47197ed";
16 constexpr auto DATA_FLOW_ID = "db3bd465-2772-484f-8fac-830b0471258b";
17
20 constexpr auto GRAIN_RATE = mxlRational{30000, 1001};
21
24 mxlStatus commitGrainAfter(mxlFlowWriter writer, std::uint64_t index, std::chrono::milliseconds delay)
25 {
26 std::this_thread::sleep_for(delay);
27
28 mxlGrainInfo info;
29 std::uint8_t* buffer = nullptr;
30 if (auto const status = mxlFlowWriterOpenGrain(writer, index, &info, &buffer); status != MXL_STATUS_OK)
31 {
32 return status;
33 }
34 info.validSlices = info.totalSlices;
35 return mxlFlowWriterCommitGrain(writer, &info);
36 }
37}
38
47TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Synchronization group : Repeated waits", "[mxl sync groups]")
48{
49 auto const opts = "{}";
50
51 auto instance = mxlCreateInstance(domain.string().c_str(), opts);
52 REQUIRE(instance != nullptr);
53
54 // Two discrete flows sharing a grain rate. The data flow is written late on purpose.
55 mxlFlowWriter videoWriter;
56 mxlFlowWriter dataWriter;
58 bool created = false;
59 REQUIRE(
60 mxlCreateFlowWriter(instance, mxl::tests::readFile("data/v210_flow.json").c_str(), "", &videoWriter, &configInfo, &created) == MXL_STATUS_OK);
61 REQUIRE(
62 mxlCreateFlowWriter(instance, mxl::tests::readFile("data/data_flow.json").c_str(), "", &dataWriter, &configInfo, &created) == MXL_STATUS_OK);
63
64 mxlFlowReader videoReader;
65 mxlFlowReader dataReader;
66 REQUIRE(mxlCreateFlowReader(instance, VIDEO_FLOW_ID, "", &videoReader) == MXL_STATUS_OK);
67 REQUIRE(mxlCreateFlowReader(instance, DATA_FLOW_ID, "", &dataReader) == MXL_STATUS_OK);
68
73
74 // First wait: both grains arrive, the second member later than the first.
75 auto const firstIndex = mxlTimestampToIndex(&GRAIN_RATE, mxlGetTime()) + 1;
76 auto const firstTimestamp = mxlIndexToTimestamp(&GRAIN_RATE, firstIndex);
77
78 auto videoStatus = MXL_ERR_UNKNOWN;
79 auto dataStatus = MXL_ERR_UNKNOWN;
80 auto videoThread = std::thread{[&]
81 {
82 videoStatus = commitGrainAfter(videoWriter, firstIndex, std::chrono::milliseconds{50});
83 }};
84 auto dataThread = std::thread{[&]
85 {
86 dataStatus = commitGrainAfter(dataWriter, firstIndex, std::chrono::milliseconds{250});
87 }};
88
89 auto const firstWaitStatus = mxlFlowSynchronizationGroupWaitForDataAt(group, firstTimestamp, 2'000'000'000ULL);
90
91 videoThread.join();
92 dataThread.join();
93
94 REQUIRE(videoStatus == MXL_STATUS_OK);
95 REQUIRE(dataStatus == MXL_STATUS_OK);
96 REQUIRE(firstWaitStatus == MXL_STATUS_OK);
97
98 // Second wait, on the same group, for a grain that nobody will ever write. The group
99 // must block until the timeout expires instead of reporting the data as available.
100 auto const missingTimestamp = mxlIndexToTimestamp(&GRAIN_RATE, firstIndex + 1000);
101
102 auto const start = std::chrono::steady_clock::now();
103 auto const secondWaitStatus = mxlFlowSynchronizationGroupWaitForDataAt(group, missingTimestamp, 200'000'000ULL);
104 auto const elapsed = std::chrono::steady_clock::now() - start;
105
106 CHECK(secondWaitStatus != MXL_STATUS_OK);
107 CHECK(elapsed >= std::chrono::milliseconds{150});
108
110 REQUIRE(mxlReleaseFlowReader(instance, videoReader) == MXL_STATUS_OK);
111 REQUIRE(mxlReleaseFlowReader(instance, dataReader) == MXL_STATUS_OK);
112 REQUIRE(mxlReleaseFlowWriter(instance, videoWriter) == MXL_STATUS_OK);
113 REQUIRE(mxlReleaseFlowWriter(instance, dataWriter) == MXL_STATUS_OK);
114 mxlDestroyInstance(instance);
115}
Public flow reader, writer, payload and synchronization APIs.
MXL_EXPORT mxlStatus mxlFlowWriterOpenGrain(mxlFlowWriter writer, uint64_t index, mxlGrainInfo *mxlGrainInfo, uint8_t **payload)
Definition flow.cpp:446
MXL_EXPORT mxlStatus mxlReleaseFlowReader(mxlInstance instance, mxlFlowReader reader)
Definition flow.cpp:177
struct mxlFlowReader_t * mxlFlowReader
Definition flow.h:205
struct mxlGrainInfo_t mxlGrainInfo
MXL_EXPORT mxlStatus mxlFlowSynchronizationGroupWaitForDataAt(mxlFlowSynchronizationGroup group, uint64_t timestamp, uint64_t timeoutNs)
Definition flow.cpp:893
MXL_EXPORT mxlStatus mxlCreateFlowSynchronizationGroup(mxlInstance instance, mxlFlowSynchronizationGroup *group)
Definition flow.cpp:770
MXL_EXPORT mxlStatus mxlReleaseFlowSynchronizationGroup(mxlInstance instance, mxlFlowSynchronizationGroup group)
Definition flow.cpp:791
MXL_EXPORT mxlStatus mxlFlowSynchronizationGroupAddReader(mxlFlowSynchronizationGroup group, mxlFlowReader reader)
Definition flow.cpp:813
struct mxlFlowWriter_t * mxlFlowWriter
Definition flow.h:206
struct mxlFlowSynchronizationGroup_t * mxlFlowSynchronizationGroup
Definition flow.h:208
MXL_EXPORT mxlStatus mxlReleaseFlowWriter(mxlInstance instance, mxlFlowWriter writer)
Definition flow.cpp:262
MXL_EXPORT mxlStatus mxlCreateFlowWriter(mxlInstance instance, char const *flowDef, char const *options, mxlFlowWriter *writer, mxlFlowConfigInfo *configInfo, bool *created)
Definition flow.cpp:198
MXL_EXPORT mxlStatus mxlCreateFlowReader(mxlInstance instance, char const *flowId, char const *options, mxlFlowReader *reader)
Definition flow.cpp:136
MXL_EXPORT mxlStatus mxlFlowWriterCommitGrain(mxlFlowWriter writer, mxlGrainInfo const *grain)
Definition flow.cpp:487
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
MXL_EXPORT mxlStatus mxlDestroyInstance(mxlInstance in_instance)
Definition mxl.cpp:106
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_STATUS_OK
Definition mxl.h:23
MXL_EXPORT mxlInstance mxlCreateInstance(char const *in_mxlDomain, char const *in_options)
Definition mxl.cpp:45
std::string readFile(std::filesystem::path const &filepath)
Definition Utils.hpp:78
struct mxlRational_t mxlRational
uint16_t totalSlices
Definition flow.h:146
uint16_t validSlices
Definition flow.h:149
TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Synchronization group : Repeated waits", "[mxl sync groups]")
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.
mxlFlowConfigInfo configInfo
mxlFlowWriter writer
uint64_t index
REQUIRE(timeDelta< 33 '366 '667LL)
MXL_EXPORT uint64_t mxlIndexToTimestamp(mxlRational const *editRate, uint64_t index)
Definition time.cpp:38
MXL_EXPORT uint64_t mxlTimestampToIndex(mxlRational const *editRate, uint64_t timestamp)
Definition time.cpp:31
MXL_EXPORT uint64_t mxlGetTime(void)
Definition time.cpp:11