blob: ba2bba02301ae595efff7e0ee941d0b07e5cd89e [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>
24#include <mutex>
25
26namespace 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.
hkuanga9ffd592020-06-05 10:38:02 -070032 * SimulatedTranscoder is used when useSimulatedTranscoder in TranscodingTestConfig
33 * is set to true.
Chong Zhang75222182020-04-29 14:43:42 -070034 *
Chong Zhangbc062482020-10-14 16:43:53 -070035 * SimulatedTranscoder simulates session execution by reporting finish after kSessionDurationUs.
36 * Session lifecycle events are reported via progress updates with special progress
Chong Zhang75222182020-04-29 14:43:42 -070037 * numbers (equal to the Event's type).
38 */
39class SimulatedTranscoder : public TranscoderInterface {
40public:
41 struct Event {
Chong Zhang00feca22020-05-08 15:02:06 -070042 enum Type { NoEvent, Start, Pause, Resume, Stop, Finished, Failed } type;
Chong Zhang3fa408f2020-04-30 11:04:28 -070043 ClientIdType clientId;
Chong Zhangbc062482020-10-14 16:43:53 -070044 SessionIdType sessionId;
Chong Zhangde60f062020-06-11 17:05:10 -070045 std::function<void()> runnable;
Chong Zhang75222182020-04-29 14:43:42 -070046 };
47
Chong Zhangbc062482020-10-14 16:43:53 -070048 static constexpr int64_t kSessionDurationUs = 1000000;
Chong Zhang75222182020-04-29 14:43:42 -070049
50 SimulatedTranscoder();
51
52 // TranscoderInterface
53 void setCallback(const std::shared_ptr<TranscoderCallbackInterface>& cb) override;
Chong Zhangbc062482020-10-14 16:43:53 -070054 void start(ClientIdType clientId, SessionIdType sessionId,
55 const TranscodingRequestParcel& request,
Chong Zhang66469272020-06-04 16:51:55 -070056 const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override;
Chong Zhangbc062482020-10-14 16:43:53 -070057 void pause(ClientIdType clientId, SessionIdType sessionId) override;
58 void resume(ClientIdType clientId, SessionIdType sessionId,
59 const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -070060 const std::shared_ptr<ITranscodingClientCallback>& clientCallback) override;
Chong Zhangbc062482020-10-14 16:43:53 -070061 void stop(ClientIdType clientId, SessionIdType sessionId) override;
Chong Zhang75222182020-04-29 14:43:42 -070062 // ~TranscoderInterface
63
64private:
65 std::weak_ptr<TranscoderCallbackInterface> mCallback;
66 std::mutex mLock;
67 std::condition_variable mCondition;
68 std::list<Event> mQueue GUARDED_BY(mLock);
69
hkuanga9ffd592020-06-05 10:38:02 -070070 // Minimum time spent on transcode the video. This is used just for testing.
Chong Zhangbc062482020-10-14 16:43:53 -070071 int64_t mSessionProcessingTimeMs = kSessionDurationUs / 1000;
hkuanga9ffd592020-06-05 10:38:02 -070072
Chong Zhang75222182020-04-29 14:43:42 -070073 static const char* toString(Event::Type type);
Chong Zhangbc062482020-10-14 16:43:53 -070074 void queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangde60f062020-06-11 17:05:10 -070075 std::function<void()> runnable);
Chong Zhang75222182020-04-29 14:43:42 -070076 void threadLoop();
77};
78
79} // namespace android
80
81#endif // ANDROID_MEDIA_SIMULATED_TRANSCODER_H