1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef AV_PLAYER_CALLBACK_H 17 #define AV_PLAYER_CALLBACK_H 18 19 #include <map> 20 #include <mutex> 21 #include "player.h" 22 #include "media_errors.h" 23 #include "common_napi.h" 24 #include "event_handler.h" 25 26 namespace OHOS { 27 namespace Media { 28 class AVPlayerNotify { 29 public: 30 AVPlayerNotify() = default; 31 virtual ~AVPlayerNotify() = default; 32 virtual void NotifyDuration(int32_t duration) = 0; 33 virtual void NotifyPosition(int32_t position) = 0; 34 virtual void NotifyState(PlayerStates state) = 0; 35 virtual void NotifyVideoSize(int32_t width, int32_t height) = 0; 36 virtual void NotifyIsLiveStream() = 0; 37 }; 38 39 class AVPlayerCallback : public PlayerCallback { 40 public: 41 AVPlayerCallback(napi_env env, AVPlayerNotify *listener); 42 virtual ~AVPlayerCallback(); 43 void OnError(int32_t errorCode, const std::string &errorMsg) override; 44 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override; 45 void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg); 46 PlayerStates GetCurrentState() const; 47 int32_t GetVideoWidth() const; 48 int32_t GetVideoHeight() const; 49 void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref); 50 void ClearCallbackReference(); 51 void ClearCallbackReference(const std::string &name); 52 void Start(); 53 void Pause(); 54 void Release(); 55 56 private: 57 void OnStartRenderFrameCb() const; 58 void OnStateChangeCb(const int32_t extra, const Format &infoBody); 59 void OnVolumeChangeCb(const int32_t extra, const Format &infoBody); 60 void OnSeekDoneCb(const int32_t extra, const Format &infoBody); 61 void OnSpeedDoneCb(const int32_t extra, const Format &infoBody); 62 void OnBitRateDoneCb(const int32_t extra, const Format &infoBody); 63 void OnPositionUpdateCb(const int32_t extra, const Format &infoBody); 64 void OnDurationUpdateCb(const int32_t extra, const Format &infoBody); 65 void OnBufferingUpdateCb(const int32_t extra, const Format &infoBody); 66 void OnSubtitleUpdateCb(const int32_t extra, const Format &infoBody); 67 void OnMessageCb(const int32_t extra, const Format &infoBody); 68 void OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody); 69 void OnAudioInterruptCb(const int32_t extra, const Format &infoBody); 70 void OnBitRateCollectedCb(const int32_t extra, const Format &infoBody); 71 void OnEosCb(const int32_t extra, const Format &infoBody); 72 void NotifyIsLiveStream(const int32_t extra, const Format &infoBody); 73 void OnTrackChangedCb(const int32_t extra, const Format &infoBody); 74 void OnTrackInfoUpdate(const int32_t extra, const Format &infoBody); 75 76 std::mutex mutex_; 77 napi_env env_ = nullptr; 78 std::map<std::string, std::weak_ptr<AutoRef>> refMap_; 79 AVPlayerNotify *listener_ = nullptr; 80 std::atomic<bool> isloaded_ = false; 81 PlayerStates state_ = PLAYER_IDLE; 82 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 83 std::map<uint32_t, void(AVPlayerCallback::*)(const int32_t extra, const Format &infoBody)> onInfoFuncs_; 84 }; 85 } // namespace Media 86 } // namespace OHOS 87 #endif // AV_PLAYER_CALLBACK_H