blob: 1b68d5c245a796c80211821474b7b4d88ba054a8 [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,
51 const TranscodingRequestParcel& /*request*/) {
Chong Zhang75222182020-04-29 14:43:42 -070052 queueEvent(Event::Start, clientId, jobId);
53}
54
Chong Zhang3fa408f2020-04-30 11:04:28 -070055void SimulatedTranscoder::pause(ClientIdType clientId, JobIdType jobId) {
Chong Zhang75222182020-04-29 14:43:42 -070056 queueEvent(Event::Pause, clientId, jobId);
57}
58
Chong Zhang3fa408f2020-04-30 11:04:28 -070059void SimulatedTranscoder::resume(ClientIdType clientId, JobIdType jobId) {
Chong Zhang75222182020-04-29 14:43:42 -070060 queueEvent(Event::Resume, clientId, jobId);
61}
62
Chong Zhang00feca22020-05-08 15:02:06 -070063void SimulatedTranscoder::stop(ClientIdType clientId, JobIdType jobId) {
64 queueEvent(Event::Stop, clientId, jobId);
65}
66
Chong Zhang3fa408f2020-04-30 11:04:28 -070067void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId, JobIdType jobId) {
Chong Zhang75222182020-04-29 14:43:42 -070068 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)clientId, jobId, toString(type));
69
70 auto lock = std::scoped_lock(mLock);
71
72 mQueue.push_back({type, clientId, jobId});
73 mCondition.notify_one();
74}
75
76void SimulatedTranscoder::threadLoop() {
77 bool running = false;
78 std::chrono::microseconds remainingUs(kJobDurationUs);
79 std::chrono::system_clock::time_point lastRunningTime;
80 Event lastRunningEvent;
81
82 std::unique_lock<std::mutex> lock(mLock);
83 // SimulatedTranscoder currently lives in the transcoding service, as long as
84 // MediaTranscodingService itself.
85 while (true) {
86 // Wait for the next event.
87 while (mQueue.empty()) {
88 if (!running) {
89 mCondition.wait(lock);
90 continue;
91 }
92 // If running, wait for the remaining life of this job. Report finish if timed out.
93 std::cv_status status = mCondition.wait_for(lock, remainingUs);
94 if (status == std::cv_status::timeout) {
95 running = false;
96
97 auto callback = mCallback.lock();
98 if (callback != nullptr) {
99 lock.unlock();
100 callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.jobId);
101 lock.lock();
102 }
103 } else {
104 // Advance last running time and remaining time. This is needed to guard
105 // against bad events (which will be ignored) or spurious wakeups, in that
106 // case we don't want to wait for the same time again.
107 auto now = std::chrono::system_clock::now();
108 remainingUs -= (now - lastRunningTime);
109 lastRunningTime = now;
110 }
111 }
112
113 // Handle the events, adjust state and send updates to client accordingly.
114 while (!mQueue.empty()) {
115 Event event = *mQueue.begin();
116 mQueue.pop_front();
117
118 ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId, event.jobId,
119 toString(event.type));
120
121 if (!running && (event.type == Event::Start || event.type == Event::Resume)) {
122 running = true;
123 lastRunningTime = std::chrono::system_clock::now();
124 lastRunningEvent = event;
125 if (event.type == Event::Start) {
126 remainingUs = std::chrono::microseconds(kJobDurationUs);
127 }
Chong Zhang00feca22020-05-08 15:02:06 -0700128 } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) {
Chong Zhang75222182020-04-29 14:43:42 -0700129 running = false;
130 remainingUs -= (std::chrono::system_clock::now() - lastRunningTime);
131 } else {
132 ALOGW("%s: discarding bad event: job {%lld, %d}: %s", __FUNCTION__,
Chong Zhang00feca22020-05-08 15:02:06 -0700133 (long long)event.clientId, event.jobId, toString(event.type));
Chong Zhang75222182020-04-29 14:43:42 -0700134 continue;
135 }
136
137 auto callback = mCallback.lock();
138 if (callback != nullptr) {
139 lock.unlock();
140 callback->onProgressUpdate(event.clientId, event.jobId, event.type);
141 lock.lock();
142 }
143 }
144 }
145}
146
147} // namespace android