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)33 void AVCastControllerItem::Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)
34 {
35 castControllerProxy_ = castControllerProxy;
36 castControllerProxy_->RegisterControllerListener(shared_from_this());
37 }
38
OnCastPlaybackStateChange(const AVPlaybackState & state)39 void AVCastControllerItem::OnCastPlaybackStateChange(const AVPlaybackState& state)
40 {
41 SLOGI("OnCastPlaybackStateChange");
42 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
43 AVPlaybackState stateOut;
44 if (state.CopyToByMask(castPlaybackMask_, stateOut)) {
45 SLOGI("update cast playback state");
46 AVSESSION_TRACE_SYNC_START("AVCastControllerItem::OnCastPlaybackStateChange");
47 callback_->OnCastPlaybackStateChange(stateOut);
48 }
49 }
50
OnMediaItemChange(const AVQueueItem & avQueueItem)51 void AVCastControllerItem::OnMediaItemChange(const AVQueueItem& avQueueItem)
52 {
53 SLOGI("OnMediaItemChange");
54 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
55 callback_->OnMediaItemChange(avQueueItem);
56 }
57
OnPlayNext()58 void AVCastControllerItem::OnPlayNext()
59 {
60 SLOGI("OnPlayNext");
61 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
62 callback_->OnPlayNext();
63 }
64
OnPlayPrevious()65 void AVCastControllerItem::OnPlayPrevious()
66 {
67 SLOGI("OnPlayPrevious");
68 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
69 callback_->OnPlayPrevious();
70 }
71
OnSeekDone(const int32_t seekNumber)72 void AVCastControllerItem::OnSeekDone(const int32_t seekNumber)
73 {
74 SLOGI("OnSeekDone");
75 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
76 callback_->OnSeekDone(seekNumber);
77 }
78
OnVideoSizeChange(const int32_t width,const int32_t height)79 void AVCastControllerItem::OnVideoSizeChange(const int32_t width, const int32_t height)
80 {
81 SLOGI("OnVideoSizeChange");
82 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
83 callback_->OnVideoSizeChange(width, height);
84 }
85
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)86 void AVCastControllerItem::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
87 {
88 SLOGI("OnPlayerError");
89 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
90 callback_->OnPlayerError(errorCode, errorMsg);
91 }
92
OnEndOfStream(const int32_t isLooping)93 void AVCastControllerItem::OnEndOfStream(const int32_t isLooping)
94 {
95 SLOGI("OnEndOfStream");
96 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
97 callback_->OnEndOfStream(isLooping);
98 }
99
SendControlCommand(const AVCastControlCommand & cmd)100 int32_t AVCastControllerItem::SendControlCommand(const AVCastControlCommand& cmd)
101 {
102 SLOGI("Call SendControlCommand of cast controller proxy");
103 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
104 castControllerProxy_->SendControlCommand(cmd);
105 return AVSESSION_SUCCESS;
106 }
107
Start(const AVQueueItem & avQueueItem)108 int32_t AVCastControllerItem::Start(const AVQueueItem& avQueueItem)
109 {
110 SLOGI("Call Start of cast controller proxy");
111 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
112 castControllerProxy_->Start(avQueueItem);
113 currentAVQueueItem_ = avQueueItem;
114 return AVSESSION_SUCCESS;
115 }
116
Prepare(const AVQueueItem & avQueueItem)117 int32_t AVCastControllerItem::Prepare(const AVQueueItem& avQueueItem)
118 {
119 SLOGI("Call prepare of cast controller proxy");
120 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
121 castControllerProxy_->Prepare(avQueueItem);
122 return AVSESSION_SUCCESS;
123 }
124
GetDuration(int32_t & duration)125 int32_t AVCastControllerItem::GetDuration(int32_t& duration)
126 {
127 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
128 return castControllerProxy_->GetDuration(duration);
129 }
130
GetCastAVPlaybackState(AVPlaybackState & avPlaybackState)131 int32_t AVCastControllerItem::GetCastAVPlaybackState(AVPlaybackState& avPlaybackState)
132 {
133 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
134 return castControllerProxy_->GetCastAVPlaybackState(avPlaybackState);
135 }
136
GetCurrentItem(AVQueueItem & currentItem)137 int32_t AVCastControllerItem::GetCurrentItem(AVQueueItem& currentItem)
138 {
139 currentItem = castControllerProxy_->GetCurrentItem();
140 return AVSESSION_SUCCESS;
141 }
142
SetDisplaySurface(std::string & surfaceId)143 int32_t AVCastControllerItem::SetDisplaySurface(std::string& surfaceId)
144 {
145 CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
146 return castControllerProxy_->SetDisplaySurface(surfaceId);
147 }
148
SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)149 int32_t AVCastControllerItem::SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
150 {
151 castPlaybackMask_ = filter;
152 return AVSESSION_SUCCESS;
153 }
154
RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)155 bool AVCastControllerItem::RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)
156 {
157 SLOGI("Call RegisterControllerListener of cast controller proxy");
158 CHECK_AND_RETURN_RET_LOG(castControllerProxy != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
159 return castControllerProxy->RegisterControllerListener(shared_from_this());
160 }
161
RegisterCallbackInner(const sptr<IRemoteObject> & callback)162 int32_t AVCastControllerItem::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
163 {
164 callback_ = iface_cast<AVCastControllerCallbackProxy>(callback);
165 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
166 return AVSESSION_SUCCESS;
167 }
168
Destroy()169 int32_t AVCastControllerItem::Destroy()
170 {
171 SLOGI("Start cast controller destroy process");
172 if (castControllerProxy_) {
173 castControllerProxy_ = nullptr;
174 }
175 if (callback_) {
176 callback_ = nullptr;
177 }
178 return AVSESSION_SUCCESS;
179 }
180 } // namespace OHOS::AVSession
181