blob: 09ad3cd8f5b2585312d2d5691daac64ecafeb944 [file] [log] [blame]
Chong Zhangbc062482020-10-14 16:43:53 -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//#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
29namespace android {
30
31static_assert((SessionIdType)-1 < 0, "SessionIdType should be signed");
32
33constexpr static uid_t OFFLINE_UID = -1;
Chong Zhanga1320c52020-12-15 14:30:12 -080034constexpr static size_t kSessionHistoryMax = 100;
Chong Zhangbc062482020-10-14 16:43:53 -070035
36//static
37String8 TranscodingSessionController::sessionToString(const SessionKeyType& sessionKey) {
38 return String8::format("{client:%lld, session:%d}", (long long)sessionKey.first,
39 sessionKey.second);
40}
41
42//static
43const char* TranscodingSessionController::sessionStateToString(const Session::State sessionState) {
44 switch (sessionState) {
45 case Session::State::NOT_STARTED:
46 return "NOT_STARTED";
47 case Session::State::RUNNING:
48 return "RUNNING";
49 case Session::State::PAUSED:
50 return "PAUSED";
Chong Zhanga1320c52020-12-15 14:30:12 -080051 case Session::State::FINISHED:
52 return "FINISHED";
53 case Session::State::CANCELED:
54 return "CANCELED";
55 case Session::State::ERROR:
56 return "ERROR";
Chong Zhangbc062482020-10-14 16:43:53 -070057 default:
58 break;
59 }
60 return "(unknown)";
61}
62
63TranscodingSessionController::TranscodingSessionController(
64 const std::shared_ptr<TranscoderInterface>& transcoder,
65 const std::shared_ptr<UidPolicyInterface>& uidPolicy,
Chong Zhang8677f1f2021-01-21 20:37:35 +000066 const std::shared_ptr<ResourcePolicyInterface>& resourcePolicy,
67 const std::shared_ptr<ThermalPolicyInterface>& thermalPolicy)
Chong Zhangbc062482020-10-14 16:43:53 -070068 : mTranscoder(transcoder),
69 mUidPolicy(uidPolicy),
70 mResourcePolicy(resourcePolicy),
Chong Zhang8677f1f2021-01-21 20:37:35 +000071 mThermalPolicy(thermalPolicy),
Chong Zhangbc062482020-10-14 16:43:53 -070072 mCurrentSession(nullptr),
73 mResourceLost(false) {
74 // Only push empty offline queue initially. Realtime queues are added when requests come in.
75 mUidSortedList.push_back(OFFLINE_UID);
76 mOfflineUidIterator = mUidSortedList.begin();
77 mSessionQueues.emplace(OFFLINE_UID, SessionQueueType());
Chong Zhangcf3f8ee2020-10-29 18:38:37 -070078 mUidPackageNames[OFFLINE_UID] = "(offline)";
Chong Zhang8677f1f2021-01-21 20:37:35 +000079 mThermalThrottling = thermalPolicy->getThrottlingStatus();
Chong Zhangbc062482020-10-14 16:43:53 -070080}
81
82TranscodingSessionController::~TranscodingSessionController() {}
83
Chong Zhanga1320c52020-12-15 14:30:12 -080084void TranscodingSessionController::dumpSession_l(const Session& session, String8& result,
85 bool closedSession) {
86 const size_t SIZE = 256;
87 char buffer[SIZE];
88 const TranscodingRequestParcel& request = session.request;
89 snprintf(buffer, SIZE, " Session: %s, %s, %d%%\n", sessionToString(session.key).c_str(),
90 sessionStateToString(session.getState()), session.lastProgress);
91 result.append(buffer);
92 snprintf(buffer, SIZE, " pkg: %s\n", request.clientPackageName.c_str());
93 result.append(buffer);
94 snprintf(buffer, SIZE, " src: %s\n", request.sourceFilePath.c_str());
95 result.append(buffer);
96 snprintf(buffer, SIZE, " dst: %s\n", request.destinationFilePath.c_str());
97 result.append(buffer);
98
99 if (closedSession) {
100 snprintf(buffer, SIZE,
101 " waiting: %.1fs, running: %.1fs, paused: %.1fs, paused count: %d\n",
102 session.waitingTime.count() / 1000000.0f, session.runningTime.count() / 1000000.0f,
103 session.pausedTime.count() / 1000000.0f, session.pauseCount);
104 result.append(buffer);
105 }
106}
107
Chong Zhangbc062482020-10-14 16:43:53 -0700108void TranscodingSessionController::dumpAllSessions(int fd, const Vector<String16>& args __unused) {
109 String8 result;
110
111 const size_t SIZE = 256;
112 char buffer[SIZE];
113 std::scoped_lock lock{mLock};
114
Chong Zhanga1320c52020-12-15 14:30:12 -0800115 snprintf(buffer, SIZE, "\n========== Dumping live sessions queues =========\n");
Chong Zhangbc062482020-10-14 16:43:53 -0700116 result.append(buffer);
117 snprintf(buffer, SIZE, " Total num of Sessions: %zu\n", mSessionMap.size());
118 result.append(buffer);
119
120 std::vector<int32_t> uids(mUidSortedList.begin(), mUidSortedList.end());
Chong Zhangbc062482020-10-14 16:43:53 -0700121
122 for (int32_t i = 0; i < uids.size(); i++) {
123 const uid_t uid = uids[i];
124
125 if (mSessionQueues[uid].empty()) {
126 continue;
127 }
Chong Zhanga1320c52020-12-15 14:30:12 -0800128 snprintf(buffer, SIZE, " uid: %d, pkg: %s\n", uid,
Chong Zhangcf3f8ee2020-10-29 18:38:37 -0700129 mUidPackageNames.count(uid) > 0 ? mUidPackageNames[uid].c_str() : "(unknown)");
Chong Zhangbc062482020-10-14 16:43:53 -0700130 result.append(buffer);
131 snprintf(buffer, SIZE, " Num of sessions: %zu\n", mSessionQueues[uid].size());
132 result.append(buffer);
133 for (auto& sessionKey : mSessionQueues[uid]) {
134 auto sessionIt = mSessionMap.find(sessionKey);
135 if (sessionIt == mSessionMap.end()) {
136 snprintf(buffer, SIZE, "Failed to look up Session %s \n",
137 sessionToString(sessionKey).c_str());
138 result.append(buffer);
139 continue;
140 }
Chong Zhanga1320c52020-12-15 14:30:12 -0800141 dumpSession_l(sessionIt->second, result);
Chong Zhangbc062482020-10-14 16:43:53 -0700142 }
143 }
144
Chong Zhanga1320c52020-12-15 14:30:12 -0800145 snprintf(buffer, SIZE, "\n========== Dumping past sessions =========\n");
146 result.append(buffer);
Chong Zhang8677f1f2021-01-21 20:37:35 +0000147 for (auto& session : mSessionHistory) {
Chong Zhanga1320c52020-12-15 14:30:12 -0800148 dumpSession_l(session, result, true /*closedSession*/);
149 }
150
Chong Zhangbc062482020-10-14 16:43:53 -0700151 write(fd, result.string(), result.size());
152}
153
154TranscodingSessionController::Session* TranscodingSessionController::getTopSession_l() {
155 if (mSessionMap.empty()) {
156 return nullptr;
157 }
158 uid_t topUid = *mUidSortedList.begin();
159 SessionKeyType topSessionKey = *mSessionQueues[topUid].begin();
160 return &mSessionMap[topSessionKey];
161}
162
Chong Zhanga1320c52020-12-15 14:30:12 -0800163void TranscodingSessionController::Session::setState(Session::State newState) {
164 if (state == newState) {
165 return;
166 }
167 auto nowTime = std::chrono::system_clock::now();
168 if (state != INVALID) {
169 std::chrono::microseconds elapsedTime = (nowTime - stateEnterTime);
170 switch (state) {
171 case PAUSED:
172 pausedTime = pausedTime + elapsedTime;
173 break;
174 case RUNNING:
175 runningTime = runningTime + elapsedTime;
176 break;
177 case NOT_STARTED:
178 waitingTime = waitingTime + elapsedTime;
179 break;
180 default:
181 break;
182 }
183 }
184 if (newState == PAUSED) {
185 pauseCount++;
186 }
187 stateEnterTime = nowTime;
188 state = newState;
189}
190
Chong Zhangbc062482020-10-14 16:43:53 -0700191void TranscodingSessionController::updateCurrentSession_l() {
192 Session* topSession = getTopSession_l();
193 Session* curSession = mCurrentSession;
194 ALOGV("updateCurrentSession: topSession is %s, curSession is %s",
195 topSession == nullptr ? "null" : sessionToString(topSession->key).c_str(),
196 curSession == nullptr ? "null" : sessionToString(curSession->key).c_str());
197
Chong Zhang8677f1f2021-01-21 20:37:35 +0000198 if (topSession == nullptr) {
199 mCurrentSession = nullptr;
200 return;
201 }
202
203 bool shouldBeRunning = !((mResourcePolicy != nullptr && mResourceLost) ||
204 (mThermalPolicy != nullptr && mThermalThrottling));
Chong Zhangbc062482020-10-14 16:43:53 -0700205 // If we found a topSession that should be run, and it's not already running,
206 // take some actions to ensure it's running.
Chong Zhang8677f1f2021-01-21 20:37:35 +0000207 if (topSession != curSession ||
208 (shouldBeRunning ^ (topSession->getState() == Session::RUNNING))) {
209 // If current session is running, pause it first. Note this is true for either
210 // cases: 1) If top session is changing, or 2) if top session is not changing but
211 // the topSession's state is changing.
Chong Zhanga1320c52020-12-15 14:30:12 -0800212 if (curSession != nullptr && curSession->getState() == Session::RUNNING) {
Chong Zhangbc062482020-10-14 16:43:53 -0700213 mTranscoder->pause(curSession->key.first, curSession->key.second);
Chong Zhanga1320c52020-12-15 14:30:12 -0800214 curSession->setState(Session::PAUSED);
Chong Zhangbc062482020-10-14 16:43:53 -0700215 }
Chong Zhang8677f1f2021-01-21 20:37:35 +0000216 // If we are not experiencing resource loss nor thermal throttling, we can start
217 // or resume the topSession now.
218 if (shouldBeRunning) {
Chong Zhanga1320c52020-12-15 14:30:12 -0800219 if (topSession->getState() == Session::NOT_STARTED) {
Chong Zhangbc062482020-10-14 16:43:53 -0700220 mTranscoder->start(topSession->key.first, topSession->key.second,
221 topSession->request, topSession->callback.lock());
Chong Zhanga1320c52020-12-15 14:30:12 -0800222 } else if (topSession->getState() == Session::PAUSED) {
Chong Zhangbc062482020-10-14 16:43:53 -0700223 mTranscoder->resume(topSession->key.first, topSession->key.second,
224 topSession->request, topSession->callback.lock());
225 }
Chong Zhanga1320c52020-12-15 14:30:12 -0800226 topSession->setState(Session::RUNNING);
Chong Zhangbc062482020-10-14 16:43:53 -0700227 }
228 }
229 mCurrentSession = topSession;
230}
231
Chong Zhanga1320c52020-12-15 14:30:12 -0800232void TranscodingSessionController::removeSession_l(const SessionKeyType& sessionKey,
233 Session::State finalState) {
Chong Zhangbc062482020-10-14 16:43:53 -0700234 ALOGV("%s: session %s", __FUNCTION__, sessionToString(sessionKey).c_str());
235
236 if (mSessionMap.count(sessionKey) == 0) {
237 ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str());
238 return;
239 }
240
241 // Remove session from uid's queue.
242 const uid_t uid = mSessionMap[sessionKey].uid;
243 SessionQueueType& sessionQueue = mSessionQueues[uid];
244 auto it = std::find(sessionQueue.begin(), sessionQueue.end(), sessionKey);
245 if (it == sessionQueue.end()) {
246 ALOGE("couldn't find session %s in queue for uid %d", sessionToString(sessionKey).c_str(),
247 uid);
248 return;
249 }
250 sessionQueue.erase(it);
251
252 // If this is the last session in a real-time queue, remove this uid's queue.
253 if (uid != OFFLINE_UID && sessionQueue.empty()) {
254 mUidSortedList.remove(uid);
255 mSessionQueues.erase(uid);
256 mUidPolicy->unregisterMonitorUid(uid);
257
258 std::unordered_set<uid_t> topUids = mUidPolicy->getTopUids();
259 moveUidsToTop_l(topUids, false /*preserveTopUid*/);
260 }
261
262 // Clear current session.
263 if (mCurrentSession == &mSessionMap[sessionKey]) {
264 mCurrentSession = nullptr;
265 }
266
Chong Zhanga1320c52020-12-15 14:30:12 -0800267 mSessionMap[sessionKey].setState(finalState);
268 mSessionHistory.push_back(mSessionMap[sessionKey]);
269 if (mSessionHistory.size() > kSessionHistoryMax) {
270 mSessionHistory.erase(mSessionHistory.begin());
271 }
272
Chong Zhangbc062482020-10-14 16:43:53 -0700273 // Remove session from session map.
274 mSessionMap.erase(sessionKey);
275}
276
277/**
278 * Moves the set of uids to the front of mUidSortedList (which is used to pick
279 * the next session to run).
280 *
281 * This is called when 1) we received a onTopUidsChanged() callback from UidPolicy,
282 * or 2) we removed the session queue for a uid because it becomes empty.
283 *
284 * In case of 1), if there are multiple uids in the set, and the current front
285 * uid in mUidSortedList is still in the set, we try to keep that uid at front
286 * so that current session run is not interrupted. (This is not a concern for case 2)
287 * because the queue for a uid was just removed entirely.)
288 */
289void TranscodingSessionController::moveUidsToTop_l(const std::unordered_set<uid_t>& uids,
290 bool preserveTopUid) {
291 // If uid set is empty, nothing to do. Do not change the queue status.
292 if (uids.empty()) {
293 return;
294 }
295
296 // Save the current top uid.
297 uid_t curTopUid = *mUidSortedList.begin();
298 bool pushCurTopToFront = false;
299 int32_t numUidsMoved = 0;
300
301 // Go through the sorted uid list once, and move the ones in top set to front.
302 for (auto it = mUidSortedList.begin(); it != mUidSortedList.end();) {
303 uid_t uid = *it;
304
305 if (uid != OFFLINE_UID && uids.count(uid) > 0) {
306 it = mUidSortedList.erase(it);
307
308 // If this is the top we're preserving, don't push it here, push
309 // it after the for-loop.
310 if (uid == curTopUid && preserveTopUid) {
311 pushCurTopToFront = true;
312 } else {
313 mUidSortedList.push_front(uid);
314 }
315
316 // If we found all uids in the set, break out.
317 if (++numUidsMoved == uids.size()) {
318 break;
319 }
320 } else {
321 ++it;
322 }
323 }
324
325 if (pushCurTopToFront) {
326 mUidSortedList.push_front(curTopUid);
327 }
328}
329
330bool TranscodingSessionController::submit(
331 ClientIdType clientId, SessionIdType sessionId, uid_t uid,
332 const TranscodingRequestParcel& request,
333 const std::weak_ptr<ITranscodingClientCallback>& callback) {
334 SessionKeyType sessionKey = std::make_pair(clientId, sessionId);
335
336 ALOGV("%s: session %s, uid %d, prioirty %d", __FUNCTION__, sessionToString(sessionKey).c_str(),
337 uid, (int32_t)request.priority);
338
339 std::scoped_lock lock{mLock};
340
341 if (mSessionMap.count(sessionKey) > 0) {
342 ALOGE("session %s already exists", sessionToString(sessionKey).c_str());
343 return false;
344 }
345
Chong Zhangcf3f8ee2020-10-29 18:38:37 -0700346 // Add the uid package name to the store of package names we already know.
347 if (mUidPackageNames.count(uid) == 0) {
348 mUidPackageNames.emplace(uid, request.clientPackageName);
349 }
350
Chong Zhangbc062482020-10-14 16:43:53 -0700351 // TODO(chz): only support offline vs real-time for now. All kUnspecified sessions
352 // go to offline queue.
353 if (request.priority == TranscodingSessionPriority::kUnspecified) {
354 uid = OFFLINE_UID;
355 }
356
357 // Add session to session map.
358 mSessionMap[sessionKey].key = sessionKey;
359 mSessionMap[sessionKey].uid = uid;
Chong Zhangbc062482020-10-14 16:43:53 -0700360 mSessionMap[sessionKey].lastProgress = 0;
Chong Zhanga1320c52020-12-15 14:30:12 -0800361 mSessionMap[sessionKey].pauseCount = 0;
Chong Zhangbc062482020-10-14 16:43:53 -0700362 mSessionMap[sessionKey].request = request;
363 mSessionMap[sessionKey].callback = callback;
Chong Zhanga1320c52020-12-15 14:30:12 -0800364 mSessionMap[sessionKey].setState(Session::NOT_STARTED);
Chong Zhangbc062482020-10-14 16:43:53 -0700365
366 // If it's an offline session, the queue was already added in constructor.
367 // If it's a real-time sessions, check if a queue is already present for the uid,
368 // and add a new queue if needed.
369 if (uid != OFFLINE_UID) {
370 if (mSessionQueues.count(uid) == 0) {
371 mUidPolicy->registerMonitorUid(uid);
372 if (mUidPolicy->isUidOnTop(uid)) {
373 mUidSortedList.push_front(uid);
374 } else {
375 // Shouldn't be submitting real-time requests from non-top app,
376 // put it in front of the offline queue.
377 mUidSortedList.insert(mOfflineUidIterator, uid);
378 }
379 } else if (uid != *mUidSortedList.begin()) {
380 if (mUidPolicy->isUidOnTop(uid)) {
381 mUidSortedList.remove(uid);
382 mUidSortedList.push_front(uid);
383 }
384 }
385 }
386 // Append this session to the uid's queue.
387 mSessionQueues[uid].push_back(sessionKey);
388
389 updateCurrentSession_l();
390
391 validateState_l();
392 return true;
393}
394
395bool TranscodingSessionController::cancel(ClientIdType clientId, SessionIdType sessionId) {
396 SessionKeyType sessionKey = std::make_pair(clientId, sessionId);
397
398 ALOGV("%s: session %s", __FUNCTION__, sessionToString(sessionKey).c_str());
399
400 std::list<SessionKeyType> sessionsToRemove;
401
402 std::scoped_lock lock{mLock};
403
404 if (sessionId < 0) {
405 for (auto it = mSessionMap.begin(); it != mSessionMap.end(); ++it) {
406 if (it->first.first == clientId && it->second.uid != OFFLINE_UID) {
407 sessionsToRemove.push_back(it->first);
408 }
409 }
410 } else {
411 if (mSessionMap.count(sessionKey) == 0) {
412 ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str());
413 return false;
414 }
415 sessionsToRemove.push_back(sessionKey);
416 }
417
418 for (auto it = sessionsToRemove.begin(); it != sessionsToRemove.end(); ++it) {
419 // If the session has ever been started, stop it now.
420 // Note that stop() is needed even if the session is currently paused. This instructs
421 // the transcoder to discard any states for the session, otherwise the states may
422 // never be discarded.
Chong Zhanga1320c52020-12-15 14:30:12 -0800423 if (mSessionMap[*it].getState() != Session::NOT_STARTED) {
Chong Zhangbc062482020-10-14 16:43:53 -0700424 mTranscoder->stop(it->first, it->second);
425 }
426
427 // Remove the session.
Chong Zhanga1320c52020-12-15 14:30:12 -0800428 removeSession_l(*it, Session::CANCELED);
Chong Zhangbc062482020-10-14 16:43:53 -0700429 }
430
431 // Start next session.
432 updateCurrentSession_l();
433
434 validateState_l();
435 return true;
436}
437
438bool TranscodingSessionController::getSession(ClientIdType clientId, SessionIdType sessionId,
439 TranscodingRequestParcel* request) {
440 SessionKeyType sessionKey = std::make_pair(clientId, sessionId);
441
442 std::scoped_lock lock{mLock};
443
444 if (mSessionMap.count(sessionKey) == 0) {
445 ALOGE("session %s doesn't exist", sessionToString(sessionKey).c_str());
446 return false;
447 }
448
449 *(TranscodingRequest*)request = mSessionMap[sessionKey].request;
450 return true;
451}
452
453void TranscodingSessionController::notifyClient(ClientIdType clientId, SessionIdType sessionId,
454 const char* reason,
455 std::function<void(const SessionKeyType&)> func) {
456 SessionKeyType sessionKey = std::make_pair(clientId, sessionId);
457
458 std::scoped_lock lock{mLock};
459
460 if (mSessionMap.count(sessionKey) == 0) {
461 ALOGW("%s: ignoring %s for session %s that doesn't exist", __FUNCTION__, reason,
462 sessionToString(sessionKey).c_str());
463 return;
464 }
465
466 // Only ignore if session was never started. In particular, propagate the status
467 // to client if the session is paused. Transcoder could have posted finish when
468 // we're pausing it, and the finish arrived after we changed current session.
Chong Zhanga1320c52020-12-15 14:30:12 -0800469 if (mSessionMap[sessionKey].getState() == Session::NOT_STARTED) {
Chong Zhangbc062482020-10-14 16:43:53 -0700470 ALOGW("%s: ignoring %s for session %s that was never started", __FUNCTION__, reason,
471 sessionToString(sessionKey).c_str());
472 return;
473 }
474
475 ALOGV("%s: session %s %s", __FUNCTION__, sessionToString(sessionKey).c_str(), reason);
476 func(sessionKey);
477}
478
479void TranscodingSessionController::onStarted(ClientIdType clientId, SessionIdType sessionId) {
480 notifyClient(clientId, sessionId, "started", [=](const SessionKeyType& sessionKey) {
481 auto callback = mSessionMap[sessionKey].callback.lock();
482 if (callback != nullptr) {
483 callback->onTranscodingStarted(sessionId);
484 }
485 });
486}
487
488void TranscodingSessionController::onPaused(ClientIdType clientId, SessionIdType sessionId) {
489 notifyClient(clientId, sessionId, "paused", [=](const SessionKeyType& sessionKey) {
490 auto callback = mSessionMap[sessionKey].callback.lock();
491 if (callback != nullptr) {
492 callback->onTranscodingPaused(sessionId);
493 }
494 });
495}
496
497void TranscodingSessionController::onResumed(ClientIdType clientId, SessionIdType sessionId) {
498 notifyClient(clientId, sessionId, "resumed", [=](const SessionKeyType& sessionKey) {
499 auto callback = mSessionMap[sessionKey].callback.lock();
500 if (callback != nullptr) {
501 callback->onTranscodingResumed(sessionId);
502 }
503 });
504}
505
506void TranscodingSessionController::onFinish(ClientIdType clientId, SessionIdType sessionId) {
507 notifyClient(clientId, sessionId, "finish", [=](const SessionKeyType& sessionKey) {
508 {
509 auto clientCallback = mSessionMap[sessionKey].callback.lock();
510 if (clientCallback != nullptr) {
511 clientCallback->onTranscodingFinished(
512 sessionId, TranscodingResultParcel({sessionId, -1 /*actualBitrateBps*/,
513 std::nullopt /*sessionStats*/}));
514 }
515 }
516
517 // Remove the session.
Chong Zhanga1320c52020-12-15 14:30:12 -0800518 removeSession_l(sessionKey, Session::FINISHED);
Chong Zhangbc062482020-10-14 16:43:53 -0700519
520 // Start next session.
521 updateCurrentSession_l();
522
523 validateState_l();
524 });
525}
526
527void TranscodingSessionController::onError(ClientIdType clientId, SessionIdType sessionId,
528 TranscodingErrorCode err) {
529 notifyClient(clientId, sessionId, "error", [=](const SessionKeyType& sessionKey) {
530 {
531 auto clientCallback = mSessionMap[sessionKey].callback.lock();
532 if (clientCallback != nullptr) {
533 clientCallback->onTranscodingFailed(sessionId, err);
534 }
535 }
536
537 // Remove the session.
Chong Zhanga1320c52020-12-15 14:30:12 -0800538 removeSession_l(sessionKey, Session::ERROR);
Chong Zhangbc062482020-10-14 16:43:53 -0700539
540 // Start next session.
541 updateCurrentSession_l();
542
543 validateState_l();
544 });
545}
546
547void TranscodingSessionController::onProgressUpdate(ClientIdType clientId, SessionIdType sessionId,
548 int32_t progress) {
549 notifyClient(clientId, sessionId, "progress", [=](const SessionKeyType& sessionKey) {
550 auto callback = mSessionMap[sessionKey].callback.lock();
551 if (callback != nullptr) {
552 callback->onProgressUpdate(sessionId, progress);
553 }
554 mSessionMap[sessionKey].lastProgress = progress;
555 });
556}
557
Chong Zhangeffd8962020-12-02 14:29:09 -0800558void TranscodingSessionController::onResourceLost(ClientIdType clientId, SessionIdType sessionId) {
Chong Zhangbc062482020-10-14 16:43:53 -0700559 ALOGI("%s", __FUNCTION__);
560
Chong Zhangeffd8962020-12-02 14:29:09 -0800561 notifyClient(clientId, sessionId, "resource_lost", [=](const SessionKeyType& sessionKey) {
562 if (mResourceLost) {
563 return;
Chong Zhangbc062482020-10-14 16:43:53 -0700564 }
Chong Zhangbc062482020-10-14 16:43:53 -0700565
Chong Zhangeffd8962020-12-02 14:29:09 -0800566 Session* resourceLostSession = &mSessionMap[sessionKey];
Chong Zhanga1320c52020-12-15 14:30:12 -0800567 if (resourceLostSession->getState() != Session::RUNNING) {
Chong Zhangeffd8962020-12-02 14:29:09 -0800568 ALOGW("session %s lost resource but is no longer running",
Chong Zhanga1320c52020-12-15 14:30:12 -0800569 sessionToString(sessionKey).c_str());
Chong Zhangeffd8962020-12-02 14:29:09 -0800570 return;
571 }
572 // If we receive a resource loss event, the transcoder already paused the transcoding,
573 // so we don't need to call onPaused() to pause it. However, we still need to notify
574 // the client and update the session state here.
Chong Zhanga1320c52020-12-15 14:30:12 -0800575 resourceLostSession->setState(Session::PAUSED);
Chong Zhangeffd8962020-12-02 14:29:09 -0800576 // Notify the client as a paused event.
577 auto clientCallback = resourceLostSession->callback.lock();
578 if (clientCallback != nullptr) {
579 clientCallback->onTranscodingPaused(sessionKey.second);
580 }
Chong Zhang8677f1f2021-01-21 20:37:35 +0000581 if (mResourcePolicy != nullptr) {
582 mResourcePolicy->setPidResourceLost(resourceLostSession->request.clientPid);
583 }
Chong Zhangeffd8962020-12-02 14:29:09 -0800584 mResourceLost = true;
585
586 validateState_l();
587 });
Chong Zhangbc062482020-10-14 16:43:53 -0700588}
589
590void TranscodingSessionController::onTopUidsChanged(const std::unordered_set<uid_t>& uids) {
591 if (uids.empty()) {
592 ALOGW("%s: ignoring empty uids", __FUNCTION__);
593 return;
594 }
595
596 std::string uidStr;
597 for (auto it = uids.begin(); it != uids.end(); it++) {
598 if (!uidStr.empty()) {
599 uidStr += ", ";
600 }
601 uidStr += std::to_string(*it);
602 }
603
604 ALOGD("%s: topUids: size %zu, uids: %s", __FUNCTION__, uids.size(), uidStr.c_str());
605
606 std::scoped_lock lock{mLock};
607
608 moveUidsToTop_l(uids, true /*preserveTopUid*/);
609
610 updateCurrentSession_l();
611
612 validateState_l();
613}
614
615void TranscodingSessionController::onResourceAvailable() {
616 std::scoped_lock lock{mLock};
617
618 if (!mResourceLost) {
619 return;
620 }
621
622 ALOGI("%s", __FUNCTION__);
623
624 mResourceLost = false;
625 updateCurrentSession_l();
626
627 validateState_l();
628}
629
Chong Zhang8677f1f2021-01-21 20:37:35 +0000630void TranscodingSessionController::onThrottlingStarted() {
631 std::scoped_lock lock{mLock};
632
633 if (mThermalThrottling) {
634 return;
635 }
636
637 ALOGI("%s", __FUNCTION__);
638
639 mThermalThrottling = true;
640 updateCurrentSession_l();
641
642 validateState_l();
643}
644
645void TranscodingSessionController::onThrottlingStopped() {
646 std::scoped_lock lock{mLock};
647
648 if (!mThermalThrottling) {
649 return;
650 }
651
652 ALOGI("%s", __FUNCTION__);
653
654 mThermalThrottling = false;
655 updateCurrentSession_l();
656
657 validateState_l();
658}
659
Chong Zhangbc062482020-10-14 16:43:53 -0700660void TranscodingSessionController::validateState_l() {
661#ifdef VALIDATE_STATE
662 LOG_ALWAYS_FATAL_IF(mSessionQueues.count(OFFLINE_UID) != 1,
663 "mSessionQueues offline queue number is not 1");
664 LOG_ALWAYS_FATAL_IF(*mOfflineUidIterator != OFFLINE_UID,
665 "mOfflineUidIterator not pointing to offline uid");
666 LOG_ALWAYS_FATAL_IF(mUidSortedList.size() != mSessionQueues.size(),
667 "mUidList and mSessionQueues size mismatch");
668
669 int32_t totalSessions = 0;
670 for (auto uid : mUidSortedList) {
671 LOG_ALWAYS_FATAL_IF(mSessionQueues.count(uid) != 1,
672 "mSessionQueues count for uid %d is not 1", uid);
673 for (auto& sessionKey : mSessionQueues[uid]) {
674 LOG_ALWAYS_FATAL_IF(mSessionMap.count(sessionKey) != 1,
675 "mSessions count for session %s is not 1",
676 sessionToString(sessionKey).c_str());
677 }
678
679 totalSessions += mSessionQueues[uid].size();
680 }
681 LOG_ALWAYS_FATAL_IF(mSessionMap.size() != totalSessions,
682 "mSessions size doesn't match total sessions counted from uid queues");
683#endif // VALIDATE_STATE
684}
685
686} // namespace android