Skip to content

Commit

Permalink
Merge branch 'master' into Leia
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed May 29, 2019
2 parents d98fbc9 + 548924c commit 06d0000
Show file tree
Hide file tree
Showing 19 changed files with 2,252 additions and 2,062 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -16,6 +16,7 @@ set(ADP_SOURCES
src/parser/SmoothTree.cpp
src/parser/TTML.cpp
src/parser/WebVTT.cpp
src/parser/PRProtectionParser.cpp
src/common/AdaptiveStream.cpp
src/helpers.cpp
src/oscompat.cpp
Expand All @@ -38,6 +39,7 @@ set(ADP_HEADERS
src/parser/SmoothTree.h
src/parser/TTML.h
src/parser/WebVTT.h
src/parser/PRProtectionParser.h
src/TSReader.h
src/log.h
src/aes_decrypter.h
Expand Down
2 changes: 1 addition & 1 deletion inputstream.adaptive/resources/settings.xml.in
Expand Up @@ -83,7 +83,7 @@
</setting>
<setting id="WIDEVINE_API" type="integer">
<level>4</level>
<default>9</default>
<default>10</default>
</setting>
</group>
</category>
Expand Down
1 change: 1 addition & 0 deletions src/TSReader.cpp
Expand Up @@ -136,6 +136,7 @@ bool TSReader::GetInformation(INPUTSTREAM_INFO &info)
info.m_ExtraData = (uint8_t*)realloc((void*)info.m_ExtraData, tsInfo.m_stream->stream_info.extra_data_size);
memcpy((void*)info.m_ExtraData, tsInfo.m_stream->stream_info.extra_data, tsInfo.m_stream->stream_info.extra_data_size);
info.m_ExtraSize = tsInfo.m_stream->stream_info.extra_data_size;
ret = true;
}
return ret;
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/AdaptiveStream.cpp
Expand Up @@ -268,8 +268,9 @@ bool AdaptiveStream::prepareDownload(const AdaptiveTree::Segment *seg)
download_url_ = current_rep_->url_ + download_url_;
}
else
{
download_url_ = current_rep_->url_;
if (~seg->range_begin_)
{
uint64_t fileOffset = seg != &current_rep_->initialization_ ? m_segmentFileOffset : 0;
if (~seg->range_end_)
sprintf(rangebuf, "bytes=%" PRIu64 "-%" PRIu64, seg->range_begin_ + fileOffset, seg->range_end_ + fileOffset);
Expand Down
7 changes: 2 additions & 5 deletions src/common/AdaptiveTree.h
Expand Up @@ -116,11 +116,8 @@ namespace adaptive
{
void SetRange(const char *range);
uint64_t range_begin_; //Either byterange start or timestamp or ~0
union
{
uint64_t range_end_; //Either byterange end or sequence_id or char* if range_begin is ~0
const char *url;
};
uint64_t range_end_; //Either byterange end or sequence_id if range_begin is ~0
const char *url;
uint64_t startPTS_;
uint16_t pssh_set_;
};
Expand Down
27 changes: 19 additions & 8 deletions src/parser/DASHTree.cpp
Expand Up @@ -26,6 +26,7 @@
#include "../oscompat.h"
#include "../helpers.h"
#include "../log.h"
#include "PRProtectionParser.h"

using namespace adaptive;

Expand All @@ -45,7 +46,8 @@ enum
MPDNODE_PSSH = 1 << 12,
MPDNODE_SEGMENTTEMPLATE = 1 << 13,
MPDNODE_SEGMENTTIMELINE = 1 << 14,
MPDNODE_ROLE = 1 << 15
MPDNODE_ROLE = 1 << 15,
MPDNODE_PLAYREADYWRMHEADER = 1 << 16
};

static const char* ltranslate(const char * in)
Expand Down Expand Up @@ -191,10 +193,7 @@ bool ParseContentProtection(const char **attr, DASHTree *dash)
if (strcmp((const char*)*(attr + 1), "urn:mpeg:dash:mp4protection:2011") == 0)
mpdFound = true;
else
{
urnFound = stricmp(dash->supportedKeySystem_.c_str(), (const char*)*(attr + 1)) == 0;
break;
}
}
else if (strcmp((const char*)*attr, "cenc:default_KID") == 0)
defaultKID = (const char*)*(attr + 1);
Expand All @@ -204,9 +203,8 @@ bool ParseContentProtection(const char **attr, DASHTree *dash)
{
dash->currentNode_ |= MPDNODE_CONTENTPROTECTION;
dash->encryptionState_ |= DASHTree::ENCRYTIONSTATE_SUPPORTED;
return true;
}
else if (mpdFound && defaultKID && strlen(defaultKID) == 36)
if ((urnFound || mpdFound) && defaultKID && strlen(defaultKID) == 36)
{
dash->current_defaultKID_.resize(16);
for (unsigned int i(0); i < 16; ++i)
Expand All @@ -218,7 +216,7 @@ bool ParseContentProtection(const char **attr, DASHTree *dash)
}
}
// Return if we have URN or not
return !mpdFound;
return urnFound || !mpdFound;
}

