• 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 MEDIA_SOURCE_H
17 #define MEDIA_SOURCE_H
18 
19 #include <gmock/gmock.h>
20 
21 #include "plugin/plugin_base.h"
22 #include "plugin/source_plugin.h"
23 
24 namespace OHOS {
25 namespace Media {
26 class CallbackImpl : public Plugins::Callback {
27 public:
OnEvent(const Plugins::PluginEvent & event)28     void OnEvent(const Plugins::PluginEvent &event) override
29     {
30         if (callbackWrap_) {
31             callbackWrap_->OnEvent(event);
32         }
33     }
34 
OnDfxEvent(const Plugins::PluginDfxEvent & event)35     void OnDfxEvent(const Plugins::PluginDfxEvent &event) override
36     {
37         if (callbackWrap_) {
38             callbackWrap_->OnDfxEvent(event);
39         }
40     }
41 
SetSelectBitRateFlag(bool flag,uint32_t desBitRate)42     void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override
43     {
44         if (callbackWrap_) {
45             callbackWrap_->SetSelectBitRateFlag(flag, desBitRate);
46         }
47     }
48 
CanAutoSelectBitRate()49     bool CanAutoSelectBitRate() override
50     {
51         if (callbackWrap_) {
52             return callbackWrap_->CanAutoSelectBitRate();
53         }
54         return false;
55     }
56 
SetCallbackWrap(Callback * callbackWrap)57     void SetCallbackWrap(Callback *callbackWrap)
58     {
59         callbackWrap_ = callbackWrap;
60     }
61 
62 private:
63     Callback *callbackWrap_{ nullptr };
64 };
65 
66 class Source : public Plugins::Callback {
67 public:
68     Source() = default;
69     virtual ~Source() = default;
70 
71     virtual Status SetSource(const std::shared_ptr<Plugins::MediaSource>& source) = 0;
72     virtual void SetBundleName(const std::string& bundleName) = 0;
73     virtual Status Prepare() = 0;
74     virtual Status Start() = 0;
75     virtual Status Stop() = 0;
76     virtual Status Pause() = 0;
77     virtual Status Resume() = 0;
78     virtual Status SetReadBlockingFlag(bool isReadBlockingAllowed) = 0;
79     virtual Plugins::Seekable GetSeekable() = 0;
80     virtual Status GetSize(uint64_t &fileSize) = 0;
81     virtual void OnEvent(const Plugins::PluginEvent &event) = 0;
82     virtual void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) = 0;
83     virtual bool CanAutoSelectBitRate() = 0;
84     virtual bool IsSeekToTimeSupported() = 0;
85     virtual int64_t GetDuration() = 0;
86     virtual Status SeekToTime(int64_t seekTime, Plugins::SeekMode mode) = 0;
87     virtual Status GetBitRates(std::vector<uint32_t>& bitRates) = 0;
88     virtual Status SelectBitRate(uint32_t bitRate) = 0;
89     virtual Status AutoSelectBitRate(uint32_t bitRate) = 0;
90     virtual Status SetStartPts(int64_t startPts) = 0;
91     virtual Status SetExtraCache(uint64_t cacheDuration) = 0;
92     virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID) = 0;
93     virtual void SetCallback(Callback* callback) = 0;
94     virtual bool IsNeedPreDownload() = 0;
95     virtual void SetDemuxerState(int32_t streamId) = 0;
96     virtual Status GetStreamInfo(std::vector<Plugins::StreamInfo>& streams) = 0;
97     virtual Status Read(int32_t streamID, std::shared_ptr<Plugins::Buffer>& buffer,
98         uint64_t offset, size_t expectedLen) = 0;
99     virtual Status SeekTo(uint64_t offset) = 0;
100     virtual void SetInterruptState(bool isInterruptNeeded) = 0;
101     virtual Status GetDownloadInfo(Plugins::DownloadInfo& downloadInfo) = 0;
102     virtual Status GetPlaybackInfo(Plugins::PlaybackInfo& playbackInfo) = 0;
103     virtual Status SelectStream(int32_t streamID) = 0;
104     virtual void SetEnableOnlineFdCache(bool isEnableFdCache) = 0;
105     virtual size_t GetSegmentOffset() = 0;
106     virtual bool GetHLSDiscontinuity() = 0;
107     virtual void WaitForBufferingEnd() = 0;
108     virtual bool SetInitialBufferSize(int32_t offset, int32_t size) = 0;
109     virtual Status SetPerfRecEnabled(bool perfRecEnabled) = 0;
110     virtual void NotifyInitSuccess() = 0;
111     virtual bool IsLocalFd() = 0;
112     virtual bool IsFlvLiveStream() const = 0;
113     virtual uint64_t GetCachedDuration() = 0;
114     virtual void RestartAndClearBuffer() = 0;
115     virtual bool IsFlvLive() = 0;
116     virtual uint64_t GetMemorySize() = 0;
117     virtual std::string GetContentType() = 0;
118     virtual bool IsHlsFmp4() = 0;
119     virtual Status StopBufferring(bool isAppBackground) = 0;
120 
121     virtual Status InitPlugin(const std::shared_ptr<Plugins::MediaSource>& source) = 0;
122     virtual std::string GetUriSuffix(const std::string& uri) = 0;
123     virtual bool GetProtocolByUri() = 0;
124     virtual bool ParseProtocol(const std::shared_ptr<Plugins::MediaSource>& source) = 0;
125     virtual Status FindPlugin(const std::shared_ptr<Plugins::MediaSource>& source) = 0;
126     virtual void ClearData() = 0;
127     virtual Status ReadWithPerfRecord(int32_t streamID, std::shared_ptr<Plugins::Buffer>& buffer,
128         uint64_t offset, size_t expectedLen) = 0;
129     std::string protocol_;
130     bool seekToTimeFlag_{false};
131     std::string uri_;
132     Plugins::Seekable seekable_;
133 
134     std::shared_ptr<Plugins::SourcePlugin> plugin_;
135 
136     bool isPluginReady_ {false};
137     bool isAboveWaterline_ {false};
138 
139     std::shared_ptr<CallbackImpl> mediaDemuxerCallback_;
140     std::atomic<bool> isInterruptNeeded_{false};
141     std::mutex mutex_;
142     std::condition_variable seekCond_;
143     bool isEnableFdCache_{ true };
144     bool perfRecEnabled_ { false };
145     bool isFlvLiveStream_ {false};
146 };
147 }  // namespace Media
148 }  // namespace OHOS
149 #endif
150