• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     virtual void NotifyDrmInfoUpdated(const std::multimap<std::string, std::vector<uint8_t>> &infos) = 0;
38     virtual int32_t GetJsApiVersion();
39 };
40 using OnInfoFunc = std::function<void(const int32_t, const Format &)>;
41 class AVPlayerCallback : public PlayerCallback {
42 public:
43     AVPlayerCallback(napi_env env, AVPlayerNotify *listener);
44     virtual ~AVPlayerCallback();
45     void OnError(int32_t errorCode, const std::string &errorMsg) override;
46     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
47     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
48     PlayerStates GetCurrentState() const;
49     int32_t GetVideoWidth() const;
50     int32_t GetVideoHeight() const;
51     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
52     void ClearCallbackReference();
53     void ClearCallbackReference(const std::string &name);
54     void Start();
55     void Pause();
56     void Release();
57 
58     std::atomic<bool> isSetVolume_ = false;
59 private:
60     void OnStartRenderFrameCb() const;
61     void OnStateChangeCb(const int32_t extra, const Format &infoBody);
62     void OnVolumeChangeCb(const int32_t extra, const Format &infoBody);
63     void OnSeekDoneCb(const int32_t extra, const Format &infoBody);
64     void OnSpeedDoneCb(const int32_t extra, const Format &infoBody);
65     void OnPlaybackRateDoneCb(const int32_t extra, const Format &infoBody);
66     void OnBitRateDoneCb(const int32_t extra, const Format &infoBody);
67     void OnPositionUpdateCb(const int32_t extra, const Format &infoBody);
68     void OnDurationUpdateCb(const int32_t extra, const Format &infoBody);
69     void OnBufferingUpdateCb(const int32_t extra, const Format &infoBody);
70     void OnSubtitleUpdateCb(const int32_t extra, const Format &infoBody);
71     void OnMessageCb(const int32_t extra, const Format &infoBody);
72     void OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody);
73     void OnAudioInterruptCb(const int32_t extra, const Format &infoBody);
74     void OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody);
75     void OnBitRateCollectedCb(const int32_t extra, const Format &infoBody);
76     void OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody);
77     void OnSetDecryptConfigDoneCb(const int32_t extra, const Format &infoBody);
78     void OnSubtitleInfoCb(const int32_t extra, const Format &infoBody);
79     void OnMaxAmplitudeCollectedCb(const int32_t extra, const Format &infoBody);
80     void OnSeiInfoCb(const int32_t extra, const Format &infoBody);
81     void OnSuperResolutionChangedCb(const int32_t extra, const Format &infoBody);
82 
83     void OnEosCb(const int32_t extra, const Format &infoBody);
84     void NotifyIsLiveStream(const int32_t extra, const Format &infoBody);
85     void OnTrackChangedCb(const int32_t extra, const Format &infoBody);
86     void OnTrackInfoUpdate(const int32_t extra, const Format &infoBody);
87     bool IsValidState(PlayerStates state, std::string &stateStr);
88     int32_t SetDrmInfoData(const uint8_t *drmInfoAddr, int32_t infoCount,
89         std::multimap<std::string, std::vector<uint8_t>> &drmInfoMap);
90     void InitInfoFuncsPart1();
91     void InitInfoFuncsPart2();
92 
93     std::mutex mutex_;
94     napi_env env_ = nullptr;
95     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
96     AVPlayerNotify *listener_ = nullptr;
97     std::atomic<bool> isloaded_ = false;
98     PlayerStates state_ = PLAYER_IDLE;
99     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
100     std::map<uint32_t, OnInfoFunc> onInfoFuncs_;
101 };
102 } // namespace Media
103 } // namespace OHOS
104 #endif // AV_PLAYER_CALLBACK_H
105