1 /* 2 * Copyright (c) 2022-2022 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 "playlist_downloader.h" 20 #include "foundation/utils/ring_buffer.h" 21 #include "plugin/plugins/source/http_source/media_downloader.h" 22 23 namespace OHOS { 24 namespace Media { 25 namespace Plugin { 26 namespace HttpPlugin { 27 class HlsMediaDownloader : public MediaDownloader, public PlayListChangeCallback { 28 public: 29 HlsMediaDownloader() noexcept; 30 ~HlsMediaDownloader() override = default; 31 bool Open(const std::string& url) override; 32 void Close(bool isAsync) override; 33 void Pause() override; 34 void Resume() override; 35 bool Read(unsigned char* buff, unsigned int wantReadLength, unsigned int& realReadLength, bool& isEos) override; 36 bool Seek(int offset) override; 37 38 size_t GetContentLength() const override; 39 double GetDuration() const override; 40 Seekable GetSeekable() const override; 41 void SetCallback(Callback* cb) override; 42 void OnPlayListChanged(const std::vector<std::string>& playList) override; 43 void SetStatusCallback(StatusCallbackFunc cb) override; 44 bool GetStartedStatus() override; 45 46 private: 47 bool SaveData(uint8_t* data, uint32_t len); 48 void FragmentDownloadLoop(); 49 50 private: 51 std::shared_ptr<RingBuffer> buffer_; 52 std::shared_ptr<Downloader> downloader_; 53 std::shared_ptr<DownloadRequest> downloadRequest_; 54 55 Callback* callback_ {nullptr}; 56 DataSaveFunc dataSave_; 57 StatusCallbackFunc statusCallback_; 58 bool startedPlayStatus_ {false}; 59 60 std::shared_ptr<PlayListDownloader> playListDownloader_; 61 62 std::shared_ptr<OSAL::Task> downloadTask_; 63 std::shared_ptr<BlockingQueue<std::string>> playList_; 64 std::map<std::string, bool> fragmentDownloadStart; 65 }; 66 } 67 } 68 } 69 } 70 #endif