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 "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_INITIALIZED:
41             result = PlaybackStatus::INITIALIZED;
42             break;
43         case OHOS::Media::PLAYER_PREPARED:
44             result = PlaybackStatus::PREPARED;
45             break;
46         case OHOS::Media::PLAYER_STARTED:
47             result = PlaybackStatus::STARTED;
48             break;
49         case OHOS::Media::PLAYER_PAUSED:
50             result = PlaybackStatus::PAUSED;
51             break;
52         case OHOS::Media::PLAYER_STOPPED:
53             result = PlaybackStatus::STOPPED;
54             break;
55         case OHOS::Media::PLAYER_PLAYBACK_COMPLETE:
56             result = PlaybackStatus::PLAYBACK_COMPLETE;
57             break;
58         default:
59             LOGE("status is not supported");
60             break;
61     }
62     return result;
63 }
64 
65 } // namespace
66 
67 struct MediaPlayerCallback : public Media::PlayerCallback {
68 public:
69     using PositionUpdatedEvent = std::function<void(uint32_t)>;
70     using SeekDoneEvent = std::function<void(uint32_t)>;
71     using StateChangedEvent = std::function<void(PlaybackStatus)>;
72     using CommonEvent = std::function<void()>;
73 
74     MediaPlayerCallback() = default;
MediaPlayerCallbackMediaPlayerCallback75     explicit MediaPlayerCallback(int32_t instanceId)
76     {
77         instanceId_ = instanceId;
78     }
79 
80     ~MediaPlayerCallback() = default;
81 
82     // Above api9
OnErrorMediaPlayerCallback83     void OnError(int32_t errorCode, const std::string &errorMsg) override
84     {
85         LOGE("OnError callback, errorCode: %{public}d, error message: %{public}s", errorCode, errorMsg.c_str());
86         ContainerScope scope(instanceId_);
87         if (errorEvent_) {
88             errorEvent_();
89         }
90     }
91 
92     void OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &InfoBody = {}) override
93     {
94         ContainerScope scope(instanceId_);
95         switch (type) {
96             case OHOS::Media::INFO_TYPE_SEEKDONE:
97                 if (seekDoneEvent_) {
98                     seekDoneEvent_(extra / MILLISECONDS_TO_SECONDS);
99                 } else if (positionUpdatedEvent_) {
100                     positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
101                 }
102                 break;
103             case OHOS::Media::INFO_TYPE_EOS:
104                 if (endOfStreamEvent_) {
105                     endOfStreamEvent_();
106                 }
107                 break;
108             case OHOS::Media::INFO_TYPE_STATE_CHANGE:
109                 PrintState(static_cast<OHOS::Media::PlayerStates>(extra));
110                 if (stateChangedEvent_) {
111                     stateChangedEvent_(ConvertToPlaybackStatus(extra));
112                 }
113                 break;
114             case OHOS::Media::INFO_TYPE_POSITION_UPDATE:
115                 if (positionUpdatedEvent_) {
116                     positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
117                 }
118                 break;
119             case OHOS::Media::INFO_TYPE_RESOLUTION_CHANGE:
120                 if (resolutionChangeEvent_) {
121                     resolutionChangeEvent_();
122                 }
123                 break;
124             case OHOS::Media::INFO_TYPE_MESSAGE:
125                 if (extra == Media::PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START) {
126                     if (startRenderFrameEvent_) {
127                         startRenderFrameEvent_();
128                     }
129                 }
130                 break;
131             default:
132                 break;
133             }
134     }
135 
PrintStateMediaPlayerCallback136     void PrintState(OHOS::Media::PlayerStates state) const
137     {
138         switch (state) {
139             case OHOS::Media::PLAYER_STOPPED:
140                 LOGI("State: Stopped");
141                 break;
142             case OHOS::Media::PLAYER_PREPARED:
143                 LOGI("State: Buffering");
144                 break;
145             case OHOS::Media::PLAYER_PAUSED:
146                 LOGI("State: Paused");
147                 break;
148             case OHOS::Media::PLAYER_STARTED:
149                 LOGI("State: Playing");
150                 break;
151             default:
152                 LOGI("Invalid state");
153                 break;
154         }
155     }
156 
SetPositionUpdatedEventMediaPlayerCallback157     void SetPositionUpdatedEvent(PositionUpdatedEvent&& positionUpdatedEvent)
158     {
159         positionUpdatedEvent_ = std::move(positionUpdatedEvent);
160     }
161 
SetEndOfStreamEventMediaPlayerCallback162     void SetEndOfStreamEvent(CommonEvent&& endOfStreamEvent)
163     {
164         endOfStreamEvent_ = std::move(endOfStreamEvent);
165     }
166 
SetStateChangedEventMediaPlayerCallback167     void SetStateChangedEvent(StateChangedEvent&& stateChangedEvent)
168     {
169         stateChangedEvent_ = std::move(stateChangedEvent);
170     }
171 
SetErrorEventMediaPlayerCallback172     void SetErrorEvent(CommonEvent&& errorEvent)
173     {
174         errorEvent_ = std::move(errorEvent);
175     }
176 
SetResolutionChangeEventMediaPlayerCallback177     void SetResolutionChangeEvent(CommonEvent&& resolutionChangeEvent)
178     {
179         resolutionChangeEvent_ = std::move(resolutionChangeEvent);
180     }
181 
SetStartRenderFrameEventMediaPlayerCallback182     void SetStartRenderFrameEvent(CommonEvent&& startRenderFrameEvent)
183     {
184         startRenderFrameEvent_ = std::move(startRenderFrameEvent);
185     }
186 
SetSeekDoneEventMediaPlayerCallback187     void SetSeekDoneEvent(SeekDoneEvent&& seekDoneEvent)
188     {
189         seekDoneEvent_ = std::move(seekDoneEvent);
190     }
191 
192 private:
193     PositionUpdatedEvent positionUpdatedEvent_;
194     SeekDoneEvent seekDoneEvent_;
195     CommonEvent endOfStreamEvent_;
196     StateChangedEvent stateChangedEvent_;
197     CommonEvent errorEvent_;
198     CommonEvent resolutionChangeEvent_;
199     CommonEvent startRenderFrameEvent_;
200     int32_t instanceId_ = -1;
201 };
202 
203 } // namespace OHOS::Ace
204 
205 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_PLAYER_CALLBACK_H
206