• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetLocalDevice(const CastLocalDevice & localDevice)76 int32_t CastSessionManagerAdaptor::SetLocalDevice(const CastLocalDevice &localDevice)
77 {
78     return proxy_ ? proxy_->SetLocalDevice(localDevice) : CAST_ENGINE_ERROR;
79 }
80 
CreateCastSession(const CastSessionProperty & property,std::shared_ptr<ICastSession> & castSession)81 int32_t CastSessionManagerAdaptor::CreateCastSession(const CastSessionProperty &property,
82     std::shared_ptr<ICastSession> &castSession)
83 {
84     if (!proxy_) {
85         CLOGE("proxy is null");
86         return CAST_ENGINE_ERROR;
87     }
88     sptr<ICastSessionImpl> impl;
89     int32_t ret = proxy_->CreateCastSession(property, impl);
90     CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
91     if (impl == nullptr) {
92         CLOGE("cast session is NULL");
93         return CAST_ENGINE_ERROR;
94     }
95 
96     auto session = std::make_shared<CastSession>(impl);
97     if (!session) {
98         CLOGE("Failed to malloc cast session");
99         return ERR_NO_MEMORY;
100     }
101     std::string sessionId{};
102     impl->GetSessionId(sessionId);
103     session->SetSessionId(sessionId);
104     castSession = session;
105 
106     return ret;
107 }
108 
SetSinkSessionCapacity(int sessionCapacity)109 int32_t CastSessionManagerAdaptor::SetSinkSessionCapacity(int sessionCapacity)
110 {
111     return proxy_ ? proxy_->SetSinkSessionCapacity(sessionCapacity) : CAST_ENGINE_ERROR;
112 }
113 
StartDiscovery(int protocols)114 int32_t CastSessionManagerAdaptor::StartDiscovery(int protocols)
115 {
116     return proxy_ ? proxy_->StartDiscovery(protocols) : CAST_ENGINE_ERROR;
117 }
118 
SetDiscoverable(bool enable)119 int32_t CastSessionManagerAdaptor::SetDiscoverable(bool enable)
120 {
121     return proxy_ ? proxy_->SetDiscoverable(enable) : CAST_ENGINE_ERROR;
122 }
123 
StopDiscovery()124 int32_t CastSessionManagerAdaptor::StopDiscovery()
125 {
126     return proxy_ ? proxy_->StopDiscovery() : CAST_ENGINE_ERROR;
127 }
128 
GetCastSession(std::string sessionId,std::shared_ptr<ICastSession> & castSession)129 int32_t CastSessionManagerAdaptor::GetCastSession(std::string sessionId, std::shared_ptr<ICastSession> &castSession)
130 {
131     sptr<ICastSessionImpl> impl;
132     int32_t ret = proxy_->GetCastSession(sessionId, impl);
133     CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
134     if (impl == nullptr) {
135         CLOGE("cast session is NULL");
136         return CAST_ENGINE_ERROR;
137     }
138 
139     auto session = std::make_shared<CastSession>(impl);
140     if (!session) {
141         CLOGE("Failed to malloc cast session");
142         return CAST_ENGINE_ERROR;
143     }
144     std::string id{};
145     impl->GetSessionId(id);
146     session->SetSessionId(id);
147     castSession = session;
148 
149     return ret;
150 }
151 
UnsubscribeDeathRecipient()152 void CastSessionManagerAdaptor::UnsubscribeDeathRecipient()
153 {
154     std::lock_guard<std::mutex> lock(mutex_);
155     if (!deathRecipient_) {
156         CLOGE("deathRecipient is null");
157         return;
158     }
159     sptr<IRemoteObject> remote = remote_.promote();
160     if (!!remote) {
161         remote->RemoveDeathRecipient(deathRecipient_);
162     }
163     deathRecipient_ = nullptr;
164     CLOGD("Unsubscribe Death Recipient Success");
165 }
166 } // namespace CastEngineClient
167 } // namespace CastEngine
168 } // namespace OHOS
169