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