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_callback_client.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_event_handler.h"
20
21 namespace OHOS::AVSession {
AVCastControllerCallbackClient(const std::shared_ptr<AVCastControllerCallback> & callback)22 AVCastControllerCallbackClient::AVCastControllerCallbackClient(
23 const std::shared_ptr<AVCastControllerCallback>& callback) : callback_(callback)
24 {
25 SLOGD("construct");
26 }
27
OnCastPlaybackStateChange(const AVPlaybackState & state)28 ErrCode AVCastControllerCallbackClient::OnCastPlaybackStateChange(const AVPlaybackState& state)
29 {
30 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
31
32 auto callback = callback_;
33 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
34 .AVSessionPostTask([callback, state]() { callback->OnCastPlaybackStateChange(state); }, EVENT_NAME),
35 "AVCastControllerCallbackClient handler postTask failed");
36 if (castPlaybackStateListener_) {
37 castPlaybackStateListener_(state);
38 }
39 SLOGI("OnCastPlaybackStateChange done with state: %{public}d", state.GetState());
40 return AVSESSION_SUCCESS;
41 }
42
OnMediaItemChange(const AVQueueItem & avQueueItem)43 ErrCode AVCastControllerCallbackClient::OnMediaItemChange(const AVQueueItem& avQueueItem)
44 {
45 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
46
47 auto callback = callback_;
48 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
49 .AVSessionPostTask([callback, avQueueItem]() { callback->OnMediaItemChange(avQueueItem); }, EVENT_NAME),
50 "AVCastControllerCallbackClient handler postTask failed");
51 SLOGI("OnCastPlaybackStateChange done");
52 return AVSESSION_SUCCESS;
53 }
54
OnPlayNext()55 ErrCode AVCastControllerCallbackClient::OnPlayNext()
56 {
57 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
58
59 auto callback = callback_;
60 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
61 .AVSessionPostTask([callback]() { callback->OnPlayNext(); }, EVENT_NAME),
62 "AVCastControllerCallbackClient handler postTask failed");
63 return AVSESSION_SUCCESS;
64 }
65
OnPlayPrevious()66 ErrCode AVCastControllerCallbackClient::OnPlayPrevious()
67 {
68 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
69
70 auto callback = callback_;
71 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
72 .AVSessionPostTask([callback]() { callback->OnPlayPrevious(); }, EVENT_NAME),
73 "AVCastControllerCallbackClient handler postTask failed");
74 return AVSESSION_SUCCESS;
75 }
76
OnSeekDone(const int32_t seekNumber)77 ErrCode AVCastControllerCallbackClient::OnSeekDone(const int32_t seekNumber)
78 {
79 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
80
81 auto callback = callback_;
82 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
83 .AVSessionPostTask([callback, seekNumber]() { callback->OnSeekDone(seekNumber); }, EVENT_NAME),
84 "AVCastControllerCallbackClient handler postTask failed");
85 return AVSESSION_SUCCESS;
86 }
87
OnVideoSizeChange(const int32_t width,const int32_t height)88 ErrCode AVCastControllerCallbackClient::OnVideoSizeChange(const int32_t width, const int32_t height)
89 {
90 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
91
92 auto callback = callback_;
93 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
94 .AVSessionPostTask([callback, width, height]() {
95 callback->OnVideoSizeChange(width, height);
96 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
97 return AVSESSION_SUCCESS;
98 }
99
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)100 ErrCode AVCastControllerCallbackClient::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
101 {
102 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
103
104 auto callback = callback_;
105 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
106 .AVSessionPostTask([callback, errorCode, errorMsg]() {
107 callback->OnPlayerError(errorCode, errorMsg);
108 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
109 return AVSESSION_SUCCESS;
110 }
111
OnEndOfStream(const int32_t isLooping)112 ErrCode AVCastControllerCallbackClient::OnEndOfStream(const int32_t isLooping)
113 {
114 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
115
116 auto callback = callback_;
117 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
118 .AVSessionPostTask([callback, isLooping]() {
119 callback->OnEndOfStream(isLooping);
120 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
121 return AVSESSION_SUCCESS;
122 }
123
OnPlayRequest(const AVQueueItem & avQueueItem)124 ErrCode AVCastControllerCallbackClient::OnPlayRequest(const AVQueueItem& avQueueItem)
125 {
126 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
127
128 auto callback = callback_;
129 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
130 .AVSessionPostTask([callback, avQueueItem]() {
131 callback->OnPlayRequest(avQueueItem);
132 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
133 return AVSESSION_SUCCESS;
134 }
135
OnKeyRequest(const std::string & assetId,const std::vector<uint8_t> & keyRequestData)136 ErrCode AVCastControllerCallbackClient::OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>&
137 keyRequestData)
138 {
139 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
140
141 auto callback = callback_;
142 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
143 .AVSessionPostTask([callback, assetId, keyRequestData]() {
144 callback->OnKeyRequest(assetId, keyRequestData);
145 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
146 return AVSESSION_SUCCESS;
147 }
148
OnCastValidCommandChanged(const std::vector<int32_t> & cmds)149 ErrCode AVCastControllerCallbackClient::OnCastValidCommandChanged(const std::vector<int32_t> &cmds)
150 {
151 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
152 SLOGI("OnCastValidCommandChanged begin, cmd size:%{public}zd", cmds.size());
153 auto callback = callback_;
154 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance().AVSessionPostTask(
155 [callback, cmds]() { callback->OnCastValidCommandChanged(cmds); }, EVENT_NAME),
156 "AVCastControllerCallbackClient handler postTask failed");
157 SLOGI("OnCastValidCommandChanged end");
158 return AVSESSION_SUCCESS;
159 }
160
onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase> & mem,uint32_t length,int64_t pos,int32_t & result)161 ErrCode AVCastControllerCallbackClient::onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase>& mem,
162 uint32_t length, int64_t pos, int32_t& result)
163 {
164 auto callback = callback_;
165 callback->onDataSrcRead(mem, length, pos, result);
166 return AVSESSION_SUCCESS;
167 }
168
OnCustomData(const AAFwk::WantParams & data)169 ErrCode AVCastControllerCallbackClient::OnCustomData(const AAFwk::WantParams& data)
170 {
171 CHECK_AND_RETURN_RET_LOG(callback_, AVSESSION_ERROR, "callback is null");
172
173 auto callback = callback_;
174 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
175 .AVSessionPostTask([callback, data]() { callback->OnCustomData(data); }, EVENT_NAME),
176 "AVCastControllerCallbackClient handler postTask failed");
177 return AVSESSION_SUCCESS;
178 }
179
AddListenerForCastPlaybackState(const std::function<void (const AVPlaybackState &)> & listener)180 void AVCastControllerCallbackClient::AddListenerForCastPlaybackState(
181 const std::function<void(const AVPlaybackState&)>& listener)
182 {
183 castPlaybackStateListener_ = listener;
184 }
185
~AVCastControllerCallbackClient()186 AVCastControllerCallbackClient::~AVCastControllerCallbackClient()
187 {
188 AVSessionEventHandler::GetInstance().AVSessionRemoveTask(EVENT_NAME);
189 SLOGD("destroy");
190 }
191 }
192