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_impl.h" 178c77b71bSopenharmony_ci#include <cinttypes> 188c77b71bSopenharmony_ci#include <climits> 198c77b71bSopenharmony_ci#include <string> 208c77b71bSopenharmony_ci#include <sys/prctl.h> 218c77b71bSopenharmony_ci#include "unistd.h" 228c77b71bSopenharmony_ci#include "securec.h" 238c77b71bSopenharmony_ci#include "format_type.h" 248c77b71bSopenharmony_ci#include "hi_liteplayer_err.h" 258c77b71bSopenharmony_ci#include "hi_liteplayer.h" 268c77b71bSopenharmony_ci#include "player_define.h" 278c77b71bSopenharmony_ci#include "media_log.h" 288c77b71bSopenharmony_ciextern "C" 298c77b71bSopenharmony_ci{ 308c77b71bSopenharmony_ci#include "codec_interface.h" 318c77b71bSopenharmony_ci} 328c77b71bSopenharmony_ci 338c77b71bSopenharmony_ciusing namespace std; 348c77b71bSopenharmony_ciusing OHOS::Media::AdapterStreamCallback; 358c77b71bSopenharmony_ci 368c77b71bSopenharmony_cinamespace OHOS { 378c77b71bSopenharmony_cinamespace Media { 388c77b71bSopenharmony_ciconst int32_t INVALID_MEDIA_POSITION = -1; 398c77b71bSopenharmony_ciconst int32_t DEFAULT_REWIND_TIME = 0; 408c77b71bSopenharmony_ciconst int32_t IDLE_QUEQUE_SLEEP_TIME_US = 5000; 418c77b71bSopenharmony_ciconst float MAX_MEDIA_VOLUME = 300.0f; 428c77b71bSopenharmony_ciconst int32_t POS_NOTIFY_INTERVAL_MS = 300; 438c77b71bSopenharmony_ci 448c77b71bSopenharmony_ci#define CHECK_FAILED_PRINT(value, target, printfString) \ 458c77b71bSopenharmony_cido { \ 468c77b71bSopenharmony_ci if ((value) != (target)) { \ 478c77b71bSopenharmony_ci MEDIA_ERR_LOG("%s", printfString ? printfString : " "); \ 488c77b71bSopenharmony_ci } \ 498c77b71bSopenharmony_ci} while (0) 508c77b71bSopenharmony_ci 518c77b71bSopenharmony_ci#define CHECK_FAILED_RETURN(value, target, ret, printfString) \ 528c77b71bSopenharmony_cido { \ 538c77b71bSopenharmony_ci if ((value) != (target)) { \ 548c77b71bSopenharmony_ci MEDIA_ERR_LOG("%s, ret:%d", printfString ? printfString : " ", ret); \ 558c77b71bSopenharmony_ci return ret; \ 568c77b71bSopenharmony_ci } \ 578c77b71bSopenharmony_ci} while (0) 588c77b71bSopenharmony_ci 598c77b71bSopenharmony_ci#define CHK_NULL_RETURN(ptr) \ 608c77b71bSopenharmony_cido { \ 618c77b71bSopenharmony_ci if (ptr == nullptr) { \ 628c77b71bSopenharmony_ci MEDIA_ERR_LOG("ptr null"); \ 638c77b71bSopenharmony_ci return -1; \ 648c77b71bSopenharmony_ci } \ 658c77b71bSopenharmony_ci} while (0) 668c77b71bSopenharmony_ci 678c77b71bSopenharmony_ciPlayerImpl::PlayerImpl() 688c77b71bSopenharmony_ci : player_(nullptr), speed_(1.0), playerControlState_(PLAY_STATUS_IDLE), 698c77b71bSopenharmony_ci isSingleLoop_(false), 708c77b71bSopenharmony_ci currentPosition_(0), 718c77b71bSopenharmony_ci rewindPosition_(INVALID_MEDIA_POSITION), 728c77b71bSopenharmony_ci surface_(nullptr), 738c77b71bSopenharmony_ci currentState_(PLAYER_IDLE), 748c77b71bSopenharmony_ci rewindMode_(PLAYER_SEEK_PREVIOUS_SYNC), 758c77b71bSopenharmony_ci currentRewindMode_(PLAYER_SEEK_PREVIOUS_SYNC), 768c77b71bSopenharmony_ci audioStreamType_(0), 778c77b71bSopenharmony_ci callback_(nullptr), 788c77b71bSopenharmony_ci inited_ (false), 798c77b71bSopenharmony_ci released_(false), 808c77b71bSopenharmony_ci isStreamSource_(false), 818c77b71bSopenharmony_ci bufferSource_(nullptr), 828c77b71bSopenharmony_ci streamCallback_(nullptr), 838c77b71bSopenharmony_ci pauseAfterPlay_(false), 848c77b71bSopenharmony_ci extraRewind_(false) 858c77b71bSopenharmony_ci{ 868c77b71bSopenharmony_ci (void)memset_s(&formatFileInfo_, sizeof(formatFileInfo_), 0, sizeof(FormatFileInfo)); 878c77b71bSopenharmony_ci formatFileInfo_.s32UsedVideoStreamIndex = -1; 888c77b71bSopenharmony_ci formatFileInfo_.s32UsedAudioStreamIndex = -1; 898c77b71bSopenharmony_ci formatFileInfo_.s64Duration = -1; 908c77b71bSopenharmony_ci buffer_.idx = -1; 918c77b71bSopenharmony_ci buffer_.flag = 0; 928c77b71bSopenharmony_ci buffer_.offset = 0; 938c77b71bSopenharmony_ci buffer_.size = 0; 948c77b71bSopenharmony_ci buffer_.timestamp = 0; 958c77b71bSopenharmony_ci (void)memset_s(&mediaAttr_, sizeof(mediaAttr_), 0, sizeof(PlayerControlStreamAttr)); 968c77b71bSopenharmony_ci} 978c77b71bSopenharmony_ci 988c77b71bSopenharmony_ciint32_t PlayerImpl::Init(void) 998c77b71bSopenharmony_ci{ 1008c77b71bSopenharmony_ci if (inited_ == true) { 1018c77b71bSopenharmony_ci return 0; 1028c77b71bSopenharmony_ci } 1038c77b71bSopenharmony_ci int ret = CodecInit(); 1048c77b71bSopenharmony_ci if (ret != 0) { 1058c77b71bSopenharmony_ci MEDIA_ERR_LOG("PlayerImpl::Init CodecInit err\n"); 1068c77b71bSopenharmony_ci return ret; 1078c77b71bSopenharmony_ci } 1088c77b71bSopenharmony_ci if (memset_s(&buffer_, sizeof(QueBuffer), 0, sizeof(QueBuffer)) != EOK) { 1098c77b71bSopenharmony_ci MEDIA_ERR_LOG("PlayerImpl::Init memset err\n"); 1108c77b71bSopenharmony_ci } 1118c77b71bSopenharmony_ci buffer_.idx = -1; 1128c77b71bSopenharmony_ci inited_ = true; 1138c77b71bSopenharmony_ci MEDIA_INFO_LOG("process success"); 1148c77b71bSopenharmony_ci return 0; 1158c77b71bSopenharmony_ci} 1168c77b71bSopenharmony_ci 1178c77b71bSopenharmony_ciint32_t PlayerImpl::DeInit(void) 1188c77b71bSopenharmony_ci{ 1198c77b71bSopenharmony_ci if (inited_ != true) { 1208c77b71bSopenharmony_ci return 0; 1218c77b71bSopenharmony_ci } 1228c77b71bSopenharmony_ci if (released_ == false) { 1238c77b71bSopenharmony_ci Release(); 1248c77b71bSopenharmony_ci } 1258c77b71bSopenharmony_ci inited_ = false; 1268c77b71bSopenharmony_ci return 0; 1278c77b71bSopenharmony_ci} 1288c77b71bSopenharmony_ci 1298c77b71bSopenharmony_ciPlayerImpl::~PlayerImpl() 1308c77b71bSopenharmony_ci{ 1318c77b71bSopenharmony_ci DeInit(); 1328c77b71bSopenharmony_ci player_ = nullptr; 1338c77b71bSopenharmony_ci MEDIA_INFO_LOG("~PlayerImpl process"); 1348c77b71bSopenharmony_ci} 1358c77b71bSopenharmony_ci 1368c77b71bSopenharmony_ciint32_t PlayerImpl::SetSource(const Source &source) 1378c77b71bSopenharmony_ci{ 1388c77b71bSopenharmony_ci Init(); 1398c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 1408c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 1418c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 1428c77b71bSopenharmony_ci if (currentState_ != PLAYER_IDLE) { 1438c77b71bSopenharmony_ci MEDIA_ERR_LOG("failed, current state is:%u", currentState_); 1448c77b71bSopenharmony_ci return -1; 1458c77b71bSopenharmony_ci } 1468c77b71bSopenharmony_ci 1478c77b71bSopenharmony_ci SourceType sType = source.GetSourceType(); 1488c77b71bSopenharmony_ci if (sType == SourceType::SOURCE_TYPE_FD) { 1498c77b71bSopenharmony_ci MEDIA_ERR_LOG("not support fdSource now"); 1508c77b71bSopenharmony_ci return -1; 1518c77b71bSopenharmony_ci } 1528c77b71bSopenharmony_ci 1538c77b71bSopenharmony_ci GetPlayer(); 1548c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 1558c77b71bSopenharmony_ci 1568c77b71bSopenharmony_ci int32_t ret = -1; 1578c77b71bSopenharmony_ci if (sType == SourceType::SOURCE_TYPE_URI) { 1588c77b71bSopenharmony_ci ret = SetUriSource(source); 1598c77b71bSopenharmony_ci } else if (sType == SourceType::SOURCE_TYPE_STREAM) { 1608c77b71bSopenharmony_ci ret = SetStreamSource(source); 1618c77b71bSopenharmony_ci } else { 1628c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetSource failed, source type is %d", static_cast<int32_t>(sType)); 1638c77b71bSopenharmony_ci } 1648c77b71bSopenharmony_ci 1658c77b71bSopenharmony_ci if (ret != 0) { 1668c77b71bSopenharmony_ci ResetInner(); 1678c77b71bSopenharmony_ci } 1688c77b71bSopenharmony_ci return ret; 1698c77b71bSopenharmony_ci} 1708c77b71bSopenharmony_ci 1718c77b71bSopenharmony_cistatic void ShowFileInfo(const FormatFileInfo *fileInfo) 1728c77b71bSopenharmony_ci{ 1738c77b71bSopenharmony_ci for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) { 1748c77b71bSopenharmony_ci const StreamResolution *resolution = &fileInfo->stSteamResolution[i]; 1758c77b71bSopenharmony_ci if (resolution->u32Width == 0 || resolution->u32Height == 0) { 1768c77b71bSopenharmony_ci break; 1778c77b71bSopenharmony_ci } 1788c77b71bSopenharmony_ci MEDIA_INFO_LOG("video[%d],w=%u,h=%u,index=%d ", i, resolution->u32Width, 1798c77b71bSopenharmony_ci resolution->u32Height, resolution->s32VideoStreamIndex); 1808c77b71bSopenharmony_ci } 1818c77b71bSopenharmony_ci MEDIA_INFO_LOG("audio channel_cnt=%u,sampleRate=%u,AudioStreamIndex=%d videoIndex:%d", 1828c77b71bSopenharmony_ci fileInfo->u32AudioChannelCnt, fileInfo->u32SampleRate, fileInfo->s32UsedAudioStreamIndex, 1838c77b71bSopenharmony_ci fileInfo->s32UsedVideoStreamIndex); 1848c77b71bSopenharmony_ci} 1858c77b71bSopenharmony_ci 1868c77b71bSopenharmony_civoid PlayerImpl::UpdateState(PlayerImpl *curPlayer, PlayerStatus state) 1878c77b71bSopenharmony_ci{ 1888c77b71bSopenharmony_ci if (curPlayer == nullptr) { 1898c77b71bSopenharmony_ci return; 1908c77b71bSopenharmony_ci } 1918c77b71bSopenharmony_ci 1928c77b71bSopenharmony_ci curPlayer->playerControlState_ = state; 1938c77b71bSopenharmony_ci MEDIA_INFO_LOG("player UpdateState, state:%d", state); 1948c77b71bSopenharmony_ci} 1958c77b71bSopenharmony_ci 1968c77b71bSopenharmony_civoid PlayerImpl::PlayerControlEventCb(void* pPlayer, PlayerControlEvent enEvent, const void* pData) 1978c77b71bSopenharmony_ci{ 1988c77b71bSopenharmony_ci PlayerControlError subErr = PLAYERCONTROL_ERROR_BUTT; 1998c77b71bSopenharmony_ci PlayerImpl *curPlayer = (PlayerImpl *)pPlayer; 2008c77b71bSopenharmony_ci 2018c77b71bSopenharmony_ci if (curPlayer == nullptr) { 2028c77b71bSopenharmony_ci MEDIA_ERR_LOG("the handle is error"); 2038c77b71bSopenharmony_ci return; 2048c77b71bSopenharmony_ci } 2058c77b71bSopenharmony_ci switch (enEvent) { 2068c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_STATE_CHANGED: 2078c77b71bSopenharmony_ci if (pData == nullptr) { 2088c77b71bSopenharmony_ci return; 2098c77b71bSopenharmony_ci } 2108c77b71bSopenharmony_ci curPlayer->UpdateState(curPlayer, *reinterpret_cast<const PlayerStatus *>(pData)); 2118c77b71bSopenharmony_ci break; 2128c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_EOF: 2138c77b71bSopenharmony_ci MEDIA_INFO_LOG("end of file"); 2148c77b71bSopenharmony_ci curPlayer->NotifyPlaybackComplete(curPlayer); 2158c77b71bSopenharmony_ci break; 2168c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_SOF: 2178c77b71bSopenharmony_ci MEDIA_INFO_LOG("start of file"); 2188c77b71bSopenharmony_ci break; 2198c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_ERROR: 2208c77b71bSopenharmony_ci if (pData == nullptr) { 2218c77b71bSopenharmony_ci return; 2228c77b71bSopenharmony_ci } 2238c77b71bSopenharmony_ci subErr = *reinterpret_cast<const PlayerControlError *>(pData); 2248c77b71bSopenharmony_ci MEDIA_ERR_LOG("error: %d", subErr); 2258c77b71bSopenharmony_ci if (curPlayer->callback_ != nullptr) { 2268c77b71bSopenharmony_ci curPlayer->callback_->OnError(0, subErr); 2278c77b71bSopenharmony_ci } 2288c77b71bSopenharmony_ci break; 2298c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_PROGRESS: 2308c77b71bSopenharmony_ci if (pData == nullptr) { 2318c77b71bSopenharmony_ci return; 2328c77b71bSopenharmony_ci } 2338c77b71bSopenharmony_ci curPlayer->currentPosition_ = *reinterpret_cast<const int64_t *>(pData); 2348c77b71bSopenharmony_ci break; 2358c77b71bSopenharmony_ci case PLAYERCONTROL_EVENT_SEEK_END: 2368c77b71bSopenharmony_ci if (pData == nullptr) { 2378c77b71bSopenharmony_ci return; 2388c77b71bSopenharmony_ci } 2398c77b71bSopenharmony_ci MEDIA_INFO_LOG("seek action end, time is %lld", *reinterpret_cast<const int64_t *>(pData)); 2408c77b71bSopenharmony_ci curPlayer->NotifySeekComplete(curPlayer, *reinterpret_cast<const int64_t *>(pData)); 2418c77b71bSopenharmony_ci break; 2428c77b71bSopenharmony_ci case PLAYERCONTROL_FIRST_VIDEO_FRAME: 2438c77b71bSopenharmony_ci MEDIA_INFO_LOG("render FirstVideoFrame"); 2448c77b71bSopenharmony_ci if (curPlayer->pauseAfterPlay_) { 2458c77b71bSopenharmony_ci curPlayer->player_->Pause(); 2468c77b71bSopenharmony_ci } 2478c77b71bSopenharmony_ci break; 2488c77b71bSopenharmony_ci case PLAYERCONTROL_FIRST_AUDIO_FRAME: 2498c77b71bSopenharmony_ci if (curPlayer->pauseAfterPlay_ && curPlayer->formatFileInfo_.s32UsedVideoStreamIndex == -1) { 2508c77b71bSopenharmony_ci curPlayer->player_->Pause(); 2518c77b71bSopenharmony_ci } 2528c77b71bSopenharmony_ci break; 2538c77b71bSopenharmony_ci default: 2548c77b71bSopenharmony_ci break; 2558c77b71bSopenharmony_ci } 2568c77b71bSopenharmony_ci} 2578c77b71bSopenharmony_ci 2588c77b71bSopenharmony_civoid PlayerImpl::ReportVideoSizeChange(void) 2598c77b71bSopenharmony_ci{ 2608c77b71bSopenharmony_ci for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) { 2618c77b71bSopenharmony_ci if (formatFileInfo_.stSteamResolution[i].s32VideoStreamIndex == formatFileInfo_.s32UsedVideoStreamIndex) { 2628c77b71bSopenharmony_ci if (callback_ != nullptr) { 2638c77b71bSopenharmony_ci callback_->OnVideoSizeChanged(formatFileInfo_.stSteamResolution[i].u32Width, 2648c77b71bSopenharmony_ci formatFileInfo_.stSteamResolution[i].u32Height); 2658c77b71bSopenharmony_ci } 2668c77b71bSopenharmony_ci break; 2678c77b71bSopenharmony_ci } 2688c77b71bSopenharmony_ci } 2698c77b71bSopenharmony_ci} 2708c77b71bSopenharmony_ci 2718c77b71bSopenharmony_ciint32_t PlayerImpl::Prepare() 2728c77b71bSopenharmony_ci{ 2738c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 2748c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 2758c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 2768c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 2778c77b71bSopenharmony_ci 2788c77b71bSopenharmony_ci if (currentState_ == PLAYER_PREPARED) { 2798c77b71bSopenharmony_ci MEDIA_ERR_LOG("have operated prepare before"); 2808c77b71bSopenharmony_ci return 0; 2818c77b71bSopenharmony_ci } 2828c77b71bSopenharmony_ci if (currentState_ != PLAYER_INITIALIZED) { 2838c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not Prepare, currentState_ is %u", currentState_); 2848c77b71bSopenharmony_ci return -1; 2858c77b71bSopenharmony_ci } 2868c77b71bSopenharmony_ci 2878c77b71bSopenharmony_ci PlayerCtrlCallbackParam param; 2888c77b71bSopenharmony_ci param.player = this; 2898c77b71bSopenharmony_ci param.callbackFun = PlayerControlEventCb; 2908c77b71bSopenharmony_ci int ret = player_->RegCallback(param); 2918c77b71bSopenharmony_ci if (ret != 0) { 2928c77b71bSopenharmony_ci MEDIA_ERR_LOG("RegCallback exec failed "); 2938c77b71bSopenharmony_ci return -1; 2948c77b71bSopenharmony_ci } 2958c77b71bSopenharmony_ci 2968c77b71bSopenharmony_ci currentState_ = PLAYER_PREPARING; 2978c77b71bSopenharmony_ci ret = player_->Prepare(); 2988c77b71bSopenharmony_ci if (ret != 0) { 2998c77b71bSopenharmony_ci MEDIA_ERR_LOG("Prepare exec failed"); 3008c77b71bSopenharmony_ci currentState_ = PLAYER_INITIALIZED; 3018c77b71bSopenharmony_ci return -1; 3028c77b71bSopenharmony_ci } 3038c77b71bSopenharmony_ci currentState_ = PLAYER_PREPARED; 3048c77b71bSopenharmony_ci (void)player_->SetAudioStreamType(audioStreamType_); 3058c77b71bSopenharmony_ci 3068c77b71bSopenharmony_ci ret = player_->GetFileInfo(formatFileInfo_); 3078c77b71bSopenharmony_ci if (ret != 0) { 3088c77b71bSopenharmony_ci MEDIA_ERR_LOG("GetFileInfo failed"); 3098c77b71bSopenharmony_ci return ret; 3108c77b71bSopenharmony_ci } 3118c77b71bSopenharmony_ci ShowFileInfo(&formatFileInfo_); 3128c77b71bSopenharmony_ci if (formatFileInfo_.s32UsedVideoStreamIndex == -1) { 3138c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 3148c77b71bSopenharmony_ci return 0; 3158c77b71bSopenharmony_ci } 3168c77b71bSopenharmony_ci 3178c77b71bSopenharmony_ci /* report video solution */ 3188c77b71bSopenharmony_ci ReportVideoSizeChange(); 3198c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 3208c77b71bSopenharmony_ci return 0; 3218c77b71bSopenharmony_ci} 3228c77b71bSopenharmony_ci 3238c77b71bSopenharmony_ciint32_t PlayerImpl::SetMediaStream(void) 3248c77b71bSopenharmony_ci{ 3258c77b71bSopenharmony_ci int32_t ret; 3268c77b71bSopenharmony_ci 3278c77b71bSopenharmony_ci mediaAttr_.s32VidStreamId = formatFileInfo_.s32UsedVideoStreamIndex; 3288c77b71bSopenharmony_ci mediaAttr_.s32AudStreamId = formatFileInfo_.s32UsedAudioStreamIndex; 3298c77b71bSopenharmony_ci ret = player_->SetMedia(mediaAttr_); 3308c77b71bSopenharmony_ci if (ret != 0) { 3318c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetMedia exec failed"); 3328c77b71bSopenharmony_ci return ret; 3338c77b71bSopenharmony_ci } 3348c77b71bSopenharmony_ci 3358c77b71bSopenharmony_ci for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) { 3368c77b71bSopenharmony_ci StreamResolution *resolution = &formatFileInfo_.stSteamResolution[i]; 3378c77b71bSopenharmony_ci if (resolution->s32VideoStreamIndex == mediaAttr_.s32VidStreamId) { 3388c77b71bSopenharmony_ci MEDIA_INFO_LOG("used video w=%u,h=%u,index=%d", 3398c77b71bSopenharmony_ci resolution->u32Width, resolution->u32Height, mediaAttr_.s32VidStreamId); 3408c77b71bSopenharmony_ci break; 3418c77b71bSopenharmony_ci } 3428c77b71bSopenharmony_ci } 3438c77b71bSopenharmony_ci return 0; 3448c77b71bSopenharmony_ci} 3458c77b71bSopenharmony_ci 3468c77b71bSopenharmony_ciint32_t PlayerImpl::Play() 3478c77b71bSopenharmony_ci{ 3488c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 3498c77b71bSopenharmony_ci int ret; 3508c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 3518c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 3528c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 3538c77b71bSopenharmony_ci if (currentState_ == PLAYER_STARTED) { 3548c77b71bSopenharmony_ci MEDIA_INFO_LOG("no need to repeat operation"); 3558c77b71bSopenharmony_ci return 0; 3568c77b71bSopenharmony_ci } 3578c77b71bSopenharmony_ci if (currentState_ != PLAYER_PREPARED && currentState_ != PLAYER_PAUSED) { 3588c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not Play, currentState is %u", currentState_); 3598c77b71bSopenharmony_ci return -1; 3608c77b71bSopenharmony_ci } 3618c77b71bSopenharmony_ci if (currentState_ == PLAYER_PAUSED) { 3628c77b71bSopenharmony_ci goto play; 3638c77b71bSopenharmony_ci } 3648c77b71bSopenharmony_ci 3658c77b71bSopenharmony_ci ret = SetMediaStream(); 3668c77b71bSopenharmony_ci CHECK_FAILED_RETURN(ret, 0, ret, "SetMeidaStream failed"); 3678c77b71bSopenharmony_ciplay: 3688c77b71bSopenharmony_ci ret = player_->Play(); 3698c77b71bSopenharmony_ci if (ret != 0) { 3708c77b71bSopenharmony_ci MEDIA_ERR_LOG("Play exec failed %x", ret); 3718c77b71bSopenharmony_ci return -1; 3728c77b71bSopenharmony_ci } 3738c77b71bSopenharmony_ci currentState_ = PLAYER_STARTED; 3748c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 3758c77b71bSopenharmony_ci return 0; 3768c77b71bSopenharmony_ci} 3778c77b71bSopenharmony_ci 3788c77b71bSopenharmony_cibool PlayerImpl::IsPlaying() 3798c77b71bSopenharmony_ci{ 3808c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 3818c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 3828c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, 0, "have released or not create"); 3838c77b71bSopenharmony_ci bool isPlaying = false; 3848c77b71bSopenharmony_ci if (player_ != nullptr) { 3858c77b71bSopenharmony_ci isPlaying = (currentState_ != PLAYER_STARTED) ? false : true; 3868c77b71bSopenharmony_ci } 3878c77b71bSopenharmony_ci return isPlaying; 3888c77b71bSopenharmony_ci} 3898c77b71bSopenharmony_ci 3908c77b71bSopenharmony_ciint32_t PlayerImpl::Pause() 3918c77b71bSopenharmony_ci{ 3928c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 3938c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 3948c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 3958c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 3968c77b71bSopenharmony_ci if (currentState_ == PLAYER_PAUSED) { 3978c77b71bSopenharmony_ci MEDIA_ERR_LOG("currentState_ is %d", currentState_); 3988c77b71bSopenharmony_ci return 0; 3998c77b71bSopenharmony_ci } 4008c77b71bSopenharmony_ci if (speed_ != 1.0) { 4018c77b71bSopenharmony_ci MEDIA_ERR_LOG("failed, currentState_ speed is %f", speed_); 4028c77b71bSopenharmony_ci return -1; 4038c77b71bSopenharmony_ci } 4048c77b71bSopenharmony_ci if (pauseAfterPlay_ && currentState_ == PLAYER_PREPARED) { 4058c77b71bSopenharmony_ci int32_t ret; 4068c77b71bSopenharmony_ci ret = SetMediaStream(); 4078c77b71bSopenharmony_ci CHECK_FAILED_RETURN(ret, 0, ret, "SetMeidaStream failed"); 4088c77b71bSopenharmony_ci ret = player_->Play(); 4098c77b71bSopenharmony_ci CHECK_FAILED_RETURN(ret, 0, ret, "Play failed"); 4108c77b71bSopenharmony_ci currentState_ = PLAYER_PAUSED; 4118c77b71bSopenharmony_ci return 0; 4128c77b71bSopenharmony_ci } 4138c77b71bSopenharmony_ci if (currentState_ != PLAYER_STARTED) { 4148c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not Pause, currentState_ is %u", currentState_); 4158c77b71bSopenharmony_ci return -1; 4168c77b71bSopenharmony_ci } 4178c77b71bSopenharmony_ci 4188c77b71bSopenharmony_ci player_->Pause(); 4198c77b71bSopenharmony_ci currentState_ = PLAYER_PAUSED; 4208c77b71bSopenharmony_ci return 0; 4218c77b71bSopenharmony_ci} 4228c77b71bSopenharmony_ci 4238c77b71bSopenharmony_ciint32_t PlayerImpl::Stop() 4248c77b71bSopenharmony_ci{ 4258c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 4268c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 4278c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 4288c77b71bSopenharmony_ci if (currentState_ == PLAYER_STOPPED) { 4298c77b71bSopenharmony_ci return 0; 4308c77b71bSopenharmony_ci } 4318c77b71bSopenharmony_ci if ((currentState_ != PLAYER_STARTED) && (currentState_ != PLAYER_PAUSED) && 4328c77b71bSopenharmony_ci (currentState_ != PLAYER_PLAYBACK_COMPLETE) && (currentState_ != PLAYER_STATE_ERROR)) { 4338c77b71bSopenharmony_ci MEDIA_INFO_LOG("current state: %u, no need to do stop", currentState_); 4348c77b71bSopenharmony_ci return -1; 4358c77b71bSopenharmony_ci } 4368c77b71bSopenharmony_ci 4378c77b71bSopenharmony_ci if (player_ != nullptr) { 4388c77b71bSopenharmony_ci int32_t ret = player_->Stop(); 4398c77b71bSopenharmony_ci if (ret != 0) { 4408c77b71bSopenharmony_ci MEDIA_ERR_LOG("Stop failed, ret is %d", ret); 4418c77b71bSopenharmony_ci } 4428c77b71bSopenharmony_ci } 4438c77b71bSopenharmony_ci currentState_ = PLAYER_STOPPED; 4448c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 4458c77b71bSopenharmony_ci return 0; 4468c77b71bSopenharmony_ci} 4478c77b71bSopenharmony_ci 4488c77b71bSopenharmony_ciint32_t PlayerImpl::RewindInner(int64_t mSeconds, PlayerSeekMode mode) 4498c77b71bSopenharmony_ci{ 4508c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 4518c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 4528c77b71bSopenharmony_ci if (mSeconds < DEFAULT_REWIND_TIME) { 4538c77b71bSopenharmony_ci MEDIA_WARNING_LOG("Attempt to rewind to invalid position %lld", mSeconds); 4548c77b71bSopenharmony_ci mSeconds = DEFAULT_REWIND_TIME; 4558c77b71bSopenharmony_ci } 4568c77b71bSopenharmony_ci int32_t ret; 4578c77b71bSopenharmony_ci int64_t durationMs = -1; 4588c77b71bSopenharmony_ci GetDurationInner(durationMs); 4598c77b71bSopenharmony_ci if ((durationMs > DEFAULT_REWIND_TIME) && (mSeconds > durationMs)) { 4608c77b71bSopenharmony_ci MEDIA_WARNING_LOG("Attempt to rewind to past end of file, request is %lld, durationMs is %lld", mSeconds, 4618c77b71bSopenharmony_ci durationMs); 4628c77b71bSopenharmony_ci return -1; 4638c77b71bSopenharmony_ci } 4648c77b71bSopenharmony_ci currentRewindMode_ = mode; 4658c77b71bSopenharmony_ci if (rewindPosition_ >= DEFAULT_REWIND_TIME) { 4668c77b71bSopenharmony_ci rewindPosition_ = mSeconds; 4678c77b71bSopenharmony_ci MEDIA_WARNING_LOG("is deal last rewind time:%lld", mSeconds); 4688c77b71bSopenharmony_ci return 0; 4698c77b71bSopenharmony_ci } 4708c77b71bSopenharmony_ci 4718c77b71bSopenharmony_ci rewindPosition_ = mSeconds; 4728c77b71bSopenharmony_ci rewindMode_ = mode; 4738c77b71bSopenharmony_ci ret = player_->Seek(mSeconds); 4748c77b71bSopenharmony_ci if (ret != 0) { 4758c77b71bSopenharmony_ci MEDIA_ERR_LOG("do seek failed, ret is %d", ret); 4768c77b71bSopenharmony_ci } 4778c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 4788c77b71bSopenharmony_ci return ret; 4798c77b71bSopenharmony_ci} 4808c77b71bSopenharmony_ci 4818c77b71bSopenharmony_cibool PlayerImpl::IsValidRewindMode(PlayerSeekMode mode) 4828c77b71bSopenharmony_ci{ 4838c77b71bSopenharmony_ci switch (mode) { 4848c77b71bSopenharmony_ci case PLAYER_SEEK_PREVIOUS_SYNC: 4858c77b71bSopenharmony_ci case PLAYER_SEEK_NEXT_SYNC: 4868c77b71bSopenharmony_ci case PLAYER_SEEK_CLOSEST_SYNC: 4878c77b71bSopenharmony_ci case PLAYER_SEEK_CLOSEST: 4888c77b71bSopenharmony_ci break; 4898c77b71bSopenharmony_ci default: 4908c77b71bSopenharmony_ci MEDIA_ERR_LOG("Unknown rewind mode %d", mode); 4918c77b71bSopenharmony_ci return false; 4928c77b71bSopenharmony_ci } 4938c77b71bSopenharmony_ci return true; 4948c77b71bSopenharmony_ci} 4958c77b71bSopenharmony_ci 4968c77b71bSopenharmony_ciint32_t PlayerImpl::Rewind(int64_t mSeconds, int32_t mode) 4978c77b71bSopenharmony_ci{ 4988c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 4998c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 5008c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 5018c77b71bSopenharmony_ci if (currentState_ != PLAYER_STARTED && currentState_ != PLAYER_PAUSED && currentState_ != PLAYER_PREPARED) { 5028c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not Rewind, currentState_ is %u", currentState_); 5038c77b71bSopenharmony_ci return -1; 5048c77b71bSopenharmony_ci } 5058c77b71bSopenharmony_ci if (speed_ != 1.0) { 5068c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not Rewind, currentState_ speed is %f", speed_); 5078c77b71bSopenharmony_ci return -1; 5088c77b71bSopenharmony_ci } 5098c77b71bSopenharmony_ci 5108c77b71bSopenharmony_ci if (IsValidRewindMode((PlayerSeekMode)mode) != true) { 5118c77b71bSopenharmony_ci MEDIA_ERR_LOG("Rewind failed, msec is %lld, mode is %d", mSeconds, mode); 5128c77b71bSopenharmony_ci return -1; 5138c77b71bSopenharmony_ci } 5148c77b71bSopenharmony_ci 5158c77b71bSopenharmony_ci if (isStreamSource_ == true) { 5168c77b71bSopenharmony_ci MEDIA_ERR_LOG("Failed, streamsource not support Rewind"); 5178c77b71bSopenharmony_ci return -1; 5188c77b71bSopenharmony_ci } 5198c77b71bSopenharmony_ci 5208c77b71bSopenharmony_ci std::lock_guard<std::mutex> rewindLock(rewindLock_); 5218c77b71bSopenharmony_ci int32_t ret = RewindInner(mSeconds, (PlayerSeekMode)mode); 5228c77b71bSopenharmony_ci if (ret != 0) { 5238c77b71bSopenharmony_ci MEDIA_ERR_LOG("ReWind failed, ret is %d", ret); 5248c77b71bSopenharmony_ci } else { 5258c77b71bSopenharmony_ci currentPosition_ = mSeconds; 5268c77b71bSopenharmony_ci extraRewind_ = true; 5278c77b71bSopenharmony_ci } 5288c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 5298c77b71bSopenharmony_ci return ret; 5308c77b71bSopenharmony_ci} 5318c77b71bSopenharmony_ci 5328c77b71bSopenharmony_ciint32_t PlayerImpl::SetVolume(float leftVolume, float rightVolume) 5338c77b71bSopenharmony_ci{ 5348c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 5358c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 5368c77b71bSopenharmony_ci VolumeAttr attr; 5378c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 5388c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 5398c77b71bSopenharmony_ci if ((currentState_ != PLAYER_STARTED) && (currentState_ != PLAYER_PAUSED) && 5408c77b71bSopenharmony_ci (currentState_ != PLAYER_PREPARED) && (currentState_ != PLAYER_INITIALIZED)) { 5418c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetVolume failed, currentState_ is %u", currentState_); 5428c77b71bSopenharmony_ci return -1; 5438c77b71bSopenharmony_ci } 5448c77b71bSopenharmony_ci if (leftVolume < 0 || leftVolume > MAX_MEDIA_VOLUME || rightVolume < 0 || rightVolume > MAX_MEDIA_VOLUME) { 5458c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetVolume failed, the volume should be set to a value ranging from 0 to 300"); 5468c77b71bSopenharmony_ci return -1; 5478c77b71bSopenharmony_ci } 5488c77b71bSopenharmony_ci attr.leftVolume = leftVolume; 5498c77b71bSopenharmony_ci attr.rightVolume = rightVolume; 5508c77b71bSopenharmony_ci int ret = player_->SetVolume(attr); 5518c77b71bSopenharmony_ci if (ret != 0) { 5528c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetVolume failed %x", ret); 5538c77b71bSopenharmony_ci } 5548c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out\n"); 5558c77b71bSopenharmony_ci return ret; 5568c77b71bSopenharmony_ci} 5578c77b71bSopenharmony_ci 5588c77b71bSopenharmony_ciint32_t PlayerImpl::SetSurface(Surface *surface) 5598c77b71bSopenharmony_ci{ 5608c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 5618c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 5628c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 5638c77b71bSopenharmony_ci if ((currentState_ != PLAYER_PREPARED) && (currentState_ != PLAYER_INITIALIZED)) { 5648c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetSurface failed, currentState_ is %u", currentState_); 5658c77b71bSopenharmony_ci return -1; 5668c77b71bSopenharmony_ci } 5678c77b71bSopenharmony_ci surface_ = surface; 5688c77b71bSopenharmony_ci player_->SetSurface(surface); 5698c77b71bSopenharmony_ci return 0; 5708c77b71bSopenharmony_ci} 5718c77b71bSopenharmony_ci 5728c77b71bSopenharmony_cibool PlayerImpl::IsSingleLooping() 5738c77b71bSopenharmony_ci{ 5748c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 5758c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, false, "have released or not create"); 5768c77b71bSopenharmony_ci bool isLoop = (player_ == nullptr) ? false : isSingleLoop_; 5778c77b71bSopenharmony_ci return isLoop; 5788c77b71bSopenharmony_ci} 5798c77b71bSopenharmony_ci 5808c77b71bSopenharmony_ciint32_t PlayerImpl::GetPlayerState(int32_t &state) 5818c77b71bSopenharmony_ci{ 5828c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 5838c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 5848c77b71bSopenharmony_ci state = currentState_; 5858c77b71bSopenharmony_ci return 0; 5868c77b71bSopenharmony_ci} 5878c77b71bSopenharmony_ci 5888c77b71bSopenharmony_ciint32_t PlayerImpl::GetCurrentPosition(int64_t &position) 5898c77b71bSopenharmony_ci{ 5908c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 5918c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 5928c77b71bSopenharmony_ci position = (currentPosition_ >= 0) ? currentPosition_ : 0; 5938c77b71bSopenharmony_ci return 0; 5948c77b71bSopenharmony_ci} 5958c77b71bSopenharmony_ci 5968c77b71bSopenharmony_civoid PlayerImpl::GetDurationInner(int64_t &durationMs) 5978c77b71bSopenharmony_ci{ 5988c77b71bSopenharmony_ci durationMs = formatFileInfo_.s64Duration; 5998c77b71bSopenharmony_ci} 6008c77b71bSopenharmony_ci 6018c77b71bSopenharmony_ciint32_t PlayerImpl::GetDuration(int64_t &durationMs) 6028c77b71bSopenharmony_ci{ 6038c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 6048c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 6058c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 6068c77b71bSopenharmony_ci if (currentState_ == PLAYER_IDLE || currentState_ == PLAYER_INITIALIZED) { 6078c77b71bSopenharmony_ci durationMs = 0; 6088c77b71bSopenharmony_ci return 0; 6098c77b71bSopenharmony_ci } 6108c77b71bSopenharmony_ci GetDurationInner(durationMs); 6118c77b71bSopenharmony_ci return 0; 6128c77b71bSopenharmony_ci} 6138c77b71bSopenharmony_ci 6148c77b71bSopenharmony_ciint32_t PlayerImpl::GetVideoWidth(int32_t &videoWidth) 6158c77b71bSopenharmony_ci{ 6168c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 6178c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 6188c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 6198c77b71bSopenharmony_ci 6208c77b71bSopenharmony_ci videoWidth = 0; 6218c77b71bSopenharmony_ci if (currentState_ != PLAYER_PREPARED && currentState_ != PLAYER_STARTED && currentState_ != PLAYER_PAUSED && 6228c77b71bSopenharmony_ci currentState_ != PLAYER_STOPPED && currentState_ != PLAYER_PLAYBACK_COMPLETE) { 6238c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not GetVideoWidth, currentState_ is %u", currentState_); 6248c77b71bSopenharmony_ci return -1; 6258c77b71bSopenharmony_ci } 6268c77b71bSopenharmony_ci 6278c77b71bSopenharmony_ci if (formatFileInfo_.s32UsedVideoStreamIndex == -1) { 6288c77b71bSopenharmony_ci return -1; 6298c77b71bSopenharmony_ci } 6308c77b71bSopenharmony_ci for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) { 6318c77b71bSopenharmony_ci if (formatFileInfo_.stSteamResolution[i].s32VideoStreamIndex == formatFileInfo_.s32UsedVideoStreamIndex) { 6328c77b71bSopenharmony_ci videoWidth = formatFileInfo_.stSteamResolution[i].u32Width; 6338c77b71bSopenharmony_ci break; 6348c77b71bSopenharmony_ci } 6358c77b71bSopenharmony_ci } 6368c77b71bSopenharmony_ci return 0; 6378c77b71bSopenharmony_ci} 6388c77b71bSopenharmony_ci 6398c77b71bSopenharmony_ciint32_t PlayerImpl::GetVideoHeight(int32_t &videoHeight) 6408c77b71bSopenharmony_ci{ 6418c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 6428c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 6438c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 6448c77b71bSopenharmony_ci 6458c77b71bSopenharmony_ci videoHeight = 0; 6468c77b71bSopenharmony_ci if (currentState_ != PLAYER_PREPARED && currentState_ != PLAYER_STARTED && currentState_ != PLAYER_PAUSED && 6478c77b71bSopenharmony_ci currentState_ != PLAYER_STOPPED && currentState_ != PLAYER_PLAYBACK_COMPLETE) { 6488c77b71bSopenharmony_ci MEDIA_ERR_LOG("Can not GetVideoHeight, currentState_ is %u", currentState_); 6498c77b71bSopenharmony_ci return -1; 6508c77b71bSopenharmony_ci } 6518c77b71bSopenharmony_ci if (formatFileInfo_.s32UsedVideoStreamIndex == -1) { 6528c77b71bSopenharmony_ci return -1; 6538c77b71bSopenharmony_ci } 6548c77b71bSopenharmony_ci 6558c77b71bSopenharmony_ci for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) { 6568c77b71bSopenharmony_ci if (formatFileInfo_.stSteamResolution[i].s32VideoStreamIndex == formatFileInfo_.s32UsedVideoStreamIndex) { 6578c77b71bSopenharmony_ci videoHeight = formatFileInfo_.stSteamResolution[i].u32Height; 6588c77b71bSopenharmony_ci break; 6598c77b71bSopenharmony_ci } 6608c77b71bSopenharmony_ci } 6618c77b71bSopenharmony_ci return 0; 6628c77b71bSopenharmony_ci} 6638c77b71bSopenharmony_ci 6648c77b71bSopenharmony_cistatic int32_t PlayerControlCheckTPlayAttr(const float speed, TplayDirect direction) 6658c77b71bSopenharmony_ci{ 6668c77b71bSopenharmony_ci if ((direction != TPLAY_DIRECT_BACKWARD) && (direction != TPLAY_DIRECT_FORWARD)) { 6678c77b71bSopenharmony_ci return HI_FAILURE; 6688c77b71bSopenharmony_ci } 6698c77b71bSopenharmony_ci 6708c77b71bSopenharmony_ci if (speed != PLAY_SPEED_2X_FAST && 6718c77b71bSopenharmony_ci speed != PLAY_SPEED_4X_FAST && 6728c77b71bSopenharmony_ci speed != PLAY_SPEED_8X_FAST && 6738c77b71bSopenharmony_ci speed != PLAY_SPEED_16X_FAST && 6748c77b71bSopenharmony_ci speed != PLAY_SPEED_32X_FAST && 6758c77b71bSopenharmony_ci speed != PLAY_SPEED_64X_FAST && 6768c77b71bSopenharmony_ci speed != PLAY_SPEED_128X_FAST) { 6778c77b71bSopenharmony_ci return HI_FAILURE; 6788c77b71bSopenharmony_ci } 6798c77b71bSopenharmony_ci 6808c77b71bSopenharmony_ci return HI_SUCCESS; 6818c77b71bSopenharmony_ci} 6828c77b71bSopenharmony_ci 6838c77b71bSopenharmony_ciint32_t PlayerImpl::SetPlaybackSpeed(float speed) 6848c77b71bSopenharmony_ci{ 6858c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 6868c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 6878c77b71bSopenharmony_ci if (speed <= 0.f) { 6888c77b71bSopenharmony_ci return -1; 6898c77b71bSopenharmony_ci } 6908c77b71bSopenharmony_ci CHK_NULL_RETURN(player_); 6918c77b71bSopenharmony_ci if (currentState_ != PLAYER_STARTED) { 6928c77b71bSopenharmony_ci MEDIA_ERR_LOG("currentState_ is %u", currentState_); 6938c77b71bSopenharmony_ci return -1; 6948c77b71bSopenharmony_ci } 6958c77b71bSopenharmony_ci if (speed == speed_) { 6968c77b71bSopenharmony_ci return 0; 6978c77b71bSopenharmony_ci } 6988c77b71bSopenharmony_ci if ((isStreamSource_ == true) && (speed != 1.0f)) { 6998c77b71bSopenharmony_ci MEDIA_ERR_LOG("Failed, streamsource not support abnormal speed"); 7008c77b71bSopenharmony_ci return -1; 7018c77b71bSopenharmony_ci } 7028c77b71bSopenharmony_ci 7038c77b71bSopenharmony_ci if (formatFileInfo_.s32UsedVideoStreamIndex == -1) { 7048c77b71bSopenharmony_ci MEDIA_ERR_LOG("audio movie not support abnormal speed"); 7058c77b71bSopenharmony_ci return -1; 7068c77b71bSopenharmony_ci } 7078c77b71bSopenharmony_ci 7088c77b71bSopenharmony_ci if (speed_ > 1 && speed == 1) { // tplay ---> play 7098c77b71bSopenharmony_ci CHECK_FAILED_PRINT(player_->Play(), 0, "player failed"); 7108c77b71bSopenharmony_ci } else { // normal/tplay -->tplay 7118c77b71bSopenharmony_ci TplayAttr tplayAttr; 7128c77b71bSopenharmony_ci TplayDirect direction = (speed > 0) ? TPLAY_DIRECT_FORWARD : TPLAY_DIRECT_BACKWARD; 7138c77b71bSopenharmony_ci float speedTmp = (speed > 0) ? speed : -speed; 7148c77b71bSopenharmony_ci tplayAttr.direction = direction; 7158c77b71bSopenharmony_ci tplayAttr.speed = speedTmp; 7168c77b71bSopenharmony_ci if (PlayerControlCheckTPlayAttr(speedTmp, direction) != 0) { 7178c77b71bSopenharmony_ci MEDIA_ERR_LOG("not support speed:%f", speed); 7188c77b71bSopenharmony_ci return -1; 7198c77b71bSopenharmony_ci } 7208c77b71bSopenharmony_ci CHECK_FAILED_PRINT(player_->TPlay(tplayAttr), 0, "tplayer failed"); 7218c77b71bSopenharmony_ci } 7228c77b71bSopenharmony_ci 7238c77b71bSopenharmony_ci if (speed == 0.f && currentState_ == PLAYER_STARTED) { 7248c77b71bSopenharmony_ci currentState_ = PLAYER_PAUSED; 7258c77b71bSopenharmony_ci } else if (speed != 0.f && (currentState_ == PLAYER_PREPARED || currentState_ == PLAYER_PAUSED)) { 7268c77b71bSopenharmony_ci currentState_ = PLAYER_STARTED; 7278c77b71bSopenharmony_ci } 7288c77b71bSopenharmony_ci speed_ = speed; 7298c77b71bSopenharmony_ci return 0; 7308c77b71bSopenharmony_ci} 7318c77b71bSopenharmony_ci 7328c77b71bSopenharmony_ciint32_t PlayerImpl::GetPlaybackSpeed(float &speed) 7338c77b71bSopenharmony_ci{ 7348c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 7358c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 7368c77b71bSopenharmony_ci speed = (currentState_ != PLAYER_PAUSED) ? speed_ : 0; 7378c77b71bSopenharmony_ci return 0; 7388c77b71bSopenharmony_ci} 7398c77b71bSopenharmony_ci 7408c77b71bSopenharmony_ciint32_t PlayerImpl::SetAudioStreamType(int32_t type) 7418c77b71bSopenharmony_ci{ 7428c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 7438c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 7448c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 7458c77b71bSopenharmony_ci if (currentState_ != PLAYER_IDLE && currentState_ != PLAYER_INITIALIZED) { 7468c77b71bSopenharmony_ci MEDIA_ERR_LOG("failed, state %u,type:%d", currentState_, type); 7478c77b71bSopenharmony_ci return -1; 7488c77b71bSopenharmony_ci } 7498c77b71bSopenharmony_ci audioStreamType_ = type; 7508c77b71bSopenharmony_ci 7518c77b71bSopenharmony_ci if (player_ != nullptr) { 7528c77b71bSopenharmony_ci player_->SetAudioStreamType(type); 7538c77b71bSopenharmony_ci } 7548c77b71bSopenharmony_ci return 0; 7558c77b71bSopenharmony_ci} 7568c77b71bSopenharmony_ci 7578c77b71bSopenharmony_civoid PlayerImpl::GetAudioStreamType(int32_t &type) 7588c77b71bSopenharmony_ci{ 7598c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 7608c77b71bSopenharmony_ci if (released_) { 7618c77b71bSopenharmony_ci MEDIA_ERR_LOG("have released or not create"); 7628c77b71bSopenharmony_ci return; 7638c77b71bSopenharmony_ci } 7648c77b71bSopenharmony_ci type = static_cast<int32_t>(audioStreamType_); 7658c77b71bSopenharmony_ci} 7668c77b71bSopenharmony_ci 7678c77b71bSopenharmony_civoid PlayerImpl::ResetInner(void) 7688c77b71bSopenharmony_ci{ 7698c77b71bSopenharmony_ci isSingleLoop_ = false; 7708c77b71bSopenharmony_ci if (player_ != nullptr) { 7718c77b71bSopenharmony_ci if (currentState_ != PLAYER_IDLE && currentState_ != PLAYER_STOPPED) { 7728c77b71bSopenharmony_ci CHECK_FAILED_PRINT(player_->Stop(), HI_SUCCESS, "stop failed"); 7738c77b71bSopenharmony_ci } 7748c77b71bSopenharmony_ci (void)player_->Deinit(); 7758c77b71bSopenharmony_ci player_.reset(); 7768c77b71bSopenharmony_ci player_ = nullptr; 7778c77b71bSopenharmony_ci } 7788c77b71bSopenharmony_ci if (bufferSource_ != nullptr) { 7798c77b71bSopenharmony_ci bufferSource_.reset(); 7808c77b71bSopenharmony_ci bufferSource_ = nullptr; 7818c77b71bSopenharmony_ci } 7828c77b71bSopenharmony_ci if (streamCallback_ != nullptr) { 7838c77b71bSopenharmony_ci streamCallback_.reset(); 7848c77b71bSopenharmony_ci streamCallback_ = nullptr; 7858c77b71bSopenharmony_ci } 7868c77b71bSopenharmony_ci 7878c77b71bSopenharmony_ci currentState_ = PLAYER_IDLE; 7888c77b71bSopenharmony_ci currentRewindMode_ = PLAYER_SEEK_PREVIOUS_SYNC; 7898c77b71bSopenharmony_ci rewindPosition_ = INVALID_MEDIA_POSITION; 7908c77b71bSopenharmony_ci rewindMode_ = PLAYER_SEEK_PREVIOUS_SYNC; 7918c77b71bSopenharmony_ci isSingleLoop_ = false; 7928c77b71bSopenharmony_ci speed_ = 1.0; 7938c77b71bSopenharmony_ci currentPosition_ = 0; 7948c77b71bSopenharmony_ci pauseAfterPlay_ = false; 7958c77b71bSopenharmony_ci extraRewind_ = false; 7968c77b71bSopenharmony_ci playerControlState_ = PLAY_STATUS_IDLE; 7978c77b71bSopenharmony_ci isStreamSource_ = false; 7988c77b71bSopenharmony_ci (void)memset_s(&formatFileInfo_, sizeof(FormatFileInfo), 0, sizeof(FormatFileInfo)); 7998c77b71bSopenharmony_ci formatFileInfo_.s32UsedVideoStreamIndex = -1; 8008c77b71bSopenharmony_ci formatFileInfo_.s32UsedAudioStreamIndex = -1; 8018c77b71bSopenharmony_ci formatFileInfo_.s64Duration = -1; 8028c77b71bSopenharmony_ci (void)memset_s(&mediaAttr_, sizeof(PlayerControlStreamAttr), 0, sizeof(PlayerControlStreamAttr)); 8038c77b71bSopenharmony_ci (void)memset_s(&buffer_, sizeof(QueBuffer), 0, sizeof(QueBuffer)); 8048c77b71bSopenharmony_ci buffer_.idx = -1; 8058c77b71bSopenharmony_ci} 8068c77b71bSopenharmony_ci 8078c77b71bSopenharmony_ciint32_t PlayerImpl::Reset(void) 8088c77b71bSopenharmony_ci{ 8098c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 8108c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 8118c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 8128c77b71bSopenharmony_ci if (currentState_ == PLAYER_IDLE) { 8138c77b71bSopenharmony_ci return 0; 8148c77b71bSopenharmony_ci } 8158c77b71bSopenharmony_ci ResetInner(); 8168c77b71bSopenharmony_ci return 0; 8178c77b71bSopenharmony_ci} 8188c77b71bSopenharmony_ci 8198c77b71bSopenharmony_ciint32_t PlayerImpl::Release() 8208c77b71bSopenharmony_ci{ 8218c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 8228c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 8238c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, 0, "have released or not create"); 8248c77b71bSopenharmony_ci ResetInner(); 8258c77b71bSopenharmony_ci 8268c77b71bSopenharmony_ci if (callback_ != nullptr) { 8278c77b71bSopenharmony_ci callback_.reset(); 8288c77b71bSopenharmony_ci callback_ = nullptr; 8298c77b71bSopenharmony_ci } 8308c77b71bSopenharmony_ci currentState_ = PLAYER_STATE_ERROR; 8318c77b71bSopenharmony_ci released_ = true; 8328c77b71bSopenharmony_ci return 0; 8338c77b71bSopenharmony_ci} 8348c77b71bSopenharmony_ci 8358c77b71bSopenharmony_ciint PlayerImpl::CreatePlayerParamCheck(PlayerControlParam &createParam) 8368c77b71bSopenharmony_ci{ 8378c77b71bSopenharmony_ci if (createParam.u32PlayPosNotifyIntervalMs < MIN_NOTIFY_INTERVAL_MS 8388c77b71bSopenharmony_ci && createParam.u32PlayPosNotifyIntervalMs > 0) { 8398c77b71bSopenharmony_ci MEDIA_ERR_LOG("notify interval small than min value %d", MIN_NOTIFY_INTERVAL_MS); 8408c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM; 8418c77b71bSopenharmony_ci } 8428c77b71bSopenharmony_ci if ((createParam.u32VideoEsBufSize < AV_ESBUF_SIZE_MIN && createParam.u32VideoEsBufSize > 0) 8438c77b71bSopenharmony_ci || createParam.u32VideoEsBufSize > VIDEO_ESBUF_SIZE_LIMIT) { 8448c77b71bSopenharmony_ci MEDIA_ERR_LOG("video esbuffer illegal %u", createParam.u32VideoEsBufSize); 8458c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM; 8468c77b71bSopenharmony_ci } 8478c77b71bSopenharmony_ci if ((createParam.u32AudioEsBufSize < AV_ESBUF_SIZE_MIN && createParam.u32AudioEsBufSize > 0) 8488c77b71bSopenharmony_ci || createParam.u32AudioEsBufSize > AUDIO_ESBUF_SIZE_LIMIT) { 8498c77b71bSopenharmony_ci MEDIA_ERR_LOG("audio esbuffer illegal %u", createParam.u32VideoEsBufSize); 8508c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM; 8518c77b71bSopenharmony_ci } 8528c77b71bSopenharmony_ci if ((createParam.u32VdecFrameBufCnt < VDEC_VBBUF_CONUT_MIN) && 8538c77b71bSopenharmony_ci (createParam.u32VdecFrameBufCnt != 0)) { 8548c77b71bSopenharmony_ci MEDIA_ERR_LOG("VDEC vb buffer count %u small than %d", createParam.u32VdecFrameBufCnt, VDEC_VBBUF_CONUT_MIN); 8558c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM; 8568c77b71bSopenharmony_ci } 8578c77b71bSopenharmony_ci return 0; 8588c77b71bSopenharmony_ci} 8598c77b71bSopenharmony_ci 8608c77b71bSopenharmony_ciint PlayerImpl::GetPlayer() 8618c77b71bSopenharmony_ci{ 8628c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 8638c77b71bSopenharmony_ci PlayerControlParam playerParam; 8648c77b71bSopenharmony_ci if (player_ != nullptr) { 8658c77b71bSopenharmony_ci return 0; 8668c77b71bSopenharmony_ci } 8678c77b71bSopenharmony_ci if (memset_s(&playerParam, sizeof(PlayerControlParam), 0x00, sizeof(playerParam)) != EOK) { 8688c77b71bSopenharmony_ci MEDIA_INFO_LOG("memset_s playerParam failed"); 8698c77b71bSopenharmony_ci return -1; 8708c77b71bSopenharmony_ci } 8718c77b71bSopenharmony_ci 8728c77b71bSopenharmony_ci playerParam.u32PlayPosNotifyIntervalMs = POS_NOTIFY_INTERVAL_MS; 8738c77b71bSopenharmony_ci if (CreatePlayerParamCheck(playerParam) != 0) { 8748c77b71bSopenharmony_ci MEDIA_ERR_LOG("CreatePlayerParamCheck failed"); 8758c77b71bSopenharmony_ci return -1; 8768c77b71bSopenharmony_ci } 8778c77b71bSopenharmony_ci player_ = std::make_shared<PlayerControl>(); 8788c77b71bSopenharmony_ci if (player_ == nullptr || player_.get() == nullptr) { 8798c77b71bSopenharmony_ci MEDIA_ERR_LOG("playerControl new failed"); 8808c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_MEM_MALLOC; 8818c77b71bSopenharmony_ci } 8828c77b71bSopenharmony_ci if (player_->Init(playerParam) != HI_SUCCESS) { 8838c77b71bSopenharmony_ci MEDIA_ERR_LOG("playerControl init failed"); 8848c77b71bSopenharmony_ci return HI_ERR_PLAYERCONTROL_MEM_MALLOC; 8858c77b71bSopenharmony_ci } 8868c77b71bSopenharmony_ci currentState_ = PLAYER_INITIALIZED; 8878c77b71bSopenharmony_ci MEDIA_INFO_LOG("GetPlayer success"); 8888c77b71bSopenharmony_ci return 0; 8898c77b71bSopenharmony_ci} 8908c77b71bSopenharmony_ci 8918c77b71bSopenharmony_civoid PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb) 8928c77b71bSopenharmony_ci{ 8938c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 8948c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 8958c77b71bSopenharmony_ci if (released_) { 8968c77b71bSopenharmony_ci MEDIA_ERR_LOG("have released or not create"); 8978c77b71bSopenharmony_ci return; 8988c77b71bSopenharmony_ci } 8998c77b71bSopenharmony_ci callback_ = cb; 9008c77b71bSopenharmony_ci} 9018c77b71bSopenharmony_ci 9028c77b71bSopenharmony_civoid PlayerImpl::NotifyPlaybackComplete(PlayerImpl *curPlayer) 9038c77b71bSopenharmony_ci{ 9048c77b71bSopenharmony_ci if (curPlayer == nullptr) { 9058c77b71bSopenharmony_ci MEDIA_ERR_LOG("curPlayer is nullptr"); 9068c77b71bSopenharmony_ci return; 9078c77b71bSopenharmony_ci } 9088c77b71bSopenharmony_ci if (!isSingleLoop_) { 9098c77b71bSopenharmony_ci if (curPlayer->formatFileInfo_.s64Duration == -1) { 9108c77b71bSopenharmony_ci curPlayer->formatFileInfo_.s64Duration = curPlayer->currentPosition_; 9118c77b71bSopenharmony_ci } 9128c77b71bSopenharmony_ci curPlayer->currentState_ = PLAYER_PLAYBACK_COMPLETE; 9138c77b71bSopenharmony_ci MEDIA_INFO_LOG("OnPlayBackComplete, iscallbackNull:%d", (curPlayer->callback_ == nullptr)); 9148c77b71bSopenharmony_ci if (curPlayer->callback_ != nullptr) { 9158c77b71bSopenharmony_ci curPlayer->callback_->OnPlaybackComplete(); 9168c77b71bSopenharmony_ci } 9178c77b71bSopenharmony_ci return; 9188c77b71bSopenharmony_ci } 9198c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(curPlayer->rewindLock_); 9208c77b71bSopenharmony_ci (void)curPlayer->RewindInner(0, PLAYER_SEEK_PREVIOUS_SYNC); 9218c77b71bSopenharmony_ci curPlayer->currentPosition_ = 0; 9228c77b71bSopenharmony_ci curPlayer->extraRewind_ = false; 9238c77b71bSopenharmony_ci} 9248c77b71bSopenharmony_ci 9258c77b71bSopenharmony_civoid PlayerImpl::NotifySeekComplete(PlayerImpl *curPlayer, int64_t seekToMs) 9268c77b71bSopenharmony_ci{ 9278c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(curPlayer->rewindLock_); 9288c77b71bSopenharmony_ci 9298c77b71bSopenharmony_ci if (curPlayer->rewindPosition_ != -1 && curPlayer->rewindPosition_ != seekToMs) { 9308c77b71bSopenharmony_ci int64_t seekTime = curPlayer->rewindPosition_; 9318c77b71bSopenharmony_ci curPlayer->rewindMode_ = PLAYER_SEEK_PREVIOUS_SYNC; 9328c77b71bSopenharmony_ci curPlayer->rewindPosition_ = -1; 9338c77b71bSopenharmony_ci curPlayer->RewindInner(seekTime, curPlayer->currentRewindMode_); 9348c77b71bSopenharmony_ci return; 9358c77b71bSopenharmony_ci } 9368c77b71bSopenharmony_ci 9378c77b71bSopenharmony_ci curPlayer->currentRewindMode_ = curPlayer->rewindMode_ = PLAYER_SEEK_PREVIOUS_SYNC; 9388c77b71bSopenharmony_ci curPlayer->rewindPosition_ = -1; 9398c77b71bSopenharmony_ci if (curPlayer->callback_ != nullptr && extraRewind_) { 9408c77b71bSopenharmony_ci extraRewind_ = false; 9418c77b71bSopenharmony_ci curPlayer->callback_->OnRewindToComplete(); 9428c77b71bSopenharmony_ci } 9438c77b71bSopenharmony_ci} 9448c77b71bSopenharmony_ci 9458c77b71bSopenharmony_ciint32_t PlayerImpl::SetLoop(bool loop) 9468c77b71bSopenharmony_ci{ 9478c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 9488c77b71bSopenharmony_ci CHECK_FAILED_RETURN(released_, false, -1, "have released or not create"); 9498c77b71bSopenharmony_ci CHECK_FAILED_RETURN(isStreamSource_, false, -1, "stream source not support loop player"); 9508c77b71bSopenharmony_ci if (currentState_ == PLAYER_STOPPED || currentState_ == PLAYER_PLAYBACK_COMPLETE || currentState_ == PLAYER_IDLE) { 9518c77b71bSopenharmony_ci MEDIA_ERR_LOG("currentState_ is %u", currentState_); 9528c77b71bSopenharmony_ci return -1; 9538c77b71bSopenharmony_ci } 9548c77b71bSopenharmony_ci isSingleLoop_ = loop; 9558c77b71bSopenharmony_ci return 0; 9568c77b71bSopenharmony_ci} 9578c77b71bSopenharmony_ci 9588c77b71bSopenharmony_ciint32_t PlayerImpl::SetUriSource(const Source &source) 9598c77b71bSopenharmony_ci{ 9608c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 9618c77b71bSopenharmony_ci const std::string uri = source.GetSourceUri(); 9628c77b71bSopenharmony_ci if (uri.empty() || uri.c_str() == nullptr) { 9638c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetUriSource failed, uri source do not set uri parameter"); 9648c77b71bSopenharmony_ci return -1; 9658c77b71bSopenharmony_ci } 9668c77b71bSopenharmony_ci char filePath[PATH_MAX]; 9678c77b71bSopenharmony_ci if (realpath(uri.c_str(), filePath) == nullptr) { 9688c77b71bSopenharmony_ci MEDIA_ERR_LOG("Realpath input file failed"); 9698c77b71bSopenharmony_ci return -1; 9708c77b71bSopenharmony_ci } 9718c77b71bSopenharmony_ci if (access(filePath, R_OK) == -1) { 9728c77b71bSopenharmony_ci MEDIA_ERR_LOG("No permission to read the file"); 9738c77b71bSopenharmony_ci return -1; 9748c77b71bSopenharmony_ci } 9758c77b71bSopenharmony_ci int32_t ret = player_->SetDataSource(uri.c_str()); 9768c77b71bSopenharmony_ci if (ret != 0) { 9778c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetSource failed, ret is %d, uri is %s", ret, uri.c_str()); 9788c77b71bSopenharmony_ci return ret; 9798c77b71bSopenharmony_ci } 9808c77b71bSopenharmony_ci return 0; 9818c77b71bSopenharmony_ci} 9828c77b71bSopenharmony_ci 9838c77b71bSopenharmony_ciAdapterStreamCallback::AdapterStreamCallback(std::shared_ptr<StreamSource> &stream, 9848c77b71bSopenharmony_ci std::shared_ptr<BufferSource> &buffer) 9858c77b71bSopenharmony_ci : streamProcess_(0), 9868c77b71bSopenharmony_ci isRunning_(false) 9878c77b71bSopenharmony_ci{ 9888c77b71bSopenharmony_ci streamSource_ = stream; 9898c77b71bSopenharmony_ci bufferSource_ = buffer; 9908c77b71bSopenharmony_ci pthread_mutex_init(&mutex_, nullptr); 9918c77b71bSopenharmony_ci} 9928c77b71bSopenharmony_ci 9938c77b71bSopenharmony_ciAdapterStreamCallback::~AdapterStreamCallback(void) 9948c77b71bSopenharmony_ci{ 9958c77b71bSopenharmony_ci DeInit(); 9968c77b71bSopenharmony_ci MEDIA_INFO_LOG("process out"); 9978c77b71bSopenharmony_ci} 9988c77b71bSopenharmony_ci 9998c77b71bSopenharmony_civoid *AdapterStreamCallback::IdleBufferProcess(void *arg) 10008c77b71bSopenharmony_ci{ 10018c77b71bSopenharmony_ci int ret; 10028c77b71bSopenharmony_ci QueBuffer buffer; 10038c77b71bSopenharmony_ci BufferInfo info; 10048c77b71bSopenharmony_ci if (memset_s(&info, sizeof(info), 0x00, sizeof(info)) != EOK) { 10058c77b71bSopenharmony_ci return nullptr; 10068c77b71bSopenharmony_ci } 10078c77b71bSopenharmony_ci AdapterStreamCallback *process = (AdapterStreamCallback*)arg; 10088c77b71bSopenharmony_ci if (process == nullptr) { 10098c77b71bSopenharmony_ci return nullptr; 10108c77b71bSopenharmony_ci } 10118c77b71bSopenharmony_ci 10128c77b71bSopenharmony_ci prctl(PR_SET_NAME, "IdlbufProc", 0, 0, 0); 10138c77b71bSopenharmony_ci MEDIA_INFO_LOG("process start"); 10148c77b71bSopenharmony_ci while (true) { 10158c77b71bSopenharmony_ci pthread_mutex_lock(&process->mutex_); 10168c77b71bSopenharmony_ci if (process->isRunning_ == false) { 10178c77b71bSopenharmony_ci pthread_mutex_unlock(&process->mutex_); 10188c77b71bSopenharmony_ci break; 10198c77b71bSopenharmony_ci } 10208c77b71bSopenharmony_ci pthread_mutex_unlock(&process->mutex_); 10218c77b71bSopenharmony_ci if (process->bufferSource_ == nullptr) { 10228c77b71bSopenharmony_ci MEDIA_ERR_LOG("bufferSource_ null break"); 10238c77b71bSopenharmony_ci break; 10248c77b71bSopenharmony_ci } 10258c77b71bSopenharmony_ci if (process->bufferSource_->GetIdleQueSize() == 0) { 10268c77b71bSopenharmony_ci usleep(IDLE_QUEQUE_SLEEP_TIME_US); 10278c77b71bSopenharmony_ci continue; 10288c77b71bSopenharmony_ci } 10298c77b71bSopenharmony_ci ret = process->bufferSource_->DequeIdleBuffer(&buffer, 0); 10308c77b71bSopenharmony_ci if (ret == 0) { 10318c77b71bSopenharmony_ci process->bufferSource_->GetBufferInfo(buffer.idx, &info); 10328c77b71bSopenharmony_ci std::shared_ptr<StreamSource> stream = process->streamSource_.lock(); 10338c77b71bSopenharmony_ci if (stream == nullptr) { 10348c77b71bSopenharmony_ci MEDIA_ERR_LOG("stream not exist break"); 10358c77b71bSopenharmony_ci break; 10368c77b71bSopenharmony_ci } 10378c77b71bSopenharmony_ci stream->OnBufferAvailable(buffer.idx, 0, info.bufLen); 10388c77b71bSopenharmony_ci } 10398c77b71bSopenharmony_ci }; 10408c77b71bSopenharmony_ci pthread_mutex_lock(&process->mutex_); 10418c77b71bSopenharmony_ci process->isRunning_ = false; 10428c77b71bSopenharmony_ci pthread_mutex_unlock(&process->mutex_); 10438c77b71bSopenharmony_ci MEDIA_INFO_LOG("work end"); 10448c77b71bSopenharmony_ci return nullptr; 10458c77b71bSopenharmony_ci} 10468c77b71bSopenharmony_ci 10478c77b71bSopenharmony_ciint AdapterStreamCallback::Init(void) 10488c77b71bSopenharmony_ci{ 10498c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 10508c77b71bSopenharmony_ci pthread_mutex_lock(&mutex_); 10518c77b71bSopenharmony_ci isRunning_ = true; 10528c77b71bSopenharmony_ci pthread_mutex_unlock(&mutex_); 10538c77b71bSopenharmony_ci int32_t ret = pthread_create(&streamProcess_, nullptr, IdleBufferProcess, this); 10548c77b71bSopenharmony_ci if (ret != 0) { 10558c77b71bSopenharmony_ci MEDIA_ERR_LOG("pthread_create failed %d", ret); 10568c77b71bSopenharmony_ci pthread_mutex_lock(&mutex_); 10578c77b71bSopenharmony_ci isRunning_ = false; 10588c77b71bSopenharmony_ci pthread_mutex_unlock(&mutex_); 10598c77b71bSopenharmony_ci return -1; 10608c77b71bSopenharmony_ci } 10618c77b71bSopenharmony_ci return 0; 10628c77b71bSopenharmony_ci} 10638c77b71bSopenharmony_ci 10648c77b71bSopenharmony_civoid AdapterStreamCallback::DeInit(void) 10658c77b71bSopenharmony_ci{ 10668c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 10678c77b71bSopenharmony_ci pthread_mutex_lock(&mutex_); 10688c77b71bSopenharmony_ci isRunning_ = false; 10698c77b71bSopenharmony_ci pthread_mutex_unlock(&mutex_); 10708c77b71bSopenharmony_ci if (streamProcess_ != 0) { 10718c77b71bSopenharmony_ci pthread_join(streamProcess_, nullptr); 10728c77b71bSopenharmony_ci } 10738c77b71bSopenharmony_ci pthread_mutex_destroy(&mutex_); 10748c77b71bSopenharmony_ci} 10758c77b71bSopenharmony_ci 10768c77b71bSopenharmony_ciuint8_t *AdapterStreamCallback::GetBuffer(size_t index) 10778c77b71bSopenharmony_ci{ 10788c77b71bSopenharmony_ci BufferInfo info; 10798c77b71bSopenharmony_ci if (bufferSource_ == nullptr) { 10808c77b71bSopenharmony_ci MEDIA_ERR_LOG("bufferSource null"); 10818c77b71bSopenharmony_ci return nullptr; 10828c77b71bSopenharmony_ci } 10838c77b71bSopenharmony_ci if (bufferSource_->GetBufferInfo(index, &info) != 0) { 10848c77b71bSopenharmony_ci MEDIA_ERR_LOG("GetBufferInfo failed"); 10858c77b71bSopenharmony_ci return nullptr; 10868c77b71bSopenharmony_ci } 10878c77b71bSopenharmony_ci return (uint8_t*)info.virAddr; 10888c77b71bSopenharmony_ci} 10898c77b71bSopenharmony_ci 10908c77b71bSopenharmony_civoid AdapterStreamCallback::QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags) 10918c77b71bSopenharmony_ci{ 10928c77b71bSopenharmony_ci QueBuffer buffer; 10938c77b71bSopenharmony_ci if (bufferSource_ == nullptr) { 10948c77b71bSopenharmony_ci MEDIA_ERR_LOG("bufferSource null"); 10958c77b71bSopenharmony_ci return; 10968c77b71bSopenharmony_ci } 10978c77b71bSopenharmony_ci 10988c77b71bSopenharmony_ci buffer.idx = index; 10998c77b71bSopenharmony_ci buffer.flag = static_cast<int32_t>(flags); 11008c77b71bSopenharmony_ci buffer.offset = offset; 11018c77b71bSopenharmony_ci buffer.size = size; 11028c77b71bSopenharmony_ci buffer.timestamp = timestampUs; 11038c77b71bSopenharmony_ci if (bufferSource_->QueFilledBuffer(&buffer) != 0) { 11048c77b71bSopenharmony_ci MEDIA_ERR_LOG("QueFilledBuffer failed"); 11058c77b71bSopenharmony_ci } 11068c77b71bSopenharmony_ci} 11078c77b71bSopenharmony_ci 11088c77b71bSopenharmony_civoid AdapterStreamCallback::SetParameters(const Format ¶ms) 11098c77b71bSopenharmony_ci{ 11108c77b71bSopenharmony_ci MEDIA_ERR_LOG("process, not support"); 11118c77b71bSopenharmony_ci} 11128c77b71bSopenharmony_ci 11138c77b71bSopenharmony_ciint32_t PlayerImpl::GetReadableSize(const void *handle) 11148c77b71bSopenharmony_ci{ 11158c77b71bSopenharmony_ci const PlayerImpl *playImpl = (const PlayerImpl*)handle; 11168c77b71bSopenharmony_ci CHK_NULL_RETURN(playImpl); 11178c77b71bSopenharmony_ci if (playImpl->bufferSource_ == nullptr) { 11188c77b71bSopenharmony_ci MEDIA_ERR_LOG("bufferSource null"); 11198c77b71bSopenharmony_ci return -1; 11208c77b71bSopenharmony_ci } 11218c77b71bSopenharmony_ci return playImpl->bufferSource_->GetFilledQueDataSize(); 11228c77b71bSopenharmony_ci} 11238c77b71bSopenharmony_ci 11248c77b71bSopenharmony_ciint32_t PlayerImpl::ReadDataPro(uint8_t *data, int32_t size, DataFlags &flags) 11258c77b71bSopenharmony_ci{ 11268c77b71bSopenharmony_ci int readLen; 11278c77b71bSopenharmony_ci BufferInfo info; 11288c77b71bSopenharmony_ci if (bufferSource_->GetBufferInfo(buffer_.idx, &info) != 0) { 11298c77b71bSopenharmony_ci return 0; 11308c77b71bSopenharmony_ci } 11318c77b71bSopenharmony_ci /* read all buffer data */ 11328c77b71bSopenharmony_ci if (buffer_.size <= size) { 11338c77b71bSopenharmony_ci if (buffer_.size == 0 && buffer_.flag == BUFFER_FLAG_EOS) { 11348c77b71bSopenharmony_ci buffer_.offset = 0; 11358c77b71bSopenharmony_ci buffer_.size = info.size; 11368c77b71bSopenharmony_ci bufferSource_->QueIdleBuffer(&buffer_); 11378c77b71bSopenharmony_ci buffer_.idx = -1; 11388c77b71bSopenharmony_ci flags = DATA_FLAG_EOS; 11398c77b71bSopenharmony_ci return 0; 11408c77b71bSopenharmony_ci } 11418c77b71bSopenharmony_ci if (memcpy_s(data, size, (unsigned char*)(info.virAddr) + buffer_.offset, buffer_.size) != EOK) { 11428c77b71bSopenharmony_ci return -1; 11438c77b71bSopenharmony_ci } 11448c77b71bSopenharmony_ci flags = (buffer_.flag == BUFFER_FLAG_EOS) ? DATA_FLAG_EOS : DATA_FLAG_PARTIAL_FRAME; 11458c77b71bSopenharmony_ci readLen = buffer_.size; 11468c77b71bSopenharmony_ci buffer_.offset = 0; 11478c77b71bSopenharmony_ci buffer_.size = info.size; 11488c77b71bSopenharmony_ci bufferSource_->QueIdleBuffer(&buffer_); 11498c77b71bSopenharmony_ci buffer_.idx = -1; 11508c77b71bSopenharmony_ci } else { 11518c77b71bSopenharmony_ci if (memcpy_s(data, size, (unsigned char*)(info.virAddr) + buffer_.offset, size) != EOK) { 11528c77b71bSopenharmony_ci return -1; 11538c77b71bSopenharmony_ci } 11548c77b71bSopenharmony_ci buffer_.offset += size; 11558c77b71bSopenharmony_ci buffer_.size -= size; 11568c77b71bSopenharmony_ci flags = DATA_FLAG_PARTIAL_FRAME; 11578c77b71bSopenharmony_ci readLen = size; 11588c77b71bSopenharmony_ci } 11598c77b71bSopenharmony_ci return readLen; 11608c77b71bSopenharmony_ci} 11618c77b71bSopenharmony_ci 11628c77b71bSopenharmony_ciint32_t PlayerImpl::ReadData(void *handle, uint8_t *data, int32_t size, int32_t timeOutMs, DataFlags *flags) 11638c77b71bSopenharmony_ci{ 11648c77b71bSopenharmony_ci PlayerImpl *playImpl = (PlayerImpl*)handle; 11658c77b71bSopenharmony_ci 11668c77b71bSopenharmony_ci if (playImpl == nullptr || playImpl->bufferSource_ == nullptr) { 11678c77b71bSopenharmony_ci MEDIA_ERR_LOG("bufferSource null"); 11688c77b71bSopenharmony_ci return -1; 11698c77b71bSopenharmony_ci } 11708c77b71bSopenharmony_ci if (data == nullptr || size < 0 || flags == nullptr) { 11718c77b71bSopenharmony_ci MEDIA_ERR_LOG("data null or buffer size < 0"); 11728c77b71bSopenharmony_ci return -1; 11738c77b71bSopenharmony_ci } 11748c77b71bSopenharmony_ci 11758c77b71bSopenharmony_ci if (playImpl->buffer_.idx != -1) { 11768c77b71bSopenharmony_ci return playImpl->ReadDataPro(data, size, *flags); 11778c77b71bSopenharmony_ci } 11788c77b71bSopenharmony_ci 11798c77b71bSopenharmony_ci if (playImpl->bufferSource_->GetFilledQueSize() <= 0) { 11808c77b71bSopenharmony_ci return 0; 11818c77b71bSopenharmony_ci } 11828c77b71bSopenharmony_ci if (playImpl->bufferSource_->DequeFilledBuffer(&playImpl->buffer_, 0) != 0) { 11838c77b71bSopenharmony_ci playImpl->buffer_.idx = -1; 11848c77b71bSopenharmony_ci return 0; 11858c77b71bSopenharmony_ci } 11868c77b71bSopenharmony_ci 11878c77b71bSopenharmony_ci return playImpl->ReadDataPro(data, size, *flags); 11888c77b71bSopenharmony_ci} 11898c77b71bSopenharmony_ci 11908c77b71bSopenharmony_ciint32_t PlayerImpl::SetStreamSource(const Source &source) 11918c77b71bSopenharmony_ci{ 11928c77b71bSopenharmony_ci MEDIA_INFO_LOG("process in"); 11938c77b71bSopenharmony_ci 11948c77b71bSopenharmony_ci isStreamSource_ = true; 11958c77b71bSopenharmony_ci isSingleLoop_ = false; 11968c77b71bSopenharmony_ci 11978c77b71bSopenharmony_ci bufferSource_ = std::make_shared<BufferSource>(); 11988c77b71bSopenharmony_ci if (bufferSource_ == nullptr) { 11998c77b71bSopenharmony_ci MEDIA_ERR_LOG("new BufferSource failed"); 12008c77b71bSopenharmony_ci return -1; 12018c77b71bSopenharmony_ci } 12028c77b71bSopenharmony_ci 12038c77b71bSopenharmony_ci bufferSource_->Init(); 12048c77b71bSopenharmony_ci std::shared_ptr<StreamSource> stream = source.GetSourceStream(); 12058c77b71bSopenharmony_ci if (stream.get() == nullptr) { 12068c77b71bSopenharmony_ci MEDIA_ERR_LOG("GetSourceStream null"); 12078c77b71bSopenharmony_ci return -1; 12088c77b71bSopenharmony_ci } 12098c77b71bSopenharmony_ci 12108c77b71bSopenharmony_ci streamCallback_ = std::make_shared<AdapterStreamCallback>(stream, bufferSource_); 12118c77b71bSopenharmony_ci if (streamCallback_ == nullptr || streamCallback_.get() == nullptr) { 12128c77b71bSopenharmony_ci MEDIA_ERR_LOG("new AdapterStreamCallback failed"); 12138c77b71bSopenharmony_ci return -1; 12148c77b71bSopenharmony_ci } 12158c77b71bSopenharmony_ci streamCallback_->Init(); 12168c77b71bSopenharmony_ci stream->SetStreamCallback(streamCallback_); 12178c77b71bSopenharmony_ci 12188c77b71bSopenharmony_ci BufferStream sourceTmp; 12198c77b71bSopenharmony_ci sourceTmp.handle = this; 12208c77b71bSopenharmony_ci sourceTmp.ReadData = ReadData; 12218c77b71bSopenharmony_ci sourceTmp.GetReadableSize = GetReadableSize; 12228c77b71bSopenharmony_ci int32_t ret = player_->SetDataSource(sourceTmp); 12238c77b71bSopenharmony_ci if (ret != 0) { 12248c77b71bSopenharmony_ci MEDIA_ERR_LOG("SetDataSource exec failed"); 12258c77b71bSopenharmony_ci return -1; 12268c77b71bSopenharmony_ci } 12278c77b71bSopenharmony_ci return 0; 12288c77b71bSopenharmony_ci} 12298c77b71bSopenharmony_ci 12308c77b71bSopenharmony_ciint32_t PlayerImpl::EnablePauseAfterPlay(bool isPauseAfterPlay) 12318c77b71bSopenharmony_ci{ 12328c77b71bSopenharmony_ci if (currentState_ != PLAYER_IDLE && currentState_ != PLAYER_INITIALIZED) { 12338c77b71bSopenharmony_ci MEDIA_ERR_LOG("currentState_ is %u", currentState_); 12348c77b71bSopenharmony_ci return -1; 12358c77b71bSopenharmony_ci } 12368c77b71bSopenharmony_ci if (pauseAfterPlay_ == isPauseAfterPlay) { 12378c77b71bSopenharmony_ci return 0; 12388c77b71bSopenharmony_ci } 12398c77b71bSopenharmony_ci 12408c77b71bSopenharmony_ci pauseAfterPlay_ = isPauseAfterPlay; 12418c77b71bSopenharmony_ci if (player_ != NULL) { 12428c77b71bSopenharmony_ci return player_->Invoke(INVOKE_ENABLE_PAUSE_AFTER_PLAYER, &pauseAfterPlay_); 12438c77b71bSopenharmony_ci } 12448c77b71bSopenharmony_ci MEDIA_INFO_LOG("isPauseAfterPlay:%d", isPauseAfterPlay); 12458c77b71bSopenharmony_ci return 0; 12468c77b71bSopenharmony_ci} 12478c77b71bSopenharmony_ci 12488c77b71bSopenharmony_ciint32_t PlayerImpl::SetParameter(const Format ¶ms) 12498c77b71bSopenharmony_ci{ 12508c77b71bSopenharmony_ci int32_t value; 12518c77b71bSopenharmony_ci std::lock_guard<std::mutex> valueLock(lock_); 12528c77b71bSopenharmony_ci 12538c77b71bSopenharmony_ci if (params.GetIntValue(PAUSE_AFTER_PLAY, value) != true) { 12548c77b71bSopenharmony_ci MEDIA_ERR_LOG("get pause after play failed"); 12558c77b71bSopenharmony_ci return -1; 12568c77b71bSopenharmony_ci } 12578c77b71bSopenharmony_ci if (value != 0 && value != 1) { 12588c77b71bSopenharmony_ci MEDIA_ERR_LOG("pause after play flag error:%d", value); 12598c77b71bSopenharmony_ci return -1; 12608c77b71bSopenharmony_ci } 12618c77b71bSopenharmony_ci 12628c77b71bSopenharmony_ci return EnablePauseAfterPlay(value); 12638c77b71bSopenharmony_ci} 12648c77b71bSopenharmony_ci} // namespace Media 12658c77b71bSopenharmony_ci} // namespace OHOS