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 "hw_cast_provider_session.h"
17 #include <thread>
18 #include "avsession_log.h"
19
20 using namespace OHOS::CastEngine;
21
22 namespace OHOS::AVSession {
~HwCastProviderSession()23 HwCastProviderSession::~HwCastProviderSession()
24 {
25 SLOGI("destruct the HwCastProviderSession");
26 Release();
27 }
28
Init()29 void HwCastProviderSession::Init()
30 {
31 SLOGI("Init the HwCastProviderSession");
32 if (castSession_) {
33 castSession_->RegisterListener(shared_from_this());
34 }
35 }
36
Release()37 void HwCastProviderSession::Release()
38 {
39 SLOGI("release the HwCastProviderSession");
40 if (!castSession_) {
41 SLOGE("castSession_ is not exist");
42 return;
43 }
44 castSession_->Release();
45 castSession_ = nullptr;
46 }
47
AddDevice(const std::string deviceId)48 bool HwCastProviderSession::AddDevice(const std::string deviceId)
49 {
50 SLOGI("AddDevice in HwCastProviderSession");
51 if (!castSession_) {
52 SLOGE("castSession_ is not exist");
53 return false;
54 }
55 CastRemoteDevice castRemoteDevice = {};
56 castRemoteDevice.deviceId = deviceId;
57
58 int32_t ret = castSession_->AddDevice(castRemoteDevice);
59 SLOGI("AddDevice in HwCastProviderSession with ret %{public}d", static_cast<int32_t>(ret));
60 return (ret == 0) ? true : false;
61 }
62
RemoveDevice(std::string deviceId)63 bool HwCastProviderSession::RemoveDevice(std::string deviceId)
64 {
65 SLOGI("RemoveDevice in HwCastProviderSession");
66 if (!castSession_) {
67 SLOGE("castSession_ is not exist");
68 return false;
69 }
70
71 return castSession_->RemoveDevice(deviceId);
72 }
73
CreateStreamPlayer()74 std::shared_ptr<CastEngine::IStreamPlayer> HwCastProviderSession::CreateStreamPlayer()
75 {
76 SLOGI("CreateStreamPlayer in HwCastProviderSession");
77 if (!castSession_) {
78 SLOGE("castSession_ is not exist");
79 return nullptr;
80 }
81
82 std::shared_ptr<CastEngine::IStreamPlayer> streamPlayerPtr = nullptr;
83 castSession_->CreateStreamPlayer(streamPlayerPtr);
84 return streamPlayerPtr;
85 }
86
GetRemoteNetWorkId(std::string deviceId, std::string &networkId)87 bool HwCastProviderSession::GetRemoteNetWorkId(std::string deviceId, std::string &networkId)
88 {
89 SLOGI("enter GetRemoteNetWorkId");
90 if (!castSession_) {
91 SLOGE("castSession_ is not exist");
92 return false;
93 }
94 CastRemoteDevice castRemoteDevice = {};
95 castSession_->GetRemoteDeviceInfo(deviceId, castRemoteDevice);
96 networkId = castRemoteDevice.networkId;
97 return true;
98 }
99
SetStreamState(DeviceInfo deviceInfo)100 bool HwCastProviderSession::SetStreamState(DeviceInfo deviceInfo)
101 {
102 std::lock_guard lockGuard(mutex_);
103 for (auto listener : castSessionStateListenerList_) {
104 if (listener != nullptr) {
105 SLOGI("trigger the OnCastStateChange for registered listeners");
106 listener->OnCastStateChange(deviceStateConnection, deviceInfo);
107 }
108 }
109 stashDeviceState_ = deviceStateConnection;
110 stashDeviceId_ = "0";
111 return true;
112 }
113
RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)114 bool HwCastProviderSession::RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
115 {
116 SLOGI("RegisterCastSessionStateListener");
117 if (listener == nullptr) {
118 SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
119 return false;
120 }
121 {
122 std::lock_guard lockGuard(mutex_);
123 if (find(castSessionStateListenerList_.begin(), castSessionStateListenerList_.end(), listener)
124 != castSessionStateListenerList_.end()) {
125 SLOGE("listener is already in castSessionStateListenerList_");
126 return false;
127 }
128 castSessionStateListenerList_.emplace_back(listener);
129 SLOGI("register listener finished with size %{public}d and check stash state %{public}d",
130 static_cast<int>(castSessionStateListenerList_.size()), static_cast<int32_t>(stashDeviceState_));
131 }
132 if (stashDeviceState_ > 0) {
133 DeviceInfo deviceInfo;
134 deviceInfo.deviceId_ = stashDeviceId_;
135 deviceInfo.deviceName_ = "RemoteCast";
136 deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
137 if (listener != nullptr) {
138 SLOGI("retry trigger the OnCastStateChange for registered listeners");
139 listener->OnCastStateChange(static_cast<int>(stashDeviceState_), deviceInfo);
140 }
141 }
142 return true;
143 }
144
UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)145 bool HwCastProviderSession::UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
146 {
147 if (listener == nullptr) {
148 SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
149 return false;
150 }
151 std::lock_guard lockGuard(mutex_);
152 for (auto iter = castSessionStateListenerList_.begin(); iter != castSessionStateListenerList_.end();) {
153 if (*iter == listener) {
154 castSessionStateListenerList_.erase(iter);
155 SLOGI("unRegister finished with size %{public}d", static_cast<int>(castSessionStateListenerList_.size()));
156 return true;
157 } else {
158 ++iter;
159 }
160 }
161
162 return false;
163 }
164
OnDeviceState(const CastEngine::DeviceStateInfo &stateInfo)165 void HwCastProviderSession::OnDeviceState(const CastEngine::DeviceStateInfo &stateInfo)
166 {
167 int32_t deviceState = static_cast<int32_t>(stateInfo.deviceState);
168 std::vector<std::shared_ptr<IAVCastSessionStateListener>> tempListenerList;
169 SLOGI("OnDeviceState from cast %{public}d", static_cast<int>(deviceState));
170 {
171 std::lock_guard lockGuard(mutex_);
172 if (castSessionStateListenerList_.size() == 0) {
173 SLOGI("current has not registered listener, stash state: %{public}d", deviceState);
174 stashDeviceState_ = deviceState;
175 stashDeviceId_ = stateInfo.deviceId;
176 return;
177 }
178 stashDeviceState_ = -1;
179 tempListenerList = castSessionStateListenerList_;
180 }
181 for (auto listener : tempListenerList) {
182 DeviceInfo deviceInfo;
183 deviceInfo.deviceId_ = stateInfo.deviceId;
184 deviceInfo.deviceName_ = "RemoteCast";
185 deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
186 if (listener != nullptr) {
187 SLOGI("trigger the OnCastStateChange for ListSize %{public}d", static_cast<int>(tempListenerList.size()));
188 listener->OnCastStateChange(static_cast<int>(deviceState), deviceInfo);
189 }
190 }
191 }
192
OnEvent(const CastEngine::EventId &eventId, const std::string &jsonParam)193 void HwCastProviderSession::OnEvent(const CastEngine::EventId &eventId, const std::string &jsonParam)
194 {
195 SLOGI("OnEvent from cast with eventId %{public}d, %{public}s", eventId, jsonParam.c_str());
196 std::string jsonStr = jsonParam;
197 std::lock_guard lockGuard(mutex_);
198 for (auto listener : castSessionStateListenerList_) {
199 if (listener != nullptr) {
200 SLOGI("trigger the OnCastEventRecv for ListSize %{public}d",
201 static_cast<int>(castSessionStateListenerList_.size()));
202 listener->OnCastEventRecv(static_cast<int>(eventId), jsonStr);
203 }
204 }
205 }
206 }
207