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