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_2_SEC = 2000; 31 inline constexpr int32_t WAITSECOND = 6; 32 inline constexpr int32_t DELTA_TIME = 1000; 33 inline constexpr int32_t PLAYING_TIME_2_SEC = 2; 34 inline constexpr int32_t PLAYING_TIME_1_SEC = 1; 35 inline constexpr int32_t PLAYING_TIME_10_SEC = 10; 36 const std::string MEDIA_ROOT = "file:///data/test/"; 37 const std::string VIDEO_FILE1 = MEDIA_ROOT + "H264_AAC.mp4"; 38 const std::string SUBTITLE_SRT_FIELE = MEDIA_ROOT + "utf8.srt"; 39 const std::string SUBTITLE_SRT_FIELE1 = MEDIA_ROOT + "utf8_test1.srt"; 40 const std::string SUBTITLE_0_SEC = "MediaOS: test for subtitle_1"; 41 const std::string SUBTITLE_1_SEC = "MediaOS: test for subtitle_2"; 42 const std::string SUBTITLE_2_SEC = "MediaOS: test for subtitle_3"; 43 const std::string SUBTITLE_3_SEC = "MediaOS: test for subtitle_4"; 44 const std::string SUBTITLE_4_SEC = "MediaOS: test for subtitle_5"; 45 const std::string SUBTITLE_5_SEC = "MediaOS: test for subtitle_6"; 46 const std::string SUBTITLE_6_SEC = "MediaOS: test for subtitle_7"; 47 const std::string SUBTITLE_8_SEC = "MediaOS: test for subtitle_9"; 48 const std::string SUBTITLE_10_SEC = "MediaOS: test for subtitle_10"; 49 const std::string SUBTITLE_TEST1_8_SEC = "MediaOS: test1 for subtitle_9"; 50 const std::string HTTPS_PLAY = "HTTPS"; 51 const std::string HTTP_PLAY = "HTTP"; 52 const std::string LOCAL_PLAY = "LOCAL"; 53 const std::string HLS_PLAY = "HLS"; 54 const std::string INVALID_FILE = MEDIA_ROOT + "invalid.mp4"; 55 } // namespace PlayerTestParam 56 57 class PlayerSignal { 58 protected: 59 PlayerStates state_ = PLAYER_IDLE; 60 int32_t seekPosition_; 61 bool seekDoneFlag_; 62 bool speedDoneFlag_; 63 bool trackDoneFlag_ = false; 64 bool trackChange_ = false; 65 bool trackInfoUpdate_ = false; 66 bool textUpdate_ = false; 67 std::string text_ = ""; 68 PlayerSeekMode seekMode_ = PlayerSeekMode::SEEK_CLOSEST; 69 std::mutex mutexCond_; 70 std::mutex subtitleMutex_; 71 std::condition_variable condVarText_; 72 std::condition_variable condVarPrepare_; 73 std::condition_variable condVarPlay_; 74 std::condition_variable condVarPause_; 75 std::condition_variable condVarStop_; 76 std::condition_variable condVarReset_; 77 std::condition_variable condVarSeek_; 78 std::condition_variable condVarSpeed_; 79 std::condition_variable condVarTrackDone_; 80 std::condition_variable condVarTrackInfoUpdate_; 81 }; 82 83 class PlayerCallbackTest : public PlayerCallback, public NoCopyable, public PlayerSignal { 84 public: ~PlayerCallbackTest()85 ~PlayerCallbackTest() {} 86 void OnError(int32_t errorCode, const std::string &errorMsg) override; 87 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override; 88 void SeekNotify(int32_t extra, const Format &infoBody); 89 void Notify(PlayerStates currentState); 90 void SetSeekDoneFlag(bool seekDoneFlag); 91 void SetSpeedDoneFlag(bool speedDoneFlag); 92 void SetSeekPosition(int32_t seekPosition); 93 void SetState(PlayerStates state); 94 void SetTrackDoneFlag(bool trackDoneFlag); 95 int32_t PrepareSync(); 96 int32_t PlaySync(); 97 int32_t PauseSync(); 98 int32_t StopSync(); 99 int32_t ResetSync(); 100 int32_t SeekSync(); 101 int32_t SpeedSync(); 102 int32_t TrackSync(bool &trackChange); 103 int32_t TrackInfoUpdateSync(); 104 std::string SubtitleTextUpdate(std::string text); 105 private: 106 void HandleSubtitleCallback(int32_t extra, const Format &infoBody); 107 void HandleTrackInfoCallback(int32_t extra, const Format &infoBody); 108 }; 109 110 class PlayerMock : public NoCopyable { 111 public: 112 explicit PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback); 113 virtual ~PlayerMock(); 114 bool CreatePlayer(); 115 int32_t SetSource(const std::string url); 116 int32_t SetDataSrc(const std::string &path, int32_t size, bool seekable); 117 int32_t SetSource(const std::string &path, int64_t offset, int64_t size); 118 int32_t SetSource(int32_t fd, int64_t offset, int64_t size); 119 int32_t Prepare(); 120 int32_t PrepareAsync(); 121 int32_t Play(); 122 int32_t Pause(); 123 int32_t Stop(); 124 int32_t Reset(); 125 int32_t Release(); 126 int32_t ReleaseSync(); 127 int32_t Seek(int32_t mseconds, PlayerSeekMode mode); 128 int32_t SetVolume(float leftVolume, float rightVolume); 129 int32_t SetLooping(bool loop); 130 int32_t GetCurrentTime(int32_t ¤tTime); 131 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack); 132 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack); 133 int32_t GetVideoWidth(); 134 int32_t GetVideoHeight(); 135 int32_t GetDuration(int32_t &duration); 136 int32_t SetPlaybackSpeed(PlaybackRateMode mode); 137 int32_t GetPlaybackSpeed(PlaybackRateMode &mode); 138 int32_t SelectBitRate(uint32_t bitRate); 139 bool IsPlaying(); 140 bool IsLooping(); 141 int32_t SetParameter(const Format ¶m); 142 int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback); 143 int32_t SetVideoSurface(sptr<Surface> surface); 144 sptr<Surface> GetVideoSurface(); 145 int32_t SelectTrack(int32_t index, bool &trackChange); 146 int32_t DeselectTrack(int32_t index, bool &trackChange); 147 int32_t GetCurrentTrack(int32_t trackType, int32_t &index); 148 int32_t AddSubSource(const std::string &url); 149 int32_t AddSubSource(const std::string &path, int64_t offset, int64_t size); 150 std::string GetSubtitleText(std::string text); 151 private: 152 void SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode); 153 std::shared_ptr<Player> player_ = nullptr; 154 std::shared_ptr<MediaDataSourceTest> dataSrc_ = nullptr; 155 std::shared_ptr<PlayerCallbackTest> callback_ = nullptr; 156 sptr<Rosen::Window> previewWindow_ = nullptr; 157 int32_t height_ = 1080; 158 int32_t width_ = 1920; 159 std::mutex mutex_; 160 }; 161 } // namespace Media 162 } // namespace OHOS 163 #endif