• 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     auto self = shared_from_this();
45     std::thread([self]() {
46         self->castSession_->Release();
47         self->castSession_ = nullptr;
48     }).detach();
49 }
50 
AddDevice(const std::string deviceId)51 bool HwCastProviderSession::AddDevice(const std::string deviceId)
52 {
53     SLOGI("AddDevice in HwCastProviderSession");
54     if (!castSession_) {
55         SLOGE("castSession_ is not exist");
56         return false;
57     }
58     CastRemoteDevice castRemoteDevice = {};
59     castRemoteDevice.deviceId = deviceId;
60 
61     return castSession_->AddDevice(castRemoteDevice);
62 }
63 
RemoveDevice(std::string deviceId)64 bool HwCastProviderSession::RemoveDevice(std::string deviceId)
65 {
66     SLOGI("RemoveDevice in HwCastProviderSession");
67     if (!castSession_) {
68         SLOGE("castSession_ is not exist");
69         return false;
70     }
71 
72     return castSession_->RemoveDevice(deviceId);
73 }
74 
CreateStreamPlayer()75 std::shared_ptr<CastEngine::IStreamPlayer> HwCastProviderSession::CreateStreamPlayer()
76 {
77     SLOGI("CreateStreamPlayer in HwCastProviderSession");
78     if (!castSession_) {
79         SLOGE("castSession_ is not exist");
80         return nullptr;
81     }
82 
83     std::shared_ptr<CastEngine::IStreamPlayer> streamPlayerPtr = nullptr;
84     castSession_->CreateStreamPlayer(streamPlayerPtr);
85     return streamPlayerPtr;
86 }
87 
RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)88 bool HwCastProviderSession::RegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
89 {
90     SLOGI("RegisterCastSessionStateListener");
91     if (listener == nullptr) {
92         SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
93         return false;
94     }
95     std::lock_guard<std::mutex> lock(mutex_);
96     if (find(castSessionStateListenerList_.begin(), castSessionStateListenerList_.end(), listener)
97         != castSessionStateListenerList_.end()) {
98         SLOGE("listener is already in castSessionStateListenerList_");
99         return false;
100     }
101     castSessionStateListenerList_.emplace_back(listener);
102     SLOGD("Provider session register cast session state listener finished");
103     return true;
104 }
105 
UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)106 bool HwCastProviderSession::UnRegisterCastSessionStateListener(std::shared_ptr<IAVCastSessionStateListener> listener)
107 {
108     if (listener == nullptr) {
109         SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
110         return false;
111     }
112     std::lock_guard<std::mutex> lock(mutex_);
113     for (auto iter = castSessionStateListenerList_.begin(); iter != castSessionStateListenerList_.end();) {
114         if (*iter == listener) {
115             castSessionStateListenerList_.erase(iter);
116             return true;
117         } else {
118             ++iter;
119         }
120     }
121 
122     return false;
123 }
124 
OnDeviceState(const CastEngine::DeviceStateInfo & stateInfo)125 void HwCastProviderSession::OnDeviceState(const CastEngine::DeviceStateInfo &stateInfo)
126 {
127     SLOGD("OnDeviceState %{public}d", static_cast<int>(stateInfo.deviceState));
128     if (castSessionStateListenerList_.size() == 0) {
129         SLOGI("current has not registered listener");
130         return;
131     }
132     for (auto listener : castSessionStateListenerList_) {
133         DeviceInfo deviceInfo;
134         deviceInfo.deviceId_ = stateInfo.deviceId;
135         if (listener != nullptr) {
136             SLOGI("trigger the OnCastStateChange for registered listeners");
137             listener->OnCastStateChange(static_cast<int>(stateInfo.deviceState), deviceInfo);
138         }
139     }
140 }
141 
OnEvent(const CastEngine::EventId & eventId,const std::string & jsonParam)142 void HwCastProviderSession::OnEvent(const CastEngine::EventId &eventId, const std::string &jsonParam)
143 {
144 }
145 }
146