• 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 "avcast_controller_item.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_trace.h"
20 #include "av_router.h"
21 
22 namespace OHOS::AVSession {
AVCastControllerItem()23 AVCastControllerItem::AVCastControllerItem()
24 {
25     SLOGD("AVCastControllerItem construct");
26 }
27 
~AVCastControllerItem()28 AVCastControllerItem::~AVCastControllerItem()
29 {
30     SLOGD("AVCastControllerItem destruct");
31 }
32 
Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy,const std::function<void (int32_t,std::vector<int32_t> &)> & validCommandsChangecallback)33 void AVCastControllerItem::Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy,
34     const std::function<void(int32_t, std::vector<int32_t>&)>& validCommandsChangecallback)
35 {
36     castControllerProxy_ = castControllerProxy;
37     castControllerProxy_->RegisterControllerListener(shared_from_this());
38     validCommandsChangecallback_ = validCommandsChangecallback;
39 }
40 
OnCastPlaybackStateChange(const AVPlaybackState & state)41 void AVCastControllerItem::OnCastPlaybackStateChange(const AVPlaybackState& state)
42 {
43     SLOGI("OnCastPlaybackStateChange with state: %{public}d", state.GetState());
44     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
45     AVPlaybackState stateOut;
46     std::lock_guard lockGuard(itemCallbackLock_);
47     if (state.CopyToByMask(castPlaybackMask_, stateOut)) {
48         SLOGI("update cast playback state");
49         AVSESSION_TRACE_SYNC_START("AVCastControllerItem::OnCastPlaybackStateChange");
50         callback_->OnCastPlaybackStateChange(stateOut);
51     }
52     SLOGI("OnCastPlaybackStateChange done with state: %{public}d", state.GetState());
53 }
54 
OnMediaItemChange(const AVQueueItem & avQueueItem)55 void AVCastControllerItem::OnMediaItemChange(const AVQueueItem& avQueueItem)
56 {
57     SLOGI("OnMediaItemChange");
58     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
59     std::lock_guard lockGuard(itemCallbackLock_);
60     callback_->OnMediaItemChange(avQueueItem);
61     SLOGI("OnMediaItemChange done");
62 }
63 
OnPlayNext()64 void AVCastControllerItem::OnPlayNext()
65 {
66     SLOGI("OnPlayNext");
67     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
68     std::lock_guard lockGuard(itemCallbackLock_);
69     callback_->OnPlayNext();
70 }
71 
OnPlayPrevious()72 void AVCastControllerItem::OnPlayPrevious()
73 {
74     SLOGI("OnPlayPrevious");
75     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
76     std::lock_guard lockGuard(itemCallbackLock_);
77     callback_->OnPlayPrevious();
78 }
79 
OnSeekDone(const int32_t seekNumber)80 void AVCastControllerItem::OnSeekDone(const int32_t seekNumber)
81 {
82     SLOGI("OnSeekDone");
83     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
84     std::lock_guard lockGuard(itemCallbackLock_);
85     callback_->OnSeekDone(seekNumber);
86 }
87 
OnVideoSizeChange(const int32_t width,const int32_t height)88 void AVCastControllerItem::OnVideoSizeChange(const int32_t width, const int32_t height)
89 {
90     SLOGI("OnVideoSizeChange");
91     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
92     std::lock_guard lockGuard(itemCallbackLock_);
93     callback_->OnVideoSizeChange(width, height);
94 }
95 
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)96 void AVCastControllerItem::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
97 {
98     SLOGI("OnPlayerError");
99     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
100     std::lock_guard lockGuard(itemCallbackLock_);
101     callback_->OnPlayerError(errorCode, errorMsg);
102 }
103 
OnEndOfStream(const int32_t isLooping)104 void AVCastControllerItem::OnEndOfStream(const int32_t isLooping)
105 {
106     SLOGI("OnEndOfStream");
107     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
108     std::lock_guard lockGuard(itemCallbackLock_);
109     callback_->OnEndOfStream(isLooping);
110 }
111 
OnPlayRequest(const AVQueueItem & avQueueItem)112 void AVCastControllerItem::OnPlayRequest(const AVQueueItem& avQueueItem)
113 {
114     SLOGI("OnPlayRequest");
115     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
116     std::lock_guard lockGuard(itemCallbackLock_);
117     callback_->OnPlayRequest(avQueueItem);
118 }
119 
SendControlCommand(const AVCastControlCommand & cmd)120 int32_t AVCastControllerItem::SendControlCommand(const AVCastControlCommand& cmd)
121 {
122     SLOGI("Call SendControlCommand of cast controller proxy");
123     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
124     castControllerProxy_->SendControlCommand(cmd);
125     return AVSESSION_SUCCESS;
126 }
127 
Start(const AVQueueItem & avQueueItem)128 int32_t AVCastControllerItem::Start(const AVQueueItem& avQueueItem)
129 {
130     SLOGI("Call Start of cast controller proxy");
131     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
132     castControllerProxy_->Start(avQueueItem);
133     currentAVQueueItem_ = avQueueItem;
134     return AVSESSION_SUCCESS;
135 }
136 
Prepare(const AVQueueItem & avQueueItem)137 int32_t AVCastControllerItem::Prepare(const AVQueueItem& avQueueItem)
138 {
139     SLOGI("Call prepare of cast controller proxy");
140     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
141     castControllerProxy_->Prepare(avQueueItem);
142     return AVSESSION_SUCCESS;
143 }
144 
GetDuration(int32_t & duration)145 int32_t AVCastControllerItem::GetDuration(int32_t& duration)
146 {
147     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
148     return castControllerProxy_->GetDuration(duration);
149 }
150 
GetCastAVPlaybackState(AVPlaybackState & avPlaybackState)151 int32_t AVCastControllerItem::GetCastAVPlaybackState(AVPlaybackState& avPlaybackState)
152 {
153     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
154     return castControllerProxy_->GetCastAVPlaybackState(avPlaybackState);
155 }
156 
GetCurrentItem(AVQueueItem & currentItem)157 int32_t AVCastControllerItem::GetCurrentItem(AVQueueItem& currentItem)
158 {
159     currentItem =  castControllerProxy_->GetCurrentItem();
160     return AVSESSION_SUCCESS;
161 }
162 
GetValidCommands(std::vector<int32_t> & cmds)163 int32_t AVCastControllerItem::GetValidCommands(std::vector<int32_t>& cmds)
164 {
165     validCommandsChangecallback_(AVCastControlCommand::CAST_CONTROL_CMD_MAX, cmds);
166     SLOGI("get available command with size %{public}d", static_cast<int32_t>(cmds.size()));
167     return AVSESSION_SUCCESS;
168 }
169 
SetDisplaySurface(std::string & surfaceId)170 int32_t AVCastControllerItem::SetDisplaySurface(std::string& surfaceId)
171 {
172     CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
173     return castControllerProxy_->SetDisplaySurface(surfaceId);
174 }
175 
SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)176 int32_t AVCastControllerItem::SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
177 {
178     castPlaybackMask_ = filter;
179     return AVSESSION_SUCCESS;
180 }
181 
AddAvailableCommand(const int32_t cmd)182 int32_t AVCastControllerItem::AddAvailableCommand(const int32_t cmd)
183 {
184     SLOGI("add available command %{/public}d", cmd);
185     std::vector<int32_t> cmds(AVCastControlCommand::CAST_CONTROL_CMD_MAX);
186     validCommandsChangecallback_(cmd, cmds);
187     SLOGI("add available command with size %{public}d", static_cast<int32_t>(cmds.size()));
188     if (cmds.empty()) {
189         SLOGI("check is sink session with empty, not set");
190     } else {
191         castControllerProxy_->SetValidAbility(cmds);
192     }
193     return AVSESSION_SUCCESS;
194 }
195 
RemoveAvailableCommand(const int32_t cmd)196 int32_t AVCastControllerItem::RemoveAvailableCommand(const int32_t cmd)
197 {
198     SLOGI("remove available command %{/public}d", cmd);
199     std::vector<int32_t> cmds(AVCastControlCommand::CAST_CONTROL_CMD_MAX);
200     validCommandsChangecallback_(cmd + removeCmdStep_, cmds);
201     SLOGI("remove available command with size %{public}d", static_cast<int32_t>(cmds.size()));
202     if (cmds.empty()) {
203         SLOGI("check is sink session with empty, not set");
204     } else {
205         castControllerProxy_->SetValidAbility(cmds);
206     }
207     return AVSESSION_SUCCESS;
208 }
209 
RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)210 bool AVCastControllerItem::RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)
211 {
212     SLOGI("Call RegisterControllerListener of cast controller proxy");
213     CHECK_AND_RETURN_RET_LOG(castControllerProxy != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
214     return castControllerProxy->RegisterControllerListener(shared_from_this());
215 }
216 
RegisterCallbackInner(const sptr<IRemoteObject> & callback)217 int32_t AVCastControllerItem::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
218 {
219     SLOGI("call RegisterCallbackInner of cast controller proxy");
220     std::lock_guard lockGuard(itemCallbackLock_);
221     callback_ = iface_cast<AVCastControllerCallbackProxy>(callback);
222     CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
223     return AVSESSION_SUCCESS;
224 }
225 
Destroy()226 int32_t AVCastControllerItem::Destroy()
227 {
228     SLOGI("Start cast controller destroy process");
229     if (castControllerProxy_) {
230         castControllerProxy_ = nullptr;
231     }
232     std::lock_guard lockGuard(itemCallbackLock_);
233     if (callback_) {
234         callback_ = nullptr;
235     }
236     return AVSESSION_SUCCESS;
237 }
238 } // namespace OHOS::AVSession
239