From 4b9c8cee7418416f1a97d15d64349ad7e4503743 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 Jul 2015 12:37:09 +0200 Subject: [PATCH] Simplified and optimized FLV input and tag to DTSC conversion. --- lib/flv_tag.cpp | 32 ++++++++++++++++++++++++++++++++ lib/flv_tag.h | 3 +++ src/input/input_flv.cpp | 21 +++++++++------------ 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/lib/flv_tag.cpp b/lib/flv_tag.cpp index 5dcc9212..0dc418ca 100644 --- a/lib/flv_tag.cpp +++ b/lib/flv_tag.cpp @@ -1032,6 +1032,38 @@ bool FLV::Tag::FileLoader(FILE * f) { return false; } //FLV_GetPacket +/// Returns 1 for video, 2 for audio, 3 for meta, 0 otherwise. +unsigned int FLV::Tag::getTrackID(){ + switch (data[0]){ + case 0x08: return 2;//audio track + case 0x09: return 1;//video track + case 0x12: return 3;//meta track + default: return 0; + } +} + +/// Returns a pointer to the raw media data for this packet. +char * FLV::Tag::getData(){ + if (data[0] == 0x08 && (data[11] & 0xF0) == 0xA0) { + return data+13; + } + if (data[0] == 0x09 && (data[11] & 0x0F) == 7) { + return data+16; + } + return data+12; +} + +/// Returns the length of the raw media data for this packet. +unsigned int FLV::Tag::getDataLen(){ + if (data[0] == 0x08 && (data[11] & 0xF0) == 0xA0) { + return len - 17; + } + if (data[0] == 0x09 && (data[11] & 0x0F) == 7) { + return len - 20; + } + return len - 16; +} + JSON::Value FLV::Tag::toJSON(DTSC::Meta & metadata, AMF::Object & amf_storage, unsigned int reTrack) { JSON::Value pack_out; // Storage for outgoing metadata. diff --git a/lib/flv_tag.h b/lib/flv_tag.h index ac322091..15837753 100644 --- a/lib/flv_tag.h +++ b/lib/flv_tag.h @@ -56,6 +56,9 @@ namespace FLV { JSON::Value toJSON(DTSC::Meta & metadata, AMF::Object & amf_storage, unsigned int reTrack = 0); bool MemLoader(char * D, unsigned int S, unsigned int & P); bool FileLoader(FILE * f); + unsigned int getTrackID(); + char * getData(); + unsigned int getDataLen(); protected: int buf; ///< Maximum length of buffer space. bool done; ///< Body reading done? diff --git a/src/input/input_flv.cpp b/src/input/input_flv.cpp index 23bea01f..82e0697a 100644 --- a/src/input/input_flv.cpp +++ b/src/input/input_flv.cpp @@ -89,29 +89,26 @@ namespace Mist { } void inputFLV::getNext(bool smart) { - static JSON::Value thisPack; - static AMF::Object amf_storage; - thisPack.null(); long long int lastBytePos = ftell(inFile); FLV::Tag tmpTag; while (!feof(inFile) && !FLV::Parse_Error){ if (tmpTag.FileLoader(inFile)){ - thisPack = tmpTag.toJSON(myMeta, amf_storage); - thisPack["bpos"] = lastBytePos; - if ( !selectedTracks.count(thisPack["trackid"].asInt())){ - getNext(); + if ( !selectedTracks.count(tmpTag.getTrackID())){ + return getNext(); } break; } } - if (FLV::Parse_Error){ - FAIL_MSG("FLV error: %s", FLV::Error_Str.c_str()); - thisPack.null(); + if (feof(inFile)){ thisPacket.null(); return; } - std::string tmpStr = thisPack.toNetPacked(); - thisPacket.reInit(tmpStr.data(), tmpStr.size()); + if (FLV::Parse_Error){ + FAIL_MSG("FLV error: %s", FLV::Error_Str.c_str()); + thisPacket.null(); + return; + } + thisPacket.genericFill(tmpTag.tagTime(), tmpTag.offset(), tmpTag.getTrackID(), tmpTag.getData(), tmpTag.getDataLen(), lastBytePos, tmpTag.isKeyframe); //init packet from tmpTags data } void inputFLV::seek(int seekTime) {