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