1 /*
2  * Copyright (C) 2023 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 #include "avcodec_client.h"
16 #include "avcodec_xcollie.h"
17 #include "ipc_skeleton.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "avcodec_trace.h"
21 #include "avcodec_sysevent.h"
22 
23 #ifdef SUPPORT_CODEC
24 #include "i_standard_codec_service.h"
25 #endif
26 #ifdef SUPPORT_CODECLIST
27 #include "i_standard_codeclist_service.h"
28 #endif
29 
30 #include "avcodec_errors.h"
31 #include "avcodec_log.h"
32 
33 namespace {
34 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AVCodecClient"};
35 }
36 
37 namespace OHOS {
38 namespace MediaAVCodec {
39 static AVCodecClient g_avCodecClientInstance;
GetInstance()40 IAVCodecService &AVCodecServiceFactory::GetInstance()
41 {
42     return g_avCodecClientInstance;
43 }
44 
AVCodecClient()45 AVCodecClient::AVCodecClient() noexcept
46 {
47     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
48 }
49 
~AVCodecClient()50 AVCodecClient::~AVCodecClient()
51 {
52     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
53 }
54 
IsAlived()55 bool AVCodecClient::IsAlived()
56 {
57     if (avCodecProxy_ == nullptr) {
58         avCodecProxy_ = GetAVCodecProxy();
59     }
60 
61     return avCodecProxy_ != nullptr;
62 }
63 #ifdef SUPPORT_CODEC
CreateCodecService(std::shared_ptr<ICodecService> & codecClient)64 int32_t AVCodecClient::CreateCodecService(std::shared_ptr<ICodecService> &codecClient)
65 {
66     std::lock_guard<std::mutex> lock(mutex_);
67     bool alived = IsAlived();
68     CHECK_AND_RETURN_RET_LOG(alived, AVCS_ERR_SERVICE_DIED, "AVCodec service does not exist.");
69 
70     sptr<IRemoteObject> object = nullptr;
71     int32_t ret = avCodecProxy_->GetSubSystemAbility(
72         IStandardAVCodecService::AVCodecSystemAbility::AVCODEC_CODEC, listenerStub_->AsObject(), object);
73     CHECK_AND_RETURN_RET_LOG(object != nullptr, ret, "Create codec proxy object failed.");
74 
75     sptr<IStandardCodecService> codecProxy = iface_cast<IStandardCodecService>(object);
76     CHECK_AND_RETURN_RET_LOG(codecProxy != nullptr, AVCS_ERR_UNSUPPORT, "Codec proxy is nullptr.");
77 
78     ret = CodecClient::Create(codecProxy, codecClient);
79     CHECK_AND_RETURN_RET_LOG(codecClient != nullptr, ret, "Failed to create codec client.");
80 
81     codecClientList_.push_back(codecClient);
82     return AVCS_ERR_OK;
83 }
84 
DestroyCodecService(std::shared_ptr<ICodecService> codecClient)85 int32_t AVCodecClient::DestroyCodecService(std::shared_ptr<ICodecService> codecClient)
86 {
87     std::lock_guard<std::mutex> lock(mutex_);
88     CHECK_AND_RETURN_RET_LOG(codecClient != nullptr, AVCS_ERR_NO_MEMORY, "codec client is nullptr.");
89     codecClientList_.remove(codecClient);
90     return AVCS_ERR_OK;
91 }
92 #endif
93 #ifdef SUPPORT_CODECLIST
CreateCodecListService()94 std::shared_ptr<ICodecListService> AVCodecClient::CreateCodecListService()
95 {
96     std::lock_guard<std::mutex> lock(mutex_);
97     bool alived = IsAlived();
98     CHECK_AND_RETURN_RET_LOG(alived, nullptr, "AVCodec service does not exist.");
99 
100     sptr<IRemoteObject> object = nullptr;
101     (void)avCodecProxy_->GetSubSystemAbility(
102         IStandardAVCodecService::AVCodecSystemAbility::AVCODEC_CODECLIST, listenerStub_->AsObject(), object);
103     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "Create codeclist proxy object failed.");
104 
105     sptr<IStandardCodecListService> codecListProxy = iface_cast<IStandardCodecListService>(object);
106     CHECK_AND_RETURN_RET_LOG(codecListProxy != nullptr, nullptr, "codeclist proxy is nullptr.");
107 
108     std::shared_ptr<CodecListClient> codecListClient = CodecListClient::Create(codecListProxy);
109     CHECK_AND_RETURN_RET_LOG(codecListClient != nullptr, nullptr, "failed to create codeclist client.");
110 
111     codecListClientList_.push_back(codecListClient);
112     return codecListClient;
113 }
114 
DestroyCodecListService(std::shared_ptr<ICodecListService> codecListClient)115 int32_t AVCodecClient::DestroyCodecListService(std::shared_ptr<ICodecListService> codecListClient)
116 {
117     std::lock_guard<std::mutex> lock(mutex_);
118     CHECK_AND_RETURN_RET_LOG(codecListClient != nullptr, AVCS_ERR_NO_MEMORY, "codeclist client is nullptr.");
119     codecListClientList_.remove(codecListClient);
120     return AVCS_ERR_OK;
121 }
122 #endif
123 
GetAVCodecProxy()124 sptr<IStandardAVCodecService> AVCodecClient::GetAVCodecProxy()
125 {
126     AVCODEC_LOGD("enter");
127     sptr<ISystemAbilityManager> samgr = nullptr;
128     CLIENT_COLLIE_LISTEN(samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(),
129         "AVCodecClient GetAVCodecProxy");
130     CHECK_AND_RETURN_RET_LOG(samgr != nullptr, nullptr, "system ability manager is nullptr.");
131 
132     sptr<IRemoteObject> object = nullptr;
133     CLIENT_COLLIE_LISTEN(object = samgr->GetSystemAbility(OHOS::AV_CODEC_SERVICE_ID), "AVCodecClient GetAVCodecProxy");
134     if (object == nullptr) {
135         CLIENT_COLLIE_LISTEN(object = samgr->LoadSystemAbility(OHOS::AV_CODEC_SERVICE_ID, 30), // 30: timeout
136                              "AVCodecClient LoadSystemAbility");
137         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "avcodec object is nullptr.");
138     }
139 
140     avCodecProxy_ = iface_cast<IStandardAVCodecService>(object);
141     CHECK_AND_RETURN_RET_LOG(avCodecProxy_ != nullptr, nullptr, "avcodec proxy is nullptr.");
142 
143     pid_t pid = 0;
144     deathRecipient_ = new (std::nothrow) AVCodecDeathRecipient(pid);
145     CHECK_AND_RETURN_RET_LOG(deathRecipient_ != nullptr, nullptr, "failed to new AVCodecDeathRecipient.");
146 
147     deathRecipient_->SetNotifyCb(std::bind(&AVCodecClient::AVCodecServerDied, std::placeholders::_1));
148     bool result = object->AddDeathRecipient(deathRecipient_);
149     CHECK_AND_RETURN_RET_LOG(result, nullptr, "Failed to add deathRecipient");
150 
151     listenerStub_ = new (std::nothrow) AVCodecListenerStub();
152     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, nullptr, "failed to new AVCodecListenerStub");
153     return avCodecProxy_;
154 }
155 
AVCodecServerDied(pid_t pid)156 void AVCodecClient::AVCodecServerDied(pid_t pid)
157 {
158     AVCODEC_LOGE("AVCodec service is died, pid:%{public}d!", pid);
159     g_avCodecClientInstance.DoAVCodecServerDied();
160     FaultEventWrite(FaultType::FAULT_TYPE_CRASH, "AVCodec service is died", "AVCodecClient");
161 }
162 
DoAVCodecServerDied()163 void AVCodecClient::DoAVCodecServerDied()
164 {
165     std::lock_guard<std::mutex> lock(mutex_);
166     if (avCodecProxy_ != nullptr) {
167         (void)avCodecProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
168         avCodecProxy_ = nullptr;
169     }
170     listenerStub_ = nullptr;
171     deathRecipient_ = nullptr;
172 
173 #ifdef SUPPORT_CODEC
174     for (auto &it : codecClientList_) {
175         auto codecClient = std::static_pointer_cast<CodecClient>(it);
176         if (codecClient != nullptr) {
177             codecClient->AVCodecServerDied();
178         }
179     }
180 #endif
181 #ifdef SUPPORT_CODECLIST
182     for (auto &it : codecListClientList_) {
183         auto codecListClient = std::static_pointer_cast<CodecListClient>(it);
184         if (codecListClient != nullptr) {
185             codecListClient->AVCodecServerDied();
186         }
187     }
188 #endif
189 }
190 } // namespace MediaAVCodec
191 } // namespace OHOS
192