1 /*
2  * Copyright (c) 2024-2024 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_HLS_MEDIA_DOWNLOADER_H
17 #define HISTREAMER_HLS_MEDIA_DOWNLOADER_H
18 
19 #include <mutex>
20 #include <thread>
21 #include "playlist_downloader.h"
22 #include "download/downloader.h"
23 #include "media_downloader.h"
24 #include "osal/utils/ring_buffer.h"
25 #include "osal/utils/steady_clock.h"
26 #include "openssl/aes.h"
27 #include "osal/task/task.h"
28 #include "common/media_source.h"
29 #include <unistd.h>
30 #include "common/media_core.h"
31 #include "utils/media_cached_buffer.h"
32 #include "utils/write_bitrate_caculator.h"
33 #include <utility>
34 #include "osal/task/mutex.h"
35 #include "osal/task/condition_variable.h"
36 
37 namespace OHOS {
38 namespace Media {
39 namespace Plugins {
40 namespace HttpPlugin {
41 
42 enum BufferingTimes : int32_t {
43     FIRST_TIMES = 1,
44     SECOND_TIMES = 2,
45 };
46 
47 enum SLEEP_TIME : int32_t {
48     REQUEST_SLEEP_TIME = 5, // 5ms
49     BUFFERING_SLEEP_TIME = 10, // 10ms
50     CACHE_DATA_SLEEP_TIME = 100, // 100ms
51     BUFFERING_TIME_OUT = 1000, // 100ms
52 };
53 constexpr size_t MIN_BUFFER_SIZE = 5 * 1024 * 1024;
54 
55 class HlsMediaDownloader : public MediaDownloader, public PlayListChangeCallback {
56 public:
57     explicit HlsMediaDownloader(
58         const std::map<std::string, std::string>& httpHeader = std::map<std::string, std::string>()) noexcept;
59     explicit HlsMediaDownloader(int expectBufferDuration,
60         const std::map<std::string, std::string>& httpHeader = std::map<std::string, std::string>());
61     explicit HlsMediaDownloader(std::string mimeType,
62         const std::map<std::string, std::string>& httpHeader = std::map<std::string, std::string>());
63     ~HlsMediaDownloader() override;
64     bool Open(const std::string& url, const std::map<std::string, std::string>& httpHeader) override;
65     void Close(bool isAsync) override;
66     void Pause() override;
67     void Resume() override;
68     Status Read(unsigned char* buff, ReadDataInfo& readDataInfo) override;
69     bool SeekToTime(int64_t seekTime, SeekMode mode) override;
70 
71     size_t GetContentLength() const override;
72     int64_t GetDuration() const override;
73     Seekable GetSeekable() const override;
74     void SetCallback(Callback* cb) override;
75     void OnPlayListChanged(const std::vector<PlayInfo>& playList) override;
76     void SetStatusCallback(StatusCallbackFunc cb) override;
77     bool GetStartedStatus() override;
78     std::vector<uint32_t> GetBitRates() override;
79     bool SelectBitRate(uint32_t bitRate) override;
80     void OnSourceKeyChange(const uint8_t *key, size_t keyLen, const uint8_t *iv) override;
81     void OnDrmInfoChanged(const std::multimap<std::string, std::vector<uint8_t>>& drmInfos) override;
82     void SetIsTriggerAutoMode(bool isAuto) override;
83     void SetReadBlockingFlag(bool isReadBlockingAllowed) override;
84     void SeekToTs(uint64_t seekTime, SeekMode mode);
85     void PutRequestIntoDownloader(const PlayInfo& playInfo);
86     int64_t RequestNewTs(uint64_t seekTime, SeekMode mode, double totalDuration,
87         double hstTime, const PlayInfo& item);
88     void UpdateDownloadFinished(const std::string &url, const std::string& location);
89     void AutoSelectBitrate(uint32_t bitRate);
90     size_t GetTotalBufferSize();
91     void SetInterruptState(bool isInterruptNeeded) override;
92     void GetPlaybackInfo(PlaybackInfo& playbackInfo) override;
93     void ReportBitrateStart(uint32_t bitRate);
94     std::pair<int32_t, int32_t> GetDownloadRateAndSpeed();
95     void GetDownloadInfo(DownloadInfo& downloadInfo) override;
96     std::pair<int32_t, int32_t> GetDownloadInfo() override;
97     void ReportVideoSizeChange();
98     Status SetCurrentBitRate(int32_t bitRate, int32_t streamID) override;
99     size_t GetBufferSize() const override;
100     bool GetPlayable() override;
101     bool GetBufferingTimeOut() override;
102     bool GetReadTimeOut() override;
103     void SetAppUid(int32_t appUid) override;
104     size_t GetSegmentOffset() override;
105     bool GetHLSDiscontinuity() override;
106     void WaitForBufferingEnd() override;
107     void SetIsReportedErrorCode() override;
108     void SetPlayStrategy(const std::shared_ptr<PlayStrategy>& playStrategy) override;
109 
110 private:
111     void SaveHttpHeader(const std::map<std::string, std::string>& httpHeader);
112     void SetDemuxerState(int32_t streamId) override;
113     void SetDownloadErrorState() override;
114     bool SaveData(uint8_t* data, uint32_t len);
115     Status ReadDelegate(unsigned char* buff, ReadDataInfo& readDataInfo);
116     void ReadCacheBuffer(unsigned char* buff, ReadDataInfo& readDataInfo);
117     bool SaveEncryptData(uint8_t* data, uint32_t len);
118     void InitMediaDownloader();
119     void DownloadRecordHistory(int64_t nowTime);
120     void OnWriteCacheBuffer(uint32_t len);
121     void OnReadBuffer(uint32_t len);
122     double GetAveDownSpeed();
123     uint64_t GetMinBuffer();
124     void DownloadReport();
125     void ReportDownloadSpeed();
126     bool CheckRiseBufferSize();
127     bool CheckPulldownBufferSize();
128 
129     void RiseBufferSize();
130     void DownBufferSize();
131     void ActiveAutoBufferSize();
132     void InActiveAutoBufferSize();
133     uint64_t TransferSizeToBitRate(int width);
134     bool HandleBuffering();
135     bool HandleCache();
136     bool CheckReadStatus();
137     Status CheckPlaylist(unsigned char* buff, ReadDataInfo& readDataInfo);
138     bool CheckBreakCondition();
139     uint32_t GetDecrptyRealLen(uint8_t* writeDataPoint, uint32_t waitLen, uint32_t writeLen);
140     void ResetPlaylistCapacity(size_t size);
141     void PlaylistBackup(const PlayInfo& fragment);
142     void HandleCachedDuration();
143     void UpdateWaterLineAbove();
144     void CalculateBitRate(size_t fragmentSize, double duration);
145     double CalculateCurrentDownloadSpeed();
146     void UpdateCachedPercent(BufferingInfoType infoType);
147     bool CheckBufferingOneSeconds();
148     float GetCacheDuration(float ratio);
149     void HandleFfmpegReadback(uint64_t ffmpegOffset);
150     void SeekToTsForRead(uint32_t currentTsIndex);
151     int64_t RequestNewTsForRead(const PlayInfo& item);
152     void PushPlayInfo(PlayInfo playInfo);
153     void PrepareToSeek();
154     bool CheckDataIntegrity();
155     void HlsInit();
156     bool SaveCacheBufferData(uint8_t* data, uint32_t len);
157     bool ClearChunksOfFragment();
158     size_t GetCrossTsBuffersize();
159 
160 private:
161     size_t totalBufferSize_ {0};
162     std::shared_ptr<Downloader> downloader_;
163     std::shared_ptr<DownloadRequest> downloadRequest_;
164     Callback* callback_ {nullptr};
165     DataSaveFunc dataSave_;
166     StatusCallbackFunc statusCallback_;
167     bool startedPlayStatus_ {false};
168 
169     std::shared_ptr<PlayListDownloader> playlistDownloader_;
170 
171     std::shared_ptr<BlockingQueue<PlayInfo>> playList_;
172     std::map<std::string, bool> fragmentDownloadStart;
173     std::map<std::string, bool> fragmentPushed;
174     std::deque<PlayInfo> backPlayList_;
175     bool isSelectingBitrate_ {false};
176     bool isDownloadStarted_ {false};
177     static constexpr uint64_t DECRYPT_UNIT_LEN = 16;
178     uint8_t afterAlignRemainedBuffer_[DECRYPT_UNIT_LEN] {0};
179     uint64_t afterAlignRemainedLength_ = 0;
180     uint64_t totalLen_ = 0;
181     std::string curUrl_;
182     uint8_t key_[16] = {0};
183     size_t keyLen_ {0};
184     uint8_t iv_[16] = {0};
185     AES_KEY aesKey_;
186     uint8_t decryptCache_[MIN_BUFFER_SIZE] {0};
187     uint8_t decryptBuffer_[MIN_BUFFER_SIZE] {0};
188     uint32_t writeTsIndex_ = 0;
189     bool isAutoSelectBitrate_ {true};
190     uint64_t seekTime_ = 0;
191 
192     bool isReadFrame_ {false};
193     bool isTimeOut_ {false};
194     bool downloadErrorState_ {false};
195     uint64_t bufferedDuration_ {0};
196     uint64_t currentBitrate_ {1 * 1024 * 1024}; // bps
197     bool userDefinedBufferDuration_ {false};
198     uint64_t expectDuration_ {0};
199     bool autoBufferSize_ {true}; // 默认为false
200     uint64_t lastCheckTime_ {0};
201     uint32_t recordCount_ {0};
202     uint64_t lastRecordTime_ {0};
203     std::atomic<bool> isInterruptNeeded_{false};
204 
205     struct BufferDownRecord {
206         /* data */
207         uint64_t dataBits {0};
208         uint64_t timeoff {0};
209         BufferDownRecord* next {nullptr};
210     };
211     // std::unique_ptr<BufferDownRecord> bufferDownRecord_;
212     BufferDownRecord* bufferDownRecord_ {nullptr};
213     // buffer least
214     struct BufferLeastRecord {
215         uint64_t minDuration {0};
216         BufferLeastRecord* next {nullptr};
217     };
218     // std::unique_ptr<BufferLeastRecord> bufferLeastRecord_;
219     BufferLeastRecord* bufferLeastRecord_ {nullptr};
220     uint64_t lastWriteTime_ {0};
221     uint64_t lastReadTime_ {0};
222     uint64_t lastWriteBit_ {0};
223     SteadyClock steadyClock_;
224 
225     uint64_t totalBits_ {0};        // 总下载量
226 
227     uint64_t lastBits_ {0};         // 上一统计周期的总下载量
228 
229     uint64_t downloadDuringTime_ {0};    // 累计有效下载时长 ms
230 
231     uint64_t downloadBits_ {0};          // 累计有效时间内下载数据量 bit
232     uint32_t changeBitRateCount_ {0};  // 设置码率次数
233     int32_t seekFailedCount_ {0};   // seek失败次数
234     int64_t openTime_ {0};
235     int64_t playDelayTime_ {0};
236     int64_t startDownloadTime_ {0};
237     int32_t avgDownloadSpeed_ {0};
238     bool isDownloadFinish_ {false};
239     double avgSpeedSum_ {0};
240     uint32_t recordSpeedCount_ {0};
241 
242     int64_t lastReportUsageTime_ {0};
243     uint64_t dataUsage_ {0};
244 
245     struct RecordData {
246         double downloadRate {0};
247         uint64_t bufferDuring {0};
248         std::shared_ptr<RecordData> next {nullptr};
249     };
250     std::shared_ptr<RecordData> recordData_ {nullptr};
251     std::map<std::string, std::string> httpHeader_ {};
252     std::atomic<bool> isStopped = false;
253     std::string mimeType_;
254     size_t waterLineAbove_ {0};
255     bool isInterrupt_ {false};
256     std::atomic<bool> isBuffering_ {false};
257     bool isFirstFrameArrived_ {false};
258     std::atomic<bool> isSeekingFlag {false};
259     Mutex switchMutex_ {};
260     bool isLastDecryptWriteError_ {false};
261     uint32_t lastRealLen_ {0};
262 
263     uint64_t lastReadCheckTime_ = 0;
264     uint64_t readTotalBytes_ = 0;
265     uint64_t readRecordDuringTime_ = 0;
266     uint64_t totalDownloadDuringTime_ {0};
267     uint32_t currentBitRate_ {0};
268     int32_t fragmentBitRate_ {0};
269     uint64_t lastDurationReacord_ {0};
270     int32_t lastCachedSize_ {0};
271     std::atomic<bool> isBufferingStart_ {false};
272     std::shared_ptr<CacheMediaChunkBufferImpl> cacheMediaBuffer_;
273     uint64_t readOffset_ {0};
274     uint64_t writeOffset_ {0};
275     std::map<uint32_t, std::pair<uint32_t, bool>> tsStorageInfo_ {};
276     std::atomic<uint32_t> readTsIndex_ {0};
277     std::atomic<bool> canWrite_ {true};
278     uint64_t ffmpegOffset_ = 0;
279     volatile size_t wantedReadLength_ {0};
280     volatile size_t bufferingTime_ {0};
281     volatile size_t readTime_ {0};
282     FairMutex tsStorageInfoMutex_ {};
283     std::shared_ptr<WriteBitrateCaculator> writeBitrateCaculator_;
284 
285     FairMutex bufferingEndMutex_ {};
286     ConditionVariable bufferingEndCond_;
287     bool isReportedErrorCode_ {false};
288 };
289 }
290 }
291 }
292 }
293 #endif