/*----------------------------------------------------------------------
Expand Down Expand Up @@ -688,6 +686,11 @@ start(void *data, const char *el, const char **attr)
}
}
}
else if (strcmp(el, "mspr:pro") == 0)
{
dash->strXMLText_.clear();
dash->currentNode_ |= MPDNODE_PLAYREADYWRMHEADER;
}
}
else if (dash->currentNode_ & (MPDNODE_SEGMENTLIST | MPDNODE_SEGMENTTEMPLATE))
{
Expand Down Expand Up @@ -755,6 +758,7 @@ start(void *data, const char *el, const char **attr)
dash->current_adaptationset_->segment_durations_ = dash->current_period_->segment_durations_;
dash->current_adaptationset_->segtpl_ = dash->current_period_->segtpl_;
dash->current_adaptationset_->startNumber_ = dash->current_period_->startNumber_;
dash->current_playready_wrmheader_.clear();

for (; *attr;)
{
Expand Down Expand Up @@ -940,7 +944,7 @@ static void XMLCALL
text(void *data, const char *s, int len)
{
DASHTree *dash(reinterpret_cast<DASHTree*>(data));
if (dash->currentNode_ & (MPDNODE_BASEURL | MPDNODE_PSSH))
if (dash->currentNode_ & (MPDNODE_BASEURL | MPDNODE_PSSH | MPDNODE_PLAYREADYWRMHEADER))
dash->strXMLText_ += std::string(s, len);
}

Expand Down Expand Up @@ -1177,6 +1181,11 @@ end(void *data, const char *el)
dash->currentNode_ &= ~MPDNODE_CONTENTPROTECTION;
}
}
else if (dash->currentNode_ & MPDNODE_PLAYREADYWRMHEADER)
{
dash->current_playready_wrmheader_ = dash->strXMLText_;
dash->currentNode_ &= ~MPDNODE_PLAYREADYWRMHEADER;
}
else if (strcmp(el, "AdaptationSet") == 0)
{
dash->currentNode_ &= ~MPDNODE_ADAPTIONSET;
Expand All @@ -1194,6 +1203,8 @@ end(void *data, const char *el)
if (dash->adp_pssh_set_ == 0xFF)
{
dash->current_pssh_ = "FILE";
if (dash->current_defaultKID_.empty() && !dash->current_playready_wrmheader_.empty())
dash->current_defaultKID_ = PRProtectionParser(dash->current_playready_wrmheader_).getKID();
dash->adp_pssh_set_ = static_cast<uint8_t>(dash->insert_psshset(dash->current_adaptationset_->type_));
dash->encryptionState_ |= DASHTree::ENCRYTIONSTATE_SUPPORTED;
}
Expand Down
1 change: 1 addition & 0 deletions src/parser/DASHTree.h
Expand Up @@ -35,6 +35,7 @@ namespace adaptive
uint64_t pts_helper_;
uint32_t firstStartNumber_;
uint64_t current_period_start_;
std::string current_playready_wrmheader_;
protected:
virtual void RefreshSegments() override;
};
Expand Down
122 changes: 122 additions & 0 deletions src/parser/PRProtectionParser.cpp
@@ -0,0 +1,122 @@
/*
* Copyright (C) 2019 peak3d
* http://www.peak3d.de
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <http://www.gnu.org/licenses/>.
*
*/

