• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
399     if (callback_.onError != nullptr) {
400         // To make sure compatibility only convert for UNSUPPORT_FORMAT_ERROR_CODE
401         avErrorCode = errorCode;
402         if (errorCode == UNSUPPORT_FORMAT_ERROR_CODE) {
403             avErrorCode = AVPLAYER_ERR_UNSUPPORT;
404         }
405         callback_.onError(player_, avErrorCode, errorMsg.c_str());
406     }
407 }
408 
409 #ifdef SUPPORT_AVPLAYER_DRM
SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback)410 int32_t NativeAVPlayerCallback::SetDrmSystemInfoCallback(Player_MediaKeySystemInfoCallback drmSystemInfoCallback)
411 {
412     std::lock_guard<std::mutex> lock(mutex_);
413     drmsysteminfocallback_ = drmSystemInfoCallback;
414     return AV_ERR_OK;
415 }
416 
GetDrmSystemInfos(const Format & infoBody,DRM_MediaKeySystemInfo * mediaKeySystemInfo,struct PlayerObject * playerObj)417 int32_t NativeAVPlayerCallback::GetDrmSystemInfos(const Format &infoBody,
418     DRM_MediaKeySystemInfo *mediaKeySystemInfo, struct PlayerObject *playerObj)
419 {
420     if (!infoBody.ContainKey(std::string(PlayerKeys::PLAYER_DRM_INFO_ADDR))) {
421         MEDIA_LOGW("there's no drminfo-update drm_info_addr key");
422         return AV_ERR_INVALID_VAL;
423     }
424     if (!infoBody.ContainKey(std::string(PlayerKeys::PLAYER_DRM_INFO_COUNT))) {
425         MEDIA_LOGW("there's no drminfo-update drm_info_count key");
426         return AV_ERR_INVALID_VAL;
427     }
428     uint8_t *drmInfoAddr = nullptr;
429     size_t size  = 0;
430     int32_t infoCount = 0;
431     infoBody.GetBuffer(std::string(PlayerKeys::PLAYER_DRM_INFO_ADDR), &drmInfoAddr, size);
432     CHECK_AND_RETURN_RET_LOG(drmInfoAddr != nullptr && size > 0, AV_ERR_INVALID_VAL, "get drminfo buffer failed");
433     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_DRM_INFO_COUNT), infoCount);
434     CHECK_AND_RETURN_RET_LOG(infoCount > 0, AV_ERR_INVALID_VAL, "get drminfo count is illegal");
435     DrmInfoItem *drmInfos = reinterpret_cast<DrmInfoItem*>(drmInfoAddr);
436     CHECK_AND_RETURN_RET_LOG(drmInfos != nullptr, AV_ERR_INVALID_VAL, "cast drmInfos nullptr");
437     for (int32_t i = 0; i < infoCount; i++) {
438         DrmInfoItem temp = drmInfos[i];
439         std::vector<uint8_t> uuid(temp.uuid, temp.uuid + DrmConstant::DRM_MAX_M3U8_DRM_UUID_LEN);
440         std::vector<uint8_t> pssh(temp.pssh, temp.pssh + temp.psshLen);
441         playerObj->localDrmInfos_.insert({ uuid, pssh });
442     }
443     int index = 0;
444     for (auto item : playerObj->localDrmInfos_) {
445         int ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].uuid,
446             item.first.size(), item.first.data(), item.first.size());
447         int err = memcpy_s(mediaKeySystemInfo->psshInfo[index].data, item.second.size(),
448             item.second.data(), item.second.size());
449         CHECK_AND_RETURN_RET_LOG((err == 0 && ret == 0), AV_ERR_INVALID_VAL, "cast drmInfos nullptr");
450         mediaKeySystemInfo->psshInfo[index++].dataLen = static_cast<int32_t>(item.second.size());
451     }
452     mediaKeySystemInfo->psshCount = index;
453     return AV_ERR_OK;
454 }
455 #endif
456 
SetPlayCallback(AVPlayerCallback callback)457 int32_t NativeAVPlayerCallback::SetPlayCallback(AVPlayerCallback callback)
458 {
459     std::lock_guard<std::mutex> lock(mutex_);
460     callback_ = callback;
461     return AV_ERR_OK;
462 }
463 
SetOnErrorCallback(OH_AVPlayerOnErrorCallback callback,void * userData)464 int32_t NativeAVPlayerCallback::SetOnErrorCallback(OH_AVPlayerOnErrorCallback callback, void *userData)
465 {
466     std::lock_guard<std::mutex> lock(mutex_);
467     if (callback != nullptr) {
468         NativeAVPlayerOnErrorCallback *errorCallback =
469             new (std::nothrow) NativeAVPlayerOnErrorCallback(callback, userData);
470         CHECK_AND_RETURN_RET_LOG(errorCallback != nullptr, AV_ERR_NO_MEMORY, "errorCallback is nullptr!");
471         errorCallback_ = std::shared_ptr<NativeAVPlayerOnErrorCallback>(errorCallback);
472     } else {
473         errorCallback_ = nullptr;
474     }
475     return AV_ERR_OK;
476 }
477 
SetOnInfoCallback(OH_AVPlayerOnInfoCallback callback,void * userData)478 int32_t NativeAVPlayerCallback::SetOnInfoCallback(OH_AVPlayerOnInfoCallback callback, void *userData)
479 {
480     std::lock_guard<std::mutex> lock(mutex_);
481     if (callback != nullptr) {
482         NativeAVPlayerOnInfoCallback *onInfoCallback =
483             new (std::nothrow) NativeAVPlayerOnInfoCallback(callback, userData);
484         CHECK_AND_RETURN_RET_LOG(onInfoCallback != nullptr, AV_ERR_NO_MEMORY, "infoCallback_ is nullptr!");
485         infoCallback_ = std::shared_ptr<NativeAVPlayerOnInfoCallback>(onInfoCallback);
486     } else {
487         infoCallback_ = nullptr;
488     }
489     return AV_ERR_OK;
490 }
491 
Start()492 void NativeAVPlayerCallback::Start()
493 {
494     isSourceLoaded_.store(true);
495 }
496 
Pause()497 void NativeAVPlayerCallback::Pause()
498 {
499     isSourceLoaded_.store(false);
500 }
501 
OnSeekDoneCb(const int32_t extra,const Format & infoBody)502 void NativeAVPlayerCallback::OnSeekDoneCb(const int32_t extra, const Format &infoBody)
503 {
504     (void)infoBody;
505     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnSeekDoneCb current source is unready");
506     int32_t currentPositon = extra;
507     MEDIA_LOGI("0x%{public}06" PRIXPTR " seekDone %{public}d", FAKE_POINTER(this), currentPositon);
508 
509     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
510     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSeekDoneCb OH_AVFormat create failed");
511     avFormat->format_.PutIntValue(OH_PLAYER_SEEK_POSITION, currentPositon);
512     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SEEKDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
513 }
514 
OnSpeedDoneCb(const int32_t extra,const Format & infoBody)515 void NativeAVPlayerCallback::OnSpeedDoneCb(const int32_t extra, const Format &infoBody)
516 {
517     (void)infoBody;
518     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnSpeedDoneCb current source is unready");
519     int32_t speedMode = extra;
520     MEDIA_LOGI("SpeedDone %{public}d", speedMode);
521 
522     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
523     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSpeedDoneCb OH_AVFormat create failed");
524     avFormat->format_.PutIntValue(OH_PLAYER_PLAYBACK_SPEED, speedMode);
525     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SPEEDDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
526 }
527 
OnBitRateDoneCb(const int32_t extra,const Format & infoBody)528 void NativeAVPlayerCallback::OnBitRateDoneCb(const int32_t extra, const Format &infoBody)
529 {
530     (void)infoBody;
531     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBitRateDoneCb current source is unready");
532     int32_t bitRate = extra;
533     MEDIA_LOGI("Bitrate done %{public}d", bitRate);
534 
535     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
536     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBitRateDoneCb OH_AVFormat create failed");
537     avFormat->format_.PutIntValue(OH_PLAYER_BITRATE, bitRate);
538     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BITRATEDONE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
539 }
540 
OnEosCb(const int32_t extra,const Format & infoBody)541 void NativeAVPlayerCallback::OnEosCb(const int32_t extra, const Format &infoBody)
542 {
543     (void)infoBody;
544     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnEosCb current source is unready");
545     int32_t isLooping = extra;
546     MEDIA_LOGI("0x%{public}06" PRIXPTR " EOS is called, isloop: %{public}d", FAKE_POINTER(this), isLooping);
547 
548     infoCallback_->OnInfo(player_, AV_INFO_TYPE_EOS, nullptr);
549 }
550 
OnStateChangeCb(const int32_t extra,const Format & infoBody)551 void NativeAVPlayerCallback::OnStateChangeCb(const int32_t extra, const Format &infoBody)
552 {
553     MEDIA_LOGI("OnStateChangeCb() is called, state %{public}d", extra);
554     if (extra == static_cast<int32_t>(PLAYER_RELEASED)) {
555         isReleased_.store(true);
556     }
557     if (player_ == nullptr || infoCallback_ == nullptr) {
558         return;
559     }
560     PlayerStates state = static_cast<PlayerStates>(extra);
561     player_->state_ = state;
562     for (uint32_t i = 0; i < STATE_MAP_LENGTH; i++) {
563         if (STATE_MAP[i].playerStates != state) {
564             continue;
565         }
566         AVPlayerState convertState = STATE_MAP[i].avPlayerState;
567         OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
568         CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnStateChangeCb OH_AVFormat create failed");
569 
570         int32_t reason = StateChangeReason::USER;
571         if (infoBody.ContainKey(PlayerKeys::PLAYER_STATE_CHANGED_REASON)) {
572             (void)infoBody.GetIntValue(PlayerKeys::PLAYER_STATE_CHANGED_REASON, reason);
573         }
574         avFormat->format_.PutIntValue(OH_PLAYER_STATE_CHANGE_REASON, static_cast<int32_t>(reason));
575         avFormat->format_.PutIntValue(OH_PLAYER_STATE, static_cast<int32_t>(convertState));
576         infoCallback_->OnInfo(player_, AV_INFO_TYPE_STATE_CHANGE,
577             reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
578         return;
579     }
580 }
581 
OnPositionUpdateCb(const int32_t extra,const Format & infoBody)582 void NativeAVPlayerCallback::OnPositionUpdateCb(const int32_t extra, const Format &infoBody)
583 {
584     (void)infoBody;
585     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnPositionUpdateCb current source is unready");
586     int32_t position = extra;
587     MEDIA_LOGD("OnPositionUpdateCb is called, position: %{public}d", position);
588 
589     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
590     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnPositionUpdateCb OH_AVFormat create failed");
591     avFormat->format_.PutIntValue(OH_PLAYER_CURRENT_POSITION, position);
592     infoCallback_->OnInfo(player_, AV_INFO_TYPE_POSITION_UPDATE,
593         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
594 }
595 
OnVolumeChangeCb(const int32_t extra,const Format & infoBody)596 void NativeAVPlayerCallback::OnVolumeChangeCb(const int32_t extra, const Format &infoBody)
597 {
598     (void)extra;
599     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnVolumeChangeCb current source is unready");
600     float volumeLevel = 0.0;
601     (void)infoBody.GetFloatValue(PlayerKeys::PLAYER_VOLUME_LEVEL, volumeLevel);
602     MEDIA_LOGD("OnVolumeChangeCb in volume=%{public}f", volumeLevel);
603 
604     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
605     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnVolumeChangeCb OH_AVFormat create failed");
606     avFormat->format_.PutFloatValue(OH_PLAYER_VOLUME, volumeLevel);
607     infoCallback_->OnInfo(player_, AV_INFO_TYPE_VOLUME_CHANGE,
608         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
609 }
610 
OnMessageCb(const int32_t extra,const Format & infoBody)611 void NativeAVPlayerCallback::OnMessageCb(const int32_t extra, const Format &infoBody)
612 {
613     (void)infoBody;
614     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnMessageCb current source is unready");
615     int32_t messageType = extra;
616     MEDIA_LOGI("OnMessageCb is called, extra: %{public}d", messageType);
617     if (extra == PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START) {
618         OnStartRenderFrameCb();
619     }
620 }
621 
OnStartRenderFrameCb() const622 void NativeAVPlayerCallback::OnStartRenderFrameCb() const
623 {
624     MEDIA_LOGI("OnStartRenderFrameCb is called");
625     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnStartRenderFrameCb current source is unready");
626 
627     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
628     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnStartRenderFrameCb OH_AVFormat create failed");
629     avFormat->format_.PutIntValue(OH_PLAYER_MESSAGE_TYPE, 1); // 1 means PLAYER_INFO_VIDEO_RENDERING_START
630     infoCallback_->OnInfo(player_, AV_INFO_TYPE_MESSAGE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
631 }
632 
OnVideoSizeChangedCb(const int32_t extra,const Format & infoBody)633 void NativeAVPlayerCallback::OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody)
634 {
635     (void)extra;
636     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnVideoSizeChangedCb current source is unready");
637     int32_t width = 0;
638     int32_t height = 0;
639     (void)infoBody.GetIntValue(PlayerKeys::PLAYER_WIDTH, width);
640     (void)infoBody.GetIntValue(PlayerKeys::PLAYER_HEIGHT, height);
641     MEDIA_LOGI("0x%{public}06" PRIXPTR " sizeChange w %{public}d h %{public}d", FAKE_POINTER(this), width, height);
642 
643     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
644     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnVideoSizeChangedCb OH_AVFormat create failed");
645     avFormat->format_.PutIntValue(OH_PLAYER_VIDEO_WIDTH, width);
646     avFormat->format_.PutIntValue(OH_PLAYER_VIDEO_HEIGHT, height);
647     infoCallback_->OnInfo(player_, AV_INFO_TYPE_RESOLUTION_CHANGE,
648         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
649 }
650 
OnBufferingUpdateCb(const int32_t extra,const Format & infoBody)651 void NativeAVPlayerCallback::OnBufferingUpdateCb(const int32_t extra, const Format &infoBody)
652 {
653     (void)extra;
654     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBufferingUpdateCb current source is unready");
655     int32_t val = 0;
656     AVPlayerBufferingType bufferingType;
657     if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_START))) {
658         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_START;
659         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_START), val);
660     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_END))) {
661         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_END;
662         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_END), val);
663     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT))) {
664         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_PERCENT;
665         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT), val);
666     } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_CACHED_DURATION))) {
667         bufferingType = AVPlayerBufferingType::AVPLAYER_BUFFERING_CACHED_DURATION;
668         (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_CACHED_DURATION), val);
669     } else {
670         return;
671     }
672     MEDIA_LOGD("OnBufferingUpdateCb is called, buffering type: %{public}d value: %{public}d",
673         static_cast<int32_t>(bufferingType), val);
674 
675     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
676     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBufferingUpdateCb OH_AVFormat create failed");
677     avFormat->format_.PutIntValue(OH_PLAYER_BUFFERING_TYPE, static_cast<int32_t>(bufferingType));
678     avFormat->format_.PutIntValue(OH_PLAYER_BUFFERING_VALUE, val);
679     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BUFFERING_UPDATE,
680         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
681 }
682 
OnBitRateCollectedCb(const int32_t extra,const Format & infoBody)683 void NativeAVPlayerCallback::OnBitRateCollectedCb(const int32_t extra, const Format &infoBody)
684 {
685     (void)extra;
686     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnBitRateCollectedCb current source is unready");
687 
688     uint8_t *addr = nullptr;
689     size_t size  = 0;
690     if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_AVAILABLE_BITRATES))) {
691         infoBody.GetBuffer(std::string(PlayerKeys::PLAYER_AVAILABLE_BITRATES), &addr, size);
692     }
693     CHECK_AND_RETURN_LOG(addr != nullptr, "bitRates addr is nullptr");
694     size_t bitRatesCount = static_cast<size_t>(size / sizeof(uint32_t));
695     CHECK_AND_RETURN_LOG(bitRatesCount > 0, "bitRates size(%{public}zu) is invalid", size);
696     MEDIA_LOGI("bitRates count: %{public}zu", bitRatesCount);
697     for (size_t i = 0; i < bitRatesCount; i++) {
698         MEDIA_LOGI("bitRates[%{public}zu]: %{public}u", i, *(static_cast<uint32_t*>(static_cast<void*>(addr)) + i));
699     }
700 
701     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
702     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnBitRateCollectedCb OH_AVFormat create failed");
703     avFormat->format_.PutBuffer(OH_PLAYER_BITRATE_ARRAY, addr, static_cast<size_t>(bitRatesCount * sizeof(uint32_t)));
704     infoCallback_->OnInfo(player_, AV_INFO_TYPE_BITRATE_COLLECT,
705         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
706 }
707 
OnAudioInterruptCb(const int32_t extra,const Format & infoBody)708 void NativeAVPlayerCallback::OnAudioInterruptCb(const int32_t extra, const Format &infoBody)
709 {
710     (void)extra;
711     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnAudioInterruptCb current source is unready");
712     int32_t eventType = 0;
713     int32_t forceType = 0;
714     int32_t hintType = 0;
715     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_TYPE, eventType);
716     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_FORCE, forceType);
717     (void)infoBody.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_HINT, hintType);
718     MEDIA_LOGI("OnAudioInterruptCb is called, eventType: %{public}d, forceType: %{public}d, hintType: %{public}d",
719         eventType, forceType, hintType);
720 
721     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
722     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnAudioInterruptCb OH_AVFormat create failed");
723     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_TYPE, eventType);
724     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_FORCE, forceType);
725     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_INTERRUPT_HINT, hintType);
726     infoCallback_->OnInfo(player_, AV_INFO_TYPE_INTERRUPT_EVENT,
727         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
728 }
729 
OnDurationUpdateCb(const int32_t extra,const Format & infoBody)730 void NativeAVPlayerCallback::OnDurationUpdateCb(const int32_t extra, const Format &infoBody)
731 {
732     (void)infoBody;
733     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnDurationUpdateCb current source is unready");
734     int64_t duration = extra;
735     MEDIA_LOGI("0x%{public}06" PRIXPTR " duration update %{public}" PRId64, FAKE_POINTER(this), duration);
736 
737     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
738     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnDurationUpdateCb OH_AVFormat create failed");
739     avFormat->format_.PutLongValue(OH_PLAYER_DURATION, duration);
740     infoCallback_->OnInfo(player_, AV_INFO_TYPE_DURATION_UPDATE,
741         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
742 }
743 
OnNotifyIsLiveStream(const int32_t extra,const Format & infoBody)744 void NativeAVPlayerCallback::OnNotifyIsLiveStream(const int32_t extra, const Format &infoBody)
745 {
746     (void)extra;
747     (void)infoBody;
748     MEDIA_LOGI("0x%{public}06" PRIXPTR " OnNotifyIsLiveStream extra: %{public}d", FAKE_POINTER(this), extra);
749 
750     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
751     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnNotifyIsLiveStream OH_AVFormat create failed");
752     avFormat->format_.PutIntValue(OH_PLAYER_IS_LIVE_STREAM, 1); // 1 means is live stream
753     infoCallback_->OnInfo(player_, AV_INFO_TYPE_IS_LIVE_STREAM,
754         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
755 }
756 
OnTrackChangedCb(const int32_t extra,const Format & infoBody)757 void NativeAVPlayerCallback::OnTrackChangedCb(const int32_t extra, const Format &infoBody)
758 {
759     (void)extra;
760     int32_t trackIndex = -1;
761     int32_t isSelect = -1;
762     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), trackIndex);
763     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_IS_SELECT), isSelect);
764     MEDIA_LOGI("OnTrackChangedCb trackIndex: %{public}d, isSelect: %{public}d", trackIndex, isSelect);
765     CHECK_AND_RETURN_LOG(trackIndex != -1 && isSelect != -1, "invalid trackIndex: %{public}d, isSelect: %{public}d",
766         trackIndex, isSelect);
767     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
768     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnTrackChangedCb OH_AVFormat create failed");
769     avFormat->format_.PutIntValue(OH_PLAYER_TRACK_INDEX, trackIndex);
770     avFormat->format_.PutIntValue(OH_PLAYER_TRACK_IS_SELECT, isSelect);
771     infoCallback_->OnInfo(player_, AV_INFO_TYPE_TRACKCHANGE, reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
772 }
773 
OnTrackInfoUpdate(const int32_t extra,const Format & infoBody)774 void NativeAVPlayerCallback::OnTrackInfoUpdate(const int32_t extra, const Format &infoBody)
775 {
776     (void)extra;
777     (void)extra;
778     MEDIA_LOGI("OnTrackInfoUpdate not support");
779 }
780 
OnSubtitleInfoCb(const int32_t extra,const Format & infoBody)781 void NativeAVPlayerCallback::OnSubtitleInfoCb(const int32_t extra, const Format &infoBody)
782 {
783     (void)extra;
784     int32_t pts = -1;
785     int32_t duration = -1;
786     std::string text;
787     infoBody.GetStringValue(PlayerKeys::SUBTITLE_TEXT, text);
788     infoBody.GetIntValue(std::string(PlayerKeys::SUBTITLE_PTS), pts);
789     infoBody.GetIntValue(std::string(PlayerKeys::SUBTITLE_DURATION), duration);
790     MEDIA_LOGI("OnSubtitleInfoCb pts: %{public}d, duration: %{public}d", pts, duration);
791 
792     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
793     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnSubtitleInfoCb OH_AVFormat create failed");
794     avFormat->format_.PutStringValue(OH_PLAYER_SUBTITLE_TEXT, text);
795     avFormat->format_.PutIntValue(OH_PLAYER_SUBTITLE_PTS, pts);
796     avFormat->format_.PutIntValue(OH_PLAYER_SUBTITLE_DURATION, duration);
797     infoCallback_->OnInfo(player_, AV_INFO_TYPE_SUBTITLE_UPDATE,
798         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
799 }
800 
OnAudioDeviceChangeCb(const int32_t extra,const Format & infoBody)801 void NativeAVPlayerCallback::OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody)
802 {
803     (void)extra;
804     CHECK_AND_RETURN_LOG(isSourceLoaded_.load(), "OnAudioDeviceChangeCb current source is unready");
805 
806     int32_t reason = 0; // means UNKOWN, see OH_AudioStream_DeviceChangeReason
807     infoBody.GetIntValue(PlayerKeys::AUDIO_DEVICE_CHANGE_REASON, reason);
808     MEDIA_LOGI("0x%{public}06" PRIXPTR " OnAudioDeviceChangeCb reason: %{public}d", FAKE_POINTER(this), reason);
809 
810     OHOS::sptr<OH_AVFormat> avFormat = new (std::nothrow) OH_AVFormat();
811     CHECK_AND_RETURN_LOG(avFormat != nullptr, "OnAudioDeviceChangeCb OH_AVFormat create failed");
812     // We only report AUDIO_DEVICE_CHANGE_REASON at this stage.
813     avFormat->format_.PutIntValue(OH_PLAYER_AUDIO_DEVICE_CHANGE_REASON, reason);
814     infoCallback_->OnInfo(player_, AV_INFO_TYPE_AUDIO_OUTPUT_DEVICE_CHANGE,
815         reinterpret_cast<OH_AVFormat *>(avFormat.GetRefPtr()));
816 }
817 
OnDrmInfoUpdatedCb(const int32_t extra,const Format & infoBody)818 void NativeAVPlayerCallback::OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody)
819 {
820 #ifdef SUPPORT_AVPLAYER_DRM
821     if (drmsysteminfocallback_ == nullptr) {
822         return;
823     }
824     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player_);
825     DRM_MediaKeySystemInfo mediaKeySystemInfo;
826     GetDrmSystemInfos(infoBody, &mediaKeySystemInfo, playerObj);
827     drmsysteminfocallback_(player_, &mediaKeySystemInfo);
828 #else
829     (void)drmsysteminfocallback_;
830     (void)extra;
831     (void)infoBody;
832 #endif
833 }
834 
OH_AVPlayer_Create(void)835 OH_AVPlayer *OH_AVPlayer_Create(void)
836 {
837     std::shared_ptr<Player> player = PlayerFactory::CreatePlayer();
838     CHECK_AND_RETURN_RET_LOG(player != nullptr, nullptr, "failed to PlayerFactory::CreatePlayer");
839 
840     PlayerObject *object = new(std::nothrow) PlayerObject(player);
841     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new PlayerObject");
842     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_Create", FAKE_POINTER(object));
843 
844     return object;
845 }
846 
OH_AVPlayer_SetURLSource(OH_AVPlayer * player,const char * url)847 OH_AVErrCode OH_AVPlayer_SetURLSource(OH_AVPlayer *player, const char *url)
848 {
849     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
850     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
851     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
852     CHECK_AND_RETURN_RET_LOG(url != nullptr, AV_ERR_INVALID_VAL, "url is null");
853     playerObj->StartListenCurrentResource();
854     int32_t ret = playerObj->player_->SetSource(url);
855     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player setUrlSource failed");
856     return AV_ERR_OK;
857 }
858 
OH_AVPlayer_SetFDSource(OH_AVPlayer * player,int32_t fd,int64_t offset,int64_t size)859 OH_AVErrCode OH_AVPlayer_SetFDSource(OH_AVPlayer *player, int32_t fd, int64_t offset, int64_t size)
860 {
861     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
862     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
863     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
864     CHECK_AND_RETURN_RET_LOG(fd >= 0, AV_ERR_INVALID_VAL, "fd is invalid");
865     playerObj->StartListenCurrentResource();
866     int32_t ret = playerObj->player_->SetSource(fd, offset, size);
867     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player setFdSource failed");
868     return AV_ERR_OK;
869 }
870 
OH_AVPlayer_Prepare(OH_AVPlayer * player)871 OH_AVErrCode OH_AVPlayer_Prepare(OH_AVPlayer *player)
872 {
873     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
874     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
875     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
876     int32_t ret = playerObj->player_->PrepareAsync();
877     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Prepare failed");
878     return AV_ERR_OK;
879 }
880 
OH_AVPlayer_Play(OH_AVPlayer * player)881 OH_AVErrCode OH_AVPlayer_Play(OH_AVPlayer *player)
882 {
883     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
884     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
885     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
886     int32_t ret = playerObj->player_->Play();
887     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player play failed");
888     return AV_ERR_OK;
889 }
890 
OH_AVPlayer_Pause(OH_AVPlayer * player)891 OH_AVErrCode OH_AVPlayer_Pause(OH_AVPlayer *player)
892 {
893     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
894     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
895     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
896     int32_t ret = playerObj->player_->Pause();
897     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Pause failed");
898     return AV_ERR_OK;
899 }
900 
OH_AVPlayer_Stop(OH_AVPlayer * player)901 OH_AVErrCode OH_AVPlayer_Stop(OH_AVPlayer *player)
902 {
903     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
904     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
905     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
906     int32_t ret = playerObj->player_->Stop();
907     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Stop failed");
908     return AV_ERR_OK;
909 }
910 
OH_AVPlayer_Reset(OH_AVPlayer * player)911 OH_AVErrCode OH_AVPlayer_Reset(OH_AVPlayer *player)
912 {
913     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
914     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
915     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
916     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
917     int32_t ret = playerObj->player_->Reset();
918     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Reset failed");
919     return AV_ERR_OK;
920 }
921 
OH_AVPlayer_Release(OH_AVPlayer * player)922 OH_AVErrCode OH_AVPlayer_Release(OH_AVPlayer *player)
923 {
924     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
925     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
926     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
927     CHECK_AND_RETURN_RET_LOG(!playerObj->isReleased_.load(), AV_ERR_OK, "player alreay isReleased");
928     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
929     int32_t ret = playerObj->player_->Release();
930     playerObj->isReleased_.store(true);
931     if (playerObj->callback_ != nullptr) {
932         Format format;
933         playerObj->callback_->OnInfo(INFO_TYPE_STATE_CHANGE, PLAYER_RELEASED, format);
934     }
935     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Release failed");
936     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_Release", FAKE_POINTER(playerObj));
937     return AV_ERR_OK;
938 }
939 
OH_AVPlayer_ReleaseSync(OH_AVPlayer * player)940 OH_AVErrCode OH_AVPlayer_ReleaseSync(OH_AVPlayer *player)
941 {
942     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
943     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
944     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
945     CHECK_AND_RETURN_RET_LOG(!playerObj->isReleased_.load(), AV_ERR_OK, "player alreay isReleased");
946     playerObj->PauseListenCurrentResource(); // Pause event listening for the current resource
947     int32_t ret = playerObj->player_->ReleaseSync();
948     playerObj->isReleased_.store(true);
949     if (playerObj->callback_ != nullptr) {
950         Format format;
951         playerObj->callback_->OnInfo(INFO_TYPE_STATE_CHANGE, PLAYER_RELEASED, format);
952     }
953     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player ReleaseSync failed");
954     MEDIA_LOGI("0x%{public}06" PRIXPTR " OH_AVPlayer_ReleaseSync", FAKE_POINTER(playerObj));
955     return AV_ERR_OK;
956 }
957 
OH_AVPlayer_SetVolume(OH_AVPlayer * player,float leftVolume,float rightVolume)958 OH_AVErrCode OH_AVPlayer_SetVolume(OH_AVPlayer *player, float leftVolume, float rightVolume)
959 {
960     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
961     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
962     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
963     int32_t ret = playerObj->player_->SetVolume(leftVolume, rightVolume);
964     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetVolume failed");
965     return AV_ERR_OK;
966 }
967 
OH_AVPlayer_SetVolumeMode(OH_AVPlayer * player,OH_AudioStream_VolumeMode mode)968 OH_AVErrCode OH_AVPlayer_SetVolumeMode(OH_AVPlayer *player, OH_AudioStream_VolumeMode mode)
969 {
970     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
971     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
972     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
973     int32_t ret = playerObj->player_->SetVolumeMode(static_cast<int32_t>(mode));
974     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetVolumeMode failed");
975     return AV_ERR_OK;
976 }
977 
OH_AVPlayer_Seek(OH_AVPlayer * player,int32_t mSeconds,AVPlayerSeekMode mode)978 OH_AVErrCode OH_AVPlayer_Seek(OH_AVPlayer *player, int32_t mSeconds, AVPlayerSeekMode mode)
979 {
980     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
981     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
982     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
983     PlayerSeekMode seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
984     switch (mode) {
985         case AVPlayerSeekMode::AV_SEEK_NEXT_SYNC:
986             seekMode = PlayerSeekMode::SEEK_NEXT_SYNC;
987             break;
988         case AVPlayerSeekMode::AV_SEEK_PREVIOUS_SYNC:
989             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
990             break;
991         case AVPlayerSeekMode::AV_SEEK_CLOSEST:
992             seekMode = PlayerSeekMode::SEEK_CLOSEST;
993             break;
994         default:
995             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
996             break;
997     }
998     int32_t ret = playerObj->player_->Seek(mSeconds, seekMode);
999     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player Seek failed");
1000     return AV_ERR_OK;
1001 }
1002 
OH_AVPlayer_GetCurrentTime(OH_AVPlayer * player,int32_t * currentTime)1003 OH_AVErrCode OH_AVPlayer_GetCurrentTime(OH_AVPlayer *player, int32_t *currentTime)
1004 {
1005     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1006     CHECK_AND_RETURN_RET_LOG(currentTime != nullptr, AV_ERR_INVALID_VAL, "currentTime 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     int32_t ret = playerObj->player_->GetCurrentTime(*currentTime);
1010     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetCurrentTime failed");
1011     return AV_ERR_OK;
1012 }
1013 
OH_AVPlayer_GetVideoWidth(OH_AVPlayer * player,int32_t * videoWidth)1014 OH_AVErrCode OH_AVPlayer_GetVideoWidth(OH_AVPlayer *player, int32_t *videoWidth)
1015 {
1016     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1017     CHECK_AND_RETURN_RET_LOG(videoWidth != nullptr, AV_ERR_INVALID_VAL, "videoWidth is nullptr");
1018     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1019     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1020     *videoWidth = playerObj->player_->GetVideoWidth();
1021     return AV_ERR_OK;
1022 }
1023 
OH_AVPlayer_GetVideoHeight(OH_AVPlayer * player,int32_t * videoHeight)1024 OH_AVErrCode OH_AVPlayer_GetVideoHeight(OH_AVPlayer *player, int32_t *videoHeight)
1025 {
1026     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1027     CHECK_AND_RETURN_RET_LOG(videoHeight != nullptr, AV_ERR_INVALID_VAL, "videoHeight is nullptr");
1028     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1029     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1030     *videoHeight = playerObj->player_->GetVideoHeight();
1031     return AV_ERR_OK;
1032 }
1033 
OH_AVPlayer_SetPlaybackSpeed(OH_AVPlayer * player,AVPlaybackSpeed speed)1034 OH_AVErrCode OH_AVPlayer_SetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed speed)
1035 {
1036     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player 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     int32_t ret = playerObj->player_->SetPlaybackSpeed(static_cast<PlaybackRateMode>(speed));
1040     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetPlaybackSpeed failed");
1041     return AV_ERR_OK;
1042 }
1043 
OH_AVPlayer_GetPlaybackSpeed(OH_AVPlayer * player,AVPlaybackSpeed * speed)1044 OH_AVErrCode OH_AVPlayer_GetPlaybackSpeed(OH_AVPlayer *player, AVPlaybackSpeed *speed)
1045 {
1046     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1047     CHECK_AND_RETURN_RET_LOG(speed != nullptr, AV_ERR_INVALID_VAL, "speed is nullptr");
1048     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1049     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1050     PlaybackRateMode md;
1051     int32_t ret = playerObj->player_->GetPlaybackSpeed(md);
1052     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetPlaybackSpeed failed");
1053     *speed = static_cast<AVPlaybackSpeed>(md);
1054     return AV_ERR_OK;
1055 }
1056 
OH_AVPlayer_SetAudioRendererInfo(OH_AVPlayer * player,OH_AudioStream_Usage streamUsage)1057 OH_AVErrCode OH_AVPlayer_SetAudioRendererInfo(OH_AVPlayer *player, OH_AudioStream_Usage streamUsage)
1058 {
1059     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1060     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1061     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1062     if (streamUsage < OH_AudioStream_Usage::AUDIOSTREAM_USAGE_UNKNOWN ||
1063         streamUsage > OH_AudioStream_Usage::AUDIOSTREAM_USAGE_NAVIGATION) {
1064         return AV_ERR_INVALID_VAL;
1065     }
1066     Format format;
1067     (void)format.PutIntValue(PlayerKeys::STREAM_USAGE, streamUsage);
1068     (void)format.PutIntValue(PlayerKeys::CONTENT_TYPE, 0);
1069     (void)format.PutIntValue(PlayerKeys::RENDERER_FLAG, 0);
1070     int32_t ret = playerObj->player_->SetParameter(format);
1071     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioRendererInfo failed");
1072     return AV_ERR_OK;
1073 }
1074 
OH_AVPlayer_SetAudioInterruptMode(OH_AVPlayer * player,OH_AudioInterrupt_Mode interruptMode)1075 OH_AVErrCode OH_AVPlayer_SetAudioInterruptMode(OH_AVPlayer *player, OH_AudioInterrupt_Mode interruptMode)
1076 {
1077     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1078     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1079     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1080     if (interruptMode < OH_AudioInterrupt_Mode::AUDIOSTREAM_INTERRUPT_MODE_SHARE ||
1081         interruptMode > OH_AudioInterrupt_Mode::AUDIOSTREAM_INTERRUPT_MODE_INDEPENDENT) {
1082         return AV_ERR_INVALID_VAL;
1083     }
1084     Format format;
1085     (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_MODE, interruptMode);
1086     int32_t ret = playerObj->player_->SetParameter(format);
1087     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioInterruptMode failed");
1088     return AV_ERR_OK;
1089 }
1090 
OH_AVPlayer_SetAudioEffectMode(OH_AVPlayer * player,OH_AudioStream_AudioEffectMode effectMode)1091 OH_AVErrCode OH_AVPlayer_SetAudioEffectMode(OH_AVPlayer *player, OH_AudioStream_AudioEffectMode effectMode)
1092 {
1093     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1094     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1095     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1096     if (effectMode < OH_AudioStream_AudioEffectMode::EFFECT_NONE ||
1097         effectMode > OH_AudioStream_AudioEffectMode::EFFECT_DEFAULT) {
1098         return AV_ERR_INVALID_VAL;
1099     }
1100     Format format;
1101     (void)format.PutIntValue(PlayerKeys::AUDIO_EFFECT_MODE, effectMode);
1102     int32_t ret = playerObj->player_->SetParameter(format);
1103     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetAudioEffectMode failed");
1104     return AV_ERR_OK;
1105 }
1106 
OH_AVPlayer_SelectBitRate(OH_AVPlayer * player,uint32_t bitRate)1107 OH_AVErrCode OH_AVPlayer_SelectBitRate(OH_AVPlayer *player, uint32_t bitRate)
1108 {
1109     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player 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_->SelectBitRate(bitRate);
1113     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SelectBitRate failed");
1114     return AV_ERR_OK;
1115 }
1116 
OH_AVPlayer_GetDuration(OH_AVPlayer * player,int32_t * duration)1117 OH_AVErrCode OH_AVPlayer_GetDuration(OH_AVPlayer *player, int32_t *duration)
1118 {
1119     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1120     CHECK_AND_RETURN_RET_LOG(duration != nullptr, AV_ERR_INVALID_VAL, "duration 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     int32_t ret = playerObj->player_->GetDuration(*duration);
1124     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetDuration failed");
1125     return AV_ERR_OK;
1126 }
1127 
OH_AVPlayer_GetState(OH_AVPlayer * player,AVPlayerState * state)1128 OH_AVErrCode OH_AVPlayer_GetState(OH_AVPlayer *player, AVPlayerState *state)
1129 {
1130     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1131     CHECK_AND_RETURN_RET_LOG(state != nullptr, AV_ERR_INVALID_VAL, "state is nullptr");
1132     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1133     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1134     if (playerObj->isReleased_.load()) {
1135         *state = AVPlayerState::AV_RELEASED;
1136         return AV_ERR_OK;
1137     }
1138     for (uint32_t i = 0; i < STATE_MAP_LENGTH; i++) {
1139         if (STATE_MAP[i].playerStates == player->state_) {
1140             *state = STATE_MAP[i].avPlayerState;
1141             return AV_ERR_OK;
1142         }
1143     }
1144 
1145     *state = AV_ERROR;
1146     return AV_ERR_OK;
1147 }
1148 
1149 #ifdef SUPPORT_VIDEO
OH_AVPlayer_SetVideoSurface(OH_AVPlayer * player,OHNativeWindow * window)1150 OH_AVErrCode  OH_AVPlayer_SetVideoSurface(OH_AVPlayer *player, OHNativeWindow *window)
1151 {
1152     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1153     CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_ERR_INVALID_VAL, "Window is nullptr!");
1154     CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_ERR_INVALID_VAL, "Input window surface is nullptr!");
1155     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1156     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1157     int32_t ret = playerObj->player_->SetVideoSurface(window->surface);
1158     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "SetVideoSurface failed!");
1159     return AV_ERR_OK;
1160 }
1161 #endif
1162 
OH_AVPlayer_IsPlaying(OH_AVPlayer * player)1163 bool OH_AVPlayer_IsPlaying(OH_AVPlayer *player)
1164 {
1165     CHECK_AND_RETURN_RET_LOG(player != nullptr, false, "input player is nullptr!");
1166     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1167     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, false, "player_ is null");
1168     return playerObj->player_->IsPlaying();
1169 }
1170 
OH_AVPlayer_IsLooping(OH_AVPlayer * player)1171 bool OH_AVPlayer_IsLooping(OH_AVPlayer *player)
1172 {
1173     CHECK_AND_RETURN_RET_LOG(player != nullptr, false, "input player is nullptr!");
1174     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1175     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, false, "player_ is null");
1176     return playerObj->player_->IsLooping();
1177 }
1178 
OH_AVPlayer_SetLooping(OH_AVPlayer * player,bool loop)1179 OH_AVErrCode OH_AVPlayer_SetLooping(OH_AVPlayer *player, bool loop)
1180 {
1181     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1182     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1183     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1184     int32_t ret = playerObj->player_->SetLooping(loop);
1185     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetLooping failed");
1186     return AV_ERR_OK;
1187 }
1188 
OH_AVPlayer_SetPlayerCallback(OH_AVPlayer * player,AVPlayerCallback callback)1189 OH_AVErrCode OH_AVPlayer_SetPlayerCallback(OH_AVPlayer *player, AVPlayerCallback callback)
1190 {
1191     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1192     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1193     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1194     CHECK_AND_RETURN_RET_LOG(callback.onInfo != nullptr, AV_ERR_INVALID_VAL, "onInfo is null");
1195     CHECK_AND_RETURN_RET_LOG(callback.onError != nullptr, AV_ERR_INVALID_VAL, "onError is null");
1196     if (playerObj->callback_ == nullptr) {
1197         NativeAVPlayerCallback *avplayerCallback =
1198             new (std::nothrow) NativeAVPlayerCallback(player, callback);
1199         CHECK_AND_RETURN_RET_LOG(avplayerCallback != nullptr, AV_ERR_NO_MEMORY, "avplayerCallback is nullptr!");
1200         playerObj->callback_ = std::shared_ptr<NativeAVPlayerCallback>(avplayerCallback);
1201     } else {
1202         playerObj->callback_->SetPlayCallback(callback);
1203     }
1204     int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1205     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetPlayerCallback failed");
1206     return AV_ERR_OK;
1207 }
1208 
OH_AVPlayer_SelectTrack(OH_AVPlayer * player,int32_t index)1209 OH_AVErrCode OH_AVPlayer_SelectTrack(OH_AVPlayer *player, int32_t index)
1210 {
1211     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1212     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1213     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1214     int32_t ret = playerObj->player_->SelectTrack(index);
1215     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SelectTrack failed");
1216     return AV_ERR_OK;
1217 }
1218 
OH_AVPlayer_DeselectTrack(OH_AVPlayer * player,int32_t index)1219 OH_AVErrCode OH_AVPlayer_DeselectTrack(OH_AVPlayer *player, int32_t index)
1220 {
1221     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player 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_->DeselectTrack(index);
1225     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player DeselectTrack failed");
1226     return AV_ERR_OK;
1227 }
1228 
OH_AVPlayer_GetCurrentTrack(OH_AVPlayer * player,int32_t trackType,int32_t * index)1229 OH_AVErrCode OH_AVPlayer_GetCurrentTrack(OH_AVPlayer *player, int32_t trackType, int32_t *index)
1230 {
1231     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1232     CHECK_AND_RETURN_RET_LOG(index != nullptr, AV_ERR_INVALID_VAL, "index is nullptr");
1233     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1234     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1235     int32_t ret = playerObj->player_->GetCurrentTrack(trackType, *index);
1236     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player GetCurrentTrack failed");
1237     return AV_ERR_OK;
1238 }
1239 
OH_AVPlayer_SetMediaKeySystemInfoCallback(OH_AVPlayer * player,Player_MediaKeySystemInfoCallback callback)1240 OH_AVErrCode OH_AVPlayer_SetMediaKeySystemInfoCallback(OH_AVPlayer *player,
1241     Player_MediaKeySystemInfoCallback callback)
1242 {
1243 #ifdef SUPPORT_AVPLAYER_DRM
1244     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1245     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1246     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1247     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_ERR_INVALID_VAL, "MediaKeySystemInfoCallback is null");
1248 
1249     if (playerObj->callback_ == nullptr) {
1250         static AVPlayerCallback playCallback = { nullptr, nullptr };
1251         playerObj->callback_ = std::make_shared<NativeAVPlayerCallback>(player, playCallback);
1252         int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1253         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1254         ret = playerObj->callback_->SetDrmSystemInfoCallback(callback);
1255         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1256     } else {
1257         int32_t ret = playerObj->callback_->SetDrmSystemInfoCallback(callback);
1258         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDrmSystemInfoCallback failed");
1259     }
1260 #else
1261     (void)player;
1262     (void)callback;
1263 #endif
1264     return AV_ERR_OK;
1265 }
1266 
OH_AVPlayer_GetMediaKeySystemInfo(OH_AVPlayer * player,DRM_MediaKeySystemInfo * mediaKeySystemInfo)1267 OH_AVErrCode OH_AVPlayer_GetMediaKeySystemInfo(OH_AVPlayer *player, DRM_MediaKeySystemInfo *mediaKeySystemInfo)
1268 {
1269 #ifdef SUPPORT_AVPLAYER_DRM
1270     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1271     CHECK_AND_RETURN_RET_LOG(mediaKeySystemInfo != nullptr, AV_ERR_INVALID_VAL, "mediaKeySystemInfo is nullptr");
1272     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1273     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1274     CHECK_AND_RETURN_RET_LOG(!playerObj->localDrmInfos_.empty(), AV_ERR_INVALID_VAL, "localDrmInfos_ is null");
1275     mediaKeySystemInfo->psshCount = playerObj->localDrmInfos_.size();
1276     int index = 0;
1277     for (auto item : playerObj->localDrmInfos_) {
1278         int ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].uuid, item.first.size(),
1279             item.first.data(), item.first.size());
1280         CHECK_AND_RETURN_RET_LOG(ret ==0, AV_ERR_INVALID_VAL, "no memory");
1281         ret = memcpy_s(mediaKeySystemInfo->psshInfo[index].data, item.second.size(),
1282             item.second.data(), item.second.size());
1283         CHECK_AND_RETURN_RET_LOG(ret ==0, AV_ERR_INVALID_VAL, "no memory");
1284         mediaKeySystemInfo->psshInfo[index++].dataLen = static_cast<int32_t>(item.second.size());
1285     }
1286 #else
1287     (void)player;
1288     (void)mediaKeySystemInfo;
1289 #endif
1290     return AV_ERR_OK;
1291 }
1292 
OH_AVPlayer_SetDecryptionConfig(OH_AVPlayer * player,MediaKeySession * mediaKeySession,bool secureVideoPath)1293 OH_AVErrCode OH_AVPlayer_SetDecryptionConfig(OH_AVPlayer *player, MediaKeySession *mediaKeySession,
1294     bool secureVideoPath)
1295 {
1296 #ifdef SUPPORT_AVPLAYER_DRM
1297     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1298     CHECK_AND_RETURN_RET_LOG(mediaKeySession != nullptr, AV_ERR_INVALID_VAL, "mediaKeySession is nullptr");
1299     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1300     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null");
1301     struct MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySession);
1302     CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, AV_ERR_INVALID_VAL, "sessionObject is null");
1303     CHECK_AND_RETURN_RET_LOG(sessionObject->sessionImpl_ != nullptr, AV_ERR_INVALID_VAL, "sessionObject is null");
1304     int32_t ret =
1305         playerObj->player_->SetDecryptConfig(sessionObject->sessionImpl_->GetMediaKeySessionServiceProxy(),
1306             secureVideoPath);
1307     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player SetDecryptConfig failed");
1308 #else
1309     (void)player;
1310     (void)mediaKeySession;
1311     (void)secureVideoPath;
1312 #endif
1313     return AV_ERR_OK;
1314 }
1315 
AVPlayerSetPlayerCallback(OH_AVPlayer * player,struct PlayerObject * playerObj,bool isUnregister)1316 static OH_AVErrCode AVPlayerSetPlayerCallback(OH_AVPlayer *player, struct PlayerObject *playerObj, bool isUnregister)
1317 {
1318     MEDIA_LOGD("AVPlayerSetPlayerCallback S");
1319     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!");
1320     CHECK_AND_RETURN_RET_LOG(playerObj != nullptr && playerObj->player_ != nullptr, AV_ERR_INVALID_VAL,
1321         "player_ is nullptr!");
1322     if (!isUnregister && playerObj->callback_ == nullptr) {
1323         MEDIA_LOGI("AVPlayerSetPlayerCallback create dummy NativeAVPlayerCallback");
1324         AVPlayerCallback dummyCallback = {
1325             .onInfo = nullptr,
1326             .onError = nullptr
1327         };
1328         NativeAVPlayerCallback *avplayerCallback =
1329             new (std::nothrow) NativeAVPlayerCallback(player, dummyCallback);
1330         CHECK_AND_RETURN_RET_LOG(avplayerCallback != nullptr, AV_ERR_NO_MEMORY, "avplayerCallback is nullptr!");
1331         playerObj->callback_ = std::shared_ptr<NativeAVPlayerCallback>(avplayerCallback);
1332 
1333         int32_t ret = playerObj->player_->SetPlayerCallback(playerObj->callback_);
1334         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "AVPlayerSetPlayerCallback failed!");
1335     }
1336     MEDIA_LOGD("AVPlayerSetPlayerCallback E");
1337     return AV_ERR_OK;
1338 }
1339 
OH_AVPlayer_SetOnErrorCallback(OH_AVPlayer * player,OH_AVPlayerOnErrorCallback callback,void * userData)1340 OH_AVErrCode OH_AVPlayer_SetOnErrorCallback(OH_AVPlayer *player, OH_AVPlayerOnErrorCallback callback, void *userData)
1341 {
1342     MEDIA_LOGD("OH_AVPlayer_SetOnErrorCallback S");
1343     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "SetOnError input player is nullptr!");
1344     if (callback == nullptr) {
1345         MEDIA_LOGI("SetOnError callback is nullptr, unregister callback.");
1346     }
1347     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1348     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "SetOnError player_ is nullptr");
1349 
1350     OH_AVErrCode errCode = AVPlayerSetPlayerCallback(player, playerObj, callback == nullptr);
1351     CHECK_AND_RETURN_RET_LOG(errCode == AV_ERR_OK, errCode, "AVPlayerSetPlayerCallback AVPlayerOnError error");
1352 
1353     if (playerObj->callback_ == nullptr || playerObj->callback_->SetOnErrorCallback(callback, userData) != AV_ERR_OK) {
1354         MEDIA_LOGE("OH_AVPlayer_SetOnErrorCallback error");
1355         return AV_ERR_NO_MEMORY;
1356     }
1357     MEDIA_LOGD("OH_AVPlayer_SetOnErrorCallback success E");
1358     return AV_ERR_OK;
1359 }
1360 
OH_AVPlayer_SetOnInfoCallback(OH_AVPlayer * player,OH_AVPlayerOnInfoCallback callback,void * userData)1361 OH_AVErrCode OH_AVPlayer_SetOnInfoCallback(OH_AVPlayer *player, OH_AVPlayerOnInfoCallback callback, void *userData)
1362 {
1363     MEDIA_LOGD("OH_AVPlayer_SetOnInfoCallback S");
1364     CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "SetOnInfo input player is nullptr!");
1365     if (callback == nullptr) {
1366         MEDIA_LOGI("SetOnInfo callback is nullptr, unregister callback.");
1367     }
1368     struct PlayerObject *playerObj = reinterpret_cast<PlayerObject *>(player);
1369     CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "SetOnInfo player_ is nullptr");
1370 
1371     OH_AVErrCode errCode = AVPlayerSetPlayerCallback(player, playerObj, callback == nullptr);
1372     CHECK_AND_RETURN_RET_LOG(errCode == AV_ERR_OK, errCode, "AVPlayerSetPlayerCallback AVPlayerOnInfoCallback error");
1373 
1374     if (playerObj->callback_ == nullptr || playerObj->callback_->SetOnInfoCallback(callback, userData) != AV_ERR_OK) {
1375         MEDIA_LOGE("OH_AVPlayer_SetOnInfoCallback error");
1376         return AV_ERR_NO_MEMORY;
1377     }
1378     MEDIA_LOGD("OH_AVPlayer_SetOnInfoCallback success E");
1379     return AV_ERR_OK;
1380 }