Bug 1418219 - merge NotifyDataLength() and NotifyDataStarted(). draft
authorJW Wang <jwwang@mozilla.com>
Thu, 16 Nov 2017 12:05:19 +0800
changeset 701075 4cab91bbb864a8737f4fc75ebd35fd46c711246c
parent 701074 73076fbf0fae87a2d140c572df10013a30c308f1
child 701104 abaf1533a24654f0cae0645c00cfbf3a325abe86
push id90067
push userjwwang@mozilla.com
push dateTue, 21 Nov 2017 07:44:54 +0000
bugs1418219
milestone59.0a1
Bug 1418219 - merge NotifyDataLength() and NotifyDataStarted(). MozReview-Commit-ID: g857eQ4YVK
dom/media/ChannelMediaResource.cpp
dom/media/MediaCache.cpp
dom/media/MediaCache.h
--- a/dom/media/ChannelMediaResource.cpp
+++ b/dom/media/ChannelMediaResource.cpp
@@ -187,16 +187,17 @@ ChannelMediaResource::OnStartRequest(nsI
     if (status == NS_ERROR_DOM_BAD_URI) {
       mCallback->NotifyNetworkError(MediaResult(status, "CORS not allowed"));
       return NS_ERROR_DOM_BAD_URI;
     }
   }
 
   nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(aRequest);
   bool seekable = false;
+  int64_t length = -1;
   int64_t startOffset = aRequestOffset;
 
   if (hc) {
     uint32_t responseStatus = 0;
     Unused << hc->GetResponseStatus(&responseStatus);
     bool succeeded = false;
     Unused << hc->GetRequestSucceeded(&succeeded);
 
@@ -270,17 +271,17 @@ ChannelMediaResource::OnStartRequest(nsI
         // If HTTP_OK_CODE is responded for a non-zero range request, we have
         // to assume seeking doesn't work.
         acceptsRanges = false;
       }
     }
     if (aRequestOffset == 0 && contentLength >= 0 &&
         (responseStatus == HTTP_OK_CODE ||
          responseStatus == HTTP_PARTIAL_RESPONSE_CODE)) {
-      mCacheStream.NotifyDataLength(contentLength);
+      length = contentLength;
     }
     // XXX we probably should examine the Content-Range header in case
     // the server gave us a range which is not quite what we asked for
 
     // If we get an HTTP_OK_CODE response to our byte range request,
     // and the server isn't sending Accept-Ranges:bytes then we don't
     // support seeking. We also can't seek in compressed streams.
     seekable = !isCompressed && acceptsRanges;
@@ -289,17 +290,17 @@ ChannelMediaResource::OnStartRequest(nsI
     startOffset = 0;
   }
 
   // Update principals before OnDataAvailable() putting the data in the cache.
   // This is important, we want to make sure all principals are updated before
   // any consumer can see the new data.
   UpdatePrincipal();
 
-  mCacheStream.NotifyDataStarted(mLoadID, startOffset, seekable);
+  mCacheStream.NotifyDataStarted(mLoadID, startOffset, seekable, length);
   mIsTransportSeekable = seekable;
   mChannelStatistics.Start();
 
   mSuspendAgent.Delegate(mChannel);
 
   // Fires an initial progress event.
   owner->DownloadProgressed();
 
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -1929,46 +1929,44 @@ MediaCache::NoteSeek(MediaCacheStream* a
         }
       }
       --endIndex;
     }
   }
 }
 
 void
-MediaCacheStream::NotifyDataLength(int64_t aLength)
-{
-  NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
-  LOG("Stream %p DataLength: %" PRId64, this, aLength);
-
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
-  mStreamLength = aLength;
-}
-
-void
 MediaCacheStream::NotifyLoadID(uint32_t aLoadID)
 {
   MOZ_ASSERT(aLoadID > 0);
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   mLoadID = aLoadID;
 }
 
 void
 MediaCacheStream::NotifyDataStarted(uint32_t aLoadID,
                                     int64_t aOffset,
-                                    bool aSeekable)
+                                    bool aSeekable,
+                                    int64_t aLength)
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
   MOZ_ASSERT(aLoadID > 0);
-  LOG("Stream %p DataStarted: %" PRId64 " aLoadID=%u", this, aOffset, aLoadID);
+  LOG("Stream %p DataStarted: %" PRId64 " aLoadID=%u aLength=%" PRId64,
+      this,
+      aOffset,
+      aLoadID,
+      aLength);
 
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   NS_WARNING_ASSERTION(aOffset == mSeekTarget || aOffset == mChannelOffset,
                        "Server is giving us unexpected offset");
   MOZ_ASSERT(aOffset >= 0);
+  if (aLength >= 0) {
+    mStreamLength = aLength;
+  }
   mChannelOffset = aOffset;
   if (mStreamLength >= 0) {
     // If we started reading at a certain offset, then for sure
     // the stream is at least that long.
     mStreamLength = std::max(mStreamLength, mChannelOffset);
   }
   mLoadID = aLoadID;
 
