2012-04-29 20:11:19 -07: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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_MEDIASEGMENT_H_
|
|
|
|
#define MOZILLA_MEDIASEGMENT_H_
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
2013-01-28 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#endif
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2013-01-28 10:22:37 -08:00
|
|
|
#include "Latency.h"
|
2012-04-29 20:11:19 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-05 03:08:05 -07:00
|
|
|
/**
|
2014-06-11 21:45:00 -07:00
|
|
|
* Track or graph rate in Hz. Maximum 1 << TRACK_RATE_MAX_BITS Hz. This
|
2014-06-05 03:08:05 -07:00
|
|
|
* maximum avoids overflow in conversions between track rates and conversions
|
|
|
|
* from seconds.
|
|
|
|
*/
|
|
|
|
typedef int32_t TrackRate;
|
|
|
|
const int64_t TRACK_RATE_MAX_BITS = 20;
|
|
|
|
const TrackRate TRACK_RATE_MAX = 1 << TRACK_RATE_MAX_BITS;
|
|
|
|
|
2012-04-29 20:11:19 -07:00
|
|
|
/**
|
|
|
|
* A number of ticks at a rate determined by some underlying track (e.g.
|
|
|
|
* audio sample rate). We want to make sure that multiplying TrackTicks by
|
2014-06-11 21:45:00 -07:00
|
|
|
* a TrackRate doesn't overflow, so we set its max accordingly.
|
2012-04-29 20:11:19 -07:00
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
typedef int64_t TrackTicks;
|
2014-06-11 21:45:00 -07:00
|
|
|
const int64_t TRACK_TICKS_MAX = INT64_MAX >> TRACK_RATE_MAX_BITS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We represent media times in 64-bit audio frame counts or ticks.
|
|
|
|
* All audio tracks in a MediaStreamGraph have the same sample rate and all
|
|
|
|
* streams in the graph measure time using ticks at the same audio rate.
|
|
|
|
*/
|
|
|
|
typedef int64_t MediaTime;
|
|
|
|
const int64_t MEDIA_TIME_MAX = TRACK_TICKS_MAX;
|
2012-04-29 20:11:19 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A MediaSegment is a chunk of media data sequential in time. Different
|
|
|
|
* types of data have different subclasses of MediaSegment, all inheriting
|
|
|
|
* from MediaSegmentBase.
|
|
|
|
* All MediaSegment data is timed using TrackTicks. The actual tick rate
|
|
|
|
* is defined on a per-track basis. For some track types, this can be
|
|
|
|
* a fixed constant for all tracks of that type (e.g. 1MHz for video).
|
|
|
|
*
|
|
|
|
* Each media segment defines a concept of "null media data" (e.g. silence
|
|
|
|
* for audio or "no video frame" for video), which can be efficiently
|
|
|
|
* represented. This is used for padding.
|
|
|
|
*/
|
|
|
|
class MediaSegment {
|
|
|
|
public:
|
|
|
|
virtual ~MediaSegment()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MediaSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
AUDIO,
|
|
|
|
VIDEO,
|
|
|
|
TYPE_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total duration of the segment.
|
|
|
|
*/
|
2012-07-31 05:17:21 -07:00
|
|
|
TrackTicks GetDuration() const { return mDuration; }
|
|
|
|
Type GetType() const { return mType; }
|
2012-04-29 20:11:19 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a MediaSegment of the same type.
|
|
|
|
*/
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual MediaSegment* CreateEmptyClone() const = 0;
|
2012-04-29 20:11:19 -07:00
|
|
|
/**
|
|
|
|
* Moves contents of aSource to the end of this segment.
|
|
|
|
*/
|
|
|
|
virtual void AppendFrom(MediaSegment* aSource) = 0;
|
2012-07-31 05:17:21 -07:00
|
|
|
/**
|
|
|
|
* Append a slice of aSource to this segment.
|
|
|
|
*/
|
|
|
|
virtual void AppendSlice(const MediaSegment& aSource,
|
|
|
|
TrackTicks aStart, TrackTicks aEnd) = 0;
|
2012-04-29 20:11:19 -07:00
|
|
|
/**
|
|
|
|
* Replace all contents up to aDuration with null data.
|
|
|
|
*/
|
|
|
|
virtual void ForgetUpTo(TrackTicks aDuration) = 0;
|
|
|
|
/**
|
|
|
|
* Insert aDuration of null data at the start of the segment.
|
|
|
|
*/
|
|
|
|
virtual void InsertNullDataAtStart(TrackTicks aDuration) = 0;
|
2012-07-31 05:17:21 -07:00
|
|
|
/**
|
|
|
|
* Insert aDuration of null data at the end of the segment.
|
|
|
|
*/
|
|
|
|
virtual void AppendNullData(TrackTicks aDuration) = 0;
|
2013-08-25 23:07:17 -07:00
|
|
|
/**
|
|
|
|
* Replace contents with disabled data of the same duration
|
|
|
|
*/
|
|
|
|
virtual void ReplaceWithDisabled() = 0;
|
2013-02-25 01:25:07 -08:00
|
|
|
/**
|
|
|
|
* Remove all contents, setting duration to 0.
|
|
|
|
*/
|
|
|
|
virtual void Clear() = 0;
|
2012-04-29 20:11:19 -07:00
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:19 -07:00
|
|
|
protected:
|
|
|
|
MediaSegment(Type aType) : mDuration(0), mType(aType)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MediaSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackTicks mDuration; // total of mDurations of all chunks
|
|
|
|
Type mType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C is the implementation class subclassed from MediaSegmentBase.
|
|
|
|
* C must contain a Chunk class.
|
|
|
|
*/
|
|
|
|
template <class C, class Chunk> class MediaSegmentBase : public MediaSegment {
|
|
|
|
public:
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual MediaSegment* CreateEmptyClone() const
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2013-01-20 12:44:44 -08:00
|
|
|
return new C();
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void AppendFrom(MediaSegment* aSource)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
NS_ASSERTION(aSource->GetType() == C::StaticType(), "Wrong type");
|
|
|
|
AppendFromInternal(static_cast<C*>(aSource));
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
void AppendFrom(C* aSource)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
AppendFromInternal(aSource);
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void AppendSlice(const MediaSegment& aSource,
|
|
|
|
TrackTicks aStart, TrackTicks aEnd)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
NS_ASSERTION(aSource.GetType() == C::StaticType(), "Wrong type");
|
|
|
|
AppendSliceInternal(static_cast<const C&>(aSource), aStart, aEnd);
|
|
|
|
}
|
|
|
|
void AppendSlice(const C& aOther, TrackTicks aStart, TrackTicks aEnd)
|
|
|
|
{
|
|
|
|
AppendSliceInternal(aOther, aStart, aEnd);
|
|
|
|
}
|
2012-04-29 20:11:19 -07:00
|
|
|
/**
|
|
|
|
* Replace the first aDuration ticks with null media data, because the data
|
|
|
|
* will not be required again.
|
|
|
|
*/
|
|
|
|
virtual void ForgetUpTo(TrackTicks aDuration)
|
|
|
|
{
|
|
|
|
if (mChunks.IsEmpty() || aDuration <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mChunks[0].IsNull()) {
|
2013-01-15 04:22:03 -08:00
|
|
|
TrackTicks extraToForget = std::min(aDuration, mDuration) - mChunks[0].GetDuration();
|
2012-04-29 20:11:19 -07:00
|
|
|
if (extraToForget > 0) {
|
2012-07-31 05:17:21 -07:00
|
|
|
RemoveLeading(extraToForget, 1);
|
2012-04-29 20:11:19 -07:00
|
|
|
mChunks[0].mDuration += extraToForget;
|
|
|
|
mDuration += extraToForget;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
RemoveLeading(aDuration, 0);
|
2012-04-29 20:11:19 -07:00
|
|
|
mChunks.InsertElementAt(0)->SetNull(aDuration);
|
|
|
|
mDuration += aDuration;
|
|
|
|
}
|
|
|
|
virtual void InsertNullDataAtStart(TrackTicks aDuration)
|
|
|
|
{
|
|
|
|
if (aDuration <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mChunks.IsEmpty() && mChunks[0].IsNull()) {
|
|
|
|
mChunks[0].mDuration += aDuration;
|
|
|
|
} else {
|
|
|
|
mChunks.InsertElementAt(0)->SetNull(aDuration);
|
|
|
|
}
|
2013-01-28 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
mChunks[0].mTimeStamp = mozilla::TimeStamp::Now();
|
|
|
|
#endif
|
2012-04-29 20:11:19 -07:00
|
|
|
mDuration += aDuration;
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
virtual void AppendNullData(TrackTicks aDuration)
|
|
|
|
{
|
|
|
|
if (aDuration <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mChunks.IsEmpty() && mChunks[mChunks.Length() - 1].IsNull()) {
|
|
|
|
mChunks[mChunks.Length() - 1].mDuration += aDuration;
|
|
|
|
} else {
|
|
|
|
mChunks.AppendElement()->SetNull(aDuration);
|
|
|
|
}
|
|
|
|
mDuration += aDuration;
|
|
|
|
}
|
2013-08-25 23:07:17 -07:00
|
|
|
virtual void ReplaceWithDisabled()
|
|
|
|
{
|
|
|
|
if (GetType() != AUDIO) {
|
|
|
|
MOZ_CRASH("Disabling unknown segment type");
|
|
|
|
}
|
|
|
|
TrackTicks duration = GetDuration();
|
|
|
|
Clear();
|
|
|
|
AppendNullData(duration);
|
|
|
|
}
|
2013-02-25 01:25:07 -08:00
|
|
|
virtual void Clear()
|
|
|
|
{
|
|
|
|
mDuration = 0;
|
|
|
|
mChunks.Clear();
|
|
|
|
}
|
2012-04-29 20:11:19 -07:00
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
class ChunkIterator {
|
|
|
|
public:
|
|
|
|
ChunkIterator(MediaSegmentBase<C, Chunk>& aSegment)
|
|
|
|
: mSegment(aSegment), mIndex(0) {}
|
|
|
|
bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
|
|
|
|
void Next() { ++mIndex; }
|
|
|
|
Chunk& operator*() { return mSegment.mChunks[mIndex]; }
|
|
|
|
Chunk* operator->() { return &mSegment.mChunks[mIndex]; }
|
|
|
|
private:
|
|
|
|
MediaSegmentBase<C, Chunk>& mSegment;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
2013-06-03 02:59:50 -07:00
|
|
|
void RemoveLeading(TrackTicks aDuration)
|
|
|
|
{
|
|
|
|
RemoveLeading(aDuration, 0);
|
|
|
|
}
|
2013-10-25 15:13:42 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
void GetStartTime(TimeStamp &aTime) {
|
|
|
|
aTime = mChunks[0].mTimeStamp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
size_t amount = mChunks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (size_t i = 0; i < mChunks.Length(); i++) {
|
|
|
|
amount += mChunks[i].SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:19 -07:00
|
|
|
protected:
|
|
|
|
MediaSegmentBase(Type aType) : MediaSegment(aType) {}
|
|
|
|
|
2012-07-31 05:17:21 -07:00
|
|
|
/**
|
|
|
|
* Appends the contents of aSource to this segment, clearing aSource.
|
|
|
|
*/
|
|
|
|
void AppendFromInternal(MediaSegmentBase<C, Chunk>* aSource)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-11-30 00:08:17 -08:00
|
|
|
MOZ_ASSERT(aSource->mDuration >= 0);
|
2012-07-31 05:17:21 -07:00
|
|
|
mDuration += aSource->mDuration;
|
|
|
|
aSource->mDuration = 0;
|
|
|
|
if (!mChunks.IsEmpty() && !aSource->mChunks.IsEmpty() &&
|
|
|
|
mChunks[mChunks.Length() - 1].CanCombineWithFollowing(aSource->mChunks[0])) {
|
|
|
|
mChunks[mChunks.Length() - 1].mDuration += aSource->mChunks[0].mDuration;
|
|
|
|
aSource->mChunks.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
mChunks.MoveElementsFrom(aSource->mChunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendSliceInternal(const MediaSegmentBase<C, Chunk>& aSource,
|
|
|
|
TrackTicks aStart, TrackTicks aEnd)
|
|
|
|
{
|
2014-03-24 03:06:06 -07:00
|
|
|
MOZ_ASSERT(aStart <= aEnd, "Endpoints inverted");
|
2014-05-13 04:27:21 -07:00
|
|
|
NS_WARN_IF_FALSE(aStart >= 0 && aEnd <= aSource.mDuration, "Slice out of range");
|
2012-07-31 05:17:21 -07:00
|
|
|
mDuration += aEnd - aStart;
|
2012-04-29 20:11:19 -07:00
|
|
|
TrackTicks offset = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aSource.mChunks.Length() && offset < aEnd; ++i) {
|
2012-07-31 05:17:21 -07:00
|
|
|
const Chunk& c = aSource.mChunks[i];
|
2013-01-15 04:22:03 -08:00
|
|
|
TrackTicks start = std::max(aStart, offset);
|
2012-04-29 20:11:19 -07:00
|
|
|
TrackTicks nextOffset = offset + c.GetDuration();
|
2013-01-15 04:22:03 -08:00
|
|
|
TrackTicks end = std::min(aEnd, nextOffset);
|
2012-04-29 20:11:19 -07:00
|
|
|
if (start < end) {
|
|
|
|
mChunks.AppendElement(c)->SliceTo(start - offset, end - offset);
|
|
|
|
}
|
|
|
|
offset = nextOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk* AppendChunk(TrackTicks aDuration)
|
|
|
|
{
|
2012-11-30 00:08:17 -08:00
|
|
|
MOZ_ASSERT(aDuration >= 0);
|
2012-04-29 20:11:19 -07:00
|
|
|
Chunk* c = mChunks.AppendElement();
|
|
|
|
c->mDuration = aDuration;
|
|
|
|
mDuration += aDuration;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
Chunk* FindChunkContaining(TrackTicks aOffset, TrackTicks* aStart = nullptr)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
|
|
|
if (aOffset < 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
TrackTicks offset = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mChunks.Length(); ++i) {
|
2012-04-29 20:11:19 -07:00
|
|
|
Chunk& c = mChunks[i];
|
|
|
|
TrackTicks nextOffset = offset + c.GetDuration();
|
|
|
|
if (aOffset < nextOffset) {
|
|
|
|
if (aStart) {
|
|
|
|
*aStart = offset;
|
|
|
|
}
|
|
|
|
return &c;
|
|
|
|
}
|
|
|
|
offset = nextOffset;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk* GetLastChunk()
|
|
|
|
{
|
|
|
|
if (mChunks.IsEmpty()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
return &mChunks[mChunks.Length() - 1];
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void RemoveLeading(TrackTicks aDuration, uint32_t aStartIndex)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aDuration >= 0, "Can't remove negative duration");
|
|
|
|
TrackTicks t = aDuration;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t chunksToRemove = 0;
|
|
|
|
for (uint32_t i = aStartIndex; i < mChunks.Length() && t > 0; ++i) {
|
2012-04-29 20:11:19 -07:00
|
|
|
Chunk* c = &mChunks[i];
|
|
|
|
if (c->GetDuration() > t) {
|
|
|
|
c->SliceTo(t, c->GetDuration());
|
|
|
|
t = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t -= c->GetDuration();
|
|
|
|
chunksToRemove = i + 1 - aStartIndex;
|
|
|
|
}
|
|
|
|
mChunks.RemoveElementsAt(aStartIndex, chunksToRemove);
|
|
|
|
mDuration -= aDuration - t;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<Chunk> mChunks;
|
2013-01-28 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
mozilla::TimeStamp mTimeStamp;
|
|
|
|
#endif
|
2012-04-29 20:11:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_MEDIASEGMENT_H_ */
|