blob: 380d757f62d9160a831b7235009c20bc84e931d4 [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 Zhang00feca22020-05-08 15:02:06 -070050void SimulatedTranscoder::start(ClientIdType clientId, JobIdType jobId,
hkuanga9ffd592020-06-05 10:38:02 -070051 const TranscodingRequestParcel& request) {
52 if (request.testConfig.processingTotalTimeMs > 0) {
53 mJobProcessingTimeMs = request.testConfig.processingTotalTimeMs;
54 }
55 ALOGV("%s: job {%d}: processingTime: %lld", __FUNCTION__, jobId,
56 (long long)mJobProcessingTimeMs);
Chong Zhangde60f062020-06-11 17:05:10 -070057 queueEvent(Event::Start, clientId, jobId, [=] {
58 auto callback = mCallback.lock();
59 if (callback != nullptr) {
60 callback->onStarted(clientId, jobId);
61 }
62 });
Chong Zhang75222182020-04-29 14:43:42 -070063}
64
Chong Zhang3fa408f2020-04-30 11:04:28 -070065void SimulatedTranscoder::pause(ClientIdType clientId, JobIdType jobId) {
Chong Zhangde60f062020-06-11 17:05:10 -070066 queueEvent(Event::Pause, clientId, jobId, [=] {
67 auto callback = mCallback.lock();
68 if (callback != nullptr) {
69 callback->onPaused(clientId, jobId);
70 }
71 });
Chong Zhang75222182020-04-29 14:43:42 -070072}
73
Chong Zhang3fa408f2020-04-30 11:04:28 -070074void SimulatedTranscoder::resume(ClientIdType clientId, JobIdType jobId) {
Chong Zhangde60f062020-06-11 17:05:10 -070075 queueEvent(Event::Resume, clientId, jobId, [=] {
76 auto callback = mCallback.lock();
77 if (callback != nullptr) {
78 callback->onResumed(clientId, jobId);
79 }
80 });
Chong Zhang75222182020-04-29 14:43:42 -070081}
82
Chong Zhang00feca22020-05-08 15:02:06 -070083void SimulatedTranscoder::stop(ClientIdType clientId, JobIdType jobId) {
Chong Zhangde60f062020-06-11 17:05:10 -070084 queueEvent(Event::Stop, clientId, jobId, nullptr);
Chong Zhang00feca22020-05-08 15:02:06 -070085}
86
Chong Zhangde60f062020-06-11 17:05:10 -070087void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId, JobIdType jobId,
88 std::function<void()> runnable) {
Chong Zhang75222182020-04-29 14:43:42 -070089 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)clientId, jobId, toString(type));
90
91 auto lock = std::scoped_lock(mLock);
92
Chong Zhangde60f062020-06-11 17:05:10 -070093 mQueue.push_back({type, clientId, jobId, runnable});
Chong Zhang75222182020-04-29 14:43:42 -070094 mCondition.notify_one();
95}
96
97void SimulatedTranscoder::threadLoop() {
98 bool running = false;
99 std::chrono::microseconds remainingUs(kJobDurationUs);
100 std::chrono::system_clock::time_point lastRunningTime;
101 Event lastRunningEvent;
102
103 std::unique_lock<std::mutex> lock(mLock);
104 // SimulatedTranscoder currently lives in the transcoding service, as long as
105 // MediaTranscodingService itself.
106 while (true) {
107 // Wait for the next event.
108 while (mQueue.empty()) {
109 if (!running) {
110 mCondition.wait(lock);
111 continue;
112 }
113 // If running, wait for the remaining life of this job. Report finish if timed out.
114 std::cv_status status = mCondition.wait_for(lock, remainingUs);
115 if (status == std::cv_status::timeout) {
116 running = false;
117
118 auto callback = mCallback.lock();
119 if (callback != nullptr) {
120 lock.unlock();
121 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.jobId);
122 lock.lock();
123 }
124 } else {
125 // Advance last running time and remaining time. This is needed to guard
126 // against bad events (which will be ignored) or spurious wakeups, in that
127 // case we don't want to wait for the same time again.
128 auto now = std::chrono::system_clock::now();
129 remainingUs -= (now - lastRunningTime);
130 lastRunningTime = now;
131 }
132 }
133
134 // Handle the events, adjust state and send updates to client accordingly.
135 while (!mQueue.empty()) {
136 Event event = *mQueue.begin();
137 mQueue.pop_front();
138
139 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId, event.jobId,
140 toString(event.type));
141
142 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
143 running = true;
144 lastRunningTime = std::chrono::system_clock::now();
145 lastRunningEvent = event;
146 if (event.type == Event::Start) {
hkuanga9ffd592020-06-05 10:38:02 -0700147 remainingUs = std::chrono::milliseconds(mJobProcessingTimeMs);
Chong Zhang75222182020-04-29 14:43:42 -0700148 }
Chong Zhang00feca22020-05-08 15:02:06 -0700149 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
Chong Zhang75222182020-04-29 14:43:42 -0700150 running = false;
151 remainingUs -= (std::chrono::system_clock::now() - lastRunningTime);
152 } else {
153 ALOGW("%s: discarding bad event: job {%lld, %d}: %s", __FUNCTION__,
Chong Zhang00feca22020-05-08 15:02:06 -0700154 (long long)event.clientId, event.jobId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700155 continue;
156 }
157
Chong Zhangde60f062020-06-11 17:05:10 -0700158 if (event.runnable != nullptr) {
Chong Zhang75222182020-04-29 14:43:42 -0700159 lock.unlock();
Chong Zhangde60f062020-06-11 17:05:10 -0700160 event.runnable();
Chong Zhang75222182020-04-29 14:43:42 -0700161 lock.lock();
162 }
163 }
164 }
165}
166
167} // namespace android