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)65 bool HwCastProviderSession::RemoveDevice(std::string deviceId)
66 {
67     SLOGI("RemoveDevice in HwCastProviderSession");
68     if (!castSession_) {
69         SLOGE("castSession_ is not exist");
70         return false;
71     }
72 
73     return castSession_->RemoveDevice(deviceId);
74 }
75 
CreateStreamPlayer()76 std::shared_ptr<CastEngine::IStreamPlayer> HwCastProviderSession::CreateStreamPlayer()
77 {
78     SLOGI("CreateStreamPlayer in HwCastProviderSession");
79     if (!castSession_) {
80         SLOGE("castSession_ is not exist");
81         return nullptr;
82     }
83 
84     std::shared_ptr<CastEngine::IStreamPlayer> streamPlayerPtr = nullptr;
85     castSession_->CreateStreamPlayer(streamPlayerPtr);
86     return streamPlayerPtr;
87 }
88 
GetRemoteNetWorkId(std::string deviceId,std::string & networkId)89 bool HwCastProviderSession::GetRemoteNetWorkId(std::string deviceId, std::string &networkId)
90 {
91     SLOGI("enter GetRemoteNetWorkId");
92     if (!castSession_) {
93         SLOGE("castSession_ is not exist");
94         return false;
95     }
96     CastRemoteDevice castRemoteDevice = {};
97     castSession_->GetRemoteDeviceInfo(deviceId, castRemoteDevice);
98     networkId = castRemoteDevice.networkId;
99     return true;
100 }
101 
SetStreamState(DeviceInfo deviceInfo)102 bool HwCastProviderSession::SetStreamState(DeviceInfo deviceInfo)
103 {
104     std::lock_guard lockGuard(mutex_);
105     for (auto listener : castSessionStateListenerList_) {
106         if (listener != nullptr) {
107             SLOGI("trigger the OnCastStateChange for registered listeners");
108             listener->OnCastStateChange(deviceStateConnection, deviceInfo);
109         }
110     }
111     stashDeviceState_ = deviceStateConnection;
112     stashDeviceId_ = deviceInfo.deviceId_;
113     return true;
114 }
115 
RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)116 bool HwCastProviderSession::RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
117 {
118     SLOGI("RegisterCastSessionStateListener");
119     if (listener == nullptr) {
120         SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
121         return false;
122     }
123     {
124         std::lock_guard lockGuard(mutex_);
125         if (find(castSessionStateListenerList_.begin(), castSessionStateListenerList_.end(), listener)
126             != castSessionStateListenerList_.end()) {
127             SLOGE("listener is already in castSessionStateListenerList_");
128             return false;
129         }
130         castSessionStateListenerList_.emplace_back(listener);
131         SLOGI("register listener finished with size %{public}d and check stash state %{public}d",
132             static_cast<int>(castSessionStateListenerList_.size()), static_cast<int32_t>(stashDeviceState_));
133     }
134     if (stashDeviceState_ > 0) {
135         DeviceInfo deviceInfo;
136         deviceInfo.deviceId_ = stashDeviceId_;
137         deviceInfo.deviceName_ = "RemoteCast";
138         deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
139         if (listener != nullptr) {
140             SLOGI("retry trigger the OnCastStateChange for registered listeners");
141             listener->OnCastStateChange(static_cast<int>(stashDeviceState_), deviceInfo);
142         }
143     }
144     return true;
145 }
146 
UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)147 bool HwCastProviderSession::UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
148 {
149     if (listener == nullptr) {
150         SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
151         return false;
152     }
153     std::lock_guard lockGuard(mutex_);
154     for (auto iter = castSessionStateListenerList_.begin(); iter != castSessionStateListenerList_.end();) {
155         if (*iter == listener) {
156             castSessionStateListenerList_.erase(iter);
157             SLOGI("unRegister finished with size %{public}d", static_cast<int>(castSessionStateListenerList_.size()));
158             return true;
159         } else {
160             ++iter;
161         }
162     }
163 
164     return false;
165 }
166 
OnDeviceState(const CastEngine::DeviceStateInfo & stateInfo)167 void HwCastProviderSession::OnDeviceState(const CastEngine::DeviceStateInfo &stateInfo)
168 {
169     int32_t deviceState = static_cast<int32_t>(stateInfo.deviceState);
170     std::vector<std::shared_ptr<IAVCastSessionStateListener>> tempListenerList;
171     SLOGI("OnDeviceState from cast %{public}d", static_cast<int>(deviceState));
172     if (stashDeviceState_ == deviceState) {
173         SLOGI("duplicate devicestate");
174         return;
175     }
176     {
177         std::lock_guard lockGuard(mutex_);
178         if (castSessionStateListenerList_.size() == 0) {
179             SLOGI("current has not registered listener, stash state: %{public}d", deviceState);
180             stashDeviceState_ = deviceState;
181             stashDeviceId_ = stateInfo.deviceId;
182             return;
183         }
184         stashDeviceState_ = -1;
185         tempListenerList = castSessionStateListenerList_;
186     }
187     for (auto listener : tempListenerList) {
188         DeviceInfo deviceInfo;
189         deviceInfo.deviceId_ = stateInfo.deviceId;
190         deviceInfo.deviceName_ = "RemoteCast";
191         deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
192         if (listener != nullptr) {
193             SLOGI("trigger the OnCastStateChange for ListSize %{public}d", static_cast<int>(tempListenerList.size()));
194             listener->OnCastStateChange(static_cast<int>(deviceState), deviceInfo);
195         }
196     }
197 }
198 
OnEvent(const CastEngine::EventId & eventId,const std::string & jsonParam)199 void HwCastProviderSession::OnEvent(const CastEngine::EventId &eventId, const std::string &jsonParam)
200 {
201     SLOGI("OnEvent from cast with eventId %{public}d, %{public}s", eventId, jsonParam.c_str());
202     std::string jsonStr = jsonParam;
203     std::lock_guard lockGuard(mutex_);
204     for (auto listener : castSessionStateListenerList_) {
205         if (listener != nullptr) {
206             SLOGI("trigger the OnCastEventRecv for ListSize %{public}d",
207                 static_cast<int>(castSessionStateListenerList_.size()));
208             listener->OnCastEventRecv(static_cast<int>(eventId), jsonStr);
209         }
210     }
211 }
212 }
213