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