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