1 /*
2  * Copyright (c) 2022-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 #ifdef OHOS_LITE
16 #include "test_player.hpp"
17 
18 #include <iostream>
19 #include <thread>
20 #include <chrono>
21 
22 #include "scene/lite/hiplayer.h"
23 #include "foundation/log.h"
24 #include "foundation/cpp_ext/memory_ext.h"
25 
26 using namespace OHOS::Media;
27 
28 namespace OHOS::Media::Test {
29 static bool g_playFinished = false;
30 static bool g_seekFinished = false;
31 
32 class PlayerCallbackImpl : public PlayerCallback {
OnPlaybackComplete()33     void OnPlaybackComplete() override
34     {
35         g_playFinished = true;
36     }
37 
OnError(int32_t errorType,int32_t errorCode)38     void OnError(int32_t errorType, int32_t errorCode) override
39     {
40     }
41 
OnInfo(int type,int extra)42     void OnInfo(int type, int extra) override
43     {
44     }
45 
OnVideoSizeChanged(int width,int height)46     void OnVideoSizeChanged(int width, int height) override
47     {
48     }
49 
OnRewindToComplete()50     void OnRewindToComplete() override
51     {
52         g_seekFinished = true;
53     }
54 };
55 
56 static std::shared_ptr<PlayerCallback> gCallback = std::make_shared<PlayerCallbackImpl>();
57 
58 class TestPlayerImpl : public TestPlayer {
59 public:
TestPlayerImpl(std::shared_ptr<Media::PlayerInterface> player)60     explicit TestPlayerImpl(std::shared_ptr<Media::PlayerInterface> player) : player_(std::move(player)) {}
~TestPlayerImpl()61     ~TestPlayerImpl() override
62     {
63         MEDIA_LOG_I("TestPlayerImpl dtor called.");
64     }
65     int32_t SetSource(const TestSource& source) override;
66     int32_t SetSingleLoop(bool loop) override;
67     bool IsPlaying() override;
68     int32_t Prepare() override;
69     int32_t Play() override;
70     int32_t Pause() override;
71     int32_t Stop() override;
72     int32_t Reset() override;
73     int32_t Seek(int64_t timeMs) override;
74     int32_t GetCurrentTime(int64_t& currentMS) override;
75     int32_t GetDuration(int64_t& durationMs) override;
76     int32_t Release() override;
77     int32_t SetVolume(float leftVolume, float rightVolume) override;
78     int32_t GetAudioTrackInfo(std::vector<Format>& audioTrack) override;
79 private:
80     std::shared_ptr<Media::PlayerInterface> player_;
81 };
82 
Create()83 std::unique_ptr<TestPlayer> TestPlayer::Create()
84 {
85     auto player = OHOS::Media::CreateHiPlayer();
86     FALSE_LOG(player->Init() == 0);
87     player->SetPlayerCallback(gCallback);
88     g_playFinished = false;
89     return CppExt::make_unique<TestPlayerImpl>(player);
90 }
91 
SetSource(const TestSource & source)92 int32_t TestPlayerImpl::SetSource(const TestSource& source)
93 {
94     OHOS::Media::Source sourceParam(source.url_);
95     return player_->SetSource(sourceParam);
96 }
97 
SetSingleLoop(bool loop)98 int32_t TestPlayerImpl::SetSingleLoop(bool loop)
99 {
100     return player_->SetLoop(loop);
101 }
102 
IsPlaying()103 bool TestPlayerImpl::IsPlaying()
104 {
105     return !g_playFinished;
106 }
107 
Prepare()108 int32_t TestPlayerImpl::Prepare()
109 {
110     return player_->Prepare();
111 }
112 
Play()113 int32_t TestPlayerImpl::Play()
114 {
115     g_playFinished = false;
116     return player_->Play();
117 }
118 
Pause()119 int32_t TestPlayerImpl::Pause()
120 {
121     return player_->Pause();
122 }
123 
Release()124 int32_t TestPlayerImpl::Release()
125 {
126     player_ = nullptr;
127     return 0;
128 }
129 
Stop()130 int32_t TestPlayerImpl::Stop()
131 {
132     return player_->Stop();
133 }
134 
Reset()135 int32_t TestPlayerImpl::Reset()
136 {
137     return player_->Reset();
138 }
139 
Seek(int64_t timeMs)140 int32_t TestPlayerImpl::Seek(int64_t timeMs)
141 {
142     int32_t ret = player_->Rewind(timeMs, 0);
143     NZERO_RETURN(ret);
144     while (!g_seekFinished) {
145         std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 50
146     }
147     FALSE_RETURN_V(g_seekFinished, false);
148     g_seekFinished = false;
149     return ret;
150 }
151 
GetCurrentTime(int64_t & currentMS)152 int32_t TestPlayerImpl::GetCurrentTime(int64_t& currentMS)
153 {
154     return player_->GetCurrentPosition(currentMS);
155 }
156 
GetDuration(int64_t & durationMs)157 int32_t TestPlayerImpl::GetDuration(int64_t& durationMs)
158 {
159     return player_->GetDuration(durationMs);
160 }
161 
SetVolume(float leftVolume,float rightVolume)162 int32_t TestPlayerImpl::SetVolume(float leftVolume, float rightVolume)
163 {
164     int32_t ret = player_->SetVolume(leftVolume, rightVolume);
165     return ret;
166 }
167 
GetAudioTrackInfo(std::vector<Format> & audioTrack)168 int32_t TestPlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
169 {
170     return 0;
171 }
172 }
173 #endif