• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DOWNLOAD_MONITOR_H
17 #define HISTREAMER_DOWNLOAD_MONITOR_H
18 
19 #include <ctime>
20 #include <list>
21 #include <memory>
22 #include <string>
23 #include "foundation/osal/thread/task.h"
24 #include "foundation/osal/thread/mutex.h"
25 #include "foundation/utils/blocking_queue.h"
26 #include "foundation/utils/ring_buffer.h"
27 #include "plugin/interface/plugin_base.h"
28 #include "plugin/plugins/source/http_source/download/downloader.h"
29 #include "plugin/plugins/source/http_source/media_downloader.h"
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Plugin {
34 namespace HttpPlugin {
35 struct RetryRequest {
36     std::shared_ptr<DownloadRequest> request;
37     std::function<void()> function;
38 };
39 
40 class DownloadMonitor : public MediaDownloader {
41 public:
42     explicit DownloadMonitor(std::shared_ptr<MediaDownloader> downloader) noexcept;
43     ~DownloadMonitor() override = default;
44     bool Open(const std::string& url) override;
45     void Close(bool isAsync) override;
46     bool Read(unsigned char* buff, unsigned int wantReadLength, unsigned int& realReadLength, bool& isEos) override;
47     bool Seek(int offset) override;
48     void Pause() override;
49     void Resume() override;
50     size_t GetContentLength() const override;
51     double GetDuration() const override;
52     Seekable GetSeekable() const override;
53     void SetCallback(Callback *cb) override;
54     void SetStatusCallback(StatusCallbackFunc cb) override;
55     bool GetStartedStatus() override;
56 
57 private:
58     void HttpMonitorLoop();
59     void OnDownloadStatus(std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request);
60     bool NeedRetry(const std::shared_ptr<DownloadRequest>& request);
61 
62     std::shared_ptr<MediaDownloader> downloader_;
63     std::list<RetryRequest> retryTasks_;
64     std::atomic<bool> isPlaying_ {false};
65     std::shared_ptr<OSAL::Task> task_;
66     time_t lastReadTime_ {0};
67     Callback* callback_ {nullptr};
68     OSAL::Mutex taskMutex_ {};
69 };
70 }
71 }
72 }
73 }
74 #endif
75