From d3a4fa6e6664494acd6dd601283bc58011fe11d0 Mon Sep 17 00:00:00 2001 From: Matt Huisman Date: Fri, 24 Jul 2020 13:21:00 +1200 Subject: [PATCH] Fix up effective url --- src/common/AdaptiveStream.cpp | 14 --------- src/common/AdaptiveStream.h | 2 -- src/common/AdaptiveTree.h | 5 ++-- src/main.cpp | 34 ++++------------------ src/parser/DASHTree.cpp | 54 +++++++++++++++++++++-------------- src/parser/DASHTree.h | 1 + src/parser/DASHTreeTest.cpp | 3 +- src/parser/HLSTree.cpp | 30 ++++++++----------- src/parser/HLSTree.h | 1 - src/parser/SmoothTree.cpp | 35 +++++++++++++++++------ src/parser/SmoothTree.h | 1 + 11 files changed, 82 insertions(+), 98 deletions(-) diff --git a/src/common/AdaptiveStream.cpp b/src/common/AdaptiveStream.cpp index 972493fbe..b278dee49 100644 --- a/src/common/AdaptiveStream.cpp +++ b/src/common/AdaptiveStream.cpp @@ -368,23 +368,9 @@ bool AdaptiveStream::prepareDownload(const AdaptiveTree::Segment* seg) else download_headers_.erase("Range"); - if (!tree_.effective_url_.empty() && download_url_.find(tree_.base_url_) == 0) - download_url_.replace(0, tree_.base_url_.size(), tree_.effective_url_); - return true; } -std::string AdaptiveStream::buildDownloadUrl(const std::string& url) -{ - if (!tree_.effective_url_.empty() && url.find(tree_.base_url_) == 0) - { - std::string newUrl(url); - newUrl.replace(0, tree_.base_url_.size(), tree_.effective_url_); - return newUrl; - } - return url; -} - bool AdaptiveStream::ensureSegment() { if (stopped_) diff --git a/src/common/AdaptiveStream.h b/src/common/AdaptiveStream.h index 91d0f9186..5262ed476 100644 --- a/src/common/AdaptiveStream.h +++ b/src/common/AdaptiveStream.h @@ -87,10 +87,8 @@ namespace adaptive virtual bool parseIndexRange() { return false; }; bool write_data(const void *buffer, size_t buffer_size); bool prepareDownload(const AdaptiveTree::Segment *seg); - void setEffectiveURL(const std::string url) { tree_.effective_url_ = url; if (tree_.effective_url_.back() != '/') tree_.effective_url_ += '/'; }; const std::string& getMediaRenewalUrl() const { return tree_.media_renewal_url_; }; const uint32_t& getMediaRenewalTime() const { return tree_.media_renewal_time_; }; - std::string buildDownloadUrl(const std::string &url); uint32_t SecondsSinceMediaRenewal() const; void UpdateSecondsSinceMediaRenewal(); private: diff --git a/src/common/AdaptiveTree.h b/src/common/AdaptiveTree.h index 2f03bd373..b4b51ad8b 100644 --- a/src/common/AdaptiveTree.h +++ b/src/common/AdaptiveTree.h @@ -27,6 +27,7 @@ #include #include #include +#include namespace adaptive { @@ -410,7 +411,7 @@ class AdaptiveTree }*current_period_, *next_period_; std::vector periods_; - std::string manifest_url_, base_url_, effective_url_, effective_filename_, base_domain_, update_parameter_; + std::string manifest_url_, base_url_, effective_url_, base_domain_, update_parameter_; std::string::size_type update_parameter_pos_; std::string etag_, last_modified_; std::string media_renewal_url_; @@ -485,7 +486,7 @@ class AdaptiveTree const std::chrono::time_point GetLastMediaRenewal() const { return lastMediaRenewal_; }; protected: - virtual bool download(const char* url, const std::map &manifestHeaders, void *opaque = nullptr, bool scanEffectiveURL = true); + virtual bool download(const char* url, const std::map &manifestHeaders, void *opaque = nullptr); virtual bool write_data(void *buffer, size_t buffer_size, void *opaque) = 0; bool PreparePaths(const std::string &url, const std::string &manifestUpdateParam); void SortTree(); diff --git a/src/main.cpp b/src/main.cpp index cbaadfde7..c93baa969 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -261,8 +261,7 @@ Kodi Streams implementation bool adaptive::AdaptiveTree::download(const char* url, const std::map& manifestHeaders, - void* opaque, - bool scanEffectiveURL) + void* opaque) { // open the file kodi::vfs::CFile file; @@ -283,27 +282,7 @@ bool adaptive::AdaptiveTree::download(const char* url, return false; } - if (scanEffectiveURL) - { - effective_url_ = file.GetPropertyValue(ADDON_FILE_PROPERTY_EFFECTIVE_URL, ""); - kodi::Log(ADDON_LOG_DEBUG, "Effective URL %s", effective_url_.c_str()); - - std::string::size_type paramPos = effective_url_.find_first_of('?'); - if (paramPos != std::string::npos) - effective_url_.resize(paramPos); - - paramPos = effective_url_.find_last_of('/'); - if (paramPos != std::string::npos) - { - effective_filename_ = effective_url_.substr(paramPos + 1); - effective_url_.resize(paramPos + 1); - } - else - effective_url_.clear(); - - if (effective_url_ == base_url_) - effective_url_.clear(); - } + effective_url_ = file.GetPropertyValue(ADDON_FILE_PROPERTY_EFFECTIVE_URL, ""); // read the file static const unsigned int CHUNKSIZE = 16384; @@ -320,7 +299,7 @@ bool adaptive::AdaptiveTree::download(const char* url, file.Close(); - kodi::Log(ADDON_LOG_DEBUG, "Download %s finished", url); + kodi::Log(ADDON_LOG_DEBUG, "Download %s finished", effective_url_.c_str()); return nbRead == 0; } @@ -331,7 +310,6 @@ bool KodiAdaptiveStream::download(const char* url, bool retry_403 = true; bool retry_MRT = true; kodi::vfs::CFile file; - std::string newUrl; RETRY: // open the file @@ -374,10 +352,8 @@ bool KodiAdaptiveStream::download(const char* url, std::vector items; if (kodi::vfs::GetDirectory(getMediaRenewalUrl(), "", items) && items.size() == 1) { - kodi::Log(ADDON_LOG_DEBUG, "Renewed URL: %s", items[0].Path().c_str()); - setEffectiveURL(items[0].Path()); - newUrl = buildDownloadUrl(url); - url = newUrl.c_str(); + url = items[0].Path().c_str(); + kodi::Log(ADDON_LOG_DEBUG, "Renewed URL: %s", url); goto RETRY; } else diff --git a/src/parser/DASHTree.cpp b/src/parser/DASHTree.cpp index 092897c83..fb25c0166 100644 --- a/src/parser/DASHTree.cpp +++ b/src/parser/DASHTree.cpp @@ -1540,7 +1540,24 @@ static void XMLCALL end(void* data, const char* el) +---------------------------------------------------------------------*/ bool DASHTree::open(const std::string& url, const std::string& manifestUpdateParam) { - PreparePaths(url, manifestUpdateParam); + std::stringstream manifest_stream; + bool ret = download(url.c_str(), manifest_headers_, &manifest_stream); + PreparePaths(effective_url_, manifestUpdateParam); + + if (!ret) + return false; + + return processManifest(manifest_stream); +} + +bool DASHTree::processManifest(std::stringstream& stream) +{ +#if FILEDEBUG + FILE* f = fopen("inputstream_adaptive.mpd", "w"); + fwrite(stream.str().data(), 1, stream.str().size(), f); + fclose(f); +#endif + parser_ = XML_ParserCreate(NULL); if (!parser_) return false; @@ -1551,34 +1568,31 @@ bool DASHTree::open(const std::string& url, const std::string& manifestUpdatePar currentNode_ = 0; strXMLText_.clear(); - std::string download_url = manifest_url_; - if (!effective_url_.empty() && download_url.find(base_url_) == 0) - download_url.replace(0, base_url_.size(), effective_url_); - - bool ret = download(download_url.c_str(), manifest_headers_) && !periods_.empty(); + bool done(true); + XML_Status retval = XML_Parse(parser_, stream.str().data(), stream.str().size(), done); XML_ParserFree(parser_); parser_ = 0; - if (ret) + if (retval == XML_STATUS_ERROR) { - current_period_ = periods_[0]; - SortTree(); - StartUpdateThread(); + //unsigned int byteNumber = XML_GetErrorByteIndex(parser_); + return false; } - return ret; + + if (periods_.empty()) + return false; + + current_period_ = periods_[0]; + SortTree(); + StartUpdateThread(); + + return true; } bool DASHTree::write_data(void* buffer, size_t buffer_size, void* opaque) { - bool done(false); - XML_Status retval = XML_Parse(parser_, (const char*)buffer, buffer_size, done); - - if (retval == XML_STATUS_ERROR) - { - //unsigned int byteNumber = XML_GetErrorByteIndex(parser_); - return false; - } + static_cast(opaque)->write(static_cast(buffer), buffer_size); return true; } @@ -1638,8 +1652,6 @@ void DASHTree::RefreshLiveSegments() updateTree.supportedKeySystem_ = supportedKeySystem_; //Location element should be used on updates updateTree.location_ = location_; - updateTree.effective_url_ = effective_url_; - updateTree.effective_filename_ = effective_filename_; if (!~update_parameter_pos_) { diff --git a/src/parser/DASHTree.h b/src/parser/DASHTree.h index 887420aca..45a62a45c 100644 --- a/src/parser/DASHTree.h +++ b/src/parser/DASHTree.h @@ -33,6 +33,7 @@ namespace adaptive AdaptationSet* adp, Representation* rep, StreamType type) override; + virtual bool processManifest(std::stringstream& stream); void SetUpdateInterval(uint32_t interval) { updateInterval_ = interval; }; uint64_t pts_helper_; diff --git a/src/parser/DASHTreeTest.cpp b/src/parser/DASHTreeTest.cpp index 1e990975d..f30bc6f74 100644 --- a/src/parser/DASHTreeTest.cpp +++ b/src/parser/DASHTreeTest.cpp @@ -29,8 +29,7 @@ void Log(const LogLevel loglevel, const char* format, ...) bool adaptive::AdaptiveTree::download(const char* url, const std::map& manifestHeaders, - void* opaque, - bool scanEffectiveURL) + void* opaque) { FILE* f = fopen(testfile.c_str(), "rb"); if (!f) diff --git a/src/parser/HLSTree.cpp b/src/parser/HLSTree.cpp index a716fa9d5..9b5a0de75 100644 --- a/src/parser/HLSTree.cpp +++ b/src/parser/HLSTree.cpp @@ -164,17 +164,20 @@ int HLSTree::processEncryption(std::string baseUrl, std::mapbandwidth_ = 0; current_representation_->codecs_ = getVideoCodec(""); current_representation_->containerType_ = CONTAINERTYPE_NOTYPE; - if (!effective_url_.empty()) - current_representation_->source_url_ = effective_url_ + effective_filename_; - else - current_representation_->source_url_ = url; + current_representation_->source_url_ = url; current_adaptationset_->representations_.push_back(current_representation_); // We assume audio is included @@ -426,16 +426,13 @@ HLSTree::PREPARE_RESULT HLSTree::prepareRepresentation(Period* period, uint32_t discont_count = 0; PREPARE_RESULT retVal = PREPARE_RESULT_OK; - if (!effective_url_.empty() && download_url.find(base_url_) == 0) - download_url.replace(0, base_url_.size(), effective_url_); - if (rep->flags_ & Representation::DOWNLOADED) ; - else if (download(download_url.c_str(), manifest_headers_, &stream, false)) + else if (download(download_url.c_str(), manifest_headers_, &stream)) { #if FILEDEBUG FILE* f = fopen("inputstream_adaptive_sub.m3u8", "w"); - fwrite(m_stream.str().data(), 1, m_stream.str().size(), f); + fwrite(stream.str().data(), 1, stream.str().size(), f); fclose(f); #endif bool byteRange(false); @@ -803,10 +800,7 @@ void HLSTree::OnDataArrived(unsigned int segNum, if (keyParts.size() > 1) parseheader(headers, keyParts[1].c_str()); - if (!effective_url_.empty() && url.find(base_url_) == 0) - url.replace(0, base_url_.size(), effective_url_); - - if (download(url.c_str(), headers, &stream, false)) + if (download(url.c_str(), headers, &stream)) { pssh.defaultKID_ = stream.str(); } diff --git a/src/parser/HLSTree.h b/src/parser/HLSTree.h index 3bb5618e2..5b1f1ea50 100644 --- a/src/parser/HLSTree.h +++ b/src/parser/HLSTree.h @@ -19,7 +19,6 @@ #pragma once #include "../common/AdaptiveTree.h" -#include #include class AESDecrypter; diff --git a/src/parser/SmoothTree.cpp b/src/parser/SmoothTree.cpp index 41669d9b4..698553066 100644 --- a/src/parser/SmoothTree.cpp +++ b/src/parser/SmoothTree.cpp @@ -349,24 +349,45 @@ static void XMLCALL end(void* data, const char* el) bool SmoothTree::open(const std::string& url, const std::string& manifestUpdateParam) { - PreparePaths(url, manifestUpdateParam); + std::stringstream manifest_stream; + bool ret = download(url.c_str(), manifest_headers_, &manifest_stream); + PreparePaths(effective_url_, manifestUpdateParam); + + if (!ret) + return false; + + return processManifest(manifest_stream); +} + +bool SmoothTree::processManifest(std::stringstream& stream) +{ +#if FILEDEBUG + FILE* f = fopen("inputstream_adaptive.ism", "w"); + fwrite(stream.str().data(), 1, stream.str().size(), f); + fclose(f); +#endif parser_ = XML_ParserCreate(NULL); if (!parser_) return false; + XML_SetUserData(parser_, (void*)this); XML_SetElementHandler(parser_, start, end); XML_SetCharacterDataHandler(parser_, text); currentNode_ = 0; strXMLText_.clear(); - bool ret = download(manifest_url_.c_str(), manifest_headers_); + bool done(true); + XML_Status retval = XML_Parse(parser_, stream.str().data(), stream.str().size(), done); XML_ParserFree(parser_); parser_ = 0; - if (!ret) + if (retval == XML_STATUS_ERROR) + { + //unsigned int byteNumber = XML_GetErrorByteIndex(parser_); return false; + } uint8_t psshset(0); @@ -405,10 +426,6 @@ bool SmoothTree::open(const std::string& url, const std::string& manifestUpdateP bool SmoothTree::write_data(void* buffer, size_t buffer_size, void* opaque) { - bool done(false); - XML_Status retval = XML_Parse(parser_, (const char*)buffer, buffer_size, done); - - if (retval == XML_STATUS_ERROR) - return false; + static_cast(opaque)->write(static_cast(buffer), buffer_size); return true; -} +} \ No newline at end of file diff --git a/src/parser/SmoothTree.h b/src/parser/SmoothTree.h index f6903aae6..971f0f753 100644 --- a/src/parser/SmoothTree.h +++ b/src/parser/SmoothTree.h @@ -29,6 +29,7 @@ namespace adaptive SmoothTree(); virtual bool open(const std::string &url, const std::string &manifestUpdateParam) override; virtual bool write_data(void *buffer, size_t buffer_size, void *opaque) override; + virtual bool processManifest(std::stringstream& stream); enum {