• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "avsession_manager_impl.h"
17 #include "iservice_registry.h"
18 #include "ipc_skeleton.h"
19 #include "system_ability_definition.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 #include "session_listener_client.h"
23 #include "avsession_trace.h"
24 #include "avsession_sysevent.h"
25 #include "avsession_utils.h"
26 
27 namespace OHOS::AVSession {
AVSessionManagerImpl()28 AVSessionManagerImpl::AVSessionManagerImpl()
29 {
30     SLOGD("constructor");
31 }
32 
GetService()33 sptr<AVSessionServiceProxy> AVSessionManagerImpl::GetService()
34 {
35     std::lock_guard<std::mutex> lockGuard(lock_);
36     if (service_) {
37         return service_;
38     }
39 
40     auto mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (mgr == nullptr) {
42         SLOGE("failed to get sa mgr");
43         return nullptr;
44     }
45     auto object = mgr->GetSystemAbility(AVSESSION_SERVICE_ID);
46     if (object == nullptr) {
47         SLOGE("failed to get service");
48         return nullptr;
49     }
50     service_ = iface_cast<AVSessionServiceProxy>(object);
51     if (service_ != nullptr) {
52         auto recipient = new(std::nothrow) ServiceDeathRecipient([this] { OnServiceDie(); });
53         if (recipient == nullptr) {
54             SLOGE("register ServiceDeathRecipient failed");
55             return nullptr;
56         }
57 
58         sptr<IAVSessionService> serviceBase = service_;
59         serviceBase->AsObject()->AddDeathRecipient(recipient);
60 
61         SLOGD("get service success");
62         RegisterClientDeathObserver();
63     }
64     return service_;
65 }
66 
OnServiceDie()67 void AVSessionManagerImpl::OnServiceDie()
68 {
69     SLOGI("enter");
70     auto callback = deathCallback_;
71     {
72         std::lock_guard<std::mutex> lockGuard(lock_);
73         service_.clear();
74         listener_.clear();
75         clientDeath_.clear();
76         deathCallback_ = nullptr;
77     }
78     if (callback) {
79         callback();
80     }
81     HISYSEVENT_RESET;
82     HISYSEVENT_UNREGISTER;
83     std::string cachePath(AVSessionUtils::GetCachePathName());
84     AVSessionUtils::DeleteCacheFiles(cachePath);
85 }
86 
CreateSession(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName)87 std::shared_ptr<AVSession> AVSessionManagerImpl::CreateSession(const std::string& tag, int32_t type,
88                                                                const AppExecFwk::ElementName& elementName)
89 {
90     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSession");
91     if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
92         SLOGE("param is invalid");
93         return nullptr;
94     }
95     if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO) {
96         SLOGE("type is invalid");
97         return nullptr;
98     }
99 
100     auto service = GetService();
101     return service ? service->CreateSession(tag, type, elementName) : nullptr;
102 }
103 
GetAllSessionDescriptors(std::vector<AVSessionDescriptor> & descriptors)104 int32_t AVSessionManagerImpl::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
105 {
106     auto service = GetService();
107     return service ? service->GetAllSessionDescriptors(descriptors) : ERR_SERVICE_NOT_EXIST;
108 }
109 
GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor> & activatedSessions)110 int32_t AVSessionManagerImpl::GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions)
111 {
112     std::vector<AVSessionDescriptor> descriptors;
113     int32_t ret = GetAllSessionDescriptors(descriptors);
114     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAllSessionDescriptors failed");
115 
116     for (const auto& descriptor : descriptors) {
117         if (descriptor.isActive_) {
118             activatedSessions.push_back(descriptor);
119         }
120     }
121     return ret;
122 }
123 
GetSessionDescriptorsBySessionId(const std::string & sessionId,AVSessionDescriptor & descriptor)124 int32_t AVSessionManagerImpl::GetSessionDescriptorsBySessionId(const std::string& sessionId,
125                                                                AVSessionDescriptor& descriptor)
126 {
127     if (sessionId.empty()) {
128         SLOGE("sessionId is invalid");
129         return ERR_INVALID_PARAM;
130     }
131 
132     auto service = GetService();
133     return service ? service->GetSessionDescriptorsBySessionId(sessionId, descriptor) : ERR_SERVICE_NOT_EXIST;
134 }
135 
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)136 int32_t AVSessionManagerImpl::CreateController(const std::string& sessionId,
137     std::shared_ptr<AVSessionController>& controller)
138 {
139     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateController");
140     if (sessionId.empty()) {
141         SLOGE("sessionId is invalid");
142         return ERR_INVALID_PARAM;
143     }
144 
145     auto service = GetService();
146     return service ? service->CreateController(sessionId, controller) : ERR_SERVICE_NOT_EXIST;
147 }
148 
RegisterSessionListener(const std::shared_ptr<SessionListener> & listener)149 int32_t AVSessionManagerImpl::RegisterSessionListener(const std::shared_ptr<SessionListener>& listener)
150 {
151     if (listener == nullptr) {
152         SLOGE("listener is nullptr");
153         return ERR_INVALID_PARAM;
154     }
155 
156     auto service = GetService();
157     if (service == nullptr) {
158         return ERR_SERVICE_NOT_EXIST;
159     }
160 
161     if (listener_ != nullptr) {
162         return ERR_SESSION_LISTENER_EXIST;
163     }
164 
165     listener_ = new(std::nothrow) SessionListenerClient(listener);
166     if (listener_ == nullptr) {
167         return ERR_NO_MEMORY;
168     }
169     auto ret = service->RegisterSessionListener(listener_);
170     if (ret != AVSESSION_SUCCESS) {
171         std::lock_guard<std::mutex> lockGuard(lock_);
172         listener_.clear();
173         return ret;
174     }
175     return AVSESSION_SUCCESS;
176 }
177 
RegisterServiceDeathCallback(const DeathCallback & callback)178 int32_t AVSessionManagerImpl::RegisterServiceDeathCallback(const DeathCallback& callback)
179 {
180     deathCallback_ = callback;
181     return AVSESSION_SUCCESS;
182 }
183 
UnregisterServiceDeathCallback()184 int32_t AVSessionManagerImpl::UnregisterServiceDeathCallback()
185 {
186     deathCallback_ = nullptr;
187     return AVSESSION_SUCCESS;
188 }
189 
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)190 int32_t AVSessionManagerImpl::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
191 {
192     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemAVKeyEvent");
193     if (!keyEvent.IsValid()) {
194         SLOGE("keyEvent is invalid");
195         return ERR_COMMAND_NOT_SUPPORT;
196     }
197 
198     auto service = GetService();
199     return service ? service->SendSystemAVKeyEvent(keyEvent) : ERR_SERVICE_NOT_EXIST;
200 }
201 
SendSystemControlCommand(const AVControlCommand & command)202 int32_t AVSessionManagerImpl::SendSystemControlCommand(const AVControlCommand& command)
203 {
204     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemControlCommand");
205     if (!command.IsValid()) {
206         SLOGE("command is invalid");
207         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "INVALID_COMMAND", "CMD", command.GetCommand(),
208             "ERROR_CODE", ERR_INVALID_PARAM, "ERROR_INFO", "avsessionmanagerimpl command is invalid");
209         return ERR_COMMAND_NOT_SUPPORT;
210     }
211 
212     auto service = GetService();
213     if (service == nullptr) {
214         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "GET_SERVICE_ERROR",
215             "ERROR_CODE", ERR_SERVICE_NOT_EXIST, "ERROR_INFO", "mgrimp sendsystemcontrolcommand get service error");
216         return ERR_SERVICE_NOT_EXIST;
217     }
218     return service->SendSystemControlCommand(command);
219 }
220 
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)221 int32_t AVSessionManagerImpl::CastAudio(const SessionToken& token,
222                                         const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
223 {
224     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudio");
225     CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
226     auto service = GetService();
227     return service ? service->CastAudio(token, descriptors) : ERR_SERVICE_NOT_EXIST;
228 }
229 
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)230 int32_t AVSessionManagerImpl::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
231 {
232     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudioForAll");
233     CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
234     auto service = GetService();
235     return service ? service->CastAudioForAll(descriptors) : ERR_SERVICE_NOT_EXIST;
236 }
237 
RegisterClientDeathObserver()238 void AVSessionManagerImpl::RegisterClientDeathObserver()
239 {
240     clientDeath_ = new(std::nothrow) ClientDeathStub();
241     if (clientDeath_ == nullptr) {
242         SLOGE("malloc failed");
243         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "MALLOC_FAILED",
244             "ERROR_INFO", "avsession manager impl register client death observer malloc failed");
245         return;
246     }
247 
248     if (service_->RegisterClientDeathObserver(clientDeath_) != AVSESSION_SUCCESS) {
249         SLOGE("register failed");
250         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "REGISTER_FAILED",
251             "ERROR_INFO", "avsession manager impl register client death observer register failed");
252         return;
253     }
254     SLOGI("success");
255 }
256 
ServiceDeathRecipient(const std::function<void ()> & callback)257 ServiceDeathRecipient::ServiceDeathRecipient(const std::function<void()>& callback)
258     : callback_(callback)
259 {
260     SLOGD("construct");
261 }
262 
OnRemoteDied(const wptr<IRemoteObject> & object)263 void ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
264 {
265     if (callback_) {
266         callback_();
267     }
268 }
269 } // namespace OHOS::AVSession