1 /*
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 #include <gtest/gtest.h>
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <fstream>
22 #include <memory>
23
24 #include <media/stagefright/MediaDefs.h>
25 #include <media/stagefright/MetaDataBase.h>
26
27 constexpr int32_t kWidth1 = 1920;
28 constexpr int32_t kHeight1 = 1080;
29 constexpr int32_t kWidth2 = 1280;
30 constexpr int32_t kHeight2 = 920;
31 constexpr int32_t kWidth3 = 720;
32 constexpr int32_t kHeight3 = 480;
33 constexpr int32_t kProfile = 1;
34 constexpr int32_t kLevel = 1;
35 constexpr int32_t kPlatformValue = 1;
36
37 // Rectangle margins
38 constexpr int32_t kLeft = 100;
39 constexpr int32_t kTop = 100;
40 constexpr int32_t kRight = 100;
41 constexpr int32_t kBottom = 100;
42
43 constexpr int64_t kDurationUs = 60000000;
44
45 constexpr float kCaptureRate = 30.0;
46
47 namespace android {
48
49 class MetaDataBaseUnitTest : public ::testing::Test {};
50
TEST_F(MetaDataBaseUnitTest,CreateMetaDataBaseTest)51 TEST_F(MetaDataBaseUnitTest, CreateMetaDataBaseTest) {
52 std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
53 ASSERT_NE(metaData, nullptr) << "Failed to create meta data";
54
55 // Testing copy constructor
56 MetaDataBase *metaDataCopy = metaData.get();
57 ASSERT_NE(metaDataCopy, nullptr) << "Failed to create meta data copy";
58 }
59
TEST_F(MetaDataBaseUnitTest,SetAndFindDataTest)60 TEST_F(MetaDataBaseUnitTest, SetAndFindDataTest) {
61 std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
62 ASSERT_NE(metaData, nullptr) << "Failed to create meta data";
63
64 // Setting the different key-value pair type for first time, overwrite
65 // expected to be false
66 bool status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
67 ASSERT_FALSE(status) << "Initializing kKeyMIMEType, overwrite is expected to be false";
68
69 status = metaData->setInt32(kKeyWidth, kWidth1);
70 ASSERT_FALSE(status) << "Initializing kKeyWidth, overwrite is expected to be false";
71 status = metaData->setInt32(kKeyHeight, kHeight1);
72 ASSERT_FALSE(status) << "Initializing kKeyHeight, overwrite is expected to be false";
73 status = metaData->setInt32(kKeyVideoProfile, kProfile);
74 ASSERT_FALSE(status) << "Initializing kKeyVideoProfile, overwrite is expected to be false";
75 status = metaData->setInt32(kKeyVideoLevel, kLevel);
76 ASSERT_FALSE(status) << "Initializing kKeyVideoLevel, overwrite is expected to be false";
77
78 status = metaData->setInt64(kKeyDuration, kDurationUs);
79 ASSERT_FALSE(status) << "Initializing kKeyDuration, overwrite is expected to be false";
80
81 status = metaData->setFloat(kKeyCaptureFramerate, kCaptureRate);
82 ASSERT_FALSE(status) << "Initializing kKeyCaptureFramerate, overwrite is expected to be false";
83
84 const int32_t *platform = &kPlatformValue;
85 status = metaData->setPointer(kKeyPlatformPrivate, (void *)platform);
86 ASSERT_FALSE(status) << "Initializing kKeyPlatformPrivate, overwrite is expected to be false";
87
88 status = metaData->setRect(kKeyCropRect, kLeft, kTop, kRight, kBottom);
89 ASSERT_FALSE(status) << "Initializing kKeyCropRect, overwrite is expected to be false";
90
91 // Dump to log for reference
92 metaData->dumpToLog();
93
94 // Find the data which was set
95 const char *mime;
96 status = metaData->findCString(kKeyMIMEType, &mime);
97 ASSERT_TRUE(status) << "kKeyMIMEType key does not exists in metadata";
98 ASSERT_STREQ(mime, MEDIA_MIMETYPE_VIDEO_AVC) << "Incorrect mime type returned";
99
100 int32_t width, height, profile, level;
101 status = metaData->findInt32(kKeyWidth, &width);
102 ASSERT_TRUE(status) << "kKeyWidth key does not exists in metadata";
103 ASSERT_EQ(width, kWidth1) << "Incorrect value of width returned";
104
105 status = metaData->findInt32(kKeyHeight, &height);
106 ASSERT_TRUE(status) << "kKeyHeight key does not exists in metadata";
107 ASSERT_EQ(height, kHeight1) << "Incorrect value of height returned";
108
109 status = metaData->findInt32(kKeyVideoProfile, &profile);
110 ASSERT_TRUE(status) << "kKeyVideoProfile key does not exists in metadata";
111 ASSERT_EQ(profile, kProfile) << "Incorrect value of profile returned";
112
113 status = metaData->findInt32(kKeyVideoLevel, &level);
114 ASSERT_TRUE(status) << "kKeyVideoLevel key does not exists in metadata";
115 ASSERT_EQ(level, kLevel) << "Incorrect value of level returned";
116
117 int64_t duration;
118 status = metaData->findInt64(kKeyDuration, &duration);
119 ASSERT_TRUE(status) << "kKeyDuration key does not exists in metadata";
120 ASSERT_EQ(duration, kDurationUs) << "Incorrect value of duration returned";
121
122 float frameRate;
123 status = metaData->findFloat(kKeyCaptureFramerate, &frameRate);
124 ASSERT_TRUE(status) << "kKeyCaptureFramerate key does not exists in metadata";
125 ASSERT_EQ(frameRate, kCaptureRate) << "Incorrect value of captureFrameRate returned";
126
127 int32_t top, bottom, left, right;
128 status = metaData->findRect(kKeyCropRect, &left, &top, &right, &bottom);
129 ASSERT_TRUE(status) << "kKeyCropRect key does not exists in metadata";
130 ASSERT_EQ(left, kLeft) << "Incorrect value of left margin returned";
131 ASSERT_EQ(top, kTop) << "Incorrect value of top margin returned";
132 ASSERT_EQ(right, kRight) << "Incorrect value of right margin returned";
133 ASSERT_EQ(bottom, kBottom) << "Incorrect value of bottom margin returned";
134
135 void *platformValue;
136 status = metaData->findPointer(kKeyPlatformPrivate, &platformValue);
137 ASSERT_TRUE(status) << "kKeyPlatformPrivate key does not exists in metadata";
138 ASSERT_EQ(platformValue, &kPlatformValue) << "Incorrect value of pointer returned";
139
140 // Check for the key which is not added to metadata
141 int32_t angle;
142 status = metaData->findInt32(kKeyRotation, &angle);
143 ASSERT_FALSE(status) << "Value for an invalid key is returned when the key is not set";
144 }
145
TEST_F(MetaDataBaseUnitTest,OverWriteFunctionalityTest)146 TEST_F(MetaDataBaseUnitTest, OverWriteFunctionalityTest) {
147 std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
148 ASSERT_NE(metaData, nullptr) << "Failed to create meta data";
149
150 // set/set/read to check first overwrite operation
151 bool status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
152 ASSERT_FALSE(status) << "Initializing kKeyMIMEType, overwrite is expected to be false";
153 // Overwrite the value
154 status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
155 ASSERT_TRUE(status) << "Setting kKeyMIMEType again, overwrite is expected to be true";
156 // Check the value
157 const char *mime;
158 status = metaData->findCString(kKeyMIMEType, &mime);
159 ASSERT_TRUE(status) << "kKeyMIMEType key does not exists in metadata";
160 ASSERT_STREQ(mime, MEDIA_MIMETYPE_VIDEO_HEVC) << "Mime value is not overwritten";
161
162 // set/set/set/read to check second overwrite operation
163 status = metaData->setInt32(kKeyWidth, kWidth1);
164 ASSERT_FALSE(status) << "Initializing kKeyWidth, overwrite is expected to be false";
165 status = metaData->setInt32(kKeyHeight, kHeight1);
166 ASSERT_FALSE(status) << "Initializing kKeyHeight, overwrite is expected to be false";
167 // Overwrite the value
168 status = metaData->setInt32(kKeyWidth, kWidth2);
169 ASSERT_TRUE(status) << "Setting kKeyWidth again, overwrite is expected to be true";
170 status = metaData->setInt32(kKeyHeight, kHeight2);
171 ASSERT_TRUE(status) << "Setting kKeyHeight again, overwrite is expected to be true";
172 // Overwrite the value again
173 status = metaData->setInt32(kKeyWidth, kWidth3);
174 ASSERT_TRUE(status) << "Setting kKeyWidth again, overwrite is expected to be true";
175 status = metaData->setInt32(kKeyHeight, kHeight3);
176 ASSERT_TRUE(status) << "Setting kKeyHeight again, overwrite is expected to be true";
177 // Check the value
178 int32_t width, height;
179 status = metaData->findInt32(kKeyWidth, &width);
180 ASSERT_TRUE(status) << "kKeyWidth key does not exists in metadata";
181 ASSERT_EQ(width, kWidth3) << "Value of width is not overwritten";
182
183 status = metaData->findInt32(kKeyHeight, &height);
184 ASSERT_TRUE(status) << "kKeyHeight key does not exists in metadata";
185 ASSERT_EQ(height, kHeight3) << "Value of height is not overwritten";
186 }
187
TEST_F(MetaDataBaseUnitTest,RemoveKeyTest)188 TEST_F(MetaDataBaseUnitTest, RemoveKeyTest) {
189 std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
190 ASSERT_NE(metaData, nullptr) << "Failed to create meta data";
191
192 bool status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
193 ASSERT_FALSE(status) << "Initializing kKeyMIMEType, overwrite is expected to be false";
194 // Query the key
195 status = metaData->hasData(kKeyMIMEType);
196 ASSERT_TRUE(status) << "MetaData does not have the mime key";
197
198 status = metaData->remove(kKeyMIMEType);
199 ASSERT_TRUE(status) << "Failed to remove the kKeyMIMEType key";
200
201 // Query the key
202 status = metaData->hasData(kKeyMIMEType);
203 ASSERT_FALSE(status) << "MetaData has mime key after removing it, expected to be false";
204
205 // Remove the non existing key
206 status = metaData->remove(kKeyMIMEType);
207 ASSERT_FALSE(status) << "Removed the non existing key";
208
209 // Check overwriting the removed key
210 metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
211 ASSERT_FALSE(status) << "Overwrite should be false since the key was removed";
212
213 status = metaData->setInt32(kKeyWidth, kWidth1);
214 ASSERT_FALSE(status) << "Initializing kKeyWidth, overwrite is expected to be false";
215
216 // Clear whole metadata
217 metaData->clear();
218
219 // Check finding key after clearing the metadata
220 int32_t width;
221 status = metaData->findInt32(kKeyWidth, &width);
222 ASSERT_FALSE(status) << "MetaData found kKeyWidth key after clearing all the items in it, "
223 "expected to be false";
224
225 // Query the key
226 status = metaData->hasData(kKeyWidth);
227 ASSERT_FALSE(status)
228 << "MetaData has width key after clearing all the items in it, expected to be false";
229
230 status = metaData->hasData(kKeyMIMEType);
231 ASSERT_FALSE(status)
232 << "MetaData has mime key after clearing all the items in it, expected to be false";
233
234 // Check removing key after clearing the metadata
235 status = metaData->remove(kKeyMIMEType);
236 ASSERT_FALSE(status) << "Removed the key, after clearing the metadata";
237
238 // Checking set after clearing the metadata
239 status = metaData->setInt32(kKeyWidth, kWidth1);
240 ASSERT_FALSE(status) << "Overwrite should be false since the metadata was cleared";
241
242 metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
243 ASSERT_FALSE(status) << "Overwrite should be false since the metadata was cleared";
244 }
245
TEST_F(MetaDataBaseUnitTest,ConvertToStringTest)246 TEST_F(MetaDataBaseUnitTest, ConvertToStringTest) {
247 std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
248 ASSERT_NE(metaData, nullptr) << "Failed to create meta data";
249
250 String8 info = metaData->toString();
251 ASSERT_EQ(info.length(), 0) << "Empty MetaData length is non-zero: " << info.length();
252
253 bool status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
254 ASSERT_FALSE(status) << "Initializing kKeyMIMEType, overwrite is expected to be false";
255
256 status = metaData->setInt32(kKeyWidth, kWidth1);
257 ASSERT_FALSE(status) << "Initializing kKeyWidth, overwrite is expected to be false";
258 status = metaData->setInt32(kKeyHeight, kHeight1);
259 ASSERT_FALSE(status) << "Initializing kKeyHeight, overwrite is expected to be false";
260 status = metaData->setInt32(kKeyVideoProfile, kProfile);
261 ASSERT_FALSE(status) << "Initializing kKeyVideoProfile, overwrite is expected to be false";
262 status = metaData->setInt32(kKeyVideoLevel, kLevel);
263 ASSERT_FALSE(status) << "Initializing kKeyVideoLevel, overwrite is expected to be false";
264
265 info = metaData->toString();
266 ASSERT_GT(info.length(), 0) << "MetaData contains no information";
267
268 // Dump to log for reference
269 metaData->dumpToLog();
270
271 // Clear whole metadata
272 metaData->clear();
273
274 info = metaData->toString();
275 ASSERT_EQ(info.length(), 0) << "MetaData length is non-zero after clearing it: "
276 << info.length();
277 }
278
279 } // namespace android
280