1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PLAYER_MOCK_H
17 #define PLAYER_MOCK_H
18 
19 #include "player.h"
20 #include "media_data_source_test_noseek.h"
21 #include "media_data_source_test_seekable.h"
22 #include "unittest_log.h"
23 #include "window.h"
24 
25 namespace OHOS {
26 namespace Media {
27 namespace PlayerTestParam {
28 inline constexpr int32_t SEEK_TIME_9_SEC = 9000;
29 inline constexpr int32_t SEEK_TIME_5_SEC = 5000;
30 inline constexpr int32_t SEEK_TIME_4_SEC = 4000;
31 inline constexpr int32_t SEEK_TIME_2_SEC = 2000;
32 inline constexpr int32_t waitsecond = 10;
33 inline constexpr int32_t DELTA_TIME = 1000;
34 inline constexpr int32_t PLAYING_TIME_2_SEC = 2;
35 inline constexpr int32_t PLAYING_TIME_1_SEC = 1;
36 inline constexpr int32_t PLAYING_TIME_10_SEC = 10;
37 inline constexpr int32_t SEEK_CONTINUOUS_WAIT_US = 16666;
38 const std::string MEDIA_ROOT = "file:///data/test/";
39 const std::string VIDEO_FILE1 = MEDIA_ROOT + "H264_AAC.mp4";
40 const std::string VIDEO_FILE2 = MEDIA_ROOT + "H264_AAC_multi_track.mp4";
41 const std::string SUBTITLE_SRT_FIELE = MEDIA_ROOT + "utf8.srt";
42 const std::string SUBTITLE_SRT_FIELE1 = MEDIA_ROOT + "utf8_test1.srt";
43 const std::string SUBTITLE_0_SEC = "MediaOS: test for subtitle_1";
44 const std::string SUBTITLE_1_SEC = "MediaOS: test for subtitle_2";
45 const std::string SUBTITLE_2_SEC = "MediaOS: test for subtitle_3";
46 const std::string SUBTITLE_3_SEC = "MediaOS: test for subtitle_4";
47 const std::string SUBTITLE_4_SEC = "MediaOS: test for subtitle_5";
48 const std::string SUBTITLE_5_SEC = "MediaOS: test for subtitle_6";
49 const std::string SUBTITLE_6_SEC = "MediaOS: test for subtitle_7";
50 const std::string SUBTITLE_8_SEC = "MediaOS: test for subtitle_9";
51 const std::string SUBTITLE_10_SEC = "MediaOS: test for subtitle_10";
52 const std::string SUBTITLE_TEST1_8_SEC = "MediaOS: test1 for subtitle_9";
53 const std::string HTTPS_PLAY = "HTTPS";
54 const std::string HTTP_PLAY = "HTTP";
55 const std::string LOCAL_PLAY = "LOCAL";
56 const std::string HLS_PLAY = "HLS";
57 const std::string INVALID_FILE = MEDIA_ROOT + "invalid.mp4";
58 } // namespace PlayerTestParam
59 
60 class PlayerSignal {
61 protected:
62     PlayerStates state_ = PLAYER_IDLE;
63     int32_t seekPosition_;
64     bool seekDoneFlag_;
65     bool speedDoneFlag_;
66     bool trackDoneFlag_ = false;
67     bool trackChange_ = false;
68     bool trackInfoUpdate_ = false;
69     bool textUpdate_ = false;
70     std::string text_ = "";
71     PlayerSeekMode seekMode_ = PlayerSeekMode::SEEK_CLOSEST;
72     std::mutex mutexCond_;
73     std::mutex subtitleMutex_;
74     std::condition_variable condVarText_;
75     std::condition_variable condVarPrepare_;
76     std::condition_variable condVarPlay_;
77     std::condition_variable condVarPause_;
78     std::condition_variable condVarStop_;
79     std::condition_variable condVarReset_;
80     std::condition_variable condVarSeek_;
81     std::condition_variable condVarSpeed_;
82     std::condition_variable condVarTrackDone_;
83     std::condition_variable condVarTrackInfoUpdate_;
84 };
85 
86 class PlayerCallbackTest : public PlayerCallback, public NoCopyable, public PlayerSignal {
87 public:
~PlayerCallbackTest()88     ~PlayerCallbackTest() {}
89     void OnError(int32_t errorCode, const std::string &errorMsg) override;
90     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
91     void SeekNotify(int32_t extra, const Format &infoBody);
92     void Notify(PlayerStates currentState);
93     void SetSeekDoneFlag(bool seekDoneFlag);
94     void SetSpeedDoneFlag(bool speedDoneFlag);
95     void SetSeekPosition(int32_t seekPosition);
96     void SetState(PlayerStates state);
97     void SetTrackDoneFlag(bool trackDoneFlag);
98     int32_t PrepareSync();
99     int32_t PlaySync();
100     int32_t PauseSync();
101     int32_t StopSync();
102     int32_t ResetSync();
103     int32_t SeekSync();
104     int32_t SpeedSync();
105     int32_t TrackSync(bool &trackChange);
106     int32_t TrackInfoUpdateSync();
107     std::string SubtitleTextUpdate(std::string text);
108 private:
109     void HandleTrackChangeCallback(int32_t extra, const Format &infoBody);
110     void HandleSubtitleCallback(int32_t extra, const Format &infoBody);
111     void HandleTrackInfoCallback(int32_t extra, const Format &infoBody);
112 };
113 
114 class PlayerMock : public NoCopyable {
115 public:
116     explicit PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback);
117     virtual ~PlayerMock();
118     bool CreatePlayer();
119     int32_t SetSource(const std::string url);
120     int32_t SetDataSrc(const std::string &path, int32_t size, bool seekable);
121     int32_t SetSource(const std::string &path, int64_t offset, int64_t size);
122     int32_t SetSource(int32_t fd, int64_t offset, int64_t size);
123     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy);
124     int32_t Prepare();
125     int32_t PrepareAsync();
126     int32_t Play();
127     int32_t Pause();
128     int32_t Stop();
129     int32_t Reset();
130     int32_t Release();
131     int32_t ReleaseSync();
132     int32_t Seek(int32_t mseconds, PlayerSeekMode mode);
133     int32_t SetVolume(float leftVolume, float rightVolume);
134     int32_t SetLooping(bool loop);
135     int32_t GetCurrentTime(int32_t &currentTime);
136     int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack);
137     int32_t GetPlaybackInfo(Format &playbackInfo);
138     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack);
139     int32_t GetVideoWidth();
140     int32_t GetVideoHeight();
141     int32_t GetDuration(int32_t &duration);
142     int32_t SetPlaybackSpeed(PlaybackRateMode mode);
143     int32_t GetPlaybackSpeed(PlaybackRateMode &mode);
144     int32_t SelectBitRate(uint32_t bitRate);
145     bool IsPlaying();
146     bool IsLooping();
147     int32_t SetParameter(const Format &param);
148     int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback);
149     int32_t SetVideoSurface(sptr<Surface> surface);
150     sptr<Surface> GetVideoSurface();
151     int32_t SelectTrack(int32_t index, bool &trackChange);
152     int32_t DeselectTrack(int32_t index, bool &trackChange);
153     int32_t GetCurrentTrack(int32_t trackType, int32_t &index);
154     int32_t AddSubSource(const std::string &url);
155     int32_t AddSubSource(const std::string &path, int64_t offset, int64_t size);
156     std::string GetSubtitleText(std::string text);
157     sptr<Surface> GetVideoSurfaceNext();
158     int32_t SetPlayRange(int64_t start, int64_t end);
159     int32_t SeekContinuous(int32_t mseconds);
160     int32_t SetPlaybackStrategy(AVPlayStrategy strategy);
161     int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted);
162     int32_t SetDeviceChangeCbStatus(bool status);
163     int32_t SetMaxAmplitudeCbStatus(bool status);
164 private:
165     void SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode);
166     std::shared_ptr<Player> player_ = nullptr;
167     std::shared_ptr<MediaDataSourceTest> dataSrc_ = nullptr;
168     std::shared_ptr<PlayerCallbackTest> callback_ = nullptr;
169     sptr<Rosen::Window> previewWindow_ = nullptr;
170     int32_t height_ = 1080;
171     int32_t width_ = 1920;
172     std::mutex mutex_;
173     sptr<Rosen::Window> previewWindowNext_ = nullptr;
174     int32_t nextSurfaceHeight_ = 100;
175     int32_t nextSurfaceWidth_ = 100;
176 };
177 } // namespace Media
178 } // namespace OHOS
179 #endif