2012-05-07 22:22:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-04-30 07:11:19 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "StreamBuffer.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-07 12:53:45 +04:00
|
|
|
extern PRLogModuleInfo* gMediaStreamGraphLog;
|
2015-05-21 23:22:04 +03:00
|
|
|
#define STREAM_LOG(type, msg) MOZ_LOG(gMediaStreamGraphLog, type, msg)
|
2013-03-07 12:53:45 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
StreamBuffer::DumpTrackInfo() const
|
|
|
|
{
|
2015-06-02 14:05:56 +03:00
|
|
|
STREAM_LOG(PR_LOG_ALWAYS, ("DumpTracks: mTracksKnownTime %lld", mTracksKnownTime));
|
2013-03-07 12:53:45 +04:00
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
Track* track = mTracks[i];
|
|
|
|
if (track->IsEnded()) {
|
2015-06-02 14:05:56 +03:00
|
|
|
STREAM_LOG(PR_LOG_ALWAYS, ("Track[%d] %d: ended", i, track->GetID()));
|
2013-03-07 12:53:45 +04:00
|
|
|
} else {
|
2015-06-02 14:05:56 +03:00
|
|
|
STREAM_LOG(PR_LOG_ALWAYS, ("Track[%d] %d: %lld", i, track->GetID(),
|
2014-09-18 09:13:13 +04:00
|
|
|
track->GetEnd()));
|
2013-03-07 12:53:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
StreamTime
|
|
|
|
StreamBuffer::GetEnd() const
|
|
|
|
{
|
|
|
|
StreamTime t = mTracksKnownTime;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
2012-04-30 07:11:19 +04:00
|
|
|
Track* track = mTracks[i];
|
|
|
|
if (!track->IsEnded()) {
|
2014-09-18 09:13:13 +04:00
|
|
|
t = std::min(t, track->GetEnd());
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-12-04 05:08:12 +04:00
|
|
|
StreamTime
|
|
|
|
StreamBuffer::GetAllTracksEnd() const
|
|
|
|
{
|
2014-05-19 00:26:54 +04:00
|
|
|
if (mTracksKnownTime < STREAM_TIME_MAX) {
|
|
|
|
// A track might be added.
|
|
|
|
return STREAM_TIME_MAX;
|
|
|
|
}
|
2013-12-04 05:08:12 +04:00
|
|
|
StreamTime t = 0;
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
Track* track = mTracks[i];
|
|
|
|
if (!track->IsEnded()) {
|
|
|
|
return STREAM_TIME_MAX;
|
|
|
|
}
|
2014-09-18 09:13:13 +04:00
|
|
|
t = std::max(t, track->GetEnd());
|
2013-12-04 05:08:12 +04:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
StreamBuffer::Track*
|
|
|
|
StreamBuffer::FindTrack(TrackID aID)
|
|
|
|
{
|
2015-05-13 16:35:10 +03:00
|
|
|
if (aID == TRACK_NONE || mTracks.IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2015-05-13 16:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The tracks are sorted by ID. We can use a binary search.
|
|
|
|
|
|
|
|
uint32_t left = 0, right = mTracks.Length() - 1;
|
|
|
|
while (left <= right) {
|
|
|
|
uint32_t middle = (left + right) / 2;
|
|
|
|
if (mTracks[middle]->GetID() == aID) {
|
|
|
|
return mTracks[middle];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTracks[middle]->GetID() > aID) {
|
2015-05-13 18:07:16 +03:00
|
|
|
if (middle == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
right = middle - 1;
|
|
|
|
} else {
|
|
|
|
left = middle + 1;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StreamBuffer::ForgetUpTo(StreamTime aTime)
|
|
|
|
{
|
2014-06-12 08:44:59 +04:00
|
|
|
// Only prune if there is a reasonable chunk (50ms @ 48kHz) to forget, so we
|
|
|
|
// don't spend too much time pruning segments.
|
|
|
|
const StreamTime minChunkSize = 2400;
|
|
|
|
if (aTime < mForgottenTime + minChunkSize) {
|
2012-04-30 07:11:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-12 08:44:59 +04:00
|
|
|
mForgottenTime = aTime;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
2012-04-30 07:11:19 +04:00
|
|
|
Track* track = mTracks[i];
|
2014-09-18 09:13:13 +04:00
|
|
|
if (track->IsEnded() && track->GetEnd() <= aTime) {
|
2012-04-30 07:11:19 +04:00
|
|
|
mTracks.RemoveElementAt(i);
|
2015-05-13 16:35:10 +03:00
|
|
|
mTracksDirty = true;
|
2012-04-30 07:11:19 +04:00
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime forgetTo = std::min(track->GetEnd() - 1, aTime);
|
2012-04-30 07:11:19 +04:00
|
|
|
track->ForgetUpTo(forgetTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|