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