Bug 1280613 - P2. Add experimental SourceBuffer.removeAsync. r?bz draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 11 May 2018 04:07:24 +0200
changeset 794578 bd59c1348d359ab57e7d5b3c0e4e5df60ab5eeef
parent 794577 9680a80024e9388d8ab4e344a6890a336a438441
child 794579 ed2e718f6a0e2576a86178f05fb7f7d5881e8217
push id109720
push userbmo:jyavenard@mozilla.com
push dateSun, 13 May 2018 14:54:13 +0000
reviewersbz
bugs1280613
milestone62.0a1
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);
 };