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