blob: 03ee886fc7338558fbf38e1d5b5ce8c3dcd4aeac [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";
36 default:
37 break;
38 }
39 return "(unknown)";
40}
41
42SimulatedTranscoder::SimulatedTranscoder() {
43 std::thread(&SimulatedTranscoder::threadLoop, this).detach();
44}
45
46void SimulatedTranscoder::setCallback(const std::shared_ptr<TranscoderCallbackInterface>& cb) {
47 mCallback = cb;
48}
49
Chong Zhang66469272020-06-04 16:51:55 -070050void SimulatedTranscoder::start(
Chong Zhangbc062482020-10-14 16:43:53 -070051 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhang66469272020-06-04 16:51:55 -070052 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
hkuang34915b12020-06-18 09:36:39 -070053 if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) {
Chong Zhangbc062482020-10-14 16:43:53 -070054 mSessionProcessingTimeMs = request.testConfig->processingTotalTimeMs;
hkuanga9ffd592020-06-05 10:38:02 -070055 }
Chong Zhangbc062482020-10-14 16:43:53 -070056 ALOGV("%s: session {%d}: processingTime: %lld", __FUNCTION__, sessionId,
57 (long long)mSessionProcessingTimeMs);
58 queueEvent(Event::Start, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070059 auto callback = mCallback.lock();
60 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070061 callback->onStarted(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070062 }
63 });
Chong Zhang75222182020-04-29 14:43:42 -070064}
65
Chong Zhangbc062482020-10-14 16:43:53 -070066void SimulatedTranscoder::pause(ClientIdType clientId, SessionIdType sessionId) {
67 queueEvent(Event::Pause, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070068 auto callback = mCallback.lock();
69 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070070 callback->onPaused(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070071 }
72 });
Chong Zhang75222182020-04-29 14:43:42 -070073}
74
Chong Zhangb55c5452020-06-26 14:32:12 -070075void SimulatedTranscoder::resume(
Chong Zhangbc062482020-10-14 16:43:53 -070076 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& /*request*/,
Chong Zhangb55c5452020-06-26 14:32:12 -070077 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangbc062482020-10-14 16:43:53 -070078 queueEvent(Event::Resume, clientId, sessionId, [=] {
Chong Zhangde60f062020-06-11 17:05:10 -070079 auto callback = mCallback.lock();
80 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -070081 callback->onResumed(clientId, sessionId);
Chong Zhangde60f062020-06-11 17:05:10 -070082 }
83 });
Chong Zhang75222182020-04-29 14:43:42 -070084}
85
Chong Zhangbc062482020-10-14 16:43:53 -070086void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId) {
87 queueEvent(Event::Stop, clientId, sessionId, nullptr);
Chong Zhang00feca22020-05-08 15:02:06 -070088}
89
Chong Zhangbc062482020-10-14 16:43:53 -070090void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId,
91 SessionIdType sessionId, std::function<void()> runnable) {
92 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId,
93 toString(type));
Chong Zhang75222182020-04-29 14:43:42 -070094
95 auto lock = std::scoped_lock(mLock);
96
Chong Zhangbc062482020-10-14 16:43:53 -070097 mQueue.push_back({type, clientId, sessionId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -070098 mCondition.notify_one();
99}
100
101void SimulatedTranscoder::threadLoop() {
102 bool running = false;
Chong Zhangbc062482020-10-14 16:43:53 -0700103 std::chrono::microseconds remainingUs(kSessionDurationUs);
Chong Zhang75222182020-04-29 14:43:42 -0700104 std::chrono::system_clock::time_point lastRunningTime;
105 Event lastRunningEvent;
106
107 std::unique_lock<std::mutex> lock(mLock);
108 // SimulatedTranscoder currently lives in the transcoding service, as long as
109 // MediaTranscodingService itself.
110 while (true) {
111 // Wait for the next event.
112 while (mQueue.empty()) {
113 if (!running) {
114 mCondition.wait(lock);
115 continue;
116 }
Chong Zhangbc062482020-10-14 16:43:53 -0700117 // If running, wait for the remaining life of this session. Report finish if timed out.
Chong Zhang75222182020-04-29 14:43:42 -0700118 std::cv_status status = mCondition.wait_for(lock, remainingUs);
119 if (status == std::cv_status::timeout) {
120 running = false;
121
122 auto callback = mCallback.lock();
123 if (callback != nullptr) {
124 lock.unlock();
Chong Zhangbc062482020-10-14 16:43:53 -0700125 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId);
Chong Zhang75222182020-04-29 14:43:42 -0700126 lock.lock();
127 }
128 } else {
129 // Advance last running time and remaining time. This is needed to guard
130 // against bad events (which will be ignored) or spurious wakeups, in that
131 // case we don't want to wait for the same time again.
132 auto now = std::chrono::system_clock::now();
133 remainingUs -= (now - lastRunningTime);
134 lastRunningTime = now;
135 }
136 }
137
138 // Handle the events, adjust state and send updates to client accordingly.
139 while (!mQueue.empty()) {
140 Event event = *mQueue.begin();
141 mQueue.pop_front();
142
Chong Zhangbc062482020-10-14 16:43:53 -0700143 ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId,
144 event.sessionId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700145
146 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
147 running = true;
148 lastRunningTime = std::chrono::system_clock::now();
149 lastRunningEvent = event;
150 if (event.type == Event::Start) {
Chong Zhangbc062482020-10-14 16:43:53 -0700151 remainingUs = std::chrono::milliseconds(mSessionProcessingTimeMs);
Chong Zhang75222182020-04-29 14:43:42 -0700152 }
Chong Zhang00feca22020-05-08 15:02:06 -0700153 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
Chong Zhang75222182020-04-29 14:43:42 -0700154 running = false;
155 remainingUs -= (std::chrono::system_clock::now() - lastRunningTime);
156 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700157 ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__,
158 (long long)event.clientId, event.sessionId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700159 continue;
160 }
161
Chong Zhangde60f062020-06-11 17:05:10 -0700162 if (event.runnable != nullptr) {
Chong Zhang75222182020-04-29 14:43:42 -0700163 lock.unlock();
Chong Zhangde60f062020-06-11 17:05:10 -0700164 event.runnable();
Chong Zhang75222182020-04-29 14:43:42 -0700165 lock.lock();
166 }
167 }
168 }
169}
170
171} // namespace android