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 HISTREAMER_FILE_FD_SOURCE_PLUGIN_H 17 #define HISTREAMER_FILE_FD_SOURCE_PLUGIN_H 18 19 #include <cstdio> 20 #include <string> 21 #include "plugin/source_plugin.h" 22 #include "plugin/plugin_buffer.h" 23 #include "osal/utils/ring_buffer.h" 24 #include "osal/task/task.h" 25 #include <mutex> 26 #include <shared_mutex> 27 #include "osal/utils/steady_clock.h" 28 29 namespace OHOS { 30 namespace Media { 31 namespace Plugins { 32 namespace FileFdSource { 33 struct HmdfsHasCache { 34 int64_t offset; 35 int64_t readSize; 36 }; 37 38 class FileFdSourcePlugin : public SourcePlugin { 39 public: 40 explicit FileFdSourcePlugin(std::string name); 41 ~FileFdSourcePlugin(); 42 Status SetCallback(Callback* cb) override; 43 Status SetSource(std::shared_ptr<MediaSource> source) override; 44 Status Read(std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) override; 45 Status Read(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) override; 46 Status GetSize(uint64_t& size) override; 47 Seekable GetSeekable() override; 48 Status SeekTo(uint64_t offset) override; 49 Status Reset() override; 50 Status Stop() override; 51 void SetDemuxerState(int32_t streamId) override; 52 void SetBundleName(const std::string& bundleName) override; 53 Status SetCurrentBitRate(int32_t bitRate, int32_t streamID) override; 54 Status SetReadBlockingFlag(bool isAllowed) override; 55 void SetInterruptState(bool isInterruptNeeded) override; 56 void NotifyBufferingStart(); 57 void NotifyBufferingPercent(); 58 void NotifyBufferingEnd(); 59 void NotifyReadFail(); 60 void SetEnableOnlineFdCache(bool isEnableFdCache) override; 61 private: 62 Status ParseUriInfo(const std::string& uri); 63 Status ReadOfflineFile(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen); 64 Status ReadOnlineFile(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen); 65 Status SeekToOfflineFile(uint64_t offset); 66 Status SeekToOnlineFile(uint64_t offset); 67 void CacheDataLoop(); 68 bool HasCacheData(size_t bufferSize, uint64_t offset); 69 void HandleReadResult(size_t bufferSize, int size); 70 std::shared_ptr<Memory> GetBufferPtr(std::shared_ptr<Buffer>& buffer, size_t expectedLen); 71 72 void CacheData(); 73 bool HandleBuffering(); 74 void PauseDownloadTask(bool isAsync); 75 inline int64_t GetLastSize(uint64_t position); 76 void CheckFileType(); 77 void GetCurrentSpeed(int64_t curTime); 78 float GetCacheTime(float num); 79 void UpdateWaterLineAbove(); 80 void DeleteCacheBuffer(char* buffer, size_t bufferSize); 81 void CheckReadTime(); 82 bool IsValidTime(int64_t curTime, int64_t lastTime); 83 84 int32_t fd_ {-1}; 85 int64_t offset_ {0}; 86 uint64_t size_ {0}; 87 uint64_t fileSize_ {0}; 88 Seekable seekable_ {Seekable::SEEKABLE}; 89 std::atomic<uint64_t> position_ {0}; 90 Callback* callback_ {nullptr}; 91 std::atomic<bool> isBuffering_ {false}; 92 std::atomic<bool> isInterrupted_ {false}; 93 std::atomic<bool> isReadBlocking_ {true}; 94 std::atomic<bool> inSeek_ {false}; 95 std::shared_ptr<Task> downloadTask_; 96 std::shared_mutex mutex_; 97 bool isReadFrame_ {false}; 98 bool isSeekHit_ {false}; 99 std::atomic<uint64_t> cachePosition_ {0}; 100 int64_t waterLineAbove_ {0}; 101 bool isCloudFile_ {false}; 102 std::shared_ptr<RingBuffer> ringBuffer_; 103 104 uint64_t downloadSize_ {0}; 105 SteadyClock steadyClock_; 106 SteadyClock steadyClock2_; 107 int64_t lastCheckTime_ {0}; 108 int32_t currentBitRate_ {0}; 109 float avgDownloadSpeed_ {0}; 110 int64_t curReadTime_ {0}; 111 int64_t retryTimes_ {0}; 112 int64_t lastReadTime_ {0}; 113 bool isEnableFdCache_{ true }; 114 }; 115 } // namespace FileSource 116 } // namespace Plugins 117 } // namespace Media 118 } // namespace OHOS 119 120 #endif // HISTREAMER_FILE_FD_SOURCE_PLUGIN_H 121