• 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 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     bool IsLocalFd() override;
62 private:
63     Status ParseUriInfo(const std::string& uri);
64     Status ReadOfflineFile(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen);
65     Status ReadOnlineFile(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen);
66     Status SeekToOfflineFile(uint64_t offset);
67     Status SeekToOnlineFile(uint64_t offset);
68     void CacheDataLoop();
69     bool HasCacheData(size_t bufferSize, uint64_t offset);
70     void HandleReadResult(size_t bufferSize, int size);
71     std::shared_ptr<Memory> GetBufferPtr(std::shared_ptr<Buffer>& buffer, size_t expectedLen);
72 
73     void CacheData();
74     bool HandleBuffering();
75     void PauseDownloadTask(bool isAsync);
76     inline int64_t GetLastSize(uint64_t position);
77     void CheckFileType();
78     void GetCurrentSpeed(int64_t curTime);
79     float GetCacheTime(float num);
80     void UpdateWaterLineAbove();
81     void DeleteCacheBuffer(char* buffer, size_t bufferSize);
82     void CheckReadTime();
83     bool IsValidTime(int64_t curTime, int64_t lastTime);
84     void WaitForInterrupt(int32_t waitTimeMS);
85     void CheckAndNotifyBufferingEnd();
86 
87     int32_t fd_ {-1};
88     int64_t offset_ {0};
89     uint64_t size_ {0};
90     uint64_t fileSize_ {0};
91     Seekable seekable_ {Seekable::SEEKABLE};
92     std::atomic<uint64_t> position_ {0};
93     Callback* callback_ {nullptr};
94     std::atomic<bool> isBuffering_ {false};
95     std::atomic<bool> isInterrupted_ {false};
96     std::atomic<bool> isReadBlocking_ {true};
97     std::atomic<bool> inSeek_ {false};
98     std::condition_variable bufferCond_;
99     std::shared_ptr<Task> downloadTask_;
100     std::shared_mutex mutex_;
101     std::mutex interruptMutex_;
102     bool isReadFrame_ {false};
103     bool isSeekHit_ {false};
104     std::atomic<uint64_t> cachePosition_ {0};
105     int64_t waterLineAbove_ {0};
106     bool isCloudFile_ {false};
107     std::shared_ptr<RingBuffer> ringBuffer_;
108 
109     uint64_t downloadSize_ {0};
110     SteadyClock steadyClock_;
111     SteadyClock steadyClock2_;
112     int64_t lastCheckTime_ {0};
113     int32_t currentBitRate_ {0};
114     float avgDownloadSpeed_ {0};
115     int64_t curReadTime_ {0};
116     int64_t retryTimes_ {0};
117     int64_t lastReadTime_ {0};
118     bool isEnableFdCache_{ true };
119     int loc_ {0};
120 };
121 } // namespace FileSource
122 } // namespace Plugins
123 } // namespace Media
124 } // namespace OHOS
125 
126 #endif // HISTREAMER_FILE_FD_SOURCE_PLUGIN_H