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_log.h"
18 #include "avsession_event_handler.h"
19
20 namespace OHOS::AVSession {
AVCastControllerCallbackClient(const std::shared_ptr<AVCastControllerCallback> & callback)21 AVCastControllerCallbackClient::AVCastControllerCallbackClient(
22 const std::shared_ptr<AVCastControllerCallback>& callback) : callback_(callback)
23 {
24 SLOGD("construct");
25 }
26
OnCastPlaybackStateChange(const AVPlaybackState & state)27 void AVCastControllerCallbackClient::OnCastPlaybackStateChange(const AVPlaybackState& state)
28 {
29 CHECK_AND_RETURN_LOG(callback_, "callback is null");
30
31 auto callback = callback_;
32 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
33 .AVSessionPostTask([callback, state]() { callback->OnCastPlaybackStateChange(state); }, EVENT_NAME),
34 "AVCastControllerCallbackClient handler postTask failed");
35 if (castPlaybackStateListener_) {
36 castPlaybackStateListener_(state);
37 }
38 }
39
OnMediaItemChange(const AVQueueItem & avQueueItem)40 void AVCastControllerCallbackClient::OnMediaItemChange(const AVQueueItem& avQueueItem)
41 {
42 CHECK_AND_RETURN_LOG(callback_, "callback is null");
43
44 auto callback = callback_;
45 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
46 .AVSessionPostTask([callback, avQueueItem]() { callback->OnMediaItemChange(avQueueItem); }, EVENT_NAME),
47 "AVCastControllerCallbackClient handler postTask failed");
48 }
49
OnPlayNext()50 void AVCastControllerCallbackClient::OnPlayNext()
51 {
52 CHECK_AND_RETURN_LOG(callback_, "callback is null");
53
54 auto callback = callback_;
55 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
56 .AVSessionPostTask([callback]() { callback->OnPlayNext(); }, EVENT_NAME),
57 "AVCastControllerCallbackClient handler postTask failed");
58 }
59
OnPlayPrevious()60 void AVCastControllerCallbackClient::OnPlayPrevious()
61 {
62 CHECK_AND_RETURN_LOG(callback_, "callback is null");
63
64 auto callback = callback_;
65 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
66 .AVSessionPostTask([callback]() { callback->OnPlayPrevious(); }, EVENT_NAME),
67 "AVCastControllerCallbackClient handler postTask failed");
68 }
69
OnSeekDone(const int32_t seekNumber)70 void AVCastControllerCallbackClient::OnSeekDone(const int32_t seekNumber)
71 {
72 CHECK_AND_RETURN_LOG(callback_, "callback is null");
73
74 auto callback = callback_;
75 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
76 .AVSessionPostTask([callback, seekNumber]() { callback->OnSeekDone(seekNumber); }, EVENT_NAME),
77 "AVCastControllerCallbackClient handler postTask failed");
78 }
79
OnVideoSizeChange(const int32_t width,const int32_t height)80 void AVCastControllerCallbackClient::OnVideoSizeChange(const int32_t width, const int32_t height)
81 {
82 CHECK_AND_RETURN_LOG(callback_, "callback is null");
83
84 auto callback = callback_;
85 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
86 .AVSessionPostTask([callback, width, height]() {
87 callback->OnVideoSizeChange(width, height);
88 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
89 }
90
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)91 void AVCastControllerCallbackClient::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
92 {
93 CHECK_AND_RETURN_LOG(callback_, "callback is null");
94
95 auto callback = callback_;
96 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
97 .AVSessionPostTask([callback, errorCode, errorMsg]() {
98 callback->OnPlayerError(errorCode, errorMsg);
99 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
100 }
101
OnEndOfStream(const int32_t isLooping)102 void AVCastControllerCallbackClient::OnEndOfStream(const int32_t isLooping)
103 {
104 CHECK_AND_RETURN_LOG(callback_, "callback is null");
105
106 auto callback = callback_;
107 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
108 .AVSessionPostTask([callback, isLooping]() {
109 callback->OnEndOfStream(isLooping);
110 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
111 }
112
AddListenerForCastPlaybackState(const std::function<void (const AVPlaybackState &)> & listener)113 void AVCastControllerCallbackClient::AddListenerForCastPlaybackState(const std::function<void(const AVPlaybackState&)>&
114 listener)
115 {
116 castPlaybackStateListener_ = listener;
117 }
118
~AVCastControllerCallbackClient()119 AVCastControllerCallbackClient::~AVCastControllerCallbackClient()
120 {
121 AVSessionEventHandler::GetInstance().AVSessionRemoveTask(EVENT_NAME);
122 SLOGD("destroy");
123 }
124 }
125