1 /* 2 * Copyright (c) 2025 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 "wm/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 rateDoneFlag_ = false; 71 bool trackDoneFlag_ = false; 72 bool trackChange_ = false; 73 bool trackInfoUpdate_ = false; 74 bool textUpdate_ = false; 75 std::string text_ = ""; 76 PlayerSeekMode seekMode_ = PlayerSeekMode::SEEK_CLOSEST; 77 std::mutex mutexCond_; 78 std::mutex subtitleMutex_; 79 std::condition_variable condVarText_; 80 std::condition_variable condVarPrepare_; 81 std::condition_variable condVarPlay_; 82 std::condition_variable condVarPause_; 83 std::condition_variable condVarStop_; 84 std::condition_variable condVarReset_; 85 std::condition_variable condVarSeek_; 86 std::condition_variable condVarRate_; 87 std::condition_variable condVarSpeed_; 88 std::condition_variable condVarTrackDone_; 89 std::condition_variable condVarTrackInfoUpdate_; 90 }; 91 92 class PlayerCallbackTest : public PlayerCallback, public NoCopyable, public PlayerSignal { 93 public: ~PlayerCallbackTest()94 ~PlayerCallbackTest() {} 95 void OnError(int32_t errorCode, const std::string &errorMsg) override; 96 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override; 97 void SeekNotify(int32_t extra, const Format &infoBody); 98 void Notify(PlayerStates currentState); 99 void SetSeekDoneFlag(bool seekDoneFlag); 100 void SetSpeedDoneFlag(bool speedDoneFlag); 101 void SetRateDoneFlag(bool rateDoneFlag); 102 void SetSeekPosition(int32_t seekPosition); 103 void SetState(PlayerStates state); 104 void SetTrackDoneFlag(bool trackDoneFlag); 105 int32_t PrepareSync(); 106 int32_t PlaySync(); 107 int32_t PauseSync(); 108 int32_t StopSync(); 109 int32_t ResetSync(); 110 int32_t SeekSync(); 111 int32_t SpeedSync(); 112 int32_t RateSync(); 113 int32_t TrackSync(bool &trackChange); 114 int32_t TrackInfoUpdateSync(); 115 std::string SubtitleTextUpdate(std::string text); 116 PlayerStates GetState(); 117 private: 118 void HandleTrackChangeCallback(int32_t extra, const Format &infoBody); 119 void HandleSubtitleCallback(int32_t extra, const Format &infoBody); 120 void HandleTrackInfoCallback(int32_t extra, const Format &infoBody); 121 }; 122 123 class PlayerMock : public NoCopyable { 124 public: 125 explicit PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback); 126 virtual ~PlayerMock(); 127 bool CreatePlayer(); 128 int32_t SetSource(const std::string url); 129 int32_t SetDataSrc(const std::string &path, int32_t size, bool seekable); 130 int32_t SetSource(const std::string &path, int64_t offset, int64_t size); 131 int32_t SetSource(int32_t fd, int64_t offset, int64_t size); 132 int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy); 133 int32_t Prepare(); 134 int32_t PrepareAsync(); 135 int32_t Play(); 136 int32_t Pause(); 137 int32_t Stop(); 138 int32_t Reset(); 139 int32_t Release(); 140 int32_t ReleaseSync(); 141 int32_t Seek(int32_t mseconds, PlayerSeekMode mode); 142 int32_t SetVolume(float leftVolume, float rightVolume); 143 int32_t SetLooping(bool loop); 144 int32_t GetCurrentTime(int32_t ¤tTime); 145 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack); 146 int32_t GetPlaybackInfo(Format &playbackInfo); 147 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack); 148 int32_t GetVideoWidth(); 149 int32_t GetVideoHeight(); 150 int32_t GetDuration(int32_t &duration); 151 int32_t SetPlaybackSpeed(PlaybackRateMode mode); 152 int32_t GetPlaybackSpeed(PlaybackRateMode &mode); 153 int32_t SetPlaybackRate(float rate); 154 int32_t SelectBitRate(uint32_t bitRate); 155 bool IsPlaying(); 156 bool IsLooping(); 157 int32_t SetParameter(const Format ¶m); 158 int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback); 159 int32_t SetVideoSurface(sptr<Surface> surface); 160 sptr<Surface> GetVideoSurface(); 161 int32_t SelectTrack(int32_t index, bool &trackChange); 162 int32_t DeselectTrack(int32_t index, bool &trackChange); 163 int32_t GetCurrentTrack(int32_t trackType, int32_t &index); 164 int32_t AddSubSource(const std::string &url); 165 int32_t AddSubSource(const std::string &path, int64_t offset, int64_t size); 166 std::string GetSubtitleText(std::string text); 167 sptr<Surface> GetVideoSurfaceNext(); 168 PlayerStates GetState(); 169 int32_t SetPlayRange(int64_t start, int64_t end); 170 int32_t SeekContinuous(int32_t mseconds); 171 int32_t SetMaxAmplitudeCbStatus(bool status); 172 int32_t SetPlaybackStrategy(AVPlayStrategy strategy); 173 int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted); 174 int32_t SetDeviceChangeCbStatus(bool status); 175 int32_t SetSuperResolution(bool enabled); 176 int32_t SetVideoWindowSize(int32_t width, int32_t height); 177 int32_t SetVolumeMode(int32_t mode); 178 int32_t EnableReportMediaProgress(bool enable); 179 void ReleaseClientListener(); 180 int32_t EnableReportAudioInterrupt(bool enable); 181 private: 182 void SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode); 183 std::shared_ptr<Player> player_ = nullptr; 184 std::shared_ptr<MediaDataSourceTest> dataSrc_ = nullptr; 185 std::shared_ptr<PlayerCallbackTest> callback_ = nullptr; 186 sptr<Rosen::Window> previewWindow_ = nullptr; 187 int32_t height_ = 1080; 188 int32_t width_ = 1920; 189 std::mutex mutex_; 190 sptr<Rosen::Window> previewWindowNext_ = nullptr; 191 int32_t nextSurfaceHeight_ = 100; 192 int32_t nextSurfaceWidth_ = 100; 193 }; 194 } // namespace Media 195 } // namespace OHOS 196 #endif