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
16 #include "avrouter_impl.h"
17 #include "ipc_skeleton.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21 #include "permission_checker.h"
22 #include "avcast_provider_manager.h"
23 #include "hw_cast_provider.h"
24
25 namespace OHOS::AVSession {
AVRouterImpl()26 AVRouterImpl::AVRouterImpl()
27 {
28 SLOGD("AVRouter construct");
29 }
30
Init(IAVSessionServiceListener * servicePtr)31 void AVRouterImpl::Init(IAVSessionServiceListener *servicePtr)
32 {
33 SLOGI("Start init AVRouter");
34 {
35 std::lock_guard lockGuard(servicePtrLock_);
36 servicePtr_ = servicePtr;
37 }
38 std::shared_ptr<HwCastProvider> hwProvider = std::make_shared<HwCastProvider>();
39 hwProvider->Init();
40 providerNumber_++;
41 std::shared_ptr<AVCastProviderManager> avCastProviderManager = std::make_shared<AVCastProviderManager>();
42 avCastProviderManager->Init(providerNumber_, hwProvider);
43 providerManagerMap_[providerNumber_] = avCastProviderManager;
44 hwProvider->RegisterCastStateListener(avCastProviderManager);
45 }
46
StartCastDiscovery(int32_t castDeviceCapability)47 int32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability)
48 {
49 SLOGI("AVRouterImpl StartCastDiscovery");
50
51 std::lock_guard lockGuard(providerManagerLock_);
52
53 for (const auto& [number, providerManager] : providerManagerMap_) {
54 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
55 AVSESSION_ERROR, "provider is nullptr");
56 providerManager->provider_->StartDiscovery(castDeviceCapability);
57 }
58 return AVSESSION_SUCCESS;
59 }
60
StopCastDiscovery()61 int32_t AVRouterImpl::StopCastDiscovery()
62 {
63 SLOGI("AVRouterImpl StopCastDiscovery");
64
65 std::lock_guard lockGuard(providerManagerLock_);
66
67 for (const auto& [number, providerManager] : providerManagerMap_) {
68 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
69 AVSESSION_ERROR, "provider is nullptr");
70 providerManager->provider_->StopDiscovery();
71 }
72 return AVSESSION_SUCCESS;
73 }
74
SetDiscoverable(const bool enable)75 int32_t AVRouterImpl::SetDiscoverable(const bool enable)
76 {
77 SLOGI("AVRouterImpl SetDiscoverable");
78
79 std::lock_guard lockGuard(providerManagerLock_);
80
81 for (const auto& [number, providerManager] : providerManagerMap_) {
82 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
83 AVSESSION_ERROR, "provider is nullptr");
84 providerManager->provider_->SetDiscoverable(enable);
85 }
86 return AVSESSION_SUCCESS;
87 }
88
OnDeviceAvailable(OutputDeviceInfo & castOutputDeviceInfo)89 int32_t AVRouterImpl::OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo)
90 {
91 SLOGI("AVRouterImpl received OnDeviceAvailable event");
92
93 std::lock_guard lockGuard(servicePtrLock_);
94 if (servicePtr_ == nullptr) {
95 return ERR_SERVICE_NOT_EXIST;
96 }
97 servicePtr_->NotifyDeviceAvailable(castOutputDeviceInfo);
98 return AVSESSION_SUCCESS;
99 }
100
ReleaseCurrentCastSession()101 void AVRouterImpl::ReleaseCurrentCastSession()
102 {
103 SLOGI("Start ReleaseCurrentCastSession");
104 std::lock_guard lockGuard(servicePtrLock_);
105 servicePtr_->ReleaseCastSession();
106 }
107
OnCastSessionCreated(const int32_t castId)108 int32_t AVRouterImpl::OnCastSessionCreated(const int32_t castId)
109 {
110 SLOGI("AVRouterImpl On cast session created, cast id is %{public}d", castId);
111
112 int64_t castHandle = -1;
113 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(1) !=
114 providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
115 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[1] != nullptr &&
116 providerManagerMap_[1]->provider_ ! = nullptr, AVSESSION_ERROR, "provider is nullptr");
117 int64_t tempId = 1;
118 castHandle = (tempId << 32) | castId; // The first 32 bits are providerId, the last 32 bits are castId
119 {
120 std::lock_guard lockGuard(servicePtrLock_);
121 servicePtr_->CreateSessionByCast(castHandle);
122 }
123 OutputDeviceInfo outputDeviceInfo;
124 castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
125 return AVSESSION_SUCCESS;
126 }
127
OnDeviceOffline(const std::string & deviceId)128 int32_t AVRouterImpl::OnDeviceOffline(const std::string& deviceId)
129 {
130 SLOGI("AVRouterImpl received OnDeviceOffline event");
131
132 std::lock_guard lockGuard(servicePtrLock_);
133 if (servicePtr_ == nullptr) {
134 return ERR_SERVICE_NOT_EXIST;
135 }
136 servicePtr_->NotifyDeviceOffline(deviceId);
137 return AVSESSION_SUCCESS;
138 }
139
OnCastServerDied(int32_t providerNumber)140 int32_t AVRouterImpl::OnCastServerDied(int32_t providerNumber)
141 {
142 SLOGI("AVRouterImpl received OnCastServerDied event");
143
144 if (providerManagerMap_.find(providerNumber) != providerManagerMap_.end()) {
145 providerManagerMap_.erase(providerNumber);
146 } else {
147 return AVSESSION_ERROR;
148 }
149 return AVSESSION_SUCCESS;
150 }
151
GetRemoteController(const int64_t castHandle)152 std::shared_ptr<IAVCastControllerProxy> AVRouterImpl::GetRemoteController(const int64_t castHandle)
153 {
154 SLOGI("AVRouterImpl start get remote controller process");
155
156 // The first 32 bits are providerId, the last 32 bits are castId
157 int32_t providerNumber = castHandle >> 32;
158 SLOGD("Get remote controller of provider %{public}d", providerNumber);
159 // The first 32 bits are providerId, the last 32 bits are castId
160 int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
161 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
162 nullptr, "Can not find corresponding provider");
163 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr &&
164 providerManagerMap_[providerNumber]->provider_ != nullptr, nullptr, "provider is nullptr");
165 return providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
166 }
167
StartCast(const OutputDeviceInfo & outputDeviceInfo)168 int64_t AVRouterImpl::StartCast(const OutputDeviceInfo& outputDeviceInfo)
169 {
170 SLOGI("AVRouterImpl start cast process");
171
172 int64_t castHandle = -1;
173 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(outputDeviceInfo.deviceInfos_[0].providerId_) !=
174 providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
175 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_] != nullptr
176 && providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_ != nullptr,
177 AVSESSION_ERROR, "provider is nullptr");
178 int32_t castId = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->StartCastSession();
179 int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
180 castHandle = (tempId << 32) | castId; // The first 32 bits are providerId, the last 32 bits are castId
181 return castHandle;
182 }
183
AddDevice(const int32_t castId,const OutputDeviceInfo & outputDeviceInfo)184 int32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
185 {
186 SLOGI("AVRouterImpl AddDevice process");
187 providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
188 outputDeviceInfo.deviceInfos_[0]);
189 castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
190 return AVSESSION_SUCCESS;
191 }
192
StopCast(const int64_t castHandle)193 int32_t AVRouterImpl::StopCast(const int64_t castHandle)
194 {
195 SLOGI("AVRouterImpl stop cast process");
196
197 int32_t providerNumber = static_cast<int32_t>(castHandle >> 32);
198 SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
199 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
200 castHandle, "Can not find corresponding provider");
201 // The first 32 bits are providerId, the last 32 bits are castId
202 int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
203 SLOGI("Stop cast, the castId is %{public}d", castId);
204 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
205 AVSESSION_ERROR, "Can not find corresponding castId");
206 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
207 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
208 providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
209 castHandleToOutputDeviceMap_[castId].deviceInfos_[0]);
210
211 return AVSESSION_SUCCESS;
212 }
213
StopCastSession(const int64_t castHandle)214 int32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
215 {
216 SLOGI("AVRouterImpl stop cast process");
217
218 int32_t providerNumber = static_cast<int32_t>(castHandle >> 32);
219
220 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
221 castHandle, "Can not find corresponding provider");
222 // The first 32 bits are providerId, the last 32 bits are castId
223 int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
224 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
225 AVSESSION_ERROR, "Can not find corresponding castId");
226 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
227 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
228 providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
229
230 return AVSESSION_SUCCESS;
231 }
232
RegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)233 int32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)
234 {
235 SLOGI("AVRouterImpl register IAVCastSessionStateListener callback to provider");
236 // The first 32 bits are providerId, the last 32 bits are castId
237 int32_t providerNumber = castHandle >> 32;
238 // The first 32 bits are providerId, the last 32 bits are castId
239 int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
240 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
241 AVSESSION_ERROR, "Can not find corresponding provider");
242 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
243 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
244 providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, callback);
245 SLOGD("AVRouter impl register callback finished");
246 return AVSESSION_SUCCESS;
247 }
248
UnRegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)249 int32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
250 const std::shared_ptr<IAVCastSessionStateListener> callback)
251 {
252 SLOGI("AVRouterImpl UnRegisterCallback IAVCastSessionStateListener callback to provider");
253 // The first 32 bits are providerId, the last 32 bits are castId
254 int32_t providerNumber = castHandle >> 32;
255 // The first 32 bits are providerId, the last 32 bits are castId
256 int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
257 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
258 AVSESSION_ERROR, "Can not find corresponding provider");
259 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
260 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
261 providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId, callback);
262 return AVSESSION_SUCCESS;
263 }
264 } // namespace OHOS::AVSession
265