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> |
| 24 | #include <mutex> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | /** |
| 29 | * SimulatedTranscoder is currently used to instantiate MediaTranscodingService |
| 30 | * on service side for testing, so that we could actually test the IPC calls of |
| 31 | * MediaTranscodingService to expose issues that's observable only over IPC. |
| 32 | * |
| 33 | * SimulatedTranscoder simulates job execution by reporting finish after kJobDurationUs. |
| 34 | * Job lifecycle events are reported via progress updates with special progress |
| 35 | * numbers (equal to the Event's type). |
| 36 | */ |
| 37 | class SimulatedTranscoder : public TranscoderInterface { |
| 38 | public: |
| 39 | struct Event { |
| 40 | enum Type { NoEvent, Start, Pause, Resume, Finished, Failed } type; |
| 41 | int64_t clientId; |
| 42 | int32_t jobId; |
| 43 | }; |
| 44 | |
| 45 | static constexpr int64_t kJobDurationUs = 1000000; |
| 46 | |
| 47 | SimulatedTranscoder(); |
| 48 | |
| 49 | // TranscoderInterface |
| 50 | void setCallback(const std::shared_ptr<TranscoderCallbackInterface>& cb) override; |
| 51 | void start(int64_t clientId, int32_t jobId) override; |
| 52 | void pause(int64_t clientId, int32_t jobId) override; |
| 53 | void resume(int64_t clientId, int32_t jobId) override; |
| 54 | // ~TranscoderInterface |
| 55 | |
| 56 | private: |
| 57 | std::weak_ptr<TranscoderCallbackInterface> mCallback; |
| 58 | std::mutex mLock; |
| 59 | std::condition_variable mCondition; |
| 60 | std::list<Event> mQueue GUARDED_BY(mLock); |
| 61 | |
| 62 | static const char* toString(Event::Type type); |
| 63 | void queueEvent(Event::Type type, int64_t clientId, int32_t jobId); |
| 64 | void threadLoop(); |
| 65 | }; |
| 66 | |
| 67 | } // namespace android |
| 68 | |
| 69 | #endif // ANDROID_MEDIA_SIMULATED_TRANSCODER_H |