Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "TranscodingSessionController" |
| 19 | |
| 20 | #define VALIDATE_STATE 1 |
| 21 | |
| 22 | #include <inttypes.h> |
| 23 | #include <media/TranscodingSessionController.h> |
| 24 | #include <media/TranscodingUidPolicy.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <utility> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | static_assert((SessionIdType)-1 < 0, "SessionIdType should be signed"); |
| 32 | |
| 33 | constexpr static uid_t OFFLINE_UID = -1; |
| 34 | |
| 35 | //static |
| 36 | String8 TranscodingSessionController::sessionToString(const SessionKeyType& sessionKey) { |
| 37 | return String8::format("{client:%lld, session:%d}", (long long)sessionKey.first, |
| 38 | sessionKey.second); |
| 39 | } |
| 40 | |
| 41 | //static |
| 42 | const char* TranscodingSessionController::sessionStateToString(const Session::State sessionState) { |
| 43 | switch (sessionState) { |
| 44 | case Session::State::NOT_STARTED: |
| 45 | return "NOT_STARTED"; |
| 46 | case Session::State::RUNNING: |
| 47 | return "RUNNING"; |
| 48 | case Session::State::PAUSED: |
| 49 | return "PAUSED"; |
| 50 | default: |
| 51 | break; |
| 52 | } |
| 53 | return "(unknown)"; |
| 54 | } |
| 55 | |
| 56 | TranscodingSessionController::TranscodingSessionController( |
| 57 | const std::shared_ptr<TranscoderInterface>& transcoder, |
| 58 | const std::shared_ptr<UidPolicyInterface>& uidPolicy, |
| 59 | const std::shared_ptr<ResourcePolicyInterface>& resourcePolicy) |
| 60 | : mTranscoder(transcoder), |
| 61 | mUidPolicy(uidPolicy), |
| 62 | mResourcePolicy(resourcePolicy), |
| 63 | mCurrentSession(nullptr), |
| 64 | mResourceLost(false) { |
| 65 | // Only push empty offline queue initially. Realtime queues are added when requests come in. |
| 66 | mUidSortedList.push_back(OFFLINE_UID); |
| 67 | mOfflineUidIterator = mUidSortedList.begin(); |
| 68 | mSessionQueues.emplace(OFFLINE_UID, SessionQueueType()); |
Chong Zhang | cf3f8ee | 2020-10-29 18:38:37 -0700 | [diff] [blame] | 69 | mUidPackageNames[OFFLINE_UID] = "(offline)"; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TranscodingSessionController::~TranscodingSessionController() {} |
| 73 | |
| 74 | void TranscodingSessionController::dumpAllSessions(int fd, const Vector<String16>& args __unused) { |
| 75 | String8 result; |
| 76 | |
| 77 | const size_t SIZE = 256; |
| 78 | char buffer[SIZE]; |
| 79 | std::scoped_lock lock{mLock}; |
| 80 | |
| 81 | snprintf(buffer, SIZE, "\n========== Dumping all sessions queues =========\n"); |
| 82 | result.append(buffer); |
| 83 | snprintf(buffer, SIZE, " Total num of Sessions: %zu\n", mSessionMap.size()); |
| 84 | result.append(buffer); |
| 85 | |
| 86 | std::vector<int32_t> uids(mUidSortedList.begin(), mUidSortedList.end()); |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 87 | |
| 88 | for (int32_t i = 0; i < uids.size(); i++) { |
| 89 | const uid_t uid = uids[i]; |
| 90 | |
| 91 | if (mSessionQueues[uid].empty()) { |
| 92 | continue; |
| 93 | } |
| 94 | snprintf(buffer, SIZE, " Uid: %d, pkg: %s\n", uid, |
Chong Zhang | cf3f8ee | 2020-10-29 18:38:37 -0700 | [diff] [blame] | 95 | mUidPackageNames.count(uid) > 0 ? mUidPackageNames[uid].c_str() : "(unknown)"); |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 96 | result.append(buffer); |
| 97 | snprintf(buffer, SIZE, " Num of sessions: %zu\n", mSessionQueues[uid].size()); |
| 98 | result.append(buffer); |
| 99 | for (auto& sessionKey : mSessionQueues[uid]) { |
| 100 | auto sessionIt = mSessionMap.find(sessionKey); |
| 101 | if (sessionIt == mSessionMap.end()) { |
| 102 | snprintf(buffer, SIZE, "Failed to look up Session %s \n", |
| 103 | sessionToString(sessionKey).c_str()); |
| 104 | result.append(buffer); |
| 105 | continue; |
| 106 | } |
| 107 | Session& session = sessionIt->second; |
| 108 | TranscodingRequestParcel& request = session.request; |
| 109 | snprintf(buffer, SIZE, " Session: %s, %s, %d%%\n", |
| 110 | sessionToString(sessionKey).c_str(), sessionStateToString(session.state), |
| 111 | session.lastProgress); |
| 112 | result.append(buffer); |
| 113 | snprintf(buffer, SIZE, " Src: %s\n", request.sourceFilePath.c_str()); |
| 114 | result.append(buffer); |
| 115 | snprintf(buffer, SIZE, " Dst: %s\n", request.destinationFilePath.c_str()); |
| 116 | result.append(buffer); |
Chong Zhang | cf3f8ee | 2020-10-29 18:38:37 -0700 | [diff] [blame] | 117 | // For the offline queue, print out the original client. |
| 118 | if (uid == OFFLINE_UID) { |
| 119 | snprintf(buffer, SIZE, " Original Client: %s\n", |
| 120 | request.clientPackageName.c_str()); |
| 121 | result.append(buffer); |
| 122 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | write(fd, result.string(), result.size()); |
| 127 | } |
| 128 | |
| 129 | TranscodingSessionController::Session* TranscodingSessionController::getTopSession_l() { |
| 130 | if (mSessionMap.empty()) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | uid_t topUid = *mUidSortedList.begin(); |
| 134 | SessionKeyType topSessionKey = *mSessionQueues[topUid].begin(); |
| 135 | return &mSessionMap[topSessionKey]; |
| 136 | } |
| 137 | |
| 138 | void TranscodingSessionController::updateCurrentSession_l() { |
| 139 | Session* topSession = getTopSession_l(); |
| 140 | Session* curSession = mCurrentSession; |
| 141 | ALOGV("updateCurrentSession: topSession is %s, curSession is %s", |
| 142 | topSession == nullptr ? "null" : sessionToString(topSession->key).c_str(), |
| 143 | curSession == nullptr ? "null" : sessionToString(curSession->key).c_str()); |
| 144 | |
| 145 | // If we found a topSession that should be run, and it's not already running, |
| 146 | // take some actions to ensure it's running. |
| 147 | if (topSession != nullptr && |
| 148 | (topSession != curSession || topSession->state != Session::RUNNING)) { |
| 149 | // If another session is currently running, pause it first. |
| 150 | if (curSession != nullptr && curSession->state == Session::RUNNING) { |
| 151 | mTranscoder->pause(curSession->key.first, curSession->key.second); |
| 152 | curSession->state = Session::PAUSED; |
| 153 | } |
| 154 | // If we are not experiencing resource loss, we can start or resume |
| 155 | // the topSession now. |
| 156 | if (!mResourceLost) { |
| 157 | if (topSession->state == Session::NOT_STARTED) { |
| 158 | mTranscoder->start(topSession->key.first, topSession->key.second, |
| 159 | topSession->request, topSession->callback.lock()); |
| 160 | } else if (topSession->state == Session::PAUSED) { |
| 161 | mTranscoder->resume(topSession->key.first, topSession->key.second, |
| 162 | topSession->request, topSession->callback.lock()); |
| 163 | } |
| 164 | topSession->state = Session::RUNNING; |
| 165 | } |
| 166 | } |
| 167 | mCurrentSession = topSession; |
| 168 | } |
| 169 | |
| 170 | void TranscodingSessionController::removeSession_l(const SessionKeyType& sessionKey) { |
| 171 | ALOGV("%s: session %s", __FUNCTION__, sessionToString(sessionKey).c_str()); |
| 172 | |
| 173 | if (mSessionMap.count(sessionKey) == 0) { |
| 174 | ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str()); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Remove session from uid's queue. |
| 179 | const uid_t uid = mSessionMap[sessionKey].uid; |
| 180 | SessionQueueType& sessionQueue = mSessionQueues[uid]; |
| 181 | auto it = std::find(sessionQueue.begin(), sessionQueue.end(), sessionKey); |
| 182 | if (it == sessionQueue.end()) { |
| 183 | ALOGE("couldn't find session %s in queue for uid %d", sessionToString(sessionKey).c_str(), |
| 184 | uid); |
| 185 | return; |
| 186 | } |
| 187 | sessionQueue.erase(it); |
| 188 | |
| 189 | // If this is the last session in a real-time queue, remove this uid's queue. |
| 190 | if (uid != OFFLINE_UID && sessionQueue.empty()) { |
| 191 | mUidSortedList.remove(uid); |
| 192 | mSessionQueues.erase(uid); |
| 193 | mUidPolicy->unregisterMonitorUid(uid); |
| 194 | |
| 195 | std::unordered_set<uid_t> topUids = mUidPolicy->getTopUids(); |
| 196 | moveUidsToTop_l(topUids, false /*preserveTopUid*/); |
| 197 | } |
| 198 | |
| 199 | // Clear current session. |
| 200 | if (mCurrentSession == &mSessionMap[sessionKey]) { |
| 201 | mCurrentSession = nullptr; |
| 202 | } |
| 203 | |
| 204 | // Remove session from session map. |
| 205 | mSessionMap.erase(sessionKey); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Moves the set of uids to the front of mUidSortedList (which is used to pick |
| 210 | * the next session to run). |
| 211 | * |
| 212 | * This is called when 1) we received a onTopUidsChanged() callback from UidPolicy, |
| 213 | * or 2) we removed the session queue for a uid because it becomes empty. |
| 214 | * |
| 215 | * In case of 1), if there are multiple uids in the set, and the current front |
| 216 | * uid in mUidSortedList is still in the set, we try to keep that uid at front |
| 217 | * so that current session run is not interrupted. (This is not a concern for case 2) |
| 218 | * because the queue for a uid was just removed entirely.) |
| 219 | */ |
| 220 | void TranscodingSessionController::moveUidsToTop_l(const std::unordered_set<uid_t>& uids, |
| 221 | bool preserveTopUid) { |
| 222 | // If uid set is empty, nothing to do. Do not change the queue status. |
| 223 | if (uids.empty()) { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Save the current top uid. |
| 228 | uid_t curTopUid = *mUidSortedList.begin(); |
| 229 | bool pushCurTopToFront = false; |
| 230 | int32_t numUidsMoved = 0; |
| 231 | |
| 232 | // Go through the sorted uid list once, and move the ones in top set to front. |
| 233 | for (auto it = mUidSortedList.begin(); it != mUidSortedList.end();) { |
| 234 | uid_t uid = *it; |
| 235 | |
| 236 | if (uid != OFFLINE_UID && uids.count(uid) > 0) { |
| 237 | it = mUidSortedList.erase(it); |
| 238 | |
| 239 | // If this is the top we're preserving, don't push it here, push |
| 240 | // it after the for-loop. |
| 241 | if (uid == curTopUid && preserveTopUid) { |
| 242 | pushCurTopToFront = true; |
| 243 | } else { |
| 244 | mUidSortedList.push_front(uid); |
| 245 | } |
| 246 | |
| 247 | // If we found all uids in the set, break out. |
| 248 | if (++numUidsMoved == uids.size()) { |
| 249 | break; |
| 250 | } |
| 251 | } else { |
| 252 | ++it; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (pushCurTopToFront) { |
| 257 | mUidSortedList.push_front(curTopUid); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | bool TranscodingSessionController::submit( |
| 262 | ClientIdType clientId, SessionIdType sessionId, uid_t uid, |
| 263 | const TranscodingRequestParcel& request, |
| 264 | const std::weak_ptr<ITranscodingClientCallback>& callback) { |
| 265 | SessionKeyType sessionKey = std::make_pair(clientId, sessionId); |
| 266 | |
| 267 | ALOGV("%s: session %s, uid %d, prioirty %d", __FUNCTION__, sessionToString(sessionKey).c_str(), |
| 268 | uid, (int32_t)request.priority); |
| 269 | |
| 270 | std::scoped_lock lock{mLock}; |
| 271 | |
| 272 | if (mSessionMap.count(sessionKey) > 0) { |
| 273 | ALOGE("session %s already exists", sessionToString(sessionKey).c_str()); |
| 274 | return false; |
| 275 | } |
| 276 | |
Chong Zhang | cf3f8ee | 2020-10-29 18:38:37 -0700 | [diff] [blame] | 277 | // Add the uid package name to the store of package names we already know. |
| 278 | if (mUidPackageNames.count(uid) == 0) { |
| 279 | mUidPackageNames.emplace(uid, request.clientPackageName); |
| 280 | } |
| 281 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 282 | // TODO(chz): only support offline vs real-time for now. All kUnspecified sessions |
| 283 | // go to offline queue. |
| 284 | if (request.priority == TranscodingSessionPriority::kUnspecified) { |
| 285 | uid = OFFLINE_UID; |
| 286 | } |
| 287 | |
| 288 | // Add session to session map. |
| 289 | mSessionMap[sessionKey].key = sessionKey; |
| 290 | mSessionMap[sessionKey].uid = uid; |
| 291 | mSessionMap[sessionKey].state = Session::NOT_STARTED; |
| 292 | mSessionMap[sessionKey].lastProgress = 0; |
| 293 | mSessionMap[sessionKey].request = request; |
| 294 | mSessionMap[sessionKey].callback = callback; |
| 295 | |
| 296 | // If it's an offline session, the queue was already added in constructor. |
| 297 | // If it's a real-time sessions, check if a queue is already present for the uid, |
| 298 | // and add a new queue if needed. |
| 299 | if (uid != OFFLINE_UID) { |
| 300 | if (mSessionQueues.count(uid) == 0) { |
| 301 | mUidPolicy->registerMonitorUid(uid); |
| 302 | if (mUidPolicy->isUidOnTop(uid)) { |
| 303 | mUidSortedList.push_front(uid); |
| 304 | } else { |
| 305 | // Shouldn't be submitting real-time requests from non-top app, |
| 306 | // put it in front of the offline queue. |
| 307 | mUidSortedList.insert(mOfflineUidIterator, uid); |
| 308 | } |
| 309 | } else if (uid != *mUidSortedList.begin()) { |
| 310 | if (mUidPolicy->isUidOnTop(uid)) { |
| 311 | mUidSortedList.remove(uid); |
| 312 | mUidSortedList.push_front(uid); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | // Append this session to the uid's queue. |
| 317 | mSessionQueues[uid].push_back(sessionKey); |
| 318 | |
| 319 | updateCurrentSession_l(); |
| 320 | |
| 321 | validateState_l(); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | bool TranscodingSessionController::cancel(ClientIdType clientId, SessionIdType sessionId) { |
| 326 | SessionKeyType sessionKey = std::make_pair(clientId, sessionId); |
| 327 | |
| 328 | ALOGV("%s: session %s", __FUNCTION__, sessionToString(sessionKey).c_str()); |
| 329 | |
| 330 | std::list<SessionKeyType> sessionsToRemove; |
| 331 | |
| 332 | std::scoped_lock lock{mLock}; |
| 333 | |
| 334 | if (sessionId < 0) { |
| 335 | for (auto it = mSessionMap.begin(); it != mSessionMap.end(); ++it) { |
| 336 | if (it->first.first == clientId && it->second.uid != OFFLINE_UID) { |
| 337 | sessionsToRemove.push_back(it->first); |
| 338 | } |
| 339 | } |
| 340 | } else { |
| 341 | if (mSessionMap.count(sessionKey) == 0) { |
| 342 | ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str()); |
| 343 | return false; |
| 344 | } |
| 345 | sessionsToRemove.push_back(sessionKey); |
| 346 | } |
| 347 | |
| 348 | for (auto it = sessionsToRemove.begin(); it != sessionsToRemove.end(); ++it) { |
| 349 | // If the session has ever been started, stop it now. |
| 350 | // Note that stop() is needed even if the session is currently paused. This instructs |
| 351 | // the transcoder to discard any states for the session, otherwise the states may |
| 352 | // never be discarded. |
| 353 | if (mSessionMap[*it].state != Session::NOT_STARTED) { |
| 354 | mTranscoder->stop(it->first, it->second); |
| 355 | } |
| 356 | |
| 357 | // Remove the session. |
| 358 | removeSession_l(*it); |
| 359 | } |
| 360 | |
| 361 | // Start next session. |
| 362 | updateCurrentSession_l(); |
| 363 | |
| 364 | validateState_l(); |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | bool TranscodingSessionController::getSession(ClientIdType clientId, SessionIdType sessionId, |
| 369 | TranscodingRequestParcel* request) { |
| 370 | SessionKeyType sessionKey = std::make_pair(clientId, sessionId); |
| 371 | |
| 372 | std::scoped_lock lock{mLock}; |
| 373 | |
| 374 | if (mSessionMap.count(sessionKey) == 0) { |
| 375 | ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str()); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | *(TranscodingRequest*)request = mSessionMap[sessionKey].request; |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | void TranscodingSessionController::notifyClient(ClientIdType clientId, SessionIdType sessionId, |
| 384 | const char* reason, |
| 385 | std::function<void(const SessionKeyType&)> func) { |
| 386 | SessionKeyType sessionKey = std::make_pair(clientId, sessionId); |
| 387 | |
| 388 | std::scoped_lock lock{mLock}; |
| 389 | |
| 390 | if (mSessionMap.count(sessionKey) == 0) { |
| 391 | ALOGW("%s: ignoring %s for session %s that doesn't exist", __FUNCTION__, reason, |
| 392 | sessionToString(sessionKey).c_str()); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // Only ignore if session was never started. In particular, propagate the status |
| 397 | // to client if the session is paused. Transcoder could have posted finish when |
| 398 | // we're pausing it, and the finish arrived after we changed current session. |
| 399 | if (mSessionMap[sessionKey].state == Session::NOT_STARTED) { |
| 400 | ALOGW("%s: ignoring %s for session %s that was never started", __FUNCTION__, reason, |
| 401 | sessionToString(sessionKey).c_str()); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | ALOGV("%s: session %s %s", __FUNCTION__, sessionToString(sessionKey).c_str(), reason); |
| 406 | func(sessionKey); |
| 407 | } |
| 408 | |
| 409 | void TranscodingSessionController::onStarted(ClientIdType clientId, SessionIdType sessionId) { |
| 410 | notifyClient(clientId, sessionId, "started", [=](const SessionKeyType& sessionKey) { |
| 411 | auto callback = mSessionMap[sessionKey].callback.lock(); |
| 412 | if (callback != nullptr) { |
| 413 | callback->onTranscodingStarted(sessionId); |
| 414 | } |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | void TranscodingSessionController::onPaused(ClientIdType clientId, SessionIdType sessionId) { |
| 419 | notifyClient(clientId, sessionId, "paused", [=](const SessionKeyType& sessionKey) { |
| 420 | auto callback = mSessionMap[sessionKey].callback.lock(); |
| 421 | if (callback != nullptr) { |
| 422 | callback->onTranscodingPaused(sessionId); |
| 423 | } |
| 424 | }); |
| 425 | } |
| 426 | |
| 427 | void TranscodingSessionController::onResumed(ClientIdType clientId, SessionIdType sessionId) { |
| 428 | notifyClient(clientId, sessionId, "resumed", [=](const SessionKeyType& sessionKey) { |
| 429 | auto callback = mSessionMap[sessionKey].callback.lock(); |
| 430 | if (callback != nullptr) { |
| 431 | callback->onTranscodingResumed(sessionId); |
| 432 | } |
| 433 | }); |
| 434 | } |
| 435 | |
| 436 | void TranscodingSessionController::onFinish(ClientIdType clientId, SessionIdType sessionId) { |
| 437 | notifyClient(clientId, sessionId, "finish", [=](const SessionKeyType& sessionKey) { |
| 438 | { |
| 439 | auto clientCallback = mSessionMap[sessionKey].callback.lock(); |
| 440 | if (clientCallback != nullptr) { |
| 441 | clientCallback->onTranscodingFinished( |
| 442 | sessionId, TranscodingResultParcel({sessionId, -1 /*actualBitrateBps*/, |
| 443 | std::nullopt /*sessionStats*/})); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Remove the session. |
| 448 | removeSession_l(sessionKey); |
| 449 | |
| 450 | // Start next session. |
| 451 | updateCurrentSession_l(); |
| 452 | |
| 453 | validateState_l(); |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | void TranscodingSessionController::onError(ClientIdType clientId, SessionIdType sessionId, |
| 458 | TranscodingErrorCode err) { |
| 459 | notifyClient(clientId, sessionId, "error", [=](const SessionKeyType& sessionKey) { |
| 460 | { |
| 461 | auto clientCallback = mSessionMap[sessionKey].callback.lock(); |
| 462 | if (clientCallback != nullptr) { |
| 463 | clientCallback->onTranscodingFailed(sessionId, err); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Remove the session. |
| 468 | removeSession_l(sessionKey); |
| 469 | |
| 470 | // Start next session. |
| 471 | updateCurrentSession_l(); |
| 472 | |
| 473 | validateState_l(); |
| 474 | }); |
| 475 | } |
| 476 | |
| 477 | void TranscodingSessionController::onProgressUpdate(ClientIdType clientId, SessionIdType sessionId, |
| 478 | int32_t progress) { |
| 479 | notifyClient(clientId, sessionId, "progress", [=](const SessionKeyType& sessionKey) { |
| 480 | auto callback = mSessionMap[sessionKey].callback.lock(); |
| 481 | if (callback != nullptr) { |
| 482 | callback->onProgressUpdate(sessionId, progress); |
| 483 | } |
| 484 | mSessionMap[sessionKey].lastProgress = progress; |
| 485 | }); |
| 486 | } |
| 487 | |
Chong Zhang | effd896 | 2020-12-02 14:29:09 -0800 | [diff] [blame] | 488 | void TranscodingSessionController::onResourceLost(ClientIdType clientId, SessionIdType sessionId) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 489 | ALOGI("%s", __FUNCTION__); |
| 490 | |
Chong Zhang | effd896 | 2020-12-02 14:29:09 -0800 | [diff] [blame] | 491 | notifyClient(clientId, sessionId, "resource_lost", [=](const SessionKeyType& sessionKey) { |
| 492 | if (mResourceLost) { |
| 493 | return; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 494 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 495 | |
Chong Zhang | effd896 | 2020-12-02 14:29:09 -0800 | [diff] [blame] | 496 | Session* resourceLostSession = &mSessionMap[sessionKey]; |
| 497 | if (resourceLostSession->state != Session::RUNNING) { |
| 498 | ALOGW("session %s lost resource but is no longer running", |
| 499 | sessionToString(sessionKey).c_str()); |
| 500 | return; |
| 501 | } |
| 502 | // If we receive a resource loss event, the transcoder already paused the transcoding, |
| 503 | // so we don't need to call onPaused() to pause it. However, we still need to notify |
| 504 | // the client and update the session state here. |
| 505 | resourceLostSession->state = Session::PAUSED; |
| 506 | // Notify the client as a paused event. |
| 507 | auto clientCallback = resourceLostSession->callback.lock(); |
| 508 | if (clientCallback != nullptr) { |
| 509 | clientCallback->onTranscodingPaused(sessionKey.second); |
| 510 | } |
| 511 | mResourcePolicy->setPidResourceLost(resourceLostSession->request.clientPid); |
| 512 | mResourceLost = true; |
| 513 | |
| 514 | validateState_l(); |
| 515 | }); |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void TranscodingSessionController::onTopUidsChanged(const std::unordered_set<uid_t>& uids) { |
| 519 | if (uids.empty()) { |
| 520 | ALOGW("%s: ignoring empty uids", __FUNCTION__); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | std::string uidStr; |
| 525 | for (auto it = uids.begin(); it != uids.end(); it++) { |
| 526 | if (!uidStr.empty()) { |
| 527 | uidStr += ", "; |
| 528 | } |
| 529 | uidStr += std::to_string(*it); |
| 530 | } |
| 531 | |
| 532 | ALOGD("%s: topUids: size %zu, uids: %s", __FUNCTION__, uids.size(), uidStr.c_str()); |
| 533 | |
| 534 | std::scoped_lock lock{mLock}; |
| 535 | |
| 536 | moveUidsToTop_l(uids, true /*preserveTopUid*/); |
| 537 | |
| 538 | updateCurrentSession_l(); |
| 539 | |
| 540 | validateState_l(); |
| 541 | } |
| 542 | |
| 543 | void TranscodingSessionController::onResourceAvailable() { |
| 544 | std::scoped_lock lock{mLock}; |
| 545 | |
| 546 | if (!mResourceLost) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | ALOGI("%s", __FUNCTION__); |
| 551 | |
| 552 | mResourceLost = false; |
| 553 | updateCurrentSession_l(); |
| 554 | |
| 555 | validateState_l(); |
| 556 | } |
| 557 | |
| 558 | void TranscodingSessionController::validateState_l() { |
| 559 | #ifdef VALIDATE_STATE |
| 560 | LOG_ALWAYS_FATAL_IF(mSessionQueues.count(OFFLINE_UID) != 1, |
| 561 | "mSessionQueues offline queue number is not 1"); |
| 562 | LOG_ALWAYS_FATAL_IF(*mOfflineUidIterator != OFFLINE_UID, |
| 563 | "mOfflineUidIterator not pointing to offline uid"); |
| 564 | LOG_ALWAYS_FATAL_IF(mUidSortedList.size() != mSessionQueues.size(), |
| 565 | "mUidList and mSessionQueues size mismatch"); |
| 566 | |
| 567 | int32_t totalSessions = 0; |
| 568 | for (auto uid : mUidSortedList) { |
| 569 | LOG_ALWAYS_FATAL_IF(mSessionQueues.count(uid) != 1, |
| 570 | "mSessionQueues count for uid %d is not 1", uid); |
| 571 | for (auto& sessionKey : mSessionQueues[uid]) { |
| 572 | LOG_ALWAYS_FATAL_IF(mSessionMap.count(sessionKey) != 1, |
| 573 | "mSessions count for session %s is not 1", |
| 574 | sessionToString(sessionKey).c_str()); |
| 575 | } |
| 576 | |
| 577 | totalSessions += mSessionQueues[uid].size(); |
| 578 | } |
| 579 | LOG_ALWAYS_FATAL_IF(mSessionMap.size() != totalSessions, |
| 580 | "mSessions size doesn't match total sessions counted from uid queues"); |
| 581 | #endif // VALIDATE_STATE |
| 582 | } |
| 583 | |
| 584 | } // namespace android |