Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | |
| 27 | //static |
| 28 | const 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 | |
| 42 | SimulatedTranscoder::SimulatedTranscoder() { |
| 43 | std::thread(&SimulatedTranscoder::threadLoop, this).detach(); |
| 44 | } |
| 45 | |
| 46 | void SimulatedTranscoder::setCallback(const std::shared_ptr<TranscoderCallbackInterface>& cb) { |
| 47 | mCallback = cb; |
| 48 | } |
| 49 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 50 | void SimulatedTranscoder::start( |
| 51 | ClientIdType clientId, JobIdType jobId, const TranscodingRequestParcel& request, |
| 52 | const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) { |
hkuang | 34915b1 | 2020-06-18 09:36:39 -0700 | [diff] [blame] | 53 | if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) { |
| 54 | mJobProcessingTimeMs = request.testConfig->processingTotalTimeMs; |
hkuang | a9ffd59 | 2020-06-05 10:38:02 -0700 | [diff] [blame] | 55 | } |
| 56 | ALOGV("%s: job {%d}: processingTime: %lld", __FUNCTION__, jobId, |
| 57 | (long long)mJobProcessingTimeMs); |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 58 | queueEvent(Event::Start, clientId, jobId, [=] { |
| 59 | auto callback = mCallback.lock(); |
| 60 | if (callback != nullptr) { |
| 61 | callback->onStarted(clientId, jobId); |
| 62 | } |
| 63 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 66 | void SimulatedTranscoder::pause(ClientIdType clientId, JobIdType jobId) { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 67 | queueEvent(Event::Pause, clientId, jobId, [=] { |
| 68 | auto callback = mCallback.lock(); |
| 69 | if (callback != nullptr) { |
| 70 | callback->onPaused(clientId, jobId); |
| 71 | } |
| 72 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 75 | void SimulatedTranscoder::resume( |
| 76 | ClientIdType clientId, JobIdType jobId, const TranscodingRequestParcel& /*request*/, |
| 77 | const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 78 | queueEvent(Event::Resume, clientId, jobId, [=] { |
| 79 | auto callback = mCallback.lock(); |
| 80 | if (callback != nullptr) { |
| 81 | callback->onResumed(clientId, jobId); |
| 82 | } |
| 83 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Chong Zhang | 00feca2 | 2020-05-08 15:02:06 -0700 | [diff] [blame] | 86 | void SimulatedTranscoder::stop(ClientIdType clientId, JobIdType jobId) { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 87 | queueEvent(Event::Stop, clientId, jobId, nullptr); |
Chong Zhang | 00feca2 | 2020-05-08 15:02:06 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 90 | void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId, JobIdType jobId, |
| 91 | std::function<void()> runnable) { |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 92 | ALOGV("%s: job {%lld, %d}: %s", __FUNCTION__, (long long)clientId, jobId, toString(type)); |
| 93 | |
| 94 | auto lock = std::scoped_lock(mLock); |
| 95 | |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 96 | mQueue.push_back({type, clientId, jobId, runnable}); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 97 | mCondition.notify_one(); |
| 98 | } |
| 99 | |
| 100 | void 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) { |
hkuang | a9ffd59 | 2020-06-05 10:38:02 -0700 | [diff] [blame] | 150 | remainingUs = std::chrono::milliseconds(mJobProcessingTimeMs); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 151 | } |
Chong Zhang | 00feca2 | 2020-05-08 15:02:06 -0700 | [diff] [blame] | 152 | } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) { |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 153 | running = false; |
| 154 | remainingUs -= (std::chrono::system_clock::now() - lastRunningTime); |
| 155 | } else { |
| 156 | ALOGW("%s: discarding bad event: job {%lld, %d}: %s", __FUNCTION__, |
Chong Zhang | 00feca2 | 2020-05-08 15:02:06 -0700 | [diff] [blame] | 157 | (long long)event.clientId, event.jobId, toString(event.type)); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 158 | continue; |
| 159 | } |
| 160 | |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 161 | if (event.runnable != nullptr) { |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 162 | lock.unlock(); |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 163 | event.runnable(); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 164 | lock.lock(); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | } // namespace android |