blob: 0151b3d770eb327ae04d94614ea27cd8925b8d0c [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 Zhang457c6892021-02-01 15:34:20 -080050SimulatedTranscoder::SimulatedTranscoder(const std::shared_ptr<TranscoderCallbackInterface>& cb,
51 int64_t heartBeatUs __unused)
52 : mCallback(cb), mLooperReady(false) {
53 ALOGV("SimulatedTranscoder CTOR: %p", this);
Chong Zhang75222182020-04-29 14:43:42 -070054}
55
Chong Zhang457c6892021-02-01 15:34:20 -080056SimulatedTranscoder::~SimulatedTranscoder() {
57 ALOGV("SimulatedTranscoder DTOR: %p", this);
Chong Zhang75222182020-04-29 14:43:42 -070058}
59
Chong Zhang66469272020-06-04 16:51:55 -070060void SimulatedTranscoder::start(
Chong Zhangbc062482020-10-14 16:43:53 -070061 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Linus Nilssona99f4042021-02-25 15:49:43 -080062 uid_t /*callingUid*/,
Chong Zhang66469272020-06-04 16:51:55 -070063 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhang456787f2021-02-18 16:47:08 -080064 {
65 auto lock = std::scoped_lock(mLock);
66 int64_t processingTimeUs = kSessionDurationUs;
67 if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) {
68 processingTimeUs = request.testConfig->processingTotalTimeMs * 1000;
69 }
70 ALOGI("%s: session {%lld, %d}: processingTimeUs: %lld", __FUNCTION__, (long long)clientId,
71 sessionId, (long long)processingTimeUs);
72 SessionKeyType key = std::make_pair(clientId, sessionId);
73 mRemainingTimeMap.emplace(key, processingTimeUs);
hkuanga9ffd592020-06-05 10:38:02 -070074 }
Chong Zhang456787f2021-02-18 16:47:08 -080075
Chong Zhangbc062482020-10-14 16:43:53 -070076 queueEvent(Event::Start, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070077 auto callback = mCallback.lock();
78 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070079 callback->onStarted(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070080 }
81 });
Chong Zhang75222182020-04-29 14:43:42 -070082}
83
Chong Zhangbc062482020-10-14 16:43:53 -070084void SimulatedTranscoder::pause(ClientIdType clientId, SessionIdType sessionId) {
85 queueEvent(Event::Pause, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070086 auto callback = mCallback.lock();
87 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070088 callback->onPaused(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070089 }
90 });
Chong Zhang75222182020-04-29 14:43:42 -070091}
92
Chong Zhangb55c5452020-06-26 14:32:12 -070093void SimulatedTranscoder::resume(
Chong Zhangbc062482020-10-14 16:43:53 -070094 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& /*request*/,
Linus Nilssona99f4042021-02-25 15:49:43 -080095 uid_t /*callingUid*/,
Chong Zhangb55c5452020-06-26 14:32:12 -070096 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangbc062482020-10-14 16:43:53 -070097 queueEvent(Event::Resume, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070098 auto callback = mCallback.lock();
99 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700100 callback->onResumed(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -0700101 }
102 });
Chong Zhang75222182020-04-29 14:43:42 -0700103}
104
Chong Zhang457c6892021-02-01 15:34:20 -0800105void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) {
Chong Zhangbc062482020-10-14 16:43:53 -0700106 queueEvent(Event::Stop, clientId, sessionId, nullptr);
Chong Zhang457c6892021-02-01 15:34:20 -0800107
108 if (abandon) {
109 queueEvent(Event::Abandon, 0, 0, nullptr);
110 }
Chong Zhang00feca22020-05-08 15:02:06 -0700111}
112
Chong Zhangbc062482020-10-14 16:43:53 -0700113void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId,
114 SessionIdType sessionId, std::function<void()> runnable) {
115 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId,
116 toString(type));
Chong Zhang75222182020-04-29 14:43:42 -0700117
118 auto lock = std::scoped_lock(mLock);
119
Chong Zhang457c6892021-02-01 15:34:20 -0800120 if (!mLooperReady) {
121 // A shared_ptr to ourselves is given to the thread's stack, so that SimulatedTranscoder
122 // object doesn't go away until the thread exits. When a watchdog timeout happens, this
123 // allows the session controller to release its reference to the TranscoderWrapper object
124 // without blocking on the thread exits.
125 std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach();
126 mLooperReady = true;
127 }
128
Chong Zhangbc062482020-10-14 16:43:53 -0700129 mQueue.push_back({type, clientId, sessionId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -0700130 mCondition.notify_one();
131}
132
133void SimulatedTranscoder::threadLoop() {
134 bool running = false;
Chong Zhang75222182020-04-29 14:43:42 -0700135 std::chrono::system_clock::time_point lastRunningTime;
136 Event lastRunningEvent;
137
138 std::unique_lock<std::mutex> lock(mLock);
139 // SimulatedTranscoder currently lives in the transcoding service, as long as
140 // MediaTranscodingService itself.
141 while (true) {
142 // Wait for the next event.
143 while (mQueue.empty()) {
144 if (!running) {
145 mCondition.wait(lock);
146 continue;
147 }
Chong Zhangbc062482020-10-14 16:43:53 -0700148 // If running, wait for the remaining life of this session. Report finish if timed out.
Chong Zhang456787f2021-02-18 16:47:08 -0800149 SessionKeyType key =
150 std::make_pair(lastRunningEvent.clientId, lastRunningEvent.sessionId);
151 std::cv_status status = mCondition.wait_for(lock, mRemainingTimeMap[key]);
Chong Zhang75222182020-04-29 14:43:42 -0700152 if (status == std::cv_status::timeout) {
153 running = false;
154
155 auto callback = mCallback.lock();
156 if (callback != nullptr) {
Chong Zhang456787f2021-02-18 16:47:08 -0800157 mRemainingTimeMap.erase(key);
158
Chong Zhang75222182020-04-29 14:43:42 -0700159 lock.unlock();
Chong Zhangbc062482020-10-14 16:43:53 -0700160 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId);
Chong Zhang75222182020-04-29 14:43:42 -0700161 lock.lock();
162 }
163 } else {
164 // Advance last running time and remaining time. This is needed to guard
165 // against bad events (which will be ignored) or spurious wakeups, in that
166 // case we don't want to wait for the same time again.
167 auto now = std::chrono::system_clock::now();
Chong Zhang456787f2021-02-18 16:47:08 -0800168 mRemainingTimeMap[key] -= (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;
187 lastRunningTime = std::chrono::system_clock::now();
188 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 {
198 mRemainingTimeMap[key] -= (std::chrono::system_clock::now() - lastRunningTime);
199 }
Chong Zhang457c6892021-02-01 15:34:20 -0800200 } else {
201 ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__,
202 (long long)event.clientId, event.sessionId, toString(event.type));
203 continue;
204 }
Chong Zhang75222182020-04-29 14:43:42 -0700205
Chong Zhang457c6892021-02-01 15:34:20 -0800206 if (event.runnable != nullptr) {
207 lock.unlock();
208 event.runnable();
209 lock.lock();
Chong Zhang75222182020-04-29 14:43:42 -0700210 }
211 }
212}
213
214} // namespace android