blob: 010f0f0132cd01eccc32dacdd0cdef2bbbc23404 [file] [log] [blame]
Chong Zhang75222182020-04-29 14:43:42 -07001/*
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 Zhang456787f2021-02-18 16:47:08 -080024#include <map>
Chong Zhang75222182020-04-29 14:43:42 -070025#include <mutex>
26
27namespace 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.
hkuanga9ffd592020-06-05 10:38:02 -070033 * SimulatedTranscoder is used when useSimulatedTranscoder in TranscodingTestConfig
34 * is set to true.
Chong Zhang75222182020-04-29 14:43:42 -070035 *
Chong Zhangbc062482020-10-14 16:43:53 -070036 * SimulatedTranscoder simulates session execution by reporting finish after kSessionDurationUs.
37 * Session lifecycle events are reported via progress updates with special progress
Chong Zhang75222182020-04-29 14:43:42 -070038 * numbers (equal to the Event's type).
39 */
Chong Zhang457c6892021-02-01 15:34:20 -080040class SimulatedTranscoder : public TranscoderInterface,
41 public std::enable_shared_from_this<SimulatedTranscoder> {
Chong Zhang75222182020-04-29 14:43:42 -070042public:
43 struct Event {
Chong Zhang457c6892021-02-01 15:34:20 -080044 enum Type { NoEvent, Start, Pause, Resume, Stop, Finished, Failed, Abandon } type;
Chong Zhang3fa408f2020-04-30 11:04:28 -070045 ClientIdType clientId;
Chong Zhangbc062482020-10-14 16:43:53 -070046 SessionIdType sessionId;
Chong Zhangde60f062020-06-11 17:05:10 -070047 std::function<void()> runnable;
Chong Zhang75222182020-04-29 14:43:42 -070048 };
49
Chong Zhangbc062482020-10-14 16:43:53 -070050 static constexpr int64_t kSessionDurationUs = 1000000;
Chong Zhang75222182020-04-29 14:43:42 -070051
Chong Zhang457c6892021-02-01 15:34:20 -080052 SimulatedTranscoder(const std::shared_ptr<TranscoderCallbackInterface>& cb,
53 int64_t heartBeatUs);
54 ~SimulatedTranscoder();
Chong Zhang75222182020-04-29 14:43:42 -070055
56 // TranscoderInterface
Chong Zhangbc062482020-10-14 16:43:53 -070057 void start(ClientIdType clientId, SessionIdType sessionId,
58 const TranscodingRequestParcel& request,
Chong Zhang66469272020-06-04 16:51:55 -070059 const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override;
Chong Zhangbc062482020-10-14 16:43:53 -070060 void pause(ClientIdType clientId, SessionIdType sessionId) override;
61 void resume(ClientIdType clientId, SessionIdType sessionId,
62 const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -070063 const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override;
Chong Zhang457c6892021-02-01 15:34:20 -080064 void stop(ClientIdType clientId, SessionIdType sessionId, bool abandon = false) override;
Chong Zhang75222182020-04-29 14:43:42 -070065 // ~TranscoderInterface
66
67private:
68 std::weak_ptr<TranscoderCallbackInterface> mCallback;
69 std::mutex mLock;
70 std::condition_variable mCondition;
71 std::list<Event> mQueue GUARDED_BY(mLock);
Chong Zhang457c6892021-02-01 15:34:20 -080072 bool mLooperReady;
Chong Zhang75222182020-04-29 14:43:42 -070073
Chong Zhang456787f2021-02-18 16:47:08 -080074 using SessionKeyType = std::pair<ClientIdType, SessionIdType>;
75 // map of session's remaining time in microsec.
76 std::map<SessionKeyType, std::chrono::microseconds> mRemainingTimeMap;
hkuanga9ffd592020-06-05 10:38:02 -070077
Chong Zhang75222182020-04-29 14:43:42 -070078 static const char* toString(Event::Type type);
Chong Zhangbc062482020-10-14 16:43:53 -070079 void queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangde60f062020-06-11 17:05:10 -070080 std::function<void()> runnable);
Chong Zhang75222182020-04-29 14:43:42 -070081 void threadLoop();
82};
83
84} // namespace android
85
86#endif // ANDROID_MEDIA_SIMULATED_TRANSCODER_H