1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci* Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci* you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci* You may obtain a copy of the License at
6049e185fSopenharmony_ci*
7049e185fSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci*
9049e185fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci* See the License for the specific language governing permissions and
13049e185fSopenharmony_ci* limitations under the License.
14049e185fSopenharmony_ci*/
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#include <cmath>
17049e185fSopenharmony_ci#include <media_errors.h>
18049e185fSopenharmony_ci#include <sstream>
19049e185fSopenharmony_ci#include <unordered_map>
20049e185fSopenharmony_ci#include "common/log.h"
21049e185fSopenharmony_ci#include "media_utils.h"
22049e185fSopenharmony_ci#include "iservice_registry.h"
23049e185fSopenharmony_ci#include "bundle_mgr_interface.h"
24049e185fSopenharmony_ci#include "system_ability_definition.h"
25049e185fSopenharmony_ci#include <unordered_set>
26049e185fSopenharmony_ci#include "media_log.h"
27049e185fSopenharmony_ci#include "parameter.h"
28049e185fSopenharmony_ci
29049e185fSopenharmony_cinamespace {
30049e185fSopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "MediaUtils" };
31049e185fSopenharmony_ci}
32049e185fSopenharmony_ci
33049e185fSopenharmony_cinamespace OHOS {
34049e185fSopenharmony_cinamespace Media {
35049e185fSopenharmony_cinamespace {
36049e185fSopenharmony_ciconst std::pair<Status, int> g_statusPair[] = {
37049e185fSopenharmony_ci    {Status::OK, MSERR_OK},
38049e185fSopenharmony_ci    {Status::ERROR_UNKNOWN, MSERR_UNKNOWN},
39049e185fSopenharmony_ci    {Status::ERROR_AGAIN, MSERR_UNKNOWN},
40049e185fSopenharmony_ci    {Status::ERROR_UNIMPLEMENTED, MSERR_UNSUPPORT},
41049e185fSopenharmony_ci    {Status::ERROR_INVALID_PARAMETER, MSERR_INVALID_VAL},
42049e185fSopenharmony_ci    {Status::ERROR_INVALID_OPERATION, MSERR_INVALID_OPERATION},
43049e185fSopenharmony_ci    {Status::ERROR_UNSUPPORTED_FORMAT, MSERR_UNSUPPORT_CONTAINER_TYPE},
44049e185fSopenharmony_ci    {Status::ERROR_NOT_EXISTED, MSERR_OPEN_FILE_FAILED},
45049e185fSopenharmony_ci    {Status::ERROR_TIMED_OUT, MSERR_EXT_TIMEOUT},
46049e185fSopenharmony_ci    {Status::ERROR_NO_MEMORY, MSERR_EXT_NO_MEMORY},
47049e185fSopenharmony_ci    {Status::ERROR_INVALID_STATE, MSERR_INVALID_STATE},
48049e185fSopenharmony_ci};
49049e185fSopenharmony_ciconst std::array<std::pair<PlaybackRateMode, float>, 10> PLAY_RATE_REFS = {
50049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_0_75_X, 0.75),
51049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_00_X, 1.0),
52049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_25_X, 1.25),
53049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_75_X, 1.75),
54049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_2_00_X, 2.00),
55049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_0_50_X, 0.50),
56049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_1_50_X, 1.50),
57049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_3_00_X, 3.00),
58049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_0_25_X, 0.25),
59049e185fSopenharmony_ci    std::make_pair(PlaybackRateMode::SPEED_FORWARD_0_125_X, 0.125),
60049e185fSopenharmony_ci};
61049e185fSopenharmony_ci
62049e185fSopenharmony_cistatic int g_readSysParaIdx = 0;
63049e185fSopenharmony_cistatic std::unordered_map<std::string, std::string> g_readSysParaMap;
64049e185fSopenharmony_ci}  // namespace
65049e185fSopenharmony_ci
66049e185fSopenharmony_cistd::string __attribute__((visibility("default"))) GetClientBundleName(int32_t uid, bool shouldLog)
67049e185fSopenharmony_ci{
68049e185fSopenharmony_ci    if (uid == 1003) { // 1003 is bootanimation uid
69049e185fSopenharmony_ci        return "bootanimation";
70049e185fSopenharmony_ci    }
71049e185fSopenharmony_ci    std::string bundleName = "";
72049e185fSopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
73049e185fSopenharmony_ci    if (samgr == nullptr) {
74049e185fSopenharmony_ci        MEDIA_LOG_E("Get ability manager failed");
75049e185fSopenharmony_ci        return bundleName;
76049e185fSopenharmony_ci    }
77049e185fSopenharmony_ci
78049e185fSopenharmony_ci    sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
79049e185fSopenharmony_ci    if (object == nullptr) {
80049e185fSopenharmony_ci        MEDIA_LOG_E("object is NULL.");
81049e185fSopenharmony_ci        return bundleName;
82049e185fSopenharmony_ci    }
83049e185fSopenharmony_ci
84049e185fSopenharmony_ci    sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
85049e185fSopenharmony_ci    if (bms == nullptr) {
86049e185fSopenharmony_ci        MEDIA_LOG_E("bundle manager service is NULL.");
87049e185fSopenharmony_ci        return bundleName;
88049e185fSopenharmony_ci    }
89049e185fSopenharmony_ci
90049e185fSopenharmony_ci    auto result = bms->GetNameForUid(uid, bundleName);
91049e185fSopenharmony_ci    if (result != ERR_OK) {
92049e185fSopenharmony_ci        MEDIA_LOG_E("Error GetBundleNameForUid fail");
93049e185fSopenharmony_ci        return "";
94049e185fSopenharmony_ci    }
95049e185fSopenharmony_ci    MEDIA_LOG_I("bundle name is %{public}s ", bundleName.c_str());
96049e185fSopenharmony_ci
97049e185fSopenharmony_ci    return bundleName;
98049e185fSopenharmony_ci}
99049e185fSopenharmony_ci
100049e185fSopenharmony_cistd::string __attribute__((visibility("default"))) GetBundleResourceLabel(std::string bundleName)
101049e185fSopenharmony_ci{
102049e185fSopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103049e185fSopenharmony_ci    if (samgr == nullptr) {
104049e185fSopenharmony_ci        MEDIA_LOG_E("Get ability manager failed");
105049e185fSopenharmony_ci        return bundleName;
106049e185fSopenharmony_ci    }
107049e185fSopenharmony_ci
108049e185fSopenharmony_ci    sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
109049e185fSopenharmony_ci    if (object == nullptr) {
110049e185fSopenharmony_ci        MEDIA_LOG_E("object is NULL.");
111049e185fSopenharmony_ci        return bundleName;
112049e185fSopenharmony_ci    }
113049e185fSopenharmony_ci
114049e185fSopenharmony_ci    sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
115049e185fSopenharmony_ci    if (bms == nullptr) {
116049e185fSopenharmony_ci        MEDIA_LOG_E("bundle manager service is NULL.");
117049e185fSopenharmony_ci        return bundleName;
118049e185fSopenharmony_ci    }
119049e185fSopenharmony_ci
120049e185fSopenharmony_ci    auto bundleResourceProxy = bms->GetBundleResourceProxy();
121049e185fSopenharmony_ci    if (bundleResourceProxy == nullptr) {
122049e185fSopenharmony_ci        MEDIA_LOG_E("GetBundleResourceProxy fail");
123049e185fSopenharmony_ci        return bundleName;
124049e185fSopenharmony_ci    }
125049e185fSopenharmony_ci    AppExecFwk::BundleResourceInfo resourceInfo;
126049e185fSopenharmony_ci    auto result = bundleResourceProxy->GetBundleResourceInfo(bundleName,
127049e185fSopenharmony_ci        static_cast<uint32_t>(OHOS::AppExecFwk::ResourceFlag::GET_RESOURCE_INFO_ALL), resourceInfo);
128049e185fSopenharmony_ci    if (result != ERR_OK) {
129049e185fSopenharmony_ci        MEDIA_LOG_E("GetBundleResourceInfo failed");
130049e185fSopenharmony_ci        return bundleName;
131049e185fSopenharmony_ci    }
132049e185fSopenharmony_ci    MEDIA_LOG_I("bundle resource label is %{public}s ", (resourceInfo.label).c_str());
133049e185fSopenharmony_ci    return resourceInfo.label;
134049e185fSopenharmony_ci}
135049e185fSopenharmony_ci
136049e185fSopenharmony_ci
137049e185fSopenharmony_ciint __attribute__((visibility("default"))) TransStatus(Status status)
138049e185fSopenharmony_ci{
139049e185fSopenharmony_ci    for (const auto& errPair : g_statusPair) {
140049e185fSopenharmony_ci        if (errPair.first == status) {
141049e185fSopenharmony_ci            return errPair.second;
142049e185fSopenharmony_ci        }
143049e185fSopenharmony_ci    }
144049e185fSopenharmony_ci    return MSERR_UNKNOWN;
145049e185fSopenharmony_ci}
146049e185fSopenharmony_ci
147049e185fSopenharmony_ciPlayerStates __attribute__((visibility("default"))) TransStateId2PlayerState(PlayerStateId state)
148049e185fSopenharmony_ci{
149049e185fSopenharmony_ci    PlayerStates playerState = PLAYER_STATE_ERROR;
150049e185fSopenharmony_ci    switch (state) {
151049e185fSopenharmony_ci        case PlayerStateId::IDLE:
152049e185fSopenharmony_ci            playerState = PLAYER_IDLE;
153049e185fSopenharmony_ci            break;
154049e185fSopenharmony_ci        case PlayerStateId::INIT:
155049e185fSopenharmony_ci            playerState = PLAYER_INITIALIZED;
156049e185fSopenharmony_ci            break;
157049e185fSopenharmony_ci        case PlayerStateId::PREPARING:
158049e185fSopenharmony_ci            playerState = PLAYER_PREPARING;
159049e185fSopenharmony_ci            break;
160049e185fSopenharmony_ci        case PlayerStateId::READY:
161049e185fSopenharmony_ci            playerState = PLAYER_PREPARED;
162049e185fSopenharmony_ci            break;
163049e185fSopenharmony_ci        case PlayerStateId::PAUSE:
164049e185fSopenharmony_ci            playerState = PLAYER_PAUSED;
165049e185fSopenharmony_ci            break;
166049e185fSopenharmony_ci        case PlayerStateId::PLAYING:
167049e185fSopenharmony_ci            playerState = PLAYER_STARTED;
168049e185fSopenharmony_ci            break;
169049e185fSopenharmony_ci        case PlayerStateId::STOPPED:
170049e185fSopenharmony_ci            playerState = PLAYER_STOPPED;
171049e185fSopenharmony_ci            break;
172049e185fSopenharmony_ci        case PlayerStateId::EOS:
173049e185fSopenharmony_ci            playerState = PLAYER_PLAYBACK_COMPLETE;
174049e185fSopenharmony_ci            break;
175049e185fSopenharmony_ci        default:
176049e185fSopenharmony_ci            break;
177049e185fSopenharmony_ci    }
178049e185fSopenharmony_ci    return playerState;
179049e185fSopenharmony_ci}
180049e185fSopenharmony_ci
181049e185fSopenharmony_ciPlugins::SeekMode __attribute__((visibility("default"))) Transform2SeekMode(PlayerSeekMode mode)
182049e185fSopenharmony_ci{
183049e185fSopenharmony_ci    switch (mode) {
184049e185fSopenharmony_ci        case PlayerSeekMode::SEEK_NEXT_SYNC:
185049e185fSopenharmony_ci            return Plugins::SeekMode::SEEK_NEXT_SYNC;
186049e185fSopenharmony_ci        case PlayerSeekMode::SEEK_PREVIOUS_SYNC:
187049e185fSopenharmony_ci            return Plugins::SeekMode::SEEK_PREVIOUS_SYNC;
188049e185fSopenharmony_ci        case PlayerSeekMode::SEEK_CLOSEST_SYNC:
189049e185fSopenharmony_ci            return Plugins::SeekMode::SEEK_CLOSEST_SYNC;
190049e185fSopenharmony_ci        case PlayerSeekMode::SEEK_CLOSEST:
191049e185fSopenharmony_ci        default:
192049e185fSopenharmony_ci            return Plugins::SeekMode::SEEK_CLOSEST;
193049e185fSopenharmony_ci    }
194049e185fSopenharmony_ci}
195049e185fSopenharmony_ciconst std::string& __attribute__((visibility("default"))) StringnessPlayerState(PlayerStates state)
196049e185fSopenharmony_ci{
197049e185fSopenharmony_ci    using StateString = std::pair<PlayerStates, std::string>;
198049e185fSopenharmony_ci    const static std::array<StateString, 9> maps = { // array size
199049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_STATE_ERROR, "state error"),
200049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_IDLE, "idle"),
201049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_INITIALIZED, "init"),
202049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_PREPARING, "preparing"),
203049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_PREPARED, "prepared"),
204049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_STARTED, "started"),
205049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_PAUSED, "paused"),
206049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_STOPPED, "stopped"),
207049e185fSopenharmony_ci        std::make_pair(PlayerStates::PLAYER_PLAYBACK_COMPLETE, "completed"),
208049e185fSopenharmony_ci    };
209049e185fSopenharmony_ci    const static std::string UNKNOWN = "unknown";
210049e185fSopenharmony_ci    auto ite = std::find_if(maps.begin(), maps.end(), [&] (const StateString& item) -> bool {
211049e185fSopenharmony_ci        return item.first == state;
212049e185fSopenharmony_ci    });
213049e185fSopenharmony_ci    if (ite == maps.end()) {
214049e185fSopenharmony_ci        return UNKNOWN;
215049e185fSopenharmony_ci    }
216049e185fSopenharmony_ci    return ite->second;
217049e185fSopenharmony_ci}
218049e185fSopenharmony_cifloat __attribute__((visibility("default"))) TransformPlayRate2Float(PlaybackRateMode rateMode)
219049e185fSopenharmony_ci{
220049e185fSopenharmony_ci    auto ite = std::find_if(PLAY_RATE_REFS.begin(), PLAY_RATE_REFS.end(), [&](const auto& pair) ->bool {
221049e185fSopenharmony_ci        return pair.first == rateMode;
222049e185fSopenharmony_ci    });
223049e185fSopenharmony_ci    if (ite == PLAY_RATE_REFS.end()) {
224049e185fSopenharmony_ci        return 1.0f;
225049e185fSopenharmony_ci    }
226049e185fSopenharmony_ci    return ite->second;
227049e185fSopenharmony_ci}
228049e185fSopenharmony_ciPlaybackRateMode __attribute__((visibility("default"))) TransformFloat2PlayRate(float rate)
229049e185fSopenharmony_ci{
230049e185fSopenharmony_ci    auto ite = std::find_if(PLAY_RATE_REFS.begin(), PLAY_RATE_REFS.end(), [&](const auto& pair) ->bool {
231049e185fSopenharmony_ci        return std::fabs(rate - pair.second) < 1e-3;
232049e185fSopenharmony_ci    });
233049e185fSopenharmony_ci    if (ite == PLAY_RATE_REFS.end()) {
234049e185fSopenharmony_ci        return PlaybackRateMode::SPEED_FORWARD_1_00_X;
235049e185fSopenharmony_ci    }
236049e185fSopenharmony_ci    return ite->first;
237049e185fSopenharmony_ci}
238049e185fSopenharmony_ci
239049e185fSopenharmony_cibool __attribute__((visibility("default"))) IsEnableOptimizeDecode()
240049e185fSopenharmony_ci{
241049e185fSopenharmony_ci    char useOptimizeDecode[10] = {0}; // 10: system param usage
242049e185fSopenharmony_ci    auto res = GetParameter("debug.media_service.optimize_decode", "1", useOptimizeDecode, sizeof(useOptimizeDecode));
243049e185fSopenharmony_ci    return res == 1 && useOptimizeDecode[0] == '1';
244049e185fSopenharmony_ci}
245049e185fSopenharmony_ci
246049e185fSopenharmony_cibool __attribute__((visibility("default"))) IsAppEnableRenderFirstFrame(int32_t uid)
247049e185fSopenharmony_ci{
248049e185fSopenharmony_ci    return uid != 1003; // 1003 is bootanimation uid
249049e185fSopenharmony_ci}
250049e185fSopenharmony_ci
251049e185fSopenharmony_cibool __attribute__((visibility("default"))) GetPackageName(const char *key, std::string &value)
252049e185fSopenharmony_ci{
253049e185fSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr");
254049e185fSopenharmony_ci    char paraValue[100] = {0};   // 100 for system parameter
255049e185fSopenharmony_ci    auto res = GetParameter(key, "-1", paraValue, sizeof(paraValue));
256049e185fSopenharmony_ci
257049e185fSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(res > 0, false, "GetSysPara fail, key:%{public}s res:%{public}d", key, res);
258049e185fSopenharmony_ci    std::stringstream valueStr;
259049e185fSopenharmony_ci    valueStr << paraValue;
260049e185fSopenharmony_ci    valueStr >> value;
261049e185fSopenharmony_ci    MEDIA_LOG_I("Config parameter %{public}s : %{public}s", key, value.c_str());
262049e185fSopenharmony_ci    return true;
263049e185fSopenharmony_ci}
264049e185fSopenharmony_ci
265049e185fSopenharmony_cistd::unordered_map<std::string, std::string> __attribute__((visibility("default"))) GetScreenCaptureSystemParam()
266049e185fSopenharmony_ci{
267049e185fSopenharmony_ci    if (g_readSysParaIdx == 0) {
268049e185fSopenharmony_ci        GetPackageName("const.multimedia.screencapture.dialogconnectionbundlename",
269049e185fSopenharmony_ci            g_readSysParaMap["const.multimedia.screencapture.dialogconnectionbundlename"]);
270049e185fSopenharmony_ci        GetPackageName("const.multimedia.screencapture.dialogconnectionabilityname",
271049e185fSopenharmony_ci            g_readSysParaMap["const.multimedia.screencapture.dialogconnectionabilityname"]);
272049e185fSopenharmony_ci        GetPackageName("const.multimedia.screencapture.screenrecorderbundlename",
273049e185fSopenharmony_ci            g_readSysParaMap["const.multimedia.screencapture.screenrecorderbundlename"]);
274049e185fSopenharmony_ci        GetPackageName("const.multimedia.screencapture.screenrecorderabilityname",
275049e185fSopenharmony_ci            g_readSysParaMap["const.multimedia.screencapture.screenrecorderabilityname"]);
276049e185fSopenharmony_ci        GetPackageName("const.multimedia.screencapture.hiviewcarebundlename",
277049e185fSopenharmony_ci            g_readSysParaMap["const.multimedia.screencapture.hiviewcarebundlename"]);
278049e185fSopenharmony_ci        g_readSysParaIdx++;
279049e185fSopenharmony_ci    }
280049e185fSopenharmony_ci    return g_readSysParaMap;
281049e185fSopenharmony_ci}
282049e185fSopenharmony_ci}  // namespace Media
283049e185fSopenharmony_ci}  // namespace OHOS
284