Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 MediaTranscodingService. |
| 18 | |
| 19 | #include <aidl/android/media/BnTranscodingClientCallback.h> |
| 20 | #include <aidl/android/media/IMediaTranscodingService.h> |
| 21 | #include <aidl/android/media/ITranscodingClient.h> |
| 22 | #include <aidl/android/media/ITranscodingClientCallback.h> |
| 23 | #include <aidl/android/media/TranscodingJobParcel.h> |
| 24 | #include <aidl/android/media/TranscodingJobPriority.h> |
| 25 | #include <aidl/android/media/TranscodingRequestParcel.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android/binder_manager.h> |
| 28 | #include <android/binder_process.h> |
| 29 | #include <binder/PermissionController.h> |
| 30 | #include <cutils/multiuser.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <utils/Log.h> |
| 36 | |
| 37 | #include <iostream> |
| 38 | #include <list> |
| 39 | |
| 40 | #include "SimulatedTranscoder.h" |
| 41 | |
| 42 | namespace android { |
| 43 | |
| 44 | namespace media { |
| 45 | |
| 46 | using Status = ::ndk::ScopedAStatus; |
| 47 | using aidl::android::media::BnTranscodingClientCallback; |
| 48 | using aidl::android::media::IMediaTranscodingService; |
| 49 | using aidl::android::media::ITranscodingClient; |
| 50 | using aidl::android::media::ITranscodingClientCallback; |
| 51 | using aidl::android::media::TranscodingJobParcel; |
| 52 | using aidl::android::media::TranscodingJobPriority; |
| 53 | using aidl::android::media::TranscodingRequestParcel; |
| 54 | using aidl::android::media::TranscodingVideoTrackFormat; |
| 55 | |
| 56 | constexpr int32_t kClientUseCallingPid = IMediaTranscodingService::USE_CALLING_PID; |
| 57 | |
| 58 | constexpr uid_t kClientUid = 5000; |
| 59 | #define UID(n) (kClientUid + (n)) |
| 60 | |
| 61 | constexpr int32_t kClientId = 0; |
| 62 | #define CLIENT(n) (kClientId + (n)) |
| 63 | |
| 64 | constexpr const char* kClientName = "TestClient"; |
| 65 | constexpr const char* kClientPackageA = "com.android.tests.transcoding.testapp.A"; |
| 66 | constexpr const char* kClientPackageB = "com.android.tests.transcoding.testapp.B"; |
| 67 | constexpr const char* kClientPackageC = "com.android.tests.transcoding.testapp.C"; |
| 68 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 69 | constexpr const char* kTestActivityName = "/com.android.tests.transcoding.MainActivity"; |
| 70 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 71 | static status_t getUidForPackage(String16 packageName, userid_t userId, /*inout*/ uid_t& uid) { |
| 72 | PermissionController pc; |
| 73 | uid = pc.getPackageUid(packageName, 0); |
| 74 | if (uid <= 0) { |
| 75 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 76 | return BAD_VALUE; |
| 77 | } |
| 78 | |
| 79 | if (userId < 0) { |
| 80 | ALOGE("Invalid user: %d", userId); |
| 81 | return BAD_VALUE; |
| 82 | } |
| 83 | |
| 84 | uid = multiuser_get_uid(userId, uid); |
| 85 | return NO_ERROR; |
| 86 | } |
| 87 | |
| 88 | struct ShellHelper { |
| 89 | static bool RunCmd(const std::string& cmdStr) { |
| 90 | int ret = system(cmdStr.c_str()); |
| 91 | if (ret != 0) { |
| 92 | ALOGE("Failed to run cmd: %s, exitcode %d", cmdStr.c_str(), ret); |
| 93 | return false; |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static bool Start(const char* packageName, const char* activityName) { |
| 99 | return RunCmd("am start -W " + std::string(packageName) + std::string(activityName) + |
| 100 | " &> /dev/null"); |
| 101 | } |
| 102 | |
| 103 | static bool Stop(const char* packageName) { |
| 104 | return RunCmd("am force-stop " + std::string(packageName)); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | struct EventTracker { |
| 109 | struct Event { |
| 110 | enum { NoEvent, Start, Pause, Resume, Finished, Failed } type; |
| 111 | int64_t clientId; |
| 112 | int32_t jobId; |
| 113 | }; |
| 114 | |
| 115 | #define DECLARE_EVENT(action) \ |
| 116 | static Event action(int32_t clientId, int32_t jobId) { \ |
| 117 | return {Event::action, clientId, jobId}; \ |
| 118 | } |
| 119 | |
| 120 | DECLARE_EVENT(Start); |
| 121 | DECLARE_EVENT(Pause); |
| 122 | DECLARE_EVENT(Resume); |
| 123 | DECLARE_EVENT(Finished); |
| 124 | DECLARE_EVENT(Failed); |
| 125 | |
| 126 | static constexpr Event NoEvent = {Event::NoEvent, 0, 0}; |
| 127 | |
| 128 | static std::string toString(const Event& event) { |
| 129 | std::string eventStr; |
| 130 | switch (event.type) { |
| 131 | case Event::Start: |
| 132 | eventStr = "Start"; |
| 133 | break; |
| 134 | case Event::Pause: |
| 135 | eventStr = "Pause"; |
| 136 | break; |
| 137 | case Event::Resume: |
| 138 | eventStr = "Resume"; |
| 139 | break; |
| 140 | case Event::Finished: |
| 141 | eventStr = "Finished"; |
| 142 | break; |
| 143 | case Event::Failed: |
| 144 | eventStr = "Failed"; |
| 145 | break; |
| 146 | default: |
| 147 | return "NoEvent"; |
| 148 | } |
| 149 | return "job {" + std::to_string(event.clientId) + ", " + std::to_string(event.jobId) + |
| 150 | "}: " + eventStr; |
| 151 | } |
| 152 | |
| 153 | // Pop 1 event from front, wait for up to timeoutUs if empty. |
| 154 | const Event& pop(int64_t timeoutUs = 0) { |
| 155 | std::unique_lock lock(mLock); |
| 156 | |
| 157 | if (mEventQueue.empty() && timeoutUs > 0) { |
| 158 | mCondition.wait_for(lock, std::chrono::microseconds(timeoutUs)); |
| 159 | } |
| 160 | |
| 161 | if (mEventQueue.empty()) { |
| 162 | mPoppedEvent = NoEvent; |
| 163 | } else { |
| 164 | mPoppedEvent = *mEventQueue.begin(); |
| 165 | mEventQueue.pop_front(); |
| 166 | } |
| 167 | |
| 168 | return mPoppedEvent; |
| 169 | } |
| 170 | |
| 171 | // Push 1 event to back. |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 172 | void append(const Event& event, |
| 173 | const TranscodingErrorCode err = TranscodingErrorCode::kNoError) { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 174 | ALOGD("%s", toString(event).c_str()); |
| 175 | |
| 176 | std::unique_lock lock(mLock); |
| 177 | |
| 178 | mEventQueue.push_back(event); |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 179 | mLastErr = err; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 180 | mCondition.notify_one(); |
| 181 | } |
| 182 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 183 | TranscodingErrorCode getLastError() { |
| 184 | std::unique_lock lock(mLock); |
| 185 | return mLastErr; |
| 186 | } |
| 187 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 188 | private: |
| 189 | std::mutex mLock; |
| 190 | std::condition_variable mCondition; |
| 191 | Event mPoppedEvent; |
| 192 | std::list<Event> mEventQueue; |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 193 | TranscodingErrorCode mLastErr; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | // Operators for GTest macros. |
| 197 | bool operator==(const EventTracker::Event& lhs, const EventTracker::Event& rhs) { |
| 198 | return lhs.type == rhs.type && lhs.clientId == rhs.clientId && lhs.jobId == rhs.jobId; |
| 199 | } |
| 200 | |
| 201 | std::ostream& operator<<(std::ostream& str, const EventTracker::Event& v) { |
| 202 | str << EventTracker::toString(v); |
| 203 | return str; |
| 204 | } |
| 205 | |
| 206 | struct TestClientCallback : public BnTranscodingClientCallback, public EventTracker { |
| 207 | TestClientCallback(int32_t id) : mClientId(id) { |
| 208 | ALOGI("TestClientCallback %d Created", mClientId); |
| 209 | } |
| 210 | |
| 211 | virtual ~TestClientCallback() { ALOGI("TestClientCallback %d destroyed", mClientId); } |
| 212 | |
| 213 | Status openFileDescriptor(const std::string& in_fileUri, const std::string& in_mode, |
| 214 | ::ndk::ScopedFileDescriptor* _aidl_return) override { |
| 215 | ALOGD("@@@ openFileDescriptor: %s", in_fileUri.c_str()); |
| 216 | int fd; |
| 217 | if (in_mode == "w" || in_mode == "rw") { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 218 | int kOpenFlags; |
| 219 | if (in_mode == "w") { |
| 220 | // Write-only, create file if non-existent, truncate existing file. |
| 221 | kOpenFlags = O_WRONLY | O_CREAT | O_TRUNC; |
| 222 | } else { |
| 223 | // Read-Write, create if non-existent, no truncate (service will truncate if needed) |
| 224 | kOpenFlags = O_RDWR | O_CREAT; |
| 225 | } |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 226 | // User R+W permission. |
| 227 | constexpr int kFileMode = S_IRUSR | S_IWUSR; |
| 228 | fd = open(in_fileUri.c_str(), kOpenFlags, kFileMode); |
| 229 | } else { |
| 230 | fd = open(in_fileUri.c_str(), O_RDONLY); |
| 231 | } |
| 232 | _aidl_return->set(fd); |
| 233 | return Status::ok(); |
| 234 | } |
| 235 | |
| 236 | Status onTranscodingStarted(int32_t in_jobId) override { |
| 237 | append(EventTracker::Start(mClientId, in_jobId)); |
| 238 | return Status::ok(); |
| 239 | } |
| 240 | |
| 241 | Status onTranscodingPaused(int32_t in_jobId) override { |
| 242 | append(EventTracker::Pause(mClientId, in_jobId)); |
| 243 | return Status::ok(); |
| 244 | } |
| 245 | |
| 246 | Status onTranscodingResumed(int32_t in_jobId) override { |
| 247 | append(EventTracker::Resume(mClientId, in_jobId)); |
| 248 | return Status::ok(); |
| 249 | } |
| 250 | |
| 251 | Status onTranscodingFinished( |
| 252 | int32_t in_jobId, |
| 253 | const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override { |
| 254 | append(Finished(mClientId, in_jobId)); |
| 255 | return Status::ok(); |
| 256 | } |
| 257 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 258 | Status onTranscodingFailed(int32_t in_jobId, |
| 259 | ::aidl::android::media::TranscodingErrorCode in_errorCode) override { |
| 260 | append(Failed(mClientId, in_jobId), in_errorCode); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 261 | return Status::ok(); |
| 262 | } |
| 263 | |
| 264 | Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */, |
| 265 | int32_t /* in_newAwaitNumber */) override { |
| 266 | return Status::ok(); |
| 267 | } |
| 268 | |
| 269 | Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override { |
| 270 | return Status::ok(); |
| 271 | } |
| 272 | |
| 273 | int32_t mClientId; |
| 274 | }; |
| 275 | |
| 276 | class MediaTranscodingServiceTestBase : public ::testing::Test { |
| 277 | public: |
| 278 | MediaTranscodingServiceTestBase() { ALOGI("MediaTranscodingServiceTestBase created"); } |
| 279 | |
| 280 | virtual ~MediaTranscodingServiceTestBase() { |
| 281 | ALOGI("MediaTranscodingServiceTestBase destroyed"); |
| 282 | } |
| 283 | |
| 284 | void SetUp() override { |
| 285 | // Need thread pool to receive callbacks, otherwise oneway callbacks are |
| 286 | // silently ignored. |
| 287 | ABinderProcess_startThreadPool(); |
| 288 | ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding")); |
| 289 | mService = IMediaTranscodingService::fromBinder(binder); |
| 290 | if (mService == nullptr) { |
| 291 | ALOGE("Failed to connect to the media.trascoding service."); |
| 292 | return; |
| 293 | } |
| 294 | mClientCallback1 = ::ndk::SharedRefBase::make<TestClientCallback>(CLIENT(1)); |
| 295 | mClientCallback2 = ::ndk::SharedRefBase::make<TestClientCallback>(CLIENT(2)); |
| 296 | mClientCallback3 = ::ndk::SharedRefBase::make<TestClientCallback>(CLIENT(3)); |
| 297 | } |
| 298 | |
| 299 | std::shared_ptr<ITranscodingClient> registerOneClient( |
| 300 | const char* packageName, const std::shared_ptr<TestClientCallback>& callback, |
| 301 | uid_t defaultUid) { |
| 302 | uid_t uid; |
| 303 | if (getUidForPackage(String16(packageName), 0 /*userId*/, uid) != NO_ERROR) { |
| 304 | uid = defaultUid; |
| 305 | } |
| 306 | |
| 307 | ALOGD("registering %s with uid %d", packageName, uid); |
| 308 | |
| 309 | std::shared_ptr<ITranscodingClient> client; |
| 310 | Status status = mService->registerClient(callback, kClientName, packageName, uid, |
| 311 | kClientUseCallingPid, &client); |
| 312 | return status.isOk() ? client : nullptr; |
| 313 | } |
| 314 | |
| 315 | void registerMultipleClients() { |
| 316 | // Register 3 clients. |
| 317 | mClient1 = registerOneClient(kClientPackageA, mClientCallback1, UID(1)); |
| 318 | EXPECT_TRUE(mClient1 != nullptr); |
| 319 | |
| 320 | mClient2 = registerOneClient(kClientPackageB, mClientCallback2, UID(2)); |
| 321 | EXPECT_TRUE(mClient2 != nullptr); |
| 322 | |
| 323 | mClient3 = registerOneClient(kClientPackageC, mClientCallback3, UID(3)); |
| 324 | EXPECT_TRUE(mClient3 != nullptr); |
| 325 | |
| 326 | // Check the number of clients. |
| 327 | int32_t numOfClients; |
| 328 | Status status = mService->getNumOfClients(&numOfClients); |
| 329 | EXPECT_TRUE(status.isOk()); |
| 330 | EXPECT_EQ(3, numOfClients); |
| 331 | } |
| 332 | |
| 333 | void unregisterMultipleClients() { |
| 334 | Status status; |
| 335 | |
| 336 | // Unregister the clients. |
| 337 | status = mClient1->unregister(); |
| 338 | EXPECT_TRUE(status.isOk()); |
| 339 | |
| 340 | status = mClient2->unregister(); |
| 341 | EXPECT_TRUE(status.isOk()); |
| 342 | |
| 343 | status = mClient3->unregister(); |
| 344 | EXPECT_TRUE(status.isOk()); |
| 345 | |
| 346 | // Check the number of clients. |
| 347 | int32_t numOfClients; |
| 348 | status = mService->getNumOfClients(&numOfClients); |
| 349 | EXPECT_TRUE(status.isOk()); |
| 350 | EXPECT_EQ(0, numOfClients); |
| 351 | } |
| 352 | |
| 353 | static constexpr bool success = true; |
| 354 | static constexpr bool fail = false; |
| 355 | |
| 356 | template <bool expectation = success> |
| 357 | bool submit(const std::shared_ptr<ITranscodingClient>& client, int32_t jobId, |
| 358 | const char* sourceFilePath, const char* destinationFilePath, |
| 359 | TranscodingJobPriority priority = TranscodingJobPriority::kNormal, |
| 360 | int bitrateBps = -1) { |
| 361 | constexpr bool shouldSucceed = (expectation == success); |
| 362 | bool result; |
| 363 | TranscodingRequestParcel request; |
| 364 | TranscodingJobParcel job; |
| 365 | |
| 366 | request.sourceFilePath = sourceFilePath; |
| 367 | request.destinationFilePath = destinationFilePath; |
| 368 | request.priority = priority; |
| 369 | if (bitrateBps > 0) { |
| 370 | request.requestedVideoTrackFormat.emplace(TranscodingVideoTrackFormat()); |
| 371 | request.requestedVideoTrackFormat->bitrateBps = bitrateBps; |
| 372 | } |
| 373 | Status status = client->submitRequest(request, &job, &result); |
| 374 | |
| 375 | EXPECT_TRUE(status.isOk()); |
| 376 | EXPECT_EQ(result, shouldSucceed); |
| 377 | if (shouldSucceed) { |
| 378 | EXPECT_EQ(job.jobId, jobId); |
| 379 | } |
| 380 | |
| 381 | return status.isOk() && (result == shouldSucceed) && (!shouldSucceed || job.jobId == jobId); |
| 382 | } |
| 383 | |
| 384 | template <bool expectation = success> |
| 385 | bool cancel(const std::shared_ptr<ITranscodingClient>& client, int32_t jobId) { |
| 386 | constexpr bool shouldSucceed = (expectation == success); |
| 387 | bool result; |
| 388 | Status status = client->cancelJob(jobId, &result); |
| 389 | |
| 390 | EXPECT_TRUE(status.isOk()); |
| 391 | EXPECT_EQ(result, shouldSucceed); |
| 392 | |
| 393 | return status.isOk() && (result == shouldSucceed); |
| 394 | } |
| 395 | |
| 396 | template <bool expectation = success> |
| 397 | bool getJob(const std::shared_ptr<ITranscodingClient>& client, int32_t jobId, |
| 398 | const char* sourceFilePath, const char* destinationFilePath) { |
| 399 | constexpr bool shouldSucceed = (expectation == success); |
| 400 | bool result; |
| 401 | TranscodingJobParcel job; |
| 402 | Status status = client->getJobWithId(jobId, &job, &result); |
| 403 | |
| 404 | EXPECT_TRUE(status.isOk()); |
| 405 | EXPECT_EQ(result, shouldSucceed); |
| 406 | if (shouldSucceed) { |
| 407 | EXPECT_EQ(job.jobId, jobId); |
| 408 | EXPECT_EQ(job.request.sourceFilePath, sourceFilePath); |
| 409 | } |
| 410 | |
| 411 | return status.isOk() && (result == shouldSucceed) && |
| 412 | (!shouldSucceed || |
| 413 | (job.jobId == jobId && job.request.sourceFilePath == sourceFilePath && |
| 414 | job.request.destinationFilePath == destinationFilePath)); |
| 415 | } |
| 416 | |
| 417 | void deleteFile(const char* path) { unlink(path); } |
| 418 | |
| 419 | std::shared_ptr<IMediaTranscodingService> mService; |
| 420 | std::shared_ptr<TestClientCallback> mClientCallback1; |
| 421 | std::shared_ptr<TestClientCallback> mClientCallback2; |
| 422 | std::shared_ptr<TestClientCallback> mClientCallback3; |
| 423 | std::shared_ptr<ITranscodingClient> mClient1; |
| 424 | std::shared_ptr<ITranscodingClient> mClient2; |
| 425 | std::shared_ptr<ITranscodingClient> mClient3; |
| 426 | const char* mTestName; |
| 427 | }; |
| 428 | |
| 429 | } // namespace media |
| 430 | } // namespace android |