The test simulates trying to read video data from the current head, while the writer is still 3 grains behind (~100 ms at the 30000/1001 rate).
18 : Wait
for grain availability
", "[
mxl flows timing]
")
19{
20 auto const opts = "{}";
21 auto instanceReader = mxlCreateInstance(domain.string().c_str(), opts);
22 REQUIRE(instanceReader != nullptr);
23
24 auto instanceWriter = mxlCreateInstance(domain.string().c_str(), opts);
25 REQUIRE(instanceWriter != nullptr);
26
27 auto flowDef = mxl::tests::readFile("data/v210_flow.json");
28 mxlFlowWriter writer;
29 mxlFlowConfigInfo configInfo;
30 bool flowWasCreated = false;
31 REQUIRE(mxlCreateFlowWriter(instanceWriter, flowDef.c_str(), opts, &writer, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
32 REQUIRE(flowWasCreated);
33
34 mxlFlowReader reader;
35 auto const flowId = uuids::to_string(configInfo.common.id);
36 REQUIRE(mxlCreateFlowReader(instanceReader, flowId.c_str(), "", &reader) == MXL_STATUS_OK);
37
38 auto const readerGrainIndex = mxlGetCurrentIndex(&configInfo.common.grainRate);
39 auto const frameDurationNs = 1000000000 * configInfo.common.grainRate.denominator / configInfo.common.grainRate.numerator;
40 auto writerThread = std::thread{[readerGrainIndex, frameDurationNs, writer]
41 {
42 constexpr auto writerLatencyGrains = 3;
43 for (std::uint64_t writerGrainIndex = readerGrainIndex - writerLatencyGrains; writerGrainIndex <= readerGrainIndex; ++writerGrainIndex)
44 {
45 mxlGrainInfo gInfo;
46 std::uint8_t* buffer = nullptr;
47 REQUIRE(mxlFlowWriterOpenGrain(writer, writerGrainIndex, &gInfo, &buffer) == MXL_STATUS_OK);
48 REQUIRE(buffer != nullptr);
49 memcpy(buffer, &writerGrainIndex, sizeof(std::uint64_t));
50 gInfo.validSlices = gInfo.totalSlices;
51 REQUIRE(mxlFlowWriterCommitGrain(writer, &gInfo) == MXL_STATUS_OK);
52 if (writerGrainIndex < readerGrainIndex)
53 {
54 mxlSleepForNs(frameDurationNs);
55 }
56 }
57 }};
58
59 mxlGrainInfo gInfo;
60 std::uint8_t* buffer = nullptr;
61 constexpr auto oneS = 1000000000;
62 REQUIRE(mxlFlowReaderGetGrain(reader, readerGrainIndex, oneS, &gInfo, &buffer) == MXL_STATUS_OK);
63 REQUIRE(buffer != nullptr);
64 REQUIRE(gInfo.validSlices == gInfo.totalSlices);
65 std::uint64_t obtainedGrainIndex = 0;
66 memcpy(&obtainedGrainIndex, buffer, sizeof(std::uint64_t));
67 REQUIRE(readerGrainIndex == obtainedGrainIndex);
68 writerThread.join();
69
70 REQUIRE(mxlReleaseFlowReader(instanceReader, reader) == MXL_STATUS_OK);
71 REQUIRE(mxlReleaseFlowWriter(instanceWriter, writer) == MXL_STATUS_OK);
72 REQUIRE(mxlDestroyInstance(instanceReader) == MXL_STATUS_OK);
73 REQUIRE(mxlDestroyInstance(instanceWriter) == MXL_STATUS_OK);
74}