• 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 "window.h"
23 
24 namespace OHOS {
25 namespace Media {
26 namespace PlayerTestParam {
27 inline constexpr int32_t SEEK_TIME_5_SEC = 5000;
28 inline constexpr int32_t SEEK_TIME_2_SEC = 2000;
29 inline constexpr int32_t WAITSECOND = 6;
30 inline constexpr int32_t DELTA_TIME = 1000;
31 inline constexpr int32_t PLAYING_TIME = 2;
32 const std::string MEDIA_ROOT = "file:///data/test/";
33 const std::string VIDEO_FILE1 = MEDIA_ROOT + "H264_AAC.mp4";
34 const std::string HTTPS_PLAY = "HTTPS";
35 const std::string HTTP_PLAY = "HTTP";
36 const std::string LOCAL_PLAY = "LOCAL";
37 const std::string HLS_PLAY = "HLS";
38 } // namespace PlayerTestParam
39 
40 class PlayerSignal {
41 protected:
42     PlayerStates state_ = PLAYER_IDLE;
43     int32_t seekPosition_;
44     bool seekDoneFlag_;
45     bool speedDoneFlag_;
46     PlayerSeekMode seekMode_ = PlayerSeekMode::SEEK_CLOSEST;
47     std::mutex mutexCond_;
48     std::condition_variable condVarPrepare_;
49     std::condition_variable condVarPlay_;
50     std::condition_variable condVarPause_;
51     std::condition_variable condVarStop_;
52     std::condition_variable condVarReset_;
53     std::condition_variable condVarSeek_;
54     std::condition_variable condVarSpeed_;
55 };
56 
57 class PlayerCallbackTest : public PlayerCallback, public NoCopyable, public PlayerSignal {
58 public:
~PlayerCallbackTest()59     ~PlayerCallbackTest() {}
60     void OnError(int32_t errorCode, const std::string &errorMsg) override;
61     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
62     void SeekNotify(int32_t extra, const Format &infoBody);
63     void Notify(PlayerStates currentState);
64     void SetSeekDoneFlag(bool seekDoneFlag);
65     void SetSpeedDoneFlag(bool speedDoneFlag);
66     void SetSeekPosition(int32_t seekPosition);
67     void SetState(PlayerStates state);
68     int32_t PrepareSync();
69     int32_t PlaySync();
70     int32_t PauseSync();
71     int32_t StopSync();
72     int32_t ResetSync();
73     int32_t SeekSync();
74     int32_t SpeedSync();
75 };
76 
77 class PlayerMock : public NoCopyable {
78 public:
79     explicit PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback);
80     virtual ~PlayerMock();
81     bool CreatePlayer();
82     int32_t SetSource(const std::string url);
83     int32_t SetDataSrc(const std::string &path, int32_t size, bool seekable);
84     int32_t SetSource(const std::string &path, int64_t offset, int64_t size);
85     int32_t Prepare();
86     int32_t PrepareAsync();
87     int32_t Play();
88     int32_t Pause();
89     int32_t Stop();
90     int32_t Reset();
91     int32_t Release();
92     int32_t ReleaseSync();
93     int32_t Seek(int32_t mseconds, PlayerSeekMode mode);
94     int32_t SetVolume(float leftVolume, float rightVolume);
95     int32_t SetLooping(bool loop);
96     int32_t GetCurrentTime(int32_t &currentTime);
97     int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack);
98     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack);
99     int32_t GetVideoWidth();
100     int32_t GetVideoHeight();
101     int32_t GetDuration(int32_t &duration);
102     int32_t SetPlaybackSpeed(PlaybackRateMode mode);
103     int32_t GetPlaybackSpeed(PlaybackRateMode &mode);
104     int32_t SelectBitRate(uint32_t bitRate);
105     bool IsPlaying();
106     bool IsLooping();
107     int32_t SetParameter(const Format &param);
108     int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback);
109     int32_t SetVideoSurface(sptr<Surface> surface);
110     sptr<Surface> GetVideoSurface();
111 private:
112     void SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode);
113     std::shared_ptr<Player> player_ = nullptr;
114     std::shared_ptr<MediaDataSourceTest> dataSrc_ = nullptr;
115     std::shared_ptr<PlayerCallbackTest> callback_ = nullptr;
116     sptr<Rosen::Window> previewWindow_ = nullptr;
117     int32_t height_ = 1080;
118     int32_t width_ = 1920;
119     std::mutex mutex_;
120 };
121 } // namespace Media
122 } // namespace OHOS
123 #endif