Commit 62748a33 authored by Christopher Mueller's avatar Christopher Mueller Committed by Hugo Beauzée-Luyssen

dash: added byte range requests

Signed-off-by: default avatarChristopher Mueller <christopher.mueller@itec.aau.at>
Signed-off-by: default avatarHugo Beauzée-Luyssen <beauze.h@gmail.com>
parent 7665bfa3
...@@ -29,8 +29,10 @@ ...@@ -29,8 +29,10 @@
using namespace dash::http; using namespace dash::http;
Chunk::Chunk() : startByte( 0 ), Chunk::Chunk () :
endByte( 0 ) startByte (0),
endByte (0),
hasByteRange (false)
{ {
} }
...@@ -62,3 +64,11 @@ void Chunk::addOptionalUrl (const std::string& url) ...@@ -62,3 +64,11 @@ void Chunk::addOptionalUrl (const std::string& url)
{ {
this->optionalUrls.push_back(url); this->optionalUrls.push_back(url);
} }
bool Chunk::useByteRange ()
{
return this->hasByteRange;
}
void Chunk::setUseByteRange (bool value)
{
this->hasByteRange = value;
}
...@@ -44,12 +44,15 @@ namespace dash ...@@ -44,12 +44,15 @@ namespace dash
void setStartByte (int startByte); void setStartByte (int startByte);
void setUrl (const std::string& url); void setUrl (const std::string& url);
void addOptionalUrl (const std::string& url); void addOptionalUrl (const std::string& url);
bool useByteRange ();
void setUseByteRange (bool value);
private: private:
std::string url; std::string url;
std::vector<std::string> optionalUrls; std::vector<std::string> optionalUrls;
int startByte; int startByte;
int endByte; int endByte;
bool hasByteRange;
}; };
} }
......
...@@ -29,29 +29,48 @@ ...@@ -29,29 +29,48 @@
using namespace dash::http; using namespace dash::http;
HTTPConnection::HTTPConnection (const std::string& url, stream_t *stream) HTTPConnection::HTTPConnection (Chunk *chunk, stream_t *stream) :
stream (stream),
chunk (chunk),
peekBufferLen (0)
{ {
this->url = url; this->url = chunk->getUrl();
this->stream = stream; this->peekBuffer = new uint8_t[PEEKBUFFER];
} }
HTTPConnection::~HTTPConnection () HTTPConnection::~HTTPConnection ()
{ {
delete[] this->peekBuffer;
this->closeSocket();
} }
int HTTPConnection::read (void *p_buffer, size_t len) int HTTPConnection::read (void *p_buffer, size_t len)
{ {
int size = stream_Read(this->urlStream, p_buffer, len); if(this->peekBufferLen == 0)
{
int size = net_Read(this->stream, this->httpSocket, NULL, p_buffer, len, false);
if(size <= 0) if(size <= 0)
return 0; return 0;
return size; return size;
}
memcpy(p_buffer, this->peekBuffer, this->peekBufferLen);
int ret = this->peekBufferLen;
this->peekBufferLen = 0;
return ret;
} }
int HTTPConnection::peek (const uint8_t **pp_peek, size_t i_peek) int HTTPConnection::peek (const uint8_t **pp_peek, size_t i_peek)
{ {
return stream_Peek(this->urlStream, pp_peek, i_peek); if(this->peekBufferLen == 0)
this->peekBufferLen = this->read(this->peekBuffer, PEEKBUFFER);
int size = i_peek > this->peekBufferLen ? this->peekBufferLen : i_peek;
uint8_t *peek = new uint8_t [size];
memcpy(peek, this->peekBuffer, size);
*pp_peek = peek;
return size;
} }
void HTTPConnection::parseURL () void HTTPConnection::parseURL ()
{ {
...@@ -67,16 +86,36 @@ void HTTPConnection::parseURL () ...@@ -67,16 +86,36 @@ void HTTPConnection::parseURL ()
this->request = "GET " + this->path + " HTTP/1.1\r\n" + this->request = "GET " + this->path + " HTTP/1.1\r\n" +
"Host: " + this->hostname + "\r\nConnection: close\r\n\r\n"; "Host: " + this->hostname + "\r\nConnection: close\r\n\r\n";
} }
void HTTPConnection::prepareRequest ()
{
if(!chunk->useByteRange())
{
this->request = "GET " + this->path + " HTTP/1.1" + "\r\n" +
"Host: " + this->hostname + "\r\n" +
"Connection: close\r\n\r\n";
}
else
{
std::stringstream req;
req << "GET " << this->path << " HTTP/1.1\r\n" <<
"Host: " << this->hostname << "\r\n" <<
"Range: bytes=" << this->chunk->getStartByte() << "-" << this->chunk->getEndByte() << "\r\n" <<
"Connection: close\r\n\r\n";
bool HTTPConnection::init() this->request = req.str();
}
}
bool HTTPConnection::init ()
{ {
this->urlStream = stream_UrlNew( this->stream, this->url.c_str() ); this->parseURL();
this->prepareRequest();
if( this->urlStream == NULL ) this->httpSocket = net_ConnectTCP(this->stream, this->hostname.c_str(), 80);
return false;
return true; if(this->sendData(this->request))
return this->parseHeader();
return false;
} }
bool HTTPConnection::parseHeader () bool HTTPConnection::parseHeader ()
{ {
...@@ -125,5 +164,5 @@ bool HTTPConnection::sendData (const std::string& data) ...@@ -125,5 +164,5 @@ bool HTTPConnection::sendData (const std::string& data)
} }
void HTTPConnection::closeSocket () void HTTPConnection::closeSocket ()
{ {
stream_Delete(this->urlStream); net_Close(this->httpSocket);
} }
...@@ -36,6 +36,9 @@ ...@@ -36,6 +36,9 @@
#include <sstream> #include <sstream>
#include "http/IHTTPConnection.h" #include "http/IHTTPConnection.h"
#include "http/Chunk.h"
#define PEEKBUFFER 4096
namespace dash namespace dash
{ {
...@@ -44,7 +47,7 @@ namespace dash ...@@ -44,7 +47,7 @@ namespace dash
class HTTPConnection : public IHTTPConnection class HTTPConnection : public IHTTPConnection
{ {
public: public:
HTTPConnection ( const std::string& url, stream_t *stream ); HTTPConnection ( Chunk *chunk, stream_t *stream );
virtual ~HTTPConnection (); virtual ~HTTPConnection ();
bool init (); bool init ();
...@@ -60,12 +63,15 @@ namespace dash ...@@ -60,12 +63,15 @@ namespace dash
std::string path; std::string path;
std::string request; std::string request;
stream_t *stream; stream_t *stream;
stream_t *urlStream; Chunk *chunk;
uint8_t *peekBuffer;
size_t peekBufferLen;
void parseURL (); void parseURL ();
bool sendData (const std::string& data); bool sendData (const std::string& data);
bool parseHeader (); bool parseHeader ();
std::string readLine (); std::string readLine ();
void prepareRequest ();
}; };
} }
} }
......
...@@ -137,7 +137,7 @@ int HTTPConnectionManager::peek (Chunk *chun ...@@ -137,7 +137,7 @@ int HTTPConnectionManager::peek (Chunk *chun
IHTTPConnection* HTTPConnectionManager::initConnection(Chunk *chunk) IHTTPConnection* HTTPConnectionManager::initConnection(Chunk *chunk)
{ {
HTTPConnection *con = new HTTPConnection(chunk->getUrl(), this->stream); HTTPConnection *con = new HTTPConnection(chunk, this->stream);
if ( con->init() == false ) if ( con->init() == false )
return NULL; return NULL;
this->chunkMap[chunk] = con; this->chunkMap[chunk] = con;
......
...@@ -101,9 +101,7 @@ Representation* IsoffMainManager::getRepresentation (Period *per ...@@ -101,9 +101,7 @@ Representation* IsoffMainManager::getRepresentation (Period *per
{ {
int currentBitrate = reps.at(j)->getBandwidth(); int currentBitrate = reps.at(j)->getBandwidth();
if(best == NULL || bitrate == -1 || if(best == NULL || (currentBitrate > best->getBandwidth() && currentBitrate < bitrate))
( currentBitrate > best->getBandwidth() &&
currentBitrate < bitrate ) )
{ {
std::cout << "Found a better Representation bandwidth=" << reps.at(j)->getBandwidth() << " in adaptationSet #" << i << std::endl; std::cout << "Found a better Representation bandwidth=" << reps.at(j)->getBandwidth() << " in adaptationSet #" << i << std::endl;
best = reps.at( j ); best = reps.at( j );
......
...@@ -157,7 +157,7 @@ void IsoffMainParser::setInitSegment (dash::xml::Node *segBaseNode, Segme ...@@ -157,7 +157,7 @@ void IsoffMainParser::setInitSegment (dash::xml::Node *segBaseNode, Segme
{ {
std::string range = initSeg.at(0)->getAttributeValue("range"); std::string range = initSeg.at(0)->getAttributeValue("range");
size_t pos = range.find("-"); size_t pos = range.find("-");
seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos, range.size()).c_str())); seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos + 1, range.size()).c_str()));
} }
for(size_t i = 0; i < this->mpd->getBaseUrls().size(); i++) for(size_t i = 0; i < this->mpd->getBaseUrls().size(); i++)
...@@ -175,11 +175,11 @@ void IsoffMainParser::setSegments (dash::xml::Node *segListNode, Segme ...@@ -175,11 +175,11 @@ void IsoffMainParser::setSegments (dash::xml::Node *segListNode, Segme
Segment *seg = new Segment( this->currentRepresentation ); Segment *seg = new Segment( this->currentRepresentation );
seg->setSourceUrl(segments.at(i)->getAttributeValue("media")); seg->setSourceUrl(segments.at(i)->getAttributeValue("media"));
if(segments.at(0)->hasAttribute("mediaRange")) if(segments.at(i)->hasAttribute("mediaRange"))
{ {
std::string range = segments.at(0)->getAttributeValue("mediaRange"); std::string range = segments.at(i)->getAttributeValue("mediaRange");
size_t pos = range.find("-"); size_t pos = range.find("-");
seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos, range.size()).c_str())); seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos + 1, range.size()).c_str()));
} }
for(size_t i = 0; i < this->mpd->getBaseUrls().size(); i++) for(size_t i = 0; i < this->mpd->getBaseUrls().size(); i++)
......
...@@ -90,6 +90,7 @@ dash::http::Chunk* Segment::toChunk () ...@@ -90,6 +90,7 @@ dash::http::Chunk* Segment::toChunk ()
if(this->startByte != -1 && this->endByte != -1) if(this->startByte != -1 && this->endByte != -1)
{ {
chunk->setUseByteRange(true);
chunk->setStartByte(this->startByte); chunk->setStartByte(this->startByte);
chunk->setEndByte(this->endByte); chunk->setEndByte(this->endByte);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment