• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "loader_callback_mock.h"
23 #include "unittest_log.h"
24 #include "window.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace PlayerTestParam {
29 inline constexpr int32_t SEEK_TIME_9_SEC = 9000;
30 inline constexpr int32_t SEEK_TIME_5_SEC = 5000;
31 inline constexpr int32_t SEEK_TIME_4_SEC = 4000;
32 inline constexpr int32_t SEEK_TIME_2_SEC = 2000;
33 inline constexpr int32_t waitsecond = 10;
34 inline constexpr int32_t DELTA_TIME = 1000;
35 inline constexpr int32_t PLAYING_TIME_2_SEC = 2;
36 inline constexpr int32_t PLAYING_TIME_1_SEC = 1;
37 inline constexpr int32_t PLAYING_TIME_10_SEC = 10;
38 inline constexpr int32_t SEEK_CONTINUOUS_WAIT_US = 16666;
39 const std::string MEDIA_ROOT = "file:///data/test/";
40 const std::string VIDEO_FILE1 = MEDIA_ROOT + "H264_AAC.mp4";
41 const std::string VIDEO_FILE2 = MEDIA_ROOT + "H264_AAC_multi_track.mp4";
42 const std::string VIDEO_URL = "https://appimg.dbankcdn.com/appVideo/f59583660abd45bcb4fb9c3e3f1125a9.mp4";
43 const std::string VIDEO_FILE3 = MEDIA_ROOT + "H264_AAC.mkv";
44 const std::string SUBTITLE_SRT_FIELE = MEDIA_ROOT + "utf8.srt";
45 const std::string SUBTITLE_SRT_FIELE1 = MEDIA_ROOT + "utf8_test1.srt";
46 const std::string SUBTITLE_0_SEC = "MediaOS: test for subtitle_1";
47 const std::string SUBTITLE_1_SEC = "MediaOS: test for subtitle_2";
48 const std::string SUBTITLE_2_SEC = "MediaOS: test for subtitle_3";
49 const std::string SUBTITLE_3_SEC = "MediaOS: test for subtitle_4";
50 const std::string SUBTITLE_4_SEC = "MediaOS: test for subtitle_5";
51 const std::string SUBTITLE_5_SEC = "MediaOS: test for subtitle_6";
52 const std::string SUBTITLE_6_SEC = "MediaOS: test for subtitle_7";
53 const std::string SUBTITLE_8_SEC = "MediaOS: test for subtitle_9";
54 const std::string SUBTITLE_10_SEC = "MediaOS: test for subtitle_10";
55 const std::string SUBTITLE_TEST1_8_SEC = "MediaOS: test1 for subtitle_9";
56 const std::string HTTPS_PLAY = "HTTPS";
57 const std::string HTTP_PLAY = "HTTP";
58 const std::string LOCAL_PLAY = "LOCAL";
59 const std::string HLS_PLAY = "HLS";
60 const std::string PRE_DOWNLOAD = "pre_download";
61 const std::string INVALID_FILE = MEDIA_ROOT + "invalid.mp4";
62 } // namespace PlayerTestParam
63 
64 class PlayerSignal {
65 protected:
66     PlayerStates state_ = PLAYER_IDLE;
67     int32_t seekPosition_;
68     bool seekDoneFlag_;
69     bool speedDoneFlag_;
70     bool trackDoneFlag_ = false;
71     bool trackChange_ = false;
72     bool trackInfoUpdate_ = false;
73     bool textUpdate_ = false;
74     std::string text_ = "";
75     PlayerSeekMode seekMode_ = PlayerSeekMode::SEEK_CLOSEST;
76     std::mutex mutexCond_;
77     std::mutex subtitleMutex_;
78     std::condition_variable condVarText_;
79     std::condition_variable condVarPrepare_;
80     std::condition_variable condVarPlay_;
81     std::condition_variable condVarPause_;
82     std::condition_variable condVarStop_;
83     std::condition_variable condVarReset_;
84     std::condition_variable condVarSeek_;
85     std::condition_variable condVarSpeed_;
86     std::condition_variable condVarTrackDone_;
87     std::condition_variable condVarTrackInfoUpdate_;
88 };
89 
90 class PlayerCallbackTest : public PlayerCallback, public NoCopyable, public PlayerSignal {
91 public:
~PlayerCallbackTest()92     ~PlayerCallbackTest() {}
93     void OnError(int32_t errorCode, const std::string &errorMsg) override;
94     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
95     void SeekNotify(int32_t extra, const Format &infoBody);
96     void Notify(PlayerStates currentState);
97     void SetSeekDoneFlag(bool seekDoneFlag);
98     void SetSpeedDoneFlag(bool speedDoneFlag);
99     void SetSeekPosition(int32_t seekPosition);
100     void SetState(PlayerStates state);
101     void SetTrackDoneFlag(bool trackDoneFlag);
102     int32_t PrepareSync();
103     int32_t PlaySync();
104     int32_t PauseSync();
105     int32_t StopSync();
106     int32_t ResetSync();
107     int32_t SeekSync();
108     int32_t SpeedSync();
109     int32_t TrackSync(bool &trackChange);
110     int32_t TrackInfoUpdateSync();
111     std::string SubtitleTextUpdate(std::string text);
112     PlayerStates GetState();
113 private:
114     void HandleTrackChangeCallback(int32_t extra, const Format &infoBody);
115     void HandleSubtitleCallback(int32_t extra, const Format &infoBody);
116     void HandleTrackInfoCallback(int32_t extra, const Format &infoBody);
117 };
118 
119 class PlayerMock : public NoCopyable {
120 public:
121     explicit PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback);
122     virtual ~PlayerMock();
123     bool CreatePlayer();
124     int32_t SetSource(const std::string url);
125     int32_t SetDataSrc(const std::string &path, int32_t size, bool seekable);
126     int32_t SetSource(const std::string &path, int64_t offset, int64_t size);
127     int32_t SetSource(int32_t fd, int64_t offset, int64_t size);
128     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy);
129     int32_t Prepare();
130     int32_t PrepareAsync();
131     int32_t Play();
132     int32_t Pause();
133     int32_t Stop();
134     int32_t Reset();
135     int32_t Release();
136     int32_t ReleaseSync();
137     int32_t Seek(int32_t mseconds, PlayerSeekMode mode);
138     int32_t SetVolume(float leftVolume, float rightVolume);
139     int32_t SetLooping(bool loop);
140     int32_t GetCurrentTime(int32_t &currentTime);
141     int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack);
142     int32_t GetPlaybackInfo(Format &playbackInfo);
143     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack);
144     int32_t GetVideoWidth();
145     int32_t GetVideoHeight();
146     int32_t GetDuration(int32_t &duration);
147     int32_t SetPlaybackSpeed(PlaybackRateMode mode);
148     int32_t GetPlaybackSpeed(PlaybackRateMode &mode);
149     int32_t SelectBitRate(uint32_t bitRate);
150     bool IsPlaying();
151     bool IsLooping();
152     int32_t SetParameter(const Format &param);
153     int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback);
154     int32_t SetVideoSurface(sptr<Surface> surface);
155     sptr<Surface> GetVideoSurface();
156     int32_t SelectTrack(int32_t index, bool &trackChange);
157     int32_t DeselectTrack(int32_t index, bool &trackChange);
158     int32_t GetCurrentTrack(int32_t trackType, int32_t &index);
159     int32_t AddSubSource(const std::string &url);
160     int32_t AddSubSource(const std::string &path, int64_t offset, int64_t size);
161     std::string GetSubtitleText(std::string text);
162     sptr<Surface> GetVideoSurfaceNext();
163     PlayerStates GetState();
164     int32_t SetPlayRange(int64_t start, int64_t end);
165     int32_t SeekContinuous(int32_t mseconds);
166     int32_t SetMaxAmplitudeCbStatus(bool status);
167     int32_t SetPlaybackStrategy(AVPlayStrategy strategy);
168     int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted);
169     int32_t SetDeviceChangeCbStatus(bool status);
170     int32_t SetVolumeMode(int32_t mode);
171     int32_t SetSuperResolution(bool enabled);
172     int32_t SetVideoWindowSize(int32_t width, int32_t height);
173 private:
174     void SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode);
175     std::shared_ptr<Player> player_ = nullptr;
176     std::shared_ptr<MediaDataSourceTest> dataSrc_ = nullptr;
177     std::shared_ptr<PlayerCallbackTest> callback_ = nullptr;
178     sptr<Rosen::Window> previewWindow_ = nullptr;
179     int32_t height_ = 1080;
180     int32_t width_ = 1920;
181     std::mutex mutex_;
182     sptr<Rosen::Window> previewWindowNext_ = nullptr;
183     int32_t nextSurfaceHeight_ = 100;
184     int32_t nextSurfaceWidth_ = 100;
185 };
186 } // namespace Media
187 } // namespace OHOS
188 #endif