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 <cinttypes>
17 #include <cstdint>
18
19 #include "avsession_log.h"
20 #include "cj_avsession_callback.h"
21 #include "cj_lambda.h"
22 #include "cj_avsession_utils.h"
23
24 namespace OHOS::AVSession {
25 const int8_t PLAY = 0;
26 const int8_t PAUSE = 1;
27 const int8_t STOP = 2;
28 const int8_t PLAY_NEXT = 3;
29 const int8_t PLAY_PREVIOUS = 4;
30 const int8_t FAST_FORWARD = 5;
31 const int8_t REWIND = 6;
32 const int8_t PLAY_FROM_ASSET_ID = 7;
33 const int8_t SEEK = 8;
34 const int8_t SET_SPEED = 9;
35 const int8_t SET_LOOP_MODE = 10;
36 const int8_t TOGGLE_FAVORITE = 11;
37 const int8_t SKIP_TO_QUEUE_ITEM = 12;
38 const int8_t HANDLE_KEY_EVENT = 13;
39 const int8_t OUTPUT_DEVICE_CHANGE = 14;
40 const int8_t COMMON_COMMAND = 15;
41 const int8_t ANSWER = 16;
42 const int8_t HANG_UP = 17;
43 const int8_t TOGGLE_CALL_MUTE = 18;
44 const int8_t CAST_DISPLAY_CHANGE = 19;
45 const int8_t MAX_EVENT_NUM = 20;
46
CJAVSessionCallback()47 CJAVSessionCallback::CJAVSessionCallback()
48 {
49 typeToCallbackMap_[PLAY] = &CJAVSessionCallback::InitPlay;
50 typeToCallbackMap_[PAUSE] = &CJAVSessionCallback::InitPause;
51 typeToCallbackMap_[STOP] = &CJAVSessionCallback::InitStop;
52 typeToCallbackMap_[PLAY_NEXT] = &CJAVSessionCallback::InitPlayNext;
53 typeToCallbackMap_[PLAY_PREVIOUS] = &CJAVSessionCallback::InitPlayPrevious;
54 typeToCallbackMap_[FAST_FORWARD] = &CJAVSessionCallback::InitFastForward;
55 typeToCallbackMap_[REWIND] = &CJAVSessionCallback::InitRewind;
56 typeToCallbackMap_[PLAY_FROM_ASSET_ID] = &CJAVSessionCallback::InitPlayFromAssetId;
57 typeToCallbackMap_[SEEK] = &CJAVSessionCallback::InitSeek;
58 typeToCallbackMap_[SET_SPEED] = &CJAVSessionCallback::InitSetSpeed;
59 typeToCallbackMap_[SET_LOOP_MODE] = &CJAVSessionCallback::InitSetLoopMode;
60 typeToCallbackMap_[TOGGLE_FAVORITE] = &CJAVSessionCallback::InitToggleFavorite;
61 typeToCallbackMap_[SKIP_TO_QUEUE_ITEM] = &CJAVSessionCallback::InitSkipToQueueItem;
62 typeToCallbackMap_[HANDLE_KEY_EVENT] = &CJAVSessionCallback::InitHandleKeyEvent;
63 typeToCallbackMap_[OUTPUT_DEVICE_CHANGE] = &CJAVSessionCallback::InitOutputDeviceChange;
64 typeToCallbackMap_[COMMON_COMMAND] = &CJAVSessionCallback::InitCommonCommand;
65 typeToCallbackMap_[ANSWER] = &CJAVSessionCallback::InitAnswer;
66 typeToCallbackMap_[HANG_UP] = &CJAVSessionCallback::InitHangUp;
67 typeToCallbackMap_[TOGGLE_CALL_MUTE] = &CJAVSessionCallback::InitToggleCallMute;
68 typeToCallbackMap_[CAST_DISPLAY_CHANGE] = &CJAVSessionCallback::InitCastDisplayChange;
69 for (uint32_t i = 0; i < MAX_EVENT_NUM; i++) {
70 callbackMutexMap_.push_back(std::make_shared<std::recursive_mutex>());
71 }
72 }
73
RegisterCallback(int32_t type,int64_t id)74 void CJAVSessionCallback::RegisterCallback(int32_t type, int64_t id)
75 {
76 if (typeToCallbackMap_.count(type) > 0) {
77 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[type]);
78 (this->*(typeToCallbackMap_[type]))(id);
79 } else {
80 SLOGE("INVALID EVENT TYPE");
81 }
82 }
83
UnRegisterCallback(int32_t type)84 void CJAVSessionCallback::UnRegisterCallback(int32_t type)
85 {
86 if (typeToCallbackMap_.count(type) > 0) {
87 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[type]);
88 (this->*(typeToCallbackMap_[type]))(0);
89 } else {
90 SLOGE("INVALID EVENT TYPE");
91 }
92 }
93
InitPlay(int64_t id)94 void CJAVSessionCallback::InitPlay(int64_t id)
95 {
96 if (id == 0) {
97 play = nullptr;
98 return;
99 }
100 auto callback = reinterpret_cast<void(*)()>(id);
101 play = [lambda = CJLambda::Create(callback)]() -> void {
102 lambda();
103 };
104 }
105
InitPause(int64_t id)106 void CJAVSessionCallback::InitPause(int64_t id)
107 {
108 if (id == 0) {
109 pause = nullptr;
110 return;
111 }
112 auto callback = reinterpret_cast<void(*)()>(id);
113 pause = [lambda = CJLambda::Create(callback)]() -> void {
114 lambda();
115 };
116 }
117
InitStop(int64_t id)118 void CJAVSessionCallback::InitStop(int64_t id)
119 {
120 if (id == 0) {
121 stop = nullptr;
122 return;
123 }
124 auto callback = reinterpret_cast<void(*)()>(id);
125 stop = [lambda = CJLambda::Create(callback)]() -> void {
126 lambda();
127 };
128 }
129
InitPlayNext(int64_t id)130 void CJAVSessionCallback::InitPlayNext(int64_t id)
131 {
132 if (id == 0) {
133 playNext = nullptr;
134 return;
135 }
136 auto callback = reinterpret_cast<void(*)()>(id);
137 playNext = [lambda = CJLambda::Create(callback)]() -> void {
138 lambda();
139 };
140 }
141
InitPlayPrevious(int64_t id)142 void CJAVSessionCallback::InitPlayPrevious(int64_t id)
143 {
144 if (id == 0) {
145 playPrevious = nullptr;
146 return;
147 }
148 auto callback = reinterpret_cast<void(*)()>(id);
149 playPrevious = [lambda = CJLambda::Create(callback)]() -> void {
150 lambda();
151 };
152 }
153
InitFastForward(int64_t id)154 void CJAVSessionCallback::InitFastForward(int64_t id)
155 {
156 if (id == 0) {
157 fastForward = nullptr;
158 return;
159 }
160 auto callback = reinterpret_cast<void(*)(int64_t)>(id);
161 fastForward = [lambda = CJLambda::Create(callback)](int64_t time) -> void {
162 lambda(time);
163 };
164 }
165
InitRewind(int64_t id)166 void CJAVSessionCallback::InitRewind(int64_t id)
167 {
168 if (id == 0) {
169 rewind = nullptr;
170 return;
171 }
172 auto callback = reinterpret_cast<void(*)(int64_t)>(id);
173 rewind = [lambda = CJLambda::Create(callback)](int64_t time) -> void {
174 lambda(time);
175 };
176 }
177
InitPlayFromAssetId(int64_t id)178 void CJAVSessionCallback::InitPlayFromAssetId(int64_t id)
179 {
180 if (id == 0) {
181 playFromAssetId = nullptr;
182 return;
183 }
184 auto callback = reinterpret_cast<void(*)(int64_t)>(id);
185 playFromAssetId = [lambda = CJLambda::Create(callback)](int64_t assetId) -> void {
186 lambda(assetId);
187 };
188 }
189
InitSeek(int64_t id)190 void CJAVSessionCallback::InitSeek(int64_t id)
191 {
192 if (id == 0) {
193 seek = nullptr;
194 return;
195 }
196 auto callback = reinterpret_cast<void(*)(int64_t)>(id);
197 seek = [lambda = CJLambda::Create(callback)](int64_t time) -> void {
198 lambda(time);
199 };
200 }
201
InitSetSpeed(int64_t id)202 void CJAVSessionCallback::InitSetSpeed(int64_t id)
203 {
204 if (id == 0) {
205 setSpeed = nullptr;
206 return;
207 }
208 auto callback = reinterpret_cast<void(*)(double)>(id);
209 setSpeed = [lambda = CJLambda::Create(callback)](double speed) -> void {
210 lambda(speed);
211 };
212 }
213
InitSetLoopMode(int64_t id)214 void CJAVSessionCallback::InitSetLoopMode(int64_t id)
215 {
216 if (id == 0) {
217 setLoopMode = nullptr;
218 return;
219 }
220 auto callback = reinterpret_cast<void(*)(int32_t)>(id);
221 setLoopMode = [lambda = CJLambda::Create(callback)](int32_t loopMode) -> void {
222 lambda(loopMode);
223 };
224 }
225
InitToggleFavorite(int64_t id)226 void CJAVSessionCallback::InitToggleFavorite(int64_t id)
227 {
228 if (id == 0) {
229 toggleFavorite = nullptr;
230 return;
231 }
232 auto callback = reinterpret_cast<void(*)(const char*)>(id);
233 toggleFavorite = [lambda = CJLambda::Create(callback)](const char* assetId) -> void {
234 lambda(assetId);
235 };
236 }
237
InitSkipToQueueItem(int64_t id)238 void CJAVSessionCallback::InitSkipToQueueItem(int64_t id)
239 {
240 if (id == 0) {
241 skipToQueueItem = nullptr;
242 return;
243 }
244 auto callback = reinterpret_cast<void(*)(int32_t)>(id);
245 skipToQueueItem = [lambda = CJLambda::Create(callback)](int32_t itemId) -> void {
246 lambda(itemId);
247 };
248 }
249
InitHandleKeyEvent(int64_t id)250 void CJAVSessionCallback::InitHandleKeyEvent(int64_t id)
251 {
252 if (id == 0) {
253 handleKeyEvent = nullptr;
254 return;
255 }
256 auto callback = reinterpret_cast<void(*)(CKeyEvent)>(id);
257 handleKeyEvent = [lambda = CJLambda::Create(callback)](CKeyEvent event) -> void {
258 lambda(event);
259 };
260 }
261
InitOutputDeviceChange(int64_t id)262 void CJAVSessionCallback::InitOutputDeviceChange(int64_t id)
263 {
264 if (id == 0) {
265 outputDeviceChange = nullptr;
266 return;
267 }
268 auto callback = reinterpret_cast<void(*)(const int32_t, COutputDeviceInfo)>(id);
269 outputDeviceChange = [lambda = CJLambda::Create(callback)](
270 const int32_t connectionState, COutputDeviceInfo deviceInfo) -> void {
271 lambda(connectionState, deviceInfo);
272 };
273 }
274
InitCommonCommand(int64_t id)275 void CJAVSessionCallback::InitCommonCommand(int64_t id)
276 {
277 if (id == 0) {
278 commonCommand = nullptr;
279 return;
280 }
281 auto callback = reinterpret_cast<void(*)(const char*, CArray)>(id);
282 commonCommand = [lambda = CJLambda::Create(callback)](const char* command, CArray obj) -> void {
283 lambda(command, obj);
284 };
285 }
286
InitAnswer(int64_t id)287 void CJAVSessionCallback::InitAnswer(int64_t id)
288 {
289 if (id == 0) {
290 answer = nullptr;
291 return;
292 }
293 auto callback = reinterpret_cast<void(*)()>(id);
294 answer = [lambda = CJLambda::Create(callback)]() -> void {
295 lambda();
296 };
297 }
298
InitHangUp(int64_t id)299 void CJAVSessionCallback::InitHangUp(int64_t id)
300 {
301 if (id == 0) {
302 hangUp = nullptr;
303 return;
304 }
305 auto callback = reinterpret_cast<void(*)()>(id);
306 hangUp = [lambda = CJLambda::Create(callback)]() -> void {
307 lambda();
308 };
309 }
310
InitToggleCallMute(int64_t id)311 void CJAVSessionCallback::InitToggleCallMute(int64_t id)
312 {
313 if (id == 0) {
314 toggleCallMute = nullptr;
315 return;
316 }
317 auto callback = reinterpret_cast<void(*)()>(id);
318 toggleCallMute = [lambda = CJLambda::Create(callback)]() -> void {
319 lambda();
320 };
321 }
322
InitCastDisplayChange(int64_t id)323 void CJAVSessionCallback::InitCastDisplayChange(int64_t id)
324 {
325 if (id == 0) {
326 castDisplayChange = nullptr;
327 return;
328 }
329 auto callback = reinterpret_cast<void(*)(CCastDisplayInfo)>(id);
330 castDisplayChange = [lambda = CJLambda::Create(callback)](CCastDisplayInfo cdisplayInfo) -> void {
331 lambda(cdisplayInfo);
332 };
333 }
334
335
OnPlay()336 void CJAVSessionCallback::OnPlay()
337 {
338 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[PLAY]);
339 if (play) {
340 SLOGD("play runs");
341 play();
342 }
343 }
344
OnPause()345 void CJAVSessionCallback::OnPause()
346 {
347 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[PAUSE]);
348 if (pause) {
349 SLOGD("pause runs");
350 pause();
351 }
352 }
353
OnStop()354 void CJAVSessionCallback::OnStop()
355 {
356 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[STOP]);
357 if (stop) {
358 SLOGD("stop runs");
359 stop();
360 }
361 }
362
OnPlayNext()363 void CJAVSessionCallback::OnPlayNext()
364 {
365 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[PLAY_NEXT]);
366 if (playNext) {
367 SLOGD("play next runs");
368 playNext();
369 }
370 }
371
OnPlayPrevious()372 void CJAVSessionCallback::OnPlayPrevious()
373 {
374 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[PLAY_PREVIOUS]);
375 if (playPrevious) {
376 SLOGD("play previous runs");
377 playPrevious();
378 }
379 }
380
OnFastForward(int64_t time)381 void CJAVSessionCallback::OnFastForward(int64_t time)
382 {
383 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[FAST_FORWARD]);
384 if (fastForward) {
385 SLOGD("fast forward by %" PRIi64 " ms", time);
386 fastForward(time);
387 }
388 }
389
OnRewind(int64_t time)390 void CJAVSessionCallback::OnRewind(int64_t time)
391 {
392 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[REWIND]);
393 if (rewind) {
394 SLOGD("rewind by %" PRIi64 " ms", time);
395 rewind(time);
396 }
397 }
398
OnSeek(int64_t time)399 void CJAVSessionCallback::OnSeek(int64_t time)
400 {
401 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[SEEK]);
402 if (seek) {
403 SLOGD("seek to %" PRIi64 " ms", time);
404 seek(time);
405 }
406 }
407
OnSetSpeed(double speed)408 void CJAVSessionCallback::OnSetSpeed(double speed)
409 {
410 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[SET_SPEED]);
411 if (setSpeed) {
412 SLOGD("set speed to %f", speed);
413 setSpeed(speed);
414 }
415 }
416
OnSetLoopMode(int32_t loopMode)417 void CJAVSessionCallback::OnSetLoopMode(int32_t loopMode)
418 {
419 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[SET_LOOP_MODE]);
420 if (setLoopMode) {
421 SLOGD("set loop mode to %d", loopMode);
422 setLoopMode(loopMode);
423 }
424 }
425
OnToggleFavorite(const std::string & assertId)426 void CJAVSessionCallback::OnToggleFavorite(const std::string& assertId)
427 {
428 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[TOGGLE_FAVORITE]);
429 if (toggleFavorite) {
430 SLOGD("toggle favorite for assertId: %s", assertId.c_str());
431 int ret = CJNO_ERROR;
432 char* cassertId = nullptr;
433 ret = ConvertNativeToCJStruct(assertId, cassertId);
434 if (ret != CJNO_ERROR) {
435 SLOGD("std::string convert to C Type failed");
436 return ;
437 }
438 toggleFavorite(cassertId);
439 free(cassertId);
440 }
441 }
442
OnMediaKeyEvent(const MMI::KeyEvent & keyEvent)443 void CJAVSessionCallback::OnMediaKeyEvent(const MMI::KeyEvent& keyEvent)
444 {
445 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[HANDLE_KEY_EVENT]);
446 if (handleKeyEvent) {
447 SLOGD("media key event received");
448 CKeyEvent ckeyEvent;
449 int ret = CJNO_ERROR;
450 ret = ConvertNativeToCJStruct(keyEvent, ckeyEvent);
451 if (ret != CJNO_ERROR) {
452 SLOGD("KeyEvent convert to C Type failed");
453 return ;
454 }
455 handleKeyEvent(ckeyEvent);
456 free(ckeyEvent.keys);
457 }
458 }
459
OnOutputDeviceChange(const int32_t connectionState,const OutputDeviceInfo & outputDeviceInfo)460 void CJAVSessionCallback::OnOutputDeviceChange(
461 const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo)
462 {
463 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[OUTPUT_DEVICE_CHANGE]);
464 if (outputDeviceChange) {
465 SLOGD("output device connection state: %d", connectionState);
466 COutputDeviceInfo coutputDeviceInfo;
467 int ret = CJNO_ERROR;
468 ret = ConvertNativeToCJStruct(outputDeviceInfo, coutputDeviceInfo);
469 if (ret != CJNO_ERROR) {
470 SLOGD("OutputDeviceInfo convert to C Type failed");
471 return ;
472 }
473 outputDeviceChange(connectionState, coutputDeviceInfo);
474 cjStructHeapFree(coutputDeviceInfo);
475 }
476 }
477
OnCommonCommand(const std::string & command,const AAFwk::WantParams & commandArgs)478 void CJAVSessionCallback::OnCommonCommand(
479 const std::string& command, const AAFwk::WantParams& commandArgs)
480 {
481 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[COMMON_COMMAND]);
482 if (commonCommand) {
483 SLOGD("common command received");
484 int ret = CJNO_ERROR;
485 char* ccommand = nullptr;
486 ret = ConvertNativeToCJStruct(command, ccommand);
487 if (ret != CJNO_ERROR) {
488 SLOGD("std::string convert to c_str failed");
489 return ;
490 }
491 SLOGD("common command received: %s", ccommand);
492 CArray cArgs;
493 ret = ConvertNativeToCJStruct(commandArgs, cArgs);
494 if (ret != CJNO_ERROR) {
495 SLOGD("AAFwk::WantParams convert to C Type failed");
496 free(ccommand);
497 return ;
498 }
499 commonCommand(ccommand, cArgs);
500 free(ccommand);
501 cjStructHeapFree(cArgs);
502 }
503 }
504
OnSkipToQueueItem(int32_t itemId)505 void CJAVSessionCallback::OnSkipToQueueItem(int32_t itemId)
506 {
507 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[SKIP_TO_QUEUE_ITEM]);
508 if (skipToQueueItem) {
509 SLOGD("skip to queue item %d", itemId);
510 skipToQueueItem(itemId);
511 }
512 }
513
OnAVCallAnswer()514 void CJAVSessionCallback::OnAVCallAnswer()
515 {
516 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[ANSWER]);
517 if (answer) {
518 SLOGD("AV call answered");
519 answer();
520 }
521 }
522
OnAVCallHangUp()523 void CJAVSessionCallback::OnAVCallHangUp()
524 {
525 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[HANG_UP]);
526 if (hangUp) {
527 SLOGD("AV call hung up");
528 hangUp();
529 }
530 }
531
OnAVCallToggleCallMute()532 void CJAVSessionCallback::OnAVCallToggleCallMute()
533 {
534 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[TOGGLE_CALL_MUTE]);
535 if (toggleCallMute) {
536 SLOGD("AV call toggle mute");
537 toggleCallMute();
538 }
539 }
540
OnPlayFromAssetId(int64_t assetId)541 void CJAVSessionCallback::OnPlayFromAssetId(int64_t assetId)
542 {
543 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[PLAY_FROM_ASSET_ID]);
544 if (playFromAssetId) {
545 SLOGD("play from asset id: %" PRId64 "", assetId);
546 playFromAssetId(assetId);
547 }
548 }
549
OnCastDisplayChange(const CastDisplayInfo & castDisplayInfo)550 void CJAVSessionCallback::OnCastDisplayChange(const CastDisplayInfo& castDisplayInfo)
551 {
552 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[CAST_DISPLAY_CHANGE]);
553 if (castDisplayChange) {
554 SLOGD("cast display change");
555 CCastDisplayInfo ccastDisplayInfo;
556 int ret = CJNO_ERROR;
557 ret = ConvertNativeToCJStruct(castDisplayInfo, ccastDisplayInfo);
558 if (ret != CJNO_ERROR) {
559 SLOGD("CastDisplayInfo convert to C Type failed");
560 return ;
561 }
562 castDisplayChange(ccastDisplayInfo);
563 cjStructHeapFree(ccastDisplayInfo);
564 }
565 }
566
OnCastDisplaySizeChange(const CastDisplayInfo & castDisplayInfo)567 void CJAVSessionCallback::OnCastDisplaySizeChange(const CastDisplayInfo& castDisplayInfo)
568 {
569 std::lock_guard<std::recursive_mutex> lock(*callbackMutexMap_[CAST_DISPLAY_CHANGE]);
570 if (castDisplayChange) {
571 SLOGD("cast display size change");
572 CCastDisplayInfo ccastDisplayInfo;
573 int ret = CJNO_ERROR;
574 ret = ConvertNativeToCJStruct(castDisplayInfo, ccastDisplayInfo);
575 if (ret != CJNO_ERROR) {
576 SLOGD("CastDisplayInfo convert to C Type failed");
577 return ;
578 }
579 castDisplaySizeChange(ccastDisplayInfo);
580 }
581 }
582 } // end of avsession namespace