diff --git a/src/output/output.cpp b/src/output/output.cpp index 469b3393..65f0fd27 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -2130,6 +2130,7 @@ namespace Mist{ if (nextKeyPage != INVALID_KEY_NUM && nextKeyPage != currentPage[nxt.tid]){ // If so, the next key is our next packet nextTime = keys.getTime(thisKey + 1); + userSelect[nxt.tid].setKeyNum(thisKey + 1); //If the next packet should've been before the current packet, something is wrong. Abort, abort! if (nextTime < nxt.time){ @@ -2205,7 +2206,7 @@ namespace Mist{ //Update keynum only when the second flips over in the timestamp //We do this because DTSC::Keys is pretty CPU-heavy - if (nxt.time / 1000 < nextTime/1000){ + if (nxt.time / 5000 < nextTime/5000){ uint32_t thisKey = M.getKeyNumForTime(nxt.tid, nxt.time); userSelect[nxt.tid].setKeyNum(thisKey); } @@ -2398,7 +2399,6 @@ namespace Mist{ return false; } close(outFile); - realTime = 0; return true; } diff --git a/src/output/output_mp4.cpp b/src/output/output_mp4.cpp index 5728dfe8..7c3561c1 100644 --- a/src/output/output_mp4.cpp +++ b/src/output/output_mp4.cpp @@ -162,7 +162,7 @@ namespace Mist{ return retVal * 1.1; } - uint64_t OutMP4::mp4moofSize(uint64_t startFragmentTime, uint64_t endFragmentTime, uint64_t &mdatSize) const{ + uint64_t OutMP4::mp4moofSize(uint64_t startFragmentTime, uint64_t endFragmentTime, uint64_t &mdatSize, std::map & keysCache) const{ size_t totalCount = 0; size_t totalSize = 8; uint64_t tmpRes = 8; // moof boxsize @@ -179,11 +179,11 @@ namespace Mist{ subIt != userSelect.end(); subIt++){ tmpRes += 8 + 20 + 20; // TRAF + TFHD + TFDT Box - DTSC::Keys keys = M.getKeys(subIt->first); + DTSC::Keys & keys = *keysCache.at(subIt->first); DTSC::Parts parts(M.parts(subIt->first)); - uint32_t startKey = M.getKeyIndexForTime(subIt->first, startFragmentTime); - uint32_t endKey = M.getKeyIndexForTime(subIt->first, endFragmentTime) + 1; + uint32_t startKey = keys.getIndexForTime(startFragmentTime); + uint32_t endKey = keys.getIndexForTime(endFragmentTime) + 1; size_t thisCount = 0; @@ -926,14 +926,17 @@ namespace Mist{ endFragmentTime = M.getTimeForFragmentIndex(mainTrack, M.getFragmentIndexForTime(mainTrack, startFragmentTime) + 1); } + + std::map keysCache; for (std::map::const_iterator subIt = userSelect.begin(); subIt != userSelect.end(); subIt++){ - DTSC::Keys keys = M.getKeys(subIt->first); + keysCache[subIt->first] = new DTSC::Keys(M.getKeys(subIt->first)); + DTSC::Keys & keys = *keysCache[subIt->first]; DTSC::Parts parts(M.parts(subIt->first)); - uint32_t startKey = M.getKeyIndexForTime(subIt->first, startFragmentTime); - uint32_t endKey = M.getKeyIndexForTime(subIt->first, endFragmentTime) + 1; + uint32_t startKey = keys.getIndexForTime(startFragmentTime); + uint32_t endKey = keys.getIndexForTime(endFragmentTime) + 1; for (size_t k = startKey; k < endKey; k++){ @@ -954,7 +957,7 @@ namespace Mist{ temp.index = p; trunOrder.insert(temp); - uint64_t keyTime = M.getTimeForKeyIndex(subIt->first, M.getKeyIndexForTime(subIt->first, timeStamp)); + uint64_t keyTime = M.getTimeForKeyIndex(subIt->first, keys.getIndexForTime(timeStamp)); if (keyTime == timeStamp){ keyParts.insert(p); } @@ -977,7 +980,7 @@ namespace Mist{ } } - uint64_t relativeOffset = mp4moofSize(startFragmentTime, endFragmentTime, mdatSize) + 8; + uint64_t relativeOffset = mp4moofSize(startFragmentTime, endFragmentTime, mdatSize, keysCache) + 8; // We need to loop over each part and fill a new set, because editing byteOffest might edit // relative order, and invalidates the iterator. @@ -995,6 +998,10 @@ namespace Mist{ trunOrder.clear(); // erase the trunOrder set, to keep memory usage down for (std::deque::iterator it = sortedTracks.begin(); it != sortedTracks.end(); ++it){ + //Wipe keys cache, no longer needed + delete keysCache[*it]; + keysCache.erase(*it); + size_t tid = *it; DTSC::Parts parts(M.parts(*it)); diff --git a/src/output/output_mp4.h b/src/output/output_mp4.h index ebb3b753..1f981baa 100644 --- a/src/output/output_mp4.h +++ b/src/output/output_mp4.h @@ -94,7 +94,7 @@ namespace Mist{ uint64_t mp4HeaderSize(uint64_t &fileSize, int fragmented = 0) const; bool mp4Header(Util::ResizeablePointer & headOut, uint64_t &size, int fragmented = 0); - uint64_t mp4moofSize(uint64_t startFragmentTime, uint64_t endFragmentTime, uint64_t &mdatSize) const; + uint64_t mp4moofSize(uint64_t startFragmentTime, uint64_t endFragmentTime, uint64_t &mdatSize, std::map & keysCache) const; virtual void sendFragmentHeaderTime(uint64_t startFragmentTime, uint64_t endFragmentTime); // this builds the moof box for fragmented MP4