18c77b71bSopenharmony_ci/*
28c77b71bSopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
38c77b71bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48c77b71bSopenharmony_ci * you may not use this file except in compliance with the License.
58c77b71bSopenharmony_ci * You may obtain a copy of the License at
68c77b71bSopenharmony_ci *
78c77b71bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88c77b71bSopenharmony_ci *
98c77b71bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108c77b71bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118c77b71bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128c77b71bSopenharmony_ci * See the License for the specific language governing permissions and
138c77b71bSopenharmony_ci * limitations under the License.
148c77b71bSopenharmony_ci */
158c77b71bSopenharmony_ci
168c77b71bSopenharmony_ci#include "player.h"
178c77b71bSopenharmony_ci#include <cinttypes>
188c77b71bSopenharmony_ci#include <sys/stat.h>
198c77b71bSopenharmony_ci#include "media_log.h"
208c77b71bSopenharmony_ci#include "player_client.h"
218c77b71bSopenharmony_ci#include "pms_interface.h"
228c77b71bSopenharmony_ci
238c77b71bSopenharmony_ciusing namespace std;
248c77b71bSopenharmony_ci
258c77b71bSopenharmony_cinamespace OHOS {
268c77b71bSopenharmony_cinamespace Media {
278c77b71bSopenharmony_ciclass Player;
288c77b71bSopenharmony_ci
298c77b71bSopenharmony_ci#define CHK_NULL_RETURN(ptr) \
308c77b71bSopenharmony_cido { \
318c77b71bSopenharmony_ci    if (ptr == nullptr) { \
328c77b71bSopenharmony_ci        MEDIA_ERR_LOG("ptr null"); \
338c77b71bSopenharmony_ci        return -1; \
348c77b71bSopenharmony_ci    } \
358c77b71bSopenharmony_ci} while (0)
368c77b71bSopenharmony_ci
378c77b71bSopenharmony_ciPlayer::Player()
388c77b71bSopenharmony_ci{
398c77b71bSopenharmony_ci    MEDIA_INFO_LOG("Player process");
408c77b71bSopenharmony_ci    player_ = PlayerClient::GetInstance();
418c77b71bSopenharmony_ci}
428c77b71bSopenharmony_ci
438c77b71bSopenharmony_ciPlayer::~Player()
448c77b71bSopenharmony_ci{
458c77b71bSopenharmony_ci    MEDIA_INFO_LOG("~Player process");
468c77b71bSopenharmony_ci}
478c77b71bSopenharmony_ci
488c77b71bSopenharmony_ciint32_t Player::SetSource(const Source &source)
498c77b71bSopenharmony_ci{
508c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
518c77b71bSopenharmony_ci    int32_t ret;
528c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
538c77b71bSopenharmony_ci    ret = player_->SetSource(source);
548c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process out");
558c77b71bSopenharmony_ci    return ret;
568c77b71bSopenharmony_ci}
578c77b71bSopenharmony_ci
588c77b71bSopenharmony_ciint32_t Player::Prepare()
598c77b71bSopenharmony_ci{
608c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
618c77b71bSopenharmony_ci    if (CheckSelfPermission("ohos.permission.MODIFY_AUDIO_SETTINGS") != GRANTED) {
628c77b71bSopenharmony_ci        MEDIA_WARNING_LOG("Process can not access audio-setting.");
638c77b71bSopenharmony_ci        return MEDIA_PERMISSION_DENIED;
648c77b71bSopenharmony_ci    }
658c77b71bSopenharmony_ci    if (CheckSelfPermission("ohos.permission.READ_MEDIA") != GRANTED) {
668c77b71bSopenharmony_ci        MEDIA_WARNING_LOG("Process can not read media.");
678c77b71bSopenharmony_ci        return MEDIA_PERMISSION_DENIED;
688c77b71bSopenharmony_ci    }
698c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
708c77b71bSopenharmony_ci    return player_->Prepare();
718c77b71bSopenharmony_ci}
728c77b71bSopenharmony_ci
738c77b71bSopenharmony_ciint32_t Player::Play()
748c77b71bSopenharmony_ci{
758c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
768c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
778c77b71bSopenharmony_ci    return player_->Play();
788c77b71bSopenharmony_ci}
798c77b71bSopenharmony_ci
808c77b71bSopenharmony_cibool Player::IsPlaying()
818c77b71bSopenharmony_ci{
828c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
838c77b71bSopenharmony_ci    if (player_ == nullptr) {
848c77b71bSopenharmony_ci        MEDIA_ERR_LOG("ptr null");
858c77b71bSopenharmony_ci        return false;
868c77b71bSopenharmony_ci    }
878c77b71bSopenharmony_ci    return player_->IsPlaying();
888c77b71bSopenharmony_ci}
898c77b71bSopenharmony_ci
908c77b71bSopenharmony_ciint32_t Player::Pause()
918c77b71bSopenharmony_ci{
928c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
938c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
948c77b71bSopenharmony_ci    return player_->Pause();
958c77b71bSopenharmony_ci}
968c77b71bSopenharmony_ci
978c77b71bSopenharmony_ciint32_t Player::Stop()
988c77b71bSopenharmony_ci{
998c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1008c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1018c77b71bSopenharmony_ci    return player_->Stop();
1028c77b71bSopenharmony_ci}
1038c77b71bSopenharmony_ci
1048c77b71bSopenharmony_ciint32_t Player::Rewind(int64_t mSeconds, int32_t mode)
1058c77b71bSopenharmony_ci{
1068c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1078c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1088c77b71bSopenharmony_ci    return player_->Rewind(mSeconds, mode);
1098c77b71bSopenharmony_ci}
1108c77b71bSopenharmony_ci
1118c77b71bSopenharmony_ciint32_t Player::SetVolume(float leftVolume, float rightVolume)
1128c77b71bSopenharmony_ci{
1138c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1148c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1158c77b71bSopenharmony_ci    return player_->SetVolume(leftVolume, rightVolume);
1168c77b71bSopenharmony_ci}
1178c77b71bSopenharmony_ci
1188c77b71bSopenharmony_ciint32_t Player::SetVideoSurface(Surface *surface)
1198c77b71bSopenharmony_ci{
1208c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1218c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1228c77b71bSopenharmony_ci    return player_->SetSurface(surface);
1238c77b71bSopenharmony_ci}
1248c77b71bSopenharmony_ci
1258c77b71bSopenharmony_cibool Player::IsSingleLooping()
1268c77b71bSopenharmony_ci{
1278c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1288c77b71bSopenharmony_ci    if (player_ == nullptr) {
1298c77b71bSopenharmony_ci        MEDIA_ERR_LOG("ptr null");
1308c77b71bSopenharmony_ci        return false;
1318c77b71bSopenharmony_ci    }
1328c77b71bSopenharmony_ci    return player_->IsSingleLooping();
1338c77b71bSopenharmony_ci}
1348c77b71bSopenharmony_ci
1358c77b71bSopenharmony_ciint32_t Player::GetCurrentTime(int64_t &time) const
1368c77b71bSopenharmony_ci{
1378c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1388c77b71bSopenharmony_ci    return player_->GetCurrentPosition(time);
1398c77b71bSopenharmony_ci}
1408c77b71bSopenharmony_ci
1418c77b71bSopenharmony_ciint32_t Player::GetDuration(int64_t &durationMs) const
1428c77b71bSopenharmony_ci{
1438c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1448c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1458c77b71bSopenharmony_ci    return player_->GetDuration(durationMs);
1468c77b71bSopenharmony_ci}
1478c77b71bSopenharmony_ci
1488c77b71bSopenharmony_ciint32_t Player::GetVideoWidth(int32_t &videoWidth)
1498c77b71bSopenharmony_ci{
1508c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1518c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1528c77b71bSopenharmony_ci    return player_->GetVideoWidth(videoWidth);
1538c77b71bSopenharmony_ci}
1548c77b71bSopenharmony_ci
1558c77b71bSopenharmony_ciint32_t Player::GetVideoHeight(int32_t &videoHeight)
1568c77b71bSopenharmony_ci{
1578c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1588c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1598c77b71bSopenharmony_ci    return player_->GetVideoHeight(videoHeight);
1608c77b71bSopenharmony_ci}
1618c77b71bSopenharmony_ci
1628c77b71bSopenharmony_ciint32_t Player::Reset()
1638c77b71bSopenharmony_ci{
1648c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1658c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1668c77b71bSopenharmony_ci    return player_->Reset();
1678c77b71bSopenharmony_ci}
1688c77b71bSopenharmony_ci
1698c77b71bSopenharmony_ciint32_t Player::Release()
1708c77b71bSopenharmony_ci{
1718c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1728c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1738c77b71bSopenharmony_ci    return player_->Release();
1748c77b71bSopenharmony_ci}
1758c77b71bSopenharmony_ci
1768c77b71bSopenharmony_civoid Player::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb)
1778c77b71bSopenharmony_ci{
1788c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1798c77b71bSopenharmony_ci    if (player_ == nullptr) {
1808c77b71bSopenharmony_ci        MEDIA_ERR_LOG("ptr null");
1818c77b71bSopenharmony_ci        return;
1828c77b71bSopenharmony_ci    }
1838c77b71bSopenharmony_ci    player_->SetPlayerCallback(cb);
1848c77b71bSopenharmony_ci}
1858c77b71bSopenharmony_ci
1868c77b71bSopenharmony_ciint32_t Player::EnableSingleLooping(bool loop)
1878c77b71bSopenharmony_ci{
1888c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1898c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1908c77b71bSopenharmony_ci    return player_->SetLoop(loop);
1918c77b71bSopenharmony_ci}
1928c77b71bSopenharmony_ci
1938c77b71bSopenharmony_ciint32_t Player::GetPlayerState(int32_t &state) const
1948c77b71bSopenharmony_ci{
1958c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
1968c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
1978c77b71bSopenharmony_ci    return player_->GetPlayerState(state);
1988c77b71bSopenharmony_ci}
1998c77b71bSopenharmony_ci
2008c77b71bSopenharmony_ciint32_t Player::SetPlaybackSpeed(float speed)
2018c77b71bSopenharmony_ci{
2028c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
2038c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
2048c77b71bSopenharmony_ci    return player_->SetPlaybackSpeed(speed);
2058c77b71bSopenharmony_ci}
2068c77b71bSopenharmony_ci
2078c77b71bSopenharmony_ciint32_t Player::GetPlaybackSpeed(float &speed)
2088c77b71bSopenharmony_ci{
2098c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
2108c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
2118c77b71bSopenharmony_ci    return player_->GetPlaybackSpeed(speed);
2128c77b71bSopenharmony_ci}
2138c77b71bSopenharmony_ci
2148c77b71bSopenharmony_ciint32_t Player::SetAudioStreamType(int32_t type)
2158c77b71bSopenharmony_ci{
2168c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
2178c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
2188c77b71bSopenharmony_ci    return player_->SetAudioStreamType(type);
2198c77b71bSopenharmony_ci}
2208c77b71bSopenharmony_ci
2218c77b71bSopenharmony_civoid Player::GetAudioStreamType(int32_t &type)
2228c77b71bSopenharmony_ci{
2238c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
2248c77b71bSopenharmony_ci    if (player_ == nullptr) {
2258c77b71bSopenharmony_ci        MEDIA_ERR_LOG("player_ null");
2268c77b71bSopenharmony_ci        return;
2278c77b71bSopenharmony_ci    }
2288c77b71bSopenharmony_ci    player_->GetAudioStreamType(type);
2298c77b71bSopenharmony_ci}
2308c77b71bSopenharmony_ci
2318c77b71bSopenharmony_ciint32_t Player::SetParameter(const Format &params)
2328c77b71bSopenharmony_ci{
2338c77b71bSopenharmony_ci    MEDIA_INFO_LOG("process in");
2348c77b71bSopenharmony_ci    CHK_NULL_RETURN(player_);
2358c77b71bSopenharmony_ci    return player_->SetParameter(params);
2368c77b71bSopenharmony_ci}
2378c77b71bSopenharmony_ci}  // namespace Media
2388c77b71bSopenharmony_ci}  // namespace OHOS
239