Bug 1457661 - P2. Ensure we call NotifyDataEnded for local resource once size is known. r?bryce draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 28 May 2018 23:06:38 +0200
changeset 800937 e077dcd603f069204889342d3a9dc84ffeaa1805
parent 800936 6dbd813ec3887a93e0030bffc118e3ece9f4a31f
push id111523
push userbmo:jyavenard@mozilla.com
push dateTue, 29 May 2018 14:28:02 +0000
reviewersbryce
bugs1457661
milestone62.0a1
Bug 1457661 - P2. Ensure we call NotifyDataEnded for local resource once size is known. r?bryce A call to NotifyDataEnded is required even if the size was known when the resource was created. This ensures that the readyState is properly updated and that playback can immediately as no more data can be added once first loaded. MozReview-Commit-ID: FaJMBxJ9NkM
dom/media/FileMediaResource.cpp
dom/media/FileMediaResource.h
--- a/dom/media/FileMediaResource.cpp
+++ b/dom/media/FileMediaResource.cpp
@@ -14,31 +14,37 @@
 
 namespace mozilla {
 
 void
 FileMediaResource::EnsureSizeInitialized()
 {
   mLock.AssertCurrentThreadOwns();
   NS_ASSERTION(mInput, "Must have file input stream");
-  if (mSizeInitialized) {
+  if (mSizeInitialized && mNotifyDataEndedProcessed) {
     return;
   }
+
+  if (!mSizeInitialized) {
+    // Get the file size and inform the decoder.
+    uint64_t size;
+    nsresult res = mInput->Available(&size);
+    if (NS_SUCCEEDED(res) && size <= INT64_MAX) {
+      mSize = (int64_t)size;
+    }
+  }
   mSizeInitialized = true;
-  // Get the file size and inform the decoder.
-  uint64_t size;
-  nsresult res = mInput->Available(&size);
-  if (NS_SUCCEEDED(res) && size <= INT64_MAX) {
-    mSize = (int64_t)size;
+  if (!mNotifyDataEndedProcessed && mSize >= 0) {
     mCallback->AbstractMainThread()->Dispatch(
       NewRunnableMethod<nsresult>("MediaResourceCallback::NotifyDataEnded",
                                   mCallback.get(),
                                   &MediaResourceCallback::NotifyDataEnded,
                                   NS_OK));
   }
+  mNotifyDataEndedProcessed = true;
 }
 
 nsresult
 FileMediaResource::GetCachedRanges(MediaByteRangeSet& aRanges)
 {
   MutexAutoLock lock(mLock);
 
   EnsureSizeInitialized();
--- a/dom/media/FileMediaResource.h
+++ b/dom/media/FileMediaResource.h
@@ -128,13 +128,16 @@ private:
   // Input stream for the media data. This can be used from any
   // thread.
   nsCOMPtr<nsIInputStream>  mInput;
 
   // Whether we've attempted to initialize mSize. Note that mSize can be -1
   // when mSizeInitialized is true if we tried and failed to get the size
   // of the file.
   bool mSizeInitialized;
+  // Set to true if NotifyDataEnded callback has been processed (which only
+  // occurs if resource size is known)
+  bool mNotifyDataEndedProcessed = false;
 };
 
 } // namespace mozilla
 
 #endif // mozilla_dom_media_FileMediaResource_h