1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 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 16fa7767c5Sopenharmony_ci#include "media_utils.h" 17fa7767c5Sopenharmony_ci#include <cmath> 18fa7767c5Sopenharmony_ci#include <media_errors.h> 19fa7767c5Sopenharmony_ci 20fa7767c5Sopenharmony_cinamespace OHOS { 21fa7767c5Sopenharmony_cinamespace Media { 22fa7767c5Sopenharmony_cinamespace { 23fa7767c5Sopenharmony_cistruct ErrorCodePair { 24fa7767c5Sopenharmony_ci ErrorCode errorCode; 25fa7767c5Sopenharmony_ci int serviceErrCode; 26fa7767c5Sopenharmony_ci}; 27fa7767c5Sopenharmony_ciconst ErrorCodePair g_errorCodePair[] = { 28fa7767c5Sopenharmony_ci {ErrorCode::SUCCESS, MSERR_OK}, 29fa7767c5Sopenharmony_ci {ErrorCode::ERROR_UNKNOWN, MSERR_UNKNOWN}, 30fa7767c5Sopenharmony_ci {ErrorCode::ERROR_AGAIN, MSERR_UNKNOWN}, 31fa7767c5Sopenharmony_ci {ErrorCode::ERROR_UNIMPLEMENTED, MSERR_UNSUPPORT}, 32fa7767c5Sopenharmony_ci {ErrorCode::ERROR_INVALID_PARAMETER_VALUE, MSERR_INVALID_VAL}, 33fa7767c5Sopenharmony_ci {ErrorCode::ERROR_INVALID_PARAMETER_TYPE, MSERR_INVALID_VAL}, 34fa7767c5Sopenharmony_ci {ErrorCode::ERROR_INVALID_OPERATION, MSERR_INVALID_OPERATION}, 35fa7767c5Sopenharmony_ci {ErrorCode::ERROR_UNSUPPORTED_FORMAT, MSERR_UNSUPPORT_CONTAINER_TYPE}, 36fa7767c5Sopenharmony_ci {ErrorCode::ERROR_NOT_EXISTED, MSERR_OPEN_FILE_FAILED}, 37fa7767c5Sopenharmony_ci {ErrorCode::ERROR_TIMED_OUT, MSERR_EXT_TIMEOUT}, 38fa7767c5Sopenharmony_ci {ErrorCode::ERROR_NO_MEMORY, MSERR_EXT_NO_MEMORY}, 39fa7767c5Sopenharmony_ci {ErrorCode::ERROR_INVALID_STATE, MSERR_INVALID_STATE}, 40fa7767c5Sopenharmony_ci}; 41fa7767c5Sopenharmony_ciconst std::array<std::pair<PlaybackRateMode, float>, 5> PLAY_RATE_REFS = { 42fa7767c5Sopenharmony_ci std::make_pair(PlaybackRateMode::SPEED_FORWARD_0_75_X, 0.75), 43fa7767c5Sopenharmony_ci std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_00_X, 1.0), 44fa7767c5Sopenharmony_ci std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_25_X, 1.25), 45fa7767c5Sopenharmony_ci std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_75_X, 1.75), 46fa7767c5Sopenharmony_ci std::make_pair(PlaybackRateMode::SPEED_FORWARD_2_00_X, 2.00), 47fa7767c5Sopenharmony_ci}; 48fa7767c5Sopenharmony_ci} // namespace 49fa7767c5Sopenharmony_ci 50fa7767c5Sopenharmony_ciint TransErrorCode(ErrorCode errorCode) 51fa7767c5Sopenharmony_ci{ 52fa7767c5Sopenharmony_ci for (const auto& errPair : g_errorCodePair) { 53fa7767c5Sopenharmony_ci if (errPair.errorCode == errorCode) { 54fa7767c5Sopenharmony_ci return errPair.serviceErrCode; 55fa7767c5Sopenharmony_ci } 56fa7767c5Sopenharmony_ci } 57fa7767c5Sopenharmony_ci return MSERR_UNKNOWN; 58fa7767c5Sopenharmony_ci} 59fa7767c5Sopenharmony_ci 60fa7767c5Sopenharmony_ciPlayerStates TransStateId2PlayerState(StateId state) 61fa7767c5Sopenharmony_ci{ 62fa7767c5Sopenharmony_ci PlayerStates playerState = PLAYER_STATE_ERROR; 63fa7767c5Sopenharmony_ci switch (state) { 64fa7767c5Sopenharmony_ci case StateId::IDLE: 65fa7767c5Sopenharmony_ci playerState = PLAYER_IDLE; 66fa7767c5Sopenharmony_ci break; 67fa7767c5Sopenharmony_ci case StateId::INIT: 68fa7767c5Sopenharmony_ci playerState = PLAYER_INITIALIZED; 69fa7767c5Sopenharmony_ci break; 70fa7767c5Sopenharmony_ci case StateId::PREPARING: 71fa7767c5Sopenharmony_ci playerState = PLAYER_PREPARING; 72fa7767c5Sopenharmony_ci break; 73fa7767c5Sopenharmony_ci case StateId::READY: 74fa7767c5Sopenharmony_ci playerState = PLAYER_PREPARED; 75fa7767c5Sopenharmony_ci break; 76fa7767c5Sopenharmony_ci case StateId::PAUSE: 77fa7767c5Sopenharmony_ci playerState = PLAYER_PAUSED; 78fa7767c5Sopenharmony_ci break; 79fa7767c5Sopenharmony_ci case StateId::PLAYING: 80fa7767c5Sopenharmony_ci playerState = PLAYER_STARTED; 81fa7767c5Sopenharmony_ci break; 82fa7767c5Sopenharmony_ci case StateId::STOPPED: 83fa7767c5Sopenharmony_ci playerState = PLAYER_STOPPED; 84fa7767c5Sopenharmony_ci break; 85fa7767c5Sopenharmony_ci case StateId::EOS: 86fa7767c5Sopenharmony_ci playerState = PLAYER_PLAYBACK_COMPLETE; 87fa7767c5Sopenharmony_ci break; 88fa7767c5Sopenharmony_ci default: 89fa7767c5Sopenharmony_ci break; 90fa7767c5Sopenharmony_ci } 91fa7767c5Sopenharmony_ci return playerState; 92fa7767c5Sopenharmony_ci} 93fa7767c5Sopenharmony_ci 94fa7767c5Sopenharmony_ciPlugin::SeekMode Transform2SeekMode(PlayerSeekMode mode) 95fa7767c5Sopenharmony_ci{ 96fa7767c5Sopenharmony_ci switch (mode) { 97fa7767c5Sopenharmony_ci case PlayerSeekMode::SEEK_NEXT_SYNC: 98fa7767c5Sopenharmony_ci return Plugin::SeekMode::SEEK_NEXT_SYNC; 99fa7767c5Sopenharmony_ci case PlayerSeekMode::SEEK_PREVIOUS_SYNC: 100fa7767c5Sopenharmony_ci return Plugin::SeekMode::SEEK_PREVIOUS_SYNC; 101fa7767c5Sopenharmony_ci case PlayerSeekMode::SEEK_CLOSEST_SYNC: 102fa7767c5Sopenharmony_ci return Plugin::SeekMode::SEEK_CLOSEST_SYNC; 103fa7767c5Sopenharmony_ci case PlayerSeekMode::SEEK_CLOSEST: 104fa7767c5Sopenharmony_ci default: 105fa7767c5Sopenharmony_ci return Plugin::SeekMode::SEEK_CLOSEST; 106fa7767c5Sopenharmony_ci } 107fa7767c5Sopenharmony_ci} 108fa7767c5Sopenharmony_ciconst std::string& StringnessPlayerState(PlayerStates state) 109fa7767c5Sopenharmony_ci{ 110fa7767c5Sopenharmony_ci using StateString = std::pair<PlayerStates, std::string>; 111fa7767c5Sopenharmony_ci const static std::array<StateString, 9> maps = { // array size 112fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_STATE_ERROR, "state error"), 113fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_IDLE, "idle"), 114fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_INITIALIZED, "init"), 115fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_PREPARING, "preparing"), 116fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_PREPARED, "prepared"), 117fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_STARTED, "started"), 118fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_PAUSED, "paused"), 119fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_STOPPED, "stopped"), 120fa7767c5Sopenharmony_ci std::make_pair(PlayerStates::PLAYER_PLAYBACK_COMPLETE, "completed"), 121fa7767c5Sopenharmony_ci }; 122fa7767c5Sopenharmony_ci const static std::string UNKNOWN = "unknown"; 123fa7767c5Sopenharmony_ci auto ite = std::find_if(maps.begin(), maps.end(), [&] (const StateString& item) -> bool { 124fa7767c5Sopenharmony_ci return item.first == state; 125fa7767c5Sopenharmony_ci }); 126fa7767c5Sopenharmony_ci if (ite == maps.end()) { 127fa7767c5Sopenharmony_ci return UNKNOWN; 128fa7767c5Sopenharmony_ci } 129fa7767c5Sopenharmony_ci return ite->second; 130fa7767c5Sopenharmony_ci} 131fa7767c5Sopenharmony_cifloat TransformPlayRate2Float(PlaybackRateMode rateMode) 132fa7767c5Sopenharmony_ci{ 133fa7767c5Sopenharmony_ci auto ite = std::find_if(PLAY_RATE_REFS.begin(), PLAY_RATE_REFS.end(), [&](const auto& pair) ->bool { 134fa7767c5Sopenharmony_ci return pair.first == rateMode; 135fa7767c5Sopenharmony_ci }); 136fa7767c5Sopenharmony_ci if (ite == PLAY_RATE_REFS.end()) { 137fa7767c5Sopenharmony_ci return 1.0f; 138fa7767c5Sopenharmony_ci } 139fa7767c5Sopenharmony_ci return ite->second; 140fa7767c5Sopenharmony_ci} 141fa7767c5Sopenharmony_ciPlaybackRateMode TransformFloat2PlayRate(float rate) 142fa7767c5Sopenharmony_ci{ 143fa7767c5Sopenharmony_ci auto ite = std::find_if(PLAY_RATE_REFS.begin(), PLAY_RATE_REFS.end(), [&](const auto& pair) ->bool { 144fa7767c5Sopenharmony_ci return std::fabs(rate - pair.second) < 1e-3; 145fa7767c5Sopenharmony_ci }); 146fa7767c5Sopenharmony_ci if (ite == PLAY_RATE_REFS.end()) { 147fa7767c5Sopenharmony_ci return PlaybackRateMode::SPEED_FORWARD_1_00_X; 148fa7767c5Sopenharmony_ci } 149fa7767c5Sopenharmony_ci return ite->first; 150fa7767c5Sopenharmony_ci} 151fa7767c5Sopenharmony_ci} // namespace Media 152fa7767c5Sopenharmony_ci} // namespace OHOS