• 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_H
17 #define CJ_AVPLAYER_H
18 
19 #include "audio_effect.h"
20 #include "cj_avplayer_callback.h"
21 #include "cj_avplayer_utils.h"
22 #include "cj_common_ffi.h"
23 #include "cj_lambda.h"
24 #include "cj_media_data_source_callback.h"
25 #include "ffi_remote_data.h"
26 #include "media_data_source_callback.h"
27 #include "player.h"
28 #include "task_queue.h"
29 
30 namespace OHOS {
31 namespace Media {
32 class CJAVPlayer : public OHOS::FFI::FFIData, public CJAVPlayerNotify {
33     DECL_TYPE(CJAVPlayer, OHOS::FFI::FFIData)
34 public:
35     CJAVPlayer();
36     ~CJAVPlayer() override;
37     bool Constructor();
38 
39     void NotifyDuration(int32_t duration) override;
40     void NotifyPosition(int32_t position) override;
41     void NotifyState(PlayerStates state) override;
42     void NotifyVideoSize(int32_t width, int32_t height) override;
43     void NotifyIsLiveStream() override;
44     void NotifyDrmInfoUpdated(const std::multimap<std::string, std::vector<uint8_t>> &infos) override;
45 
46     char *GetUrl();
47     void SetUrl(char *url);
48     CAVFileDescriptor GetAVFileDescriptor();
49     void SetAVFileDescriptor(CAVFileDescriptor fileDescriptor);
50     CAVDataSrcDescriptor GetDataSrc();
51     void SetDataSrc(CAVDataSrcDescriptor dataSrcDescriptor);
52     char *GetSurfaceID();
53     void SetSurfaceID(char *surfaceId);
54     bool GetLoop();
55     void SetLoop(bool loop);
56     int32_t GetVideoScaleType();
57     void SetVideoScaleType(int32_t videoScaleType);
58     int32_t GetAudioInterruptMode();
59     void SetAudioInterruptMode(int32_t interruptMode);
60     OHOS::AudioStandard::CAudioRendererInfo GetAudioRendererInfo();
61     void SetAudioRendererInfo(OHOS::AudioStandard::CAudioRendererInfo info);
62     int32_t GetAudioEffectMode();
63     void SetAudioEffectMode(int32_t effectMode);
64     char *GetState();
65     int32_t GetCurrentTime();
66     int32_t GetDuration();
67     int32_t GetWidth();
68     int32_t GetHeight();
69 
70     int32_t PrepareTask();
71     int32_t Prepare();
72     int32_t PlayTask();
73     int32_t Play();
74     int32_t PauseTask();
75     int32_t Pause();
76     int32_t StopTask();
77     int32_t Stop();
78     int32_t ResetTask();
79     int32_t Reset();
80     int32_t ReleaseTask();
81     int32_t Release();
82     void SeekTask(int32_t time, int32_t mode);
83     void Seek(int32_t time, int32_t mode);
84 
85     int32_t OnStateChange(int64_t callbackId);
86     int32_t OffStateChange(int64_t callbackId);
87     int32_t OffStateChangeAll();
88     int32_t OnError(int64_t callbackId);
89     int32_t OffError(int64_t callbackId);
90     int32_t OffErrorAll();
91     int32_t OnSeekDone(int64_t callbackId);
92     int32_t OffSeekDone(int64_t callbackId);
93     int32_t OffSeekDoneAll();
94     int32_t OnSpeedDone(int64_t callbackId);
95     int32_t OffSpeedDone(int64_t callbackId);
96     int32_t OffSpeedDoneAll();
97     int32_t OnBitRateDone(int64_t callbackId);
98     int32_t OffBitRateDone(int64_t callbackId);
99     int32_t OffBitRateDoneAll();
100     int32_t OnAvailableBitrates(int64_t callbackId);
101     int32_t OffAvailableBitrates(int64_t callbackId);
102     int32_t OffAvailableBitratesAll();
103     int32_t OnMediaKeySystemInfoUpdate(int64_t callbackId);
104     int32_t OffMediaKeySystemInfoUpdate(int64_t callbackId);
105     int32_t OffMediaKeySystemInfoUpdateAll();
106     int32_t OnVolumeChange(int64_t callbackId);
107     int32_t OffVolumeChange(int64_t callbackId);
108     int32_t OffVolumeChangeAll();
109     int32_t OnEndOfStream(int64_t callbackId);
110     int32_t OffEndOfStream(int64_t callbackId);
111     int32_t OffEndOfStreamAll();
112     int32_t OnTimeUpdate(int64_t callbackId);
113     int32_t OffTimeUpdate(int64_t callbackId);
114     int32_t OffTimeUpdateAll();
115     int32_t OnDurationUpdate(int64_t callbackId);
116     int32_t OffDurationUpdate(int64_t callbackId);
117     int32_t OffDurationUpdateAll();
118     int32_t OnBufferingUpdate(int64_t callbackId);
119     int32_t OffBufferingUpdate(int64_t callbackId);
120     int32_t OffBufferingUpdateAll();
121     int32_t OnStartRenderFrame(int64_t callbackId);
122     int32_t OffStartRenderFrame(int64_t callbackId);
123     int32_t OffStartRenderFrameAll();
124     int32_t OnVideoSizeChange(int64_t callbackId);
125     int32_t OffVideoSizeChange(int64_t callbackId);
126     int32_t OffVideoSizeChangeAll();
127     int32_t OnAudioInterrupt(int64_t callbackId);
128     int32_t OffAudioInterrupt(int64_t callbackId);
129     int32_t OffAudioInterruptAll();
130     int32_t OnAudioDeviceChange(int64_t callbackId);
131     int32_t OffAudioDeviceChange(int64_t callbackId);
132     int32_t OffAudioDeviceChangeAll();
133     int32_t OnSubtitleUpdate(int64_t callbackId);
134     int32_t OffSubtitleUpdate(int64_t callbackId);
135     int32_t OffSubtitleUpdateAll();
136     int32_t OnTrackChange(int64_t callbackId);
137     int32_t OffTrackChange(int64_t callbackId);
138     int32_t OffTrackChangeAll();
139     int32_t OnTrackInfoUpdate(int64_t callbackId);
140     int32_t OffTrackInfoUpdate(int64_t callbackId);
141     int32_t OffTrackInfoUpdateAll();
142     int32_t OnAmplitudeUpdate(int64_t callbackId);
143     int32_t OffAmplitudeUpdate(int64_t callbackId);
144     int32_t OffAmplitudeUpdateAll();
145 
146     std::string GetCurrentState();
147     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
148     void StartListenCurrentResource();
149     void SetSource(std::string url);
150     void EnqueueNetworkTask(const std::string url);
151     void EnqueueFdTask(const int32_t fd);
152     void QueueOnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
153     bool IsLiveSource() const;
154     bool IsControllable();
155     void PauseListenCurrentResource();
156     void ResetUserParameters();
157     PlayerSeekMode TransferSeekMode(int32_t mode);
158     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy);
159     int32_t SetPlaybackStrategy(AVPlayStrategy strategy);
160     int32_t SetMediaMuted(int32_t mediaType, bool muted);
161     int32_t GetSelectedTracks(std::vector<int32_t> &trackIndex);
162     int32_t SelectTrack(int32_t index, int32_t mode);
163     int32_t DeselectTrack(int32_t index);
164     std::multimap<std::string, std::vector<uint8_t>> GetMediaKeySystemInfos();
165     void SetSpeed(int32_t speed);
166     void SetBitrate(int32_t bitrate);
167     void SetVolume(float volume);
168     int32_t AddSubtitleFromFd(int32_t fd, int64_t offset, int64_t length);
169     int32_t AddSubtitleFromUrl(std::string url);
170     int32_t GetPlaybackInfo(Format &format);
171     int32_t GetTrackDescription(std::vector<Format> &trackInfos);
172     bool HandleParameter(OHOS::AudioStandard::CAudioRendererInfo info);
173     void SetSurface(const std::string &surfaceStr);
174 
175 private:
176     std::shared_ptr<Player> player_ = nullptr;
177     std::shared_ptr<CJAVPlayerCallback> playerCb_ = nullptr;
178     std::shared_ptr<CJMediaDataSourceCallback> dataSrcCb_ = nullptr;
179     std::atomic<bool> isReleased_ = false;
180     std::atomic<bool> isInterrupted_ = false;
181     std::string url_ = "";
182     struct AVFileDescriptor fileDescriptor_;
183     std::string surface_ = "";
184     bool loop_ = false;
185     int32_t videoScaleType_ = 0;
186     OHOS::AudioStandard::InterruptMode interruptMode_ = AudioStandard::InterruptMode::SHARE_MODE;
187     OHOS::AudioStandard::AudioRendererInfo audioRendererInfo_ = OHOS::AudioStandard::AudioRendererInfo{
188         OHOS::AudioStandard::ContentType::CONTENT_TYPE_MUSIC, OHOS::AudioStandard::StreamUsage::STREAM_USAGE_MEDIA, 0};
189     int32_t audioEffectMode_ = OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT;
190     std::mutex mutex_;
191     std::recursive_mutex taskMutex_;
192     PlayerStates state_ = PLAYER_IDLE;
193     std::condition_variable stateChangeCond_;
194     int32_t width_ = 0;
195     int32_t height_ = 0;
196     int32_t position_ = -1;
197     int32_t duration_ = -1;
198     bool isLiveStream_ = false;
199     std::multimap<std::string, std::vector<uint8_t>> localDrmInfos_;
200     std::mutex syncMutex_;
201 };
202 
203 } // namespace Media
204 } // namespace OHOS
205 #endif