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