• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     self->castSession_->Release();
45     self->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 
RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)87 bool HwCastProviderSession::RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
88 {
89     SLOGI("RegisterCastSessionStateListener");
90     if (listener == nullptr) {
91         SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
92         return false;
93     }
94     std::lock_guard lockGuard(mutex_);
95     if (find(castSessionStateListenerList_.begin(), castSessionStateListenerList_.end(), listener)
96         != castSessionStateListenerList_.end()) {
97         SLOGE("listener is already in castSessionStateListenerList_");
98         return false;
99     }
100     castSessionStateListenerList_.emplace_back(listener);
101     SLOGI("register listener finished and check stash state %{public}d", static_cast<int32_t>(stashDeviceState_));
102     if (stashDeviceState_ > 0) {
103         DeviceInfo deviceInfo;
104         deviceInfo.deviceId_ = stashDeviceId_;
105         deviceInfo.deviceName_ = "RemoteCast";
106         deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
107         if (listener != nullptr) {
108             SLOGI("retry trigger the OnCastStateChange for registered listeners");
109             listener->OnCastStateChange(static_cast<int>(stashDeviceState_), deviceInfo);
110         }
111     }
112     return true;
113 }
114 
UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)115 bool HwCastProviderSession::UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
116 {
117     if (listener == nullptr) {
118         SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
119         return false;
120     }
121     std::lock_guard lockGuard(mutex_);
122     for (auto iter = castSessionStateListenerList_.begin(); iter != castSessionStateListenerList_.end();) {
123         if (*iter == listener) {
124             castSessionStateListenerList_.erase(iter);
125             return true;
126         } else {
127             ++iter;
128         }
129     }
130 
131     return false;
132 }
133 
OnDeviceState(const CastEngine::DeviceStateInfo & stateInfo)134 void HwCastProviderSession::OnDeviceState(const CastEngine::DeviceStateInfo &stateInfo)
135 {
136     int32_t deviceState = static_cast<int32_t>(stateInfo.deviceState);
137     SLOGI("OnDeviceState from cast %{public}d", static_cast<int>(deviceState));
138     if (castSessionStateListenerList_.size() == 0) {
139         SLOGI("current has not registered listener, stash state: %{public}d", static_cast<int32_t>(deviceState));
140         stashDeviceState_ = deviceState;
141         stashDeviceId_ = stateInfo.deviceId;
142         return;
143     }
144     stashDeviceState_ = -1;
145     std::lock_guard lockGuard(mutex_);
146     for (auto listener : castSessionStateListenerList_) {
147         DeviceInfo deviceInfo;
148         deviceInfo.deviceId_ = stateInfo.deviceId;
149         deviceInfo.deviceName_ = "RemoteCast";
150         deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
151         if (listener != nullptr) {
152             SLOGI("trigger the OnCastStateChange for registered listeners");
153             listener->OnCastStateChange(static_cast<int>(deviceState), deviceInfo);
154         }
155     }
156 }
157 
CheckProcessDone()158 void HwCastProviderSession::CheckProcessDone()
159 {
160     SLOGI("CheckProcessDone wait lock");
161     std::lock_guard lockGuard(mutex_);
162     SLOGI("CheckProcessDone wait lock done");
163 }
164 
OnEvent(const CastEngine::EventId & eventId,const std::string & jsonParam)165 void HwCastProviderSession::OnEvent(const CastEngine::EventId &eventId, const std::string &jsonParam)
166 {
167     SLOGI("OnEvent from cast with eventId %{public}d, %{public}s", eventId, jsonParam.c_str());
168     std::string jsonStr = jsonParam;
169     for (auto listener : castSessionStateListenerList_) {
170         if (listener != nullptr) {
171             SLOGI("trigger the OnCastEventRecv for registered listeners");
172             listener->OnCastEventRecv(static_cast<int>(eventId), jsonStr);
173         }
174     }
175 }
176 }
177