1 /*
2 * Copyright (c) 2024 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 "cj_avsession_controller_impl.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <optional>
21 #include <vector>
22 #include <string>
23
24 #include "want.h"
25 #include "avqueue_item.h"
26 #include "avsession_log.h"
27 #include "avsession_manager.h"
28 #include "cj_avsession_utils.h"
29 #include "avsession_errors.h"
30 #include "cj_avsession_controller_callback.h"
31 #include "cj_avsession_controller_impl.h"
32
33 namespace OHOS::AVSession {
34
35 std::map<std::string, std::shared_ptr<CJAVSessionControllerImpl>> CJAVSessionControllerImpl::ControllerList_ = {};
36 std::mutex CJAVSessionControllerImpl::controllerListMutex_;
37
CJAVSessionControllerImpl(std::shared_ptr<AVSessionController> & nativeController)38 CJAVSessionControllerImpl::CJAVSessionControllerImpl(std::shared_ptr<AVSessionController>& nativeController)
39 {
40 if (nativeController) {
41 sessionId_ = nativeController->GetSessionId();
42 }
43 controller_ = nativeController;
44 }
45
~CJAVSessionControllerImpl()46 CJAVSessionControllerImpl::~CJAVSessionControllerImpl() {}
47
48
CJAVSessionControllerInvalidImpl()49 CJAVSessionControllerInvalidImpl::CJAVSessionControllerInvalidImpl() {}
~CJAVSessionControllerInvalidImpl()50 CJAVSessionControllerInvalidImpl::~CJAVSessionControllerInvalidImpl() {}
51
GetInstance(const std::string & sessionId)52 std::shared_ptr<CJAVSessionControllerBase> CJAVSessionControllerImpl::GetInstance(const std::string &sessionId)
53 {
54 std::lock_guard<std::mutex> lock(controllerListMutex_);
55 if (ControllerList_.count(sessionId) == 1) {
56 return std::static_pointer_cast<CJAVSessionControllerBase>(ControllerList_[sessionId]);
57 }
58 return std::static_pointer_cast<CJAVSessionControllerBase>(CJ_AVSESSION_CONTROLLER_INVALID_IMPL);
59 }
60
NewInstance(std::shared_ptr<AVSessionController> & nativeController)61 std::shared_ptr<CJAVSessionControllerImpl> CJAVSessionControllerImpl::NewInstance(
62 std::shared_ptr<AVSessionController> &nativeController)
63 {
64 struct CJAVSessionControllerImplInner: public CJAVSessionControllerImpl {
65 CJAVSessionControllerImplInner(std::shared_ptr<AVSessionController>& nativeController)
66 : CJAVSessionControllerImpl(nativeController) {}
67 };
68 auto controller = std::make_shared<CJAVSessionControllerImplInner>(nativeController);
69 std::lock_guard<std::mutex> lock(controllerListMutex_);
70 ControllerList_[nativeController->GetSessionId()] = controller;
71 return controller;
72 }
73
Destroy()74 int32_t CJAVSessionControllerImpl::Destroy()
75 {
76 {
77 std::lock_guard<std::mutex> lock(controllerListMutex_);
78 ControllerList_.erase(sessionId_);
79 }
80 return controller_->Destroy();
81 }
82
GetAVCallState(CAVCallState & avCallState)83 int32_t CJAVSessionControllerImpl::GetAVCallState(CAVCallState& avCallState)
84 {
85 auto call = [&](AVCallState& native) {
86 return controller_->GetAVCallState(native);
87 };
88 return CJControllerGetterCStruct<AVCallState, CAVCallState>(call, avCallState, "GetAVCallState");
89 }
90
GetAVCallMetaData(CAVCallMetaData & avCallMetadata)91 int32_t CJAVSessionControllerImpl::GetAVCallMetaData(CAVCallMetaData& avCallMetadata)
92 {
93 auto call = [&](AVCallMetaData& native) {
94 return controller_->GetAVCallMetaData(native);
95 };
96 return CJControllerGetterCStruct<AVCallMetaData, CAVCallMetaData>(call, avCallMetadata, "GetAVCallMetaData");
97 }
98
GetAVPlaybackState(CAVPlaybackState & avPlaybackState)99 int32_t CJAVSessionControllerImpl::GetAVPlaybackState(CAVPlaybackState& avPlaybackState)
100 {
101 auto call = [&](AVPlaybackState& native) {
102 return controller_->GetAVPlaybackState(native);
103 };
104 return CJControllerGetterCStruct<AVPlaybackState, CAVPlaybackState>(call, avPlaybackState, "GetAVPlaybackState");
105 }
106
GetAVMetaData(CAVMetaData & avMetadata)107 int32_t CJAVSessionControllerImpl::GetAVMetaData(CAVMetaData& avMetadata)
108 {
109 auto call = [&](AVMetaData& native) {
110 return controller_->GetAVMetaData(native);
111 };
112 return CJControllerGetterCStruct<AVMetaData, CAVMetaData>(call, avMetadata, "GetAVMetaData");
113 }
114
GetOutputDevice(COutputDeviceInfo & outputDeviceInfo)115 int32_t CJAVSessionControllerImpl::GetOutputDevice(COutputDeviceInfo& outputDeviceInfo)
116 {
117 auto call = [&](OutputDeviceInfo& native) {
118 AVSessionDescriptor descriptor;
119 int ret = AVSessionManager::GetInstance().
120 GetSessionDescriptorsBySessionId(controller_->GetSessionId(), descriptor);
121 native = descriptor.outputDeviceInfo_;
122 return ret;
123 };
124 return CJControllerGetterCStruct<OutputDeviceInfo, COutputDeviceInfo>(call, outputDeviceInfo, "GetOutputDevice");
125 }
126
GetRealPlaybackPosition(int64_t & position)127 int32_t CJAVSessionControllerImpl::GetRealPlaybackPosition(int64_t& position)
128 {
129 position = controller_->GetRealPlaybackPosition();
130 return AVSESSION_SUCCESS;
131 }
132
IsActive(bool & isActive)133 int32_t CJAVSessionControllerImpl::IsActive(bool& isActive)
134 {
135 auto call = [&](bool& native) {
136 return controller_->IsSessionActive(native);
137 };
138 return CJControllerGetterCStruct<bool, bool>(call, isActive, "IsActive");
139 }
140
GetValidCommands(CArray & commands)141 int32_t CJAVSessionControllerImpl::GetValidCommands(CArray& commands)
142 {
143 auto call = [&](std::vector<int32_t>& native) {
144 return controller_->GetValidCommands(native);
145 };
146 return CJControllerGetterCStruct<std::vector<int32_t>, CArray>(call, commands, "GetValidCommands");
147 }
148
GetAVQueueItems(CArray & items)149 int32_t CJAVSessionControllerImpl::GetAVQueueItems(CArray& items)
150 {
151 auto call = [&](std::vector<AVQueueItem>& native) {
152 return controller_->GetAVQueueItems(native);
153 };
154 return CJControllerGetterCStruct<std::vector<AVQueueItem>, CArray>(call, items, "GetAVQueueItems");
155 }
156
GetAVQueueTitle(char * & title)157 int32_t CJAVSessionControllerImpl::GetAVQueueTitle(char*& title)
158 {
159 auto call = [&](std::string& native) {
160 return controller_->GetAVQueueTitle(native);
161 };
162 return CJControllerGetterCStruct<std::string, char*>(call, title, "GetAVQueueTitle");
163 }
164
GetExtras(CArray & extras)165 int32_t CJAVSessionControllerImpl::GetExtras(CArray& extras)
166 {
167 auto call = [&](AAFwk::WantParams& native) {
168 return controller_->GetExtras(native);
169 };
170 return CJControllerGetterCStruct<AAFwk::WantParams, CArray>(call, extras, "GetExtras");
171 }
172
GetLaunchAbility(int64_t & abilityId)173 int32_t CJAVSessionControllerImpl::GetLaunchAbility(int64_t& abilityId)
174 {
175 auto call = [&](AbilityRuntime::WantAgent::WantAgent& native) {
176 return controller_->GetLaunchAbility(native);
177 };
178 return CJControllerGetterCStruct<AbilityRuntime::WantAgent::WantAgent, int64_t>(
179 call, abilityId, "GetLaunchAbility");
180 }
181
SendAVKeyEvent(CKeyEvent & event)182 int32_t CJAVSessionControllerImpl::SendAVKeyEvent(CKeyEvent& event)
183 {
184 std::shared_ptr<MMI::KeyEvent> ptr = MMI::KeyEvent::Create();
185 if (ptr == nullptr) {
186 return ERR_NO_MEMORY;
187 }
188 convertCJStructToNative(event, *ptr);
189 int32_t ret = controller_->SendAVKeyEvent(*ptr);
190 if (ret != AVSESSION_SUCCESS) {
191 SLOGE("controller SendAVKeyEvent failed:%{public}d", ret);
192 }
193 return ret;
194 }
195
SendCommonCommand(char * & command,CArray & args)196 int32_t CJAVSessionControllerImpl::SendCommonCommand(char*& command, CArray& args)
197 {
198 if (args.size != 1) {
199 return ERR_INVALID_PARAM;
200 }
201 AAFwk::WantParams commandArgs;
202 convertCJStructToNative(args, commandArgs);
203 int32_t ret = controller_->SendCommonCommand(std::string(command), commandArgs);
204 if (ret != AVSESSION_SUCCESS) {
205 SLOGE("controller SendCommonCommand failed:%{public}d", ret);
206 }
207 return ret;
208 }
209
SendControlCommand(CAVSessionCommand & command)210 int32_t CJAVSessionControllerImpl::SendControlCommand(CAVSessionCommand& command)
211 {
212 auto call = [&](const AVControlCommand& cmd) {
213 return controller_->SendControlCommand(cmd);
214 };
215 return CJAVSessionSetterCStruct<AVControlCommand, CAVSessionCommand>(call, command, "SendControlCommand");
216 }
217
SkipToQueueItem(int32_t & itemId)218 int32_t CJAVSessionControllerImpl::SkipToQueueItem(int32_t& itemId)
219 {
220 auto call = [&](int32_t& itemId) {
221 return controller_->SkipToQueueItem(itemId);
222 };
223 return CJAVSessionSetterCStruct<int32_t, int32_t>(call, itemId, "SkipToQueueItem");
224 }
225
OnEvent(int32_t type,int64_t id)226 int32_t CJAVSessionControllerImpl::OnEvent(int32_t type, int64_t id)
227 {
228 if (controller_ == nullptr) {
229 SLOGE("OnEvent failed : controller is nullptr");
230 return AVSESSION_ERROR;
231 }
232 int32_t ret = AVSESSION_SUCCESS;
233 if (callback_ == nullptr) {
234 callback_ = std::make_shared<CJAVControllerCallback>();
235 int32_t ret = controller_->RegisterCallback(callback_);
236 if (ret != AVSESSION_SUCCESS) {
237 SLOGE("OnEvent failed : register callback failed");
238 return ret;
239 }
240 }
241 callback_->RegisterCallback(type, id);
242 return ret;
243 }
244
OffEvent(int32_t type)245 int32_t CJAVSessionControllerImpl::OffEvent(int32_t type)
246 {
247 if (controller_ == nullptr) {
248 SLOGE("OffEvent failed : controller is nullptr");
249 return AVSESSION_ERROR;
250 }
251 int32_t ret = AVSESSION_SUCCESS;
252 if (callback_ == nullptr) {
253 return ret;
254 }
255 callback_->UnRegisterCallback(type);
256 return ret;
257 }
258
OnEventCallMetadataChange(int32_t type,CParameters * filter,int64_t id)259 int32_t CJAVSessionControllerImpl::OnEventCallMetadataChange(int32_t type, CParameters* filter, int64_t id)
260 {
261 if (controller_ == nullptr) {
262 SLOGE("OnEvent failed : controller is nullptr");
263 return AVSESSION_ERROR;
264 }
265 AVCallMetaData::AVCallMetaMaskType avCallMetaMask;
266 if (filter == nullptr) {
267 SLOGE("filter is nullptr");
268 return AVSESSION_ERROR;
269 } else {
270 if (filter->valueType != I32_PTR_TYPE) {
271 SLOGE("Expect AVCallMetaDataFilter Kind is 'int32_t array', but actual is %{public}d", filter->valueType);
272 return AVSESSION_ERROR;
273 }
274 if (filter->value == nullptr || filter->size == 0) {
275 SLOGE("No filter is provided to set");
276 return AVSESSION_ERROR;
277 }
278 auto head = static_cast<int32_t*>(filter->value);
279 for (int i = 0; i < filter->size; i++) {
280 if (head[i] == AVCallMetaData::AVCALL_META_KEY_MAX) {
281 avCallMetaMask.set();
282 break;
283 } else {
284 avCallMetaMask.set(head[i]);
285 }
286 }
287 auto retConvert = controller_->SetAVCallMetaFilter(avCallMetaMask);
288 if (retConvert != AVSESSION_SUCCESS) {
289 SLOGE("controller SetAVCallMetaFilter failed");
290 return retConvert;
291 }
292 }
293 int32_t ret = AVSESSION_SUCCESS;
294 if (callback_ == nullptr) {
295 callback_ = std::make_shared<CJAVControllerCallback>();
296 int32_t ret = controller_->RegisterCallback(callback_);
297 if (ret != AVSESSION_SUCCESS) {
298 SLOGE("OnEvent failed : register callback failed");
299 return ret;
300 }
301 }
302 callback_->RegisterCallback(type, id);
303 return ret;
304 }
305
OnEventCallStateChange(int32_t type,CParameters * filter,int64_t id)306 int32_t CJAVSessionControllerImpl::OnEventCallStateChange(int32_t type, CParameters* filter, int64_t id)
307 {
308 if (controller_ == nullptr) {
309 SLOGE("OnEvent failed : controller is nullptr");
310 return AVSESSION_ERROR;
311 }
312 AVCallState::AVCallStateMaskType avCallStateMask;
313 if (filter == nullptr) {
314 SLOGE("filter is nullptr");
315 return AVSESSION_ERROR;
316 } else {
317 if (filter->valueType != I32_PTR_TYPE) {
318 SLOGE("Expect AVCallStateFilter Kind is 'int32_t array', but actual is %{public}d", filter->valueType);
319 return AVSESSION_ERROR;
320 }
321 if (filter->value == nullptr || filter->size == 0) {
322 SLOGE("No filter is provided to set");
323 return AVSESSION_ERROR;
324 }
325 auto head = static_cast<int32_t*>(filter->value);
326 for (int i = 0; i < filter->size; i++) {
327 if (head[i] == AVCallState::AVCALL_STATE_KEY_MAX) {
328 avCallStateMask.set();
329 break;
330 } else {
331 avCallStateMask.set(head[i]);
332 }
333 }
334 auto retConvert = controller_->SetAVCallStateFilter(avCallStateMask);
335 if (retConvert != AVSESSION_SUCCESS) {
336 SLOGE("controller SetAVCallStateFilter failed");
337 return retConvert;
338 }
339 }
340 int32_t ret = AVSESSION_SUCCESS;
341 if (callback_ == nullptr) {
342 callback_ = std::make_shared<CJAVControllerCallback>();
343 int32_t ret = controller_->RegisterCallback(callback_);
344 if (ret != AVSESSION_SUCCESS) {
345 SLOGE("OnEvent failed : register callback failed");
346 return ret;
347 }
348 }
349 callback_->RegisterCallback(type, id);
350 return ret;
351 }
352
OnEventPlaybackStateChange(int32_t type,CParameters * filter,int64_t id)353 int32_t CJAVSessionControllerImpl::OnEventPlaybackStateChange(int32_t type, CParameters* filter, int64_t id)
354 {
355 if (controller_ == nullptr) {
356 SLOGE("OnEvent failed : controller is nullptr");
357 return AVSESSION_ERROR;
358 }
359 AVPlaybackState::PlaybackStateMaskType playbackMask;
360 if (filter == nullptr) {
361 SLOGE("filter is nullptr");
362 return AVSESSION_ERROR;
363 } else {
364 if (filter->valueType != I32_PTR_TYPE) {
365 SLOGE("Expect AVPlaybackStateFilter Kind is 'int32_t array', but actual is %{public}d", filter->valueType);
366 return AVSESSION_ERROR;
367 }
368 if (filter->value == nullptr || filter->size == 0) {
369 SLOGE("No filter is provided to set");
370 return AVSESSION_ERROR;
371 }
372 auto head = static_cast<int32_t*>(filter->value);
373 for (int i = 0; i < filter->size; i++) {
374 if (head[i] == AVPlaybackState::PLAYBACK_KEY_MAX) {
375 playbackMask.set();
376 break;
377 } else {
378 playbackMask.set(head[i]);
379 }
380 }
381 auto retConvert = controller_->SetPlaybackFilter(playbackMask);
382 if (retConvert != AVSESSION_SUCCESS) {
383 SLOGE("controller SetPlaybackFilter failed");
384 return retConvert;
385 }
386 }
387 int32_t ret = AVSESSION_SUCCESS;
388 if (callback_ == nullptr) {
389 callback_ = std::make_shared<CJAVControllerCallback>();
390 int32_t ret = controller_->RegisterCallback(callback_);
391 if (ret != AVSESSION_SUCCESS) {
392 SLOGE("OnEvent failed : register callback failed");
393 return ret;
394 }
395 }
396 callback_->RegisterCallback(type, id);
397 return ret;
398 }
399
OnEventMetaDataChang(int32_t type,CParameters * filter,int64_t id)400 int32_t CJAVSessionControllerImpl::OnEventMetaDataChang(int32_t type, CParameters* filter, int64_t id)
401 {
402 if (controller_ == nullptr) {
403 SLOGE("OnEvent failed : controller is nullptr");
404 return AVSESSION_ERROR;
405 }
406 AVMetaData::MetaMaskType metaMask;
407 if (filter == nullptr) {
408 SLOGE("filter is nullptr");
409 return AVSESSION_ERROR;
410 } else {
411 if (filter->valueType != I32_PTR_TYPE) {
412 SLOGE("Expect AVMetaDataFilter Kind is 'int32_t array', but actual is %{public}d", filter->valueType);
413 return AVSESSION_ERROR;
414 }
415 if (filter->value == nullptr || filter->size == 0) {
416 SLOGE("No filter is provided to set");
417 return AVSESSION_ERROR;
418 }
419 auto head = static_cast<int32_t*>(filter->value);
420 for (int i = 0; i < filter->size; i++) {
421 if (head[i] == AVMetaData::META_KEY_MAX) {
422 metaMask.set();
423 break;
424 } else {
425 metaMask.set(head[i]);
426 }
427 }
428 auto retConvert = controller_->SetMetaFilter(metaMask);
429 if (retConvert != AVSESSION_SUCCESS) {
430 SLOGE("controller SetMetaFilter failed");
431 return retConvert;
432 }
433 }
434 int32_t ret = AVSESSION_SUCCESS;
435 if (callback_ == nullptr) {
436 callback_ = std::make_shared<CJAVControllerCallback>();
437 int32_t ret = controller_->RegisterCallback(callback_);
438 if (ret != AVSESSION_SUCCESS) {
439 SLOGE("OnEvent failed : register callback failed");
440 return ret;
441 }
442 }
443 callback_->RegisterCallback(type, id);
444 return ret;
445 }
446 } // namespace OHOS::AVSession