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 | #ifndef ANDROID_MEDIA_SIMULATED_TRANSCODER_H |
| 18 | #define ANDROID_MEDIA_SIMULATED_TRANSCODER_H |
| 19 | |
| 20 | #include <android-base/thread_annotations.h> |
| 21 | #include <media/TranscoderInterface.h> |
| 22 | |
| 23 | #include <list> |
Chong Zhang | 456787f | 2021-02-18 16:47:08 -0800 | [diff] [blame^] | 24 | #include <map> |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 25 | #include <mutex> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /** |
| 30 | * SimulatedTranscoder is currently used to instantiate MediaTranscodingService |
| 31 | * on service side for testing, so that we could actually test the IPC calls of |
| 32 | * MediaTranscodingService to expose issues that's observable only over IPC. |
hkuang | a9ffd59 | 2020-06-05 10:38:02 -0700 | [diff] [blame] | 33 | * SimulatedTranscoder is used when useSimulatedTranscoder in TranscodingTestConfig |
| 34 | * is set to true. |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 35 | * |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 36 | * SimulatedTranscoder simulates session execution by reporting finish after kSessionDurationUs. |
| 37 | * Session lifecycle events are reported via progress updates with special progress |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 38 | * numbers (equal to the Event's type). |
| 39 | */ |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 40 | class SimulatedTranscoder : public TranscoderInterface, |
| 41 | public std::enable_shared_from_this<SimulatedTranscoder> { |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 42 | public: |
| 43 | struct Event { |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 44 | enum Type { NoEvent, Start, Pause, Resume, Stop, Finished, Failed, Abandon } type; |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 45 | ClientIdType clientId; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 46 | SessionIdType sessionId; |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 47 | std::function<void()> runnable; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 50 | static constexpr int64_t kSessionDurationUs = 1000000; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 51 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 52 | SimulatedTranscoder(const std::shared_ptr<TranscoderCallbackInterface>& cb, |
| 53 | int64_t heartBeatUs); |
| 54 | ~SimulatedTranscoder(); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 55 | |
| 56 | // TranscoderInterface |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 57 | void start(ClientIdType clientId, SessionIdType sessionId, |
| 58 | const TranscodingRequestParcel& request, |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 59 | const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 60 | void pause(ClientIdType clientId, SessionIdType sessionId) override; |
| 61 | void resume(ClientIdType clientId, SessionIdType sessionId, |
| 62 | const TranscodingRequestParcel& request, |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 63 | const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 64 | void stop(ClientIdType clientId, SessionIdType sessionId, bool abandon = false) override; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 65 | // ~TranscoderInterface |
| 66 | |
| 67 | private: |
| 68 | std::weak_ptr<TranscoderCallbackInterface> mCallback; |
| 69 | std::mutex mLock; |
| 70 | std::condition_variable mCondition; |
| 71 | std::list<Event> mQueue GUARDED_BY(mLock); |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 72 | bool mLooperReady; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 73 | |
Chong Zhang | 456787f | 2021-02-18 16:47:08 -0800 | [diff] [blame^] | 74 | using SessionKeyType = std::pair<ClientIdType, SessionIdType>; |
| 75 | // map of session's remaining time in microsec. |
| 76 | std::map<SessionKeyType, std::chrono::microseconds> mRemainingTimeMap; |
hkuang | a9ffd59 | 2020-06-05 10:38:02 -0700 | [diff] [blame] | 77 | |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 78 | static const char* toString(Event::Type type); |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 79 | void queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId, |
Chong Zhang | de60f06 | 2020-06-11 17:05:10 -0700 | [diff] [blame] | 80 | std::function<void()> runnable); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 81 | void threadLoop(); |
| 82 | }; |
| 83 | |
| 84 | } // namespace android |
| 85 | |
| 86 | #endif // ANDROID_MEDIA_SIMULATED_TRANSCODER_H |