blob: db83ccb52c1da82559b0e4dcf8bb7239a0114e65 [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,
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*/,
Chong Zhangb55c5452020-06-26 14:32:12 -070094 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangbc062482020-10-14 16:43:53 -070095 queueEvent(Event::Resume, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070096 auto callback = mCallback.lock();
97 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070098 callback->onResumed(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070099 }
100 });
Chong Zhang75222182020-04-29 14:43:42 -0700101}
102
Chong Zhang457c6892021-02-01 15:34:20 -0800103void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) {
Chong Zhangbc062482020-10-14 16:43:53 -0700104 queueEvent(Event::Stop, clientId, sessionId, nullptr);
Chong Zhang457c6892021-02-01 15:34:20 -0800105
106 if (abandon) {
107 queueEvent(Event::Abandon, 0, 0, nullptr);
108 }
Chong Zhang00feca22020-05-08 15:02:06 -0700109}
110
Chong Zhangbc062482020-10-14 16:43:53 -0700111void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId,
112 SessionIdType sessionId, std::function<void()> runnable) {
113 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId,
114 toString(type));
Chong Zhang75222182020-04-29 14:43:42 -0700115
116 auto lock = std::scoped_lock(mLock);
117
Chong Zhang457c6892021-02-01 15:34:20 -0800118 if (!mLooperReady) {
119 // A shared_ptr to ourselves is given to the thread's stack, so that SimulatedTranscoder
120 // object doesn't go away until the thread exits. When a watchdog timeout happens, this
121 // allows the session controller to release its reference to the TranscoderWrapper object
122 // without blocking on the thread exits.
123 std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach();
124 mLooperReady = true;
125 }
126
Chong Zhangbc062482020-10-14 16:43:53 -0700127 mQueue.push_back({type, clientId, sessionId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -0700128 mCondition.notify_one();
129}
130
131void SimulatedTranscoder::threadLoop() {
132 bool running = false;
Chong Zhang75222182020-04-29 14:43:42 -0700133 std::chrono::system_clock::time_point lastRunningTime;
134 Event lastRunningEvent;
135
136 std::unique_lock<std::mutex> lock(mLock);
137 // SimulatedTranscoder currently lives in the transcoding service, as long as
138 // MediaTranscodingService itself.
139 while (true) {
140 // Wait for the next event.
141 while (mQueue.empty()) {
142 if (!running) {
143 mCondition.wait(lock);
144 continue;
145 }
Chong Zhangbc062482020-10-14 16:43:53 -0700146 // If running, wait for the remaining life of this session. Report finish if timed out.
Chong Zhang456787f2021-02-18 16:47:08 -0800147 SessionKeyType key =
148 std::make_pair(lastRunningEvent.clientId, lastRunningEvent.sessionId);
149 std::cv_status status = mCondition.wait_for(lock, mRemainingTimeMap[key]);
Chong Zhang75222182020-04-29 14:43:42 -0700150 if (status == std::cv_status::timeout) {
151 running = false;
152
153 auto callback = mCallback.lock();
154 if (callback != nullptr) {
Chong Zhang456787f2021-02-18 16:47:08 -0800155 mRemainingTimeMap.erase(key);
156
Chong Zhang75222182020-04-29 14:43:42 -0700157 lock.unlock();
Chong Zhangbc062482020-10-14 16:43:53 -0700158 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId);
Chong Zhang75222182020-04-29 14:43:42 -0700159 lock.lock();
160 }
161 } else {
162 // Advance last running time and remaining time. This is needed to guard
163 // against bad events (which will be ignored) or spurious wakeups, in that
164 // case we don't want to wait for the same time again.
165 auto now = std::chrono::system_clock::now();
Chong Zhang456787f2021-02-18 16:47:08 -0800166 mRemainingTimeMap[key] -= (now - lastRunningTime);
Chong Zhang75222182020-04-29 14:43:42 -0700167 lastRunningTime = now;
168 }
169 }
170
171 // Handle the events, adjust state and send updates to client accordingly.
Chong Zhang457c6892021-02-01 15:34:20 -0800172 Event event = *mQueue.begin();
173 mQueue.pop_front();
Chong Zhang75222182020-04-29 14:43:42 -0700174
Chong Zhang456787f2021-02-18 16:47:08 -0800175 ALOGD("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId,
Chong Zhang457c6892021-02-01 15:34:20 -0800176 event.sessionId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700177
Chong Zhang457c6892021-02-01 15:34:20 -0800178 if (event.type == Event::Abandon) {
179 break;
180 }
181
Chong Zhang456787f2021-02-18 16:47:08 -0800182 SessionKeyType key = std::make_pair(event.clientId, event.sessionId);
Chong Zhang457c6892021-02-01 15:34:20 -0800183 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
184 running = true;
185 lastRunningTime = std::chrono::system_clock::now();
186 lastRunningEvent = event;
Chong Zhang456787f2021-02-18 16:47:08 -0800187 ALOGV("%s: session {%lld, %d}: remaining time: %lld", __FUNCTION__,
188 (long long)event.clientId, event.sessionId,
189 (long long)mRemainingTimeMap[key].count());
190
Chong Zhang457c6892021-02-01 15:34:20 -0800191 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
192 running = false;
Chong Zhang456787f2021-02-18 16:47:08 -0800193 if (event.type == Event::Stop) {
194 mRemainingTimeMap.erase(key);
195 } else {
196 mRemainingTimeMap[key] -= (std::chrono::system_clock::now() - lastRunningTime);
197 }
Chong Zhang457c6892021-02-01 15:34:20 -0800198 } else {
199 ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__,
200 (long long)event.clientId, event.sessionId, toString(event.type));
201 continue;
202 }
Chong Zhang75222182020-04-29 14:43:42 -0700203
Chong Zhang457c6892021-02-01 15:34:20 -0800204 if (event.runnable != nullptr) {
205 lock.unlock();
206 event.runnable();
207 lock.lock();
Chong Zhang75222182020-04-29 14:43:42 -0700208 }
209 }
210}
211
212} // namespace android