2008-10-19 00:39:21 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-08-11 06:29:50 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
#include "MediaResource.h"
|
2011-08-11 06:29:50 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2008-10-19 00:39:21 -07:00
|
|
|
#include "nsDebug.h"
|
2012-11-14 11:45:13 -08:00
|
|
|
#include "nsBuiltinDecoder.h"
|
2008-10-19 00:39:21 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileChannel.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2009-01-24 03:00:17 -08:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
|
|
|
#include "nsHTMLMediaElement.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2009-03-31 17:52:56 -07:00
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsURILoader.h"
|
2010-08-04 19:15:55 -07:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2011-06-21 08:37:45 -07:00
|
|
|
#include "mozilla/Util.h" // for DebugOnly
|
2011-08-11 06:29:50 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-09-24 20:24:44 -07:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gMediaResourceLog;
|
|
|
|
#define LOG(msg, ...) PR_LOG(gMediaResourceLog, PR_LOG_DEBUG, \
|
|
|
|
(msg, ##__VA_ARGS__))
|
|
|
|
// Debug logging macro with object pointer and class name.
|
|
|
|
#define CMLOG(msg, ...) \
|
|
|
|
LOG("%p [ChannelMediaResource]: " msg, this, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOG(msg, ...)
|
|
|
|
#define CMLOG(msg, ...)
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t HTTP_OK_CODE = 200;
|
|
|
|
static const uint32_t HTTP_PARTIAL_RESPONSE_CODE = 206;
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
2009-04-11 02:39:24 -07:00
|
|
|
|
2012-11-14 11:45:13 -08:00
|
|
|
ChannelMediaResource::ChannelMediaResource(nsBuiltinDecoder* aDecoder,
|
2009-03-31 17:52:56 -07:00
|
|
|
nsIChannel* aChannel, nsIURI* aURI)
|
2012-02-14 20:35:01 -08:00
|
|
|
: MediaResource(aDecoder, aChannel, aURI),
|
2009-11-05 05:55:35 -08:00
|
|
|
mOffset(0), mSuspendCount(0),
|
2011-09-29 16:34:37 -07:00
|
|
|
mReopenOnError(false), mIgnoreClose(false),
|
2009-03-31 17:52:56 -07:00
|
|
|
mCacheStream(this),
|
2012-02-14 20:35:01 -08:00
|
|
|
mLock("ChannelMediaResource.mLock"),
|
2012-08-17 10:01:08 -07:00
|
|
|
mIgnoreResume(false),
|
2012-09-29 16:29:04 -07:00
|
|
|
mSeekingForMetadata(false),
|
|
|
|
mByteRangeDownloads(false),
|
|
|
|
mByteRangeFirstOpen(true),
|
|
|
|
mSeekOffsetMonitor("media.dashseekmonitor"),
|
|
|
|
mSeekOffset(-1)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2012-09-29 16:29:04 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gMediaResourceLog) {
|
|
|
|
gMediaResourceLog = PR_NewLogModule("MediaResource");
|
|
|
|
}
|
|
|
|
#endif
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::~ChannelMediaResource()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
if (mListener) {
|
|
|
|
// Kill its reference to us since we're going away
|
|
|
|
mListener->Revoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// ChannelMediaResource::Listener just observes the channel and
|
|
|
|
// forwards notifications to the ChannelMediaResource. We use multiple
|
2009-03-31 17:52:56 -07:00
|
|
|
// listener objects so that when we open a new stream for a seek we can
|
2012-02-14 20:35:01 -08:00
|
|
|
// disconnect the old listener from the ChannelMediaResource and hook up
|
2009-03-31 17:52:56 -07:00
|
|
|
// a new listener, so notifications from the old channel are discarded
|
|
|
|
// and don't confuse us.
|
2012-02-14 20:35:01 -08:00
|
|
|
NS_IMPL_ISUPPORTS4(ChannelMediaResource::Listener,
|
2009-05-13 14:52:50 -07:00
|
|
|
nsIRequestObserver, nsIStreamListener, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor)
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Listener::OnStartRequest(nsIRequest* aRequest,
|
2009-03-31 17:52:56 -07:00
|
|
|
nsISupports* aContext)
|
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!mResource)
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
2012-02-14 20:35:01 -08:00
|
|
|
return mResource->OnStartRequest(aRequest);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Listener::OnStopRequest(nsIRequest* aRequest,
|
2009-03-31 17:52:56 -07:00
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!mResource)
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
2012-02-14 20:35:01 -08:00
|
|
|
return mResource->OnStopRequest(aRequest, aStatus);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Listener::OnDataAvailable(nsIRequest* aRequest,
|
2009-11-05 05:55:35 -08:00
|
|
|
nsISupports* aContext,
|
2009-03-31 17:52:56 -07:00
|
|
|
nsIInputStream* aStream,
|
2012-09-05 19:41:02 -07:00
|
|
|
uint64_t aOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!mResource)
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
2012-02-14 20:35:01 -08:00
|
|
|
return mResource->OnDataAvailable(aRequest, aStream, aCount);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2009-05-13 14:52:50 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Listener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
2010-08-04 19:15:55 -07:00
|
|
|
nsIChannel* aNewChannel,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags,
|
2010-08-04 19:15:55 -07:00
|
|
|
nsIAsyncVerifyRedirectCallback* cb)
|
2009-05-13 14:52:50 -07:00
|
|
|
{
|
2010-08-04 19:15:55 -07:00
|
|
|
nsresult rv = NS_OK;
|
2012-02-14 20:35:01 -08:00
|
|
|
if (mResource)
|
|
|
|
rv = mResource->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
|
2010-08-04 19:15:55 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
2009-05-13 14:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Listener::GetInterface(const nsIID & aIID, void **aResult)
|
2009-05-13 14:52:50 -07:00
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::OnStartRequest(nsIRequest* aRequest)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
2009-09-29 14:32:30 -07:00
|
|
|
nsresult status;
|
|
|
|
nsresult rv = aRequest->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
2011-03-28 13:18:45 -07:00
|
|
|
// If the request was cancelled by nsCORSListenerProxy due to failing
|
|
|
|
// the CORS security check, send an error through to the media element.
|
2009-09-29 14:32:30 -07:00
|
|
|
if (status == NS_ERROR_DOM_BAD_URI) {
|
2009-03-31 17:52:56 -07:00
|
|
|
mDecoder->NetworkError();
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(aRequest);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool seekable = false;
|
2009-03-31 17:52:56 -07:00
|
|
|
if (hc) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t responseStatus = 0;
|
2009-09-29 14:32:30 -07:00
|
|
|
hc->GetResponseStatus(&responseStatus);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool succeeded = false;
|
2009-09-29 14:32:30 -07:00
|
|
|
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) {
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString ranges;
|
2009-03-31 17:52:56 -07:00
|
|
|
hc->GetResponseHeader(NS_LITERAL_CSTRING("Accept-Ranges"),
|
|
|
|
ranges);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool acceptsRanges = ranges.EqualsLiteral("bytes");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-05-17 19:02:20 -07:00
|
|
|
if (mOffset == 0) {
|
2010-01-27 04:29:26 -08:00
|
|
|
// Look for duration headers from known Ogg content systems.
|
|
|
|
// In the case of multiple options for obtaining the duration
|
|
|
|
// the order of precedence is:
|
2009-03-31 17:52:56 -07:00
|
|
|
// 1) The Media resource metadata if possible (done by the decoder itself).
|
2010-01-27 04:29:26 -08:00
|
|
|
// 2) Content-Duration message header.
|
|
|
|
// 3) X-AMZ-Meta-Content-Duration.
|
|
|
|
// 4) X-Content-Duration.
|
|
|
|
// 5) Perform a seek in the decoder to find the value.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString durationText;
|
2012-07-27 06:59:29 -07:00
|
|
|
nsresult ec = NS_OK;
|
2010-01-27 04:29:26 -08:00
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("Content-Duration"), durationText);
|
2009-03-31 17:52:56 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("X-AMZ-Meta-Content-Duration"), durationText);
|
|
|
|
}
|
2010-01-27 04:29:26 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("X-Content-Duration"), durationText);
|
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-03-25 04:14:19 -07:00
|
|
|
double duration = durationText.ToDouble(&ec);
|
2009-03-31 17:52:56 -07:00
|
|
|
if (ec == NS_OK && duration >= 0) {
|
2011-04-13 15:12:23 -07:00
|
|
|
mDecoder->SetDuration(duration);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2011-08-01 11:11:20 -07:00
|
|
|
} else {
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecoder->SetInfinite(true);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 05:55:35 -08:00
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Check response code for byte-range requests (seeking, chunk requests).
|
|
|
|
if (!mByteRange.IsNull() && (responseStatus == HTTP_PARTIAL_RESPONSE_CODE)) {
|
|
|
|
// Byte range requests should get partial response codes and should
|
|
|
|
// accept ranges.
|
|
|
|
if (!acceptsRanges) {
|
|
|
|
CMLOG("Error! HTTP_PARTIAL_RESPONSE_CODE received but server says "
|
|
|
|
"range requests are not accepted! Channel[%p]", hc.get());
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
CloseChannel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse Content-Range header.
|
|
|
|
int64_t rangeStart = 0;
|
|
|
|
int64_t rangeEnd = 0;
|
|
|
|
int64_t rangeTotal = 0;
|
|
|
|
rv = ParseContentRangeHeader(hc, rangeStart, rangeEnd, rangeTotal);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Content-Range header text should be parse-able.
|
|
|
|
CMLOG("Error processing \'Content-Range' for "
|
|
|
|
"HTTP_PARTIAL_RESPONSE_CODE: rv[%x]channel [%p]", rv, hc.get());
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
CloseChannel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give some warnings if the ranges are unexpected.
|
|
|
|
// XXX These could be error conditions.
|
|
|
|
NS_WARN_IF_FALSE(mByteRange.mStart == rangeStart,
|
|
|
|
"response range start does not match request");
|
|
|
|
NS_WARN_IF_FALSE(mOffset == rangeStart,
|
|
|
|
"response range start does not match current offset");
|
|
|
|
NS_WARN_IF_FALSE(mByteRange.mEnd == rangeEnd,
|
|
|
|
"response range end does not match request");
|
|
|
|
// 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) {
|
|
|
|
mCacheStream.NotifyDataLength(rangeTotal);
|
|
|
|
} else {
|
|
|
|
mDecoder->SetInfinite(true);
|
|
|
|
}
|
|
|
|
mCacheStream.NotifyDataStarted(rangeStart);
|
|
|
|
|
|
|
|
mOffset = rangeStart;
|
|
|
|
acceptsRanges = true;
|
|
|
|
} else if (((mOffset > 0) || !mByteRange.IsNull())
|
|
|
|
&& (responseStatus == HTTP_OK_CODE)) {
|
|
|
|
// If we get an OK response but we were seeking, or requesting a byte
|
|
|
|
// range, then we have to assume that seeking doesn't work. We also need
|
|
|
|
// to tell the cache that it's getting data for the start of the stream.
|
2009-03-31 17:52:56 -07:00
|
|
|
mCacheStream.NotifyDataStarted(0);
|
2009-05-17 19:02:20 -07:00
|
|
|
mOffset = 0;
|
2011-02-20 19:38:29 -08:00
|
|
|
|
|
|
|
// The server claimed it supported range requests. It lied.
|
2011-09-29 16:34:37 -07:00
|
|
|
acceptsRanges = false;
|
2009-11-05 05:55:35 -08:00
|
|
|
} else if (mOffset == 0 &&
|
2009-03-31 17:52:56 -07:00
|
|
|
(responseStatus == HTTP_OK_CODE ||
|
|
|
|
responseStatus == HTTP_PARTIAL_RESPONSE_CODE)) {
|
|
|
|
// We weren't seeking and got a valid response status,
|
|
|
|
// set the length of the content.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t cl = -1;
|
2012-10-22 10:51:07 -07:00
|
|
|
hc->GetContentLength(&cl);
|
2012-05-30 19:33:15 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
if (cl >= 0) {
|
|
|
|
mCacheStream.NotifyDataLength(cl);
|
|
|
|
}
|
|
|
|
}
|
2009-05-17 19:02:20 -07: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
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
seekable =
|
|
|
|
responseStatus == HTTP_PARTIAL_RESPONSE_CODE || acceptsRanges;
|
2011-08-01 11:11:20 -07:00
|
|
|
|
|
|
|
if (seekable) {
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecoder->SetInfinite(false);
|
2011-08-01 11:11:20 -07:00
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
mDecoder->SetSeekable(seekable);
|
|
|
|
mCacheStream.SetSeekable(seekable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICachingChannel> cc = do_QueryInterface(aRequest);
|
|
|
|
if (cc) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool fromCache = false;
|
2009-09-29 14:32:30 -07:00
|
|
|
rv = cc->IsFromCache(&fromCache);
|
2009-03-31 17:52:56 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && !fromCache) {
|
2011-09-29 16:34:37 -07:00
|
|
|
cc->SetCacheAsFile(true);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-04-11 02:39:24 -07:00
|
|
|
mChannelStatistics.Start(TimeStamp::Now());
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mReopenOnError = false;
|
2012-08-17 10:01:08 -07:00
|
|
|
// If we are seeking to get metadata, because we are playing an OGG file,
|
|
|
|
// ignore if the channel gets closed without us suspending it explicitly. We
|
|
|
|
// don't want to tell the element that the download has finished whereas we
|
|
|
|
// just happended to have reached the end of the media while seeking.
|
|
|
|
mIgnoreClose = mSeekingForMetadata;
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Re-suspend the channel if it needs to be suspended
|
2011-04-01 02:43:29 -07:00
|
|
|
// No need to call PossiblySuspend here since the channel is
|
2012-02-23 17:07:35 -08:00
|
|
|
// definitely in the right state for us in OnStartRequest.
|
2009-03-31 17:52:56 -07:00
|
|
|
mChannel->Suspend();
|
2011-09-29 16:34:37 -07:00
|
|
|
mIgnoreResume = false;
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fires an initial progress event and sets up the stall counter so stall events
|
|
|
|
// fire if no download occurs within the required time frame.
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecoder->Progress(false);
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
nsresult
|
|
|
|
ChannelMediaResource::ParseContentRangeHeader(nsIHttpChannel * aHttpChan,
|
|
|
|
int64_t& aRangeStart,
|
|
|
|
int64_t& aRangeEnd,
|
|
|
|
int64_t& aRangeTotal)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
CMLOG("Received bytes [%d] to [%d] of [%d]",
|
|
|
|
aRangeStart, aRangeEnd, aRangeTotal);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::OnStopRequest(nsIRequest* aRequest, nsresult aStatus)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
2009-05-17 19:02:20 -07:00
|
|
|
NS_ASSERTION(mSuspendCount == 0,
|
|
|
|
"How can OnStopRequest fire while we're suspended?");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-04-11 02:39:24 -07:00
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// If we were loading a byte range, notify decoder and return.
|
|
|
|
// Skip this for unterminated byte range requests, e.g. seeking for whole
|
|
|
|
// file downloads.
|
|
|
|
if (mByteRangeDownloads) {
|
|
|
|
mDecoder->NotifyDownloadEnded(aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-14 19:20:49 -07: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. 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.
|
|
|
|
if (mReopenOnError &&
|
|
|
|
aStatus != NS_ERROR_PARSED_DATA_CACHED && aStatus != NS_BINDING_ABORTED &&
|
|
|
|
(mOffset == 0 || mCacheStream.IsSeekable())) {
|
|
|
|
// If the stream did close normally, then if the server is seekable we'll
|
|
|
|
// just seek to the end of the resource and get an HTTP 416 error because
|
|
|
|
// there's nothing there, so this isn't bad.
|
2011-09-29 16:34:37 -07:00
|
|
|
nsresult rv = CacheClientSeek(mOffset, false);
|
2009-05-17 19:02:20 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
// If the reopen/reseek fails, just fall through and treat this
|
|
|
|
// error as fatal.
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2009-05-17 19:02:20 -07:00
|
|
|
|
|
|
|
if (!mIgnoreClose) {
|
|
|
|
mCacheStream.NotifyDataEnded(aStatus);
|
2011-02-14 15:21:32 -08: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.
|
2012-02-23 17:07:35 -08:00
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
2011-02-14 15:21:32 -08:00
|
|
|
|
2012-02-23 17:07:35 -08:00
|
|
|
if (loadFlags & nsIRequest::LOAD_BACKGROUND) {
|
|
|
|
ModifyLoadFlags(loadFlags & ~nsIRequest::LOAD_BACKGROUND);
|
2011-02-14 15:21:32 -08:00
|
|
|
}
|
2009-05-17 19:02:20 -07:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-13 14:52:50 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::OnChannelRedirect(nsIChannel* aOld, nsIChannel* aNew,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2009-05-13 14:52:50 -07:00
|
|
|
{
|
|
|
|
mChannel = aNew;
|
|
|
|
SetupChannelHeaders();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CopySegmentClosure {
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource* mResource;
|
2009-05-13 14:52:50 -07:00
|
|
|
};
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_METHOD
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::CopySegmentToCache(nsIInputStream *aInStream,
|
2009-03-31 17:52:56 -07:00
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-05-13 14:52:50 -07:00
|
|
|
CopySegmentClosure* closure = static_cast<CopySegmentClosure*>(aClosure);
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
closure->mResource->mDecoder->NotifyDataArrived(aFromSegment, aCount, closure->mResource->mOffset);
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2009-05-17 19:02:20 -07:00
|
|
|
// Keep track of where we're up to
|
2012-02-14 20:35:01 -08:00
|
|
|
closure->mResource->mOffset += aCount;
|
2012-09-29 16:29:04 -07:00
|
|
|
LOG("%p [ChannelMediaResource]: CopySegmentToCache new mOffset = %d",
|
|
|
|
closure->mResource, closure->mResource->mOffset);
|
2012-02-14 20:35:01 -08:00
|
|
|
closure->mResource->mCacheStream.NotifyDataReceived(aCount, aFromSegment,
|
|
|
|
closure->mPrincipal);
|
2009-03-31 17:52:56 -07:00
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::OnDataAvailable(nsIRequest* aRequest,
|
2009-03-31 17:52:56 -07:00
|
|
|
nsIInputStream* aStream,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-03-31 17:52:56 -07:00
|
|
|
mChannelStatistics.AddBytes(aCount);
|
|
|
|
}
|
|
|
|
|
2009-05-13 14:52:50 -07:00
|
|
|
CopySegmentClosure closure;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
if (secMan && mChannel) {
|
|
|
|
secMan->GetChannelPrincipal(mChannel, getter_AddRefs(closure.mPrincipal));
|
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
closure.mResource = this;
|
2009-05-13 14:52:50 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = aCount;
|
2009-03-31 17:52:56 -07:00
|
|
|
while (count > 0) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t read;
|
2009-11-05 05:55:35 -08:00
|
|
|
nsresult rv = aStream->ReadSegments(CopySegmentToCache, &closure, count,
|
2009-03-31 17:52:56 -07:00
|
|
|
&read);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
NS_ASSERTION(read > 0, "Read 0 bytes while data was available?");
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
/* |OpenByteRange|
|
|
|
|
* For terminated byte range requests, use this function.
|
|
|
|
* Callback is |nsBuiltinDecoder|::|NotifyByteRangeDownloaded|().
|
|
|
|
* See |CacheClientSeek| also.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ChannelMediaResource::OpenByteRange(nsIStreamListener** aStreamListener,
|
|
|
|
MediaByteRange const & aByteRange)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
mByteRangeDownloads = true;
|
|
|
|
mByteRange = aByteRange;
|
|
|
|
|
|
|
|
// OpenByteRange may be called multiple times; same URL, different ranges.
|
|
|
|
// For the first call using this URL, forward to Open for some init.
|
|
|
|
if (mByteRangeFirstOpen) {
|
|
|
|
mByteRangeFirstOpen = false;
|
|
|
|
return Open(aStreamListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For subsequent calls, ensure channel is recreated with correct byte range.
|
|
|
|
CloseChannel();
|
|
|
|
|
|
|
|
nsresult rv = RecreateChannel();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return OpenChannel(aStreamListener);
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::Open(nsIStreamListener **aStreamListener)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsresult rv = mCacheStream.Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-05-17 19:02:20 -07:00
|
|
|
NS_ASSERTION(mOffset == 0, "Who set mOffset already?");
|
2009-10-09 04:48:30 -07:00
|
|
|
|
|
|
|
if (!mChannel) {
|
|
|
|
// When we're a clone, the decoder might ask us to Open even though
|
|
|
|
// we haven't established an mChannel (because we might not need one)
|
|
|
|
NS_ASSERTION(!aStreamListener,
|
|
|
|
"Should have already been given a channel if we're to return a stream listener");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-17 19:02:20 -07:00
|
|
|
return OpenChannel(aStreamListener);
|
2009-03-29 18:19:51 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::OpenChannel(nsIStreamListener** aStreamListener)
|
2009-03-29 18:19:51 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
NS_ENSURE_TRUE(mChannel, NS_ERROR_NULL_POINTER);
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(!mListener, "Listener should have been removed by now");
|
2009-03-29 18:19:51 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
if (aStreamListener) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aStreamListener = nullptr;
|
2008-10-29 22:20:08 -07:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
mListener = new Listener(this);
|
|
|
|
NS_ENSURE_TRUE(mListener, NS_ERROR_OUT_OF_MEMORY);
|
2009-03-31 20:19:00 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
if (aStreamListener) {
|
|
|
|
*aStreamListener = mListener;
|
2009-03-29 18:19:51 -07:00
|
|
|
NS_ADDREF(*aStreamListener);
|
2008-10-29 22:20:08 -07:00
|
|
|
} else {
|
2009-05-13 14:52:50 -07:00
|
|
|
mChannel->SetNotificationCallbacks(mListener.get());
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsCOMPtr<nsIStreamListener> listener = mListener.get();
|
|
|
|
|
2009-01-24 03:00:17 -08:00
|
|
|
// Ensure that if we're loading cross domain, that the server is sending
|
|
|
|
// an authorizing Access-Control header.
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-01-24 03:00:17 -08:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
2012-10-01 22:03:24 -07:00
|
|
|
nsRefPtr<nsCORSListenerProxy> crossSiteListener =
|
2011-03-28 13:18:45 -07:00
|
|
|
new nsCORSListenerProxy(mListener,
|
|
|
|
element->NodePrincipal(),
|
2012-09-18 19:16:23 -07:00
|
|
|
false);
|
|
|
|
nsresult rv = crossSiteListener->Init(mChannel);
|
2009-12-15 12:08:59 -08:00
|
|
|
listener = crossSiteListener;
|
|
|
|
NS_ENSURE_TRUE(crossSiteListener, NS_ERROR_OUT_OF_MEMORY);
|
2009-01-24 03:00:17 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-01-25 00:07:43 -08:00
|
|
|
} else {
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(element->NodePrincipal(),
|
|
|
|
mURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
2009-01-25 00:07:43 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-03-31 20:19:00 -07:00
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-05-13 14:52:50 -07:00
|
|
|
SetupChannelHeaders();
|
2009-11-05 05:55:35 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = mChannel->AsyncOpen(listener, nullptr);
|
2008-10-29 22:20:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-30 19:30:23 -07:00
|
|
|
// Tell the media element that we are fetching data from a channel.
|
|
|
|
element->DownloadResumed(true);
|
2008-10-29 22:20:08 -07:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void ChannelMediaResource::SetupChannelHeaders()
|
2009-05-13 14:52:50 -07: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) {
|
2012-09-29 16:29:04 -07:00
|
|
|
// Use |mByteRange| for a specific chunk, or |mOffset| if seeking in a
|
|
|
|
// complete file download.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString rangeString("bytes=");
|
2012-09-29 16:29:04 -07:00
|
|
|
if (!mByteRange.IsNull()) {
|
|
|
|
rangeString.AppendInt(mByteRange.mStart);
|
|
|
|
mOffset = mByteRange.mStart;
|
|
|
|
} else {
|
|
|
|
rangeString.AppendInt(mOffset);
|
|
|
|
}
|
2009-05-13 14:52:50 -07:00
|
|
|
rangeString.Append("-");
|
2012-09-29 16:29:04 -07:00
|
|
|
if (!mByteRange.IsNull()) {
|
|
|
|
rangeString.AppendInt(mByteRange.mEnd);
|
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
hc->SetRequestHeader(NS_LITERAL_CSTRING("Range"), rangeString, false);
|
2010-07-28 21:58:07 -07:00
|
|
|
|
|
|
|
// Send Accept header for video and audio types only (Bug 489071)
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2010-07-28 21:58:07 -07:00
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-16 10:36:23 -07:00
|
|
|
element->SetRequestHeaders(hc);
|
2009-05-13 14:52:50 -07:00
|
|
|
} else {
|
2009-05-17 19:02:20 -07:00
|
|
|
NS_ASSERTION(mOffset == 0, "Don't know how to seek on this channel type");
|
2009-05-13 14:52:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::Close()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
mCacheStream.Close();
|
|
|
|
CloseChannel();
|
2008-10-19 00:39:21 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
already_AddRefed<nsIPrincipal> ChannelMediaResource::GetCurrentPrincipal()
|
2009-05-13 14:52:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mCacheStream.GetCurrentPrincipal();
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-20 00:55:40 -07:00
|
|
|
bool ChannelMediaResource::CanClone()
|
|
|
|
{
|
|
|
|
return mCacheStream.IsAvailableForSharing();
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:13 -08:00
|
|
|
MediaResource* ChannelMediaResource::CloneData(nsBuiltinDecoder* aDecoder)
|
2009-09-14 19:30:44 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2012-03-20 00:55:40 -07:00
|
|
|
NS_ASSERTION(mCacheStream.IsAvailableForSharing(), "Stream can't be cloned");
|
2009-09-14 19:30:44 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
ChannelMediaResource* resource = new ChannelMediaResource(aDecoder, nullptr, mURI);
|
2012-02-14 20:35:01 -08:00
|
|
|
if (resource) {
|
2009-10-09 04:48:30 -07: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 unneccesary HTTP channel
|
|
|
|
// and perform a useless HTTP transaction.
|
2012-02-14 20:35:01 -08:00
|
|
|
resource->mSuspendCount = 1;
|
|
|
|
resource->mCacheStream.InitAsClone(&mCacheStream);
|
|
|
|
resource->mChannelStatistics = mChannelStatistics;
|
|
|
|
resource->mChannelStatistics.Stop(TimeStamp::Now());
|
2009-09-14 19:30:44 -07:00
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
return resource;
|
2009-09-14 19:30:44 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void ChannelMediaResource::CloseChannel()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-03-29 18:19:51 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-04-11 02:39:24 -07:00
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->Revoke();
|
2012-07-30 07:20:58 -07:00
|
|
|
mListener = nullptr;
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
if (mChannel) {
|
2009-03-31 17:52:56 -07:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Resume the channel before we cancel it
|
2011-04-01 02:43:29 -07:00
|
|
|
PossiblyResume();
|
2009-03-31 17:52:56 -07: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 DocumentViewerImpl::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);
|
2012-07-30 07:20:58 -07:00
|
|
|
mChannel = nullptr;
|
2008-11-18 00:19:32 -08:00
|
|
|
}
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::ReadFromCache(char* aBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aOffset,
|
|
|
|
uint32_t aCount)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.ReadFromCache(aBuffer, aOffset, aCount);
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::Read(char* aBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aBytes)
|
2009-01-21 15:54:40 -08:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
return mCacheStream.Read(aBuffer, aCount, aBytes);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult ChannelMediaResource::Seek(int32_t aWhence, int64_t aOffset)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Remember |aOffset|, because Media Cache may request a diff offset later.
|
|
|
|
if (mByteRangeDownloads) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mSeekOffsetMonitor);
|
|
|
|
mSeekOffset = aOffset;
|
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
return mCacheStream.Seek(aWhence, aOffset);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2012-08-17 10:01:08 -07:00
|
|
|
void ChannelMediaResource::StartSeekingForMetadata()
|
|
|
|
{
|
|
|
|
mSeekingForMetadata = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaResource::EndSeekingForMetadata()
|
|
|
|
{
|
|
|
|
mSeekingForMetadata = false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t ChannelMediaResource::Tell()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
|
|
|
return mCacheStream.Tell();
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult ChannelMediaResource::GetCachedRanges(nsTArray<MediaByteRange>& aRanges)
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.GetCachedRanges(aRanges);
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void ChannelMediaResource::Suspend(bool aCloseImmediately)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-10-09 04:46:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-05-17 19:03:37 -07:00
|
|
|
if (!element) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-17 19:02:20 -07:00
|
|
|
if (mChannel) {
|
|
|
|
if (aCloseImmediately && mCacheStream.IsSeekable()) {
|
|
|
|
// Kill off our channel right now, but don't tell anyone about it.
|
2011-09-29 16:34:37 -07:00
|
|
|
mIgnoreClose = true;
|
2009-05-17 19:02:20 -07:00
|
|
|
CloseChannel();
|
2009-05-17 19:03:37 -07:00
|
|
|
element->DownloadSuspended();
|
2009-05-17 19:02:20 -07:00
|
|
|
} else if (mSuspendCount == 0) {
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-05-17 19:02:20 -07:00
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
|
|
|
}
|
2011-04-01 02:43:29 -07:00
|
|
|
PossiblySuspend();
|
2009-05-17 19:03:37 -07:00
|
|
|
element->DownloadSuspended();
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
}
|
2009-05-17 19:02:20 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
++mSuspendCount;
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void ChannelMediaResource::Resume()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-10-09 04:46:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-05-17 19:02:20 -07:00
|
|
|
NS_ASSERTION(mSuspendCount > 0, "Too many resumes!");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-05-17 19:03:37 -07:00
|
|
|
if (!element) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-09 04:48:30 -07:00
|
|
|
NS_ASSERTION(mSuspendCount > 0, "Resume without previous Suspend!");
|
2009-03-31 17:52:56 -07:00
|
|
|
--mSuspendCount;
|
2009-05-17 19:02:20 -07:00
|
|
|
if (mSuspendCount == 0) {
|
|
|
|
if (mChannel) {
|
|
|
|
// Just wake up our existing channel
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-05-17 19:02:20 -07:00
|
|
|
mChannelStatistics.Start(TimeStamp::Now());
|
|
|
|
}
|
|
|
|
// if an error occurs after Resume, assume it's because the server
|
|
|
|
// timed out the connection and we should reopen it.
|
2011-09-29 16:34:37 -07:00
|
|
|
mReopenOnError = true;
|
2011-04-01 02:43:29 -07:00
|
|
|
PossiblyResume();
|
2009-05-17 19:03:37 -07:00
|
|
|
element->DownloadResumed();
|
2009-05-17 19:02:20 -07:00
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t totalLength = mCacheStream.GetLength();
|
2009-09-20 20:47:47 -07:00
|
|
|
// If mOffset is at the end of the stream, then we shouldn't try to
|
|
|
|
// seek to it. The seek will fail and be wasted anyway. We can leave
|
|
|
|
// the channel dead; if the media cache wants to read some other data
|
|
|
|
// in the future, it will call CacheClientSeek itself which will reopen the
|
|
|
|
// channel.
|
|
|
|
if (totalLength < 0 || mOffset < totalLength) {
|
|
|
|
// There is (or may be) data to read at mOffset, so start reading it.
|
|
|
|
// Need to recreate the channel.
|
2011-09-29 16:34:37 -07:00
|
|
|
CacheClientSeek(mOffset, false);
|
2009-09-20 20:47:47 -07:00
|
|
|
}
|
2009-05-17 19:03:37 -07:00
|
|
|
element->DownloadResumed();
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::RecreateChannel()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-04-09 18:28:24 -07:00
|
|
|
nsLoadFlags loadFlags =
|
|
|
|
nsICachingChannel::LOAD_BYPASS_LOCAL_CACHE_IF_BUSY |
|
|
|
|
(mLoadInBackground ? nsIRequest::LOAD_BACKGROUND : 0);
|
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// The decoder is being shut down, so don't bother opening a new channel
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-04-09 18:28:24 -07:00
|
|
|
if (!element) {
|
2009-03-31 17:52:56 -07:00
|
|
|
// The decoder is being shut down, so don't bother opening a new channel
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-09 18:28:24 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
|
|
|
NS_ENSURE_TRUE(loadGroup, NS_ERROR_NULL_POINTER);
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2012-09-04 17:49:46 -07:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
|
|
|
nullptr,
|
|
|
|
loadGroup,
|
|
|
|
nullptr,
|
|
|
|
loadFlags);
|
|
|
|
|
|
|
|
// We have cached the Content-Type, which should not change. Give a hint to
|
|
|
|
// the channel to avoid a sniffing failure, which would be expected because we
|
|
|
|
// are probably seeking in the middle of the bitstream, and sniffing relies
|
|
|
|
// on the presence of a magic number at the beginning of the stream.
|
|
|
|
nsAutoCString contentType;
|
|
|
|
element->GetMimeType(contentType);
|
|
|
|
NS_ASSERTION(!contentType.IsEmpty(),
|
|
|
|
"When recreating a channel, we should know the Content-Type.");
|
|
|
|
mChannel->SetContentType(contentType);
|
|
|
|
|
|
|
|
return rv;
|
2009-09-14 19:30:44 -07:00
|
|
|
}
|
|
|
|
|
2009-09-14 19:30:45 -07:00
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::DoNotifyDataReceived()
|
2009-09-14 19:30:45 -07:00
|
|
|
{
|
|
|
|
mDataReceivedEvent.Revoke();
|
|
|
|
mDecoder->NotifyBytesDownloaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::CacheClientNotifyDataReceived()
|
2009-09-14 19:30:45 -07:00
|
|
|
{
|
2009-10-09 04:46:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
// NOTE: this can be called with the media cache lock held, so don't
|
|
|
|
// block or do anything which might try to acquire a lock!
|
2009-09-14 19:30:45 -07:00
|
|
|
|
|
|
|
if (mDataReceivedEvent.IsPending())
|
|
|
|
return;
|
|
|
|
|
|
|
|
mDataReceivedEvent =
|
2012-02-14 20:35:01 -08:00
|
|
|
NS_NewNonOwningRunnableMethod(this, &ChannelMediaResource::DoNotifyDataReceived);
|
2009-09-14 19:30:45 -07:00
|
|
|
NS_DispatchToMainThread(mDataReceivedEvent.get(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataEnded : public nsRunnable {
|
|
|
|
public:
|
2012-11-14 11:45:13 -08:00
|
|
|
DataEnded(nsBuiltinDecoder* aDecoder, nsresult aStatus) :
|
2009-09-14 19:30:45 -07:00
|
|
|
mDecoder(aDecoder), mStatus(aStatus) {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyDownloadEnded(mStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2012-11-14 11:45:13 -08:00
|
|
|
nsRefPtr<nsBuiltinDecoder> mDecoder;
|
2009-09-14 19:30:45 -07:00
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::CacheClientNotifyDataEnded(nsresult aStatus)
|
2009-09-14 19:30:45 -07:00
|
|
|
{
|
2009-10-09 04:46:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
// NOTE: this can be called with the media cache lock held, so don't
|
|
|
|
// block or do anything which might try to acquire a lock!
|
2009-09-14 19:30:45 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new DataEnded(mDecoder, aStatus);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:12:42 -07:00
|
|
|
void
|
|
|
|
ChannelMediaResource::CacheClientNotifyPrincipalChanged()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
|
|
|
|
mDecoder->NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
|
2009-09-14 19:30:44 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
ChannelMediaResource::CacheClientSeek(int64_t aOffset, bool aResume)
|
2009-09-14 19:30:44 -07:00
|
|
|
{
|
2009-10-09 04:46:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-09-14 19:30:44 -07:00
|
|
|
|
|
|
|
CloseChannel();
|
|
|
|
|
2009-05-17 19:02:20 -07:00
|
|
|
if (aResume) {
|
|
|
|
NS_ASSERTION(mSuspendCount > 0, "Too many resumes!");
|
|
|
|
// No need to mess with the channel, since we're making a new one
|
|
|
|
--mSuspendCount;
|
|
|
|
}
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Note: For chunked downloads, e.g. DASH, we need to determine which chunk
|
|
|
|
// contains the requested offset, |mOffset|. This is either previously
|
|
|
|
// requested in |Seek| or updated to the most recent bytes downloaded.
|
|
|
|
// So the process below is:
|
|
|
|
// 1 - Query decoder for chunk containing desired offset, |mOffset|.
|
|
|
|
// Return silently if the offset is not available; suggests decoder is
|
|
|
|
// yet to get range information.
|
|
|
|
// Return with NetworkError for all other errors.
|
|
|
|
//
|
|
|
|
// 2 - Adjust |mByteRange|.mStart to |aOffset|, requested by media cache.
|
|
|
|
// For seeking, the media cache always requests the start of the cache
|
|
|
|
// block, so we need to adjust the first chunk of a seek.
|
|
|
|
// E.g. For "DASH-WebM On Demand" this means the first chunk after
|
|
|
|
// seeking will most likely be larger than the subsegment (cluster).
|
|
|
|
//
|
|
|
|
// 3 - Call |OpenByteRange| requesting |mByteRange| bytes.
|
|
|
|
|
|
|
|
if (mByteRangeDownloads) {
|
|
|
|
// Query decoder for chunk containing desired offset.
|
|
|
|
nsresult rv;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mSeekOffsetMonitor);
|
|
|
|
// Ensure that media cache can only request an equal or smaller offset;
|
|
|
|
// it may be trying to include the start of a cache block.
|
|
|
|
NS_ENSURE_TRUE(aOffset <= mSeekOffset, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
rv = mDecoder->GetByteRangeForSeek(mSeekOffset, mByteRange);
|
|
|
|
mSeekOffset = -1;
|
|
|
|
}
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
// Assume decoder will request correct bytes when range information
|
|
|
|
// becomes available. Return silently.
|
|
|
|
return NS_OK;
|
|
|
|
} else if (NS_FAILED(rv) || mByteRange.IsNull()) {
|
|
|
|
// Decoder reported an error we don't want to handle here; just return.
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
CloseChannel();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// Media cache may decrease offset to start of cache data block.
|
|
|
|
// Adjust start of byte range accordingly.
|
|
|
|
mByteRange.mStart = mOffset = aOffset;
|
|
|
|
return OpenByteRange(nullptr, mByteRange);
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:50:13 -07:00
|
|
|
mOffset = aOffset;
|
|
|
|
|
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Close the existing channel to force the channel to be recreated at
|
|
|
|
// the correct offset upon resume.
|
|
|
|
if (mChannel) {
|
|
|
|
mIgnoreClose = true;
|
|
|
|
CloseChannel();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-14 19:30:44 -07:00
|
|
|
nsresult rv = RecreateChannel();
|
2009-03-31 17:52:56 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-09-14 19:30:44 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return OpenChannel(nullptr);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::CacheClientSuspend()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
Suspend(false);
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-10-09 04:46:23 -07:00
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
nsresult
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::CacheClientResume()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
Resume();
|
|
|
|
|
2009-10-09 04:46:23 -07:00
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
2009-03-31 17:52:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t
|
|
|
|
ChannelMediaResource::GetNextCachedData(int64_t aOffset)
|
2009-05-17 15:15:57 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.GetNextCachedData(aOffset);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t
|
|
|
|
ChannelMediaResource::GetCachedDataEnd(int64_t aOffset)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.GetCachedDataEnd(aOffset);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
ChannelMediaResource::IsDataCachedToEndOfResource(int64_t aOffset)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.IsDataCachedToEndOfStream(aOffset);
|
|
|
|
}
|
|
|
|
|
2011-11-29 21:05:49 -08:00
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::EnsureCacheUpToDate()
|
2011-11-29 21:05:49 -08:00
|
|
|
{
|
|
|
|
mCacheStream.EnsureCacheUpdate();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::IsSuspendedByCache(MediaResource** aActiveResource)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
return mCacheStream.AreAllStreamsForResourceSuspended(aActiveResource);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::IsSuspended()
|
2010-08-12 17:41:47 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2010-08-12 17:41:47 -07:00
|
|
|
return mSuspendCount > 0;
|
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::SetReadMode(nsMediaCacheStream::ReadMode aMode)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
mCacheStream.SetReadMode(aMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
ChannelMediaResource::SetPlaybackRate(uint32_t aBytesPerSecond)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
mCacheStream.SetPlaybackRate(aBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Pin()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
mCacheStream.Pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::Unpin()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
mCacheStream.Unpin();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::GetDownloadRate(bool* aIsReliable)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-04-11 02:39:24 -07:00
|
|
|
return mChannelStatistics.GetRate(TimeStamp::Now(), aIsReliable);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::GetLength()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
return mCacheStream.GetLength();
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
|
|
|
|
2011-04-01 02:43:29 -07:00
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::PossiblySuspend()
|
2011-04-01 02:43:29 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isPending = false;
|
2011-04-01 02:43:29 -07:00
|
|
|
nsresult rv = mChannel->IsPending(&isPending);
|
|
|
|
if (NS_SUCCEEDED(rv) && isPending) {
|
|
|
|
mChannel->Suspend();
|
2011-09-29 16:34:37 -07:00
|
|
|
mIgnoreResume = false;
|
2011-04-01 02:43:29 -07:00
|
|
|
} else {
|
2011-09-29 16:34:37 -07:00
|
|
|
mIgnoreResume = true;
|
2011-04-01 02:43:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-14 20:35:01 -08:00
|
|
|
ChannelMediaResource::PossiblyResume()
|
2011-04-01 02:43:29 -07:00
|
|
|
{
|
|
|
|
if (!mIgnoreResume) {
|
|
|
|
mChannel->Resume();
|
|
|
|
} else {
|
2011-09-29 16:34:37 -07:00
|
|
|
mIgnoreResume = false;
|
2011-04-01 02:43:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
class FileMediaResource : public MediaResource
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-11-14 11:45:13 -08:00
|
|
|
FileMediaResource(nsBuiltinDecoder* aDecoder, nsIChannel* aChannel, nsIURI* aURI) :
|
2012-09-03 17:05:41 -07:00
|
|
|
MediaResource(aDecoder, aChannel, aURI),
|
|
|
|
mSize(-1),
|
2012-08-28 20:55:57 -07:00
|
|
|
mLock("FileMediaResource.mLock"),
|
|
|
|
mSizeInitialized(false)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
~FileMediaResource()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
|
|
|
}
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
// Main thread
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual nsresult Open(nsIStreamListener** aStreamListener);
|
2008-10-19 00:39:21 -07:00
|
|
|
virtual nsresult Close();
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual void Suspend(bool aCloseImmediately) {}
|
2009-03-31 17:52:56 -07:00
|
|
|
virtual void Resume() {}
|
2009-05-13 14:52:50 -07:00
|
|
|
virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal();
|
2012-03-20 00:55:40 -07:00
|
|
|
virtual bool CanClone();
|
2012-11-14 11:45:13 -08:00
|
|
|
virtual MediaResource* CloneData(nsBuiltinDecoder* aDecoder);
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount);
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
// These methods are called off the main thread.
|
|
|
|
|
|
|
|
// Other thread
|
|
|
|
virtual void SetReadMode(nsMediaCacheStream::ReadMode aMode) {}
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual void SetPlaybackRate(uint32_t aBytesPerSecond) {}
|
|
|
|
virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
|
|
|
|
virtual nsresult Seek(int32_t aWhence, int64_t aOffset);
|
2012-08-17 10:01:08 -07:00
|
|
|
virtual void StartSeekingForMetadata() {};
|
|
|
|
virtual void EndSeekingForMetadata() {};
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int64_t Tell();
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
// Any thread
|
|
|
|
virtual void Pin() {}
|
|
|
|
virtual void Unpin() {}
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual double GetDownloadRate(bool* aIsReliable)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
// The data's all already here
|
2011-09-29 16:34:37 -07:00
|
|
|
*aIsReliable = true;
|
2009-03-31 17:52:56 -07:00
|
|
|
return 100*1024*1024; // arbitray, use 100MB/s
|
|
|
|
}
|
2012-08-28 20:55:57 -07:00
|
|
|
virtual int64_t GetLength() {
|
|
|
|
MutexAutoLock lock(mLock);
|
2012-09-03 17:05:41 -07:00
|
|
|
if (mInput) {
|
|
|
|
EnsureSizeInitialized();
|
|
|
|
}
|
|
|
|
return mSizeInitialized ? mSize : 0;
|
2012-08-28 20:55:57 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int64_t GetNextCachedData(int64_t aOffset)
|
2009-05-17 15:15:57 -07:00
|
|
|
{
|
2012-09-03 17:05:41 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (!mInput) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EnsureSizeInitialized();
|
2009-05-17 15:15:57 -07:00
|
|
|
return (aOffset < mSize) ? aOffset : -1;
|
|
|
|
}
|
2012-09-03 17:05:41 -07:00
|
|
|
virtual int64_t GetCachedDataEnd(int64_t aOffset) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (!mInput) {
|
|
|
|
return aOffset;
|
|
|
|
}
|
|
|
|
EnsureSizeInitialized();
|
|
|
|
return NS_MAX(aOffset, mSize);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual bool IsDataCachedToEndOfResource(int64_t aOffset) { return true; }
|
2012-02-14 20:35:01 -08:00
|
|
|
virtual bool IsSuspendedByCache(MediaResource** aActiveResource)
|
2011-12-08 20:48:40 -08:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
if (aActiveResource) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aActiveResource = nullptr;
|
2011-12-08 20:48:40 -08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool IsSuspended() { return false; }
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult GetCachedRanges(nsTArray<MediaByteRange>& aRanges);
|
2011-03-23 15:28:58 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
private:
|
2012-08-28 20:55:57 -07:00
|
|
|
// Ensures mSize is initialized, if it can be.
|
2012-09-03 17:05:41 -07:00
|
|
|
// mLock must be held when this is called, and mInput must be non-null.
|
|
|
|
void EnsureSizeInitialized();
|
2012-08-28 20:55:57 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
// The file size, or -1 if not known. Immutable after Open().
|
2012-09-03 17:05:41 -07:00
|
|
|
// Can be used from any thread.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mSize;
|
2009-03-31 17:52:56 -07:00
|
|
|
|
|
|
|
// This lock handles synchronisation between calls to Close() and
|
|
|
|
// the Read, Seek, etc calls. Close must not be called while a
|
|
|
|
// Read or Seek is in progress since it resets various internal
|
|
|
|
// values to null.
|
2012-09-03 17:05:41 -07:00
|
|
|
// This lock protects mSeekable, mInput, mSize, and mSizeInitialized.
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
Mutex mLock;
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Seekable stream interface to file. This can be used from any
|
|
|
|
// thread.
|
|
|
|
nsCOMPtr<nsISeekableStream> mSeekable;
|
|
|
|
|
|
|
|
// Input stream for the media data. This can be used from any
|
2012-09-03 17:05:41 -07:00
|
|
|
// thread. This is annulled when the decoder is being shutdown.
|
|
|
|
// The decoder can be shut down while we're calculating buffered
|
|
|
|
// ranges or seeking, so this must be null-checked before it's used.
|
2008-10-19 00:39:21 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mInput;
|
2012-08-28 20:55:57 -07:00
|
|
|
|
|
|
|
// 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;
|
2008-10-19 00:39:21 -07:00
|
|
|
};
|
|
|
|
|
2009-11-05 05:55:35 -08:00
|
|
|
class LoadedEvent : public nsRunnable
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
|
|
|
public:
|
2012-11-14 11:45:13 -08:00
|
|
|
LoadedEvent(nsBuiltinDecoder* aDecoder) :
|
2009-03-31 17:52:56 -07:00
|
|
|
mDecoder(aDecoder)
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(LoadedEvent);
|
|
|
|
}
|
|
|
|
~LoadedEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(LoadedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyDownloadEnded(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-11-14 11:45:13 -08:00
|
|
|
nsRefPtr<nsBuiltinDecoder> mDecoder;
|
2009-02-05 00:02:21 -08:00
|
|
|
};
|
|
|
|
|
2012-09-03 17:05:41 -07:00
|
|
|
void FileMediaResource::EnsureSizeInitialized()
|
2012-08-28 20:55:57 -07:00
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2012-09-03 17:05:41 -07:00
|
|
|
NS_ASSERTION(mInput, "Must have file input stream");
|
|
|
|
if (mSizeInitialized) {
|
2012-08-28 20:55:57 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSizeInitialized = true;
|
|
|
|
// Get the file size and inform the decoder.
|
|
|
|
uint64_t size;
|
|
|
|
nsresult res = mInput->Available(&size);
|
2012-09-27 23:57:33 -07:00
|
|
|
if (NS_SUCCEEDED(res) && size <= INT64_MAX) {
|
2012-08-28 20:55:57 -07:00
|
|
|
mSize = (int64_t)size;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new LoadedEvent(mDecoder);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult FileMediaResource::GetCachedRanges(nsTArray<MediaByteRange>& aRanges)
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
2012-08-28 20:55:57 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-09-03 17:05:41 -07:00
|
|
|
if (!mInput) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
EnsureSizeInitialized();
|
2011-03-23 15:28:58 -07:00
|
|
|
if (mSize == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
aRanges.AppendElement(MediaByteRange(0, mSize));
|
2011-03-23 15:28:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult FileMediaResource::Open(nsIStreamListener** aStreamListener)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
if (aStreamListener) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aStreamListener = nullptr;
|
2008-10-29 22:20:08 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 05:04:12 -07:00
|
|
|
nsresult rv = NS_OK;
|
2008-10-29 22:20:08 -07:00
|
|
|
if (aStreamListener) {
|
|
|
|
// The channel is already open. We need a synchronous stream that
|
|
|
|
// implements nsISeekableStream, so we have to find the underlying
|
|
|
|
// file and reopen it
|
|
|
|
nsCOMPtr<nsIFileChannel> fc(do_QueryInterface(mChannel));
|
2012-09-04 14:53:52 -07:00
|
|
|
if (fc) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fc->GetFile(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-23 03:42:14 -07:00
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(mInput), file);
|
|
|
|
} else if (IsBlobURI(mURI)) {
|
|
|
|
rv = NS_GetStreamForBlobURI(mURI, getter_AddRefs(mInput));
|
|
|
|
}
|
2008-10-29 22:20:08 -07:00
|
|
|
} else {
|
2009-11-05 05:55:35 -08:00
|
|
|
// Ensure that we never load a local file from some page on a
|
2009-01-25 00:07:54 -08:00
|
|
|
// web server.
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-01-25 00:07:54 -08:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(element->NodePrincipal(),
|
|
|
|
mURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
rv = mChannel->Open(getter_AddRefs(mInput));
|
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mSeekable = do_QueryInterface(mInput);
|
2008-10-29 22:20:08 -07:00
|
|
|
if (!mSeekable) {
|
|
|
|
// XXX The file may just be a .url or similar
|
|
|
|
// shortcut that points to a Web site. We need to fix this by
|
|
|
|
// doing an async open and waiting until we locate the real resource,
|
|
|
|
// then using that (if it's still a file!).
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
return NS_OK;
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult FileMediaResource::Close()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2008-10-19 00:39:21 -07:00
|
|
|
if (mChannel) {
|
2009-03-31 17:52:56 -07:00
|
|
|
mChannel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
2012-07-30 07:20:58 -07:00
|
|
|
mChannel = nullptr;
|
|
|
|
mInput = nullptr;
|
|
|
|
mSeekable = nullptr;
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
already_AddRefed<nsIPrincipal> FileMediaResource::GetCurrentPrincipal()
|
2009-05-13 14:52:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!secMan || !mChannel)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-05-13 14:52:50 -07:00
|
|
|
secMan->GetChannelPrincipal(mChannel, getter_AddRefs(principal));
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-20 00:55:40 -07:00
|
|
|
bool FileMediaResource::CanClone()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:13 -08:00
|
|
|
MediaResource* FileMediaResource::CloneData(nsBuiltinDecoder* aDecoder)
|
2009-09-14 19:30:44 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// The decoder is being shut down, so we can't clone
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-09-14 19:30:44 -07:00
|
|
|
if (!element) {
|
|
|
|
// The decoder is being shut down, so we can't clone
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-09-14 19:30:44 -07:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(loadGroup, nullptr);
|
2009-09-14 19:30:44 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv =
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_NewChannel(getter_AddRefs(channel), mURI, nullptr, loadGroup, nullptr, 0);
|
2009-09-14 19:30:44 -07:00
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-09-14 19:30:44 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
return new FileMediaResource(aDecoder, channel, mURI);
|
2009-09-14 19:30:44 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult FileMediaResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2010-08-05 00:40:35 -07:00
|
|
|
if (!mInput || !mSeekable)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-03 17:05:41 -07:00
|
|
|
EnsureSizeInitialized();
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t offset = 0;
|
2010-08-05 00:40:35 -07:00
|
|
|
nsresult res = mSeekable->Tell(&offset);
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
res = mSeekable->Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead = 0;
|
2010-08-05 00:40:35 -07:00
|
|
|
do {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t x = 0;
|
|
|
|
uint32_t bytesToRead = aCount - bytesRead;
|
2010-08-05 00:40:35 -07:00
|
|
|
res = mInput->Read(aBuffer, bytesToRead, &x);
|
|
|
|
bytesRead += x;
|
|
|
|
} while (bytesRead != aCount && res == NS_OK);
|
|
|
|
|
|
|
|
// Reset read head to original position so we don't disturb any other
|
|
|
|
// reading thread.
|
|
|
|
nsresult seekres = mSeekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
|
|
|
|
// If a read failed in the loop above, we want to return its failure code.
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
|
|
|
|
// Else we succeed if the reset-seek succeeds.
|
|
|
|
return seekres;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult FileMediaResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-02-05 00:02:21 -08:00
|
|
|
if (!mInput)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-03 17:05:41 -07:00
|
|
|
EnsureSizeInitialized();
|
2009-02-05 00:02:21 -08:00
|
|
|
return mInput->Read(aBuffer, aCount, aBytes);
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult FileMediaResource::Seek(int32_t aWhence, int64_t aOffset)
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2009-03-31 20:19:00 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-03-31 17:52:56 -07:00
|
|
|
if (!mSeekable)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-03 17:05:41 -07:00
|
|
|
EnsureSizeInitialized();
|
2009-03-31 17:52:56 -07:00
|
|
|
return mSeekable->Seek(aWhence, aOffset);
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t FileMediaResource::Tell()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2008-10-19 00:39:21 -07:00
|
|
|
if (!mSeekable)
|
|
|
|
return 0;
|
2012-09-03 17:05:41 -07:00
|
|
|
EnsureSizeInitialized();
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t offset = 0;
|
2008-10-19 00:39:21 -07:00
|
|
|
mSeekable->Tell(&offset);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource*
|
2012-11-14 11:45:13 -08:00
|
|
|
MediaResource::Create(nsBuiltinDecoder* aDecoder, nsIChannel* aChannel)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2009-11-05 05:55:35 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
2012-09-04 14:53:52 -07:00
|
|
|
"MediaResource::Open called on non-main thread");
|
2009-03-31 17:52:56 -07:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
// If the channel was redirected, we want the post-redirect URI;
|
|
|
|
// but if the URI scheme was expanded, say from chrome: to jar:file:,
|
|
|
|
// we want the original URI.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
nsCOMPtr<nsIFileChannel> fc = do_QueryInterface(aChannel);
|
2012-09-04 14:53:52 -07:00
|
|
|
if (fc || IsBlobURI(uri)) {
|
2012-02-14 20:35:01 -08:00
|
|
|
return new FileMediaResource(aDecoder, aChannel, uri);
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
return new ChannelMediaResource(aDecoder, aChannel, uri);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void MediaResource::MoveLoadsToBackground() {
|
2009-04-09 18:28:24 -07:00
|
|
|
NS_ASSERTION(!mLoadInBackground, "Why are you calling this more than once?");
|
2011-09-29 16:34:37 -07:00
|
|
|
mLoadInBackground = true;
|
2009-04-09 18:28:24 -07:00
|
|
|
if (!mChannel) {
|
|
|
|
// No channel, resource is probably already loaded.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-08 21:52:53 -08:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
NS_WARNING("Null owner in MediaResource::MoveLoadsToBackground()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsHTMLMediaElement* element = owner->GetMediaElement();
|
2009-04-09 18:28:24 -07:00
|
|
|
if (!element) {
|
2012-02-14 20:35:01 -08:00
|
|
|
NS_WARNING("Null element in MediaResource::MoveLoadsToBackground()");
|
2009-04-09 18:28:24 -07:00
|
|
|
return;
|
|
|
|
}
|
2011-02-14 15:21:32 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isPending = false;
|
2011-02-14 15:21:32 -08:00
|
|
|
if (NS_SUCCEEDED(mChannel->IsPending(&isPending)) &&
|
|
|
|
isPending) {
|
|
|
|
nsLoadFlags loadFlags;
|
2012-03-28 06:14:33 -07:00
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadFlags(&loadFlags);
|
2011-02-14 15:21:32 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
|
|
|
|
|
|
|
loadFlags |= nsIRequest::LOAD_BACKGROUND;
|
|
|
|
ModifyLoadFlags(loadFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
void MediaResource::ModifyLoadFlags(nsLoadFlags aFlags)
|
2011-02-14 15:21:32 -08:00
|
|
|
{
|
2009-04-09 18:28:24 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2012-03-28 06:14:33 -07:00
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2009-04-09 18:28:24 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadGroup() failed!");
|
2011-02-14 15:21:32 -08:00
|
|
|
|
2009-04-09 18:28:24 -07:00
|
|
|
nsresult status;
|
|
|
|
mChannel->GetStatus(&status);
|
2011-02-14 15:21:32 -08:00
|
|
|
|
2009-04-09 18:28:24 -07:00
|
|
|
// Note: if (NS_FAILED(status)), the channel won't be in the load group.
|
|
|
|
if (loadGroup &&
|
2011-02-14 15:21:32 -08:00
|
|
|
NS_SUCCEEDED(status)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = loadGroup->RemoveRequest(mChannel, nullptr, status);
|
2009-04-09 18:28:24 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveRequest() failed!");
|
2011-02-14 15:21:32 -08:00
|
|
|
}
|
2009-04-09 18:28:24 -07:00
|
|
|
|
2011-02-14 15:21:32 -08:00
|
|
|
rv = mChannel->SetLoadFlags(aFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "SetLoadFlags() failed!");
|
2009-04-09 18:28:24 -07:00
|
|
|
|
2011-02-14 15:21:32 -08:00
|
|
|
if (loadGroup &&
|
|
|
|
NS_SUCCEEDED(status)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = loadGroup->AddRequest(mChannel, nullptr);
|
2009-04-09 18:28:24 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AddRequest() failed!");
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|