@@ -2722,17 +2720,17 @@ MediaCacheStream::ReadFromCache(char* aB
 nsresult
 MediaCacheStream::Init(int64_t aContentLength)
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
   MOZ_ASSERT(!mMediaCache, "Has been initialized.");
 
   if (aContentLength > 0) {
     uint32_t length = uint32_t(std::min(aContentLength, int64_t(UINT32_MAX)));
-    LOG("MediaCacheStream::NotifyDataLength(this=%p) "
+    LOG("MediaCacheStream::Init(this=%p) "
         "MEDIACACHESTREAM_NOTIFIED_LENGTH=%" PRIu32,
         this,
         length);
     Telemetry::Accumulate(
       Telemetry::HistogramID::MEDIACACHESTREAM_NOTIFIED_LENGTH, length);
 
     mStreamLength = aContentLength;
   }
--- a/dom/media/MediaCache.h
+++ b/dom/media/MediaCache.h
@@ -225,42 +225,47 @@ public:
   // Called on the main thread only.
   bool IsAvailableForSharing() const { return !mClosed && !mIsPrivateBrowsing; }
   // Get the principal for this stream. Anything accessing the contents of
   // this stream must have a principal that subsumes this principal.
   nsIPrincipal* GetCurrentPrincipal() { return mPrincipal; }
 
   // These callbacks are called on the main thread by the client
   // when data has been received via the channel.
-  // Tells the cache what the server said the data length is going to be.
-  // The actual data length may be greater (we receive more data than
+
+  // Notifies the cache that a load has begun. We pass the offset
+  // because in some cases the offset might not be what the cache
+  // requested. In particular we might unexpectedly start providing
+  // data at offset 0. This need not be called if the offset is the
+  // offset that the cache requested in
+  // ChannelMediaResource::CacheClientSeek. This can be called at any
+  // time by the client, not just after a CacheClientSeek.
+  //
+  // aSeekable tells us whether the stream is seekable or not. Non-seekable
+  // streams will always pass 0 for aOffset to CacheClientSeek. This should only
+  // be called while the stream is at channel offset 0. Seekability can
+  // change during the lifetime of the MediaCacheStream --- every time
+  // we do an HTTP load the seekability may be different (and sometimes
+  // is, in practice, due to the effects of caching proxies).
+  //
+  // aLength tells the cache what the server said the data length is going to
+  // be. The actual data length may be greater (we receive more data than
   // specified) or smaller (the stream ends before we reach the given
   // length), because servers can lie. The server's reported data length
   // *and* the actual data length can even vary over time because a
   // misbehaving server may feed us a different stream after each seek
   // operation. So this is really just a hint. The cache may however
   // stop reading (suspend the channel) when it thinks we've read all the
   // data available based on an incorrect reported length. Seeks relative
   // EOF also depend on the reported length if we haven't managed to
   // read the whole stream yet.
-  void NotifyDataLength(int64_t aLength);
-  // Notifies the cache that a load has begun. We pass the offset
-  // because in some cases the offset might not be what the cache
-  // requested. In particular we might unexpectedly start providing
-  // data at offset 0. This need not be called if the offset is the
-  // offset that the cache requested in
-  // ChannelMediaResource::CacheClientSeek. This can be called at any
-  // time by the client, not just after a CacheClientSeek.
-  // aSeekable tells us whether the stream is seekable or not. Non-seekable
-  // streams will always pass 0 for aOffset to CacheClientSeek. This should only
-  // be called while the stream is at channel offset 0. Seekability can
-  // change during the lifetime of the MediaCacheStream --- every time
-  // we do an HTTP load the seekability may be different (and sometimes
-  // is, in practice, due to the effects of caching proxies).
-  void NotifyDataStarted(uint32_t aLoadID, int64_t aOffset, bool aSeekable);
+  void NotifyDataStarted(uint32_t aLoadID,
+                         int64_t aOffset,
+                         bool aSeekable,
+                         int64_t aLength);
   // Notifies the cache that data has been received. The stream already
   // knows the offset because data is received in sequence and
   // the starting offset is known via NotifyDataStarted or because
   // the cache requested the offset in
   // ChannelMediaResource::CacheClientSeek, or because it defaulted to 0.
   void NotifyDataReceived(uint32_t aLoadID,
                           uint32_t aCount,
                           const uint8_t* aData);
@@ -282,17 +287,17 @@ public:
   // Main thread only.
   void NotifyClientSuspended(bool aSuspended);
 
   // These methods can be called on any thread.
   // Cached blocks associated with this stream will not be evicted
   // while the stream is pinned.
   void Pin();
   void Unpin();
-  // See comments above for NotifyDataLength about how the length
+  // See comments above for NotifyDataStarted about how the length
   // can vary over time. Returns -1 if no length is known. Returns the
   // reported length if we haven't got any better information. If
   // the stream ended normally we return the length we actually got.
   // If we've successfully read data beyond the originally reported length,
   // we return the end of the data we've read.
   int64_t GetLength();
   // Return the offset where next channel data will write to. Main thread only.
   int64_t GetOffset() const;