From fe6d0fdccf957ed15d9c637adca797c3636c8116 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 21 Mar 2011 00:40:49 +0100 Subject: [PATCH 1/5] Nieuwe awesome parser des doods voor RAW tcpflow logs van F4M streams, en hopelijk fix voor F4M streams zelf, nieuwe dataparser voor FLV streams, en awesomeheid in het algemeen --- util/MP4/box.cpp | 6 ++- util/ddv_socket.cpp | 1 + util/flv_data.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++ util/http_parser.cpp | 38 ++++++++++++++++++- 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 util/flv_data.cpp diff --git a/util/MP4/box.cpp b/util/MP4/box.cpp index 2e948bf6..e4c221cf 100644 --- a/util/MP4/box.cpp +++ b/util/MP4/box.cpp @@ -42,10 +42,10 @@ class Box { BoxHeader GetHeader( ); void ResetPayload( ); void Parse( std::string PrintOffset = "" ); - private: - BoxHeader header; uint8_t * Payload; uint32_t PayloadSize; + private: + BoxHeader header; };//Box Class Box::Box() { @@ -385,6 +385,8 @@ void Box::Parse( std::string PrintOffset ) { std::cerr << PrintOffset << " DiscontinuityIndicator: " << (int)FragmentRunEntryTable[i].DiscontinuityIndicator << "\n"; } } + } else if ( header.BoxType == 0x6D646174 ) { + std::cerr << "mdat box containing " << PayloadSize << " bytes of payload" << std::endl; } else { std::cerr << "BoxType '" << (char)(header.BoxType >> 24) diff --git a/util/ddv_socket.cpp b/util/ddv_socket.cpp index 0f26d36c..b1bd03e0 100644 --- a/util/ddv_socket.cpp +++ b/util/ddv_socket.cpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/util/flv_data.cpp b/util/flv_data.cpp new file mode 100644 index 00000000..28a18da4 --- /dev/null +++ b/util/flv_data.cpp @@ -0,0 +1,90 @@ +#include //for read() +#include + +struct FLV_Pack { + int len; + int buf; + bool isKeyframe; + char * data; +};//FLV_Pack + +char FLVHeader[13]; +bool All_Hell_Broke_Loose = false; + +//checks FLV Header for correctness +//returns true if everything is alright, false otherwise +bool FLV_Checkheader(char * header){ + if (header[0] != 'F') return false; + if (header[1] != 'L') return false; + if (header[2] != 'V') return false; + if (header[8] != 0x09) return false; + if (header[9] != 0) return false; + if (header[10] != 0) return false; + if (header[11] != 0) return false; + if (header[12] != 0) return false; + return true; +}//FLV_Checkheader + +//returns true if header is an FLV header +bool FLV_Isheader(char * header){ + if (header[0] != 'F') return false; + if (header[1] != 'L') return false; + if (header[2] != 'V') return false; + return true; +}//FLV_Isheader + +bool ReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P){ + if (sofar >= count){return true;} + int r = 0; + if (P+(count-sofar) > S){r = S-P;}else{r = count-sofar;} + memcpy(buffer+sofar, D+P, r); + P += r; + sofar += r; + if (sofar >= count){return true;} + return false; +} + +//gets a packet, storing in given FLV_Pack pointer. +//will assign pointer if null +//resizes FLV_Pack data field bigger if data doesn't fit +// (does not auto-shrink for speed!) +bool FLV_GetPacket(FLV_Pack *& p, char * D, unsigned int S, unsigned int & P){ + static bool done = true; + static unsigned int sofar = 0; + if (!p){p = (FLV_Pack*)calloc(1, sizeof(FLV_Pack));} + if (p->buf < 15){p->data = (char*)realloc(p->data, 15000000); p->buf = 15000000;} + + if (done){ + //read a header + if (ReadUntil(p->data, 11, sofar, D, S, P)){ + //if its a correct FLV header, throw away and read tag header + if (FLV_Isheader(p->data)){ + if (ReadUntil(p->data, 13, sofar, D, S, P)){ + if (FLV_Checkheader(p->data)){ + sofar = 0; + memcpy(FLVHeader, p->data, 13); + }else{All_Hell_Broke_Loose = true;} + } + }else{ + //if a tag header, calculate length and read tag body + p->len = p->data[3] + 15; + p->len += (p->data[2] << 8); + p->len += (p->data[1] << 16); + //if (p->buf < p->len){p->data = (char*)realloc(p->data, p->len);p->buf = p->len;} + done = false; + } + } + }else{ + //read tag body + if (ReadUntil(p->data, p->len, sofar, D, S, P)){ + //calculate keyframeness, next time read header again, return true + p->isKeyframe = false; + if ((p->data[0] == 0x09) && (((p->data[11] & 0xf0) >> 4) == 1)){p->isKeyframe = true;} + done = true; + sofar = 0; + return true; + } + } + return false; +}//FLV_GetPacket + diff --git a/util/http_parser.cpp b/util/http_parser.cpp index 6f7d619f..ff32eda3 100644 --- a/util/http_parser.cpp +++ b/util/http_parser.cpp @@ -1,10 +1,14 @@ +#pragma once +#include "ddv_socket.cpp" #include #include +#include class HTTPReader{ public: HTTPReader(); bool ReadSocket(int CONN_fd); + bool ReadSocket(FILE * F); std::string GetHeader(std::string i); std::string GetVar(std::string i); void SetHeader(std::string i, std::string v); @@ -18,6 +22,8 @@ class HTTPReader{ void SendBodyPart(int conn, char * buffer, int len); void SendBodyPart(int conn, std::string bodypart); void Clean(); + bool CleanForNext(); + std::string body; std::string method; std::string url; std::string protocol; @@ -39,12 +45,26 @@ void HTTPReader::Clean(){ method = "GET"; url = "/"; protocol = "HTTP/1.1"; + body = ""; length = 0; HTTPbuffer = ""; headers.erase(headers.begin(), headers.end()); vars.erase(vars.begin(), vars.end()); } +bool HTTPReader::CleanForNext(){ + seenHeaders = false; + seenReq = false; + method = "GET"; + url = "/"; + protocol = "HTTP/1.1"; + body = ""; + length = 0; + headers.erase(headers.begin(), headers.end()); + vars.erase(vars.begin(), vars.end()); + return parse(); +} + std::string HTTPReader::BuildRequest(){ std::map::iterator it; std::string tmp = method+" "+url+" "+protocol+"\n"; @@ -129,6 +149,16 @@ bool HTTPReader::ReadSocket(int CONN_fd){ return false; }//HTTPReader::ReadSocket +bool HTTPReader::ReadSocket(FILE * F){ + //returned true als hele http packet gelezen is + int b = 1; + char buffer[500]; + while (b > 0){ + b = fread(buffer, 1, 500, F); + HTTPbuffer.append(buffer, b); + } + return false; +}//HTTPReader::ReadSocket bool HTTPReader::parse(){ size_t f; @@ -165,7 +195,13 @@ bool HTTPReader::parse(){ if (seenHeaders){ if (length > 0){ //TODO: POST variable parsing - return (HTTPbuffer.length() >= length); + if (HTTPbuffer.length() >= length){ + body = HTTPbuffer.substr(0, length); + HTTPbuffer.erase(0, length); + return true; + }else{ + return false; + } }else{ return true; } From 10ab707e8ade3ecea6c139b02cd720ba513fae1e Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 21 Mar 2011 00:44:44 +0100 Subject: [PATCH 2/5] oeps. --- util/MP4/box.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/util/MP4/box.cpp b/util/MP4/box.cpp index 1e5a5424..46412680 100644 --- a/util/MP4/box.cpp +++ b/util/MP4/box.cpp @@ -46,8 +46,6 @@ class Box { uint8_t * Payload; BoxHeader header; uint32_t PayloadSize; - private: - BoxHeader header; };//Box Class Box::Box() { @@ -177,7 +175,6 @@ void Box::ResetPayload( ) { Payload = NULL; } } -<<<<<<< HEAD void Box::Parse( std::string PrintOffset ) { if( header.BoxType == 0x61627374 ) { @@ -399,5 +396,4 @@ void Box::Parse( std::string PrintOffset ) { << "' not yet implemented!\n"; } } -======= ->>>>>>> 7520f5799f3da3c1a89a28fd4d62358b0028d8d2 + From c2552e88fd355c2e081ffa2cf34b5630103ae86e Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 21 Mar 2011 00:57:48 +0100 Subject: [PATCH 3/5] Random edits --- util/http_parser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/http_parser.cpp b/util/http_parser.cpp index ff32eda3..36557fbd 100644 --- a/util/http_parser.cpp +++ b/util/http_parser.cpp @@ -178,7 +178,7 @@ bool HTTPReader::parse(){ if (f != std::string::npos){url = tmpA.substr(0, f); tmpA.erase(0, f+1);} f = tmpA.find(' '); if (f != std::string::npos){protocol = tmpA.substr(0, f); tmpA.erase(0, f+1);} - //TODO: GET variable parsing + //TODO: GET variable parsing? }else{ if (tmpA.size() == 0){ seenHeaders = true; @@ -194,7 +194,7 @@ bool HTTPReader::parse(){ } if (seenHeaders){ if (length > 0){ - //TODO: POST variable parsing + //TODO: POST variable parsing? if (HTTPbuffer.length() >= length){ body = HTTPbuffer.substr(0, length); HTTPbuffer.erase(0, length); From 352a1f00e176f34d1ba4973f9ae6cc3a7116afc6 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 21 Mar 2011 01:12:48 +0100 Subject: [PATCH 4/5] Back to the future 2 --- util/MP4/interface.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util/MP4/interface.cpp b/util/MP4/interface.cpp index 5175acbe..111f9b33 100644 --- a/util/MP4/interface.cpp +++ b/util/MP4/interface.cpp @@ -547,7 +547,7 @@ std::string Interface::GenerateLiveBootstrap( uint32_t CurMediaTime ) { afrt->SetUpdate(false); afrt->SetTimeScale( 1000 ); afrt->AddQualityEntry( "" ); - afrt->AddFragmentRunEntry( 1, 596458 , 4000 ); + afrt->AddFragmentRunEntry( 1, 1 , 4000 ); afrt->WriteContent( ); //SetUpASRT @@ -562,9 +562,9 @@ std::string Interface::GenerateLiveBootstrap( uint32_t CurMediaTime ) { abst->SetLive( true ); abst->SetUpdate( false ); abst->SetTimeScale( 1000 ); - abst->SetMediaTime( 596458 ); + abst->SetMediaTime( CurMediaTime ); abst->SetSMPTE( 0 ); - abst->SetMovieIdentifier( "" ); + abst->SetMovieIdentifier( "fifa" ); abst->SetDRM( "" ); abst->SetMetaData( "" ); abst->AddServerEntry( "" ); From e69ef556177d8eca3097cfc593779c67c4ce4705 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 21 Mar 2011 01:15:54 +0100 Subject: [PATCH 5/5] En toen wist ik het ook niet meer --- util/MP4/interface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/MP4/interface.cpp b/util/MP4/interface.cpp index 111f9b33..d6331389 100644 --- a/util/MP4/interface.cpp +++ b/util/MP4/interface.cpp @@ -547,7 +547,7 @@ std::string Interface::GenerateLiveBootstrap( uint32_t CurMediaTime ) { afrt->SetUpdate(false); afrt->SetTimeScale( 1000 ); afrt->AddQualityEntry( "" ); - afrt->AddFragmentRunEntry( 1, 1 , 4000 ); + afrt->AddFragmentRunEntry( 1, 4000, 4000 ); afrt->WriteContent( ); //SetUpASRT