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> |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 23 | #include <aidl/android/media/TranscodingRequestParcel.h> |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 24 | #include <aidl/android/media/TranscodingSessionParcel.h> |
| 25 | #include <aidl/android/media/TranscodingSessionPriority.h> |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 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; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 51 | using aidl::android::media::TranscodingRequestParcel; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 52 | using aidl::android::media::TranscodingSessionParcel; |
| 53 | using aidl::android::media::TranscodingSessionPriority; |
Chong Zhang | 456787f | 2021-02-18 16:47:08 -0800 | [diff] [blame] | 54 | using aidl::android::media::TranscodingTestConfig; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 55 | using aidl::android::media::TranscodingVideoTrackFormat; |
| 56 | |
| 57 | constexpr int32_t kClientUseCallingPid = IMediaTranscodingService::USE_CALLING_PID; |
| 58 | |
| 59 | constexpr uid_t kClientUid = 5000; |
| 60 | #define UID(n) (kClientUid + (n)) |
| 61 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 62 | constexpr pid_t kClientPid = 10000; |
| 63 | #define PID(n) (kClientPid + (n)) |
| 64 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 65 | constexpr int32_t kClientId = 0; |
| 66 | #define CLIENT(n) (kClientId + (n)) |
| 67 | |
| 68 | constexpr const char* kClientName = "TestClient"; |
| 69 | constexpr const char* kClientPackageA = "com.android.tests.transcoding.testapp.A"; |
| 70 | constexpr const char* kClientPackageB = "com.android.tests.transcoding.testapp.B"; |
| 71 | constexpr const char* kClientPackageC = "com.android.tests.transcoding.testapp.C"; |
| 72 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 73 | constexpr const char* kTestActivityName = "/com.android.tests.transcoding.MainActivity"; |
| 74 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 75 | static status_t getUidForPackage(String16 packageName, userid_t userId, /*inout*/ uid_t& uid) { |
| 76 | PermissionController pc; |
| 77 | uid = pc.getPackageUid(packageName, 0); |
| 78 | if (uid <= 0) { |
| 79 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 80 | return BAD_VALUE; |
| 81 | } |
| 82 | |
| 83 | if (userId < 0) { |
| 84 | ALOGE("Invalid user: %d", userId); |
| 85 | return BAD_VALUE; |
| 86 | } |
| 87 | |
| 88 | uid = multiuser_get_uid(userId, uid); |
| 89 | return NO_ERROR; |
| 90 | } |
| 91 | |
| 92 | struct ShellHelper { |
| 93 | static bool RunCmd(const std::string& cmdStr) { |
| 94 | int ret = system(cmdStr.c_str()); |
| 95 | if (ret != 0) { |
| 96 | ALOGE("Failed to run cmd: %s, exitcode %d", cmdStr.c_str(), ret); |
| 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | static bool Start(const char* packageName, const char* activityName) { |
| 103 | return RunCmd("am start -W " + std::string(packageName) + std::string(activityName) + |
| 104 | " &> /dev/null"); |
| 105 | } |
| 106 | |
| 107 | static bool Stop(const char* packageName) { |
| 108 | return RunCmd("am force-stop " + std::string(packageName)); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | struct EventTracker { |
| 113 | struct Event { |
| 114 | enum { NoEvent, Start, Pause, Resume, Finished, Failed } type; |
| 115 | int64_t clientId; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 116 | int32_t sessionId; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 119 | #define DECLARE_EVENT(action) \ |
| 120 | static Event action(int32_t clientId, int32_t sessionId) { \ |
| 121 | return {Event::action, clientId, sessionId}; \ |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | DECLARE_EVENT(Start); |
| 125 | DECLARE_EVENT(Pause); |
| 126 | DECLARE_EVENT(Resume); |
| 127 | DECLARE_EVENT(Finished); |
| 128 | DECLARE_EVENT(Failed); |
| 129 | |
| 130 | static constexpr Event NoEvent = {Event::NoEvent, 0, 0}; |
| 131 | |
| 132 | static std::string toString(const Event& event) { |
| 133 | std::string eventStr; |
| 134 | switch (event.type) { |
| 135 | case Event::Start: |
| 136 | eventStr = "Start"; |
| 137 | break; |
| 138 | case Event::Pause: |
| 139 | eventStr = "Pause"; |
| 140 | break; |
| 141 | case Event::Resume: |
| 142 | eventStr = "Resume"; |
| 143 | break; |
| 144 | case Event::Finished: |
| 145 | eventStr = "Finished"; |
| 146 | break; |
| 147 | case Event::Failed: |
| 148 | eventStr = "Failed"; |
| 149 | break; |
| 150 | default: |
| 151 | return "NoEvent"; |
| 152 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 153 | return "session {" + std::to_string(event.clientId) + ", " + |
| 154 | std::to_string(event.sessionId) + "}: " + eventStr; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Pop 1 event from front, wait for up to timeoutUs if empty. |
| 158 | const Event& pop(int64_t timeoutUs = 0) { |
| 159 | std::unique_lock lock(mLock); |
| 160 | |
| 161 | if (mEventQueue.empty() && timeoutUs > 0) { |
| 162 | mCondition.wait_for(lock, std::chrono::microseconds(timeoutUs)); |
| 163 | } |
| 164 | |
| 165 | if (mEventQueue.empty()) { |
| 166 | mPoppedEvent = NoEvent; |
| 167 | } else { |
| 168 | mPoppedEvent = *mEventQueue.begin(); |
| 169 | mEventQueue.pop_front(); |
| 170 | } |
| 171 | |
| 172 | return mPoppedEvent; |
| 173 | } |
| 174 | |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 175 | bool waitForSpecificEventAndPop(const Event& target, std::list<Event>* outEvents, |
| 176 | int64_t timeoutUs = 0) { |
| 177 | std::unique_lock lock(mLock); |
| 178 | |
| 179 | auto startTime = std::chrono::system_clock::now(); |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 180 | int64_t remainingUs = timeoutUs; |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 181 | |
| 182 | std::list<Event>::iterator it; |
| 183 | while (((it = std::find(mEventQueue.begin(), mEventQueue.end(), target)) == |
| 184 | mEventQueue.end()) && |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 185 | remainingUs > 0) { |
| 186 | std::cv_status status = |
| 187 | mCondition.wait_for(lock, std::chrono::microseconds(remainingUs)); |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 188 | if (status == std::cv_status::timeout) { |
| 189 | break; |
| 190 | } |
| 191 | std::chrono::microseconds elapsedTime = std::chrono::system_clock::now() - startTime; |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 192 | remainingUs = timeoutUs - elapsedTime.count(); |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if (it == mEventQueue.end()) { |
| 196 | return false; |
| 197 | } |
| 198 | *outEvents = std::list<Event>(mEventQueue.begin(), std::next(it)); |
| 199 | mEventQueue.erase(mEventQueue.begin(), std::next(it)); |
| 200 | return true; |
| 201 | } |
| 202 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 203 | // Push 1 event to back. |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 204 | void append(const Event& event, |
| 205 | const TranscodingErrorCode err = TranscodingErrorCode::kNoError) { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 206 | ALOGD("%s", toString(event).c_str()); |
| 207 | |
| 208 | std::unique_lock lock(mLock); |
| 209 | |
| 210 | mEventQueue.push_back(event); |
Chong Zhang | 87d199c | 2021-03-01 19:02:18 -0800 | [diff] [blame^] | 211 | if (err != TranscodingErrorCode::kNoError) { |
| 212 | mLastErrQueue.push_back(err); |
| 213 | } |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 214 | mCondition.notify_one(); |
| 215 | } |
| 216 | |
Chong Zhang | 98b8a37 | 2020-07-08 17:27:37 -0700 | [diff] [blame] | 217 | void updateProgress(int progress) { |
| 218 | std::unique_lock lock(mLock); |
| 219 | mLastProgress = progress; |
| 220 | mUpdateCount++; |
| 221 | } |
| 222 | |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 223 | int getUpdateCount(int* lastProgress) { |
Chong Zhang | 98b8a37 | 2020-07-08 17:27:37 -0700 | [diff] [blame] | 224 | std::unique_lock lock(mLock); |
| 225 | *lastProgress = mLastProgress; |
| 226 | return mUpdateCount; |
| 227 | } |
| 228 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 229 | TranscodingErrorCode getLastError() { |
| 230 | std::unique_lock lock(mLock); |
Chong Zhang | 87d199c | 2021-03-01 19:02:18 -0800 | [diff] [blame^] | 231 | if (mLastErrQueue.empty()) { |
| 232 | return TranscodingErrorCode::kNoError; |
| 233 | } |
| 234 | TranscodingErrorCode err = mLastErrQueue.front(); |
| 235 | mLastErrQueue.pop_front(); |
| 236 | return err; |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 239 | private: |
| 240 | std::mutex mLock; |
| 241 | std::condition_variable mCondition; |
| 242 | Event mPoppedEvent; |
| 243 | std::list<Event> mEventQueue; |
Chong Zhang | 87d199c | 2021-03-01 19:02:18 -0800 | [diff] [blame^] | 244 | std::list<TranscodingErrorCode> mLastErrQueue; |
Chong Zhang | 98b8a37 | 2020-07-08 17:27:37 -0700 | [diff] [blame] | 245 | int mUpdateCount = 0; |
| 246 | int mLastProgress = -1; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | // Operators for GTest macros. |
| 250 | bool operator==(const EventTracker::Event& lhs, const EventTracker::Event& rhs) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 251 | return lhs.type == rhs.type && lhs.clientId == rhs.clientId && lhs.sessionId == rhs.sessionId; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | std::ostream& operator<<(std::ostream& str, const EventTracker::Event& v) { |
| 255 | str << EventTracker::toString(v); |
| 256 | return str; |
| 257 | } |
| 258 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 259 | static constexpr bool success = true; |
| 260 | static constexpr bool fail = false; |
| 261 | |
| 262 | struct TestClientCallback : public BnTranscodingClientCallback, |
| 263 | public EventTracker, |
| 264 | public std::enable_shared_from_this<TestClientCallback> { |
| 265 | TestClientCallback(const char* packageName, int32_t id) |
| 266 | : mClientId(id), mClientPid(PID(id)), mClientUid(UID(id)), mPackageName(packageName) { |
| 267 | ALOGI("TestClientCallback %d created: pid %d, uid %d", id, PID(id), UID(id)); |
| 268 | |
| 269 | // Use package uid if that's available. |
| 270 | uid_t packageUid; |
| 271 | if (getUidForPackage(String16(packageName), 0 /*userId*/, packageUid) == NO_ERROR) { |
| 272 | mClientUid = packageUid; |
| 273 | } |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | virtual ~TestClientCallback() { ALOGI("TestClientCallback %d destroyed", mClientId); } |
| 277 | |
| 278 | Status openFileDescriptor(const std::string& in_fileUri, const std::string& in_mode, |
| 279 | ::ndk::ScopedFileDescriptor* _aidl_return) override { |
| 280 | ALOGD("@@@ openFileDescriptor: %s", in_fileUri.c_str()); |
| 281 | int fd; |
| 282 | if (in_mode == "w" || in_mode == "rw") { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 283 | int kOpenFlags; |
| 284 | if (in_mode == "w") { |
| 285 | // Write-only, create file if non-existent, truncate existing file. |
| 286 | kOpenFlags = O_WRONLY | O_CREAT | O_TRUNC; |
| 287 | } else { |
| 288 | // Read-Write, create if non-existent, no truncate (service will truncate if needed) |
| 289 | kOpenFlags = O_RDWR | O_CREAT; |
| 290 | } |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 291 | // User R+W permission. |
| 292 | constexpr int kFileMode = S_IRUSR | S_IWUSR; |
| 293 | fd = open(in_fileUri.c_str(), kOpenFlags, kFileMode); |
| 294 | } else { |
| 295 | fd = open(in_fileUri.c_str(), O_RDONLY); |
| 296 | } |
| 297 | _aidl_return->set(fd); |
| 298 | return Status::ok(); |
| 299 | } |
| 300 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 301 | Status onTranscodingStarted(int32_t in_sessionId) override { |
| 302 | append(EventTracker::Start(mClientId, in_sessionId)); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 303 | return Status::ok(); |
| 304 | } |
| 305 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 306 | Status onTranscodingPaused(int32_t in_sessionId) override { |
| 307 | append(EventTracker::Pause(mClientId, in_sessionId)); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 308 | return Status::ok(); |
| 309 | } |
| 310 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 311 | Status onTranscodingResumed(int32_t in_sessionId) override { |
| 312 | append(EventTracker::Resume(mClientId, in_sessionId)); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 313 | return Status::ok(); |
| 314 | } |
| 315 | |
| 316 | Status onTranscodingFinished( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 317 | int32_t in_sessionId, |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 318 | const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 319 | append(Finished(mClientId, in_sessionId)); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 320 | return Status::ok(); |
| 321 | } |
| 322 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 323 | Status onTranscodingFailed(int32_t in_sessionId, |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 324 | ::aidl::android::media::TranscodingErrorCode in_errorCode) override { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 325 | append(Failed(mClientId, in_sessionId), in_errorCode); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 326 | return Status::ok(); |
| 327 | } |
| 328 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 329 | Status onAwaitNumberOfSessionsChanged(int32_t /* in_sessionId */, |
| 330 | int32_t /* in_oldAwaitNumber */, |
| 331 | int32_t /* in_newAwaitNumber */) override { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 332 | return Status::ok(); |
| 333 | } |
| 334 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 335 | Status onProgressUpdate(int32_t /* in_sessionId */, int32_t in_progress) override { |
Chong Zhang | 98b8a37 | 2020-07-08 17:27:37 -0700 | [diff] [blame] | 336 | updateProgress(in_progress); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 337 | return Status::ok(); |
| 338 | } |
| 339 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 340 | Status registerClient(const char* packageName, |
| 341 | const std::shared_ptr<IMediaTranscodingService>& service) { |
| 342 | // Override the default uid if the package uid is found. |
| 343 | uid_t uid; |
| 344 | if (getUidForPackage(String16(packageName), 0 /*userId*/, uid) == NO_ERROR) { |
| 345 | mClientUid = uid; |
| 346 | } |
| 347 | |
| 348 | ALOGD("registering %s with uid %d", packageName, mClientUid); |
| 349 | |
| 350 | std::shared_ptr<ITranscodingClient> client; |
| 351 | Status status = |
| 352 | service->registerClient(shared_from_this(), kClientName, packageName, &client); |
| 353 | |
| 354 | mClient = status.isOk() ? client : nullptr; |
| 355 | return status; |
| 356 | } |
| 357 | |
| 358 | Status unregisterClient() { |
| 359 | Status status; |
| 360 | if (mClient != nullptr) { |
| 361 | status = mClient->unregister(); |
| 362 | mClient = nullptr; |
| 363 | } |
| 364 | return status; |
| 365 | } |
| 366 | |
| 367 | template <bool expectation = success> |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 368 | bool submit(int32_t sessionId, const char* sourceFilePath, const char* destinationFilePath, |
| 369 | TranscodingSessionPriority priority = TranscodingSessionPriority::kNormal, |
Chong Zhang | 456787f | 2021-02-18 16:47:08 -0800 | [diff] [blame] | 370 | int bitrateBps = -1, int overridePid = -1, int overrideUid = -1, |
| 371 | int sessionDurationMs = -1) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 372 | constexpr bool shouldSucceed = (expectation == success); |
| 373 | bool result; |
| 374 | TranscodingRequestParcel request; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 375 | TranscodingSessionParcel session; |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 376 | |
| 377 | request.sourceFilePath = sourceFilePath; |
| 378 | request.destinationFilePath = destinationFilePath; |
| 379 | request.priority = priority; |
| 380 | request.clientPid = (overridePid == -1) ? mClientPid : overridePid; |
| 381 | request.clientUid = (overrideUid == -1) ? mClientUid : overrideUid; |
Chong Zhang | cf3f8ee | 2020-10-29 18:38:37 -0700 | [diff] [blame] | 382 | request.clientPackageName = (overrideUid == -1) ? mPackageName : ""; |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 383 | if (bitrateBps > 0) { |
| 384 | request.requestedVideoTrackFormat.emplace(TranscodingVideoTrackFormat()); |
| 385 | request.requestedVideoTrackFormat->bitrateBps = bitrateBps; |
| 386 | } |
Chong Zhang | 456787f | 2021-02-18 16:47:08 -0800 | [diff] [blame] | 387 | if (sessionDurationMs > 0) { |
| 388 | request.isForTesting = true; |
| 389 | request.testConfig.emplace(TranscodingTestConfig()); |
| 390 | request.testConfig->processingTotalTimeMs = sessionDurationMs; |
| 391 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 392 | Status status = mClient->submitRequest(request, &session, &result); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 393 | |
| 394 | EXPECT_TRUE(status.isOk()); |
| 395 | EXPECT_EQ(result, shouldSucceed); |
| 396 | if (shouldSucceed) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 397 | EXPECT_EQ(session.sessionId, sessionId); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 400 | return status.isOk() && (result == shouldSucceed) && |
| 401 | (!shouldSucceed || session.sessionId == sessionId); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | template <bool expectation = success> |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 405 | bool cancel(int32_t sessionId) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 406 | constexpr bool shouldSucceed = (expectation == success); |
| 407 | bool result; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 408 | Status status = mClient->cancelSession(sessionId, &result); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 409 | |
| 410 | EXPECT_TRUE(status.isOk()); |
| 411 | EXPECT_EQ(result, shouldSucceed); |
| 412 | |
| 413 | return status.isOk() && (result == shouldSucceed); |
| 414 | } |
| 415 | |
| 416 | template <bool expectation = success> |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 417 | bool getSession(int32_t sessionId, const char* sourceFilePath, |
| 418 | const char* destinationFilePath) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 419 | constexpr bool shouldSucceed = (expectation == success); |
| 420 | bool result; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 421 | TranscodingSessionParcel session; |
| 422 | Status status = mClient->getSessionWithId(sessionId, &session, &result); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 423 | |
| 424 | EXPECT_TRUE(status.isOk()); |
| 425 | EXPECT_EQ(result, shouldSucceed); |
| 426 | if (shouldSucceed) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 427 | EXPECT_EQ(session.sessionId, sessionId); |
| 428 | EXPECT_EQ(session.request.sourceFilePath, sourceFilePath); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | return status.isOk() && (result == shouldSucceed) && |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 432 | (!shouldSucceed || (session.sessionId == sessionId && |
| 433 | session.request.sourceFilePath == sourceFilePath && |
| 434 | session.request.destinationFilePath == destinationFilePath)); |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 437 | int32_t mClientId; |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 438 | pid_t mClientPid; |
| 439 | uid_t mClientUid; |
| 440 | std::string mPackageName; |
| 441 | std::shared_ptr<ITranscodingClient> mClient; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | class MediaTranscodingServiceTestBase : public ::testing::Test { |
| 445 | public: |
| 446 | MediaTranscodingServiceTestBase() { ALOGI("MediaTranscodingServiceTestBase created"); } |
| 447 | |
| 448 | virtual ~MediaTranscodingServiceTestBase() { |
| 449 | ALOGI("MediaTranscodingServiceTestBase destroyed"); |
| 450 | } |
| 451 | |
| 452 | void SetUp() override { |
| 453 | // Need thread pool to receive callbacks, otherwise oneway callbacks are |
| 454 | // silently ignored. |
| 455 | ABinderProcess_startThreadPool(); |
| 456 | ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding")); |
| 457 | mService = IMediaTranscodingService::fromBinder(binder); |
| 458 | if (mService == nullptr) { |
| 459 | ALOGE("Failed to connect to the media.trascoding service."); |
| 460 | return; |
| 461 | } |
Chong Zhang | 4fe34f9 | 2020-10-06 15:47:28 -0700 | [diff] [blame] | 462 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 463 | mClient1 = ::ndk::SharedRefBase::make<TestClientCallback>(kClientPackageA, 1); |
| 464 | mClient2 = ::ndk::SharedRefBase::make<TestClientCallback>(kClientPackageB, 2); |
| 465 | mClient3 = ::ndk::SharedRefBase::make<TestClientCallback>(kClientPackageC, 3); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 468 | Status registerOneClient(const std::shared_ptr<TestClientCallback>& callback) { |
| 469 | ALOGD("registering %s with uid %d", callback->mPackageName.c_str(), callback->mClientUid); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 470 | |
| 471 | std::shared_ptr<ITranscodingClient> client; |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 472 | Status status = |
| 473 | mService->registerClient(callback, kClientName, callback->mPackageName, &client); |
| 474 | |
| 475 | if (status.isOk()) { |
| 476 | callback->mClient = client; |
| 477 | } else { |
| 478 | callback->mClient = nullptr; |
| 479 | } |
| 480 | return status; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void registerMultipleClients() { |
| 484 | // Register 3 clients. |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 485 | EXPECT_TRUE(registerOneClient(mClient1).isOk()); |
| 486 | EXPECT_TRUE(registerOneClient(mClient2).isOk()); |
| 487 | EXPECT_TRUE(registerOneClient(mClient3).isOk()); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 488 | |
| 489 | // Check the number of clients. |
| 490 | int32_t numOfClients; |
| 491 | Status status = mService->getNumOfClients(&numOfClients); |
| 492 | EXPECT_TRUE(status.isOk()); |
Chong Zhang | 4fe34f9 | 2020-10-06 15:47:28 -0700 | [diff] [blame] | 493 | EXPECT_GE(numOfClients, 3); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void unregisterMultipleClients() { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 497 | // Unregister the clients. |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 498 | EXPECT_TRUE(mClient1->unregisterClient().isOk()); |
| 499 | EXPECT_TRUE(mClient2->unregisterClient().isOk()); |
| 500 | EXPECT_TRUE(mClient3->unregisterClient().isOk()); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 503 | void deleteFile(const char* path) { unlink(path); } |
| 504 | |
| 505 | std::shared_ptr<IMediaTranscodingService> mService; |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 506 | std::shared_ptr<TestClientCallback> mClient1; |
| 507 | std::shared_ptr<TestClientCallback> mClient2; |
| 508 | std::shared_ptr<TestClientCallback> mClient3; |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | } // namespace media |
| 512 | } // namespace android |