content/media/MediaSegment.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/MediaSegment.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,374 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_MEDIASEGMENT_H_
    1.10 +#define MOZILLA_MEDIASEGMENT_H_
    1.11 +
    1.12 +#include "nsTArray.h"
    1.13 +#ifdef MOZILLA_INTERNAL_API
    1.14 +#include "mozilla/TimeStamp.h"
    1.15 +#endif
    1.16 +#include <algorithm>
    1.17 +#include "Latency.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +
    1.21 +/**
    1.22 + * We represent media times in 64-bit fixed point. So 1 MediaTime is
    1.23 + * 1/(2^MEDIA_TIME_FRAC_BITS) seconds.
    1.24 + */
    1.25 +typedef int64_t MediaTime;
    1.26 +const int64_t MEDIA_TIME_FRAC_BITS = 20;
    1.27 +const int64_t MEDIA_TIME_MAX = INT64_MAX;
    1.28 +
    1.29 +inline MediaTime MillisecondsToMediaTime(int32_t aMS)
    1.30 +{
    1.31 +  return (MediaTime(aMS) << MEDIA_TIME_FRAC_BITS)/1000;
    1.32 +}
    1.33 +
    1.34 +inline MediaTime SecondsToMediaTime(double aS)
    1.35 +{
    1.36 +  NS_ASSERTION(aS <= (MEDIA_TIME_MAX >> MEDIA_TIME_FRAC_BITS),
    1.37 +               "Out of range");
    1.38 +  return MediaTime(aS * (1 << MEDIA_TIME_FRAC_BITS));
    1.39 +}
    1.40 +
    1.41 +inline double MediaTimeToSeconds(MediaTime aTime)
    1.42 +{
    1.43 +  return aTime*(1.0/(1 << MEDIA_TIME_FRAC_BITS));
    1.44 +}
    1.45 +
    1.46 +inline int64_t MediaTimeToMicroseconds(MediaTime aTime)
    1.47 +{
    1.48 +  return aTime*(1000000.0/(1 << MEDIA_TIME_FRAC_BITS));
    1.49 +}
    1.50 +
    1.51 +/**
    1.52 + * A number of ticks at a rate determined by some underlying track (e.g.
    1.53 + * audio sample rate). We want to make sure that multiplying TrackTicks by
    1.54 + * 2^MEDIA_TIME_FRAC_BITS doesn't overflow, so we set its max accordingly.
    1.55 + */
    1.56 +typedef int64_t TrackTicks;
    1.57 +const int64_t TRACK_TICKS_MAX = INT64_MAX >> MEDIA_TIME_FRAC_BITS;
    1.58 +
    1.59 +/**
    1.60 + * A MediaSegment is a chunk of media data sequential in time. Different
    1.61 + * types of data have different subclasses of MediaSegment, all inheriting
    1.62 + * from MediaSegmentBase.
    1.63 + * All MediaSegment data is timed using TrackTicks. The actual tick rate
    1.64 + * is defined on a per-track basis. For some track types, this can be
    1.65 + * a fixed constant for all tracks of that type (e.g. 1MHz for video).
    1.66 + *
    1.67 + * Each media segment defines a concept of "null media data" (e.g. silence
    1.68 + * for audio or "no video frame" for video), which can be efficiently
    1.69 + * represented. This is used for padding.
    1.70 + */
    1.71 +class MediaSegment {
    1.72 +public:
    1.73 +  virtual ~MediaSegment()
    1.74 +  {
    1.75 +    MOZ_COUNT_DTOR(MediaSegment);
    1.76 +  }
    1.77 +
    1.78 +  enum Type {
    1.79 +    AUDIO,
    1.80 +    VIDEO,
    1.81 +    TYPE_COUNT
    1.82 +  };
    1.83 +
    1.84 +  /**
    1.85 +   * Gets the total duration of the segment.
    1.86 +   */
    1.87 +  TrackTicks GetDuration() const { return mDuration; }
    1.88 +  Type GetType() const { return mType; }
    1.89 +
    1.90 +  /**
    1.91 +   * Create a MediaSegment of the same type.
    1.92 +   */
    1.93 +  virtual MediaSegment* CreateEmptyClone() const = 0;
    1.94 +  /**
    1.95 +   * Moves contents of aSource to the end of this segment.
    1.96 +   */
    1.97 +  virtual void AppendFrom(MediaSegment* aSource) = 0;
    1.98 +  /**
    1.99 +   * Append a slice of aSource to this segment.
   1.100 +   */
   1.101 +  virtual void AppendSlice(const MediaSegment& aSource,
   1.102 +                           TrackTicks aStart, TrackTicks aEnd) = 0;
   1.103 +  /**
   1.104 +   * Replace all contents up to aDuration with null data.
   1.105 +   */
   1.106 +  virtual void ForgetUpTo(TrackTicks aDuration) = 0;
   1.107 +  /**
   1.108 +   * Insert aDuration of null data at the start of the segment.
   1.109 +   */
   1.110 +  virtual void InsertNullDataAtStart(TrackTicks aDuration) = 0;
   1.111 +  /**
   1.112 +   * Insert aDuration of null data at the end of the segment.
   1.113 +   */
   1.114 +  virtual void AppendNullData(TrackTicks aDuration) = 0;
   1.115 +  /**
   1.116 +   * Replace contents with disabled data of the same duration
   1.117 +   */
   1.118 +  virtual void ReplaceWithDisabled() = 0;
   1.119 +  /**
   1.120 +   * Remove all contents, setting duration to 0.
   1.121 +   */
   1.122 +  virtual void Clear() = 0;
   1.123 +
   1.124 +  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
   1.125 +  {
   1.126 +    return 0;
   1.127 +  }
   1.128 +
   1.129 +  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
   1.130 +  {
   1.131 +    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
   1.132 +  }
   1.133 +
   1.134 +protected:
   1.135 +  MediaSegment(Type aType) : mDuration(0), mType(aType)
   1.136 +  {
   1.137 +    MOZ_COUNT_CTOR(MediaSegment);
   1.138 +  }
   1.139 +
   1.140 +  TrackTicks mDuration; // total of mDurations of all chunks
   1.141 +  Type mType;
   1.142 +};
   1.143 +
   1.144 +/**
   1.145 + * C is the implementation class subclassed from MediaSegmentBase.
   1.146 + * C must contain a Chunk class.
   1.147 + */
   1.148 +template <class C, class Chunk> class MediaSegmentBase : public MediaSegment {
   1.149 +public:
   1.150 +  virtual MediaSegment* CreateEmptyClone() const
   1.151 +  {
   1.152 +    return new C();
   1.153 +  }
   1.154 +  virtual void AppendFrom(MediaSegment* aSource)
   1.155 +  {
   1.156 +    NS_ASSERTION(aSource->GetType() == C::StaticType(), "Wrong type");
   1.157 +    AppendFromInternal(static_cast<C*>(aSource));
   1.158 +  }
   1.159 +  void AppendFrom(C* aSource)
   1.160 +  {
   1.161 +    AppendFromInternal(aSource);
   1.162 +  }
   1.163 +  virtual void AppendSlice(const MediaSegment& aSource,
   1.164 +                           TrackTicks aStart, TrackTicks aEnd)
   1.165 +  {
   1.166 +    NS_ASSERTION(aSource.GetType() == C::StaticType(), "Wrong type");
   1.167 +    AppendSliceInternal(static_cast<const C&>(aSource), aStart, aEnd);
   1.168 +  }
   1.169 +  void AppendSlice(const C& aOther, TrackTicks aStart, TrackTicks aEnd)
   1.170 +  {
   1.171 +    AppendSliceInternal(aOther, aStart, aEnd);
   1.172 +  }
   1.173 +  /**
   1.174 +   * Replace the first aDuration ticks with null media data, because the data
   1.175 +   * will not be required again.
   1.176 +   */
   1.177 +  virtual void ForgetUpTo(TrackTicks aDuration)
   1.178 +  {
   1.179 +    if (mChunks.IsEmpty() || aDuration <= 0) {
   1.180 +      return;
   1.181 +    }
   1.182 +    if (mChunks[0].IsNull()) {
   1.183 +      TrackTicks extraToForget = std::min(aDuration, mDuration) - mChunks[0].GetDuration();
   1.184 +      if (extraToForget > 0) {
   1.185 +        RemoveLeading(extraToForget, 1);
   1.186 +        mChunks[0].mDuration += extraToForget;
   1.187 +        mDuration += extraToForget;
   1.188 +      }
   1.189 +      return;
   1.190 +    }
   1.191 +    RemoveLeading(aDuration, 0);
   1.192 +    mChunks.InsertElementAt(0)->SetNull(aDuration);
   1.193 +    mDuration += aDuration;
   1.194 +  }
   1.195 +  virtual void InsertNullDataAtStart(TrackTicks aDuration)
   1.196 +  {
   1.197 +    if (aDuration <= 0) {
   1.198 +      return;
   1.199 +    }
   1.200 +    if (!mChunks.IsEmpty() && mChunks[0].IsNull()) {
   1.201 +      mChunks[0].mDuration += aDuration;
   1.202 +    } else {
   1.203 +      mChunks.InsertElementAt(0)->SetNull(aDuration);
   1.204 +    }
   1.205 +#ifdef MOZILLA_INTERNAL_API
   1.206 +    mChunks[0].mTimeStamp = mozilla::TimeStamp::Now();
   1.207 +#endif
   1.208 +    mDuration += aDuration;
   1.209 +  }
   1.210 +  virtual void AppendNullData(TrackTicks aDuration)
   1.211 +  {
   1.212 +    if (aDuration <= 0) {
   1.213 +      return;
   1.214 +    }
   1.215 +    if (!mChunks.IsEmpty() && mChunks[mChunks.Length() - 1].IsNull()) {
   1.216 +      mChunks[mChunks.Length() - 1].mDuration += aDuration;
   1.217 +    } else {
   1.218 +      mChunks.AppendElement()->SetNull(aDuration);
   1.219 +    }
   1.220 +    mDuration += aDuration;
   1.221 +  }
   1.222 +  virtual void ReplaceWithDisabled()
   1.223 +  {
   1.224 +    if (GetType() != AUDIO) {
   1.225 +      MOZ_CRASH("Disabling unknown segment type");
   1.226 +    }
   1.227 +    TrackTicks duration = GetDuration();
   1.228 +    Clear();
   1.229 +    AppendNullData(duration);
   1.230 +  }
   1.231 +  virtual void Clear()
   1.232 +  {
   1.233 +    mDuration = 0;
   1.234 +    mChunks.Clear();
   1.235 +  }
   1.236 +
   1.237 +  class ChunkIterator {
   1.238 +  public:
   1.239 +    ChunkIterator(MediaSegmentBase<C, Chunk>& aSegment)
   1.240 +      : mSegment(aSegment), mIndex(0) {}
   1.241 +    bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
   1.242 +    void Next() { ++mIndex; }
   1.243 +    Chunk& operator*() { return mSegment.mChunks[mIndex]; }
   1.244 +    Chunk* operator->() { return &mSegment.mChunks[mIndex]; }
   1.245 +  private:
   1.246 +    MediaSegmentBase<C, Chunk>& mSegment;
   1.247 +    uint32_t mIndex;
   1.248 +  };
   1.249 +
   1.250 +  void RemoveLeading(TrackTicks aDuration)
   1.251 +  {
   1.252 +    RemoveLeading(aDuration, 0);
   1.253 +  }
   1.254 +
   1.255 +#ifdef MOZILLA_INTERNAL_API
   1.256 +  void GetStartTime(TimeStamp &aTime) {
   1.257 +    aTime = mChunks[0].mTimeStamp;
   1.258 +  }
   1.259 +#endif
   1.260 +
   1.261 +  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
   1.262 +  {
   1.263 +    size_t amount = mChunks.SizeOfExcludingThis(aMallocSizeOf);
   1.264 +    for (size_t i = 0; i < mChunks.Length(); i++) {
   1.265 +      amount += mChunks[i].SizeOfExcludingThisIfUnshared(aMallocSizeOf);
   1.266 +    }
   1.267 +    return amount;
   1.268 +  }
   1.269 +
   1.270 +  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
   1.271 +  {
   1.272 +    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
   1.273 +  }
   1.274 +
   1.275 +protected:
   1.276 +  MediaSegmentBase(Type aType) : MediaSegment(aType) {}
   1.277 +
   1.278 +  /**
   1.279 +   * Appends the contents of aSource to this segment, clearing aSource.
   1.280 +   */
   1.281 +  void AppendFromInternal(MediaSegmentBase<C, Chunk>* aSource)
   1.282 +  {
   1.283 +    MOZ_ASSERT(aSource->mDuration >= 0);
   1.284 +    mDuration += aSource->mDuration;
   1.285 +    aSource->mDuration = 0;
   1.286 +    if (!mChunks.IsEmpty() && !aSource->mChunks.IsEmpty() &&
   1.287 +        mChunks[mChunks.Length() - 1].CanCombineWithFollowing(aSource->mChunks[0])) {
   1.288 +      mChunks[mChunks.Length() - 1].mDuration += aSource->mChunks[0].mDuration;
   1.289 +      aSource->mChunks.RemoveElementAt(0);
   1.290 +    }
   1.291 +    mChunks.MoveElementsFrom(aSource->mChunks);
   1.292 +  }
   1.293 +
   1.294 +  void AppendSliceInternal(const MediaSegmentBase<C, Chunk>& aSource,
   1.295 +                           TrackTicks aStart, TrackTicks aEnd)
   1.296 +  {
   1.297 +    MOZ_ASSERT(aStart <= aEnd, "Endpoints inverted");
   1.298 +    NS_WARN_IF_FALSE(aStart >= 0 && aEnd <= aSource.mDuration, "Slice out of range");
   1.299 +    mDuration += aEnd - aStart;
   1.300 +    TrackTicks offset = 0;
   1.301 +    for (uint32_t i = 0; i < aSource.mChunks.Length() && offset < aEnd; ++i) {
   1.302 +      const Chunk& c = aSource.mChunks[i];
   1.303 +      TrackTicks start = std::max(aStart, offset);
   1.304 +      TrackTicks nextOffset = offset + c.GetDuration();
   1.305 +      TrackTicks end = std::min(aEnd, nextOffset);
   1.306 +      if (start < end) {
   1.307 +        mChunks.AppendElement(c)->SliceTo(start - offset, end - offset);
   1.308 +      }
   1.309 +      offset = nextOffset;
   1.310 +    }
   1.311 +  }
   1.312 +
   1.313 +  Chunk* AppendChunk(TrackTicks aDuration)
   1.314 +  {
   1.315 +    MOZ_ASSERT(aDuration >= 0);
   1.316 +    Chunk* c = mChunks.AppendElement();
   1.317 +    c->mDuration = aDuration;
   1.318 +    mDuration += aDuration;
   1.319 +    return c;
   1.320 +  }
   1.321 +
   1.322 +  Chunk* FindChunkContaining(TrackTicks aOffset, TrackTicks* aStart = nullptr)
   1.323 +  {
   1.324 +    if (aOffset < 0) {
   1.325 +      return nullptr;
   1.326 +    }
   1.327 +    TrackTicks offset = 0;
   1.328 +    for (uint32_t i = 0; i < mChunks.Length(); ++i) {
   1.329 +      Chunk& c = mChunks[i];
   1.330 +      TrackTicks nextOffset = offset + c.GetDuration();
   1.331 +      if (aOffset < nextOffset) {
   1.332 +        if (aStart) {
   1.333 +          *aStart = offset;
   1.334 +        }
   1.335 +        return &c;
   1.336 +      }
   1.337 +      offset = nextOffset;
   1.338 +    }
   1.339 +    return nullptr;
   1.340 +  }
   1.341 +
   1.342 +  Chunk* GetLastChunk()
   1.343 +  {
   1.344 +    if (mChunks.IsEmpty()) {
   1.345 +      return nullptr;
   1.346 +    }
   1.347 +    return &mChunks[mChunks.Length() - 1];
   1.348 +  }
   1.349 +
   1.350 +  void RemoveLeading(TrackTicks aDuration, uint32_t aStartIndex)
   1.351 +  {
   1.352 +    NS_ASSERTION(aDuration >= 0, "Can't remove negative duration");
   1.353 +    TrackTicks t = aDuration;
   1.354 +    uint32_t chunksToRemove = 0;
   1.355 +    for (uint32_t i = aStartIndex; i < mChunks.Length() && t > 0; ++i) {
   1.356 +      Chunk* c = &mChunks[i];
   1.357 +      if (c->GetDuration() > t) {
   1.358 +        c->SliceTo(t, c->GetDuration());
   1.359 +        t = 0;
   1.360 +        break;
   1.361 +      }
   1.362 +      t -= c->GetDuration();
   1.363 +      chunksToRemove = i + 1 - aStartIndex;
   1.364 +    }
   1.365 +    mChunks.RemoveElementsAt(aStartIndex, chunksToRemove);
   1.366 +    mDuration -= aDuration - t;
   1.367 +  }
   1.368 +
   1.369 +  nsTArray<Chunk> mChunks;
   1.370 +#ifdef MOZILLA_INTERNAL_API
   1.371 +  mozilla::TimeStamp mTimeStamp;
   1.372 +#endif
   1.373 +};
   1.374 +
   1.375 +}
   1.376 +
   1.377 +#endif /* MOZILLA_MEDIASEGMENT_H_ */

mercurial