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 <mutex>
17 #include <securec.h>
18 #ifdef SUPPORT_AVPLAYER_DRM
19 #include "native_drm_object.h"
20 #endif
21 #include "media_log.h"
22 #include "media_errors.h"
23 #include "native_mfmagic.h"
24 #include "native_player_magic.h"
25 #include "native_window.h"
26 #include "avplayer.h"
27 namespace {
28     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "NativeAVPlayer"};
29     constexpr uint32_t ERROR_CODE_MAP_LENGTH = 11;
30     constexpr uint32_t ERROR_CODE_API9_MAP_LENGTH = 44;
31     constexpr uint32_t STATE_MAP_LENGTH = 9;
32     constexpr uint32_t INFO_TYPE_LENGTH = 19;
33     constexpr int32_t UNSUPPORT_FORMAT_ERROR_CODE = 331350544;
34     constexpr int32_t AVPLAYER_ERR_UNSUPPORT = 9;
35 }
36 
37 using namespace OHOS::Media;
38 using namespace OHOS::DrmStandard;
39 class NativeAVPlayerCallback;
40 
41 const char* OH_PLAYER_MESSAGE_TYPE = PlayerKeys::PLAYER_MESSAGE_TYPE.data();
42 const char* OH_PLAYER_IS_LIVE_STREAM = PlayerKeys::PLAYER_IS_LIVE_STREAM.data();
43 const char* OH_PLAYER_SEEK_POSITION = PlayerKeys::PLAYER_SEEK_POSITION.data();
44 const char* OH_PLAYER_PLAYBACK_SPEED = PlayerKeys::PLAYER_PLAYBACK_SPEED.data();
45 const char* OH_PLAYER_BITRATE = PlayerKeys::PLAYER_BITRATE_DONE.data();
46 const char* OH_PLAYER_CURRENT_POSITION = PlayerKeys::PLAYER_CURRENT_POSITION.data();
47 const char* OH_PLAYER_DURATION = PlayerKeys::PLAYER_DURATION.data();
48 const char* OH_PLAYER_STATE = PlayerKeys::PLAYER_STATE_CHANGE.data();
49 const char* OH_PLAYER_STATE_CHANGE_REASON = PlayerKeys::PLAYER_STATE_CHANGED_REASON.data();
50 const char* OH_PLAYER_VOLUME = PlayerKeys::PLAYER_VOLUME_LEVEL.data();
51 const char* OH_PLAYER_BITRATE_ARRAY = PlayerKeys::PLAYER_AVAILABLE_BITRATES.data();
52 const char* OH_PLAYER_AUDIO_INTERRUPT_TYPE = PlayerKeys::AUDIO_INTERRUPT_TYPE.data();
53 const char* OH_PLAYER_AUDIO_INTERRUPT_FORCE = PlayerKeys::AUDIO_INTERRUPT_FORCE.data();
54 const char* OH_PLAYER_AUDIO_INTERRUPT_HINT = PlayerKeys::AUDIO_INTERRUPT_HINT.data();
55 const char* OH_PLAYER_AUDIO_DEVICE_CHANGE_REASON = PlayerKeys::AUDIO_DEVICE_CHANGE_REASON.data();
56 const char* OH_PLAYER_BUFFERING_TYPE = PlayerKeys::PLAYER_BUFFERING_TYPE.data();
57 const char* OH_PLAYER_BUFFERING_VALUE = PlayerKeys::PLAYER_BUFFERING_VALUE.data();
58 const char* OH_PLAYER_VIDEO_WIDTH = PlayerKeys::PLAYER_WIDTH.data();
59 const char* OH_PLAYER_VIDEO_HEIGHT = PlayerKeys::PLAYER_HEIGHT.data();
60 const char* OH_PLAYER_TRACK_INDEX = PlayerKeys::PLAYER_TRACK_INDEX.data();
61 const char* OH_PLAYER_TRACK_IS_SELECT = PlayerKeys::PLAYER_IS_SELECT.data();
62 const char* OH_PLAYER_SUBTITLE_TEXT = PlayerKeys::SUBTITLE_TEXT.data();
63 const char* OH_PLAYER_SUBTITLE_PTS = PlayerKeys::SUBTITLE_PTS.data();
64 const char* OH_PLAYER_SUBTITLE_DURATION = PlayerKeys::SUBTITLE_DURATION.data();
65 
66 typedef struct PlayerErrorCodeConvert {
67     MediaServiceExtErrCode errorCodeExt;
68     OH_AVErrCode avErrorCode;
69 } PlayerErrorCodeConvert;
70 
71 typedef struct PlayerErrorCodeApi9Convert {
72     MediaServiceExtErrCodeAPI9 errorCodeApi9;
73     OH_AVErrCode avErrorCode;
74 } PlayerErrorCodeApi9Convert;
75 
76 typedef struct StateConvert {
77     PlayerStates playerStates;
78     AVPlayerState avPlayerState;
79 } StateConvert;
80 
81 typedef struct PlayerOnInfoTypeConvert {
82     PlayerOnInfoType playerOnInfoType;
83     AVPlayerOnInfoType aVPlayerOnInfoType;
84 } PlayerOnInfoTypeConvert;
85 
86 static const PlayerErrorCodeConvert g_errorCodeMap[ERROR_CODE_MAP_LENGTH] = {
87     {MSERR_EXT_OK, AV_ERR_OK},
88     {MSERR_EXT_NO_MEMORY, AV_ERR_NO_MEMORY},
89     {MSERR_EXT_OPERATE_NOT_PERMIT, AV_ERR_OPERATE_NOT_PERMIT},
90     {MSERR_EXT_INVALID_VAL, AV_ERR_INVALID_VAL},
91     {MSERR_EXT_IO, AV_ERR_IO},
92     {MSERR_EXT_TIMEOUT, AV_ERR_TIMEOUT},
93     {MSERR_EXT_UNKNOWN, AV_ERR_UNKNOWN},
94     {MSERR_EXT_SERVICE_DIED, AV_ERR_SERVICE_DIED},
95     {MSERR_EXT_INVALID_STATE, AV_ERR_INVALID_STATE},
96     {MSERR_EXT_UNSUPPORT, AV_ERR_UNSUPPORT},
97     {MSERR_EXT_EXTEND_START, AV_ERR_EXTEND_START},
98 };
99 
100 static const PlayerErrorCodeApi9Convert g_errorCodeApi9Map[ERROR_CODE_API9_MAP_LENGTH] = {
101     {MSERR_EXT_API9_NO_PERMISSION, AV_ERR_OPERATE_NOT_PERMIT},
102     {MSERR_EXT_API9_PERMISSION_DENIED, AV_ERR_OPERATE_NOT_PERMIT},
103     {MSERR_EXT_API9_INVALID_PARAMETER, AV_ERR_INVALID_VAL},
104     {MSERR_EXT_API9_UNSUPPORT_CAPABILITY, AV_ERR_OPERATE_NOT_PERMIT},
105     {MSERR_EXT_API9_NO_MEMORY, AV_ERR_NO_MEMORY},
106     {MSERR_EXT_API9_OPERATE_NOT_PERMIT, AV_ERR_OPERATE_NOT_PERMIT},
107     {MSERR_EXT_API9_IO, AV_ERR_IO},
108     {MSERR_EXT_API9_TIMEOUT, AV_ERR_TIMEOUT},
109     {MSERR_EXT_API9_SERVICE_DIED, AV_ERR_SERVICE_DIED},
110     {MSERR_EXT_API9_UNSUPPORT_FORMAT, AV_ERR_UNSUPPORT},
111     {MSERR_EXT_API9_AUDIO_INTERRUPTED, AV_ERR_OPERATE_NOT_PERMIT},
112     {MSERR_EXT_API14_IO_CANNOT_FIND_HOST, AV_ERR_IO},
113     {MSERR_EXT_API14_IO_CONNECTION_TIMEOUT, AV_ERR_IO},
114     {MSERR_EXT_API14_IO_NETWORK_ABNORMAL, AV_ERR_IO},
115     {MSERR_EXT_API14_IO_NETWORK_UNAVAILABLE, AV_ERR_IO},
116     {MSERR_EXT_API14_IO_NO_PERMISSION, AV_ERR_IO},
117     {MSERR_EXT_API14_IO_NETWORK_ACCESS_DENIED, AV_ERR_IO},
118     {MSERR_EXT_API14_IO_RESOURE_NOT_FOUND, AV_ERR_IO},
119     {MSERR_EXT_API14_IO_SSL_CLIENT_CERT_NEEDED, AV_ERR_IO},
120     {MSERR_EXT_API14_IO_SSL_CONNECT_FAIL, AV_ERR_IO},
121     {MSERR_EXT_API14_IO_SSL_SERVER_CERT_UNTRUSTED, AV_ERR_IO},
122     {MSERR_EXT_API14_IO_UNSUPPORTTED_REQUEST, AV_ERR_IO},
123     {MSERR_EXT_API14_IO_DATA_ABNORMAL, AV_ERR_IO},
124     {MSERR_EXT_API14_IO_FILE_ACCESS_DENIED, AV_ERR_IO},
125     {MSERR_EXT_API14_IO_FILE_BAD_HANDLE, AV_ERR_IO},
126     {MSERR_EXT_API14_IO_FILE_NOT_FOUND, AV_ERR_IO},
127     {MSERR_EXT_API14_IO_FILE_PERMISSION_DENIED, AV_ERR_IO},
128     {MSERR_EXT_API14_IO_AUDIO_DEC_FAILED, AV_ERR_IO},
129     {MSERR_EXT_API14_IO_AUDIO_DEC_INIT_FAILED, AV_ERR_IO},
130     {MSERR_EXT_API14_IO_AUDIO_DEC_UNAVAILABLE, AV_ERR_IO},
131     {MSERR_EXT_API14_IO_AUDIO_DEVICE_ERROR, AV_ERR_IO},
132     {MSERR_EXT_API14_IO_AUDIO_DEVICE_INVALID_STATE, AV_ERR_IO},
133     {MSERR_EXT_API14_IO_AUDIO_DEVICE_TIMEOUT, AV_ERR_IO},
134     {MSERR_EXT_API14_IO_AUDIO_DEVICE_UNAVAILABLE, AV_ERR_IO},
135     {MSERR_EXT_API14_IO_AUDIO_ENC_FAILED, AV_ERR_IO},
136     {MSERR_EXT_API14_IO_AUDIO_ENC_INIT_FAILED, AV_ERR_IO},
137     {MSERR_EXT_API14_IO_AUDIO_ENC_UNAVAILABLE, AV_ERR_IO},
138     {MSERR_EXT_API14_IO_VIDEO_DEC_FAILED, AV_ERR_IO},
139     {MSERR_EXT_API14_IO_VIDEO_DEC_INIT_FAILED, AV_ERR_IO},
140     {MSERR_EXT_API14_IO_VIDEO_DEC_UNAVAILABLE, AV_ERR_IO},
141     {MSERR_EXT_API14_IO_VIDEO_DEVICE_ERROR, AV_ERR_IO},
142     {MSERR_EXT_API14_IO_VIDEO_ENC_FAILED, AV_ERR_IO},
143     {MSERR_EXT_API14_IO_VIDEO_ENC_INIT_FAILED, AV_ERR_IO},
144     {MSERR_EXT_API14_IO_VIDEO_ENC_UNAVAILABLE, AV_ERR_IO},
145 };
146 
147 static const StateConvert g_stateMap[STATE_MAP_LENGTH] = {
148     { PLAYER_STATE_ERROR, AV_ERROR},
149     { PLAYER_IDLE, AV_IDLE },
150     { PLAYER_INITIALIZED, AV_INITIALIZED },
151     { PLAYER_PREPARED, AV_PREPARED },
152     { PLAYER_STARTED, AV_PLAYING },
153     { PLAYER_PAUSED, AV_PAUSED },
154     { PLAYER_STOPPED, AV_STOPPED },
155     { PLAYER_PLAYBACK_COMPLETE, AV_COMPLETED },
156     { PLAYER_RELEASED, AV_RELEASED },
157 };
158 
159 static const PlayerOnInfoTypeConvert g_onInfoType[INFO_TYPE_LENGTH] = {
160     { INFO_TYPE_SEEKDONE, AV_INFO_TYPE_SEEKDONE },
161     { INFO_TYPE_SPEEDDONE, AV_INFO_TYPE_SPEEDDONE },
162     { INFO_TYPE_BITRATEDONE, AV_INFO_TYPE_BITRATEDONE },
163     { INFO_TYPE_EOS, AV_INFO_TYPE_EOS },
164     { INFO_TYPE_STATE_CHANGE, AV_INFO_TYPE_STATE_CHANGE },
165     { INFO_TYPE_POSITION_UPDATE, AV_INFO_TYPE_POSITION_UPDATE },
166     { INFO_TYPE_MESSAGE, AV_INFO_TYPE_MESSAGE },
167     { INFO_TYPE_VOLUME_CHANGE, AV_INFO_TYPE_VOLUME_CHANGE },
168     { INFO_TYPE_RESOLUTION_CHANGE, AV_INFO_TYPE_RESOLUTION_CHANGE },
169     { INFO_TYPE_BUFFERING_UPDATE, AV_INFO_TYPE_BUFFERING_UPDATE },
170     { INFO_TYPE_BITRATE_COLLECT, AV_INFO_TYPE_BITRATE_COLLECT },
171     { INFO_TYPE_INTERRUPT_EVENT, AV_INFO_TYPE_INTERRUPT_EVENT },
172     { INFO_TYPE_DURATION_UPDATE, AV_INFO_TYPE_DURATION_UPDATE },
173     { INFO_TYPE_IS_LIVE_STREAM, AV_INFO_TYPE_IS_LIVE_STREAM },
174     { INFO_TYPE_TRACKCHANGE, AV_INFO_TYPE_TRACKCHANGE },
175     { INFO_TYPE_TRACK_INFO_UPDATE, AV_INFO_TYPE_TRACK_INFO_UPDATE },
176     { INFO_TYPE_SUBTITLE_UPDATE_INFO, AV_INFO_TYPE_SUBTITLE_UPDATE },
177     { INFO_TYPE_AUDIO_DEVICE_CHANGE, AV_INFO_TYPE_AUDIO_OUTPUT_DEVICE_CHANGE},
178 };
179 
MSErrCodeToAVErrCode(MediaServiceErrCode errorCode)180 static OH_AVErrCode MSErrCodeToAVErrCode(MediaServiceErrCode errorCode)
181 {
182     MediaServiceExtErrCode errorCodeExt = MSErrorToExtError(static_cast<MediaServiceErrCode>(errorCode));
183     for (uint32_t i = 0; i < ERROR_CODE_MAP_LENGTH; i++) {
184         if (g_errorCodeMap[i].errorCodeExt == errorCodeExt) {
185             return g_errorCodeMap[i].avErrorCode;
186         }
187     }
188     return AV_ERR_UNKNOWN;
189 }
190 
MSErrCodeToAVErrCodeApi9(MediaServiceExtErrCodeAPI9 errorCode)191 static OH_AVErrCode MSErrCodeToAVErrCodeApi9(MediaServiceExtErrCodeAPI9 errorCode)
192 {
193     for (uint32_t i = 0; i < ERROR_CODE_API9_MAP_LENGTH; i++) {
194         if (g_errorCodeApi9Map[i].errorCodeApi9 == errorCode) {
195             return g_errorCodeApi9Map[i].avErrorCode;
196         }
197     }
198     return AV_ERR_UNKNOWN;
199 }
200 
201 struct PlayerObject : public OH_AVPlayer {
PlayerObjectPlayerObject202     explicit PlayerObject(const std::shared_ptr<Player> &player)
203         : player_(player) {}
204     ~PlayerObject() = default;
205 
206     void StartListenCurrentResource();
207     void PauseListenCurrentResource();
208 
209     const std::shared_ptr<Player> player_ = nullptr;
210     std::shared_ptr<NativeAVPlayerCallback> callback_ = nullptr;
211     std::multimap<std::vector<uint8_t>, std::vector<uint8_t>> localDrmInfos_;
212     std::atomic<bool> isReleased_ = false;
213 };
214 
215 #ifdef SUPPORT_AVPLAYER_DRM
216 class DrmSystemInfoCallback {
217 public:
218     virtual ~DrmSystemInfoCallback() = default;
219     virtual int32_t SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback) = 0;
220     virtual int32_t GetDrmSystemInfos(const Format &infoBody,
221         DRM_MediaKeySystemInfo *mediaKeySystemInfo, struct PlayerObject *playerObj) = 0;
222 };
223 #endif
224 
225 class NativeAVPlayerOnErrorCallback {
226 public:
NativeAVPlayerOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData)227     NativeAVPlayerOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData)
228         : callback_(callback), userData_(userData) {}
229     virtual ~NativeAVPlayerOnErrorCallback() = default;
230 
OnError(OH_AVPlayer *player, int32_t errorCode, const std::string &errorMsg)231     void OnError(OH_AVPlayer *player, int32_t errorCode, const std::string &errorMsg)
232     {
233         CHECK_AND_RETURN(player != nullptr && callback_ != nullptr);
234         callback_(player, errorCode, errorMsg.c_str(), userData_);
235     }
236 
237 private:
238     OH_AVPlayerOnErrorCallback callback_ = nullptr;
239     void *userData_ = nullptr;
240 };
241 
242 class NativeAVPlayerOnInfoCallback {
243 public:
NativeAVPlayerOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData)244     NativeAVPlayerOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData)
245         : callback_(callback), userData_(userData) {}
246     virtual ~NativeAVPlayerOnInfoCallback() = default;
247 
OnInfo(OH_AVPlayer *player, AVPlayerOnInfoType infoType, OH_AVFormat* infoBody)248     void OnInfo(OH_AVPlayer *player, AVPlayerOnInfoType infoType, OH_AVFormat* infoBody)
249     {
250         CHECK_AND_RETURN(player != nullptr && callback_ != nullptr);
251         callback_(player, infoType, infoBody, userData_);
252     }
253 
254 private:
255     OH_AVPlayerOnInfoCallback callback_ = nullptr;
256     void *userData_ = nullptr;
257 };
258 
259 #ifdef SUPPORT_AVPLAYER_DRM
260 class NativeAVPlayerCallback : public PlayerCallback, public DrmSystemInfoCallback {
261 public:
262     using OnInfoFunc = std::function<void(const int32_t, const Format &)>;
263     NativeAVPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback);
264 
265     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
266     void OnError(int32_t errorCode, const std::string &errorMsg) override;
267     int32_t SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback) override;
268     int32_t GetDrmSystemInfos(const Format &infoBody,
269         DRM_MediaKeySystemInfo *mediaKeySystemInfo, struct PlayerObject *playerObj) override;
270 #else
271 class NativeAVPlayerCallback : public PlayerCallback {
272 public:
273     using OnInfoFunc = std::function<void(const int32_t, const Format &)>;
274     NativeAVPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback);
275 
276     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
277     void OnError(int32_t errorCode, const std::string &errorMsg) override;
278 #endif
279 
280 public:
281     int32_t SetPlayCallback(AVPlayerCallback callback);
282     int32_t SetOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData);
283     int32_t SetOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData);
284     void Start();
285     void Pause();
286 
287 private:
288     void OnSeekDoneCb(const int32_t extra, const Format &infoBody);
289     void OnSpeedDoneCb(const int32_t extra, const Format &infoBody);
290     void OnBitRateDoneCb(const int32_t extra, const Format &infoBody);
291     void OnEosCb(const int32_t extra, const Format &infoBody);
292     void OnStateChangeCb(const int32_t extra, const Format &infoBody);
293     void OnPositionUpdateCb(const int32_t extra, const Format &infoBody);
294     void OnVolumeChangeCb(const int32_t extra, const Format &infoBody);
295     void OnMessageCb(const int32_t extra, const Format &infoBody);
296     void OnStartRenderFrameCb() const;
297     void OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody);
298     void OnBufferingUpdateCb(const int32_t extra, const Format &infoBody);
299     void OnBitRateCollectedCb(const int32_t extra, const Format &infoBody);
300     void OnAudioInterruptCb(const int32_t extra, const Format &infoBody);
301     void OnDurationUpdateCb(const int32_t extra, const Format &infoBody);
302     void OnNotifyIsLiveStream(const int32_t extra, const Format &infoBody);
303     void OnTrackChangedCb(const int32_t extra, const Format &infoBody);
304     void OnTrackInfoUpdate(const int32_t extra, const Format &infoBody);
305     void OnSubtitleInfoCb(const int32_t extra, const Format &infoBody);
306     void OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody);
307     void OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody);
308 
309 private:
310     std::mutex mutex_;
311     std::atomic<bool> isReleased_ = false;
312     std::atomic<bool> isSourceLoaded_ = false;
313     struct OH_AVPlayer *player_ = nullptr;
314     std::shared_ptr<NativeAVPlayerOnErrorCallback> errorCallback_ = nullptr;
315     std::shared_ptr<NativeAVPlayerOnInfoCallback> infoCallback_ = nullptr;
316     std::map<uint32_t, OnInfoFunc> onInfoFuncs_;
317     struct AVPlayerCallback callback_ = { .onInfo = nullptr, .onError = nullptr };
318     Player_MediaKeySystemInfoCallback drmsysteminfocallback_ = nullptr;
319 };
320 
StartListenCurrentResource()321 void PlayerObject::StartListenCurrentResource()
322 {
323     if (callback_ != nullptr) {
324         callback_->Start();
325     }
326 }
327 
PauseListenCurrentResource()328 void PlayerObject::PauseListenCurrentResource()
329 {
330     if (callback_ != nullptr) {
331         callback_->Pause();
332     }
333 }
334 
NativeAVPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback)335 NativeAVPlayerCallback::NativeAVPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback) : player_(player),
336     callback_(callback) {
337     MEDIA_LOGD("0x%{public}06" PRIXPTR " callback create", FAKE_POINTER(this));
338     onInfoFuncs_ = {
339         { INFO_TYPE_SEEKDONE,
340             [this](const int32_t extra, const Format &infoBody) { OnSeekDoneCb(extra, infoBody); } },
341         { INFO_TYPE_SPEEDDONE,
342             [this](const int32_t extra, const Format &infoBody) { OnSpeedDoneCb(extra, infoBody); } },
343         { INFO_TYPE_BITRATEDONE,
344             [this](const int32_t extra, const Format &infoBody) { OnBitRateDoneCb(extra, infoBody); } },
345         { INFO_TYPE_EOS,
346             [this](const int32_t extra, const Format &infoBody) { OnEosCb(extra, infoBody); } },
347         { INFO_TYPE_STATE_CHANGE,
348             [this](const int32_t extra, const Format &infoBody) { OnStateChangeCb(extra, infoBody); } },
349         { INFO_TYPE_POSITION_UPDATE,
350             [this](const int32_t extra, const Format &infoBody) { OnPositionUpdateCb(extra, infoBody); } },
351         { INFO_TYPE_MESSAGE,
352             [this](const int32_t extra, const Format &infoBody) { OnMessageCb(extra, infoBody);} },
353         { INFO_TYPE_VOLUME_CHANGE,
354             [this](const int32_t extra, const Format &infoBody) { OnVolumeChangeCb(extra, infoBody); } },
355         { INFO_TYPE_RESOLUTION_CHANGE,
356             [this](const int32_t extra, const Format &infoBody) { OnVideoSizeChangedCb(extra, infoBody); } },
357         { INFO_TYPE_BUFFERING_UPDATE,
358             [this](const int32_t extra, const Format &infoBody) { OnBufferingUpdateCb(extra, infoBody); } },
359         { INFO_TYPE_BITRATE_COLLECT,
360             [this](const int32_t extra, const Format &infoBody) { OnBitRateCollectedCb(extra, infoBody); } },
361         { INFO_TYPE_INTERRUPT_EVENT,
362             [this](const int32_t extra, const Format &infoBody) { OnAudioInterruptCb(extra, infoBody); } },
363         { INFO_TYPE_DURATION_UPDATE,
364             [this](const int32_t extra, const Format &infoBody) { OnDurationUpdateCb(extra, infoBody); } },
365         { INFO_TYPE_IS_LIVE_STREAM,
366             [this](const int32_t extra, const Format &infoBody) { OnNotifyIsLiveStream(extra, infoBody); } },
367         { INFO_TYPE_TRACKCHANGE,
368             [this](const int32_t extra, const Format &infoBody) { OnTrackChangedCb(extra, infoBody); } },
369         { INFO_TYPE_TRACK_INFO_UPDATE,
370             [this](const int32_t extra, const Format &infoBody) { OnTrackInfoUpdate(extra, infoBody); } },
371         { INFO_TYPE_SUBTITLE_UPDATE_INFO,
372             [this](const int32_t extra, const Format &infoBody) { OnSubtitleInfoCb(extra, infoBody); } },
373         { INFO_TYPE_AUDIO_DEVICE_CHANGE,
374             [this](const int32_t extra, const Format &infoBody) { OnAudioDeviceChangeCb(extra, infoBody); } },
375         { INFO_TYPE_DRM_INFO_UPDATED,
376             [this](const int32_t extra, const Format &infoBody) { OnDrmInfoUpdatedCb(extra, infoBody); } },
377     };
378 }
379 
OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)380 void NativeAVPlayerCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
381 {
382     std::unique_lock<std::mutex> lock(mutex_);
383     if (isReleased_.load() || player_ == nullptr) {
384         MEDIA_LOGI("OnInfo() is called, type %{public}d, extra %{public}d, isReleased %{public}d",
385             static_cast<int32_t>(type), extra, isReleased_.load());
386         return;
387     }
388 
389     if (infoCallback_ != nullptr) { // infoCallback_ precedes over callback_.onInfo
390         MEDIA_LOGD("OnInfo type %{public}d extra %{public}d", type, extra);
391         if (onInfoFuncs_.count(type) > 0) {
392             onInfoFuncs_[type](extra, infoBody);
393         } else {
394             MEDIA_LOGD("0x%{public}06" PRIXPTR " OnInfo: no member func, type %{public}d extra %{public}d",
395                 FAKE_POINTER(this), type, extra);
396         }
397         return;
398     }
399 
400     if (type == INFO_TYPE_DRM_INFO_UPDATED) { // process drm with independent callback
401         OnDrmInfoUpdatedCb(extra, infoBody);
402         return;
403     }
404 
405     if (callback_.onInfo == nullptr) {
406         return;
407     }
408     if (type == INFO_TYPE_STATE_CHANGE) {
409         PlayerStates state = static_cast<PlayerStates>(extra);
410         player_->state_ = state;
411         for (uint32_t i = 0; i < STATE_MAP_LENGTH; i++) {
412             if (g_stateMap[i].playerStates != state) {
413                 continue;
414             }
415             int32_t convertState = g_stateMap[i].avPlayerState;
416             callback_.onInfo(player_, AV_INFO_TYPE_STATE_CHANGE, convertState);
417             return;
418         };
419         return;
420     }
421     for (uint32_t i = 0; i < INFO_TYPE_LENGTH; i++) {
422         if (g_onInfoType[i].playerOnInfoType == type) {
423             callback_.onInfo(player_, g_onInfoType[i].aVPlayerOnInfoType, extra);
424             break;
425         }
426     }
427 }
428 
OnError(int32_t errorCode, const std::string &errorMsg)429 void NativeAVPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
430 {
431     MEDIA_LOGI("OnError() is called, errorCode: %{public}d, isReleased: %{public}d, errorMsg: %{public}s",
432         errorCode, isReleased_.load(), errorMsg.c_str());
433     std::unique_lock<std::mutex> lock(mutex_);
434     if (isReleased_.load() || player_ == nullptr) {
435         return;
436     }
437     int32_t avErrorCode;
438     if (errorCallback_) { // errorCallback_ precedes over callback_.onInfo
439         MediaServiceExtErrCodeAPI9 errorCodeApi9 = MSERR_EXT_API9_OK;
440         if (errorCode >= MSERR_EXT_API9_NO_PERMISSION && errorCode <= MSERR_EXT_API14_IO_VIDEO_ENC_UNAVAILABLE) {
441             errorCodeApi9 = static_cast<MediaServiceExtErrCodeAPI9>(errorCode);
442             avErrorCode = MSErrCodeToAVErrCodeApi9(errorCodeApi9);
443         } else {
444             avErrorCode = MSErrCodeToAVErrCode(static_cast<MediaServiceErrCode>(errorCode));
445             errorCodeApi9 = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(errorCode));
446         }
447         std::string errorMsgExt = MSExtAVErrorToString(errorCodeApi9) + errorMsg;
448         errorCallback_->OnError(player_, avErrorCode, errorMsgExt.c_str());
449         return;
450     }
451 
452     if (callback_.onError != nullptr) {
453         // To make sure compatibility only convert for UNSUPPORT_FORMAT_ERROR_CODE
454         avErrorCode = errorCode;
455         if (errorCode == UNSUPPORT_FORMAT_ERROR_CODE) {
456             avErrorCode = AVPLAYER_ERR_UNSUPPORT;
457         }
458         callback_.onError(player_, avErrorCode, errorMsg.c_str());
459     }
460 }
461 
462 #ifdef SUPPORT_AVPLAYER_DRM
SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback)463 int32_t NativeAVPlayerCallback::SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback)
464 {
465     std::lock_guard<std::mutex> lock(mutex_);
466     drmsysteminfocallback_ = drmSystemInfoCallback;
467     return AV_ERR_OK;
468 }
469 
GetDrmSystemInfos(const Format &infoBody, DRM_MediaKeySystemInfo *mediaKeySystemInfo, struct PlayerObject *playerObj)470 int32_t NativeAVPlayerCallback::GetDrmSystemInfos(const Format &infoBody,
471     DRM_MediaKeySystemInfo *mediaKeySystemInfo, struct PlayerObject *playerObj)
472 {
473     if (!infoBody.ContainKey(std::string(PlayerKeys::PLAYER_DRM_INFO_ADDR))) {
474         MEDIA_LOGW("there's no drminfo-update drm_info_addr key");
475         return AV_ERR_INVALID_VAL;
476     }
477     if (!infoBody.ContainKey(std::string(PlayerKeys::PLAYER_DRM_INFO_COUNT))) {
478         MEDIA_LOGW("there's no drminfo-update drm_info_count key");
479         return AV_ERR_INVALID_VAL;
480     }
481     uint8_t *drmInfoAddr = nullptr;
482     size_t size  = 0;
483     int32_t infoCount = 0;
484     infoBody.GetBuffer(std::string(PlayerKeys::PLAYER_DRM_INFO_ADDR), &drmInfoAddr, size);
485     CHECK_AND_RETURN_RET_LOG(drmInfoAddr != nullptr && size > 0, AV_ERR_INVALID_VAL, "get drminfo buffer failed");
486     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_DRM_INFO_COUNT), infoCount);
487     CHECK_AND_RETURN_RET_LOG(infoCount > 0, AV_ERR_INVALID_VAL, "get drminfo count is illegal");
488     DrmInfoItem *drmInfos = reinterpret_cast<DrmInfoItem*>(drmInfoAddr);
489     CHECK_AND_RETURN_RET_LOG(drmInfos != nullptr, AV_ERR_INVALID_VAL, "cast drmInfos nullptr");
490     for (int32_t i = 0; i < infoCount; i++) {
491         DrmInfoItem temp = drmInfos[i];
492         std::vector<uint8_t> uuid(temp.uuid, temp.uuid + DrmConstant::DRM_MAX_M3U8_DRM_UUID_LEN);
493         std::vector<uint8_t> pssh(temp.pssh, temp.pssh + temp.psshLen);
494         playerObj->localDrmInfos_.insert({ uuid, pssh });
495     }
496     int index = 0;
497     for (auto item : playerObj->localDrmInfos_) {
498         int ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].uuid,
499             item.first.size(), item.first.data(), item.first.size());
500         int err = memcpy_s(mediaKeySystemInfo->psshInfo[index].data, item.second.size(),
501             item.second.data(), item.second.size());
502         CHECK_AND_RETURN_RET_LOG((err == 0 && ret == 0), AV_ERR_INVALID_VAL, "cast drmInfos nullptr");
503         mediaKeySystemInfo->psshInfo[index++].dataLen = static_cast<int32_t>(item.second.size());
504     }
505     mediaKeySystemInfo->psshCount = index;
506     return AV_ERR_OK;
507 }
508 #endif
509 
SetPlayCallback(AVPlayerCallback callback)510 int32_t NativeAVPlayerCallback::SetPlayCallback(AVPlayerCallback callback)
511 {
512     std::lock_guard<std::mutex> lock(mutex_);
513     callback_ = callback;
514     return AV_ERR_OK;
515 }
516 
SetOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData)517 int32_t NativeAVPlayerCallback::SetOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData)
518 {
519     std::lock_guard<std::mutex> lock(mutex_);
520     if (callback != nullptr) {
521         NativeAVPlayerOnErrorCallback *errorCallback =
522             new (std::nothrow) NativeAVPlayerOnErrorCallback(callback, userData);
523         CHECK_AND_RETURN_RET_LOG(errorCallback != nullptr, AV_ERR_NO_MEMORY, "errorCallback is nullptr!");
524         errorCallback_ = std::shared_ptr<NativeAVPlayerOnErrorCallback>(errorCallback);
525     } else {
526         errorCallback_ = nullptr;
527     }
528     return AV_ERR_OK;
529 }
530 
SetOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData)531 int32_t NativeAVPlayerCallback::SetOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData)
532 {
533     std::lock_guard<std::mutex> lock(mutex_);
534     if (callback != nullptr) {
535         NativeAVPlayerOnInfoCallback *onInfoCallback =
536             new (std::nothrow) NativeAVPlayerOnInfoCallback(callback, userData);
537         CHECK_AND_RETURN_RET_LOG(onInfoCallback != nullptr, AV_ERR_NO_MEMORY, "infoCallback_ is nullptr!");
538         infoCallback_ = std::shared_ptr<NativeAVPlayerOnInfoCallback>(onInfoCallback);
539     } else {
540         infoCallback_ = nullptr;
541     }
542     return AV_ERR_OK;
543 }
544 
Start()545 void NativeAVPlayerCallback::Start()
546 {
547     isSourceLoaded_.store(true);
548 }
549 
Pause()550 void NativeAVPlayerCallback::Pause()
551 {
552     isSourceLoaded_.store(false);
553 }
554 
OnSeekDoneCb(const int32_t extra, const Format &infoBody)555 void NativeAVPlayerCallback::OnSeekDoneCb(const int32_t extra, const Format &infoBody)
556 {
557     (void)infoBody;
558     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnSeekDoneCb current source is unready");
559     int32_t currentPositon = extra;
560     MEDIA_LOGI("0x%{public}06" PRIXPTR " seekDone %{public}d", FAKE_POINTER(this), currentPositon);
561 
562     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
563     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSeekDoneCb OH_AVFormat create failed");
564     avFormat->format_.PutIntValue(OH_PLAYER_SEEK_POSITION, currentPositon);
565     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SEEKDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
566 }
567 
OnSpeedDoneCb(const int32_t extra, const Format &infoBody)568 void NativeAVPlayerCallback::OnSpeedDoneCb(const int32_t extra, const Format &infoBody)
569 {
570     (void)infoBody;
571     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnSpeedDoneCb current source is unready");
572     int32_t speedMode = extra;
573     MEDIA_LOGI("SpeedDone %{public}d", speedMode);
574 
575     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
576     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSpeedDoneCb OH_AVFormat create failed");
577     avFormat->format_.PutIntValue(OH_PLAYER_PLAYBACK_SPEED, speedMode);
578     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SPEEDDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
579 }
580 
OnBitRateDoneCb(const int32_t extra, const Format &infoBody)581 void NativeAVPlayerCallback::OnBitRateDoneCb(const int32_t extra, const Format &infoBody)
582 {
583     (void)infoBody;
584     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBitRateDoneCb current source is unready");
585     int32_t bitRate = extra;
586     MEDIA_LOGI("Bitrate done %{public}d", bitRate);
587 
588     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
589     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBitRateDoneCb OH_AVFormat create failed");
590     avFormat->format_.PutIntValue(OH_PLAYER_BITRATE, bitRate);
591     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BITRATEDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
592 }
593 
OnEosCb(const int32_t extra, const Format &infoBody)594 void NativeAVPlayerCallback::OnEosCb(const int32_t extra, const Format &infoBody)
595 {
596     (void)infoBody;
597     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnEosCb current source is unready");
598     int32_t isLooping = extra;
599     MEDIA_LOGI("0x%{public}06" PRIXPTR " EOS is called, isloop: %{public}d", FAKE_POINTER(this), isLooping);
600 
601     infoCallback_->OnInfo(player_, AV_INFO_TYPE_EOS, nullptr);
602 }
603 
OnStateChangeCb(const int32_t extra, const Format &infoBody)604 void NativeAVPlayerCallback::OnStateChangeCb(const int32_t extra, const Format &infoBody)
605 {
606     MEDIA_LOGI("OnStateChangeCb() is called, state %{public}d", extra);
607     if (extra == static_cast<int32_t>(PLAYER_RELEASED)) {
608         isReleased_.store(true);
609     }
610     if (player_ == nullptr || infoCallback_ == nullptr) {
611         return;
612     }
613     PlayerStates state = static_cast<PlayerStates>(extra);
614     player_->state_ = state;
615     for (uint32_t i = 0; i < STATE_MAP_LENGTH; i++) {
616         if (g_stateMap[i].playerStates != state) {
617             continue;
618         }
619         AVPlayerState convertState = g_stateMap[i].avPlayerState;
620         OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
621         CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnStateChangeCb OH_AVFormat create failed");
622 
623         int32_t reason = StateChangeReason::USER;
624         if (infoBody.ContainKey(PlayerKeys::PLAYER_STATE_CHANGED_REASON)) {
625             (void)infoBody.GetIntValue(PlayerKeys::PLAYER_STATE_CHANGED_REASON, reason);
626         }
627         avFormat->format_.PutIntValue(OH_PLAYER_STATE_CHANGE_REASON, static_cast<int32_t>(reason));
628         avFormat->format_.PutIntValue(OH_PLAYER_STATE, static_cast<int32_t>(convertState));
629         infoCallback_->OnInfo(player_, AV_INFO_TYPE_STATE_CHANGE,
630             reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
631         return;
632     }
633 }
634 
OnPositionUpdateCb(const int32_t extra, const Format &infoBody)635 void NativeAVPlayerCallback::OnPositionUpdateCb(const int32_t extra, const Format &infoBody)
636 {
637     (void)infoBody;
638     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnPositionUpdateCb current source is unready");
639     int32_t position = extra;
640     MEDIA_LOGD("OnPositionUpdateCb is called, position: %{public}d", position);
641 
642     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
643     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnPositionUpdateCb OH_AVFormat create failed");
644     avFormat->format_.PutIntValue(OH_PLAYER_CURRENT_POSITION, position);
645     infoCallback_->OnInfo(player_, AV_INFO_TYPE_POSITION_UPDATE,
646         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
647 }
648 
OnVolumeChangeCb(const int32_t extra, const Format &infoBody)649 void NativeAVPlayerCallback::OnVolumeChangeCb(const int32_t extra, const Format &infoBody)
650 {
651     (void)extra;
652     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnVolumeChangeCb current source is unready");
653     float volumeLevel = 0.0;
654     (void)infoBody.GetFloatValue(PlayerKeys::PLAYER_VOLUME_LEVEL, volumeLevel);
655     MEDIA_LOGD("OnVolumeChangeCb in volume=%{public}f", volumeLevel);
656 
657     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
658     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnVolumeChangeCb OH_AVFormat create failed");
659     avFormat->format_.PutFloatValue(OH_PLAYER_VOLUME, volumeLevel);
660     infoCallback_->OnInfo(player_, AV_INFO_TYPE_VOLUME_CHANGE,
661         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
662 }
663 
OnMessageCb(const int32_t extra, const Format &infoBody)664 void NativeAVPlayerCallback::OnMessageCb(const int32_t extra, const Format &infoBody)
665 {
666     (void)infoBody;
667     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnMessageCb current source is unready");
668     int32_t messageType = extra;
669     MEDIA_LOGI("OnMessageCb is called, extra: %{public}d", messageType);
670     if (extra == PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START) {
671         OnStartRenderFrameCb();
672     }
673 }
674 
OnStartRenderFrameCb() const675 void NativeAVPlayerCallback::OnStartRenderFrameCb() const
676 {
677     MEDIA_LOGI("OnStartRenderFrameCb is called");
678     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnStartRenderFrameCb current source is unready");
679 
680     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
681     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnStartRenderFrameCb OH_AVFormat create failed");
682     avFormat->format_.PutIntValue(OH_PLAYER_MESSAGE_TYPE, 1); // 1 means PLAYER_INFO_VIDEO_RENDERING_START
683     infoCallback_->OnInfo(player_, AV_INFO_TYPE_MESSAGE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
684 }
685 
OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody)686 void NativeAVPlayerCallback::OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody)
687 {
688     (void)extra;
689     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnVideoSizeChangedCb current source is unready");
690     int32_t width = 0;
691     int32_t height = 0;
692     (void)infoBody.GetIntValue(PlayerKeys::PLAYER_WIDTH, width);
693     (void)infoBody.GetIntValue(PlayerKeys::PLAYER_HEIGHT, height);
694     MEDIA_LOGI("0x%{public}06" PRIXPTR " sizeChange w %{public}d h %{public}d", FAKE_POINTER(this), width, height);
695 
696     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
697     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnVideoSizeChangedCb OH_AVFormat create failed");
698     avFormat->format_.PutIntValue(OH_PLAYER_VIDEO_WIDTH, width);
699     avFormat->format_.PutIntValue(OH_PLAYER_VIDEO_HEIGHT, height);
700     infoCallback_->OnInfo(player_, AV_INFO_TYPE_RESOLUTION_CHANGE,
701         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
702 }
703 
OnBufferingUpdateCb(const int32_t extra, const Format &infoBody)704 void NativeAVPlayerCallback::OnBufferingUpdateCb(const int32_t extra, const Format &infoBody)
705 {
706     (void)extra;
707     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBufferingUpdateCb current source is unready");
708     int32_t val = 0;
709     AVPlayerBufferingType bufferingType;
710     if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_START))) {
711         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_START;
712         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_START), val);
713     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_END))) {
714         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_END;
715         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_END), val);
716     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT))) {
717         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_PERCENT;
718         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT), val);
719     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_CACHED_DURATION))) {
720         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_CACHED_DURATION;
721         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_CACHED_DURATION), val);
722     } else {
723         return;
724     }
725     MEDIA_LOGD("OnBufferingUpdateCb is called, buffering type: %{public}d value: %{public}d",
726         static_cast<int32_t>(bufferingType), val);
727 
728     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
729     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBufferingUpdateCb OH_AVFormat create failed");
730     avFormat->format_.PutIntValue(OH_PLAYER_BUFFERING_TYPE, static_cast<int32_t>(bufferingType));
731     avFormat->format_.PutIntValue(OH_PLAYER_BUFFERING_VALUE, val);
732     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BUFFERING_UPDATE,
733         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
734 }
735 
OnBitRateCollectedCb(const int32_t extra, const Format &infoBody)736 void NativeAVPlayerCallback::OnBitRateCollectedCb(const int32_t extra, const Format &infoBody)
737 {
738     (void)extra;
739     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBitRateCollectedCb current source is unready");
740 
741     uint8_t *addr = nullptr;
742     size_t size  = 0;
743     if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_AVAILABLE_BITRATES))) {
744         infoBody.GetBuffer(std::string(PlayerKeys::PLAYER_AVAILABLE_BITRATES), &addr, size);
745     }
746     CHECK_AND_RETURN_LOG(addr != nullptr, "bitRates addr is nullptr");
747     size_t bitRatesCount = static_cast<size_t>(size / sizeof(uint32_t));
748     CHECK_AND_RETURN_LOG(bitRatesCount > 0, "bitRates size(%{public}zu) is invalid", size);
749     MEDIA_LOGI("bitRates count: %{public}zu", bitRatesCount);
750     for (size_t i = 0; i < bitRatesCount; i++) {
751         MEDIA_LOGI("bitRates[%{public}zu]: %{public}u", i, *(static_cast<uint32_t*>(static_cast<void*>(addr)) + i));
752     }
753 
754     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
755     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBitRateCollectedCb OH_AVFormat create failed");
756     avFormat->format_.PutBuffer(OH_PLAYER_BITRATE_ARRAY, addr, static_cast<size_t>(bitRatesCount * sizeof(uint32_t)));
757     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BITRATE_COLLECT,
758         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
759 }
760 
OnAudioInterruptCb(const int32_t extra, const Format &infoBody)761 void NativeAVPlayerCallback::OnAudioInterruptCb(const int32_t extra, const Format &infoBody)
762 {
763     (void)extra;
764     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnAudioInterruptCb current source is unready");
765     int32_t eventType = 0;
766     int32_t forceType = 0;
767     int32_t hintType = 0;
768     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_TYPE, eventType);
769     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_FORCE, forceType);
770     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_HINT, hintType);
771     MEDIA_LOGI("OnAudioInterruptCb is called, eventType: %{public}d, forceType: %{public}d, hintType: %{public}d",
772         eventType, forceType, hintType);
773 
774     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
775     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnAudioInterruptCb OH_AVFormat create failed");
776     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_TYPE, eventType);
777     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_FORCE, forceType);
778     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_HINT, hintType);
779     infoCallback_->OnInfo(player_, AV_INFO_TYPE_INTERRUPT_EVENT,
780         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
781 }
782 
OnDurationUpdateCb(const int32_t extra, const Format &infoBody)783 void NativeAVPlayerCallback::OnDurationUpdateCb(const int32_t extra, const Format &infoBody)
784 {
785     (void)infoBody;
786     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnDurationUpdateCb current source is unready");
787     int64_t duration = extra;
788     MEDIA_LOGI("0x%{public}06" PRIXPTR " duration update %{public}" PRId64, FAKE_POINTER(this), duration);
789 
790     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
791     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnDurationUpdateCb OH_AVFormat create failed");
792     avFormat->format_.PutLongValue(OH_PLAYER_DURATION, duration);
793     infoCallback_->OnInfo(player_, AV_INFO_TYPE_DURATION_UPDATE,
794         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
795 }
796 
OnNotifyIsLiveStream(const int32_t extra, const Format &infoBody)797 void NativeAVPlayerCallback::OnNotifyIsLiveStream(const int32_t extra, const Format &infoBody)
798 {
799     (void)extra;
800     (void)infoBody;
801     MEDIA_LOGI("0x%{public}06" PRIXPTR " OnNotifyIsLiveStream extra: %{public}d", FAKE_POINTER(this), extra);
802 
803     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
804     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnNotifyIsLiveStream OH_AVFormat create failed");
805     avFormat->format_.PutIntValue(OH_PLAYER_IS_LIVE_STREAM, 1); // 1 means is live stream
806     infoCallback_->OnInfo(player_, AV_INFO_TYPE_IS_LIVE_STREAM,
807         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
808 }
809 
OnTrackChangedCb(const int32_t extra, const Format &infoBody)810 void NativeAVPlayerCallback::OnTrackChangedCb(const int32_t extra, const Format &infoBody)
811 {
812     (void)extra;
813     int32_t trackIndex = -1;
814     int32_t isSelect = -1;
815     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), trackIndex);
816     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_IS_SELECT), isSelect);
817     MEDIA_LOGI("OnTrackChangedCb trackIndex: %{public}d, isSelect: %{public}d", trackIndex, isSelect);
818     CHECK_AND_RETURN_LOG(trackIndex != -1 && isSelect != -1, "invalid trackIndex: %{public}d, isSelect: %{public}d",
819         trackIndex, isSelect);
820     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
821     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnTrackChangedCb OH_AVFormat create failed");
822     avFormat->format_.PutIntValue(OH_PLAYER_TRACK_INDEX, trackIndex);
823     avFormat->format_.PutIntValue(OH_PLAYER_TRACK_IS_SELECT, isSelect);
824     infoCallback_->OnInfo(player_, AV_INFO_TYPE_TRACKCHANGE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
825 }
826 
OnTrackInfoUpdate(const int32_t extra, const Format &infoBody)827 void NativeAVPlayerCallback::OnTrackInfoUpdate(const int32_t extra, const Format &infoBody)
828 {
829     (void)extra;
830     (void)extra;
831     MEDIA_LOGI("OnTrackInfoUpdate not support");
832 }
833 
OnSubtitleInfoCb(const int32_t extra, const Format &infoBody)834 void NativeAVPlayerCallback::OnSubtitleInfoCb(const int32_t extra, const Format &infoBody)
835 {
836     (void)extra;
837     int32_t pts = -1;
838     int32_t duration = -1;
839     std::string text;
840     infoBody.GetStringValue(PlayerKeys::SUBTITLE_TEXT, text);
841     infoBody.GetIntValue(std::string(PlayerKeys::SUBTITLE_PTS), pts);
842     infoBody.GetIntValue(std::string(PlayerKeys::SUBTITLE_DURATION), duration);
843     MEDIA_LOGI("OnSubtitleInfoCb pts: %{public}d, duration: %{public}d", pts, duration);
844 
845     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
846     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSubtitleInfoCb OH_AVFormat create failed");
847     avFormat->format_.PutStringValue(OH_PLAYER_SUBTITLE_TEXT, text);
848     avFormat->format_.PutIntValue(OH_PLAYER_SUBTITLE_PTS, pts);
849     avFormat->format_.PutIntValue(OH_PLAYER_SUBTITLE_DURATION, duration);
850     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SUBTITLE_UPDATE,
851         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
852 }
853 
OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody)854 void NativeAVPlayerCallback::OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody)
855 {
856     (void)extra;
857     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnAudioDeviceChangeCb current source is unready");
858 
859     int32_t reason = 0; // means UNKOWN, see OH_AudioStream_DeviceChangeReason
860     infoBody.GetIntValue(PlayerKeys::AUDIO_DEVICE_CHANGE_REASON, reason);
861     MEDIA_LOGI("0x%{public}06" PRIXPTR " OnAudioDeviceChangeCb reason: %{public}d", FAKE_POINTER(this), reason);
862 
863     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
864     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnAudioDeviceChangeCb OH_AVFormat create failed");
865     // We only report AUDIO_DEVICE_CHANGE_REASON at this stage.
866     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_DEVICE_CHANGE_REASON, reason);
867     infoCallback_->OnInfo(player_, AV_INFO_TYPE_AUDIO_OUTPUT_DEVICE_CHANGE,
868         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
869 }
870 
OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody)871 void NativeAVPlayerCallback::OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody)
872 {
873 #ifdef SUPPORT_AVPLAYER_DRM
874     if (drmsysteminfocallback_ == nullptr) {
875         return;
876     }
877     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player_);
878     DRM_MediaKeySystemInfo mediaKeySystemInfo;
879     GetDrmSystemInfos(infoBody, &mediaKeySystemInfo, playerObj);
880     drmsysteminfocallback_(player_, &mediaKeySystemInfo);
881 #else
882     (void)drmsysteminfocallback_;
883     (void)extra;
884     (void)infoBody;
885 #endif
886 }
887 
OH_AVPlayer_Create(void)888 OH_AVPlayer *OH_AVPlayer_Create(void)
889 {
890     std::shared_ptr<Player> player = PlayerFactory::CreatePlayer();
891     CHECK_AND_RETURN_RET_LOG(player != nullptr, nullptr, "failed to PlayerFactory::CreatePlayer");
892 
893     PlayerObject *object = new(std::nothrow) PlayerObject(player);
894     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new PlayerObject");
895     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_Create", FAKE_POINTER(object));
896 
897     return object;
898 }
899 
OH_AVPlayer_SetURLSource(OH_AVPlayer *player, const char *url)900 OH_AVErrCode OH_AVPlayer_SetURLSource(OH_AVPlayer *player, const char *url)
901 {
902     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
903     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
904     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
905     CHECK_AND_RETURN_RET_LOG(url != nullptr, AV_ERR_INVALID_VAL, "url is null");
906     playerObj->StartListenCurrentResource();
907     int32_t ret = playerObj->player_->SetSource(url);
908     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player setUrlSource failed");
909     return AV_ERR_OK;
910 }
911 
OH_AVPlayer_SetFDSource(OH_AVPlayer *player, int32_t fd, int64_t offset, int64_t size)912 OH_AVErrCode OH_AVPlayer_SetFDSource(OH_AVPlayer *player, int32_t fd, int64_t offset, int64_t size)
913 {
914     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
915     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
916     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
917     CHECK_AND_RETURN_RET_LOG(fd >= 0, AV_ERR_INVALID_VAL, "fd is invalid");
918     playerObj->StartListenCurrentResource();
919     int32_t ret = playerObj->player_->SetSource(fd, offset, size);
920     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player setFdSource failed");
921     return AV_ERR_OK;
922 }
923 
OH_AVPlayer_Prepare(OH_AVPlayer *player)924 OH_AVErrCode OH_AVPlayer_Prepare(OH_AVPlayer *player)
925 {
926     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
927     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
928     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
929     int32_t ret = playerObj->player_->PrepareAsync();
930     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Prepare failed");
931     return AV_ERR_OK;
932 }
933 
OH_AVPlayer_Play(OH_AVPlayer *player)934 OH_AVErrCode OH_AVPlayer_Play(OH_AVPlayer *player)
935 {
936     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
937     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
938     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
939     int32_t ret = playerObj->player_->Play();
940     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player play failed");
941     return AV_ERR_OK;
942 }
943 
OH_AVPlayer_Pause(OH_AVPlayer *player)944 OH_AVErrCode OH_AVPlayer_Pause(OH_AVPlayer *player)
945 {
946     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
947     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
948     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
949     int32_t ret = playerObj->player_->Pause();
950     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Pause failed");
951     return AV_ERR_OK;
952 }
953 
OH_AVPlayer_Stop(OH_AVPlayer *player)954 OH_AVErrCode OH_AVPlayer_Stop(OH_AVPlayer *player)
955 {
956     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
957     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
958     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
959     int32_t ret = playerObj->player_->Stop();
960     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Stop failed");
961     return AV_ERR_OK;
962 }
963 
OH_AVPlayer_Reset(OH_AVPlayer *player)964 OH_AVErrCode OH_AVPlayer_Reset(OH_AVPlayer *player)
965 {
966     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
967     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
968     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
969     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
970     int32_t ret = playerObj->player_->Reset();
971     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Reset failed");
972     return AV_ERR_OK;
973 }
974 
OH_AVPlayer_Release(OH_AVPlayer *player)975 OH_AVErrCode OH_AVPlayer_Release(OH_AVPlayer *player)
976 {
977     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
978     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
979     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
980     CHECK_AND_RETURN_RET_LOG(!playerObj->isReleased_.load(), AV_ERR_OK, "player alreay isReleased");
981     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
982     int32_t ret = playerObj->player_->Release();
983     playerObj->isReleased_.store(true);
984     if (playerObj->callback_ != nullptr) {
985         Format format;
986         playerObj->callback_->OnInfo(INFO_TYPE_STATE_CHANGE, PLAYER_RELEASED, format);
987     }
988     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Release failed");
989     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_Release", FAKE_POINTER(playerObj));
990     return AV_ERR_OK;
991 }
992 
OH_AVPlayer_ReleaseSync(OH_AVPlayer *player)993 OH_AVErrCode OH_AVPlayer_ReleaseSync(OH_AVPlayer *player)
994 {
995     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
996     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
997     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
998     CHECK_AND_RETURN_RET_LOG(!playerObj->isReleased_.load(), AV_ERR_OK, "player alreay isReleased");
999     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
1000     int32_t ret = playerObj->player_->ReleaseSync();
1001     playerObj->isReleased_.store(true);
1002     if (playerObj->callback_ != nullptr) {
1003         Format format;
1004         playerObj->callback_->OnInfo(INFO_TYPE_STATE_CHANGE, PLAYER_RELEASED, format);
1005     }
1006     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player ReleaseSync failed");
1007     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_ReleaseSync", FAKE_POINTER(playerObj));
1008     return AV_ERR_OK;
1009 }
1010 
OH_AVPlayer_SetVolume(OH_AVPlayer *player, float leftVolume, float rightVolume)1011 OH_AVErrCode OH_AVPlayer_SetVolume(OH_AVPlayer *player, float leftVolume, float rightVolume)
1012 {
1013     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1014     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1015     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1016     int32_t ret = playerObj->player_->SetVolume(leftVolume, rightVolume);
1017     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetVolume failed");
1018     return AV_ERR_OK;
1019 }
1020 
OH_AVPlayer_Seek(OH_AVPlayer *player, int32_t mSeconds, AVPlayerSeekMode mode)1021 OH_AVErrCode OH_AVPlayer_Seek(OH_AVPlayer *player, int32_t mSeconds, AVPlayerSeekMode mode)
1022 {
1023     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1024     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1025     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1026     PlayerSeekMode seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1027     switch (mode) {
1028         case AVPlayerSeekMode::AV_SEEK_NEXT_SYNC:
1029             seekMode = PlayerSeekMode::SEEK_NEXT_SYNC;
1030             break;
1031         case AVPlayerSeekMode::AV_SEEK_PREVIOUS_SYNC:
1032             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1033             break;
1034         case AVPlayerSeekMode::AV_SEEK_CLOSEST:
1035             seekMode = PlayerSeekMode::SEEK_CLOSEST;
1036             break;
1037         default:
1038             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1039             break;
1040     }
1041     int32_t ret = playerObj->player_->Seek(mSeconds, seekMode);
1042     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Seek failed");
1043     return AV_ERR_OK;
1044 }
1045 
OH_AVPlayer_GetCurrentTime(OH_AVPlayer *player, int32_t *currentTime)1046 OH_AVErrCode OH_AVPlayer_GetCurrentTime(OH_AVPlayer *player, int32_t *currentTime)
1047 {
1048     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1049     CHECK_AND_RETURN_RET_LOG(currentTime != nullptr, AV_ERR_INVALID_VAL, "currentTime is nullptr");
1050     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1051     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1052     int32_t ret = playerObj->player_->GetCurrentTime(*currentTime);
1053     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetCurrentTime failed");
1054     return AV_ERR_OK;
1055 }
1056 
OH_AVPlayer_GetVideoWidth(OH_AVPlayer *player, int32_t *videoWidth)1057 OH_AVErrCode OH_AVPlayer_GetVideoWidth(OH_AVPlayer *player, int32_t *videoWidth)
1058 {
1059     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1060     CHECK_AND_RETURN_RET_LOG(videoWidth != nullptr, AV_ERR_INVALID_VAL, "videoWidth is nullptr");
1061     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1062     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1063     *videoWidth = playerObj->player_->GetVideoWidth();
1064     return AV_ERR_OK;
1065 }
1066 
OH_AVPlayer_GetVideoHeight(OH_AVPlayer *player, int32_t *videoHeight)1067 OH_AVErrCode OH_AVPlayer_GetVideoHeight(OH_AVPlayer *player, int32_t *videoHeight)
1068 {
1069     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1070     CHECK_AND_RETURN_RET_LOG(videoHeight != nullptr, AV_ERR_INVALID_VAL, "videoHeight is nullptr");
1071     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1072     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1073     *videoHeight = playerObj->player_->GetVideoHeight();
1074     return AV_ERR_OK;
1075 }
1076 
OH_AVPlayer_SetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed speed)1077 OH_AVErrCode OH_AVPlayer_SetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed speed)
1078 {
1079     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1080     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1081     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1082     int32_t ret = playerObj->player_->SetPlaybackSpeed(static_cast<PlaybackRateMode>(speed));
1083     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetPlaybackSpeed failed");
1084     return AV_ERR_OK;
1085 }
1086 
OH_AVPlayer_GetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed *speed)1087 OH_AVErrCode OH_AVPlayer_GetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed *speed)
1088 {
1089     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1090     CHECK_AND_RETURN_RET_LOG(speed != nullptr, AV_ERR_INVALID_VAL, "speed is nullptr");
1091     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1092     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1093     PlaybackRateMode md;
1094     int32_t ret = playerObj->player_->GetPlaybackSpeed(md);
1095     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetPlaybackSpeed failed");
1096     *speed = static_cast<AVPlaybackSpeed>(md);
1097     return AV_ERR_OK;
1098 }
1099 
OH_AVPlayer_SetAudioRendererInfo(OH_AVPlayer *player, OH_AudioStream_Usage streamUsage)1100 OH_AVErrCode OH_AVPlayer_SetAudioRendererInfo(OH_AVPlayer *player, OH_AudioStream_Usage streamUsage)
1101 {
1102     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1103     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1104     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1105     if (streamUsage < OH_AudioStream_Usage::AUDIOSTREAM_USAGE_UNKNOWN ||
1106         streamUsage > OH_AudioStream_Usage::AUDIOSTREAM_USAGE_NAVIGATION) {
1107         return AV_ERR_INVALID_VAL;
1108     }
1109     Format format;
1110     (void)format.PutIntValue(PlayerKeys::STREAM_USAGE, streamUsage);
1111     (void)format.PutIntValue(PlayerKeys::CONTENT_TYPE, 0);
1112     (void)format.PutIntValue(PlayerKeys::RENDERER_FLAG, 0);
1113     int32_t ret = playerObj->player_->SetParameter(format);
1114     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioRendererInfo failed");
1115     return AV_ERR_OK;
1116 }
1117 
OH_AVPlayer_SetAudioInterruptMode(OH_AVPlayer *player, OH_AudioInterrupt_Mode interruptMode)1118 OH_AVErrCode OH_AVPlayer_SetAudioInterruptMode(OH_AVPlayer *player, OH_AudioInterrupt_Mode interruptMode)
1119 {
1120     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1121     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1122     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1123     if (interruptMode < OH_AudioInterrupt_Mode::AUDIOSTREAM_INTERRUPT_MODE_SHARE ||
1124         interruptMode > OH_AudioInterrupt_Mode::AUDIOSTREAM_INTERRUPT_MODE_INDEPENDENT) {
1125         return AV_ERR_INVALID_VAL;
1126     }
1127     Format format;
1128     (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_MODE, interruptMode);
1129     int32_t ret = playerObj->player_->SetParameter(format);
1130     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioInterruptMode failed");
1131     return AV_ERR_OK;
1132 }
1133 
OH_AVPlayer_SetAudioEffectMode(OH_AVPlayer *player, OH_AudioStream_AudioEffectMode effectMode)1134 OH_AVErrCode OH_AVPlayer_SetAudioEffectMode(OH_AVPlayer *player, OH_AudioStream_AudioEffectMode effectMode)
1135 {
1136     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1137     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1138     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1139     if (effectMode < OH_AudioStream_AudioEffectMode::EFFECT_NONE ||
1140         effectMode > OH_AudioStream_AudioEffectMode::EFFECT_DEFAULT) {
1141         return AV_ERR_INVALID_VAL;
1142     }
1143     Format format;
1144     (void)format.PutIntValue(PlayerKeys::AUDIO_EFFECT_MODE, effectMode);
1145     int32_t ret = playerObj->player_->SetParameter(format);
1146     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioEffectMode failed");
1147     return AV_ERR_OK;
1148 }
1149 
OH_AVPlayer_SelectBitRate(OH_AVPlayer *player, uint32_t bitRate)1150 OH_AVErrCode OH_AVPlayer_SelectBitRate(OH_AVPlayer *player, uint32_t bitRate)
1151 {
1152     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1153     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1154     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1155     int32_t ret = playerObj->player_->SelectBitRate(bitRate);
1156     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SelectBitRate failed");
1157     return AV_ERR_OK;
1158 }
1159 
OH_AVPlayer_GetDuration(OH_AVPlayer *player, int32_t *duration)1160 OH_AVErrCode OH_AVPlayer_GetDuration(OH_AVPlayer *player, int32_t *duration)
1161 {
1162     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1163     CHECK_AND_RETURN_RET_LOG(duration != nullptr, AV_ERR_INVALID_VAL, "duration is nullptr");
1164     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1165     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1166     int32_t ret = playerObj->player_->GetDuration(*duration);
1167     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetDuration failed");
1168     return AV_ERR_OK;
1169 }
1170 
OH_AVPlayer_GetState(OH_AVPlayer *player, AVPlayerState *state)1171 OH_AVErrCode OH_AVPlayer_GetState(OH_AVPlayer *player, AVPlayerState *state)
1172 {
1173     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1174     CHECK_AND_RETURN_RET_LOG(state != nullptr, AV_ERR_INVALID_VAL, "state is nullptr");
1175     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1176     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1177     if (playerObj->isReleased_.load()) {
1178         *state = AVPlayerState::AV_RELEASED;
1179         return AV_ERR_OK;
1180     }
1181     for (uint32_t i = 0; i < STATE_MAP_LENGTH; i++) {
1182         if (g_stateMap[i].playerStates == player->state_) {
1183             *state = g_stateMap[i].avPlayerState;
1184             return AV_ERR_OK;
1185         }
1186     }
1187 
1188     *state = AV_ERROR;
1189     return AV_ERR_OK;
1190 }
1191 
1192 #ifdef SUPPORT_VIDEO
OH_AVPlayer_SetVideoSurface(OH_AVPlayer *player, OHNativeWindow *window)1193 OH_AVErrCode  OH_AVPlayer_SetVideoSurface(OH_AVPlayer *player, OHNativeWindow *window)
1194 {
1195     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1196     CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_ERR_INVALID_VAL, "Window is nullptr!");
1197     CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_ERR_INVALID_VAL, "Input window surface is nullptr!");
1198     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1199     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1200     int32_t ret = playerObj->player_->SetVideoSurface(window->surface);
1201     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "SetVideoSurface failed!");
1202     return AV_ERR_OK;
1203 }
1204 #endif
1205 
OH_AVPlayer_IsPlaying(OH_AVPlayer *player)1206 bool OH_AVPlayer_IsPlaying(OH_AVPlayer *player)
1207 {
1208     CHECK_AND_RETURN_RET_LOG(player != nullptr, false, "input player is nullptr!");
1209     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1210     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, false, "player_ is null");
1211     return playerObj->player_->IsPlaying();
1212 }
1213 
OH_AVPlayer_IsLooping(OH_AVPlayer *player)1214 bool OH_AVPlayer_IsLooping(OH_AVPlayer *player)
1215 {
1216     CHECK_AND_RETURN_RET_LOG(player != nullptr, false, "input player is nullptr!");
1217     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1218     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, false, "player_ is null");
1219     return playerObj->player_->IsLooping();
1220 }
1221 
OH_AVPlayer_SetLooping(OH_AVPlayer *player, bool loop)1222 OH_AVErrCode OH_AVPlayer_SetLooping(OH_AVPlayer *player, bool loop)
1223 {
1224     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1225     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1226     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1227     int32_t ret = playerObj->player_->SetLooping(loop);
1228     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetLooping failed");
1229     return AV_ERR_OK;
1230 }
1231 
OH_AVPlayer_SetPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback)1232 OH_AVErrCode OH_AVPlayer_SetPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback)
1233 {
1234     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1235     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1236     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1237     CHECK_AND_RETURN_RET_LOG(callback.onInfo != nullptr, AV_ERR_INVALID_VAL, "onInfo is null");
1238     CHECK_AND_RETURN_RET_LOG(callback.onError != nullptr, AV_ERR_INVALID_VAL, "onError is null");
1239     if (playerObj->callback_ == nullptr) {
1240         NativeAVPlayerCallback *avplayerCallback =
1241             new (std::nothrow) NativeAVPlayerCallback(player, callback);
1242         CHECK_AND_RETURN_RET_LOG(avplayerCallback != nullptr, AV_ERR_NO_MEMORY, "avplayerCallback is nullptr!");
1243         playerObj->callback_ = std::shared_ptr<NativeAVPlayerCallback>(avplayerCallback);
1244     } else {
1245         playerObj->callback_->SetPlayCallback(callback);
1246     }
1247     int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1248     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetPlayerCallback failed");
1249     return AV_ERR_OK;
1250 }
1251 
OH_AVPlayer_SelectTrack(OH_AVPlayer *player, int32_t index)1252 OH_AVErrCode OH_AVPlayer_SelectTrack(OH_AVPlayer *player, int32_t index)
1253 {
1254     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1255     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1256     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1257     int32_t ret = playerObj->player_->SelectTrack(index);
1258     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SelectTrack failed");
1259     return AV_ERR_OK;
1260 }
1261 
OH_AVPlayer_DeselectTrack(OH_AVPlayer *player, int32_t index)1262 OH_AVErrCode OH_AVPlayer_DeselectTrack(OH_AVPlayer *player, int32_t index)
1263 {
1264     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1265     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1266     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1267     int32_t ret = playerObj->player_->DeselectTrack(index);
1268     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player DeselectTrack failed");
1269     return AV_ERR_OK;
1270 }
1271 
OH_AVPlayer_GetCurrentTrack(OH_AVPlayer *player, int32_t trackType, int32_t *index)1272 OH_AVErrCode OH_AVPlayer_GetCurrentTrack(OH_AVPlayer *player, int32_t trackType, int32_t *index)
1273 {
1274     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1275     CHECK_AND_RETURN_RET_LOG(index != nullptr, AV_ERR_INVALID_VAL, "index is nullptr");
1276     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1277     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1278     int32_t ret = playerObj->player_->GetCurrentTrack(trackType, *index);
1279     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetCurrentTrack failed");
1280     return AV_ERR_OK;
1281 }
1282 
OH_AVPlayer_SetMediaKeySystemInfoCallback(OH_AVPlayer *player, Player_MediaKeySystemInfoCallback callback)1283 OH_AVErrCode OH_AVPlayer_SetMediaKeySystemInfoCallback(OH_AVPlayer *player,
1284     Player_MediaKeySystemInfoCallback callback)
1285 {
1286 #ifdef SUPPORT_AVPLAYER_DRM
1287     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1288     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1289     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1290     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_ERR_INVALID_VAL, "MediaKeySystemInfoCallback is null");
1291 
1292     if (playerObj->callback_ == nullptr) {
1293         static AVPlayerCallback playCallback = { nullptr, nullptr };
1294         playerObj->callback_ = std::make_shared<NativeAVPlayerCallback>(player, playCallback);
1295         int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1296         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1297         ret = playerObj->callback_->SetDrmSystemInfoCallback(callback);
1298         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1299     } else {
1300         int32_t ret = playerObj->callback_->SetDrmSystemInfoCallback(callback);
1301         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1302     }
1303 #else
1304     (void)player;
1305     (void)callback;
1306 #endif
1307     return AV_ERR_OK;
1308 }
1309 
OH_AVPlayer_GetMediaKeySystemInfo(OH_AVPlayer *player, DRM_MediaKeySystemInfo *mediaKeySystemInfo)1310 OH_AVErrCode OH_AVPlayer_GetMediaKeySystemInfo(OH_AVPlayer *player, DRM_MediaKeySystemInfo *mediaKeySystemInfo)
1311 {
1312 #ifdef SUPPORT_AVPLAYER_DRM
1313     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1314     CHECK_AND_RETURN_RET_LOG(mediaKeySystemInfo != nullptr, AV_ERR_INVALID_VAL, "mediaKeySystemInfo is nullptr");
1315     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1316     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1317     CHECK_AND_RETURN_RET_LOG(!playerObj->localDrmInfos_.empty(), AV_ERR_INVALID_VAL, "localDrmInfos_ is null");
1318     mediaKeySystemInfo->psshCount = playerObj->localDrmInfos_.size();
1319     int index = 0;
1320     for (auto item : playerObj->localDrmInfos_) {
1321         int ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].uuid, item.first.size(),
1322             item.first.data(), item.first.size());
1323         CHECK_AND_RETURN_RET_LOG(ret ==0, AV_ERR_INVALID_VAL, "no memory");
1324         ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].data, item.second.size(),
1325             item.second.data(), item.second.size());
1326         CHECK_AND_RETURN_RET_LOG(ret ==0, AV_ERR_INVALID_VAL, "no memory");
1327         mediaKeySystemInfo->psshInfo[index++].dataLen = static_cast<int32_t>(item.second.size());
1328     }
1329 #else
1330     (void)player;
1331     (void)mediaKeySystemInfo;
1332 #endif
1333     return AV_ERR_OK;
1334 }
1335 
OH_AVPlayer_SetDecryptionConfig(OH_AVPlayer *player, MediaKeySession *mediaKeySession, bool secureVideoPath)1336 OH_AVErrCode OH_AVPlayer_SetDecryptionConfig(OH_AVPlayer *player, MediaKeySession *mediaKeySession,
1337     bool secureVideoPath)
1338 {
1339 #ifdef SUPPORT_AVPLAYER_DRM
1340     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1341     CHECK_AND_RETURN_RET_LOG(mediaKeySession != nullptr, AV_ERR_INVALID_VAL, "mediaKeySession is nullptr");
1342     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1343     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1344     struct MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySession);
1345     CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, AV_ERR_INVALID_VAL, "sessionObject is null");
1346     CHECK_AND_RETURN_RET_LOG(sessionObject->sessionImpl_ != nullptr, AV_ERR_INVALID_VAL, "sessionObject is null");
1347     int32_t ret =
1348         playerObj->player_->SetDecryptConfig(sessionObject->sessionImpl_->GetMediaKeySessionServiceProxy(),
1349             secureVideoPath);
1350     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDecryptConfig failed");
1351 #else
1352     (void)player;
1353     (void)mediaKeySession;
1354     (void)secureVideoPath;
1355 #endif
1356     return AV_ERR_OK;
1357 }
1358 
AVPlayerSetPlayerCallback(OH_AVPlayer *player, struct PlayerObject *playerObj, bool isUnregister)1359 static OH_AVErrCode AVPlayerSetPlayerCallback(OH_AVPlayer *player, struct PlayerObject *playerObj, bool isUnregister)
1360 {
1361     MEDIA_LOGD("AVPlayerSetPlayerCallback S");
1362     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1363     CHECK_AND_RETURN_RET_LOG(playerObj != nullptr && playerObj->player_ != nullptr, AV_ERR_INVALID_VAL,
1364         "player_ is nullptr!");
1365     if (!isUnregister && playerObj->callback_ == nullptr) {
1366         MEDIA_LOGI("AVPlayerSetPlayerCallback create dummy NativeAVPlayerCallback");
1367         AVPlayerCallback dummyCallback = {
1368             .onInfo = nullptr,
1369             .onError = nullptr
1370         };
1371         NativeAVPlayerCallback *avplayerCallback =
1372             new (std::nothrow) NativeAVPlayerCallback(player, dummyCallback);
1373         CHECK_AND_RETURN_RET_LOG(avplayerCallback != nullptr, AV_ERR_NO_MEMORY, "avplayerCallback is nullptr!");
1374         playerObj->callback_ = std::shared_ptr<NativeAVPlayerCallback>(avplayerCallback);
1375 
1376         int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1377         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "AVPlayerSetPlayerCallback failed!");
1378     }
1379     MEDIA_LOGD("AVPlayerSetPlayerCallback E");
1380     return AV_ERR_OK;
1381 }
1382 
OH_AVPlayer_SetOnErrorCallback(OH_AVPlayer *player, OH_AVPlayerOnErrorCallback callback, void *userData)1383 OH_AVErrCode OH_AVPlayer_SetOnErrorCallback(OH_AVPlayer *player, OH_AVPlayerOnErrorCallback callback, void *userData)
1384 {
1385     MEDIA_LOGD("OH_AVPlayer_SetOnErrorCallback S");
1386     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "SetOnError input player is nullptr!");
1387     if (callback == nullptr) {
1388         MEDIA_LOGI("SetOnError callback is nullptr, unregister callback.");
1389     }
1390     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1391     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "SetOnError player_ is nullptr");
1392 
1393     OH_AVErrCode errCode = AVPlayerSetPlayerCallback(player, playerObj, callback == nullptr);
1394     CHECK_AND_RETURN_RET_LOG(errCode == AV_ERR_OK, errCode, "AVPlayerSetPlayerCallback AVPlayerOnError error");
1395 
1396     if (playerObj->callback_ == nullptr || playerObj->callback_->SetOnErrorCallback(callback, userData) != AV_ERR_OK) {
1397         MEDIA_LOGE("OH_AVPlayer_SetOnErrorCallback error");
1398         return AV_ERR_NO_MEMORY;
1399     }
1400     MEDIA_LOGD("OH_AVPlayer_SetOnErrorCallback success E");
1401     return AV_ERR_OK;
1402 }
1403 
OH_AVPlayer_SetOnInfoCallback(OH_AVPlayer *player, OH_AVPlayerOnInfoCallback callback, void *userData)1404 OH_AVErrCode OH_AVPlayer_SetOnInfoCallback(OH_AVPlayer *player, OH_AVPlayerOnInfoCallback callback, void *userData)
1405 {
1406     MEDIA_LOGD("OH_AVPlayer_SetOnInfoCallback S");
1407     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "SetOnInfo input player is nullptr!");
1408     if (callback == nullptr) {
1409         MEDIA_LOGI("SetOnInfo callback is nullptr, unregister callback.");
1410     }
1411     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1412     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "SetOnInfo player_ is nullptr");
1413 
1414     OH_AVErrCode errCode = AVPlayerSetPlayerCallback(player, playerObj, callback == nullptr);
1415     CHECK_AND_RETURN_RET_LOG(errCode == AV_ERR_OK, errCode, "AVPlayerSetPlayerCallback AVPlayerOnInfoCallback error");
1416 
1417     if (playerObj->callback_ == nullptr || playerObj->callback_->SetOnInfoCallback(callback, userData) != AV_ERR_OK) {
1418         MEDIA_LOGE("OH_AVPlayer_SetOnInfoCallback error");
1419         return AV_ERR_NO_MEMORY;
1420     }
1421     MEDIA_LOGD("OH_AVPlayer_SetOnInfoCallback success E");
1422     return AV_ERR_OK;
1423 }