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
26using namespace OHOS::Media;
27
28namespace OHOS::Media::Test {
29static bool g_playFinished = false;
30static bool g_seekFinished = false;
31
32class PlayerCallbackImpl : public PlayerCallback {
33    void OnPlaybackComplete() override
34    {
35        g_playFinished = true;
36    }
37
38    void OnError(int32_t errorType, int32_t errorCode) override
39    {
40    }
41
42    void OnInfo(int type, int extra) override
43    {
44    }
45
46    void OnVideoSizeChanged(int width, int height) override
47    {
48    }
49
50    void OnRewindToComplete() override
51    {
52        g_seekFinished = true;
53    }
54};
55
56static std::shared_ptr<PlayerCallback> gCallback = std::make_shared<PlayerCallbackImpl>();
57
58class TestPlayerImpl : public TestPlayer {
59public:
60    explicit TestPlayerImpl(std::shared_ptr<Media::PlayerInterface> player) : player_(std::move(player)) {}
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;
79private:
80    std::shared_ptr<Media::PlayerInterface> player_;
81};
82
83std::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
92int32_t TestPlayerImpl::SetSource(const TestSource& source)
93{
94    OHOS::Media::Source sourceParam(source.url_);
95    return player_->SetSource(sourceParam);
96}
97
98int32_t TestPlayerImpl::SetSingleLoop(bool loop)
99{
100    return player_->SetLoop(loop);
101}
102
103bool TestPlayerImpl::IsPlaying()
104{
105    return !g_playFinished;
106}
107
108int32_t TestPlayerImpl::Prepare()
109{
110    return player_->Prepare();
111}
112
113int32_t TestPlayerImpl::Play()
114{
115    g_playFinished = false;
116    return player_->Play();
117}
118
119int32_t TestPlayerImpl::Pause()
120{
121    return player_->Pause();
122}
123
124int32_t TestPlayerImpl::Release()
125{
126    player_ = nullptr;
127    return 0;
128}
129
130int32_t TestPlayerImpl::Stop()
131{
132    return player_->Stop();
133}
134
135int32_t TestPlayerImpl::Reset()
136{
137    return player_->Reset();
138}
139
140int32_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
152int32_t TestPlayerImpl::GetCurrentTime(int64_t& currentMS)
153{
154    return player_->GetCurrentPosition(currentMS);
155}
156
157int32_t TestPlayerImpl::GetDuration(int64_t& durationMs)
158{
159    return player_->GetDuration(durationMs);
160}
161
162int32_t TestPlayerImpl::SetVolume(float leftVolume, float rightVolume)
163{
164    int32_t ret = player_->SetVolume(leftVolume, rightVolume);
165    return ret;
166}
167
168int32_t TestPlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
169{
170    return 0;
171}
172}
173#endif