1 /*
2 * Copyright (C) 2023-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 * Description: supply cast session manager adaptor.
15 * Author: zhangge
16 * Create: 2022-5-29
17 */
18
19 #include "cast_engine_errors.h"
20 #include "cast_engine_log.h"
21 #include "cast_session_manager_adaptor.h"
22 #include "cast_service_listener_impl_stub.h"
23 #include "cast_session.h"
24
25 namespace OHOS {
26 namespace CastEngine {
27 namespace CastEngineClient {
28 DEFINE_CAST_ENGINE_LABEL("Cast-Client-ManagerAdaptor");
29
~CastSessionManagerAdaptor()30 CastSessionManagerAdaptor::~CastSessionManagerAdaptor()
31 {
32 CLOGD("destructor in");
33 UnsubscribeDeathRecipient();
34 }
35
RegisterListener(std::shared_ptr<ICastSessionManagerListener> listener,sptr<IRemoteObject::DeathRecipient> deathRecipient)36 int32_t CastSessionManagerAdaptor::RegisterListener(std::shared_ptr<ICastSessionManagerListener> listener,
37 sptr<IRemoteObject::DeathRecipient> deathRecipient)
38 {
39 sptr<ICastServiceListenerImpl> impl = new (std::nothrow) CastServiceListenerImplStub(listener);
40 if (impl == nullptr) {
41 CLOGE("Failed to malloc service listener");
42 return CAST_ENGINE_ERROR;
43 }
44 auto object = proxy_ ? proxy_->GetSessionManagerService() : nullptr;
45 if (!object) {
46 CLOGW("Failed to get session manager service");
47 return CAST_ENGINE_ERROR;
48 }
49 int32_t ret = proxy_ ? proxy_->RegisterListener(impl) : CAST_ENGINE_ERROR;
50 if (ret != CAST_ENGINE_SUCCESS) {
51 return ret;
52 }
53 if (object->AddDeathRecipient(deathRecipient)) {
54 std::lock_guard<std::mutex> lock(mutex_);
55 deathRecipient_ = deathRecipient;
56 remote_ = object;
57 } else {
58 CLOGE("Add cast engine service death recipient failed");
59 }
60
61 return CAST_ENGINE_SUCCESS;
62 }
63
UnregisterListener()64 int32_t CastSessionManagerAdaptor::UnregisterListener()
65 {
66 UnsubscribeDeathRecipient();
67 return proxy_ ? proxy_->UnregisterListener() : CAST_ENGINE_ERROR;
68 }
69
Release()70 int32_t CastSessionManagerAdaptor::Release()
71 {
72 UnsubscribeDeathRecipient();
73 return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
74 }
75
StartDeviceLogging(int32_t fd,uint32_t maxSize)76 int32_t CastSessionManagerAdaptor::StartDeviceLogging(int32_t fd, uint32_t maxSize)
77 {
78 return proxy_ ? proxy_->StartDeviceLogging(fd, maxSize) : CAST_ENGINE_ERROR;
79 }
80
StartDiscovery(int protocols,std::vector<std::string> drmSchemes)81 int32_t CastSessionManagerAdaptor::StartDiscovery(int protocols, std::vector<std::string> drmSchemes)
82 {
83 return proxy_ ? proxy_->StartDiscovery(protocols, drmSchemes) : CAST_ENGINE_ERROR;
84 }
85
StopDiscovery()86 int32_t CastSessionManagerAdaptor::StopDiscovery()
87 {
88 return proxy_ ? proxy_->StopDiscovery() : CAST_ENGINE_ERROR;
89 }
90
CreateCastSession(const CastSessionProperty & property,std::shared_ptr<ICastSession> & castSession)91 int32_t CastSessionManagerAdaptor::CreateCastSession(const CastSessionProperty &property,
92 std::shared_ptr<ICastSession> &castSession)
93 {
94 if (!proxy_) {
95 CLOGE("proxy is null");
96 return CAST_ENGINE_ERROR;
97 }
98 sptr<ICastSessionImpl> impl;
99 int32_t ret = proxy_->CreateCastSession(property, impl);
100 CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
101 if (impl == nullptr) {
102 CLOGE("cast session is NULL");
103 return CAST_ENGINE_ERROR;
104 }
105
106 auto session = std::make_shared<CastSession>(impl);
107 if (!session) {
108 CLOGE("Failed to malloc cast session");
109 return ERR_NO_MEMORY;
110 }
111 std::string sessionId{};
112 impl->GetSessionId(sessionId);
113 session->SetSessionId(sessionId);
114 castSession = session;
115
116 return ret;
117 }
118
SetLocalDevice(const CastLocalDevice & localDevice)119 int32_t CastSessionManagerAdaptor::SetLocalDevice(const CastLocalDevice &localDevice)
120 {
121 return proxy_ ? proxy_->SetLocalDevice(localDevice) : CAST_ENGINE_ERROR;
122 }
123
SetSinkSessionCapacity(int sessionCapacity)124 int32_t CastSessionManagerAdaptor::SetSinkSessionCapacity(int sessionCapacity)
125 {
126 return proxy_ ? proxy_->SetSinkSessionCapacity(sessionCapacity) : CAST_ENGINE_ERROR;
127 }
128
SetDiscoverable(bool enable)129 int32_t CastSessionManagerAdaptor::SetDiscoverable(bool enable)
130 {
131 return proxy_ ? proxy_->SetDiscoverable(enable) : CAST_ENGINE_ERROR;
132 }
133
GetCastSession(std::string sessionId,std::shared_ptr<ICastSession> & castSession)134 int32_t CastSessionManagerAdaptor::GetCastSession(std::string sessionId, std::shared_ptr<ICastSession> &castSession)
135 {
136 sptr<ICastSessionImpl> impl;
137 int32_t ret = proxy_->GetCastSession(sessionId, impl);
138 CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
139 if (impl == nullptr) {
140 CLOGE("cast session is NULL");
141 return CAST_ENGINE_ERROR;
142 }
143
144 auto session = std::make_shared<CastSession>(impl);
145 if (!session) {
146 CLOGE("Failed to malloc cast session");
147 return CAST_ENGINE_ERROR;
148 }
149 std::string id{};
150 impl->GetSessionId(id);
151 session->SetSessionId(id);
152 castSession = session;
153
154 return ret;
155 }
156
UnsubscribeDeathRecipient()157 void CastSessionManagerAdaptor::UnsubscribeDeathRecipient()
158 {
159 std::lock_guard<std::mutex> lock(mutex_);
160 if (!deathRecipient_) {
161 CLOGE("deathRecipient is null");
162 return;
163 }
164
165 sptr<IRemoteObject> remote = remote_.promote();
166 if (!!remote) {
167 remote->RemoveDeathRecipient(deathRecipient_);
168 }
169
170 deathRecipient_.clear();
171 CLOGD("Unsubscribe Death Recipient Success");
172 }
173 } // namespace CastEngineClient
174 } // namespace CastEngine
175 } // namespace OHOS
176