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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_MEDIASEGMENT_H_
|
|
|
|
#define MOZILLA_MEDIASEGMENT_H_
|
|
|
|
|
2019-10-02 13:22:48 +03:00
|
|
|
#include "PrincipalHandle.h"
|
2012-04-30 07:11:19 +04:00
|
|
|
#include "nsTArray.h"
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
# include "mozilla/TimeStamp.h"
|
|
|
|
#endif
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-05 14:08:05 +04:00
|
|
|
/**
|
2014-06-12 08:45:00 +04:00
|
|
|
* Track or graph rate in Hz. Maximum 1 << TRACK_RATE_MAX_BITS Hz. This
|
2014-06-05 14:08:05 +04: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-30 07:11:19 +04:00
|
|
|
/**
|
2019-10-02 13:23:02 +03: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 a TrackRate
|
|
|
|
* doesn't overflow, so we set its max accordingly.
|
|
|
|
* TrackTime should be used instead when we're working with MediaTrackGraph's
|
|
|
|
* rate, but TrackTicks can be used outside MediaTracks when we have data at a
|
|
|
|
* different rate.
|
2012-04-30 07:11:19 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef int64_t TrackTicks;
|
2014-06-12 08:45:00 +04: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.
|
2019-10-02 13:23:02 +03:00
|
|
|
* All tracks in a MediaTrackGraph have the same rate.
|
2014-06-12 08:45:00 +04:00
|
|
|
*/
|
|
|
|
typedef int64_t MediaTime;
|
|
|
|
const int64_t MEDIA_TIME_MAX = TRACK_TICKS_MAX;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2014-09-18 09:20:43 +04:00
|
|
|
/**
|
2019-10-02 13:23:02 +03:00
|
|
|
* Media time relative to the start of a MediaTrack.
|
2014-09-18 09:20:43 +04:00
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
typedef MediaTime TrackTime;
|
|
|
|
const TrackTime TRACK_TIME_MAX = MEDIA_TIME_MAX;
|
2014-09-18 09:20:43 +04:00
|
|
|
|
2015-07-23 02:48:47 +03:00
|
|
|
/**
|
|
|
|
* Media time relative to the start of the graph timeline.
|
|
|
|
*/
|
|
|
|
typedef MediaTime GraphTime;
|
|
|
|
const GraphTime GRAPH_TIME_MAX = MEDIA_TIME_MAX;
|
|
|
|
|
2019-12-19 01:50:32 +03:00
|
|
|
/* Time conversion helper functions */
|
|
|
|
inline TrackTicks RateConvertTicksRoundDown(TrackRate aOutRate,
|
|
|
|
TrackRate aInRate,
|
|
|
|
TrackTicks aTicks) {
|
|
|
|
MOZ_ASSERT(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
|
|
|
|
MOZ_ASSERT(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
|
|
|
|
MOZ_ASSERT(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
|
|
|
|
return (aTicks * aOutRate) / aInRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TrackTicks RateConvertTicksRoundUp(TrackRate aOutRate, TrackRate aInRate,
|
|
|
|
TrackTicks aTicks) {
|
|
|
|
MOZ_ASSERT(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
|
|
|
|
MOZ_ASSERT(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
|
|
|
|
MOZ_ASSERT(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
|
|
|
|
return (aTicks * aOutRate + aInRate - 1) / aInRate;
|
|
|
|
}
|
|
|
|
|
2018-03-28 15:39:54 +03:00
|
|
|
/**
|
|
|
|
* The number of chunks allocated by default for a MediaSegment.
|
|
|
|
* Appending more chunks than this will cause further allocations.
|
|
|
|
*
|
|
|
|
* 16 is an arbitrary number intended to cover the most common cases in the
|
2019-10-02 13:23:02 +03:00
|
|
|
* MediaTrackGraph (1 with silence and 1-2 with data for a realtime track)
|
2018-03-28 15:39:54 +03:00
|
|
|
* with some margin.
|
|
|
|
*/
|
|
|
|
const size_t DEFAULT_SEGMENT_CAPACITY = 16;
|
|
|
|
|
2012-04-30 07:11:19 +04: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.
|
2019-10-02 13:23:02 +03:00
|
|
|
* All MediaSegment data is timed using TrackTime. The actual tick rate
|
2012-04-30 07:11:19 +04:00
|
|
|
* 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:
|
2017-02-17 16:31:50 +03:00
|
|
|
MediaSegment(const MediaSegment&) = delete;
|
|
|
|
MediaSegment& operator=(const MediaSegment&) = delete;
|
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR_VIRTUAL(MediaSegment)
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
enum Type { AUDIO, VIDEO, TYPE_COUNT };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total duration of the segment.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime GetDuration() const { return mDuration; }
|
2012-07-31 16:17:21 +04:00
|
|
|
Type GetType() const { return mType; }
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2016-04-06 15:56:44 +03:00
|
|
|
/**
|
|
|
|
* Gets the last principal id that was appended to this segment.
|
|
|
|
*/
|
2018-03-29 17:15:51 +03:00
|
|
|
const PrincipalHandle& GetLastPrincipalHandle() const {
|
|
|
|
return mLastPrincipalHandle;
|
|
|
|
}
|
2016-04-06 15:56:44 +03:00
|
|
|
/**
|
2019-10-02 13:23:02 +03:00
|
|
|
* Called by the MediaTrackGraph as it appends a chunk with a different
|
2016-04-06 15:56:44 +03:00
|
|
|
* principal id than the current one.
|
|
|
|
*/
|
2018-03-29 17:15:51 +03:00
|
|
|
void SetLastPrincipalHandle(PrincipalHandle aLastPrincipalHandle) {
|
2018-06-01 19:30:30 +03:00
|
|
|
mLastPrincipalHandle = std::forward<PrincipalHandle>(aLastPrincipalHandle);
|
2016-04-06 15:56:44 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:06:02 +03:00
|
|
|
/**
|
|
|
|
* Returns true if all chunks in this segment are null.
|
|
|
|
*/
|
|
|
|
virtual bool IsNull() const = 0;
|
|
|
|
|
2019-10-02 13:22:53 +03:00
|
|
|
/**
|
|
|
|
* Returns true if this segment contains no chunks.
|
|
|
|
*/
|
|
|
|
virtual bool IsEmpty() const = 0;
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Create a MediaSegment of the same type.
|
|
|
|
*/
|
2012-07-31 16:17:21 +04:00
|
|
|
virtual MediaSegment* CreateEmptyClone() const = 0;
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Moves contents of aSource to the end of this segment.
|
|
|
|
*/
|
|
|
|
virtual void AppendFrom(MediaSegment* aSource) = 0;
|
2012-07-31 16:17:21 +04:00
|
|
|
/**
|
|
|
|
* Append a slice of aSource to this segment.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
virtual void AppendSlice(const MediaSegment& aSource, TrackTime aStart,
|
|
|
|
TrackTime aEnd) = 0;
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Replace all contents up to aDuration with null data.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
virtual void ForgetUpTo(TrackTime aDuration) = 0;
|
2014-10-29 17:47:28 +03:00
|
|
|
/**
|
|
|
|
* Forget all data buffered after a given point
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
virtual void FlushAfter(TrackTime aNewEnd) = 0;
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Insert aDuration of null data at the start of the segment.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
virtual void InsertNullDataAtStart(TrackTime aDuration) = 0;
|
2012-07-31 16:17:21 +04:00
|
|
|
/**
|
|
|
|
* Insert aDuration of null data at the end of the segment.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
virtual void AppendNullData(TrackTime aDuration) = 0;
|
2013-08-26 10:07:17 +04:00
|
|
|
/**
|
2016-08-15 15:19:42 +03:00
|
|
|
* Replace contents with disabled (silence/black) data of the same duration
|
2013-08-26 10:07:17 +04:00
|
|
|
*/
|
|
|
|
virtual void ReplaceWithDisabled() = 0;
|
2016-08-15 15:19:42 +03:00
|
|
|
/**
|
|
|
|
* Replace contents with null data of the same duration
|
|
|
|
*/
|
|
|
|
virtual void ReplaceWithNull() = 0;
|
2013-02-25 13:25:07 +04:00
|
|
|
/**
|
|
|
|
* Remove all contents, setting duration to 0.
|
|
|
|
*/
|
|
|
|
virtual void Clear() = 0;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
protected:
|
2016-04-06 15:56:44 +03:00
|
|
|
explicit MediaSegment(Type aType)
|
|
|
|
: mDuration(0),
|
|
|
|
mType(aType),
|
|
|
|
mLastPrincipalHandle(PRINCIPAL_HANDLE_NONE) {
|
2012-04-30 07:11:19 +04:00
|
|
|
MOZ_COUNT_CTOR(MediaSegment);
|
|
|
|
}
|
|
|
|
|
2017-02-17 16:31:50 +03:00
|
|
|
MediaSegment(MediaSegment&& aSegment)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mDuration(std::move(aSegment.mDuration)),
|
|
|
|
mType(std::move(aSegment.mType)),
|
|
|
|
mLastPrincipalHandle(std::move(aSegment.mLastPrincipalHandle)) {
|
2017-02-17 16:31:50 +03:00
|
|
|
MOZ_COUNT_CTOR(MediaSegment);
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime mDuration; // total of mDurations of all chunks
|
2012-04-30 07:11:19 +04:00
|
|
|
Type mType;
|
2016-04-06 15:56:44 +03:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
// The latest principal handle that the MediaTrackGraph has processed for
|
2016-04-06 15:56:44 +03:00
|
|
|
// this segment.
|
|
|
|
PrincipalHandle mLastPrincipalHandle;
|
2012-04-30 07:11:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C is the implementation class subclassed from MediaSegmentBase.
|
|
|
|
* C must contain a Chunk class.
|
|
|
|
*/
|
|
|
|
template <class C, class Chunk>
|
|
|
|
class MediaSegmentBase : public MediaSegment {
|
|
|
|
public:
|
2017-05-16 14:06:02 +03:00
|
|
|
bool IsNull() const override {
|
|
|
|
for (typename C::ConstChunkIterator iter(*this); !iter.IsEnded();
|
|
|
|
iter.Next()) {
|
|
|
|
if (!iter->IsNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-02 13:22:53 +03:00
|
|
|
bool IsEmpty() const override { return mChunks.IsEmpty(); }
|
2016-01-18 06:50:29 +03:00
|
|
|
MediaSegment* CreateEmptyClone() const override { return new C(); }
|
|
|
|
void AppendFrom(MediaSegment* aSource) override {
|
2012-07-31 16:17:21 +04:00
|
|
|
NS_ASSERTION(aSource->GetType() == C::StaticType(), "Wrong type");
|
|
|
|
AppendFromInternal(static_cast<C*>(aSource));
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2012-07-31 16:17:21 +04:00
|
|
|
void AppendFrom(C* aSource) { AppendFromInternal(aSource); }
|
2019-10-02 13:23:02 +03:00
|
|
|
void AppendSlice(const MediaSegment& aSource, TrackTime aStart,
|
|
|
|
TrackTime aEnd) override {
|
2012-07-31 16:17:21 +04:00
|
|
|
NS_ASSERTION(aSource.GetType() == C::StaticType(), "Wrong type");
|
|
|
|
AppendSliceInternal(static_cast<const C&>(aSource), aStart, aEnd);
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
void AppendSlice(const C& aOther, TrackTime aStart, TrackTime aEnd) {
|
2012-07-31 16:17:21 +04:00
|
|
|
AppendSliceInternal(aOther, aStart, aEnd);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
/**
|
|
|
|
* Replace the first aDuration ticks with null media data, because the data
|
|
|
|
* will not be required again.
|
|
|
|
*/
|
2019-10-02 13:23:02 +03:00
|
|
|
void ForgetUpTo(TrackTime aDuration) override {
|
2012-04-30 07:11:19 +04:00
|
|
|
if (mChunks.IsEmpty() || aDuration <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mChunks[0].IsNull()) {
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime extraToForget =
|
2014-09-18 09:20:43 +04:00
|
|
|
std::min(aDuration, mDuration) - mChunks[0].GetDuration();
|
2012-04-30 07:11:19 +04:00
|
|
|
if (extraToForget > 0) {
|
2012-07-31 16:17:21 +04:00
|
|
|
RemoveLeading(extraToForget, 1);
|
2012-04-30 07:11:19 +04:00
|
|
|
mChunks[0].mDuration += extraToForget;
|
|
|
|
mDuration += extraToForget;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-07-31 16:17:21 +04:00
|
|
|
RemoveLeading(aDuration, 0);
|
2012-04-30 07:11:19 +04:00
|
|
|
mChunks.InsertElementAt(0)->SetNull(aDuration);
|
|
|
|
mDuration += aDuration;
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
void FlushAfter(TrackTime aNewEnd) override {
|
2014-10-29 17:47:28 +03:00
|
|
|
if (mChunks.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:58:20 +03:00
|
|
|
if (!aNewEnd) {
|
|
|
|
Clear();
|
|
|
|
} else if (mChunks[0].IsNull()) {
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime extraToKeep = aNewEnd - mChunks[0].GetDuration();
|
2014-10-29 17:47:28 +03:00
|
|
|
if (extraToKeep < 0) {
|
|
|
|
// reduce the size of the Null, get rid of everthing else
|
|
|
|
mChunks[0].SetNull(aNewEnd);
|
|
|
|
extraToKeep = 0;
|
|
|
|
}
|
|
|
|
RemoveTrailing(extraToKeep, 1);
|
|
|
|
} else {
|
|
|
|
if (aNewEnd > mDuration) {
|
|
|
|
NS_ASSERTION(aNewEnd <= mDuration, "can't add data in FlushAfter");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RemoveTrailing(aNewEnd, 0);
|
|
|
|
}
|
|
|
|
mDuration = aNewEnd;
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
void InsertNullDataAtStart(TrackTime aDuration) override {
|
2012-04-30 07:11:19 +04:00
|
|
|
if (aDuration <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mChunks.IsEmpty() && mChunks[0].IsNull()) {
|
|
|
|
mChunks[0].mDuration += aDuration;
|
|
|
|
} else {
|
|
|
|
mChunks.InsertElementAt(0)->SetNull(aDuration);
|
|
|
|
}
|
|
|
|
mDuration += aDuration;
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
void AppendNullData(TrackTime aDuration) override {
|
2012-07-31 16:17:21 +04:00
|
|
|
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;
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void ReplaceWithDisabled() override {
|
2013-08-26 10:07:17 +04:00
|
|
|
if (GetType() != AUDIO) {
|
|
|
|
MOZ_CRASH("Disabling unknown segment type");
|
|
|
|
}
|
2016-08-15 15:19:42 +03:00
|
|
|
ReplaceWithNull();
|
|
|
|
}
|
|
|
|
void ReplaceWithNull() override {
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime duration = GetDuration();
|
2013-08-26 10:07:17 +04:00
|
|
|
Clear();
|
|
|
|
AppendNullData(duration);
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Clear() override {
|
2013-02-25 13:25:07 +04:00
|
|
|
mDuration = 0;
|
2018-03-28 15:39:54 +03:00
|
|
|
mChunks.ClearAndRetainStorage();
|
|
|
|
mChunks.SetCapacity(DEFAULT_SEGMENT_CAPACITY);
|
2013-02-25 13:25:07 +04:00
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2012-10-07 09:34:30 +04:00
|
|
|
class ChunkIterator {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit ChunkIterator(MediaSegmentBase<C, Chunk>& aSegment)
|
2012-10-07 09:34:30 +04:00
|
|
|
: 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]; }
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2012-10-07 09:34:30 +04:00
|
|
|
private:
|
|
|
|
MediaSegmentBase<C, Chunk>& mSegment;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
2015-04-08 08:51:21 +03:00
|
|
|
class ConstChunkIterator {
|
|
|
|
public:
|
|
|
|
explicit ConstChunkIterator(const MediaSegmentBase<C, Chunk>& aSegment)
|
|
|
|
: mSegment(aSegment), mIndex(0) {}
|
|
|
|
bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
|
|
|
|
void Next() { ++mIndex; }
|
|
|
|
const Chunk& operator*() { return mSegment.mChunks[mIndex]; }
|
|
|
|
const Chunk* operator->() { return &mSegment.mChunks[mIndex]; }
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2015-04-08 08:51:21 +03:00
|
|
|
private:
|
|
|
|
const MediaSegmentBase<C, Chunk>& mSegment;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
2012-10-07 09:34:30 +04:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
void RemoveLeading(TrackTime aDuration) { RemoveLeading(aDuration, 0); }
|
2013-10-26 02:13:42 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override {
|
2015-07-29 09:24:24 +03:00
|
|
|
size_t amount = mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
for (size_t i = 0; i < mChunks.Length(); i++) {
|
|
|
|
amount += mChunks[i].SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
|
2014-04-13 22:08:10 +04:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-08-10 05:31:14 +03:00
|
|
|
Chunk* GetLastChunk() {
|
|
|
|
if (mChunks.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &mChunks[mChunks.Length() - 1];
|
|
|
|
}
|
|
|
|
|
2021-02-11 16:38:51 +03:00
|
|
|
const Chunk* GetLastChunk() const {
|
|
|
|
if (mChunks.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &mChunks[mChunks.Length() - 1];
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
protected:
|
2018-03-28 15:39:54 +03:00
|
|
|
explicit MediaSegmentBase(Type aType) : MediaSegment(aType), mChunks() {}
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2017-02-17 16:31:50 +03:00
|
|
|
MediaSegmentBase(MediaSegmentBase&& aSegment)
|
2020-08-04 14:27:07 +03:00
|
|
|
: MediaSegment(std::move(aSegment)),
|
|
|
|
mChunks(std::move(aSegment.mChunks)) {
|
2018-03-28 15:41:11 +03:00
|
|
|
MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
|
|
|
|
"Capacity must be retained in self after swap");
|
|
|
|
MOZ_ASSERT(aSegment.mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
|
|
|
|
"Capacity must be retained in other after swap");
|
|
|
|
}
|
2017-02-17 16:31:50 +03:00
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
/**
|
|
|
|
* Appends the contents of aSource to this segment, clearing aSource.
|
|
|
|
*/
|
|
|
|
void AppendFromInternal(MediaSegmentBase<C, Chunk>* aSource) {
|
2012-11-30 12:08:17 +04:00
|
|
|
MOZ_ASSERT(aSource->mDuration >= 0);
|
2012-07-31 16:17:21 +04:00
|
|
|
mDuration += aSource->mDuration;
|
|
|
|
aSource->mDuration = 0;
|
2018-03-28 15:41:11 +03:00
|
|
|
size_t offset = 0;
|
2012-07-31 16:17:21 +04:00
|
|
|
if (!mChunks.IsEmpty() && !aSource->mChunks.IsEmpty() &&
|
|
|
|
mChunks[mChunks.Length() - 1].CanCombineWithFollowing(
|
|
|
|
aSource->mChunks[0])) {
|
|
|
|
mChunks[mChunks.Length() - 1].mDuration += aSource->mChunks[0].mDuration;
|
2018-03-28 15:41:11 +03:00
|
|
|
offset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; offset < aSource->mChunks.Length(); ++offset) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mChunks.AppendElement(std::move(aSource->mChunks[offset]));
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
2018-03-28 15:41:11 +03:00
|
|
|
|
|
|
|
aSource->mChunks.ClearAndRetainStorage();
|
|
|
|
MOZ_ASSERT(aSource->mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
|
|
|
|
"Capacity must be retained after appending from aSource");
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppendSliceInternal(const MediaSegmentBase<C, Chunk>& aSource,
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime aStart, TrackTime aEnd) {
|
2014-03-24 14:06:06 +04:00
|
|
|
MOZ_ASSERT(aStart <= aEnd, "Endpoints inverted");
|
2016-04-21 12:44:35 +03:00
|
|
|
NS_ASSERTION(aStart >= 0 && aEnd <= aSource.mDuration,
|
|
|
|
"Slice out of range");
|
2012-07-31 16:17:21 +04:00
|
|
|
mDuration += aEnd - aStart;
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime offset = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aSource.mChunks.Length() && offset < aEnd; ++i) {
|
2012-07-31 16:17:21 +04:00
|
|
|
const Chunk& c = aSource.mChunks[i];
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime start = std::max(aStart, offset);
|
|
|
|
TrackTime nextOffset = offset + c.GetDuration();
|
|
|
|
TrackTime end = std::min(aEnd, nextOffset);
|
2012-04-30 07:11:19 +04:00
|
|
|
if (start < end) {
|
2017-05-08 12:39:08 +03:00
|
|
|
if (!mChunks.IsEmpty() &&
|
|
|
|
mChunks[mChunks.Length() - 1].CanCombineWithFollowing(c)) {
|
|
|
|
MOZ_ASSERT(start - offset >= 0 && end - offset <= aSource.mDuration,
|
|
|
|
"Slice out of bounds");
|
|
|
|
mChunks[mChunks.Length() - 1].mDuration += end - start;
|
|
|
|
} else {
|
|
|
|
mChunks.AppendElement(c)->SliceTo(start - offset, end - offset);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
offset = nextOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
Chunk* AppendChunk(TrackTime aDuration) {
|
2012-11-30 12:08:17 +04:00
|
|
|
MOZ_ASSERT(aDuration >= 0);
|
2012-04-30 07:11:19 +04:00
|
|
|
Chunk* c = mChunks.AppendElement();
|
|
|
|
c->mDuration = aDuration;
|
|
|
|
mDuration += aDuration;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
void RemoveLeading(TrackTime aDuration, uint32_t aStartIndex) {
|
2012-04-30 07:11:19 +04:00
|
|
|
NS_ASSERTION(aDuration >= 0, "Can't remove negative duration");
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime t = aDuration;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t chunksToRemove = 0;
|
|
|
|
for (uint32_t i = aStartIndex; i < mChunks.Length() && t > 0; ++i) {
|
2012-04-30 07:11:19 +04:00
|
|
|
Chunk* c = &mChunks[i];
|
|
|
|
if (c->GetDuration() > t) {
|
|
|
|
c->SliceTo(t, c->GetDuration());
|
|
|
|
t = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t -= c->GetDuration();
|
|
|
|
chunksToRemove = i + 1 - aStartIndex;
|
|
|
|
}
|
2018-03-28 15:41:11 +03:00
|
|
|
if (aStartIndex == 0 && chunksToRemove == mChunks.Length()) {
|
|
|
|
mChunks.ClearAndRetainStorage();
|
|
|
|
} else {
|
|
|
|
mChunks.RemoveElementsAt(aStartIndex, chunksToRemove);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
mDuration -= aDuration - t;
|
2018-03-28 15:41:11 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
|
|
|
|
"Capacity must be retained after removing chunks");
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
void RemoveTrailing(TrackTime aKeep, uint32_t aStartIndex) {
|
2014-10-29 17:47:28 +03:00
|
|
|
NS_ASSERTION(aKeep >= 0, "Can't keep negative duration");
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackTime t = aKeep;
|
2014-10-29 17:47:28 +03:00
|
|
|
uint32_t i;
|
2019-11-07 12:58:27 +03:00
|
|
|
for (i = aStartIndex; i < mChunks.Length() && t; ++i) {
|
2014-10-29 17:47:28 +03:00
|
|
|
Chunk* c = &mChunks[i];
|
|
|
|
if (c->GetDuration() > t) {
|
|
|
|
c->SliceTo(0, t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t -= c->GetDuration();
|
|
|
|
}
|
2019-11-07 12:58:27 +03:00
|
|
|
// At this point `i` is already advanced due to last check in the loop.
|
|
|
|
if (i < mChunks.Length()) {
|
2020-06-08 11:50:15 +03:00
|
|
|
mChunks.RemoveLastElements(mChunks.Length() - i);
|
2014-10-29 17:47:28 +03:00
|
|
|
}
|
2018-03-28 15:41:11 +03:00
|
|
|
MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
|
|
|
|
"Capacity must be retained after removing chunks");
|
2014-10-29 17:47:28 +03:00
|
|
|
// Caller must adjust mDuration
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:13:36 +03:00
|
|
|
AutoTArray<Chunk, DEFAULT_SEGMENT_CAPACITY> mChunks;
|
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
|
|
|
|
|
|
|
#endif /* MOZILLA_MEDIASEGMENT_H_ */
|