Bug 1280613 - P2. Add experimental SourceBuffer.removeAsync. r=bz
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 11 May 2018 04:07:24 +0200
changeset 472687 bc87e9eef2b87159f95552163901225efd07d40d
parent 472686 e5bf2e9ff60a28306fcdc73cf82f13faa4f1ce4b
child 472688 38d9226183f0762e3182b16d0c1927e912961f47
push id9374
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:43:20 +0000
treeherdermozilla-beta@160e085dfb0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1280613
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1280613 - P2. Add experimental SourceBuffer.removeAsync. r=bz The aim of those changes is to be incubated in the WICG. MozReview-Commit-ID: K93xiTod6tw
dom/media/mediasource/SourceBuffer.cpp
dom/media/mediasource/SourceBuffer.h
dom/webidl/SourceBuffer.webidl
--- a/dom/media/mediasource/SourceBuffer.cpp
+++ b/dom/media/mediasource/SourceBuffer.cpp
@@ -280,16 +280,65 @@ SourceBuffer::ResetParserState()
 
 void
 SourceBuffer::Remove(double aStart, double aEnd, ErrorResult& aRv)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MSE_API("Remove(aStart=%f, aEnd=%f)", aStart, aEnd);
   DDLOG(DDLogCategory::API, "Remove-from", aStart);
   DDLOG(DDLogCategory::API, "Remove-until", aEnd);
+
+  PrepareRemove(aStart, aEnd, aRv);
+  if (aRv.Failed()) {
+    return;
+  }
+  RangeRemoval(aStart, aEnd);
+}
+
+already_AddRefed<Promise>
+SourceBuffer::RemoveAsync(double aStart, double aEnd, ErrorResult& aRv)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MSE_API("RemoveAsync(aStart=%f, aEnd=%f)", aStart, aEnd);
+  DDLOG(DDLogCategory::API, "Remove-from", aStart);
+  DDLOG(DDLogCategory::API, "Remove-until", aEnd);
+
+  if (!IsAttached()) {
+    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
+    return nullptr;
+  }
+
+  nsCOMPtr<nsIGlobalObject> parentObject =
+    do_QueryInterface(mMediaSource->GetParentObject());
+  if (!parentObject) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+
+  RefPtr<Promise> promise = Promise::Create(parentObject, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  PrepareRemove(aStart, aEnd, aRv);
+
+  if (aRv.Failed()) {
+    // The bindings will automatically return a rejected promise.
+    return nullptr;
+  }
+  MOZ_ASSERT(!mDOMPromise, "Can't have a pending operation going");
+  mDOMPromise = promise;
+  RangeRemoval(aStart, aEnd);
+
+  return promise.forget();
+}
+
+void
+SourceBuffer::PrepareRemove(double aStart, double aEnd, ErrorResult& aRv)
+{
   if (!IsAttached()) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
   if (mUpdating) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
@@ -297,18 +346,16 @@ SourceBuffer::Remove(double aStart, doub
       aStart < 0 || aStart > mMediaSource->Duration() ||
       aEnd <= aStart || IsNaN(aEnd)) {
     aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
     return;
   }
   if (mMediaSource->ReadyState() == MediaSourceReadyState::Ended) {
     mMediaSource->SetReadyState(MediaSourceReadyState::Open);
   }
-
-  RangeRemoval(aStart, aEnd);
 }
 
 void
 SourceBuffer::RangeRemoval(double aStart, double aEnd)
 {
   StartUpdating();
 
   RefPtr<SourceBuffer> self = this;
--- a/dom/media/mediasource/SourceBuffer.h
+++ b/dom/media/mediasource/SourceBuffer.h
@@ -93,16 +93,20 @@ public:
   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);
 
   IMPL_EVENT_HANDLER(updatestart);
   IMPL_EVENT_HANDLER(update);
   IMPL_EVENT_HANDLER(updateend);
   IMPL_EVENT_HANDLER(error);
   IMPL_EVENT_HANDLER(abort);
 
@@ -160,16 +164,18 @@ private:
   // 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
   // http://w3c.github.io/media-source/#sourcebuffer-append-error
   void AppendError(const MediaResult& aDecodeError);
 
   // Implements the "Prepare Append Algorithm". Returns MediaByteBuffer object
   // on success or nullptr (with aRv set) on error.
--- a/dom/webidl/SourceBuffer.webidl
+++ b/dom/webidl/SourceBuffer.webidl
@@ -48,12 +48,16 @@ interface SourceBuffer : EventTarget {
   Promise<void> appendBufferAsync(ArrayBufferView data);
   //[Throws]
   //void appendStream(Stream stream, [EnforceRange] optional unsigned long long maxSize);
   [Throws]
   void abort();
   [Throws]
   void remove(double start, unrestricted double end);
   // Experimental function as proposed in:
+  // https://github.com/w3c/media-source/issues/100 for promise proposal.
+  [Throws, Func="mozilla::dom::MediaSource::ExperimentalEnabled"]
+  Promise<void> removeAsync(double start, unrestricted double end);
+  // Experimental function as proposed in:
   // https://github.com/w3c/media-source/issues/155
   [Throws, Func="mozilla::dom::MediaSource::ExperimentalEnabled"]
   void changeType(DOMString type);
 };