From db2e630436389ea25a257f6e82530c825ca1b77f Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Wed, 10 Jul 2013 11:27:26 +0200 Subject: [PATCH] HLS working, dunno why, but it works. --- lib/dtsc.cpp | 29 +++++++++++++++++++++++++++-- lib/dtsc.h | 1 + lib/flv_tag.cpp | 2 ++ lib/ts_packet.cpp | 32 +++++++++++++++++++------------- lib/ts_packet.h | 2 +- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/lib/dtsc.cpp b/lib/dtsc.cpp index f9b3da3b..37ce52c7 100644 --- a/lib/dtsc.cpp +++ b/lib/dtsc.cpp @@ -81,6 +81,7 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ if (version == 2){ newPack = JSON::fromDTMI2((unsigned char*)buffer.c_str() + 8, len, i); } + buffer.erase(0, len + 8); addPacket(newPack); syncing = false; return true; @@ -173,6 +174,7 @@ bool DTSC::Stream::parsePacket(Socket::Buffer & buffer){ } void DTSC::Stream::addPacket(JSON::Value & newPack){ + long long unsigned int now = Util::getMS(); livePos newPos; newPos.trackID = newPack["trackid"].asInt(); newPos.seekTime = newPack["time"].asInt(); @@ -614,7 +616,6 @@ void DTSC::File::seekNext(){ return; } clearerr(F); - seek_time(currentPositions.begin()->seekTime + 1, currentPositions.begin()->trackID); fseek(F,currentPositions.begin()->seekPos, SEEK_SET); currentPositions.erase(currentPositions.begin()); lastreadpos = ftell(F); @@ -668,10 +669,33 @@ void DTSC::File::seekNext(){ }else{ jsonbuffer = JSON::fromDTMI(strbuffer); } + int tempLoc = getBytePos(); + char newHeader[20]; + if (fread((void*)newHeader, 20, 1, F) == 1){ + if (memcmp(newHeader, DTSC::Magic_Packet2, 4) == 0){ + seekPos tmpPos; + tmpPos.seekPos = tempLoc; + tmpPos.trackID = ntohl(((int*)newHeader)[2]); + if (selectedTracks.find(tmpPos.trackID) != selectedTracks.end()){ + tmpPos.seekTime = ((long long unsigned int)ntohl(((int*)newHeader)[3])) << 32; + tmpPos.seekTime += ntohl(((int*)newHeader)[4]); + }else{ + for (JSON::ArrIter it = getTrackById(jsonbuffer["trackid"].asInt())["keys"].ArrBegin(); it != getTrackById(jsonbuffer["trackid"].asInt())["keys"].ArrEnd(); it++){ + if ((*it)["time"].asInt() > jsonbuffer["time"].asInt()){ + tmpPos.seekTime = (*it)["time"].asInt(); + tmpPos.seekPos = (*it)["bpos"].asInt(); + break; + } + } + } + currentPositions.insert(tmpPos); + } + } } void DTSC::File::parseNext(){ + lastreadpos = ftell(F); if (fread(buffer, 4, 1, F) != 1){ if (feof(F)){ #if DEBUG >= 4 @@ -769,6 +793,7 @@ bool DTSC::File::seek_time(int ms, int trackNo){ } bool foundPacket = false; while ( !foundPacket){ + lastreadpos = ftell(F); if (reachedEOF()){ return false; } @@ -787,8 +812,8 @@ bool DTSC::File::seek_time(int ms, int trackNo){ //get timestamp of packet, if too large, break, if not, skip size bytes. long long unsigned int myTime = ((long long unsigned int)ntohl(((int*)header)[3]) << 32); myTime += ntohl(((int*)header)[4]); + tmpPos.seekTime = myTime; if (myTime >= ms){ - tmpPos.seekTime = myTime; foundPacket = true; }else{ tmpPos.seekPos += 8 + packSize; diff --git a/lib/dtsc.h b/lib/dtsc.h index bf38e4f2..dc4bfbfb 100644 --- a/lib/dtsc.h +++ b/lib/dtsc.h @@ -11,6 +11,7 @@ #include //for FILE #include "json.h" #include "socket.h" +#include "timing.h" /// Holds all DDVTECH Stream Container classes and parsers. ///length (int, length in seconds, if available) diff --git a/lib/flv_tag.cpp b/lib/flv_tag.cpp index 8ea65b70..d093459a 100644 --- a/lib/flv_tag.cpp +++ b/lib/flv_tag.cpp @@ -1071,6 +1071,7 @@ JSON::Value FLV::Tag::toJSON(JSON::Value & metadata){ } pack_out["datatype"] = "audio"; pack_out["time"] = tagTime(); + pack_out["trackid"] = 2; metadata["tracks"]["track2"]["trackid"] = 2; metadata["tracks"]["track2"]["type"] = "audio"; if ( !metadata["tracks"]["track2"].isMember("codec") || metadata["tracks"]["track2"]["codec"].asString() == "?" || metadata["tracks"]["track2"]["codec"].asString() == ""){ @@ -1145,6 +1146,7 @@ JSON::Value FLV::Tag::toJSON(JSON::Value & metadata){ metadata["tracks"]["track1"]["codec"] = getVideoCodec(); } pack_out["datatype"] = "video"; + pack_out["trackid"] = 1; switch (videodata & 0xF0){ case 0x10: pack_out["keyframe"] = 1; diff --git a/lib/ts_packet.cpp b/lib/ts_packet.cpp index 97a79364..07f15f05 100644 --- a/lib/ts_packet.cpp +++ b/lib/ts_packet.cpp @@ -1,6 +1,7 @@ /// \file ts_packet.cpp /// Holds all code for the TS namespace. +#include #include "ts_packet.h" #ifndef FILLER_DATA @@ -142,18 +143,23 @@ int TS::Packet::AdaptationFieldLen(){ } /// Prints a packet to stdout, for analyser purposes. -void TS::Packet::Print(){ - std::cout << "TS Packet: " << (strBuf[0] == 0x47) << "\n\tNewUnit: " << UnitStart() << "\n\tPID: " << PID() << "\n\tContinuity Counter: " - << ContinuityCounter() << "\n\tAdaption Field: " << AdaptationField() << "\n"; +std::string TS::Packet::toPrettyString(size_t indent){ + std::stringstream output; + output << std::string(indent,' ') << "TS Packet: " << (strBuf[0] == 0x47) << std::endl; + output << std::string(indent+2,' ') << "NewUnit: " << UnitStart() << std::endl; + output << std::string(indent+2,' ') << "PID: " << PID() << std::endl; + output << std::string(indent+2,' ') << "Continuity Counter: " << ContinuityCounter() << std::endl; + output << std::string(indent+2,' ') << "Adaption Field: " << AdaptationField() << std::endl; if (AdaptationField()){ - std::cout << "\t\tAdaption Field Length: " << AdaptationFieldLen() << "\n"; + output << std::string(indent+4,' ') << "Adaptation Length: " << AdaptationFieldLen() << std::endl;; if (AdaptationFieldLen()){ - std::cout << "\t\tRandom Access: " << RandomAccess() << "\n"; + output << std::string(indent+4,' ') << "Random Access: " << RandomAccess() << std::endl; } if (PCR() != -1){ - std::cout << "\t\tPCR: " << PCR() << "( " << (double)PCR() / 27000000 << " s )\n"; + output << std::string(indent+4,' ') << "PCR: " << PCR() << "( " << (double)PCR() / 27000000 << " s )" << std::endl; } } + return output.str(); } /// Gets whether a new unit starts in this TS::Packet. @@ -275,17 +281,17 @@ void TS::Packet::PESVideoLeadIn(unsigned int NewLen, long long unsigned int PTS) /// \param NewLen The length of this frame. /// \param PTS The timestamp of the frame. void TS::Packet::PESAudioLeadIn(unsigned int NewLen, uint64_t PTS){ - NewLen += 8; + NewLen += 5; strBuf += (char)0x00; //PacketStartCodePrefix strBuf += (char)0x00; //PacketStartCodePrefix (Cont) strBuf += (char)0x01; //PacketStartCodePrefix (Cont) strBuf += (char)0xc0; //StreamType Audio strBuf += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength strBuf += (char)(NewLen & 0x00FF); //PES PacketLength (Cont) - strBuf += (char)0x80; //Reserved + Flags + strBuf += (char)0x84; //Reserved + Flags strBuf += (char)0x80; //PTSOnlyFlag + Flags strBuf += (char)0x05; //PESHeaderDataLength - strBuf += (char)(0x20 + ((PTS & 0x1C0000000LL) >> 29) + 1); //PTS + strBuf += (char)(0x30 + ((PTS & 0x1C0000000LL) >> 29) + 1); //PTS strBuf += (char)((PTS & 0x03FC00000LL) >> 22); //PTS (Cont) strBuf += (char)(((PTS & 0x0003F8000LL) >> 14) + 1); //PTS (Cont) strBuf += (char)((PTS & 0x000007F80LL) >> 7); //PTS (Cont) @@ -321,12 +327,12 @@ void TS::Packet::PESVideoLeadIn(std::string & toSend, long long unsigned int PTS void TS::Packet::PESAudioLeadIn(std::string & toSend, long long unsigned int PTS){ std::string tmpStr; tmpStr.reserve(14); - unsigned int NewLen = toSend.size() + 8; + unsigned int NewLen = toSend.size() + 5; tmpStr.append("\000\000\001\300", 4); tmpStr += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength tmpStr += (char)(NewLen & 0x00FF); //PES PacketLength (Cont) - tmpStr.append("\200\200\005", 3); - tmpStr += (char)(0x20 + ((PTS & 0x1C0000000LL) >> 29) + 1); //PTS + tmpStr.append("\204\200\005", 3); + tmpStr += (char)(0x30 + ((PTS & 0x1C0000000LL) >> 29) + 1); //PTS tmpStr += (char)((PTS & 0x03FC00000LL) >> 22); //PTS (Cont) tmpStr += (char)(((PTS & 0x0003F8000LL) >> 14) + 1); //PTS (Cont) tmpStr += (char)((PTS & 0x000007F80LL) >> 7); //PTS (Cont) @@ -369,7 +375,7 @@ std::string & TS::Packet::getPESAudioLeadIn(unsigned int NewLen, long long unsig tmpStr.append("\000\000\001\300", 4); tmpStr += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength tmpStr += (char)(NewLen & 0x00FF); //PES PacketLength (Cont) - tmpStr.append("\200\200\005", 3); + tmpStr.append("\204\200\005", 3); tmpStr += (char)(0x20 + ((PTS & 0x1C0000000LL) >> 29) + 1); //PTS tmpStr += (char)((PTS & 0x03FC00000LL) >> 22); //PTS (Cont) tmpStr += (char)(((PTS & 0x0003F8000LL) >> 14) + 1); //PTS (Cont) diff --git a/lib/ts_packet.h b/lib/ts_packet.h index d289733d..5a1649b5 100644 --- a/lib/ts_packet.h +++ b/lib/ts_packet.h @@ -38,7 +38,7 @@ namespace TS { void RandomAccess(int NewVal); int BytesFree(); - void Print(); + std::string toPrettyString(size_t indent = 0); const char* ToString(); void PESVideoLeadIn(unsigned int NewLen, long long unsigned int PTS = 1); void PESAudioLeadIn(unsigned int NewLen, uint64_t PTS = 0);