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