blob: 1de1f7b90c52a35b5fcc0c707984a2f270b1fdde [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*/) {
hkuang34915b12020-06-18 09:36:39 -070063 if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) {
Chong Zhangbc062482020-10-14 16:43:53 -070064 mSessionProcessingTimeMs = request.testConfig->processingTotalTimeMs;
hkuanga9ffd592020-06-05 10:38:02 -070065 }
Chong Zhangbc062482020-10-14 16:43:53 -070066 ALOGV("%s: session {%d}: processingTime: %lld", __FUNCTION__, sessionId,
67 (long long)mSessionProcessingTimeMs);
68 queueEvent(Event::Start, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070069 auto callback = mCallback.lock();
70 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070071 callback->onStarted(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070072 }
73 });
Chong Zhang75222182020-04-29 14:43:42 -070074}
75
Chong Zhangbc062482020-10-14 16:43:53 -070076void SimulatedTranscoder::pause(ClientIdType clientId, SessionIdType sessionId) {
77 queueEvent(Event::Pause, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070078 auto callback = mCallback.lock();
79 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070080 callback->onPaused(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070081 }
82 });
Chong Zhang75222182020-04-29 14:43:42 -070083}
84
Chong Zhangb55c5452020-06-26 14:32:12 -070085void SimulatedTranscoder::resume(
Chong Zhangbc062482020-10-14 16:43:53 -070086 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& /*request*/,
Chong Zhangb55c5452020-06-26 14:32:12 -070087 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangbc062482020-10-14 16:43:53 -070088 queueEvent(Event::Resume, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070089 auto callback = mCallback.lock();
90 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070091 callback->onResumed(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070092 }
93 });
Chong Zhang75222182020-04-29 14:43:42 -070094}
95
Chong Zhang457c6892021-02-01 15:34:20 -080096void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) {
Chong Zhangbc062482020-10-14 16:43:53 -070097 queueEvent(Event::Stop, clientId, sessionId, nullptr);
Chong Zhang457c6892021-02-01 15:34:20 -080098
99 if (abandon) {
100 queueEvent(Event::Abandon, 0, 0, nullptr);
101 }
Chong Zhang00feca22020-05-08 15:02:06 -0700102}
103
Chong Zhangbc062482020-10-14 16:43:53 -0700104void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId,
105 SessionIdType sessionId, std::function<void()> runnable) {
106 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId,
107 toString(type));
Chong Zhang75222182020-04-29 14:43:42 -0700108
109 auto lock = std::scoped_lock(mLock);
110
Chong Zhang457c6892021-02-01 15:34:20 -0800111 if (!mLooperReady) {
112 // A shared_ptr to ourselves is given to the thread's stack, so that SimulatedTranscoder
113 // object doesn't go away until the thread exits. When a watchdog timeout happens, this
114 // allows the session controller to release its reference to the TranscoderWrapper object
115 // without blocking on the thread exits.
116 std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach();
117 mLooperReady = true;
118 }
119
Chong Zhangbc062482020-10-14 16:43:53 -0700120 mQueue.push_back({type, clientId, sessionId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -0700121 mCondition.notify_one();
122}
123
124void SimulatedTranscoder::threadLoop() {
125 bool running = false;
Chong Zhangbc062482020-10-14 16:43:53 -0700126 std::chrono::microseconds remainingUs(kSessionDurationUs);
Chong Zhang75222182020-04-29 14:43:42 -0700127 std::chrono::system_clock::time_point lastRunningTime;
128 Event lastRunningEvent;
129
130 std::unique_lock<std::mutex> lock(mLock);
131 // SimulatedTranscoder currently lives in the transcoding service, as long as
132 // MediaTranscodingService itself.
133 while (true) {
134 // Wait for the next event.
135 while (mQueue.empty()) {
136 if (!running) {
137 mCondition.wait(lock);
138 continue;
139 }
Chong Zhangbc062482020-10-14 16:43:53 -0700140 // If running, wait for the remaining life of this session. Report finish if timed out.
Chong Zhang75222182020-04-29 14:43:42 -0700141 std::cv_status status = mCondition.wait_for(lock, remainingUs);
142 if (status == std::cv_status::timeout) {
143 running = false;
144
145 auto callback = mCallback.lock();
146 if (callback != nullptr) {
147 lock.unlock();
Chong Zhangbc062482020-10-14 16:43:53 -0700148 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId);
Chong Zhang75222182020-04-29 14:43:42 -0700149 lock.lock();
150 }
151 } else {
152 // Advance last running time and remaining time. This is needed to guard
153 // against bad events (which will be ignored) or spurious wakeups, in that
154 // case we don't want to wait for the same time again.
155 auto now = std::chrono::system_clock::now();
156 remainingUs -= (now - lastRunningTime);
157 lastRunningTime = now;
158 }
159 }
160
161 // Handle the events, adjust state and send updates to client accordingly.
Chong Zhang457c6892021-02-01 15:34:20 -0800162 Event event = *mQueue.begin();
163 mQueue.pop_front();
Chong Zhang75222182020-04-29 14:43:42 -0700164
Chong Zhang457c6892021-02-01 15:34:20 -0800165 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId,
166 event.sessionId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700167
Chong Zhang457c6892021-02-01 15:34:20 -0800168 if (event.type == Event::Abandon) {
169 break;
170 }
171
172 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
173 running = true;
174 lastRunningTime = std::chrono::system_clock::now();
175 lastRunningEvent = event;
176 if (event.type == Event::Start) {
177 remainingUs = std::chrono::milliseconds(mSessionProcessingTimeMs);
Chong Zhang75222182020-04-29 14:43:42 -0700178 }
Chong Zhang457c6892021-02-01 15:34:20 -0800179 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
180 running = false;
181 remainingUs -= (std::chrono::system_clock::now() - lastRunningTime);
182 } else {
183 ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__,
184 (long long)event.clientId, event.sessionId, toString(event.type));
185 continue;
186 }
Chong Zhang75222182020-04-29 14:43:42 -0700187
Chong Zhang457c6892021-02-01 15:34:20 -0800188 if (event.runnable != nullptr) {
189 lock.unlock();
190 event.runnable();
191 lock.lock();
Chong Zhang75222182020-04-29 14:43:42 -0700192 }
193 }
194}
195
196} // namespace android