1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "media_analysis_proxy.h"
17
18 #include "if_system_ability_manager.h"
19 #include "system_ability_definition.h"
20 #include "iservice_registry.h"
21
22 namespace OHOS {
23 namespace Media {
24 std::mutex MediaAnalysisProxy::mutex_;
25
MediaAnalysisProxy(const sptr<IRemoteObject> & object)26 MediaAnalysisProxy::MediaAnalysisProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IMediaAnalysisService>(object)
27 {
28 MEDIA_INFO_LOG("creat MediaAnalysisProxy instance");
29 }
30
~MediaAnalysisProxy()31 MediaAnalysisProxy::~MediaAnalysisProxy()
32 {
33 MEDIA_INFO_LOG("destroy MediaAnalysisProxy instance");
34 }
35
SendTransactCmd(int32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 bool MediaAnalysisProxy::SendTransactCmd(int32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 if (saMgr == nullptr) {
40 MEDIA_ERR_LOG("Get samgr failed, samgr is nullptr, code: %{public}d", code);
41 return false;
42 }
43 int32_t minTimeout = 4;
44 sptr<IRemoteObject> remoteObject;
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 remoteObject = saMgr->CheckSystemAbility(SAID);
48 if (remoteObject == nullptr) {
49 int ret = SetParameter("persist.multimedia.media_analysis_service.startactively", "1");
50 if (ret != 0) {
51 MEDIA_ERR_LOG("Failed to set parameter startactively, ret:%{public}d, code: %{public}d", ret, code);
52 }
53 remoteObject = saMgr->LoadSystemAbility(SAID, minTimeout);
54 if (remoteObject == nullptr) {
55 MEDIA_ERR_LOG(
56 "Failed to send transact %{public}d due to remote object is null, SA will be unloaded", code);
57 saMgr->UnloadSystemAbility(SAID);
58 return false;
59 }
60 }
61 }
62
63 int32_t result = remoteObject->SendRequest(code, data, reply, option);
64 if (result != NO_ERROR) {
65 MEDIA_ERR_LOG("receive error transact result: %{public}d, code: %{public}d, SA will be unloaded", result, code);
66 saMgr->UnloadSystemAbility(SAID);
67 return false;
68 }
69 MEDIA_INFO_LOG("send request success, code: %{public}d", code);
70 return true;
71 }
72 } // namespace Media
73 } // namespace OHOS