blob: e80dbc5dd5468d4d1417fd35867eef883c691b17 [file] [log] [blame]
Chong Zhang75222182020-04-29 14:43:42 -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 "SimulatedTranscoder"
19#include "SimulatedTranscoder.h"
20
21#include <utils/Log.h>
22
23#include <thread>
24
25namespace android {
26
27//static
28const char* SimulatedTranscoder::toString(Event::Type type) {
29 switch (type) {
30 case Event::Start:
31 return "Start";
32 case Event::Pause:
33 return "Pause";
34 case Event::Resume:
35 return "Resume";
Chong Zhang457c6892021-02-01 15:34:20 -080036 case Event::Stop:
37 return "Stop";
38 case Event::Finished:
39 return "Finished";
40 case Event::Failed:
41 return "Failed";
42 case Event::Abandon:
43 return "Abandon";
Chong Zhang75222182020-04-29 14:43:42 -070044 default:
45 break;
46 }
47 return "(unknown)";
48}
49
Chong Zhang87d199c2021-03-01 19:02:18 -080050SimulatedTranscoder::SimulatedTranscoder(const std::shared_ptr<TranscoderCallbackInterface>& cb)
Chong Zhang457c6892021-02-01 15:34:20 -080051 : mCallback(cb), mLooperReady(false) {
52 ALOGV("SimulatedTranscoder CTOR: %p", this);
Chong Zhang75222182020-04-29 14:43:42 -070053}
54
Chong Zhang457c6892021-02-01 15:34:20 -080055SimulatedTranscoder::~SimulatedTranscoder() {
56 ALOGV("SimulatedTranscoder DTOR: %p", this);
Chong Zhang75222182020-04-29 14:43:42 -070057}
58
Chong Zhang66469272020-06-04 16:51:55 -070059void SimulatedTranscoder::start(
Chong Zhangbc062482020-10-14 16:43:53 -070060 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Linus Nilssona99f4042021-02-25 15:49:43 -080061 uid_t /*callingUid*/,
Chong Zhang66469272020-06-04 16:51:55 -070062 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhang456787f2021-02-18 16:47:08 -080063 {
64 auto lock = std::scoped_lock(mLock);
65 int64_t processingTimeUs = kSessionDurationUs;
66 if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) {
67 processingTimeUs = request.testConfig->processingTotalTimeMs * 1000;
68 }
69 ALOGI("%s: session {%lld, %d}: processingTimeUs: %lld", __FUNCTION__, (long long)clientId,
70 sessionId, (long long)processingTimeUs);
71 SessionKeyType key = std::make_pair(clientId, sessionId);
72 mRemainingTimeMap.emplace(key, processingTimeUs);
hkuanga9ffd592020-06-05 10:38:02 -070073 }
Chong Zhang456787f2021-02-18 16:47:08 -080074
Chong Zhangbc062482020-10-14 16:43:53 -070075 queueEvent(Event::Start, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070076 auto callback = mCallback.lock();
77 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070078 callback->onStarted(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070079 }
80 });
Chong Zhang75222182020-04-29 14:43:42 -070081}
82
Chong Zhangbc062482020-10-14 16:43:53 -070083void SimulatedTranscoder::pause(ClientIdType clientId, SessionIdType sessionId) {
84 queueEvent(Event::Pause, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070085 auto callback = mCallback.lock();
86 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070087 callback->onPaused(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070088 }
89 });
Chong Zhang75222182020-04-29 14:43:42 -070090}
91
Chong Zhangb55c5452020-06-26 14:32:12 -070092void SimulatedTranscoder::resume(
Chong Zhangbc062482020-10-14 16:43:53 -070093 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& /*request*/,
Linus Nilssona99f4042021-02-25 15:49:43 -080094 uid_t /*callingUid*/,
Chong Zhangb55c5452020-06-26 14:32:12 -070095 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangbc062482020-10-14 16:43:53 -070096 queueEvent(Event::Resume, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070097 auto callback = mCallback.lock();
98 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070099 callback->onResumed(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -0700100 }
101 });
Chong Zhang75222182020-04-29 14:43:42 -0700102}
103
Chong Zhang457c6892021-02-01 15:34:20 -0800104void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) {
Chong Zhangbc062482020-10-14 16:43:53 -0700105 queueEvent(Event::Stop, clientId, sessionId, nullptr);
Chong Zhang457c6892021-02-01 15:34:20 -0800106
107 if (abandon) {
108 queueEvent(Event::Abandon, 0, 0, nullptr);
109 }
Chong Zhang00feca22020-05-08 15:02:06 -0700110}
111
Chong Zhangbc062482020-10-14 16:43:53 -0700112void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId,
113 SessionIdType sessionId, std::function<void()> runnable) {
114 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId,
115 toString(type));
Chong Zhang75222182020-04-29 14:43:42 -0700116
117 auto lock = std::scoped_lock(mLock);
118
Chong Zhang457c6892021-02-01 15:34:20 -0800119 if (!mLooperReady) {
120 // A shared_ptr to ourselves is given to the thread's stack, so that SimulatedTranscoder
121 // object doesn't go away until the thread exits. When a watchdog timeout happens, this
122 // allows the session controller to release its reference to the TranscoderWrapper object
123 // without blocking on the thread exits.
124 std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach();
125 mLooperReady = true;
126 }
127
Chong Zhangbc062482020-10-14 16:43:53 -0700128 mQueue.push_back({type, clientId, sessionId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -0700129 mCondition.notify_one();
130}
131
132void SimulatedTranscoder::threadLoop() {
133 bool running = false;
Chong Zhang87d199c2021-03-01 19:02:18 -0800134 std::chrono::steady_clock::time_point lastRunningTime;
Chong Zhang75222182020-04-29 14:43:42 -0700135 Event lastRunningEvent;
136
137 std::unique_lock<std::mutex> lock(mLock);
138 // SimulatedTranscoder currently lives in the transcoding service, as long as
139 // MediaTranscodingService itself.
140 while (true) {
141 // Wait for the next event.
142 while (mQueue.empty()) {
143 if (!running) {
144 mCondition.wait(lock);
145 continue;
146 }
Chong Zhangbc062482020-10-14 16:43:53 -0700147 // If running, wait for the remaining life of this session. Report finish if timed out.
Chong Zhang456787f2021-02-18 16:47:08 -0800148 SessionKeyType key =
149 std::make_pair(lastRunningEvent.clientId, lastRunningEvent.sessionId);
150 std::cv_status status = mCondition.wait_for(lock, mRemainingTimeMap[key]);
Chong Zhang75222182020-04-29 14:43:42 -0700151 if (status == std::cv_status::timeout) {
152 running = false;
153
154 auto callback = mCallback.lock();
155 if (callback != nullptr) {
Chong Zhang456787f2021-02-18 16:47:08 -0800156 mRemainingTimeMap.erase(key);
157
Chong Zhang75222182020-04-29 14:43:42 -0700158 lock.unlock();
Chong Zhangbc062482020-10-14 16:43:53 -0700159 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId);
Chong Zhang75222182020-04-29 14:43:42 -0700160 lock.lock();
161 }
162 } else {
163 // Advance last running time and remaining time. This is needed to guard
164 // against bad events (which will be ignored) or spurious wakeups, in that
165 // case we don't want to wait for the same time again.
Chong Zhang87d199c2021-03-01 19:02:18 -0800166 auto now = std::chrono::steady_clock::now();
167 mRemainingTimeMap[key] -= std::chrono::duration_cast<std::chrono::microseconds>(
168 now - lastRunningTime);
Chong Zhang75222182020-04-29 14:43:42 -0700169 lastRunningTime = now;
170 }
171 }
172
173 // Handle the events, adjust state and send updates to client accordingly.
Chong Zhang457c6892021-02-01 15:34:20 -0800174 Event event = *mQueue.begin();
175 mQueue.pop_front();
Chong Zhang75222182020-04-29 14:43:42 -0700176
Chong Zhang456787f2021-02-18 16:47:08 -0800177 ALOGD("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId,
Chong Zhang457c6892021-02-01 15:34:20 -0800178 event.sessionId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700179
Chong Zhang457c6892021-02-01 15:34:20 -0800180 if (event.type == Event::Abandon) {
181 break;
182 }
183
Chong Zhang456787f2021-02-18 16:47:08 -0800184 SessionKeyType key = std::make_pair(event.clientId, event.sessionId);
Chong Zhang457c6892021-02-01 15:34:20 -0800185 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
186 running = true;
Chong Zhang87d199c2021-03-01 19:02:18 -0800187 lastRunningTime = std::chrono::steady_clock::now();
Chong Zhang457c6892021-02-01 15:34:20 -0800188 lastRunningEvent = event;
Chong Zhang456787f2021-02-18 16:47:08 -0800189 ALOGV("%s: session {%lld, %d}: remaining time: %lld", __FUNCTION__,
190 (long long)event.clientId, event.sessionId,
191 (long long)mRemainingTimeMap[key].count());
192
Chong Zhang457c6892021-02-01 15:34:20 -0800193 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
194 running = false;
Chong Zhang456787f2021-02-18 16:47:08 -0800195 if (event.type == Event::Stop) {
196 mRemainingTimeMap.erase(key);
197 } else {
Chong Zhang87d199c2021-03-01 19:02:18 -0800198 mRemainingTimeMap[key] -= std::chrono::duration_cast<std::chrono::microseconds>(
199 std::chrono::steady_clock::now() - lastRunningTime);
Chong Zhang456787f2021-02-18 16:47:08 -0800200 }
Chong Zhang457c6892021-02-01 15:34:20 -0800201 } else {
202 ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__,
203 (long long)event.clientId, event.sessionId, toString(event.type));
204 continue;
205 }
Chong Zhang75222182020-04-29 14:43:42 -0700206
Chong Zhang457c6892021-02-01 15:34:20 -0800207 if (event.runnable != nullptr) {
208 lock.unlock();
209 event.runnable();
210 lock.lock();
Chong Zhang75222182020-04-29 14:43:42 -0700211 }
212 }
213}
214
215} // namespace android