blob: 97d5f5f6c0da155c9779de485cb356ecc14f9cdf [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(
51 ClientIdType clientId, JobIdType jobId, const TranscodingRequestParcel& request,
52 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
hkuang34915b12020-06-18 09:36:39 -070053 if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) {
54 mJobProcessingTimeMs = request.testConfig->processingTotalTimeMs;
hkuanga9ffd592020-06-05 10:38:02 -070055 }
56 ALOGV("%s: job {%d}: processingTime: %lld", __FUNCTION__, jobId,
57 (long long)mJobProcessingTimeMs);
Chong Zhangde60f062020-06-11 17:05:10 -070058 queueEvent(Event::Start, clientId, jobId, [=] {
59 auto callback = mCallback.lock();
60 if (callback != nullptr) {
61 callback->onStarted(clientId, jobId);
62 }
63 });
Chong Zhang75222182020-04-29 14:43:42 -070064}
65
Chong Zhang3fa408f2020-04-30 11:04:28 -070066void SimulatedTranscoder::pause(ClientIdType clientId, JobIdType jobId) {
Chong Zhangde60f062020-06-11 17:05:10 -070067 queueEvent(Event::Pause, clientId, jobId, [=] {
68 auto callback = mCallback.lock();
69 if (callback != nullptr) {
70 callback->onPaused(clientId, jobId);
71 }
72 });
Chong Zhang75222182020-04-29 14:43:42 -070073}
74
Chong Zhangb55c5452020-06-26 14:32:12 -070075void SimulatedTranscoder::resume(
76 ClientIdType clientId, JobIdType jobId, const TranscodingRequestParcel& /*request*/,
77 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) {
Chong Zhangde60f062020-06-11 17:05:10 -070078 queueEvent(Event::Resume, clientId, jobId, [=] {
79 auto callback = mCallback.lock();
80 if (callback != nullptr) {
81 callback->onResumed(clientId, jobId);
82 }
83 });
Chong Zhang75222182020-04-29 14:43:42 -070084}
85
Chong Zhang00feca22020-05-08 15:02:06 -070086void SimulatedTranscoder::stop(ClientIdType clientId, JobIdType jobId) {
Chong Zhangde60f062020-06-11 17:05:10 -070087 queueEvent(Event::Stop, clientId, jobId, nullptr);
Chong Zhang00feca22020-05-08 15:02:06 -070088}
89
Chong Zhangde60f062020-06-11 17:05:10 -070090void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId, JobIdType jobId,
91 std::function<void()> runnable) {
Chong Zhang75222182020-04-29 14:43:42 -070092 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)clientId, jobId, toString(type));
93
94 auto lock = std::scoped_lock(mLock);
95
Chong Zhangde60f062020-06-11 17:05:10 -070096 mQueue.push_back({type, clientId, jobId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -070097 mCondition.notify_one();
98}
99
100void SimulatedTranscoder::threadLoop() {
101 bool running = false;
102 std::chrono::microseconds remainingUs(kJobDurationUs);
103 std::chrono::system_clock::time_point lastRunningTime;
104 Event lastRunningEvent;
105
106 std::unique_lock<std::mutex> lock(mLock);
107 // SimulatedTranscoder currently lives in the transcoding service, as long as
108 // MediaTranscodingService itself.
109 while (true) {
110 // Wait for the next event.
111 while (mQueue.empty()) {
112 if (!running) {
113 mCondition.wait(lock);
114 continue;
115 }
116 // If running, wait for the remaining life of this job. Report finish if timed out.
117 std::cv_status status = mCondition.wait_for(lock, remainingUs);
118 if (status == std::cv_status::timeout) {
119 running = false;
120
121 auto callback = mCallback.lock();
122 if (callback != nullptr) {
123 lock.unlock();
124 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.jobId);
125 lock.lock();
126 }
127 } else {
128 // Advance last running time and remaining time. This is needed to guard
129 // against bad events (which will be ignored) or spurious wakeups, in that
130 // case we don't want to wait for the same time again.
131 auto now = std::chrono::system_clock::now();
132 remainingUs -= (now - lastRunningTime);
133 lastRunningTime = now;
134 }
135 }
136
137 // Handle the events, adjust state and send updates to client accordingly.
138 while (!mQueue.empty()) {
139 Event event = *mQueue.begin();
140 mQueue.pop_front();
141
142 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId, event.jobId,
143 toString(event.type));
144
145 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
146 running = true;
147 lastRunningTime = std::chrono::system_clock::now();
148 lastRunningEvent = event;
149 if (event.type == Event::Start) {
hkuanga9ffd592020-06-05 10:38:02 -0700150 remainingUs = std::chrono::milliseconds(mJobProcessingTimeMs);
Chong Zhang75222182020-04-29 14:43:42 -0700151 }
Chong Zhang00feca22020-05-08 15:02:06 -0700152 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
Chong Zhang75222182020-04-29 14:43:42 -0700153 running = false;
154 remainingUs -= (std::chrono::system_clock::now() - lastRunningTime);
155 } else {
156 ALOGW("%s: discarding bad event: job {%lld, %d}: %s", __FUNCTION__,
Chong Zhang00feca22020-05-08 15:02:06 -0700157 (long long)event.clientId, event.jobId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700158 continue;
159 }
160
Chong Zhangde60f062020-06-11 17:05:10 -0700161 if (event.runnable != nullptr) {
Chong Zhang75222182020-04-29 14:43:42 -0700162 lock.unlock();
Chong Zhangde60f062020-06-11 17:05:10 -0700163 event.runnable();
Chong Zhang75222182020-04-29 14:43:42 -0700164 lock.lock();
165 }
166 }
167 }
168}
169
170} // namespace android