#include "PRProtectionParser.h"
#include "expat.h"
#include "../helpers.h"
#include <string.h>

namespace adaptive
{

/*----------------------------------------------------------------------
| expat protection start
+---------------------------------------------------------------------*/
static void XMLCALL
protection_start(void *data, const char *el, const char **attr)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
parser->m_strXMLText.clear();
}

/*----------------------------------------------------------------------
| expat protection text
+---------------------------------------------------------------------*/
static void XMLCALL
protection_text(void *data, const char *s, int len)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
parser->m_strXMLText += std::string(s, len);
}

/*----------------------------------------------------------------------
| expat protection end
+---------------------------------------------------------------------*/
static void XMLCALL
protection_end(void *data, const char *el)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
if (strcmp(el, "KID") == 0)
{
uint8_t buffer[32];
unsigned int buffer_size(32);
b64_decode(parser->m_strXMLText.data(), parser->m_strXMLText.size(), buffer, buffer_size);

if (buffer_size == 16)
{
char kid[17]; kid[16] = 0;
prkid2wvkid(reinterpret_cast<const char *>(buffer), kid);
parser->setKID(kid);
}
}
else if (strcmp(el, "LA_URL") == 0)
{
parser->setLicenseURL(parser->m_strXMLText);
}
}

PRProtectionParser::PRProtectionParser(std::string wwrmheader)
{
if (wwrmheader.empty())
return;

//(p)repair the content
std::string::size_type pos = 0;
while ((pos = wwrmheader.find('\n', 0)) != std::string::npos)
wwrmheader.erase(pos, 1);

while (wwrmheader.size() & 3)
wwrmheader += "=";

unsigned int xml_size = wwrmheader.size();
uint8_t *buffer = (uint8_t*)malloc(xml_size), *xml_start(buffer);

if (!b64_decode(wwrmheader.c_str(), xml_size, buffer, xml_size))
{
free(buffer);
return;
}

m_strPSSH = std::string(reinterpret_cast<char*>(buffer), xml_size);

while (xml_size && *xml_start != '<')
{
xml_start++;
xml_size--;
}

XML_Parser pp = XML_ParserCreate("UTF-16");
if (!pp)
{
free(buffer);
return;
}

XML_SetUserData(pp, (void*)this);
XML_SetElementHandler(pp, protection_start, protection_end);
XML_SetCharacterDataHandler(pp, protection_text);

bool done(false);
XML_Parse(pp, (const char*)(xml_start), xml_size, done);

XML_ParserFree(pp);
free(buffer);
}


}//namespace
44 changes: 44 additions & 0 deletions src/parser/PRProtectionParser.h
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 peak3d
* http://www.peak3d.de
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <string>

namespace adaptive
{

class PRProtectionParser
{
public:
PRProtectionParser(std::string wrmheader);
std::string getKID() const { return m_strKID; };
std::string getLicenseURL() const { return m_strLicenseURL; };
std::string getPSSH() const { return m_strPSSH; };

void setKID(const std::string kid) { m_strKID = kid; };
void setLicenseURL(const std::string licenseURL) { m_strLicenseURL = licenseURL; };

std::string m_strXMLText;
private:
std::string m_strKID;
std::string m_strLicenseURL;
std::string m_strPSSH;
};

} // namespace

0 comments on commit 06d0000

Please sign in to comment.