• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_LITE
16 #include "test_player.hpp"
17 
18 #include <iostream>
19 #include <thread>
20 #include <chrono>
21 
22 #include "foundation/log.h"
23 #include "i_engine_factory.h"
24 #include "i_player_engine.h"
25 #include "media_errors.h"
26 
27 extern "C" {
28 __attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
29 }
30 
31 using namespace OHOS::Media;
32 
33 namespace OHOS::Media::Test {
34 bool g_playFinished = false;
35 bool g_seekFinished = false;
36 
37 class PlayerCallbackImpl : public IPlayerEngineObs {
38 public:
OnError(PlayerErrorType errorType,int32_t errorCode)39     void OnError(PlayerErrorType errorType, int32_t errorCode) override
40     {
41         if (errorCode == MSERR_SEEK_FAILED) {
42             g_seekFinished = true;
43         }
44     }
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)45     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format& infoBody) override
46     {
47         if (type == INFO_TYPE_EOS) {
48             g_playFinished = true;
49         }
50         if (type == INFO_TYPE_SEEKDONE) {
51             g_seekFinished = true;
52         }
53     }
54 };
55 std::shared_ptr<IPlayerEngineObs> gCallback = std::make_shared<PlayerCallbackImpl>();
56 
57 class TestPlayerImpl : public TestPlayer {
58 public:
TestPlayerImpl(std::unique_ptr<IPlayerEngine> player)59     explicit TestPlayerImpl(std::unique_ptr<IPlayerEngine> player) : player_(std::move(player)) {}
60     int32_t SetSource(const TestSource& source) override;
61     int32_t SetSingleLoop(bool loop) override;
62     bool IsPlaying() override;
63     int32_t Prepare() override;
64     int32_t Play() override;
65     int32_t Pause() override;
66     int32_t Stop() override;
67     int32_t Reset() override;
68     int32_t Release() override;
69     int32_t Seek(int64_t timeMs) override;
70     int32_t GetCurrentTime(int64_t& currentMs) override;
71     int32_t GetDuration(int64_t& durationMs) override;
72     int32_t SetVolume(float leftVolume, float rightVolume) override;
73     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override;
74 private:
75     std::unique_ptr<IPlayerEngine> player_;
76 };
77 
CreateTestSource(std::string & url,TestSourceType type,Plugin::Seekable seekable)78 TestSource TestSource::CreateTestSource(std::string& url, TestSourceType type, Plugin::Seekable seekable)
79 {
80     switch (type) {
81         case TestSourceType::URI:
82             return TestSource(url);
83         case TestSourceType::STREAM:
84             return TestSource(url, seekable);
85     }
86 }
87 
Create()88 std::unique_ptr<TestPlayer> TestPlayer::Create()
89 {
90     auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
91     auto player = engineFactory->CreatePlayerEngine(0, 0);
92     player->SetObs(gCallback);
93     g_playFinished = false;
94     return std::make_unique<TestPlayerImpl>(std::move(player));
95 }
96 
SetSource(const TestSource & source)97 int32_t TestPlayerImpl::SetSource(const TestSource& source)
98 {
99     if (source.type_ == TestSourceType::URI) {
100         return player_->SetSource(source.url_);
101     } else if (source.type_ == TestSourceType::STREAM) {
102         auto src = std::make_shared<IMediaDataSourceImpl>(source.url_, source.seekable_);
103         return player_->SetSource(src);
104     }
105 }
106 
SetSingleLoop(bool loop)107 int32_t TestPlayerImpl::SetSingleLoop(bool loop)
108 {
109     return player_->SetLooping(loop);
110 }
111 
IsPlaying()112 bool TestPlayerImpl::IsPlaying()
113 {
114     return !g_playFinished;
115 }
116 
Prepare()117 int32_t TestPlayerImpl::Prepare()
118 {
119     return player_->Prepare();
120 }
121 
Play()122 int32_t TestPlayerImpl::Play()
123 {
124     g_playFinished = false;
125     return player_->Play();
126 }
127 
Pause()128 int32_t TestPlayerImpl::Pause()
129 {
130     return player_->Pause();
131 }
132 
Stop()133 int32_t TestPlayerImpl::Stop()
134 {
135     return player_->Stop();
136 }
137 
Reset()138 int32_t TestPlayerImpl::Reset()
139 {
140     return player_->Reset();
141 }
142 
Release()143 int32_t TestPlayerImpl::Release()
144 {
145     player_ = nullptr;
146     return ERR_OK;
147 }
148 
Seek(int64_t timeMs)149 int32_t TestPlayerImpl::Seek(int64_t timeMs)
150 {
151     int32_t ret = player_->Seek(timeMs, PlayerSeekMode::SEEK_CLOSEST);
152     NZERO_RETURN(ret);
153     while (!g_seekFinished) {
154         std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 50
155     }
156     FALSE_RETURN_V(g_seekFinished, false);
157     g_seekFinished = false;
158     return ret;
159 }
160 
GetCurrentTime(int64_t & currentMs)161 int32_t TestPlayerImpl::GetCurrentTime(int64_t& currentMs)
162 {
163     int32_t currentTimeMS = 0;
164     int32_t ret = player_->GetCurrentTime(currentTimeMS);
165     currentMs = currentTimeMS;
166     return ret;
167 }
168 
GetDuration(int64_t & durationMs)169 int32_t TestPlayerImpl::GetDuration(int64_t& durationMs)
170 {
171     int32_t duration;
172     int32_t ret = player_->GetDuration(duration);
173     durationMs = duration;
174     return ret;
175 }
176 
SetVolume(float leftVolume,float rightVolume)177 int32_t TestPlayerImpl::SetVolume(float leftVolume, float rightVolume)
178 {
179     int32_t ret = player_->SetVolume(leftVolume, rightVolume);
180     return ret;
181 }
182 
GetAudioTrackInfo(std::vector<Format> & audioTrack)183 int32_t TestPlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
184 {
185     int32_t ret = player_->GetAudioTrackInfo(audioTrack);
186     return ret;
187 }
188 }
189 #endif