author Sylvestre Ledru <>
Mon, 19 Nov 2018 13:25:37 +0000
changeset 446960 0ceae9db9ec0be18daa1a279511ad305723185d4
parent 418469 bc87e9eef2b87159f95552163901225efd07d40d
permissions -rw-r--r--
Bug 1204606 - Reformat of dom/media r=jya # skip-blame Differential Revision:

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 */

#ifndef mozilla_dom_SourceBuffer_h_
#define mozilla_dom_SourceBuffer_h_

#include "mozilla/MozPromise.h"
#include "MediaContainerType.h"
#include "MediaSource.h"
#include "js/RootingAPI.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/SourceBufferBinding.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/mozalloc.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionNoteChild.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nscore.h"
#include "TrackBuffersManager.h"
#include "SourceBufferTask.h"

class JSObject;
struct JSContext;

namespace mozilla {

class AbstractThread;
class ErrorResult;
class MediaByteBuffer;
template <typename T>
class AsyncEventRunner;


namespace dom {

class TimeRanges;

class SourceBuffer final : public DOMEventTargetHelper,
                           public DecoderDoctorLifeLogger<SourceBuffer> {
  /** WebIDL Methods. */
  SourceBufferAppendMode Mode() const {
    return mCurrentAttributes.GetAppendMode();

  void SetMode(SourceBufferAppendMode aMode, ErrorResult& aRv);

  bool Updating() const { return mUpdating; }

  TimeRanges* GetBuffered(ErrorResult& aRv);
  media::TimeIntervals GetTimeIntervals();

  double TimestampOffset() const {
    return mCurrentAttributes.GetApparentTimestampOffset();

  void SetTimestampOffset(double aTimestampOffset, ErrorResult& aRv);

  double AppendWindowStart() const {
    return mCurrentAttributes.GetAppendWindowStart();

  void SetAppendWindowStart(double aAppendWindowStart, ErrorResult& aRv);

  double AppendWindowEnd() const {
    return mCurrentAttributes.GetAppendWindowEnd();

  void SetAppendWindowEnd(double aAppendWindowEnd, ErrorResult& aRv);

  void AppendBuffer(const ArrayBuffer& aData, ErrorResult& aRv);
  void AppendBuffer(const ArrayBufferView& aData, ErrorResult& aRv);

  already_AddRefed<Promise> AppendBufferAsync(const ArrayBuffer& aData,
                                              ErrorResult& aRv);
  already_AddRefed<Promise> AppendBufferAsync(const ArrayBufferView& aData,
                                              ErrorResult& aRv);

  void Abort(ErrorResult& aRv);
  void AbortBufferAppend();

  void Remove(double aStart, double aEnd, ErrorResult& aRv);

  already_AddRefed<Promise> RemoveAsync(double aStart, double aEnd,
                                        ErrorResult& aRv);

  void ChangeType(const nsAString& aType, ErrorResult& aRv);


  /** End WebIDL Methods. */


  SourceBuffer(MediaSource* aMediaSource, const MediaContainerType& aType);

  MediaSource* GetParentObject() const;

  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override;

  // Notify the SourceBuffer that it has been detached from the
  // MediaSource's sourceBuffer list.
  void Detach();
  bool IsAttached() const { return mMediaSource != nullptr; }

  void Ended();

  double GetBufferedStart();
  double GetBufferedEnd();
  double HighestStartTime();
  double HighestEndTime();

  // Runs the range removal algorithm as defined by the MSE spec.
  void RangeRemoval(double aStart, double aEnd);

  bool IsActive() const { return mActive; }


  friend class AsyncEventRunner<SourceBuffer>;
  friend class BufferAppendRunnable;
  friend class mozilla::TrackBuffersManager;
  void DispatchSimpleEvent(const char* aName);
  void QueueAsyncSimpleEvent(const char* aName);

  // Update mUpdating and fire the appropriate events.
  void StartUpdating();
  void StopUpdating();
  void AbortUpdating();
  void ResetParserState();

  // If the media segment contains data beyond the current duration,
  // then run the duration change algorithm with new duration set to the
  // maximum of the current duration and the group end timestamp.
  void CheckEndTime();

  // Shared implementation of AppendBuffer overloads.
  void AppendData(const uint8_t* aData, uint32_t aLength, ErrorResult& aRv);
  // Shared implementation of AppendBufferAsync overloads.
  already_AddRefed<Promise> AppendDataAsync(const uint8_t* aData,
                                            uint32_t aLength, ErrorResult& aRv);

  void PrepareRemove(double aStart, double aEnd, ErrorResult& aRv);

  // Implement the "Append Error Algorithm".
  // Will call endOfStream() with "decode" error if aDecodeError is true.
  // 3.5.3 Append Error Algorithm
  void AppendError(const MediaResult& aDecodeError);

  // Implements the "Prepare Append Algorithm". Returns MediaByteBuffer object
  // on success or nullptr (with aRv set) on error.
  already_AddRefed<MediaByteBuffer> PrepareAppend(const uint8_t* aData,
                                                  uint32_t aLength,
                                                  ErrorResult& aRv);

  void AppendDataCompletedWithSuccess(
      const SourceBufferTask::AppendBufferResult& aResult);
  void AppendDataErrored(const MediaResult& aError);

  RefPtr<MediaSource> mMediaSource;
  const RefPtr<AbstractThread> mAbstractMainThread;

  RefPtr<TrackBuffersManager> mTrackBuffersManager;
  SourceBufferAttributes mCurrentAttributes;

  bool mUpdating;

  mozilla::Atomic<bool> mActive;

  MozPromiseRequestHolder<SourceBufferTask::AppendPromise> mPendingAppend;
  MediaContainerType mType;

  RefPtr<TimeRanges> mBuffered;


  // Only used if MSE v2 experimental mode is active.
  // Contains the current Promise to be resolved following use of
  // appendBufferAsync and removeAsync. Not set of no operation is pending.
  RefPtr<Promise> mDOMPromise;

}  // namespace dom

}  // namespace mozilla

#endif /* mozilla_dom_SourceBuffer_h_ */