1 /* 2 * Copyright (c) 2023-2023 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 "media_demuxer.h" 32 33 namespace OHOS { 34 namespace Media { 35 using SourceType = OHOS::Media::Plugins::SourceType; 36 using MediaSource = OHOS::Media::Plugins::MediaSource; 37 38 class CallbackImpl : public Plugins::Callback { 39 public: OnEvent(const Plugins::PluginEvent & event)40 void OnEvent(const Plugins::PluginEvent &event) override 41 { 42 if (callbackWrap_) { 43 callbackWrap_->OnEvent(event); 44 } 45 } 46 SetSelectBitRateFlag(bool flag,uint32_t desBitRate)47 void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override 48 { 49 if (callbackWrap_) { 50 callbackWrap_->SetSelectBitRateFlag(flag, desBitRate); 51 } 52 } 53 CanAutoSelectBitRate()54 bool CanAutoSelectBitRate() override 55 { 56 if (callbackWrap_) { 57 return callbackWrap_->CanAutoSelectBitRate(); 58 } 59 return false; 60 } 61 SetCallbackWrap(Callback * callbackWrap)62 void SetCallbackWrap(Callback* callbackWrap) 63 { 64 callbackWrap_ = callbackWrap; 65 } 66 67 private: 68 Callback* callbackWrap_ {nullptr}; 69 }; 70 71 class Source : public Plugins::Callback { 72 public: 73 explicit Source(); 74 ~Source() override; 75 76 virtual Status SetSource(const std::shared_ptr<MediaSource>& source); 77 void SetBundleName(const std::string& bundleName); 78 Status Prepare(); 79 Status Start(); 80 Status Stop(); 81 Status Pause(); 82 Status Resume(); 83 Status SetReadBlockingFlag(bool isReadBlockingAllowed); 84 Plugins::Seekable GetSeekable(); 85 86 Status GetSize(uint64_t &fileSize); 87 88 void OnEvent(const Plugins::PluginEvent &event) override; 89 void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override; 90 bool CanAutoSelectBitRate() override; 91 92 bool IsSeekToTimeSupported(); 93 int64_t GetDuration(); 94 Status SeekToTime(int64_t seekTime, SeekMode mode); 95 Status SeekTo(uint64_t offset); 96 Status GetBitRates(std::vector<uint32_t>& bitRates); 97 Status SelectBitRate(uint32_t bitRate); 98 Status SetCurrentBitRate(int32_t bitRate, int32_t streamID); 99 void SetCallback(Callback* callback); 100 bool IsNeedPreDownload(); 101 void SetDemuxerState(int32_t streamId); 102 Status GetStreamInfo(std::vector<StreamInfo>& streams); 103 Status Read(int32_t streamID, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen); 104 void SetInterruptState(bool isInterruptNeeded); 105 Status GetDownloadInfo(DownloadInfo& downloadInfo); 106 Status GetPlaybackInfo(PlaybackInfo& playbackInfo); 107 Status SelectStream(int32_t streamID); 108 size_t GetSegmentOffset(); 109 bool GetHLSDiscontinuity(); 110 void SetEnableOnlineFdCache(bool isEnableFdCache); 111 void WaitForBufferingEnd(); 112 113 private: 114 Status InitPlugin(const std::shared_ptr<MediaSource>& source); 115 static std::string GetUriSuffix(const std::string& uri); 116 bool GetProtocolByUri(); 117 bool ParseProtocol(const std::shared_ptr<MediaSource>& source); 118 Status FindPlugin(const std::shared_ptr<MediaSource>& source); 119 void ClearData(); 120 121 std::string protocol_; 122 bool seekToTimeFlag_{false}; 123 std::string uri_; 124 Plugins::Seekable seekable_; 125 126 std::shared_ptr<Plugins::SourcePlugin> plugin_; 127 128 std::shared_ptr<Plugins::PluginInfo> pluginInfo_{}; 129 bool isPluginReady_ {false}; 130 bool isAboveWaterline_ {false}; 131 132 std::shared_ptr<CallbackImpl> mediaDemuxerCallback_; 133 std::atomic<bool> isInterruptNeeded_{false}; 134 bool isEnableFdCache_{ true }; 135 }; 136 } // namespace Media 137 } // namespace OHOS 138 #endif 139