• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_stub.h"
16 #include "avcodec_errors.h"
17 #include "avcodec_log.h"
18 #include "avcodec_server_manager.h"
19 #include "avcodec_xcollie.h"
20 
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecServiceStub"};
24 }
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
AVCodecServiceStub()28 AVCodecServiceStub::AVCodecServiceStub()
29 {
30     deathRecipientMap_.clear();
31     avCodecListenerMap_.clear();
32     InitStub();
33     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
34 }
35 
~AVCodecServiceStub()36 AVCodecServiceStub::~AVCodecServiceStub()
37 {
38     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
39 }
40 
InitStub()41 void AVCodecServiceStub::InitStub() {}
42 
DestroyStubForPid(pid_t pid)43 int32_t AVCodecServiceStub::DestroyStubForPid(pid_t pid)
44 {
45     {
46         std::lock_guard<std::mutex> lock(mutex_);
47         sptr<AVCodecDeathRecipient> deathRecipient = nullptr;
48         sptr<IStandardAVCodecListener> avCodecListener = nullptr;
49 
50         auto itDeath = deathRecipientMap_.find(pid);
51         if (itDeath != deathRecipientMap_.end()) {
52             deathRecipient = itDeath->second;
53 
54             if (deathRecipient != nullptr) {
55                 deathRecipient->SetNotifyCb(nullptr);
56             }
57 
58             (void)deathRecipientMap_.erase(itDeath);
59         }
60 
61         auto itListener = avCodecListenerMap_.find(pid);
62         if (itListener != avCodecListenerMap_.end()) {
63             avCodecListener = itListener->second;
64 
65             if (avCodecListener != nullptr && avCodecListener->AsObject() != nullptr && deathRecipient != nullptr) {
66                 (void)avCodecListener->AsObject()->RemoveDeathRecipient(deathRecipient);
67             }
68 
69             (void)avCodecListenerMap_.erase(itListener);
70         }
71     }
72 
73     AVCodecServerManager::GetInstance().DestroyStubObjectForPid(pid);
74     return AVCS_ERR_OK;
75 }
76 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int AVCodecServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
78 {
79     AVCODEC_LOGD("Stub: OnRemoteRequest of code: %{public}u is received", code);
80 
81     auto remoteDescriptor = data.ReadInterfaceToken();
82     if (AVCodecServiceStub::GetDescriptor() != remoteDescriptor) {
83         AVCODEC_LOGE("Invalid descriptor");
84         return AVCS_ERR_INVALID_OPERATION;
85     }
86     int32_t ret = -1;
87     switch (code) {
88         case static_cast<uint32_t>(AVCodecServiceInterfaceCode::GET_SUBSYSTEM):
89             ret = GetSystemAbility(data, reply);
90             break;
91         default:
92             AVCODEC_LOGW("AVCodecServiceStub: no member func supporting, applying default process");
93             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
94     }
95     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Failed to call member func GetSystemAbility");
96     return ret;
97 }
98 
ClientDied(pid_t pid)99 void AVCodecServiceStub::ClientDied(pid_t pid)
100 {
101     AVCODEC_LOGE("client is dead, pid:%{public}d", pid);
102     (void)DestroyStubForPid(pid);
103 }
104 
SetDeathListener(const sptr<IRemoteObject> & object)105 int32_t AVCodecServiceStub::SetDeathListener(const sptr<IRemoteObject> &object)
106 {
107     std::lock_guard<std::mutex> lock(mutex_);
108     CHECK_AND_RETURN_RET_LOG(object != nullptr, AVCS_ERR_NO_MEMORY, "set listener object is nullptr");
109 
110     pid_t pid = IPCSkeleton::GetCallingPid();
111     for (auto it = avCodecListenerMap_.begin(); it != avCodecListenerMap_.end(); ++it) {
112         if (it->first == pid) {
113             AVCODEC_LOGD("client pid already exits");
114             return AVCS_ERR_OK;
115         }
116     }
117     sptr<IStandardAVCodecListener> avCodecListener = iface_cast<IStandardAVCodecListener>(object);
118     CHECK_AND_RETURN_RET_LOG(avCodecListener != nullptr, AVCS_ERR_NO_MEMORY,
119                              "failed to convert IStandardAVCodecListener");
120 
121     sptr<AVCodecDeathRecipient> deathRecipient = new (std::nothrow) AVCodecDeathRecipient(pid);
122     CHECK_AND_RETURN_RET_LOG(deathRecipient != nullptr, AVCS_ERR_NO_MEMORY, "failed to new AVCodecDeathRecipient");
123 
124     deathRecipient->SetNotifyCb(std::bind(&AVCodecServiceStub::ClientDied, this, std::placeholders::_1));
125 
126     if (avCodecListener->AsObject() != nullptr) {
127         (void)avCodecListener->AsObject()->AddDeathRecipient(deathRecipient);
128     }
129 
130     AVCODEC_LOGD("client pid pid:%{public}d", pid);
131     avCodecListenerMap_[pid] = avCodecListener;
132     deathRecipientMap_[pid] = deathRecipient;
133     return AVCS_ERR_OK;
134 }
135 
GetSystemAbility(MessageParcel & data,MessageParcel & reply)136 int32_t AVCodecServiceStub::GetSystemAbility(MessageParcel &data, MessageParcel &reply)
137 {
138     AVCodecSystemAbility id = static_cast<AVCodecSystemAbility>(data.ReadInt32());
139     sptr<IRemoteObject> listenerObj = data.ReadRemoteObject();
140 
141     (void)reply.WriteRemoteObject(GetSubSystemAbility(id, listenerObj));
142 
143     return AVCS_ERR_OK;
144 }
145 } // namespace MediaAVCodec
146 } // namespace OHOS
147