• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 CJ_AVPLAYER_CALLBACK_H
17 #define CJ_AVPLAYER_CALLBACK_H
18 
19 #include <map>
20 #include <mutex>
21 #include "cj_avplayer_utils.h"
22 #include "multimedia_audio_common.h"
23 #include "multimedia_audio_ffi.h"
24 #include "player.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace AVPlayerState {
29 const std::string STATE_IDLE = "idle";
30 const std::string STATE_INITIALIZED = "initialized";
31 const std::string STATE_PREPARED = "prepared";
32 const std::string STATE_PLAYING = "playing";
33 const std::string STATE_PAUSED = "paused";
34 const std::string STATE_STOPPED = "stopped";
35 const std::string STATE_RELEASED = "released";
36 const std::string STATE_ERROR = "error";
37 const std::string STATE_COMPLETED = "completed";
38 } // namespace AVPlayerState
39 
40 class CJAVPlayerNotify {
41 public:
42     CJAVPlayerNotify() = default;
43     virtual ~CJAVPlayerNotify() = default;
44     virtual void NotifyDuration(int32_t duration) = 0;
45     virtual void NotifyPosition(int32_t position) = 0;
46     virtual void NotifyState(PlayerStates state) = 0;
47     virtual void NotifyVideoSize(int32_t width, int32_t height) = 0;
48     virtual void NotifyIsLiveStream() = 0;
49     virtual void NotifyDrmInfoUpdated(const std::multimap<std::string, std::vector<uint8_t>> &infos) = 0;
50 };
51 using OnInfoFunc = std::function<void(const int32_t, const Format &)>;
52 class CJAVPlayerCallback : public PlayerCallback {
53 public:
54     explicit CJAVPlayerCallback(CJAVPlayerNotify *listener);
55     virtual ~CJAVPlayerCallback();
56     void OnError(int32_t errorCode, const std::string &errorMsg) override;
57     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
58     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
59     void Start();
60     void Pause();
61     void Release();
62 
63     std::atomic<bool> isSetVolume_ = false;
64 
65     void OnStartRenderFrameCb() const;
66     void OnStateChangeCb(const int32_t extra, const Format &infoBody);
67     void OnVolumeChangeCb(const int32_t extra, const Format &infoBody);
68     void OnSeekDoneCb(const int32_t extra, const Format &infoBody);
69     void OnSpeedDoneCb(const int32_t extra, const Format &infoBody);
70     void OnBitRateDoneCb(const int32_t extra, const Format &infoBody);
71     void OnPositionUpdateCb(const int32_t extra, const Format &infoBody);
72     void OnDurationUpdateCb(const int32_t extra, const Format &infoBody);
73     void OnBufferingUpdateCb(const int32_t extra, const Format &infoBody);
74     void OnMessageCb(const int32_t extra, const Format &infoBody);
75     void OnVideoSizeChangedCb(const int32_t extra, const Format &infoBody);
76     void OnAudioInterruptCb(const int32_t extra, const Format &infoBody);
77     void OnAudioDeviceChangeCb(const int32_t extra, const Format &infoBody);
78     void OnBitRateCollectedCb(const int32_t extra, const Format &infoBody);
79     void OnDrmInfoUpdatedCb(const int32_t extra, const Format &infoBody);
80     void OnSubtitleInfoCb(const int32_t extra, const Format &infoBody);
81     void OnMaxAmplitudeCollectedCb(const int32_t extra, const Format &infoBody);
82 
83     void OnEosCb(const int32_t extra, const Format &infoBody);
84     void OnTrackChangedCb(const int32_t extra, const Format &infoBody);
85     void OnTrackInfoUpdate(const int32_t extra, const Format &infoBody);
86     bool IsValidState(PlayerStates state, std::string &stateStr);
87     int32_t SetDrmInfoData(const uint8_t *drmInfoAddr, int32_t infoCount,
88                            std::multimap<std::string, std::vector<uint8_t>> &drmInfoMap);
89 
90     int64_t stateChangeCallbackId = -1;
91     std::function<void(std::string &stateStr, int32_t reason)> stateChangeCallback = nullptr;
92     int64_t errorCallbackId = -1;
93     std::function<void(int32_t errorCode, const std::string &errorMsg)> errorCallback = nullptr;
94     int64_t seekDoneCallbackId = -1;
95     std::function<void(int32_t currentPositon)> seekDoneCallback = nullptr;
96     int64_t speedDoneCallbackId = -1;
97     std::function<void(int32_t speedMode)> speedDoneCallback = nullptr;
98     int64_t bitRateDoneCallbackId = -1;
99     std::function<void(int32_t bitRate)> bitRateDoneCallback = nullptr;
100     int64_t mediaKeySystemInfoUpdateCallbackId = -1;
101     std::function<void(CArrCMediaKeySystemInfo drmInfoMap)> mediaKeySystemInfoUpdateCallback = nullptr;
102     int64_t availableBitratesCallbackId = -1;
103     std::function<void(std::vector<int32_t> bitrateVec)> availableBitratesCallback = nullptr;
104     int64_t volumeChangeCallbackId = -1;
105     std::function<void(float volumeLevel)> volumeChangeCallback = nullptr;
106     int64_t endOfStreamCallbackId = -1;
107     std::function<void()> endOfStreamCallback = nullptr;
108     int64_t timeUpdateCallbackId = -1;
109     std::function<void(int32_t position)> timeUpdateCallback = nullptr;
110     int64_t durationUpdateCallbackId = -1;
111     std::function<void(int32_t duration)> durationUpdateCallback = nullptr;
112     int64_t bufferingUpdateCallbackId = -1;
113     std::function<void(int32_t bufferingType, int32_t val)> bufferingUpdateCallback = nullptr;
114     int64_t startRenderFrameCallbackId = -1;
115     std::function<void()> startRenderFrameCallback = nullptr;
116     int64_t videoSizeChangeCallbackId = -1;
117     std::function<void(int32_t width, int32_t height)> videoSizeChangeCallback = nullptr;
118     int64_t audioInterruptCallbackId = -1;
119     std::function<void(int32_t eventType, int32_t forceType, int32_t hintType)> audioInterruptCallback = nullptr;
120     int64_t audioDeviceChangeCallbackId = -1;
121     std::function<void(AudioStandard::AudioDeviceDescriptor deviceInfo, int32_t reason)> audioDeviceChangeCallback =
122         nullptr;
123     int64_t subtitleUpdateCallbackId = -1;
124     std::function<void(std::string text, int32_t pts, int32_t duration)> subtitleUpdateCallback = nullptr;
125     int64_t trackChangeCallbackId = -1;
126     std::function<void(int32_t index, int32_t isSelect)> trackChangeCallback = nullptr;
127     int64_t trackInfoUpdateCallbackId = -1;
128     std::function<void(CArrCMediaDescription trackInfo)> trackInfoUpdateCallback = nullptr;
129     int64_t amplitudeUpdateCallbackId = -1;
130     std::function<void(std::vector<float> MaxAmplitudeVec)> amplitudeUpdateCallback = nullptr;
131 
132 private:
133     std::mutex mutex_;
134     CJAVPlayerNotify *listener_ = nullptr;
135     std::atomic<bool> isloaded_ = false;
136     PlayerStates state_ = PLAYER_IDLE;
137     std::map<uint32_t, OnInfoFunc> onInfoFuncs_;
138 };
139 
140 } // namespace Media
141 } // namespace OHOS
142 #endif // CJ_AVPLAYER_CALLBACK_H
143