• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "dcamera_source_handler_ipc.h"
17 
18 #include <cstdint>
19 #include <new>
20 
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "distributed_camera_constants.h"
24 #include "distributed_hardware_log.h"
25 #include "iremote_broker.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
DCameraSourceHandlerIpc()29 DCameraSourceHandlerIpc::DCameraSourceHandlerIpc() : isInit_(false)
30 {
31     DHLOGI("DCameraSourceHandlerIpc Create");
32 }
33 
~DCameraSourceHandlerIpc()34 DCameraSourceHandlerIpc::~DCameraSourceHandlerIpc()
35 {
36     DHLOGI("DCameraSourceHandlerIpc Delete");
37     UnInit();
38 }
39 
40 IMPLEMENT_SINGLE_INSTANCE(DCameraSourceHandlerIpc);
41 
Init()42 void DCameraSourceHandlerIpc::Init()
43 {
44     std::lock_guard<std::mutex> autoLock(initCamSrvLock_);
45     DHLOGI("DCameraSourceHandlerIpc Init Start");
46     if (isInit_) {
47         DHLOGI("DCameraSourceHandlerIpc has already init");
48         return;
49     }
50     sourceLocalRecipient_ = new SourceLocalRecipient();
51     isInit_ = true;
52     DHLOGI("DCameraSourceHandlerIpc Init End");
53 }
54 
UnInit()55 void DCameraSourceHandlerIpc::UnInit()
56 {
57     std::lock_guard<std::mutex> autoLock(initCamSrvLock_);
58     DHLOGI("DCameraSourceHandlerIpc UnInit Start");
59     if (!isInit_) {
60         DHLOGI("DCameraSourceHandlerIpc has already UnInit");
61         return;
62     }
63     DeleteSourceLocalCamSrv();
64     DHLOGI("DCameraSourceHandlerIpc Start free recipient");
65     sourceLocalRecipient_ = nullptr;
66     isInit_ = false;
67     DHLOGI("DCameraSourceHandlerIpc UnInit End");
68 }
69 
GetSourceLocalCamSrv()70 sptr<IDistributedCameraSource> DCameraSourceHandlerIpc::GetSourceLocalCamSrv()
71 {
72     {
73         std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_);
74         if (localSource_ != nullptr) {
75             DHLOGI("DCameraSourceHandlerIpc GetSourceLocalCamSrv from cache");
76             return localSource_;
77         }
78     }
79     DHLOGI("GetSourceLocalCamSrv Start");
80     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
81     if (sm == nullptr) {
82         DHLOGE("GetSourceLocalCamSrv GetSystemAbilityManager failed");
83         return nullptr;
84     }
85 
86     sptr<IRemoteObject> object = sm->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
87     if (object == nullptr) {
88         DHLOGE("GetSourceLocalCamSrv GetSystemAbility failed");
89         return nullptr;
90     }
91     int32_t ret = object->AddDeathRecipient(sourceLocalRecipient_);
92     sptr<IDistributedCameraSource> localSource = iface_cast<IDistributedCameraSource>(object);
93     if (localSource == nullptr) {
94         DHLOGI("GetSourceLocalCamSrv failed, localSource is null ret: %d", ret);
95         return nullptr;
96     }
97     {
98         std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_);
99         if (localSource_ != nullptr) {
100             localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_);
101         }
102         localSource_ = localSource;
103     }
104     DHLOGI("GetSourceLocalCamSrv success, AddDeathRecipient ret: %d", ret);
105     return localSource;
106 }
107 
DeleteSourceLocalCamSrv()108 void DCameraSourceHandlerIpc::DeleteSourceLocalCamSrv()
109 {
110     DHLOGI("DeleteSourceLocalCamSrv start");
111     std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_);
112     if (localSource_ != nullptr) {
113         localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_);
114     }
115     localSource_ = nullptr;
116     DHLOGI("DeleteSourceLocalCamSrv end");
117 }
118 
OnRemoteDied(const wptr<IRemoteObject> & remote)119 void DCameraSourceHandlerIpc::SourceLocalRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
120 {
121     DHLOGI("SourceLocalRecipient OnRemoteDied received died notify!");
122     DCameraSourceHandlerIpc::GetInstance().OnSourceLocalCamSrvDied(remote);
123 }
124 
OnSourceLocalCamSrvDied(const wptr<IRemoteObject> & remote)125 void DCameraSourceHandlerIpc::OnSourceLocalCamSrvDied(const wptr<IRemoteObject>& remote)
126 {
127     DHLOGI("OnSourceLocalCamSrvDied delete diedRemoted");
128     std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_);
129     if (localSource_ == nullptr) {
130         DHLOGE("DCameraSourceHandlerIpc::OnSourceLocalCamSrvDied, localSource is null.");
131         return;
132     }
133     sptr<IRemoteObject> diedRemoted = remote.promote();
134     if (diedRemoted == nullptr) {
135         DHLOGE("OnSourceLocalCamSrvDied promote failed!");
136         return;
137     }
138     if (localSource_->AsObject() != diedRemoted) {
139         DHLOGI("OnSourceLocalCamSrvDied not found remote object.");
140         return;
141     }
142     DHLOGI("OnSourceLocalCamSrvDied Clear");
143     localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_);
144     localSource_ = nullptr;
145 }
146 } // namespace DistributedHardware
147 } // namespace OHOS
148