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_manager.h" 29 #include "plugin/plugin_event.h" 30 #include "plugin/source_plugin.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 callbackWrap_->OnEvent(event); 43 } 44 SetCallbackWrap(Callback * callbackWrap)45 void SetCallbackWrap(Callback* callbackWrap) 46 { 47 callbackWrap_ = callbackWrap; 48 } 49 50 private: 51 Callback* callbackWrap_ {nullptr}; 52 }; 53 54 class Source : public Plugins::Callback { 55 public: 56 explicit Source(); 57 ~Source() override; 58 59 Status PullData(uint64_t offset, int64_t seekTime, size_t size, std::shared_ptr<Plugins::Buffer>& data); 60 virtual Status SetSource(const std::shared_ptr<MediaSource>& source); 61 Status Prepare(); 62 Status Start(); 63 Status Stop(); 64 Status Pause(); 65 Status Resume(); 66 Status SetReadBlockingFlag(bool isReadBlockingAllowed); 67 Plugins::Seekable GetSeekable(); 68 69 Status GetSize(uint64_t &fileSize); 70 71 void OnEvent(const Plugins::PluginEvent &event) override; 72 bool IsSeekToTimeSupported(); 73 Status SetPushData(const std::shared_ptr<PushDataImpl>& data); 74 int64_t GetDuration(); 75 Status SeekToTime(int64_t seekTime); 76 Status GetBitRates(std::vector<uint32_t>& bitRates); 77 Status SelectBitRate(uint32_t bitRate); 78 void SetCallback(Callback* callback); 79 bool IsNeedPreDownload(); 80 private: 81 void ActivateMode(); 82 Status InitPlugin(const std::shared_ptr<MediaSource>& source); 83 static std::string GetUriSuffix(const std::string& uri); 84 void ReadLoop(); 85 bool GetProtocolByUri(); 86 bool ParseProtocol(const std::shared_ptr<MediaSource>& source); 87 Status CreatePlugin(const std::shared_ptr<Plugins::PluginInfo>& info, const std::string& name, 88 Plugins::PluginManager& manager); 89 Status FindPlugin(const std::shared_ptr<MediaSource>& source); 90 91 void ClearData(); 92 93 std::shared_ptr<Task> taskPtr_; 94 std::string protocol_; 95 bool isHls_{false}; 96 std::string uri_; 97 Plugins::Seekable seekable_; 98 uint64_t position_; 99 int64_t mediaOffset_ {0}; // offset used in push mode 100 int32_t retryTimes_ {0}; 101 102 std::shared_ptr<Plugins::SourcePlugin> plugin_; 103 104 std::shared_ptr<Plugins::PluginInfo> pluginInfo_{}; 105 bool isPluginReady_ {false}; 106 bool isAboveWaterline_ {false}; 107 108 std::shared_ptr<PushDataImpl> pushData_; 109 std::shared_ptr<CallbackImpl> mediaDemuxerCallback_; 110 }; 111 } // namespace Media 112 } // namespace OHOS 113 #endif