1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci#ifdef OHOS_LITE
16fa7767c5Sopenharmony_ci#include "test_player.hpp"
17fa7767c5Sopenharmony_ci
18fa7767c5Sopenharmony_ci#include <iostream>
19fa7767c5Sopenharmony_ci#include <thread>
20fa7767c5Sopenharmony_ci#include <chrono>
21fa7767c5Sopenharmony_ci
22fa7767c5Sopenharmony_ci#include "scene/lite/hiplayer.h"
23fa7767c5Sopenharmony_ci#include "foundation/log.h"
24fa7767c5Sopenharmony_ci#include "foundation/cpp_ext/memory_ext.h"
25fa7767c5Sopenharmony_ci
26fa7767c5Sopenharmony_ciusing namespace OHOS::Media;
27fa7767c5Sopenharmony_ci
28fa7767c5Sopenharmony_cinamespace OHOS::Media::Test {
29fa7767c5Sopenharmony_cistatic bool g_playFinished = false;
30fa7767c5Sopenharmony_cistatic bool g_seekFinished = false;
31fa7767c5Sopenharmony_ci
32fa7767c5Sopenharmony_ciclass PlayerCallbackImpl : public PlayerCallback {
33fa7767c5Sopenharmony_ci    void OnPlaybackComplete() override
34fa7767c5Sopenharmony_ci    {
35fa7767c5Sopenharmony_ci        g_playFinished = true;
36fa7767c5Sopenharmony_ci    }
37fa7767c5Sopenharmony_ci
38fa7767c5Sopenharmony_ci    void OnError(int32_t errorType, int32_t errorCode) override
39fa7767c5Sopenharmony_ci    {
40fa7767c5Sopenharmony_ci    }
41fa7767c5Sopenharmony_ci
42fa7767c5Sopenharmony_ci    void OnInfo(int type, int extra) override
43fa7767c5Sopenharmony_ci    {
44fa7767c5Sopenharmony_ci    }
45fa7767c5Sopenharmony_ci
46fa7767c5Sopenharmony_ci    void OnVideoSizeChanged(int width, int height) override
47fa7767c5Sopenharmony_ci    {
48fa7767c5Sopenharmony_ci    }
49fa7767c5Sopenharmony_ci
50fa7767c5Sopenharmony_ci    void OnRewindToComplete() override
51fa7767c5Sopenharmony_ci    {
52fa7767c5Sopenharmony_ci        g_seekFinished = true;
53fa7767c5Sopenharmony_ci    }
54fa7767c5Sopenharmony_ci};
55fa7767c5Sopenharmony_ci
56fa7767c5Sopenharmony_cistatic std::shared_ptr<PlayerCallback> gCallback = std::make_shared<PlayerCallbackImpl>();
57fa7767c5Sopenharmony_ci
58fa7767c5Sopenharmony_ciclass TestPlayerImpl : public TestPlayer {
59fa7767c5Sopenharmony_cipublic:
60fa7767c5Sopenharmony_ci    explicit TestPlayerImpl(std::shared_ptr<Media::PlayerInterface> player) : player_(std::move(player)) {}
61fa7767c5Sopenharmony_ci    ~TestPlayerImpl() override
62fa7767c5Sopenharmony_ci    {
63fa7767c5Sopenharmony_ci        MEDIA_LOG_I("TestPlayerImpl dtor called.");
64fa7767c5Sopenharmony_ci    }
65fa7767c5Sopenharmony_ci    int32_t SetSource(const TestSource& source) override;
66fa7767c5Sopenharmony_ci    int32_t SetSingleLoop(bool loop) override;
67fa7767c5Sopenharmony_ci    bool IsPlaying() override;
68fa7767c5Sopenharmony_ci    int32_t Prepare() override;
69fa7767c5Sopenharmony_ci    int32_t Play() override;
70fa7767c5Sopenharmony_ci    int32_t Pause() override;
71fa7767c5Sopenharmony_ci    int32_t Stop() override;
72fa7767c5Sopenharmony_ci    int32_t Reset() override;
73fa7767c5Sopenharmony_ci    int32_t Seek(int64_t timeMs) override;
74fa7767c5Sopenharmony_ci    int32_t GetCurrentTime(int64_t& currentMS) override;
75fa7767c5Sopenharmony_ci    int32_t GetDuration(int64_t& durationMs) override;
76fa7767c5Sopenharmony_ci    int32_t Release() override;
77fa7767c5Sopenharmony_ci    int32_t SetVolume(float leftVolume, float rightVolume) override;
78fa7767c5Sopenharmony_ci    int32_t GetAudioTrackInfo(std::vector<Format>& audioTrack) override;
79fa7767c5Sopenharmony_ciprivate:
80fa7767c5Sopenharmony_ci    std::shared_ptr<Media::PlayerInterface> player_;
81fa7767c5Sopenharmony_ci};
82fa7767c5Sopenharmony_ci
83fa7767c5Sopenharmony_cistd::unique_ptr<TestPlayer> TestPlayer::Create()
84fa7767c5Sopenharmony_ci{
85fa7767c5Sopenharmony_ci    auto player = OHOS::Media::CreateHiPlayer();
86fa7767c5Sopenharmony_ci    FALSE_LOG(player->Init() == 0);
87fa7767c5Sopenharmony_ci    player->SetPlayerCallback(gCallback);
88fa7767c5Sopenharmony_ci    g_playFinished = false;
89fa7767c5Sopenharmony_ci    return CppExt::make_unique<TestPlayerImpl>(player);
90fa7767c5Sopenharmony_ci}
91fa7767c5Sopenharmony_ci
92fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::SetSource(const TestSource& source)
93fa7767c5Sopenharmony_ci{
94fa7767c5Sopenharmony_ci    OHOS::Media::Source sourceParam(source.url_);
95fa7767c5Sopenharmony_ci    return player_->SetSource(sourceParam);
96fa7767c5Sopenharmony_ci}
97fa7767c5Sopenharmony_ci
98fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::SetSingleLoop(bool loop)
99fa7767c5Sopenharmony_ci{
100fa7767c5Sopenharmony_ci    return player_->SetLoop(loop);
101fa7767c5Sopenharmony_ci}
102fa7767c5Sopenharmony_ci
103fa7767c5Sopenharmony_cibool TestPlayerImpl::IsPlaying()
104fa7767c5Sopenharmony_ci{
105fa7767c5Sopenharmony_ci    return !g_playFinished;
106fa7767c5Sopenharmony_ci}
107fa7767c5Sopenharmony_ci
108fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Prepare()
109fa7767c5Sopenharmony_ci{
110fa7767c5Sopenharmony_ci    return player_->Prepare();
111fa7767c5Sopenharmony_ci}
112fa7767c5Sopenharmony_ci
113fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Play()
114fa7767c5Sopenharmony_ci{
115fa7767c5Sopenharmony_ci    g_playFinished = false;
116fa7767c5Sopenharmony_ci    return player_->Play();
117fa7767c5Sopenharmony_ci}
118fa7767c5Sopenharmony_ci
119fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Pause()
120fa7767c5Sopenharmony_ci{
121fa7767c5Sopenharmony_ci    return player_->Pause();
122fa7767c5Sopenharmony_ci}
123fa7767c5Sopenharmony_ci
124fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Release()
125fa7767c5Sopenharmony_ci{
126fa7767c5Sopenharmony_ci    player_ = nullptr;
127fa7767c5Sopenharmony_ci    return 0;
128fa7767c5Sopenharmony_ci}
129fa7767c5Sopenharmony_ci
130fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Stop()
131fa7767c5Sopenharmony_ci{
132fa7767c5Sopenharmony_ci    return player_->Stop();
133fa7767c5Sopenharmony_ci}
134fa7767c5Sopenharmony_ci
135fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Reset()
136fa7767c5Sopenharmony_ci{
137fa7767c5Sopenharmony_ci    return player_->Reset();
138fa7767c5Sopenharmony_ci}
139fa7767c5Sopenharmony_ci
140fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::Seek(int64_t timeMs)
141fa7767c5Sopenharmony_ci{
142fa7767c5Sopenharmony_ci    int32_t ret = player_->Rewind(timeMs, 0);
143fa7767c5Sopenharmony_ci    NZERO_RETURN(ret);
144fa7767c5Sopenharmony_ci    while (!g_seekFinished) {
145fa7767c5Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 50
146fa7767c5Sopenharmony_ci    }
147fa7767c5Sopenharmony_ci    FALSE_RETURN_V(g_seekFinished, false);
148fa7767c5Sopenharmony_ci    g_seekFinished = false;
149fa7767c5Sopenharmony_ci    return ret;
150fa7767c5Sopenharmony_ci}
151fa7767c5Sopenharmony_ci
152fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::GetCurrentTime(int64_t& currentMS)
153fa7767c5Sopenharmony_ci{
154fa7767c5Sopenharmony_ci    return player_->GetCurrentPosition(currentMS);
155fa7767c5Sopenharmony_ci}
156fa7767c5Sopenharmony_ci
157fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::GetDuration(int64_t& durationMs)
158fa7767c5Sopenharmony_ci{
159fa7767c5Sopenharmony_ci    return player_->GetDuration(durationMs);
160fa7767c5Sopenharmony_ci}
161fa7767c5Sopenharmony_ci
162fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::SetVolume(float leftVolume, float rightVolume)
163fa7767c5Sopenharmony_ci{
164fa7767c5Sopenharmony_ci    int32_t ret = player_->SetVolume(leftVolume, rightVolume);
165fa7767c5Sopenharmony_ci    return ret;
166fa7767c5Sopenharmony_ci}
167fa7767c5Sopenharmony_ci
168fa7767c5Sopenharmony_ciint32_t TestPlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
169fa7767c5Sopenharmony_ci{
170fa7767c5Sopenharmony_ci    return 0;
171fa7767c5Sopenharmony_ci}
172fa7767c5Sopenharmony_ci}
173fa7767c5Sopenharmony_ci#endif