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"; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 36 | case Event::Stop: |
| 37 | return "Stop"; |
| 38 | case Event::Finished: |
| 39 | return "Finished"; |
| 40 | case Event::Failed: |
| 41 | return "Failed"; |
| 42 | case Event::Abandon: |
| 43 | return "Abandon"; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 44 | default: |
| 45 | break; |
| 46 | } |
| 47 | return "(unknown)"; |
| 48 | } |
| 49 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 50 | SimulatedTranscoder::SimulatedTranscoder(const std::shared_ptr<TranscoderCallbackInterface>& cb, |
| 51 | int64_t heartBeatUs __unused) |
| 52 | : mCallback(cb), mLooperReady(false) { |
| 53 | ALOGV("SimulatedTranscoder CTOR: %p", this); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 56 | SimulatedTranscoder::~SimulatedTranscoder() { |
| 57 | ALOGV("SimulatedTranscoder DTOR: %p", this); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 60 | void SimulatedTranscoder::start( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 61 | ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request, |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 62 | const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) { |
hkuang | 34915b1 | 2020-06-18 09:36:39 -0700 | [diff] [blame] | 63 | if (request.testConfig.has_value() && request.testConfig->processingTotalTimeMs > 0) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 64 | mSessionProcessingTimeMs = request.testConfig->processingTotalTimeMs; |
hkuang | a9ffd59 | 2020-06-05 10:38:02 -0700 | [diff] [blame] | 65 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 66 | ALOGV("%s: session {%d}: processingTime: %lld", __FUNCTION__, sessionId, |
| 67 | (long long)mSessionProcessingTimeMs); |
| 68 | queueEvent(Event::Start, clientId, sessionId, [=] { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 69 | auto callback = mCallback.lock(); |
| 70 | if (callback != nullptr) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 71 | callback->onStarted(clientId, sessionId); |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 72 | } |
| 73 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 76 | void SimulatedTranscoder::pause(ClientIdType clientId, SessionIdType sessionId) { |
| 77 | queueEvent(Event::Pause, clientId, sessionId, [=] { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 78 | auto callback = mCallback.lock(); |
| 79 | if (callback != nullptr) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 80 | callback->onPaused(clientId, sessionId); |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 81 | } |
| 82 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 85 | void SimulatedTranscoder::resume( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 86 | ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& /*request*/, |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 87 | const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 88 | queueEvent(Event::Resume, clientId, sessionId, [=] { |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 89 | auto callback = mCallback.lock(); |
| 90 | if (callback != nullptr) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 91 | callback->onResumed(clientId, sessionId); |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 92 | } |
| 93 | }); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 96 | void SimulatedTranscoder::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 97 | queueEvent(Event::Stop, clientId, sessionId, nullptr); |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 98 | |
| 99 | if (abandon) { |
| 100 | queueEvent(Event::Abandon, 0, 0, nullptr); |
| 101 | } |
Chong Zhang | 00feca2 | 2020-05-08 15:02:06 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 104 | void SimulatedTranscoder::queueEvent(Event::Type type, ClientIdType clientId, |
| 105 | SessionIdType sessionId, std::function<void()> runnable) { |
| 106 | ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)clientId, sessionId, |
| 107 | toString(type)); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 108 | |
| 109 | auto lock = std::scoped_lock(mLock); |
| 110 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 111 | if (!mLooperReady) { |
| 112 | // A shared_ptr to ourselves is given to the thread's stack, so that SimulatedTranscoder |
| 113 | // object doesn't go away until the thread exits. When a watchdog timeout happens, this |
| 114 | // allows the session controller to release its reference to the TranscoderWrapper object |
| 115 | // without blocking on the thread exits. |
| 116 | std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach(); |
| 117 | mLooperReady = true; |
| 118 | } |
| 119 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 120 | mQueue.push_back({type, clientId, sessionId, runnable}); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 121 | mCondition.notify_one(); |
| 122 | } |
| 123 | |
| 124 | void SimulatedTranscoder::threadLoop() { |
| 125 | bool running = false; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 126 | std::chrono::microseconds remainingUs(kSessionDurationUs); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 127 | std::chrono::system_clock::time_point lastRunningTime; |
| 128 | Event lastRunningEvent; |
| 129 | |
| 130 | std::unique_lock<std::mutex> lock(mLock); |
| 131 | // SimulatedTranscoder currently lives in the transcoding service, as long as |
| 132 | // MediaTranscodingService itself. |
| 133 | while (true) { |
| 134 | // Wait for the next event. |
| 135 | while (mQueue.empty()) { |
| 136 | if (!running) { |
| 137 | mCondition.wait(lock); |
| 138 | continue; |
| 139 | } |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 140 | // If running, wait for the remaining life of this session. Report finish if timed out. |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 141 | std::cv_status status = mCondition.wait_for(lock, remainingUs); |
| 142 | if (status == std::cv_status::timeout) { |
| 143 | running = false; |
| 144 | |
| 145 | auto callback = mCallback.lock(); |
| 146 | if (callback != nullptr) { |
| 147 | lock.unlock(); |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 148 | callback->onFinish(lastRunningEvent.clientId, lastRunningEvent.sessionId); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 149 | lock.lock(); |
| 150 | } |
| 151 | } else { |
| 152 | // Advance last running time and remaining time. This is needed to guard |
| 153 | // against bad events (which will be ignored) or spurious wakeups, in that |
| 154 | // case we don't want to wait for the same time again. |
| 155 | auto now = std::chrono::system_clock::now(); |
| 156 | remainingUs -= (now - lastRunningTime); |
| 157 | lastRunningTime = now; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Handle the events, adjust state and send updates to client accordingly. |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 162 | Event event = *mQueue.begin(); |
| 163 | mQueue.pop_front(); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 164 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 165 | ALOGV("%s: session {%lld, %d}: %s", __FUNCTION__, (long long)event.clientId, |
| 166 | event.sessionId, toString(event.type)); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 167 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 168 | if (event.type == Event::Abandon) { |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | if (!running && (event.type == Event::Start || event.type == Event::Resume)) { |
| 173 | running = true; |
| 174 | lastRunningTime = std::chrono::system_clock::now(); |
| 175 | lastRunningEvent = event; |
| 176 | if (event.type == Event::Start) { |
| 177 | remainingUs = std::chrono::milliseconds(mSessionProcessingTimeMs); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 178 | } |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 179 | } else if (running && (event.type == Event::Pause || event.type == Event::Stop)) { |
| 180 | running = false; |
| 181 | remainingUs -= (std::chrono::system_clock::now() - lastRunningTime); |
| 182 | } else { |
| 183 | ALOGW("%s: discarding bad event: session {%lld, %d}: %s", __FUNCTION__, |
| 184 | (long long)event.clientId, event.sessionId, toString(event.type)); |
| 185 | continue; |
| 186 | } |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 187 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame^] | 188 | if (event.runnable != nullptr) { |
| 189 | lock.unlock(); |
| 190 | event.runnable(); |
| 191 | lock.lock(); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | } // namespace android |