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 #define HST_LOG_TAG "PlayListDownloader"
16 #include "playlist_downloader.h"
17 #include "securec.h"
18
19 namespace OHOS {
20 namespace Media {
21 namespace Plugin {
22 namespace HttpPlugin {
PlayListDownloader()23 PlayListDownloader::PlayListDownloader()
24 {
25 downloader_ = std::make_shared<Downloader>("hlsPlayList");
26 dataSave_ = [this] (uint8_t*&& data, uint32_t&& len, int64_t&& offset) {
27 return SaveData(std::forward<decltype(data)>(data), std::forward<decltype(len)>(len),
28 std::forward<decltype(offset)>(offset));
29 };
30 // this is default callback
31 statusCallback_ = [this] (DownloadStatus&& status, std::shared_ptr<Downloader> d,
32 std::shared_ptr<DownloadRequest>& request) {
33 OnDownloadStatus(std::forward<decltype(status)>(status), downloader_,
34 std::forward<decltype(request)>(request));
35 };
36 updateTask_ = std::make_shared<OSAL::Task>(std::string("FragmentListUpdate"));
37 updateTask_->RegisterHandler([this] { PlayListUpdateLoop(); });
38 }
39
~PlayListDownloader()40 PlayListDownloader::~PlayListDownloader()
41 {
42 updateTask_->Stop();
43 downloader_->Stop();
44 }
45
DoOpen(const std::string & url)46 void PlayListDownloader::DoOpen(const std::string& url)
47 {
48 playList_.clear();
49 auto realStatusCallback = [this] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
50 std::shared_ptr<DownloadRequest>& request) {
51 statusCallback_(status, downloader_, std::forward<decltype(request)>(request));
52 };
53 downloadRequest_ = std::make_shared<DownloadRequest>(url, dataSave_, realStatusCallback, true);
54 downloader_->Download(downloadRequest_, -1); // -1
55 downloader_->Start();
56 }
57
GetPlayListDownloadStatus()58 bool PlayListDownloader::GetPlayListDownloadStatus()
59 {
60 return startedDownloadStatus_;
61 }
62
SaveData(uint8_t * data,uint32_t len,int64_t offset)63 bool PlayListDownloader::SaveData(uint8_t* data, uint32_t len, int64_t offset)
64 {
65 (void)offset;
66 playList_.append(reinterpret_cast<const char*>(data), len);
67 startedDownloadStatus_ = true;
68 ParseManifest();
69 return true;
70 }
71
OnDownloadStatus(DownloadStatus status,std::shared_ptr<Downloader> &,std::shared_ptr<DownloadRequest> & request)72 void PlayListDownloader::OnDownloadStatus(DownloadStatus status, std::shared_ptr<Downloader>&,
73 std::shared_ptr<DownloadRequest>& request)
74 {
75 // This should not be called normally
76 MEDIA_LOG_D("Should not call this OnDownloadStatus, should call monitor.");
77 if (request->GetClientError() != NetworkClientErrorCode::ERROR_OK || request->GetServerError() != 0) {
78 MEDIA_LOG_E("OnDownloadStatus " PUBLIC_LOG_D32 ", url : " PUBLIC_LOG_S,
79 status, request->GetUrl().c_str());
80 }
81 }
82
SetStatusCallback(StatusCallbackFunc cb)83 void PlayListDownloader::SetStatusCallback(StatusCallbackFunc cb)
84 {
85 statusCallback_ = cb;
86 }
87
ParseManifest()88 void PlayListDownloader::ParseManifest()
89 {
90 MEDIA_LOG_E("Should not call this ParseManifest");
91 }
92
Resume()93 void PlayListDownloader::Resume()
94 {
95 downloader_->Resume();
96 updateTask_->Start();
97 }
98
Pause()99 void PlayListDownloader::Pause()
100 {
101 updateTask_->Pause();
102 downloader_->Pause();
103 }
104
Close()105 void PlayListDownloader::Close()
106 {
107 updateTask_->Stop();
108 downloader_->Stop();
109 }
110 }
111 }
112 }
113 }