From db1ad97c36a5b87f449859ddd4b5f3e98c46dd9d Mon Sep 17 00:00:00 2001 From: Thulinma Date: Sat, 1 Jul 2017 13:45:35 +0200 Subject: [PATCH 1/4] Added Util::ResizeablePointer class --- lib/util.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ lib/util.h | 18 ++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/util.cpp b/lib/util.cpp index a69e6afd..f6ea07be 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -12,6 +12,7 @@ #if defined(_WIN32) #include // _mkdir #endif +#include #define RECORD_POINTER p + getOffset() + (getRecordPosition(recordNo) * getRSize()) + fd.offset #define RAXHDR_FIELDOFFSET p[1] @@ -148,6 +149,46 @@ namespace Util{ return fseeko(stream, offset, whence); } + ResizeablePointer::ResizeablePointer(){ + currSize = 0; + ptr = 0; + maxSize = 0; + } + + ResizeablePointer::~ResizeablePointer(){ + if (ptr){free(ptr);} + currSize = 0; + ptr = 0; + maxSize = 0; + } + + bool ResizeablePointer::assign(void * p, uint32_t l){ + if (!allocate(l)){return false;} + memcpy(ptr, p, l); + currSize = l; + return true; + } + + bool ResizeablePointer::append(void * p, uint32_t l){ + if (!allocate(l+currSize)){return false;} + memcpy(((char*)ptr)+currSize, p, l); + currSize += l; + return true; + } + + bool ResizeablePointer::allocate(uint32_t l){ + if (l > maxSize){ + void *tmp = realloc(ptr, l); + if (!tmp){ + FAIL_MSG("Could not allocate %lu bytes of memory", l); + return false; + } + ptr = tmp; + maxSize = l; + } + return true; + } + /// If waitReady is true (default), waits for isReady() to return true in 50ms sleep increments. RelAccX::RelAccX(char *data, bool waitReady){ if (!data){ diff --git a/lib/util.h b/lib/util.h index fb3019de..b62e7d18 100644 --- a/lib/util.h +++ b/lib/util.h @@ -12,6 +12,24 @@ namespace Util{ uint64_t ftell(FILE *stream); uint64_t fseek(FILE *stream, uint64_t offset, int whence); + /// Helper class that maintains a resizeable pointer and will free it upon deletion of the class. + class ResizeablePointer{ + public: + ResizeablePointer(); + ~ResizeablePointer(); + inline uint32_t& size(){return currSize;} + bool assign(void * p, uint32_t l); + bool append(void * p, uint32_t l); + bool allocate(uint32_t l); + inline operator char*(){return (char*)ptr;} + inline operator void*(){return ptr;} + private: + void * ptr; + uint32_t currSize; + uint32_t maxSize; + + }; + /// Holds type, size and offset for RelAccX class internal data fields. class RelAccXFieldData{ public: From 05d1fa5c8d26e99c7c955e8d9aa4f45a73987115 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Sat, 8 Jul 2017 14:40:53 +0200 Subject: [PATCH 2/4] Updated RTMP output to use Util::ResizeablePointer --- src/output/output_rtmp.cpp | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/output/output_rtmp.cpp b/src/output/output_rtmp.cpp index 46144d63..85641a3b 100644 --- a/src/output/output_rtmp.cpp +++ b/src/output/output_rtmp.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -181,8 +182,7 @@ namespace Mist { 0, 0, 0, 0}; //bytes 12-15 = extended timestamp char dataheader[] ={0, 0, 0, 0, 0}; unsigned int dheader_len = 1; - static char * swappyPointer = 0; - static uint32_t swappySize = 0; + static Util::ResizeablePointer swappy; char * tmpData = 0;//pointer to raw media data unsigned int data_len = 0;//length of processed media data thisPacket.getString("data", tmpData, data_len); @@ -234,21 +234,12 @@ namespace Mist { dataheader[0] |= 0x10; } if (track.codec == "PCM"){ - if (track.size == 16){ - if (swappySize < data_len){ - char * tmp = (char*)realloc(swappyPointer, data_len); - if (!tmp){ - FAIL_MSG("Could not allocate data for PCM endianness swap!"); - return; - } - swappyPointer = tmp; - swappySize = data_len; - } + if (track.size == 16 && swappy.allocate(data_len)){ for (uint32_t i = 0; i < data_len; i+=2){ - swappyPointer[i] = tmpData[i+1]; - swappyPointer[i+1] = tmpData[i]; + swappy[i] = tmpData[i+1]; + swappy[i+1] = tmpData[i]; } - tmpData = swappyPointer; + tmpData = swappy; } dataheader[0] |= 0x30; } From 3eb69ff2c7244046dacf76eb026dd11b34fced8b Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 28 Jul 2017 18:38:54 +0200 Subject: [PATCH 3/4] Changed bufferLivePacket and related functions to have const DTSC::Packet references --- lib/dtsc.h | 2 +- lib/dtscmeta.cpp | 2 +- src/io.cpp | 10 +++++----- src/io.h | 10 +++++----- src/output/output.cpp | 2 +- src/output/output.h | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/dtsc.h b/lib/dtsc.h index b1404431..d557c34b 100644 --- a/lib/dtsc.h +++ b/lib/dtsc.h @@ -324,7 +324,7 @@ namespace DTSC { return vod || live; } void reinit(const DTSC::Packet & source); - void update(DTSC::Packet & pack, unsigned long segment_size = 5000); + void update(const DTSC::Packet & pack, unsigned long segment_size = 5000); void updatePosOverride(DTSC::Packet & pack, uint64_t bpos); void update(JSON::Value & pack, unsigned long segment_size = 5000); void update(long long packTime, long long packOffset, long long packTrack, long long packDataSize, uint64_t packBytePos, bool isKeyframe, long long packSendSize = 0, unsigned long segment_size = 5000); diff --git a/lib/dtscmeta.cpp b/lib/dtscmeta.cpp index 9aa90975..7cfe2535 100644 --- a/lib/dtscmeta.cpp +++ b/lib/dtscmeta.cpp @@ -1438,7 +1438,7 @@ namespace DTSC { } ///\brief Updates a meta object given a DTSC::Packet - void Meta::update(DTSC::Packet & pack, unsigned long segment_size) { + void Meta::update(const DTSC::Packet & pack, unsigned long segment_size) { char * data; unsigned int dataLen; pack.getString("data", data, dataLen); diff --git a/src/io.cpp b/src/io.cpp index cf19c70f..d382532e 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -253,11 +253,11 @@ namespace Mist { ///Buffers the next packet on the currently opened page ///\param pack The packet to buffer - void InOutBase::bufferNext(DTSC::Packet & pack) { + void InOutBase::bufferNext(const DTSC::Packet & pack) { nProxy.bufferNext(pack, myMeta); } - void negotiationProxy::bufferNext(DTSC::Packet & pack, DTSC::Meta & myMeta) { + void negotiationProxy::bufferNext(const DTSC::Packet & pack, DTSC::Meta & myMeta) { static bool multiWrong = false; //Save the trackid of the track for easier access unsigned long tid = pack.getTrackId(); @@ -388,11 +388,11 @@ namespace Mist { /// ///Initiates/continues negotiation with the buffer as well ///\param packet The packet to buffer - void InOutBase::bufferLivePacket(DTSC::Packet & packet){ + void InOutBase::bufferLivePacket(const DTSC::Packet & packet){ nProxy.bufferLivePacket(packet, myMeta); } - void negotiationProxy::bufferLivePacket(DTSC::Packet & packet, DTSC::Meta & myMeta){ + void negotiationProxy::bufferLivePacket(const DTSC::Packet & packet, DTSC::Meta & myMeta){ myMeta.vod = false; myMeta.live = true; //Store the trackid for easier access @@ -426,7 +426,7 @@ namespace Mist { } } - void negotiationProxy::bufferSinglePacket(DTSC::Packet & packet, DTSC::Meta & myMeta){ + void negotiationProxy::bufferSinglePacket(const DTSC::Packet & packet, DTSC::Meta & myMeta){ //Store the trackid for easier access unsigned long tid = packet.getTrackId(); //This update needs to happen whether the track is accepted or not. diff --git a/src/io.h b/src/io.h index 3e85b902..c95e4d90 100644 --- a/src/io.h +++ b/src/io.h @@ -30,10 +30,10 @@ namespace Mist { negotiationProxy(); void clear(); bool bufferStart(unsigned long tid, unsigned long pageNumber, DTSC::Meta & myMeta); - void bufferNext(DTSC::Packet & pack, DTSC::Meta & myMeta); + void bufferNext(const DTSC::Packet & pack, DTSC::Meta & myMeta); void bufferFinalize(unsigned long tid, DTSC::Meta &myMeta); - void bufferLivePacket(DTSC::Packet & packet, DTSC::Meta & myMeta); - void bufferSinglePacket(DTSC::Packet & packet, DTSC::Meta & myMeta); + void bufferLivePacket(const DTSC::Packet & packet, DTSC::Meta & myMeta); + void bufferSinglePacket(const DTSC::Packet & packet, DTSC::Meta & myMeta); bool isBuffered(unsigned long tid, unsigned long keyNum); unsigned long bufferedOnPage(unsigned long tid, unsigned long keyNum); @@ -66,10 +66,10 @@ namespace Mist { public: void initiateMeta(); bool bufferStart(unsigned long tid, unsigned long pageNumber); - void bufferNext(DTSC::Packet & pack); + void bufferNext(const DTSC::Packet & pack); void bufferFinalize(unsigned long tid); void bufferRemove(unsigned long tid, unsigned long pageNumber); - virtual void bufferLivePacket(DTSC::Packet & packet); + virtual void bufferLivePacket(const DTSC::Packet & packet); protected: void continueNegotiate(unsigned long tid, bool quickNegotiate = false); void continueNegotiate(); diff --git a/src/output/output.cpp b/src/output/output.cpp index b6e06167..da6e444b 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -39,7 +39,7 @@ namespace Mist{ cfg->addOption("noinput", option); } - void Output::bufferLivePacket(DTSC::Packet & packet){ + void Output::bufferLivePacket(const DTSC::Packet & packet){ if (!pushIsOngoing){ waitForStreamPushReady(); } diff --git a/src/output/output.h b/src/output/output.h index 14ce5258..6412322e 100644 --- a/src/output/output.h +++ b/src/output/output.h @@ -119,7 +119,7 @@ namespace Mist { bool allowPush(const std::string & passwd); void waitForStreamPushReady(); bool pushIsOngoing; - void bufferLivePacket(DTSC::Packet & packet); + void bufferLivePacket(const DTSC::Packet & packet); }; } From c895a6b7e273fd5d9ca3034b326f0f6b01105f27 Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Fri, 28 Jul 2017 12:00:12 +0200 Subject: [PATCH 4/4] Added codec to "Done buffering" message --- src/input/input.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/input.cpp b/src/input/input.cpp index 2a2cedc7..9d007e88 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -628,7 +628,7 @@ namespace Mist { } bufferFinalize(track); bufferTimer = Util::bootMS() - bufferTimer; - DEBUG_MSG(DLVL_DEVEL, "Done buffering page %d (%llu packets, %llu bytes) for track %d in %llums", keyNum, packCounter, byteCounter, track, bufferTimer); + DEBUG_MSG(DLVL_DEVEL, "Done buffering page %d (%llu packets, %llu bytes) for track %d (%s) in %llums", keyNum, packCounter, byteCounter, track, myMeta.tracks[track].codec.c_str(), bufferTimer); pageCounter[track][keyNum] = 15; return true; }