Skip to content

Commit

Permalink
Merge pull request xbmc#9 from matthuisman/redirect_2
Browse files Browse the repository at this point in the history
Fix up effective url
  • Loading branch information
matthuisman committed Aug 17, 2020
2 parents 4f3e038 + d3a4fa6 commit 0a713c6
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 98 deletions.
14 changes: 0 additions & 14 deletions src/common/AdaptiveStream.cpp
Expand Up @@ -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_)
Expand Down
2 changes: 0 additions & 2 deletions src/common/AdaptiveStream.h
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/common/AdaptiveTree.h
Expand Up @@ -27,6 +27,7 @@
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <sstream>

namespace adaptive
{
Expand Down Expand Up @@ -410,7 +411,7 @@ class AdaptiveTree
}*current_period_, *next_period_;

std::vector<Period*> 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_;
Expand Down Expand Up @@ -485,7 +486,7 @@ class AdaptiveTree
const std::chrono::time_point<std::chrono::system_clock> GetLastMediaRenewal() const { return lastMediaRenewal_; };

protected:
virtual bool download(const char* url, const std::map<std::string, std::string> &manifestHeaders, void *opaque = nullptr, bool scanEffectiveURL = true);
virtual bool download(const char* url, const std::map<std::string, std::string> &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();
Expand Down
34 changes: 5 additions & 29 deletions src/main.cpp
Expand Up @@ -261,8 +261,7 @@ Kodi Streams implementation

bool adaptive::AdaptiveTree::download(const char* url,
const std::map<std::string, std::string>& manifestHeaders,
void* opaque,
bool scanEffectiveURL)
void* opaque)
{
// open the file
kodi::vfs::CFile file;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -374,10 +352,8 @@ bool KodiAdaptiveStream::download(const char* url,
std::vector<kodi::vfs::CDirEntry> 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
Expand Down
54 changes: 33 additions & 21 deletions src/parser/DASHTree.cpp
Expand Up @@ -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;
Expand All @@ -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<std::stringstream*>(opaque)->write(static_cast<const char*>(buffer), buffer_size);
return true;
}

Expand Down Expand Up @@ -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_)
{
Expand Down
1 change: 1 addition & 0 deletions src/parser/DASHTree.h
Expand Up @@ -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_;
Expand Down
3 changes: 1 addition & 2 deletions src/parser/DASHTreeTest.cpp
Expand Up @@ -29,8 +29,7 @@ void Log(const LogLevel loglevel, const char* format, ...)

bool adaptive::AdaptiveTree::download(const char* url,
const std::map<std::string, std::string>& manifestHeaders,
void* opaque,
bool scanEffectiveURL)
void* opaque)
{
FILE* f = fopen(testfile.c_str(), "rb");
if (!f)
Expand Down
30 changes: 12 additions & 18 deletions src/parser/HLSTree.cpp
Expand Up @@ -160,17 +160,20 @@ int HLSTree::processEncryption(std::string baseUrl, std::map<std::string, std::s

bool HLSTree::open(const std::string& url, const std::string& manifestUpdateParam)
{
PreparePaths(url, manifestUpdateParam);
if (download(manifest_url_.c_str(), manifest_headers_, &manifest_stream))
return processManifest(manifest_stream, url);
return false;
bool ret = download(url.c_str(), manifest_headers_, &manifest_stream);
PreparePaths(effective_url_, manifestUpdateParam);

if (!ret)
return false;

return processManifest(manifest_stream, effective_url_);
}

bool HLSTree::processManifest(std::stringstream& stream, const std::string& url)
{
#if FILEDEBUG
FILE* f = fopen("inputstream_adaptive_master.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

Expand Down Expand Up @@ -310,10 +313,7 @@ bool HLSTree::processManifest(std::stringstream& stream, const std::string& url)
current_representation_->bandwidth_ = 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
Expand Down Expand Up @@ -414,16 +414,13 @@ HLSTree::PREPARE_RESULT HLSTree::prepareRepresentation(Period* period,
Representation* entry_rep = rep;
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);
Expand Down Expand Up @@ -832,10 +829,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();
}
Expand Down
1 change: 0 additions & 1 deletion src/parser/HLSTree.h
Expand Up @@ -19,7 +19,6 @@
#pragma once

#include "../common/AdaptiveTree.h"
#include <sstream>
#include <map>

class AESDecrypter;
Expand Down
35 changes: 26 additions & 9 deletions src/parser/SmoothTree.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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<std::stringstream*>(opaque)->write(static_cast<const char*>(buffer), buffer_size);
return true;
}
}
1 change: 1 addition & 0 deletions src/parser/SmoothTree.h
Expand Up @@ -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
{
Expand Down

0 comments on commit 0a713c6

Please sign in to comment.