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 SLOGI("OnCastPlaybackStateChange done with state: %{public}d", state.GetState());
39 }
40
OnMediaItemChange(const AVQueueItem & avQueueItem)41 void AVCastControllerCallbackClient::OnMediaItemChange(const AVQueueItem& avQueueItem)
42 {
43 CHECK_AND_RETURN_LOG(callback_, "callback is null");
44
45 auto callback = callback_;
46 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
47 .AVSessionPostTask([callback, avQueueItem]() { callback->OnMediaItemChange(avQueueItem); }, EVENT_NAME),
48 "AVCastControllerCallbackClient handler postTask failed");
49 SLOGI("OnCastPlaybackStateChange done");
50 }
51
OnPlayNext()52 void AVCastControllerCallbackClient::OnPlayNext()
53 {
54 CHECK_AND_RETURN_LOG(callback_, "callback is null");
55
56 auto callback = callback_;
57 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
58 .AVSessionPostTask([callback]() { callback->OnPlayNext(); }, EVENT_NAME),
59 "AVCastControllerCallbackClient handler postTask failed");
60 }
61
OnPlayPrevious()62 void AVCastControllerCallbackClient::OnPlayPrevious()
63 {
64 CHECK_AND_RETURN_LOG(callback_, "callback is null");
65
66 auto callback = callback_;
67 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
68 .AVSessionPostTask([callback]() { callback->OnPlayPrevious(); }, EVENT_NAME),
69 "AVCastControllerCallbackClient handler postTask failed");
70 }
71
OnSeekDone(const int32_t seekNumber)72 void AVCastControllerCallbackClient::OnSeekDone(const int32_t seekNumber)
73 {
74 CHECK_AND_RETURN_LOG(callback_, "callback is null");
75
76 auto callback = callback_;
77 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
78 .AVSessionPostTask([callback, seekNumber]() { callback->OnSeekDone(seekNumber); }, EVENT_NAME),
79 "AVCastControllerCallbackClient handler postTask failed");
80 }
81
OnVideoSizeChange(const int32_t width,const int32_t height)82 void AVCastControllerCallbackClient::OnVideoSizeChange(const int32_t width, const int32_t height)
83 {
84 CHECK_AND_RETURN_LOG(callback_, "callback is null");
85
86 auto callback = callback_;
87 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
88 .AVSessionPostTask([callback, width, height]() {
89 callback->OnVideoSizeChange(width, height);
90 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
91 }
92
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)93 void AVCastControllerCallbackClient::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
94 {
95 CHECK_AND_RETURN_LOG(callback_, "callback is null");
96
97 auto callback = callback_;
98 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
99 .AVSessionPostTask([callback, errorCode, errorMsg]() {
100 callback->OnPlayerError(errorCode, errorMsg);
101 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
102 }
103
OnEndOfStream(const int32_t isLooping)104 void AVCastControllerCallbackClient::OnEndOfStream(const int32_t isLooping)
105 {
106 CHECK_AND_RETURN_LOG(callback_, "callback is null");
107
108 auto callback = callback_;
109 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
110 .AVSessionPostTask([callback, isLooping]() {
111 callback->OnEndOfStream(isLooping);
112 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
113 }
114
OnPlayRequest(const AVQueueItem & avQueueItem)115 void AVCastControllerCallbackClient::OnPlayRequest(const AVQueueItem& avQueueItem)
116 {
117 CHECK_AND_RETURN_LOG(callback_, "callback is null");
118
119 auto callback = callback_;
120 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
121 .AVSessionPostTask([callback, avQueueItem]() {
122 callback->OnPlayRequest(avQueueItem);
123 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
124 }
125
OnKeyRequest(const std::string & assetId,const std::vector<uint8_t> & keyRequestData)126 void AVCastControllerCallbackClient::OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>&
127 keyRequestData)
128 {
129 CHECK_AND_RETURN_LOG(callback_, "callback is null");
130
131 auto callback = callback_;
132 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance()
133 .AVSessionPostTask([callback, assetId, keyRequestData]() {
134 callback->OnKeyRequest(assetId, keyRequestData);
135 }, EVENT_NAME), "AVCastControllerCallbackClient handler postTask failed");
136 }
137
OnCastValidCommandChanged(const std::vector<int32_t> & cmds)138 void AVCastControllerCallbackClient::OnCastValidCommandChanged(const std::vector<int32_t> &cmds)
139 {
140 CHECK_AND_RETURN_LOG(callback_, "callback is null");
141 SLOGI("OnCastValidCommandChanged begin, cmd size:%{public}zd", cmds.size());
142 auto callback = callback_;
143 CHECK_AND_PRINT_LOG(AVSessionEventHandler::GetInstance().AVSessionPostTask(
144 [callback, cmds]() { callback->OnCastValidCommandChanged(cmds); }, EVENT_NAME),
145 "AVCastControllerCallbackClient handler postTask failed");
146 SLOGI("OnCastValidCommandChanged end");
147 }
148
AddListenerForCastPlaybackState(const std::function<void (const AVPlaybackState &)> & listener)149 void AVCastControllerCallbackClient::AddListenerForCastPlaybackState(const std::function<void(const AVPlaybackState&)>&
150 listener)
151 {
152 castPlaybackStateListener_ = listener;
153 }
154
~AVCastControllerCallbackClient()155 AVCastControllerCallbackClient::~AVCastControllerCallbackClient()
156 {
157 AVSessionEventHandler::GetInstance().AVSessionRemoveTask(EVENT_NAME);
158 SLOGD("destroy");
159 }
160 }
161