-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFFFRTestSeek.cpp
300 lines (278 loc) · 10.3 KB
/
FFFRTestSeek.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright 2019 Matthew Oliver
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FFFRTestData.h"
#include "FFFrameReader.h"
#include <gtest/gtest.h>
using namespace Ffr;
struct TestParamsSeek
{
uint32_t m_bufferLength;
};
static std::vector<TestParamsSeek> g_testDataStream = {
{10},
{1},
};
class SeekTest1 : public ::testing::TestWithParam<std::tuple<TestParamsSeek, TestParams>>
{
protected:
SeekTest1() = default;
void SetUp() override
{
setLogLevel(LogLevel::Warning);
DecoderOptions options;
options.m_bufferLength = std::get<0>(GetParam()).m_bufferLength;
m_stream = Stream::getStream(std::get<1>(GetParam()).m_fileName, options);
ASSERT_NE(m_stream, nullptr);
ASSERT_EQ(m_stream->getMaxFrames(), std::get<0>(GetParam()).m_bufferLength);
}
void TearDown() override
{
m_stream.reset();
}
std::shared_ptr<Stream> m_stream = nullptr;
};
TEST_P(SeekTest1, seek)
{
constexpr int64_t seekFrames = 80;
const int64_t actualSeek =
seekFrames >= std::get<1>(GetParam()).m_totalFrames ? std::get<1>(GetParam()).m_totalFrames - 5 : seekFrames;
const auto time1 = m_stream->frameToTime(actualSeek);
ASSERT_TRUE(m_stream->seek(time1));
const auto frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), time1);
}
TEST_P(SeekTest1, seekSmall)
{
ASSERT_NE(m_stream->getNextFrame(), nullptr);
// Seek forward 2 frames only.
const auto time1 = m_stream->frameToTime(2);
ASSERT_TRUE(m_stream->seek(time1));
const auto frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), time1);
}
TEST_P(SeekTest1, seekFail)
{
ASSERT_FALSE(m_stream->seek(m_stream->getDuration()));
ASSERT_FALSE(m_stream->seek(m_stream->getDuration() + 300000));
ASSERT_FALSE(m_stream->seek(-1000000));
// Check we can do a valid seek after a failed one
const auto time1 = m_stream->frameToTime(2);
ASSERT_TRUE(m_stream->seek(time1));
const auto frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), time1);
}
TEST_P(SeekTest1, seekEnd)
{
ASSERT_TRUE(m_stream->seek(m_stream->getDuration() - std::get<1>(GetParam()).m_frameTime -
((((std::get<1>(GetParam()).m_frameTime / 3) & 0x3) == 2) ? 1 : 0)));
const auto frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
}
TEST_P(SeekTest1, seekLoop)
{
constexpr uint32_t seekJump = 40;
constexpr uint32_t numLoops = 5;
constexpr uint32_t numFrames = 25;
double timeStamp1 = 0.0;
int64_t time1 = 0;
// Perform multiple forward seeks
for (uint32_t i = 0; i < numLoops; i++) {
if (time1 >= std::get<1>(GetParam()).m_duration) {
return;
}
ASSERT_TRUE(m_stream->seek(time1));
// Check that multiple sequential frames can be read
int64_t time2 = time1;
for (uint32_t j = 0; j < numFrames; j++) {
const auto frame1 = m_stream->getNextFrame();
// Allow EOF
if (frame1 == nullptr && m_stream->isEndOfFile()) {
return;
}
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), time2);
const double timeStamp2 =
timeStamp1 + (static_cast<double>(j + 1) * (1000000.0 / std::get<1>(GetParam()).m_frameRate));
time2 = llround(timeStamp2);
}
timeStamp1 = (static_cast<double>(i + 1) * static_cast<double>(seekJump) *
(1000000.0 / std::get<1>(GetParam()).m_frameRate));
time1 = llround(timeStamp1);
}
}
TEST_P(SeekTest1, seekBack)
{
// Seek forward
constexpr int64_t seekFrames = 80;
const int64_t actualSeek =
seekFrames >= std::get<1>(GetParam()).m_totalFrames ? std::get<1>(GetParam()).m_totalFrames - 5 : seekFrames;
const auto time1 = m_stream->frameToTime(actualSeek);
ASSERT_TRUE(m_stream->seek(time1));
auto frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), time1);
// Seek back
ASSERT_TRUE(m_stream->seek(0));
frame1 = m_stream->getNextFrame();
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getTimeStamp(), 0);
}
TEST_P(SeekTest1, seekFrameLoop)
{
constexpr uint32_t seekJump = 40;
constexpr uint32_t numLoops = 5;
constexpr uint32_t numFrames = 25;
int64_t frame = 0;
// Perform multiple forward seeks
for (uint32_t i = 0; i < numLoops; i++) {
if (frame >= std::get<1>(GetParam()).m_totalFrames) {
return;
}
ASSERT_TRUE(m_stream->seekFrame(frame));
// Check that multiple sequential frames can be read
int64_t frame2 = frame;
for (uint32_t j = 0; j < numFrames; j++) {
const auto frame1 = m_stream->getNextFrame();
// Allow EOF
if (frame1 == nullptr && m_stream->isEndOfFile()) {
return;
}
ASSERT_NE(frame1, nullptr);
ASSERT_EQ(frame1->getFrameNumber(), frame2);
++frame2;
}
frame += seekJump;
}
}
TEST_P(SeekTest1, getNextFramesSeek)
{
// First seek to a frame
constexpr int64_t seekFrames = 80;
const int64_t actualSeek =
seekFrames >= std::get<1>(GetParam()).m_totalFrames ? std::get<1>(GetParam()).m_totalFrames - 9 : seekFrames;
const auto time1 = m_stream->frameToTime(actualSeek);
ASSERT_TRUE(m_stream->seek(time1));
// Now get frame sequence offset from current
const std::vector<int64_t> framesList1 = {0, 1, 5, 7, 8};
std::vector<int64_t> timesList1;
for (const auto& i : framesList1) {
const auto time2 = m_stream->frameToTime(i);
timesList1.emplace_back(time2);
}
const auto frames1 = m_stream->getNextFrames(timesList1);
ASSERT_EQ(frames1.size(), std::min(timesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
const auto time2 = m_stream->frameToTime(framesList1[j] + actualSeek);
ASSERT_EQ(i->getTimeStamp(), time2);
++j;
}
}
TEST_P(SeekTest1, getNextFrames)
{
// Ensure that value in list is greater than buffer size
const std::vector<int64_t> framesList1 = {3, 5, 7, 8, 12, 23};
std::vector<int64_t> timesList1;
for (const auto& i : framesList1) {
const auto time2 = m_stream->frameToTime(i);
timesList1.emplace_back(time2);
}
const auto frames1 = m_stream->getNextFrames(timesList1);
ASSERT_EQ(frames1.size(), std::min(timesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
ASSERT_EQ(i->getTimeStamp(), timesList1[j]);
++j;
}
}
TEST_P(SeekTest1, getNextFrameByIndexSeek)
{
// First seek to a frame
constexpr int64_t seekFrames = 80;
const int64_t actualSeek =
seekFrames >= std::get<1>(GetParam()).m_totalFrames ? std::get<1>(GetParam()).m_totalFrames - 9 : seekFrames;
ASSERT_TRUE(m_stream->seekFrame(actualSeek));
// Now get frame sequence offset from current
const std::vector<int64_t> framesList1 = {0, 1, 5, 7, 8};
const auto frames1 = m_stream->getNextFramesByIndex(framesList1);
ASSERT_EQ(
frames1.size(), std::min(framesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
ASSERT_EQ(i->getFrameNumber(), framesList1[j] + actualSeek);
++j;
}
}
TEST_P(SeekTest1, getNextFramesByIndex)
{
// Ensure that value in list is greater than buffer size
const std::vector<int64_t> framesList1 = {3, 5, 7, 8, 12, 23};
const auto frames1 = m_stream->getNextFramesByIndex(framesList1);
ASSERT_EQ(
frames1.size(), std::min(framesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
ASSERT_EQ(i->getFrameNumber(), framesList1[j]);
++j;
}
}
TEST_P(SeekTest1, getFrames)
{
// Seek forward 2 frames only. This should not effect result
const auto time1 = m_stream->frameToTime(2);
ASSERT_TRUE(m_stream->seek(time1));
// Ensure that value in list is greater than buffer size
const std::vector<int64_t> framesList1 = {3, 5, 7, 8, 12, 23};
std::vector<int64_t> timesList1;
for (const auto& i : framesList1) {
const auto time2 = m_stream->frameToTime(i);
timesList1.emplace_back(time2);
}
const auto frames1 = m_stream->getFrames(timesList1);
ASSERT_EQ(frames1.size(), std::min(timesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
ASSERT_EQ(i->getTimeStamp(), timesList1[j]);
++j;
}
}
TEST_P(SeekTest1, getFramesByIndex)
{
// Seek forward 2 frames only. This should not effect result
const auto time1 = m_stream->frameToTime(2);
ASSERT_TRUE(m_stream->seek(time1));
// Ensure that value in list is greater than buffer size
const std::vector<int64_t> framesList1 = {3, 5, 7, 8, 12, 23};
const auto frames1 = m_stream->getFramesByIndex(framesList1);
ASSERT_EQ(
frames1.size(), std::min(framesList1.size(), static_cast<size_t>(std::get<0>(GetParam()).m_bufferLength)));
// Check that the returned frames are correct
auto j = 0;
for (auto& i : frames1) {
ASSERT_EQ(i->getFrameNumber(), framesList1[j]);
++j;
}
}
INSTANTIATE_TEST_SUITE_P(SeekTestData, SeekTest1,
::testing::Combine(::testing::ValuesIn(g_testDataStream), ::testing::ValuesIn(g_testData)));