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