2017-09-18 14:33:49 +03:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ChannelMediaResource.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsIClassOfService.h"
|
2017-09-28 01:34:05 +03:00
|
|
|
#include "nsIInputStream.h"
|
2017-09-13 11:51:12 +03:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2018-08-07 06:03:00 +03:00
|
|
|
#include "nsHttp.h"
|
2017-09-18 14:33:49 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
static const uint32_t HTTP_PARTIAL_RESPONSE_CODE = 206;
|
|
|
|
static const uint32_t HTTP_OK_CODE = 200;
|
2017-10-04 17:53:19 +03:00
|
|
|
static const uint32_t HTTP_REQUESTED_RANGE_NOT_SATISFIABLE_CODE = 416;
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
mozilla::LazyLogModule gMediaResourceLog("MediaResource");
|
|
|
|
// Debug logging macro with object pointer and class name.
|
2017-10-13 08:31:59 +03:00
|
|
|
#define LOG(msg, ...) \
|
|
|
|
DDMOZ_LOG(gMediaResourceLog, mozilla::LogLevel::Debug, msg, ##__VA_ARGS__)
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
ChannelMediaResource::ChannelMediaResource(MediaResourceCallback* aCallback,
|
|
|
|
nsIChannel* aChannel, nsIURI* aURI,
|
2019-01-03 21:22:49 +03:00
|
|
|
int64_t aStreamLength,
|
2017-09-18 14:33:49 +03:00
|
|
|
bool aIsPrivateBrowsing)
|
|
|
|
: BaseMediaResource(aCallback, aChannel, aURI),
|
|
|
|
mCacheStream(this, aIsPrivateBrowsing),
|
2019-01-03 21:22:49 +03:00
|
|
|
mSuspendAgent(mCacheStream),
|
|
|
|
mKnownStreamLength(aStreamLength) {}
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
ChannelMediaResource::~ChannelMediaResource() {
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_ASSERT(mClosed);
|
2017-09-18 14:33:49 +03:00
|
|
|
MOZ_ASSERT(!mChannel);
|
|
|
|
MOZ_ASSERT(!mListener);
|
2017-12-08 06:08:38 +03:00
|
|
|
if (mSharedInfo) {
|
|
|
|
mSharedInfo->mResources.RemoveElement(this);
|
|
|
|
}
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelMediaResource::Listener just observes the channel and
|
|
|
|
// forwards notifications to the ChannelMediaResource. We use multiple
|
|
|
|
// listener objects so that when we open a new stream for a seek we can
|
|
|
|
// disconnect the old listener from the ChannelMediaResource and hook up
|
|
|
|
// a new listener, so notifications from the old channel are discarded
|
|
|
|
// and don't confuse us.
|
|
|
|
NS_IMPL_ISUPPORTS(ChannelMediaResource::Listener, nsIRequestObserver,
|
|
|
|
nsIStreamListener, nsIChannelEventSink, nsIInterfaceRequestor,
|
|
|
|
nsIThreadRetargetableStreamListener)
|
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
nsresult ChannelMediaResource::Listener::OnStartRequest(nsIRequest* aRequest) {
|
2017-09-20 09:37:18 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-09-21 12:16:40 +03:00
|
|
|
if (!mResource) return NS_OK;
|
2017-09-18 14:33:49 +03:00
|
|
|
return mResource->OnStartRequest(aRequest, mOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Listener::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsresult aStatus) {
|
2017-09-20 09:37:18 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-09-21 12:16:40 +03:00
|
|
|
if (!mResource) return NS_OK;
|
2017-11-22 05:35:48 +03:00
|
|
|
return mResource->OnStopRequest(aRequest, aStatus);
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Listener::OnDataAvailable(
|
2019-02-28 02:42:27 +03:00
|
|
|
nsIRequest* aRequest, nsIInputStream* aStream, uint64_t aOffset,
|
2017-09-18 14:33:49 +03:00
|
|
|
uint32_t aCount) {
|
|
|
|
// This might happen off the main thread.
|
2017-09-20 09:37:18 +03:00
|
|
|
RefPtr<ChannelMediaResource> res;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
res = mResource;
|
|
|
|
}
|
|
|
|
// Note Rekove() might happen at the same time to reset mResource. We check
|
|
|
|
// the load ID to determine if the data is from an old channel.
|
|
|
|
return res ? res->OnDataAvailable(mLoadID, aStream, aCount) : NS_OK;
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Listener::AsyncOnChannelRedirect(
|
|
|
|
nsIChannel* aOld, nsIChannel* aNew, uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* cb) {
|
2017-09-20 09:37:18 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsresult rv = NS_OK;
|
2017-09-21 12:16:40 +03:00
|
|
|
if (mResource) {
|
2017-09-18 14:33:49 +03:00
|
|
|
rv = mResource->OnChannelRedirect(aOld, aNew, aFlags, mOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Listener::CheckListenerChain() { return NS_OK; }
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Listener::GetInterface(const nsIID& aIID,
|
|
|
|
void** aResult) {
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2017-09-20 09:37:18 +03:00
|
|
|
void ChannelMediaResource::Listener::Revoke() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mResource = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
static bool IsPayloadCompressed(nsIHttpChannel* aChannel) {
|
|
|
|
nsAutoCString encoding;
|
|
|
|
Unused << aChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Encoding"),
|
|
|
|
encoding);
|
|
|
|
return encoding.Length() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::OnStartRequest(nsIRequest* aRequest,
|
|
|
|
int64_t aRequestOffset) {
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsresult status;
|
|
|
|
nsresult rv = aRequest->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (status == NS_BINDING_ABORTED) {
|
|
|
|
// Request was aborted before we had a chance to receive any data, or
|
|
|
|
// even an OnStartRequest(). Close the channel. This is important, as
|
|
|
|
// we don't want to mess up our state, as if we're cloned that would
|
|
|
|
// cause the clone to copy incorrect metadata (like whether we're
|
|
|
|
// infinite for example).
|
|
|
|
CloseChannel();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
|
|
|
// If the request was cancelled by nsCORSListenerProxy due to failing
|
|
|
|
// the CORS security check, send an error through to the media element.
|
|
|
|
if (status == NS_ERROR_DOM_BAD_URI) {
|
2017-11-17 06:07:30 +03:00
|
|
|
mCallback->NotifyNetworkError(MediaResult(status, "CORS not allowed"));
|
2017-09-18 14:33:49 +03:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(aRequest);
|
|
|
|
bool seekable = false;
|
2017-11-16 07:05:19 +03:00
|
|
|
int64_t length = -1;
|
2017-09-20 06:30:03 +03:00
|
|
|
int64_t startOffset = aRequestOffset;
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
if (hc) {
|
|
|
|
uint32_t responseStatus = 0;
|
|
|
|
Unused << hc->GetResponseStatus(&responseStatus);
|
|
|
|
bool succeeded = false;
|
|
|
|
Unused << hc->GetRequestSucceeded(&succeeded);
|
|
|
|
|
|
|
|
if (!succeeded && NS_SUCCEEDED(status)) {
|
|
|
|
// HTTP-level error (e.g. 4xx); treat this as a fatal network-level error.
|
|
|
|
// We might get this on a seek.
|
|
|
|
// (Note that lower-level errors indicated by NS_FAILED(status) are
|
|
|
|
// handled in OnStopRequest.)
|
|
|
|
// A 416 error should treated as EOF here... it's possible
|
|
|
|
// that we don't get Content-Length, we read N bytes, then we
|
|
|
|
// suspend and resume, the resume reopens the channel and we seek to
|
|
|
|
// offset N, but there are no more bytes, so we get a 416
|
|
|
|
// "Requested Range Not Satisfiable".
|
|
|
|
if (responseStatus == HTTP_REQUESTED_RANGE_NOT_SATISFIABLE_CODE) {
|
|
|
|
// OnStopRequest will not be fired, so we need to do some of its
|
2017-11-10 10:06:39 +03:00
|
|
|
// work here. Note we need to pass the load ID first so the following
|
|
|
|
// NotifyDataEnded() can pass the ID check.
|
|
|
|
mCacheStream.NotifyLoadID(mLoadID);
|
|
|
|
mCacheStream.NotifyDataEnded(mLoadID, status);
|
2017-09-18 14:33:49 +03:00
|
|
|
} else {
|
2017-11-17 06:07:30 +03:00
|
|
|
mCallback->NotifyNetworkError(
|
|
|
|
MediaResult(NS_ERROR_FAILURE, "HTTP error"));
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This disconnects our listener so we don't get any more data. We
|
|
|
|
// certainly don't want an error page to end up in our cache!
|
|
|
|
CloseChannel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString ranges;
|
|
|
|
Unused << hc->GetResponseHeader(NS_LITERAL_CSTRING("Accept-Ranges"),
|
|
|
|
ranges);
|
2018-08-07 06:03:00 +03:00
|
|
|
bool acceptsRanges =
|
|
|
|
net::nsHttp::FindToken(ranges.get(), "bytes", HTTP_HEADER_VALUE_SEPS);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
int64_t contentLength = -1;
|
|
|
|
const bool isCompressed = IsPayloadCompressed(hc);
|
|
|
|
if (!isCompressed) {
|
|
|
|
hc->GetContentLength(&contentLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check response code for byte-range requests (seeking, chunk requests).
|
|
|
|
// We don't expect to get a 206 response for a compressed stream, but
|
|
|
|
// double check just to be sure.
|
|
|
|
if (!isCompressed && responseStatus == HTTP_PARTIAL_RESPONSE_CODE) {
|
|
|
|
// Parse Content-Range header.
|
|
|
|
int64_t rangeStart = 0;
|
|
|
|
int64_t rangeEnd = 0;
|
|
|
|
int64_t rangeTotal = 0;
|
|
|
|
rv = ParseContentRangeHeader(hc, rangeStart, rangeEnd, rangeTotal);
|
|
|
|
|
|
|
|
// We received 'Content-Range', so the server accepts range requests.
|
|
|
|
bool gotRangeHeader = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
if (gotRangeHeader) {
|
2017-09-20 06:30:03 +03:00
|
|
|
startOffset = rangeStart;
|
2017-09-18 14:33:49 +03:00
|
|
|
// We received 'Content-Range', so the server accepts range requests.
|
|
|
|
// Notify media cache about the length and start offset of data
|
|
|
|
// received. Note: If aRangeTotal == -1, then the total bytes is unknown
|
|
|
|
// at this stage.
|
|
|
|
// For now, tell the decoder that the stream is infinite.
|
|
|
|
if (rangeTotal != -1) {
|
|
|
|
contentLength = std::max(contentLength, rangeTotal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acceptsRanges = gotRangeHeader;
|
2017-09-20 06:30:03 +03:00
|
|
|
} else if (responseStatus == HTTP_OK_CODE) {
|
|
|
|
// HTTP_OK_CODE means data will be sent from the start of the stream.
|
|
|
|
startOffset = 0;
|
|
|
|
|
|
|
|
if (aRequestOffset > 0) {
|
|
|
|
// If HTTP_OK_CODE is responded for a non-zero range request, we have
|
|
|
|
// to assume seeking doesn't work.
|
|
|
|
acceptsRanges = false;
|
|
|
|
}
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
if (aRequestOffset == 0 && contentLength >= 0 &&
|
|
|
|
(responseStatus == HTTP_OK_CODE ||
|
|
|
|
responseStatus == HTTP_PARTIAL_RESPONSE_CODE)) {
|
2017-11-16 07:05:19 +03:00
|
|
|
length = contentLength;
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
// 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;
|
2017-09-20 06:30:03 +03:00
|
|
|
} else {
|
|
|
|
// Not an HTTP channel. Assume data will be sent from position zero.
|
|
|
|
startOffset = 0;
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
2017-09-20 06:30:03 +03:00
|
|
|
|
2017-09-20 09:23:59 +03:00
|
|
|
// 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();
|
2018-03-05 23:55:03 +03:00
|
|
|
if (owner->HasError()) {
|
|
|
|
// Updating the principal resulted in an error. Abort the load.
|
|
|
|
CloseChannel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-09-20 09:23:59 +03:00
|
|
|
|
2017-11-16 07:05:19 +03:00
|
|
|
mCacheStream.NotifyDataStarted(mLoadID, startOffset, seekable, length);
|
2017-11-07 09:26:10 +03:00
|
|
|
mIsTransportSeekable = seekable;
|
2018-08-09 22:53:07 +03:00
|
|
|
if (mFirstReadLength < 0) {
|
|
|
|
mFirstReadLength = length;
|
|
|
|
}
|
2017-09-18 14:33:49 +03:00
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
mSuspendAgent.Delegate(mChannel);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
// Fires an initial progress event.
|
|
|
|
owner->DownloadProgressed();
|
|
|
|
|
2017-09-13 11:51:12 +03:00
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> retarget;
|
|
|
|
if (Preferences::GetBool("media.omt_data_delivery.enabled", false) &&
|
2017-10-23 11:51:17 +03:00
|
|
|
(retarget = do_QueryInterface(aRequest))) {
|
2017-09-13 11:51:12 +03:00
|
|
|
// Note this will not always succeed. We need to handle the case where
|
|
|
|
// all resources sharing the same cache might run their data callbacks
|
|
|
|
// on different threads.
|
|
|
|
retarget->RetargetDeliveryTo(mCacheStream.OwnerThread());
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelMediaResource::IsTransportSeekable() {
|
2017-11-07 09:26:10 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-08-09 22:53:07 +03:00
|
|
|
// We Report the transport as seekable if we know we will never seek into
|
|
|
|
// the underlying transport. As the MediaCache reads content by block of
|
|
|
|
// BLOCK_SIZE bytes, so the content length is less it will always be fully
|
|
|
|
// read from offset = 0 and we can then always successfully seek within this
|
|
|
|
// buffered content.
|
|
|
|
return mIsTransportSeekable ||
|
|
|
|
(mFirstReadLength > 0 &&
|
|
|
|
mFirstReadLength < MediaCacheStream::BLOCK_SIZE);
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::ParseContentRangeHeader(
|
|
|
|
nsIHttpChannel* aHttpChan, int64_t& aRangeStart, int64_t& aRangeEnd,
|
Bug 1443942 - Ensure MediaCacheStreams are initialized with the length of the resource, not the length of the byte range response. r=jya
I'm seeing intermittent failures of test_midflight_redirect_blocked. In this
test, our custom server responds to Firefox's 0- HTTP Byte Range request with a
[0,200] response. When Firefox requests 200-, the server responds with a cross
origin redirect, and then the remainder of the resource.
However sometimes while running test_midflight_redirect_blocked the MP4 demuxer
reads through all 200 bytes while trying to parse metadata before the redirect
has occurred and fed more data into the cache, and so the demuxer thinks it's
hit end of stream, and reports a failure. The demuxer thinks it's hit end of
stream, because we initialize the MediaCacheStream length in
ChannelMediaResource::Open() with the value of the Content-Length HTTP header.
But in an HTTP byte range response, the Content-Length header tells you the
length of the range returned, not the length of the entire resource. The length
of the resource is in the Content-Range header, we need to use that if
available.
So to fix this intermittent test failure, we need to also parse the
Content-Range header in ChannelMediaResource::Open(), and use the length from
that if available.
MozReview-Commit-ID: 29pPRsUvxag
--HG--
extra : rebase_source : ba1ef03d65ebd22698a29d8385f36b4c747ccf43
2018-04-04 03:36:00 +03:00
|
|
|
int64_t& aRangeTotal) const {
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ENSURE_ARG(aHttpChan);
|
|
|
|
|
|
|
|
nsAutoCString rangeStr;
|
|
|
|
nsresult rv = aHttpChan->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("Content-Range"), rangeStr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_FALSE(rangeStr.IsEmpty(), NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
// Parse the range header: e.g. Content-Range: bytes 7000-7999/8000.
|
|
|
|
int32_t spacePos = rangeStr.Find(NS_LITERAL_CSTRING(" "));
|
|
|
|
int32_t dashPos = rangeStr.Find(NS_LITERAL_CSTRING("-"), true, spacePos);
|
|
|
|
int32_t slashPos = rangeStr.Find(NS_LITERAL_CSTRING("/"), true, dashPos);
|
|
|
|
|
|
|
|
nsAutoCString aRangeStartText;
|
|
|
|
rangeStr.Mid(aRangeStartText, spacePos + 1, dashPos - (spacePos + 1));
|
|
|
|
aRangeStart = aRangeStartText.ToInteger64(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(0 <= aRangeStart, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
nsAutoCString aRangeEndText;
|
|
|
|
rangeStr.Mid(aRangeEndText, dashPos + 1, slashPos - (dashPos + 1));
|
|
|
|
aRangeEnd = aRangeEndText.ToInteger64(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(aRangeStart < aRangeEnd, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
nsAutoCString aRangeTotalText;
|
|
|
|
rangeStr.Right(aRangeTotalText, rangeStr.Length() - (slashPos + 1));
|
|
|
|
if (aRangeTotalText[0] == '*') {
|
|
|
|
aRangeTotal = -1;
|
|
|
|
} else {
|
|
|
|
aRangeTotal = aRangeTotalText.ToInteger64(&rv);
|
|
|
|
NS_ENSURE_TRUE(aRangeEnd < aRangeTotal, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Received bytes [%" PRId64 "] to [%" PRId64 "] of [%" PRId64
|
|
|
|
"] for decoder[%p]",
|
|
|
|
aRangeStart, aRangeEnd, aRangeTotal, mCallback.get());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-11-22 05:35:48 +03:00
|
|
|
nsresult ChannelMediaResource::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsresult aStatus) {
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
NS_ASSERTION(!mSuspendAgent.IsSuspended(),
|
|
|
|
"How can OnStopRequest fire while we're suspended?");
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
2017-11-10 09:21:23 +03:00
|
|
|
// Move this request back into the foreground. This is necessary for
|
|
|
|
// requests owned by video documents to ensure the load group fires
|
|
|
|
// OnStopRequest when restoring from session history.
|
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
|
|
|
|
|
|
|
if (loadFlags & nsIRequest::LOAD_BACKGROUND) {
|
|
|
|
ModifyLoadFlags(loadFlags & ~nsIRequest::LOAD_BACKGROUND);
|
|
|
|
}
|
|
|
|
|
2018-05-24 15:36:58 +03:00
|
|
|
// Note that aStatus might have succeeded --- this might be a normal close
|
|
|
|
// --- even in situations where the server cut us off because we were
|
|
|
|
// suspended. It is also possible that the server sends us fewer bytes than
|
|
|
|
// requested. So we need to "reopen on error" in that case too. The only
|
|
|
|
// cases where we don't need to reopen are when *we* closed the stream.
|
|
|
|
// But don't reopen if we need to seek and we don't think we can... that would
|
|
|
|
// cause us to just re-read the stream, which would be really bad.
|
|
|
|
/*
|
|
|
|
* | length | offset | reopen |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | -1 | 0 | yes |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | -1 | > 0 | seekable |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | 0 | X | no |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | > 0 | 0 | yes |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | > 0 | != length | seekable |
|
|
|
|
* +--------+-----------+----------+
|
|
|
|
* | > 0 | == length | no |
|
|
|
|
*/
|
|
|
|
if (aStatus != NS_ERROR_PARSED_DATA_CACHED && aStatus != NS_BINDING_ABORTED) {
|
|
|
|
auto lengthAndOffset = mCacheStream.GetLengthAndOffset();
|
|
|
|
int64_t length = lengthAndOffset.mLength;
|
|
|
|
int64_t offset = lengthAndOffset.mOffset;
|
|
|
|
if ((offset == 0 || mIsTransportSeekable) && offset != length) {
|
|
|
|
// If the stream did close normally, restart the channel if we're either
|
|
|
|
// at the start of the resource, or if the server is seekable and we're
|
|
|
|
// not at the end of stream. We don't restart the stream if we're at the
|
|
|
|
// end because not all web servers handle this case consistently; see:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1373618#c36
|
|
|
|
nsresult rv = Seek(offset, false);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// Close the streams that failed due to error. This will cause all
|
|
|
|
// client Read and Seek operations on those streams to fail. Blocked
|
|
|
|
// Reads will also be woken up.
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 18:38:02 +03:00
|
|
|
mCacheStream.NotifyDataEnded(mLoadID, aStatus);
|
2017-09-18 14:33:49 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::OnChannelRedirect(nsIChannel* aOld,
|
|
|
|
nsIChannel* aNew,
|
|
|
|
uint32_t aFlags,
|
|
|
|
int64_t aOffset) {
|
2017-11-08 11:16:24 +03:00
|
|
|
// OnChannelRedirect() is followed by OnStartRequest() where we will
|
|
|
|
// call mSuspendAgent.Delegate().
|
2017-09-18 14:33:49 +03:00
|
|
|
mChannel = aNew;
|
|
|
|
return SetupChannelHeaders(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::CopySegmentToCache(
|
|
|
|
nsIInputStream* aInStream, void* aClosure, const char* aFromSegment,
|
|
|
|
uint32_t aToOffset, uint32_t aCount, uint32_t* aWriteCount) {
|
2017-09-20 06:12:48 +03:00
|
|
|
*aWriteCount = aCount;
|
2017-11-15 12:56:10 +03:00
|
|
|
Closure* closure = static_cast<Closure*>(aClosure);
|
|
|
|
MediaCacheStream* cacheStream = &closure->mResource->mCacheStream;
|
|
|
|
if (cacheStream->OwnerThread()->IsOnCurrentThread()) {
|
|
|
|
cacheStream->NotifyDataReceived(
|
|
|
|
closure->mLoadID, aCount,
|
|
|
|
reinterpret_cast<const uint8_t*>(aFromSegment));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ChannelMediaResource> self = closure->mResource;
|
|
|
|
uint32_t loadID = closure->mLoadID;
|
|
|
|
UniquePtr<uint8_t[]> data = MakeUnique<uint8_t[]>(aCount);
|
|
|
|
memcpy(data.get(), aFromSegment, aCount);
|
|
|
|
cacheStream->OwnerThread()->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::NotifyDataReceived",
|
2018-05-30 22:15:35 +03:00
|
|
|
[self, loadID, data = std::move(data), aCount]() {
|
2017-11-15 12:56:10 +03:00
|
|
|
self->mCacheStream.NotifyDataReceived(loadID, aCount, data.get());
|
|
|
|
}));
|
|
|
|
|
2017-09-20 06:12:48 +03:00
|
|
|
return NS_OK;
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 06:41:08 +03:00
|
|
|
nsresult ChannelMediaResource::OnDataAvailable(uint32_t aLoadID,
|
2017-09-18 14:33:49 +03:00
|
|
|
nsIInputStream* aStream,
|
|
|
|
uint32_t aCount) {
|
|
|
|
// This might happen off the main thread.
|
2017-09-20 06:41:08 +03:00
|
|
|
Closure closure{aLoadID, this};
|
2017-09-18 14:33:49 +03:00
|
|
|
uint32_t count = aCount;
|
|
|
|
while (count > 0) {
|
|
|
|
uint32_t read;
|
2017-09-20 06:41:08 +03:00
|
|
|
nsresult rv =
|
|
|
|
aStream->ReadSegments(CopySegmentToCache, &closure, count, &read);
|
2017-09-18 14:33:49 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(read > 0, "Read 0 bytes while data was available?");
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1443942 - Ensure MediaCacheStreams are initialized with the length of the resource, not the length of the byte range response. r=jya
I'm seeing intermittent failures of test_midflight_redirect_blocked. In this
test, our custom server responds to Firefox's 0- HTTP Byte Range request with a
[0,200] response. When Firefox requests 200-, the server responds with a cross
origin redirect, and then the remainder of the resource.
However sometimes while running test_midflight_redirect_blocked the MP4 demuxer
reads through all 200 bytes while trying to parse metadata before the redirect
has occurred and fed more data into the cache, and so the demuxer thinks it's
hit end of stream, and reports a failure. The demuxer thinks it's hit end of
stream, because we initialize the MediaCacheStream length in
ChannelMediaResource::Open() with the value of the Content-Length HTTP header.
But in an HTTP byte range response, the Content-Length header tells you the
length of the range returned, not the length of the entire resource. The length
of the resource is in the Content-Range header, we need to use that if
available.
So to fix this intermittent test failure, we need to also parse the
Content-Range header in ChannelMediaResource::Open(), and use the length from
that if available.
MozReview-Commit-ID: 29pPRsUvxag
--HG--
extra : rebase_source : ba1ef03d65ebd22698a29d8385f36b4c747ccf43
2018-04-04 03:36:00 +03:00
|
|
|
int64_t ChannelMediaResource::CalculateStreamLength() const {
|
|
|
|
if (!mChannel) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(mChannel);
|
|
|
|
if (!hc) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool succeeded = false;
|
|
|
|
Unused << hc->GetRequestSucceeded(&succeeded);
|
|
|
|
if (!succeeded) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't determine the length of uncompressed payload.
|
|
|
|
const bool isCompressed = IsPayloadCompressed(hc);
|
|
|
|
if (isCompressed) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t contentLength = -1;
|
|
|
|
if (NS_FAILED(hc->GetContentLength(&contentLength))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t responseStatus = 0;
|
|
|
|
Unused << hc->GetResponseStatus(&responseStatus);
|
|
|
|
if (responseStatus != HTTP_PARTIAL_RESPONSE_CODE) {
|
|
|
|
return contentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an HTTP Byte Range response. The Content-Length is the length
|
|
|
|
// of the response, not the resource. We need to parse the Content-Range
|
|
|
|
// header and extract the range total in order to get the stream length.
|
|
|
|
int64_t rangeStart = 0;
|
|
|
|
int64_t rangeEnd = 0;
|
|
|
|
int64_t rangeTotal = 0;
|
|
|
|
bool gotRangeHeader = NS_SUCCEEDED(
|
|
|
|
ParseContentRangeHeader(hc, rangeStart, rangeEnd, rangeTotal));
|
|
|
|
if (gotRangeHeader && rangeTotal != -1) {
|
|
|
|
contentLength = std::max(contentLength, rangeTotal);
|
|
|
|
}
|
|
|
|
return contentLength;
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsresult ChannelMediaResource::Open(nsIStreamListener** aStreamListener) {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
MOZ_ASSERT(aStreamListener);
|
|
|
|
MOZ_ASSERT(mChannel);
|
|
|
|
|
2019-01-03 21:22:49 +03:00
|
|
|
int64_t streamLength =
|
|
|
|
mKnownStreamLength < 0 ? CalculateStreamLength() : mKnownStreamLength;
|
Bug 1443942 - Ensure MediaCacheStreams are initialized with the length of the resource, not the length of the byte range response. r=jya
I'm seeing intermittent failures of test_midflight_redirect_blocked. In this
test, our custom server responds to Firefox's 0- HTTP Byte Range request with a
[0,200] response. When Firefox requests 200-, the server responds with a cross
origin redirect, and then the remainder of the resource.
However sometimes while running test_midflight_redirect_blocked the MP4 demuxer
reads through all 200 bytes while trying to parse metadata before the redirect
has occurred and fed more data into the cache, and so the demuxer thinks it's
hit end of stream, and reports a failure. The demuxer thinks it's hit end of
stream, because we initialize the MediaCacheStream length in
ChannelMediaResource::Open() with the value of the Content-Length HTTP header.
But in an HTTP byte range response, the Content-Length header tells you the
length of the range returned, not the length of the entire resource. The length
of the resource is in the Content-Range header, we need to use that if
available.
So to fix this intermittent test failure, we need to also parse the
Content-Range header in ChannelMediaResource::Open(), and use the length from
that if available.
MozReview-Commit-ID: 29pPRsUvxag
--HG--
extra : rebase_source : ba1ef03d65ebd22698a29d8385f36b4c747ccf43
2018-04-04 03:36:00 +03:00
|
|
|
nsresult rv = mCacheStream.Init(streamLength);
|
2017-09-18 14:33:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-12-08 06:08:38 +03:00
|
|
|
mSharedInfo = new SharedInfo;
|
|
|
|
mSharedInfo->mResources.AppendElement(this);
|
|
|
|
|
Bug 1443942 - Ensure MediaCacheStreams are initialized with the length of the resource, not the length of the byte range response. r=jya
I'm seeing intermittent failures of test_midflight_redirect_blocked. In this
test, our custom server responds to Firefox's 0- HTTP Byte Range request with a
[0,200] response. When Firefox requests 200-, the server responds with a cross
origin redirect, and then the remainder of the resource.
However sometimes while running test_midflight_redirect_blocked the MP4 demuxer
reads through all 200 bytes while trying to parse metadata before the redirect
has occurred and fed more data into the cache, and so the demuxer thinks it's
hit end of stream, and reports a failure. The demuxer thinks it's hit end of
stream, because we initialize the MediaCacheStream length in
ChannelMediaResource::Open() with the value of the Content-Length HTTP header.
But in an HTTP byte range response, the Content-Length header tells you the
length of the range returned, not the length of the entire resource. The length
of the resource is in the Content-Range header, we need to use that if
available.
So to fix this intermittent test failure, we need to also parse the
Content-Range header in ChannelMediaResource::Open(), and use the length from
that if available.
MozReview-Commit-ID: 29pPRsUvxag
--HG--
extra : rebase_source : ba1ef03d65ebd22698a29d8385f36b4c747ccf43
2018-04-04 03:36:00 +03:00
|
|
|
mIsLiveStream = streamLength < 0;
|
2017-09-20 06:41:08 +03:00
|
|
|
mListener = new Listener(this, 0, ++mLoadID);
|
2017-09-18 14:33:49 +03:00
|
|
|
*aStreamListener = mListener;
|
|
|
|
NS_ADDREF(*aStreamListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::OpenChannel(int64_t aOffset) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
|
2017-09-18 14:33:49 +03:00
|
|
|
MOZ_ASSERT(mChannel);
|
|
|
|
MOZ_ASSERT(!mListener, "Listener should have been removed by now");
|
|
|
|
|
2017-09-20 06:41:08 +03:00
|
|
|
mListener = new Listener(this, aOffset, ++mLoadID);
|
2017-09-18 14:33:49 +03:00
|
|
|
nsresult rv = mChannel->SetNotificationCallbacks(mListener.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = SetupChannelHeaders(aOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = mChannel->AsyncOpen(mListener);
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Tell the media element that we are fetching data from a channel.
|
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
2017-10-10 02:04:18 +03:00
|
|
|
element->DownloadResumed();
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::SetupChannelHeaders(int64_t aOffset) {
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
|
2017-10-27 07:10:23 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
// Always use a byte range request even if we're reading from the start
|
|
|
|
// of the resource.
|
|
|
|
// This enables us to detect if the stream supports byte range
|
|
|
|
// requests, and therefore seeking, early.
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(mChannel);
|
|
|
|
if (hc) {
|
|
|
|
// Use |mOffset| if seeking in a complete file download.
|
|
|
|
nsAutoCString rangeString("bytes=");
|
|
|
|
rangeString.AppendInt(aOffset);
|
|
|
|
rangeString.Append('-');
|
|
|
|
nsresult rv =
|
|
|
|
hc->SetRequestHeader(NS_LITERAL_CSTRING("Range"), rangeString, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Send Accept header for video and audio types only (Bug 489071)
|
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
2017-09-18 14:33:49 +03:00
|
|
|
element->SetRequestHeaders(hc);
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(aOffset == 0, "Don't know how to seek on this channel type");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::Close() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2017-10-27 08:46:44 +03:00
|
|
|
if (!mClosed) {
|
|
|
|
CloseChannel();
|
|
|
|
mCacheStream.Close();
|
|
|
|
mClosed = true;
|
|
|
|
}
|
2017-09-18 14:33:49 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIPrincipal> ChannelMediaResource::GetCurrentPrincipal() {
|
2017-12-08 06:08:38 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return do_AddRef(mSharedInfo->mPrincipal);
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelMediaResource::CanClone() {
|
2017-12-14 11:22:53 +03:00
|
|
|
return !mClosed && mCacheStream.IsAvailableForSharing();
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BaseMediaResource> ChannelMediaResource::CloneData(
|
|
|
|
MediaResourceCallback* aCallback) {
|
2017-12-14 11:22:53 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(CanClone(), "Stream can't be cloned");
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
RefPtr<ChannelMediaResource> resource =
|
2019-01-03 21:22:49 +03:00
|
|
|
new ChannelMediaResource(aCallback, nullptr, mURI, mKnownStreamLength);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
2017-12-06 11:00:20 +03:00
|
|
|
resource->mIsLiveStream = mIsLiveStream;
|
2017-11-07 09:26:10 +03:00
|
|
|
resource->mIsTransportSeekable = mIsTransportSeekable;
|
2017-12-08 06:08:38 +03:00
|
|
|
resource->mSharedInfo = mSharedInfo;
|
|
|
|
mSharedInfo->mResources.AppendElement(resource.get());
|
2017-11-07 09:26:10 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
// Initially the clone is treated as suspended by the cache, because
|
|
|
|
// we don't have a channel. If the cache needs to read data from the clone
|
|
|
|
// it will call CacheClientResume (or CacheClientSeek with aResume true)
|
|
|
|
// which will recreate the channel. This way, if all of the media data
|
|
|
|
// is already in the cache we don't create an unnecessary HTTP channel
|
|
|
|
// and perform a useless HTTP transaction.
|
2017-12-16 18:50:07 +03:00
|
|
|
resource->mCacheStream.InitAsClone(&mCacheStream);
|
2017-09-18 14:33:49 +03:00
|
|
|
return resource.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::CloseChannel() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
if (mChannel) {
|
2017-11-08 11:16:24 +03:00
|
|
|
mSuspendAgent.Revoke();
|
2017-09-18 14:33:49 +03:00
|
|
|
// The status we use here won't be passed to the decoder, since
|
|
|
|
// we've already revoked the listener. It can however be passed
|
|
|
|
// to nsDocumentViewer::LoadComplete if our channel is the one
|
|
|
|
// that kicked off creation of a video document. We don't want that
|
|
|
|
// document load to think there was an error.
|
|
|
|
// NS_ERROR_PARSED_DATA_CACHED is the best thing we have for that
|
|
|
|
// at the moment.
|
|
|
|
mChannel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
|
|
|
mChannel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
2017-09-21 12:16:40 +03:00
|
|
|
mListener->Revoke();
|
2017-09-18 14:33:49 +03:00
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::ReadFromCache(char* aBuffer, int64_t aOffset,
|
|
|
|
uint32_t aCount) {
|
|
|
|
return mCacheStream.ReadFromCache(aBuffer, aOffset, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes) {
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2017-12-01 09:02:44 +03:00
|
|
|
return mCacheStream.ReadAt(aOffset, aBuffer, aCount, aBytes);
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::ThrottleReadahead(bool bThrottle) {
|
|
|
|
mCacheStream.ThrottleReadahead(bThrottle);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::GetCachedRanges(MediaByteRangeSet& aRanges) {
|
|
|
|
return mCacheStream.GetCachedRanges(aRanges);
|
|
|
|
}
|
|
|
|
|
2017-10-27 07:10:23 +03:00
|
|
|
void ChannelMediaResource::Suspend(bool aCloseImmediately) {
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
|
2017-10-27 08:46:44 +03:00
|
|
|
if (mClosed) {
|
2017-10-27 07:10:23 +03:00
|
|
|
// Nothing to do when we are closed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
2017-11-07 09:26:10 +03:00
|
|
|
if (mChannel && aCloseImmediately && mIsTransportSeekable) {
|
2017-09-18 14:33:49 +03:00
|
|
|
CloseChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSuspendAgent.Suspend()) {
|
2018-01-05 11:04:16 +03:00
|
|
|
element->DownloadSuspended();
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 07:10:23 +03:00
|
|
|
void ChannelMediaResource::Resume() {
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
|
2017-10-27 08:46:44 +03:00
|
|
|
if (mClosed) {
|
2017-10-27 07:10:23 +03:00
|
|
|
// Nothing to do when we are closed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
if (mSuspendAgent.Resume()) {
|
|
|
|
if (mChannel) {
|
|
|
|
// Just wake up our existing channel
|
|
|
|
element->DownloadResumed();
|
|
|
|
} else {
|
2017-12-07 10:43:36 +03:00
|
|
|
mCacheStream.NotifyResume();
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ChannelMediaResource::RecreateChannel() {
|
2017-10-27 08:46:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
|
2017-10-27 07:10:23 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsLoadFlags loadFlags = nsICachingChannel::LOAD_BYPASS_LOCAL_CACHE_IF_BUSY |
|
|
|
|
(mLoadInBackground ? nsIRequest::LOAD_BACKGROUND : 0);
|
|
|
|
|
|
|
|
MediaDecoderOwner* owner = mCallback->GetMediaOwner();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(owner);
|
2017-09-18 14:33:49 +03:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2017-10-27 17:17:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element);
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
|
|
|
NS_ENSURE_TRUE(loadGroup, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsSecurityFlags securityFlags =
|
|
|
|
element->ShouldCheckAllowOrigin()
|
|
|
|
? nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS
|
|
|
|
: nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
|
|
|
|
|
|
|
|
MOZ_ASSERT(element->IsAnyOfHTMLElements(nsGkAtoms::audio, nsGkAtoms::video));
|
|
|
|
nsContentPolicyType contentPolicyType =
|
|
|
|
element->IsHTMLElement(nsGkAtoms::audio)
|
|
|
|
? nsIContentPolicy::TYPE_INTERNAL_AUDIO
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_VIDEO;
|
|
|
|
|
2017-10-11 12:07:35 +03:00
|
|
|
// If element has 'triggeringprincipal' attribute, we will use the value as
|
|
|
|
// triggeringPrincipal for the channel, otherwise it will default to use
|
2017-09-15 13:47:44 +03:00
|
|
|
// aElement->NodePrincipal().
|
2017-10-11 12:07:35 +03:00
|
|
|
// This function returns true when element has 'triggeringprincipal', so if
|
2017-09-15 13:47:44 +03:00
|
|
|
// setAttrs is true we will override the origin attributes on the channel
|
|
|
|
// later.
|
2017-10-11 12:07:35 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
|
|
|
|
bool setAttrs = nsContentUtils::QueryTriggeringPrincipal(
|
|
|
|
element, getter_AddRefs(triggeringPrincipal));
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-09-15 13:47:44 +03:00
|
|
|
nsresult rv = NS_NewChannelWithTriggeringPrincipal(
|
|
|
|
getter_AddRefs(mChannel), mURI, element, triggeringPrincipal,
|
|
|
|
securityFlags, contentPolicyType,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // aPerformanceStorage
|
2017-09-15 13:47:44 +03:00
|
|
|
loadGroup,
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags);
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-09-15 13:47:44 +03:00
|
|
|
if (setAttrs) {
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = mChannel->LoadInfo();
|
|
|
|
// The function simply returns NS_OK, so we ignore the return value.
|
|
|
|
Unused << loadInfo->SetOriginAttributes(
|
|
|
|
triggeringPrincipal->OriginAttributesRef());
|
2017-09-15 13:47:44 +03:00
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(mChannel));
|
|
|
|
if (cos) {
|
|
|
|
// Unconditionally disable throttling since we want the media to fluently
|
|
|
|
// play even when we switch the tab to background.
|
|
|
|
cos->AddClassFlags(nsIClassOfService::DontThrottle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::CacheClientNotifyDataReceived() {
|
2017-12-06 11:01:12 +03:00
|
|
|
mCallback->AbstractMainThread()->Dispatch(NewRunnableMethod(
|
2017-09-18 14:33:49 +03:00
|
|
|
"MediaResourceCallback::NotifyDataArrived", mCallback.get(),
|
|
|
|
&MediaResourceCallback::NotifyDataArrived));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::CacheClientNotifyDataEnded(nsresult aStatus) {
|
2017-12-06 11:00:20 +03:00
|
|
|
mCallback->AbstractMainThread()->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaResource::CacheClientNotifyDataEnded",
|
|
|
|
[self = RefPtr<ChannelMediaResource>(this), aStatus]() {
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
self->mIsLiveStream = false;
|
|
|
|
}
|
|
|
|
self->mCallback->NotifyDataEnded(aStatus);
|
|
|
|
}));
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::CacheClientNotifyPrincipalChanged() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
|
|
|
|
mCallback->NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::UpdatePrincipal() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-09-20 09:23:59 +03:00
|
|
|
MOZ_ASSERT(mChannel);
|
2017-09-18 14:33:49 +03:00
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
2017-12-08 06:08:38 +03:00
|
|
|
if (!secMan) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
|
|
|
|
if (nsContentUtils::CombineResourcePrincipals(&mSharedInfo->mPrincipal,
|
|
|
|
principal)) {
|
|
|
|
for (auto* r : mSharedInfo->mResources) {
|
|
|
|
r->CacheClientNotifyPrincipalChanged();
|
|
|
|
}
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 06:30:07 +03:00
|
|
|
void ChannelMediaResource::CacheClientNotifySuspendedStatusChanged(
|
|
|
|
bool aSuspended) {
|
2017-10-24 05:00:23 +03:00
|
|
|
mCallback->AbstractMainThread()->Dispatch(NewRunnableMethod<bool>(
|
|
|
|
"MediaResourceCallback::NotifySuspendedStatusChanged", mCallback.get(),
|
|
|
|
&MediaResourceCallback::NotifySuspendedStatusChanged, aSuspended));
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
2017-10-24 04:23:12 +03:00
|
|
|
nsresult ChannelMediaResource::Seek(int64_t aOffset, bool aResume) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-27 07:10:23 +03:00
|
|
|
|
2017-10-27 08:46:44 +03:00
|
|
|
if (mClosed) {
|
2017-10-27 07:10:23 +03:00
|
|
|
// Nothing to do when we are closed.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:23:12 +03:00
|
|
|
LOG("Seek requested for aOffset [%" PRId64 "]", aOffset);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
CloseChannel();
|
|
|
|
|
|
|
|
if (aResume) {
|
|
|
|
mSuspendAgent.Resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't create a new channel if we are still suspended. The channel will
|
|
|
|
// be recreated when we are resumed.
|
|
|
|
if (mSuspendAgent.IsSuspended()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = RecreateChannel();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return OpenChannel(aOffset);
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:23:12 +03:00
|
|
|
void ChannelMediaResource::CacheClientSeek(int64_t aOffset, bool aResume) {
|
|
|
|
RefPtr<ChannelMediaResource> self = this;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaResource::Seek", [self, aOffset, aResume]() {
|
|
|
|
nsresult rv = self->Seek(aOffset, aResume);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Close the streams that failed due to error. This will cause all
|
|
|
|
// client Read and Seek operations on those streams to fail. Blocked
|
|
|
|
// Reads will also be woken up.
|
|
|
|
self->Close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mCallback->AbstractMainThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
void ChannelMediaResource::CacheClientSuspend() {
|
2017-10-24 04:38:34 +03:00
|
|
|
mCallback->AbstractMainThread()->Dispatch(
|
|
|
|
NewRunnableMethod<bool>("ChannelMediaResource::Suspend", this,
|
|
|
|
&ChannelMediaResource::Suspend, false));
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::CacheClientResume() {
|
2017-10-24 04:38:34 +03:00
|
|
|
mCallback->AbstractMainThread()->Dispatch(NewRunnableMethod(
|
|
|
|
"ChannelMediaResource::Resume", this, &ChannelMediaResource::Resume));
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ChannelMediaResource::GetNextCachedData(int64_t aOffset) {
|
|
|
|
return mCacheStream.GetNextCachedData(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ChannelMediaResource::GetCachedDataEnd(int64_t aOffset) {
|
|
|
|
return mCacheStream.GetCachedDataEnd(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelMediaResource::IsDataCachedToEndOfResource(int64_t aOffset) {
|
|
|
|
return mCacheStream.IsDataCachedToEndOfStream(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelMediaResource::IsSuspended() { return mSuspendAgent.IsSuspended(); }
|
|
|
|
|
|
|
|
void ChannelMediaResource::SetReadMode(MediaCacheStream::ReadMode aMode) {
|
|
|
|
mCacheStream.SetReadMode(aMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::SetPlaybackRate(uint32_t aBytesPerSecond) {
|
|
|
|
mCacheStream.SetPlaybackRate(aBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::Pin() { mCacheStream.Pin(); }
|
|
|
|
|
|
|
|
void ChannelMediaResource::Unpin() { mCacheStream.Unpin(); }
|
|
|
|
|
|
|
|
double ChannelMediaResource::GetDownloadRate(bool* aIsReliable) {
|
2017-11-29 11:49:54 +03:00
|
|
|
return mCacheStream.GetDownloadRate(aIsReliable);
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ChannelMediaResource::GetLength() { return mCacheStream.GetLength(); }
|
|
|
|
|
2019-05-06 19:39:58 +03:00
|
|
|
void ChannelMediaResource::GetDebugInfo(dom::MediaResourceDebugInfo& aInfo) {
|
|
|
|
mCacheStream.GetDebugInfo(aInfo.mCacheStream);
|
2017-10-25 08:57:39 +03:00
|
|
|
}
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
// ChannelSuspendAgent
|
|
|
|
|
|
|
|
bool ChannelSuspendAgent::Suspend() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
SuspendInternal();
|
2017-10-24 06:25:41 +03:00
|
|
|
if (++mSuspendCount == 1) {
|
|
|
|
mCacheStream.NotifyClientSuspended(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-09-18 14:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelSuspendAgent::SuspendInternal() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mChannel) {
|
|
|
|
bool isPending = false;
|
|
|
|
nsresult rv = mChannel->IsPending(&isPending);
|
|
|
|
if (NS_SUCCEEDED(rv) && isPending && !mIsChannelSuspended) {
|
|
|
|
mChannel->Suspend();
|
|
|
|
mIsChannelSuspended = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelSuspendAgent::Resume() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(IsSuspended(), "Resume without suspend!");
|
|
|
|
|
2017-10-24 06:25:41 +03:00
|
|
|
if (--mSuspendCount == 0) {
|
2017-09-18 14:33:49 +03:00
|
|
|
if (mChannel && mIsChannelSuspended) {
|
|
|
|
mChannel->Resume();
|
|
|
|
mIsChannelSuspended = false;
|
|
|
|
}
|
2017-10-24 06:25:41 +03:00
|
|
|
mCacheStream.NotifyClientSuspended(false);
|
2017-09-18 14:33:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
void ChannelSuspendAgent::Delegate(nsIChannel* aChannel) {
|
2017-09-18 14:33:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-08 11:16:24 +03:00
|
|
|
MOZ_ASSERT(aChannel);
|
|
|
|
MOZ_ASSERT(!mChannel, "The previous channel not closed.");
|
|
|
|
MOZ_ASSERT(!mIsChannelSuspended);
|
|
|
|
|
|
|
|
mChannel = aChannel;
|
|
|
|
// Ensure the suspend status of the channel matches our suspend count.
|
|
|
|
if (IsSuspended()) {
|
2017-09-18 14:33:49 +03:00
|
|
|
SuspendInternal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
void ChannelSuspendAgent::Revoke() {
|
2017-09-18 14:33:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
if (!mChannel) {
|
|
|
|
// Channel already revoked. Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before closing the channel, it needs to be resumed to make sure its
|
2017-09-18 14:33:49 +03:00
|
|
|
// internal state is correct. Besides, We need to suspend the channel after
|
|
|
|
// recreating.
|
|
|
|
if (mIsChannelSuspended) {
|
|
|
|
mChannel->Resume();
|
|
|
|
mIsChannelSuspended = false;
|
|
|
|
}
|
|
|
|
mChannel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelSuspendAgent::IsSuspended() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return (mSuspendCount > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|