2012-04-30 07:11:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-05-04 05:55:59 +03:00
|
|
|
#ifndef MOZILLA_STREAMTRACKS_H_
|
|
|
|
#define MOZILLA_STREAMTRACKS_H_
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
#include "MediaSegment.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2016-05-04 05:55:59 +03:00
|
|
|
* Unique ID for track within a StreamTracks. Tracks from different
|
|
|
|
* StreamTrackss may have the same ID; this matters when appending StreamTrackss,
|
2012-04-30 07:11:19 +04:00
|
|
|
* since tracks with the same ID are matched. Only IDs greater than 0 are allowed.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef int32_t TrackID;
|
2012-04-30 07:11:19 +04:00
|
|
|
const TrackID TRACK_NONE = 0;
|
2014-08-26 05:20:44 +04:00
|
|
|
const TrackID TRACK_INVALID = -1;
|
2015-09-30 04:31:53 +03:00
|
|
|
const TrackID TRACK_ANY = -2;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2016-01-05 05:16:22 +03:00
|
|
|
inline bool IsTrackIDExplicit(const TrackID& aId) {
|
|
|
|
return aId > TRACK_NONE;
|
|
|
|
}
|
|
|
|
|
2014-06-12 08:44:59 +04:00
|
|
|
inline TrackTicks RateConvertTicksRoundDown(TrackRate aOutRate,
|
|
|
|
TrackRate aInRate,
|
|
|
|
TrackTicks aTicks)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2014-06-12 08:44:59 +04:00
|
|
|
NS_ASSERTION(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
|
|
|
|
NS_ASSERTION(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
|
2016-04-21 12:44:35 +03:00
|
|
|
NS_ASSERTION(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
|
2014-06-12 08:44:59 +04:00
|
|
|
return (aTicks * aOutRate) / aInRate;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2014-06-12 08:44:59 +04:00
|
|
|
inline TrackTicks RateConvertTicksRoundUp(TrackRate aOutRate,
|
|
|
|
TrackRate aInRate, TrackTicks aTicks)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2014-06-12 08:44:59 +04:00
|
|
|
NS_ASSERTION(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
|
|
|
|
NS_ASSERTION(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
|
|
|
|
NS_ASSERTION(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
|
|
|
|
return (aTicks * aOutRate + aInRate - 1) / aInRate;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This object contains the decoded data for a stream's tracks.
|
2016-05-04 05:55:59 +03:00
|
|
|
* A StreamTracks can be appended to. Logically a StreamTracks only gets longer,
|
2012-04-30 07:11:19 +04:00
|
|
|
* but we also have the ability to "forget" data before a certain time that
|
|
|
|
* we know won't be used again. (We prune a whole number of seconds internally.)
|
|
|
|
*
|
2016-05-04 05:55:59 +03:00
|
|
|
* StreamTrackss should only be used from one thread at a time.
|
2012-04-30 07:11:19 +04:00
|
|
|
*
|
2016-05-04 05:55:59 +03:00
|
|
|
* A StreamTracks has a set of tracks that can be of arbitrary types ---
|
2012-04-30 07:11:19 +04:00
|
|
|
* the data for each track is a MediaSegment. The set of tracks can vary
|
2016-05-04 05:55:59 +03:00
|
|
|
* over the timeline of the StreamTracks.
|
2012-04-30 07:11:19 +04:00
|
|
|
*/
|
2016-05-04 05:55:59 +03:00
|
|
|
class StreamTracks
|
2015-05-13 16:35:10 +03:00
|
|
|
{
|
2012-04-30 07:11:19 +04:00
|
|
|
public:
|
|
|
|
/**
|
2016-05-04 05:55:59 +03:00
|
|
|
* Every track has a start time --- when it started in the StreamTracks.
|
2012-04-30 07:11:19 +04:00
|
|
|
* It has an end flag; when false, no end point is known; when true,
|
|
|
|
* the track ends when the data we have for the track runs out.
|
|
|
|
* Tracks have a unique ID assigned at creation. This allows us to identify
|
2016-05-04 05:55:59 +03:00
|
|
|
* the same track across StreamTrackss. A StreamTracks should never have
|
2012-04-30 07:11:19 +04:00
|
|
|
* two tracks with the same ID (even if they don't overlap in time).
|
|
|
|
* TODO Tracks can also be enabled and disabled over time.
|
2012-07-31 16:17:21 +04:00
|
|
|
* Takes ownership of aSegment.
|
2012-04-30 07:11:19 +04:00
|
|
|
*/
|
2015-05-13 16:35:10 +03:00
|
|
|
class Track final
|
|
|
|
{
|
2014-09-18 09:20:44 +04:00
|
|
|
Track(TrackID aID, StreamTime aStart, MediaSegment* aSegment)
|
2012-04-30 07:11:19 +04:00
|
|
|
: mStart(aStart),
|
|
|
|
mSegment(aSegment),
|
|
|
|
mID(aID),
|
|
|
|
mEnded(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Track);
|
|
|
|
|
|
|
|
NS_ASSERTION(aID > TRACK_NONE, "Bad track ID");
|
|
|
|
NS_ASSERTION(0 <= aStart && aStart <= aSegment->GetDuration(), "Bad start position");
|
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
|
2014-06-12 08:44:59 +04:00
|
|
|
public:
|
2012-04-30 07:11:19 +04:00
|
|
|
~Track()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Track);
|
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
template <class T> T* Get() const
|
|
|
|
{
|
|
|
|
if (mSegment->GetType() == T::StaticType()) {
|
|
|
|
return static_cast<T*>(mSegment.get());
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
MediaSegment* GetSegment() const { return mSegment; }
|
|
|
|
TrackID GetID() const { return mID; }
|
|
|
|
bool IsEnded() const { return mEnded; }
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime GetStart() const { return mStart; }
|
|
|
|
StreamTime GetEnd() const { return mSegment->GetDuration(); }
|
2012-04-30 07:11:19 +04:00
|
|
|
MediaSegment::Type GetType() const { return mSegment->GetType(); }
|
|
|
|
|
|
|
|
void SetEnded() { mEnded = true; }
|
|
|
|
void AppendFrom(Track* aTrack)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mEnded, "Can't append to ended track");
|
|
|
|
NS_ASSERTION(aTrack->mID == mID, "IDs must match");
|
|
|
|
NS_ASSERTION(aTrack->mStart == 0, "Source track must start at zero");
|
|
|
|
NS_ASSERTION(aTrack->mSegment->GetType() == GetType(), "Track types must match");
|
|
|
|
|
|
|
|
mSegment->AppendFrom(aTrack->mSegment);
|
|
|
|
mEnded = aTrack->mEnded;
|
|
|
|
}
|
|
|
|
MediaSegment* RemoveSegment()
|
|
|
|
{
|
|
|
|
return mSegment.forget();
|
|
|
|
}
|
2014-09-18 09:20:43 +04:00
|
|
|
void ForgetUpTo(StreamTime aTime)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
|
|
|
mSegment->ForgetUpTo(aTime);
|
|
|
|
}
|
2014-09-18 09:20:43 +04:00
|
|
|
void FlushAfter(StreamTime aNewEnd)
|
2014-10-29 17:47:28 +03:00
|
|
|
{
|
|
|
|
// Forget everything after a given endpoint
|
|
|
|
// a specified amount
|
|
|
|
mSegment->FlushAfter(aNewEnd);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
if (mSegment) {
|
|
|
|
amount += mSegment->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
private:
|
2016-05-04 05:55:59 +03:00
|
|
|
friend class StreamTracks;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
// Start offset is in ticks at rate mRate
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime mStart;
|
2016-05-04 05:55:59 +03:00
|
|
|
// The segment data starts at the start of the owning StreamTracks, i.e.,
|
2012-04-30 07:11:19 +04:00
|
|
|
// there's mStart silence/no video at the beginning.
|
|
|
|
nsAutoPtr<MediaSegment> mSegment;
|
|
|
|
// Unique ID
|
|
|
|
TrackID mID;
|
|
|
|
// True when the track ends with the data in mSegment
|
|
|
|
bool mEnded;
|
|
|
|
};
|
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
class MOZ_STACK_CLASS CompareTracksByID final
|
|
|
|
{
|
2012-04-30 07:11:19 +04:00
|
|
|
public:
|
|
|
|
bool Equals(Track* aA, Track* aB) const {
|
|
|
|
return aA->GetID() == aB->GetID();
|
|
|
|
}
|
|
|
|
bool LessThan(Track* aA, Track* aB) const {
|
|
|
|
return aA->GetID() < aB->GetID();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-04 05:55:59 +03:00
|
|
|
StreamTracks()
|
2016-01-12 21:16:59 +03:00
|
|
|
: mGraphRate(0)
|
|
|
|
, mTracksKnownTime(0)
|
2015-05-13 16:35:10 +03:00
|
|
|
, mForgottenTime(0)
|
|
|
|
, mTracksDirty(false)
|
2014-06-12 08:44:59 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mGraphRateIsSet(false)
|
|
|
|
#endif
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2016-05-04 05:55:59 +03:00
|
|
|
MOZ_COUNT_CTOR(StreamTracks);
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2016-05-04 05:55:59 +03:00
|
|
|
~StreamTracks()
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2016-05-04 05:55:59 +03:00
|
|
|
MOZ_COUNT_DTOR(StreamTracks);
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mTracks.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
for (size_t i = 0; i < mTracks.Length(); i++) {
|
|
|
|
amount += mTracks[i]->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2014-06-12 08:44:59 +04:00
|
|
|
/**
|
|
|
|
* Initialize the graph rate for use in calculating StreamTimes from track
|
|
|
|
* ticks. Called when a MediaStream's graph pointer is initialized.
|
|
|
|
*/
|
|
|
|
void InitGraphRate(TrackRate aGraphRate)
|
|
|
|
{
|
|
|
|
mGraphRate = aGraphRate;
|
|
|
|
#if DEBUG
|
|
|
|
MOZ_ASSERT(!mGraphRateIsSet);
|
|
|
|
mGraphRateIsSet = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackRate GraphRate() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mGraphRateIsSet);
|
|
|
|
return mGraphRate;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Takes ownership of aSegment. Don't do this while iterating, or while
|
|
|
|
* holding a Track reference.
|
|
|
|
* aSegment must have aStart worth of null data.
|
|
|
|
*/
|
2014-09-18 09:20:43 +04:00
|
|
|
Track& AddTrack(TrackID aID, StreamTime aStart, MediaSegment* aSegment)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2014-06-12 08:44:59 +04:00
|
|
|
NS_ASSERTION(!FindTrack(aID), "Track with this ID already exists");
|
|
|
|
|
2014-09-18 09:20:44 +04:00
|
|
|
Track* track = new Track(aID, aStart, aSegment);
|
2014-06-12 08:44:59 +04:00
|
|
|
mTracks.InsertElementSorted(track, CompareTracksByID());
|
2015-05-13 16:35:10 +03:00
|
|
|
mTracksDirty = true;
|
2014-06-12 08:44:59 +04:00
|
|
|
|
2014-06-05 14:08:05 +04:00
|
|
|
if (mTracksKnownTime == STREAM_TIME_MAX) {
|
|
|
|
// There exists code like
|
|
|
|
// http://mxr.mozilla.org/mozilla-central/source/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp?rev=96b197deb91e&mark=1292-1297#1292
|
2016-05-04 05:55:59 +03:00
|
|
|
NS_WARNING("Adding track to StreamTracks that should have no more tracks");
|
2014-06-05 14:08:05 +04:00
|
|
|
} else {
|
2014-09-18 09:13:13 +04:00
|
|
|
NS_ASSERTION(mTracksKnownTime <= aStart, "Start time too early");
|
2014-06-05 14:08:05 +04:00
|
|
|
}
|
2014-06-12 08:44:59 +04:00
|
|
|
return *track;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
void AdvanceKnownTracksTime(StreamTime aKnownTime)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aKnownTime >= mTracksKnownTime, "Can't move tracks-known time earlier");
|
|
|
|
mTracksKnownTime = aKnownTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-04 05:55:59 +03:00
|
|
|
* The end time for the StreamTracks is the latest time for which we have
|
2012-04-30 07:11:19 +04:00
|
|
|
* data for all tracks that haven't ended by that time.
|
|
|
|
*/
|
|
|
|
StreamTime GetEnd() const;
|
|
|
|
|
2013-12-04 05:08:12 +04:00
|
|
|
/**
|
|
|
|
* Returns the earliest time >= 0 at which all tracks have ended
|
|
|
|
* and all their data has been played out and no new tracks can be added,
|
|
|
|
* or STREAM_TIME_MAX if there is no such time.
|
|
|
|
*/
|
|
|
|
StreamTime GetAllTracksEnd() const;
|
|
|
|
|
2013-03-07 12:53:45 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void DumpTrackInfo() const;
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
Track* FindTrack(TrackID aID);
|
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
class MOZ_STACK_CLASS TrackIter final
|
|
|
|
{
|
2012-04-30 07:11:19 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Iterate through the tracks of aBuffer in order of ID.
|
|
|
|
*/
|
2016-05-04 05:55:59 +03:00
|
|
|
explicit TrackIter(const StreamTracks& aBuffer) :
|
2012-04-30 07:11:19 +04:00
|
|
|
mBuffer(&aBuffer.mTracks), mIndex(0), mMatchType(false) {}
|
|
|
|
/**
|
|
|
|
* Iterate through the tracks of aBuffer with type aType, in order of ID.
|
|
|
|
*/
|
2016-05-04 05:55:59 +03:00
|
|
|
TrackIter(const StreamTracks& aBuffer, MediaSegment::Type aType) :
|
2012-04-30 07:11:19 +04:00
|
|
|
mBuffer(&aBuffer.mTracks), mIndex(0), mType(aType), mMatchType(true) { FindMatch(); }
|
|
|
|
bool IsEnded() { return mIndex >= mBuffer->Length(); }
|
|
|
|
void Next()
|
|
|
|
{
|
|
|
|
++mIndex;
|
|
|
|
FindMatch();
|
|
|
|
}
|
2012-07-31 16:17:21 +04:00
|
|
|
Track* get() { return mBuffer->ElementAt(mIndex); }
|
2012-04-30 07:11:19 +04:00
|
|
|
Track& operator*() { return *mBuffer->ElementAt(mIndex); }
|
|
|
|
Track* operator->() { return mBuffer->ElementAt(mIndex); }
|
|
|
|
private:
|
|
|
|
void FindMatch()
|
|
|
|
{
|
|
|
|
if (!mMatchType)
|
|
|
|
return;
|
|
|
|
while (mIndex < mBuffer->Length() &&
|
|
|
|
mBuffer->ElementAt(mIndex)->GetType() != mType) {
|
|
|
|
++mIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray<nsAutoPtr<Track> >* mBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mIndex;
|
2012-04-30 07:11:19 +04:00
|
|
|
MediaSegment::Type mType;
|
|
|
|
bool mMatchType;
|
|
|
|
};
|
|
|
|
friend class TrackIter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forget stream data before aTime; they will no longer be needed.
|
|
|
|
* Also can forget entire tracks that have ended at or before aTime.
|
|
|
|
* Can't be used to forget beyond GetEnd().
|
|
|
|
*/
|
|
|
|
void ForgetUpTo(StreamTime aTime);
|
2012-07-31 16:17:21 +04:00
|
|
|
/**
|
|
|
|
* Returns the latest time passed to ForgetUpTo.
|
|
|
|
*/
|
|
|
|
StreamTime GetForgottenDuration()
|
|
|
|
{
|
|
|
|
return mForgottenTime;
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
bool GetAndResetTracksDirty()
|
|
|
|
{
|
|
|
|
if (!mTracksDirty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTracksDirty = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
protected:
|
2014-06-12 08:44:59 +04:00
|
|
|
TrackRate mGraphRate; // StreamTime per second
|
2012-04-30 07:11:19 +04:00
|
|
|
// Any new tracks added will start at or after this time. In other words, the track
|
|
|
|
// list is complete and correct for all times less than this time.
|
|
|
|
StreamTime mTracksKnownTime;
|
|
|
|
StreamTime mForgottenTime;
|
2015-05-13 16:35:10 +03:00
|
|
|
|
|
|
|
private:
|
2016-05-04 05:55:59 +03:00
|
|
|
// All known tracks for this StreamTracks
|
2015-05-13 16:35:10 +03:00
|
|
|
nsTArray<nsAutoPtr<Track>> mTracks;
|
|
|
|
bool mTracksDirty;
|
|
|
|
|
2014-06-12 08:44:59 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mGraphRateIsSet;
|
|
|
|
#endif
|
2012-04-30 07:11:19 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2016-05-04 05:55:59 +03:00
|
|
|
#endif /* MOZILLA_STREAMTRACKS_H_ */
|
2012-04-30 07:11:19 +04:00
|
|
|
|