Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "StreamBuffer.h" |
michael@0 | 7 | #include "prlog.h" |
michael@0 | 8 | #include <algorithm> |
michael@0 | 9 | |
michael@0 | 10 | namespace mozilla { |
michael@0 | 11 | |
michael@0 | 12 | #ifdef PR_LOGGING |
michael@0 | 13 | extern PRLogModuleInfo* gMediaStreamGraphLog; |
michael@0 | 14 | #define STREAM_LOG(type, msg) PR_LOG(gMediaStreamGraphLog, type, msg) |
michael@0 | 15 | #else |
michael@0 | 16 | #define STREAM_LOG(type, msg) |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | #ifdef DEBUG |
michael@0 | 20 | void |
michael@0 | 21 | StreamBuffer::DumpTrackInfo() const |
michael@0 | 22 | { |
michael@0 | 23 | STREAM_LOG(PR_LOG_ALWAYS, ("DumpTracks: mTracksKnownTime %lld", mTracksKnownTime)); |
michael@0 | 24 | for (uint32_t i = 0; i < mTracks.Length(); ++i) { |
michael@0 | 25 | Track* track = mTracks[i]; |
michael@0 | 26 | if (track->IsEnded()) { |
michael@0 | 27 | STREAM_LOG(PR_LOG_ALWAYS, ("Track[%d] %d: ended", i, track->GetID())); |
michael@0 | 28 | } else { |
michael@0 | 29 | STREAM_LOG(PR_LOG_ALWAYS, ("Track[%d] %d: %lld", i, track->GetID(), |
michael@0 | 30 | track->GetEndTimeRoundDown())); |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | StreamTime |
michael@0 | 37 | StreamBuffer::GetEnd() const |
michael@0 | 38 | { |
michael@0 | 39 | StreamTime t = mTracksKnownTime; |
michael@0 | 40 | for (uint32_t i = 0; i < mTracks.Length(); ++i) { |
michael@0 | 41 | Track* track = mTracks[i]; |
michael@0 | 42 | if (!track->IsEnded()) { |
michael@0 | 43 | t = std::min(t, track->GetEndTimeRoundDown()); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | return t; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | StreamTime |
michael@0 | 50 | StreamBuffer::GetAllTracksEnd() const |
michael@0 | 51 | { |
michael@0 | 52 | if (mTracksKnownTime < STREAM_TIME_MAX) { |
michael@0 | 53 | // A track might be added. |
michael@0 | 54 | return STREAM_TIME_MAX; |
michael@0 | 55 | } |
michael@0 | 56 | StreamTime t = 0; |
michael@0 | 57 | for (uint32_t i = 0; i < mTracks.Length(); ++i) { |
michael@0 | 58 | Track* track = mTracks[i]; |
michael@0 | 59 | if (!track->IsEnded()) { |
michael@0 | 60 | return STREAM_TIME_MAX; |
michael@0 | 61 | } |
michael@0 | 62 | t = std::max(t, track->GetEndTimeRoundDown()); |
michael@0 | 63 | } |
michael@0 | 64 | return t; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | StreamBuffer::Track* |
michael@0 | 68 | StreamBuffer::FindTrack(TrackID aID) |
michael@0 | 69 | { |
michael@0 | 70 | if (aID == TRACK_NONE) |
michael@0 | 71 | return nullptr; |
michael@0 | 72 | for (uint32_t i = 0; i < mTracks.Length(); ++i) { |
michael@0 | 73 | Track* track = mTracks[i]; |
michael@0 | 74 | if (track->GetID() == aID) { |
michael@0 | 75 | return track; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | return nullptr; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | void |
michael@0 | 82 | StreamBuffer::ForgetUpTo(StreamTime aTime) |
michael@0 | 83 | { |
michael@0 | 84 | // Round to nearest 50ms so we don't spend too much time pruning segments. |
michael@0 | 85 | const MediaTime roundTo = MillisecondsToMediaTime(50); |
michael@0 | 86 | StreamTime forget = (aTime/roundTo)*roundTo; |
michael@0 | 87 | if (forget <= mForgottenTime) { |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | mForgottenTime = forget; |
michael@0 | 91 | |
michael@0 | 92 | for (uint32_t i = 0; i < mTracks.Length(); ++i) { |
michael@0 | 93 | Track* track = mTracks[i]; |
michael@0 | 94 | if (track->IsEnded() && track->GetEndTimeRoundDown() <= forget) { |
michael@0 | 95 | mTracks.RemoveElementAt(i); |
michael@0 | 96 | --i; |
michael@0 | 97 | continue; |
michael@0 | 98 | } |
michael@0 | 99 | TrackTicks forgetTo = std::min(track->GetEnd() - 1, track->TimeToTicksRoundDown(forget)); |
michael@0 | 100 | track->ForgetUpTo(forgetTo); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | } |