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 "HlsPlayListDownloader" 16 #include <mutex> 17 #include "hls_playlist_downloader.h" 18 19 namespace OHOS { 20 namespace Media { 21 namespace Plugin { 22 namespace HttpPlugin { PlayListUpdateLoop()23void HlsPlayListDownloader::PlayListUpdateLoop() 24 { 25 OSAL::SleepFor(5000); // 5000 how often is playlist updated 26 UpdateManifest(); 27 } 28 29 // StateMachine thread: call plugin SetSource -> call Open 30 // StateMachine thread: call plugin GetSeekable -> call GetSeekable 31 // PlayListDownload thread: call ParseManifest 32 // First call Open, then start PlayListDownload thread, it seems no lock is required. 33 // [In future] StateMachine thread: call plugin GetDuration -> call GetDuration Open(const std::string & url)34void HlsPlayListDownloader::Open(const std::string& url) 35 { 36 url_ = url; 37 master_ = nullptr; 38 DoOpen(url); 39 } 40 UpdateManifest()41void HlsPlayListDownloader::UpdateManifest() 42 { 43 if (currentVariant_ && currentVariant_->m3u8_ && !currentVariant_->m3u8_->uri_.empty()) { 44 DoOpen(currentVariant_->m3u8_->uri_); 45 } else { 46 MEDIA_LOG_E("UpdateManifest currentVariant_ not ready."); 47 } 48 } 49 SetPlayListCallback(PlayListChangeCallback * callback)50void HlsPlayListDownloader::SetPlayListCallback(PlayListChangeCallback* callback) 51 { 52 callback_ = callback; 53 } 54 GetDuration() const55double HlsPlayListDownloader::GetDuration() const 56 { 57 if (!master_) { 58 return 0; 59 } 60 return master_->bLive_ ? -1.0 : master_->duration_; // -1.0 61 } 62 GetSeekable() const63Seekable HlsPlayListDownloader::GetSeekable() const 64 { 65 if (!master_) { 66 return Seekable::INVALID; 67 } 68 return master_->bLive_ ? Seekable::UNSEEKABLE : Seekable::SEEKABLE; 69 } 70 ParseManifest()71void HlsPlayListDownloader::ParseManifest() 72 { 73 if (!master_) { 74 master_ = std::make_shared<M3U8MasterPlaylist>(playList_, url_); 75 currentVariant_ = master_->defaultVariant_; 76 if (!master_->isSimple_) { 77 UpdateManifest(); 78 } 79 updateTask_->Start(); 80 } else { 81 currentVariant_->m3u8_->Update(playList_); 82 auto files = currentVariant_->m3u8_->files_; 83 auto playList = std::vector<std::string>(); 84 playList.reserve(files.size()); 85 for (auto &file: files) { 86 playList.push_back(file->uri_); 87 } 88 callback_->OnPlayListChanged(playList); 89 } 90 } 91 } 92 } 93 } 94 }