1 /*
2 * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_PLAYER_CALLBACK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_PLAYER_CALLBACK_H
18
19 #include "base/log/log.h"
20
21 #include "core/common/container_scope.h"
22 #include "core/components/video/video_utils.h"
23 #include "foundation/multimedia/player_framework/interfaces/inner_api/native/player.h"
24
25 namespace OHOS::Ace {
26 namespace {
27
28 constexpr int32_t MILLISECONDS_TO_SECONDS = 1000;
29
ConvertToPlaybackStatus(int32_t status)30 PlaybackStatus ConvertToPlaybackStatus(int32_t status)
31 {
32 PlaybackStatus result = PlaybackStatus::NONE;
33 switch (status) {
34 case OHOS::Media::PLAYER_STATE_ERROR:
35 result = PlaybackStatus::ERROR;
36 break;
37 case OHOS::Media::PLAYER_IDLE:
38 result = PlaybackStatus::IDLE;
39 break;
40 case OHOS::Media::PLAYER_PREPARED:
41 result = PlaybackStatus::PREPARED;
42 break;
43 case OHOS::Media::PLAYER_STARTED:
44 result = PlaybackStatus::STARTED;
45 break;
46 case OHOS::Media::PLAYER_PAUSED:
47 result = PlaybackStatus::PAUSED;
48 break;
49 case OHOS::Media::PLAYER_STOPPED:
50 result = PlaybackStatus::STOPPED;
51 break;
52 case OHOS::Media::PLAYER_PLAYBACK_COMPLETE:
53 result = PlaybackStatus::PLAYBACK_COMPLETE;
54 break;
55 default:
56 LOGE("status is not supported");
57 break;
58 }
59 return result;
60 }
61
62 } // namespace
63
64 struct MediaPlayerCallback : public Media::PlayerCallback {
65
66 public:
67 using PositionUpdatedEvent = std::function<void(uint32_t)>;
68 using StateChangedEvent = std::function<void(PlaybackStatus)>;
69 using CommonEvent = std::function<void()>;
70
71 MediaPlayerCallback() = default;
MediaPlayerCallbackMediaPlayerCallback72 explicit MediaPlayerCallback(int32_t instanceId)
73 {
74 instanceId_ = instanceId;
75 }
76
77 ~MediaPlayerCallback() = default;
78
79 // Above api9
OnErrorMediaPlayerCallback80 void OnError(int32_t errorCode, const std::string &errorMsg) override
81 {
82 LOGE("OnError callback, errorCode: %{public}d, error message: %{public}s", errorCode, errorMsg.c_str());
83 ContainerScope scope(instanceId_);
84 if (errorEvent_) {
85 errorEvent_();
86 }
87 }
88
89 void OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &InfoBody = {}) override
90 {
91 LOGD("video OnInfo type: %{public}d, extra: %{public}d", type, extra);
92 ContainerScope scope(instanceId_);
93 switch (type) {
94 case OHOS::Media::INFO_TYPE_SEEKDONE:
95 LOGD("video OnSeekDone callback");
96 if (positionUpdatedEvent_) {
97 positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
98 }
99 break;
100 case OHOS::Media::INFO_TYPE_EOS:
101 LOGD("video OnEndOfStream callback");
102 if (endOfStreamEvent_) {
103 endOfStreamEvent_();
104 }
105 break;
106 case OHOS::Media::INFO_TYPE_STATE_CHANGE:
107 LOGD("video OnStateChanged callback");
108 PrintState(static_cast<OHOS::Media::PlayerStates>(extra));
109 if (stateChangedEvent_) {
110 stateChangedEvent_(ConvertToPlaybackStatus(extra));
111 }
112 break;
113 case OHOS::Media::INFO_TYPE_POSITION_UPDATE:
114 LOGD("video INFO_TYPE_POSITION_UPDATE callback");
115 if (positionUpdatedEvent_) {
116 positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
117 }
118 break;
119 case OHOS::Media::INFO_TYPE_RESOLUTION_CHANGE:
120 LOGD("video INFO_TYPE_RESOLUTION_CHANGE callback");
121 if (resolutionChangeEvent_) {
122 resolutionChangeEvent_();
123 }
124 break;
125 case OHOS::Media::INFO_TYPE_MESSAGE:
126 LOGD("OnMessage callback type: %{public}d", extra);
127 if (extra == Media::PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START) {
128 if (startRenderFrameEvent_) {
129 startRenderFrameEvent_();
130 }
131 }
132 break;
133 default:
134 break;
135 }
136 }
137
PrintStateMediaPlayerCallback138 void PrintState(OHOS::Media::PlayerStates state) const
139 {
140 switch (state) {
141 case OHOS::Media::PLAYER_STOPPED:
142 LOGI("State: Stopped");
143 break;
144 case OHOS::Media::PLAYER_PREPARED:
145 LOGI("State: Buffering");
146 break;
147 case OHOS::Media::PLAYER_PAUSED:
148 LOGI("State: Paused");
149 break;
150 case OHOS::Media::PLAYER_STARTED:
151 LOGI("State: Playing");
152 break;
153 default:
154 LOGI("Invalid state");
155 break;
156 }
157 }
158
SetPositionUpdatedEventMediaPlayerCallback159 void SetPositionUpdatedEvent(PositionUpdatedEvent&& positionUpdatedEvent)
160 {
161 positionUpdatedEvent_ = std::move(positionUpdatedEvent);
162 }
163
SetEndOfStreamEventMediaPlayerCallback164 void SetEndOfStreamEvent(CommonEvent&& endOfStreamEvent)
165 {
166 endOfStreamEvent_ = std::move(endOfStreamEvent);
167 }
168
SetStateChangedEventMediaPlayerCallback169 void SetStateChangedEvent(StateChangedEvent&& stateChangedEvent)
170 {
171 stateChangedEvent_ = std::move(stateChangedEvent);
172 }
173
SetErrorEventMediaPlayerCallback174 void SetErrorEvent(CommonEvent&& errorEvent)
175 {
176 errorEvent_ = std::move(errorEvent);
177 }
178
SetResolutionChangeEventMediaPlayerCallback179 void SetResolutionChangeEvent(CommonEvent&& resolutionChangeEvent)
180 {
181 resolutionChangeEvent_ = std::move(resolutionChangeEvent);
182 }
183
SetStartRenderFrameEventMediaPlayerCallback184 void SetStartRenderFrameEvent(CommonEvent&& startRenderFrameEvent)
185 {
186 startRenderFrameEvent_ = std::move(startRenderFrameEvent);
187 }
188
189 private:
190 PositionUpdatedEvent positionUpdatedEvent_;
191 CommonEvent endOfStreamEvent_;
192 StateChangedEvent stateChangedEvent_;
193 CommonEvent errorEvent_;
194 CommonEvent resolutionChangeEvent_;
195 CommonEvent startRenderFrameEvent_;
196 int32_t instanceId_ = -1;
197 };
198
199 } // namespace OHOS::Ace
200
201 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_PLAYER_CALLBACK_H
202