Bug 1514581 - Don't use async streams with FileMediaResource. r=jya, a=RyanVM
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 18 Dec 2018 17:20:14 +0100
changeset 509106 542014a5898ea48ef886246ba73797977c038fdc
parent 509105 2f5faf2206df5e8fed5b12e4079c5dfe446b1987
child 509107 514d347e259eca8b0ae810565fa517ad7f25b91f
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya, RyanVM
bugs1514581
milestone65.0
Bug 1514581 - Don't use async streams with FileMediaResource. r=jya, a=RyanVM
dom/media/BaseMediaResource.cpp
--- a/dom/media/BaseMediaResource.cpp
+++ b/dom/media/BaseMediaResource.cpp
@@ -2,16 +2,17 @@
 
 #include "ChannelMediaResource.h"
 #include "CloneableWithRangeMediaResource.h"
 #include "FileMediaResource.h"
 #include "MediaContainerType.h"
 #include "mozilla/dom/BlobImpl.h"
 #include "mozilla/dom/BlobURLProtocolHandler.h"
 #include "mozilla/dom/HTMLMediaElement.h"
+#include "mozilla/InputStreamLengthHelper.h"
 #include "nsDebug.h"
 #include "nsError.h"
 #include "nsICloneableInputStream.h"
 #include "nsIFile.h"
 #include "nsIFileChannel.h"
 #include "nsIInputStream.h"
 #include "nsISeekableStream.h"
 #include "nsNetUtil.h"
@@ -54,34 +55,35 @@ already_AddRefed<BaseMediaResource> Base
     IgnoredErrorResult rv;
 
     nsCOMPtr<nsIInputStream> stream;
     blobImpl->CreateInputStream(getter_AddRefs(stream), rv);
     if (NS_WARN_IF(rv.Failed())) {
       return nullptr;
     }
 
-    // It's better to read the size from the blob instead of using ::Available,
-    // because, if the stream implements nsIAsyncInputStream interface,
-    // ::Available will not return the size of the stream, but what can be
-    // currently read.
+    // If this stream knows its own size synchronously, we can still use
+    // FileMediaResource. If the size is known, it means that the reading
+    // doesn't require any async operation.  This is required because
+    // FileMediaResource doesn't work with nsIAsyncInputStreams.
+    int64_t length;
+    if (InputStreamLengthHelper::GetSyncLength(stream, &length) &&
+        length >= 0) {
+      RefPtr<BaseMediaResource> resource =
+          new FileMediaResource(aCallback, aChannel, uri, length);
+      return resource.forget();
+    }
+
+    // Also if the stream doesn't know its own size synchronously, we can still
+    // read the length from the blob.
     uint64_t size = blobImpl->GetSize(rv);
     if (NS_WARN_IF(rv.Failed())) {
       return nullptr;
     }
 
-    // If the URL is a blob URL, with a seekable inputStream, we can still use
-    // a FileMediaResource.
-    nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(stream);
-    if (seekableStream) {
-      RefPtr<BaseMediaResource> resource =
-          new FileMediaResource(aCallback, aChannel, uri, size);
-      return resource.forget();
-    }
-
     // Maybe this blob URL can be cloned with a range.
     nsCOMPtr<nsICloneableInputStreamWithRange> cloneableWithRange =
         do_QueryInterface(stream);
     if (cloneableWithRange) {
       RefPtr<BaseMediaResource> resource = new CloneableWithRangeMediaResource(
           aCallback, aChannel, uri, stream, size);
       return resource.forget();
     }