• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ContainerScope scope(instanceId_);
92         switch (type) {
93             case OHOS::Media::INFO_TYPE_SEEKDONE:
94                 if (positionUpdatedEvent_) {
95                     positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
96                 }
97                 break;
98             case OHOS::Media::INFO_TYPE_EOS:
99                 if (endOfStreamEvent_) {
100                     endOfStreamEvent_();
101                 }
102                 break;
103             case OHOS::Media::INFO_TYPE_STATE_CHANGE:
104                 PrintState(static_cast<OHOS::Media::PlayerStates>(extra));
105                 if (stateChangedEvent_) {
106                     stateChangedEvent_(ConvertToPlaybackStatus(extra));
107                 }
108                 break;
109             case OHOS::Media::INFO_TYPE_POSITION_UPDATE:
110                 if (positionUpdatedEvent_) {
111                     positionUpdatedEvent_(extra / MILLISECONDS_TO_SECONDS);
112                 }
113                 break;
114             case OHOS::Media::INFO_TYPE_RESOLUTION_CHANGE:
115                 if (resolutionChangeEvent_) {
116                     resolutionChangeEvent_();
117                 }
118                 break;
119             case OHOS::Media::INFO_TYPE_MESSAGE:
120                 if (extra == Media::PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START) {
121                     if (startRenderFrameEvent_) {
122                         startRenderFrameEvent_();
123                     }
124                 }
125                 break;
126             default:
127                 break;
128             }
129     }
130 
PrintStateMediaPlayerCallback131     void PrintState(OHOS::Media::PlayerStates state) const
132     {
133         switch (state) {
134             case OHOS::Media::PLAYER_STOPPED:
135                 LOGI("State: Stopped");
136                 break;
137             case OHOS::Media::PLAYER_PREPARED:
138                 LOGI("State: Buffering");
139                 break;
140             case OHOS::Media::PLAYER_PAUSED:
141                 LOGI("State: Paused");
142                 break;
143             case OHOS::Media::PLAYER_STARTED:
144                 LOGI("State: Playing");
145                 break;
146             default:
147                 LOGI("Invalid state");
148                 break;
149         }
150     }
151 
SetPositionUpdatedEventMediaPlayerCallback152     void SetPositionUpdatedEvent(PositionUpdatedEvent&& positionUpdatedEvent)
153     {
154         positionUpdatedEvent_ = std::move(positionUpdatedEvent);
155     }
156 
SetEndOfStreamEventMediaPlayerCallback157     void SetEndOfStreamEvent(CommonEvent&& endOfStreamEvent)
158     {
159         endOfStreamEvent_ = std::move(endOfStreamEvent);
160     }
161 
SetStateChangedEventMediaPlayerCallback162     void SetStateChangedEvent(StateChangedEvent&& stateChangedEvent)
163     {
164         stateChangedEvent_ = std::move(stateChangedEvent);
165     }
166 
SetErrorEventMediaPlayerCallback167     void SetErrorEvent(CommonEvent&& errorEvent)
168     {
169         errorEvent_ = std::move(errorEvent);
170     }
171 
SetResolutionChangeEventMediaPlayerCallback172     void SetResolutionChangeEvent(CommonEvent&& resolutionChangeEvent)
173     {
174         resolutionChangeEvent_ = std::move(resolutionChangeEvent);
175     }
176 
SetStartRenderFrameEventMediaPlayerCallback177     void SetStartRenderFrameEvent(CommonEvent&& startRenderFrameEvent)
178     {
179         startRenderFrameEvent_ = std::move(startRenderFrameEvent);
180     }
181 
182 private:
183     PositionUpdatedEvent positionUpdatedEvent_;
184     CommonEvent endOfStreamEvent_;
185     StateChangedEvent stateChangedEvent_;
186     CommonEvent errorEvent_;
187     CommonEvent resolutionChangeEvent_;
188     CommonEvent startRenderFrameEvent_;
189     int32_t instanceId_ = -1;
190 };
191 
192 } // namespace OHOS::Ace
193 
194 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_PLAYER_CALLBACK_H
195