blob: 2046ca0ba94daa2f1e125cb4a02e6dd1e5e6ebc4 [file] [log] [blame]
Linus Nilssoncb9198e2020-04-01 13:38:09 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Unit Test for MediaSampleQueue
18
19// #define LOG_NDEBUG 0
20#define LOG_TAG "MediaSampleQueueTests"
21
22#include <android-base/logging.h>
23#include <gtest/gtest.h>
24#include <media/MediaSampleQueue.h>
25
26#include <thread>
27
28namespace android {
29
30/** Duration to use when delaying threads to order operations. */
31static constexpr int64_t kThreadDelayDurationMs = 100;
32
33class MediaSampleQueueTests : public ::testing::Test {
34public:
35 MediaSampleQueueTests() { LOG(DEBUG) << "MediaSampleQueueTests created"; }
36 ~MediaSampleQueueTests() { LOG(DEBUG) << "MediaSampleQueueTests destroyed"; }
37};
38
39static std::shared_ptr<MediaSample> newSample(uint32_t id) {
40 return MediaSample::createWithReleaseCallback(nullptr /* buffer */, 0 /* offset */, id,
41 nullptr /* callback */);
42}
43
44TEST_F(MediaSampleQueueTests, TestSequentialDequeueOrder) {
45 LOG(DEBUG) << "TestSequentialDequeueOrder Starts";
46
47 static constexpr int kNumSamples = 4;
48 MediaSampleQueue sampleQueue;
49
50 // Enqueue loop.
51 for (int i = 0; i < kNumSamples; ++i) {
52 sampleQueue.enqueue(newSample(i));
53 }
54
55 // Dequeue loop.
56 for (int i = 0; i < kNumSamples; ++i) {
57 std::shared_ptr<MediaSample> sample;
58 bool aborted = sampleQueue.dequeue(&sample);
59 EXPECT_NE(sample, nullptr);
60 EXPECT_EQ(sample->bufferId, i);
61 EXPECT_FALSE(aborted);
62 }
63}
64
65TEST_F(MediaSampleQueueTests, TestInterleavedDequeueOrder) {
66 LOG(DEBUG) << "TestInterleavedDequeueOrder Starts";
67
68 static constexpr int kNumSamples = 4;
69 MediaSampleQueue sampleQueue;
70
71 // Enqueue and dequeue.
72 for (int i = 0; i < kNumSamples; ++i) {
73 sampleQueue.enqueue(newSample(i));
74
75 std::shared_ptr<MediaSample> sample;
76 bool aborted = sampleQueue.dequeue(&sample);
77 EXPECT_NE(sample, nullptr);
78 EXPECT_EQ(sample->bufferId, i);
79 EXPECT_FALSE(aborted);
80 }
81}
82
83TEST_F(MediaSampleQueueTests, TestBlockingDequeue) {
84 LOG(DEBUG) << "TestBlockingDequeue Starts";
85
86 MediaSampleQueue sampleQueue;
87
88 std::thread enqueueThread([&sampleQueue] {
89 // Note: This implementation is a bit racy. Any amount of sleep will not guarantee that the
90 // main thread will be blocked on the sample queue by the time this thread calls enqueue.
91 // But we can say with high confidence that it will and the test will not fail regardless.
92 std::this_thread::sleep_for(std::chrono::milliseconds(kThreadDelayDurationMs));
93 sampleQueue.enqueue(newSample(1));
94 });
95
96 std::shared_ptr<MediaSample> sample;
97 bool aborted = sampleQueue.dequeue(&sample);
98 EXPECT_NE(sample, nullptr);
99 EXPECT_EQ(sample->bufferId, 1);
100 EXPECT_FALSE(aborted);
101
102 enqueueThread.join();
103}
104
105TEST_F(MediaSampleQueueTests, TestDequeueBufferRelease) {
106 LOG(DEBUG) << "TestDequeueBufferRelease Starts";
107
108 static constexpr int kNumSamples = 4;
109 std::vector<bool> bufferReleased(kNumSamples, false);
110
111 MediaSample::OnSampleReleasedCallback callback = [&bufferReleased](MediaSample* sample) {
112 bufferReleased[sample->bufferId] = true;
113 };
114
115 MediaSampleQueue sampleQueue;
116 for (int i = 0; i < kNumSamples; ++i) {
117 bool aborted = sampleQueue.enqueue(
118 MediaSample::createWithReleaseCallback(nullptr, 0, i, callback));
119 EXPECT_FALSE(aborted);
120 }
121
122 for (int i = 0; i < kNumSamples; ++i) {
123 EXPECT_FALSE(bufferReleased[i]);
124 }
125
126 for (int i = 0; i < kNumSamples; ++i) {
127 {
128 std::shared_ptr<MediaSample> sample;
129 bool aborted = sampleQueue.dequeue(&sample);
130 EXPECT_NE(sample, nullptr);
131 EXPECT_EQ(sample->bufferId, i);
132 EXPECT_FALSE(bufferReleased[i]);
133 EXPECT_FALSE(aborted);
134 }
135
136 for (int j = 0; j < kNumSamples; ++j) {
137 EXPECT_EQ(bufferReleased[j], j <= i);
138 }
139 }
140}
141
142TEST_F(MediaSampleQueueTests, TestAbortBufferRelease) {
143 LOG(DEBUG) << "TestAbortBufferRelease Starts";
144
145 static constexpr int kNumSamples = 4;
146 std::vector<bool> bufferReleased(kNumSamples, false);
147
148 MediaSample::OnSampleReleasedCallback callback = [&bufferReleased](MediaSample* sample) {
149 bufferReleased[sample->bufferId] = true;
150 };
151
152 MediaSampleQueue sampleQueue;
153 for (int i = 0; i < kNumSamples; ++i) {
154 bool aborted = sampleQueue.enqueue(
155 MediaSample::createWithReleaseCallback(nullptr, 0, i, callback));
156 EXPECT_FALSE(aborted);
157 }
158
159 for (int i = 0; i < kNumSamples; ++i) {
160 EXPECT_FALSE(bufferReleased[i]);
161 }
162
163 sampleQueue.abort();
164
165 for (int i = 0; i < kNumSamples; ++i) {
166 EXPECT_TRUE(bufferReleased[i]);
167 }
168}
169
170TEST_F(MediaSampleQueueTests, TestNonEmptyAbort) {
171 LOG(DEBUG) << "TestNonEmptyAbort Starts";
172
173 MediaSampleQueue sampleQueue;
174 bool aborted = sampleQueue.enqueue(newSample(1));
175 EXPECT_FALSE(aborted);
176
177 sampleQueue.abort();
178
179 std::shared_ptr<MediaSample> sample;
180 aborted = sampleQueue.dequeue(&sample);
181 EXPECT_TRUE(aborted);
182 EXPECT_EQ(sample, nullptr);
183
184 aborted = sampleQueue.enqueue(sample);
185 EXPECT_TRUE(aborted);
186}
187
188TEST_F(MediaSampleQueueTests, TestEmptyAbort) {
189 LOG(DEBUG) << "TestEmptyAbort Starts";
190
191 MediaSampleQueue sampleQueue;
192 sampleQueue.abort();
193
194 std::shared_ptr<MediaSample> sample;
195 bool aborted = sampleQueue.dequeue(&sample);
196 EXPECT_TRUE(aborted);
197 EXPECT_EQ(sample, nullptr);
198
199 aborted = sampleQueue.enqueue(sample);
200 EXPECT_TRUE(aborted);
201}
202
203TEST_F(MediaSampleQueueTests, TestBlockingAbort) {
204 LOG(DEBUG) << "TestBlockingAbort Starts";
205
206 MediaSampleQueue sampleQueue;
207
208 std::thread abortingThread([&sampleQueue] {
209 // Note: This implementation is a bit racy. Any amount of sleep will not guarantee that the
210 // main thread will be blocked on the sample queue by the time this thread calls abort.
211 // But we can say with high confidence that it will and the test will not fail regardless.
212 std::this_thread::sleep_for(std::chrono::milliseconds(kThreadDelayDurationMs));
213 sampleQueue.abort();
214 });
215
216 std::shared_ptr<MediaSample> sample;
217 bool aborted = sampleQueue.dequeue(&sample);
218 EXPECT_TRUE(aborted);
219 EXPECT_EQ(sample, nullptr);
220
221 abortingThread.join();
222}
223
224} // namespace android
225
226int main(int argc, char** argv) {
227 ::testing::InitGoogleTest(&argc, argv);
228 return RUN_ALL_TESTS();
229}