2008-10-19 11:39:21 +04: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 15:12:37 +04: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 17:29:50 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
#include "MediaResource.h"
|
2013-09-23 13:53:36 +04:00
|
|
|
#include "RtspMediaResource.h"
|
2011-08-11 17:29:50 +04: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-04-01 08:29:02 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2008-10-19 11:39:21 +04:00
|
|
|
#include "nsDebug.h"
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "MediaDecoder.h"
|
2008-10-19 11:39:21 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileChannel.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2009-01-24 14:00:17 +03:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2013-03-19 16:23:54 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2009-04-01 04:52:56 +04:00
|
|
|
#include "nsICachingChannel.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-09-25 07:24:44 +04:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2014-02-26 04:36:54 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2014-09-21 20:37:32 +04:00
|
|
|
#include "nsIContentPolicy.h"
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-09-30 03:29:04 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gMediaResourceLog;
|
2013-11-21 07:02:42 +04:00
|
|
|
#define RESOURCE_LOG(msg, ...) PR_LOG(gMediaResourceLog, PR_LOG_DEBUG, \
|
|
|
|
(msg, ##__VA_ARGS__))
|
2012-09-30 03:29:04 +04:00
|
|
|
// Debug logging macro with object pointer and class name.
|
|
|
|
#define CMLOG(msg, ...) \
|
2013-11-21 07:02:42 +04:00
|
|
|
RESOURCE_LOG("%p [ChannelMediaResource]: " msg, this, ##__VA_ARGS__)
|
2012-09-30 03:29:04 +04:00
|
|
|
#else
|
2013-11-21 07:02:42 +04:00
|
|
|
#define RESOURCE_LOG(msg, ...)
|
2012-09-30 03:29:04 +04:00
|
|
|
#define CMLOG(msg, ...)
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t HTTP_OK_CODE = 200;
|
|
|
|
static const uint32_t HTTP_PARTIAL_RESPONSE_CODE = 206;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
2009-04-11 13:39:24 +04:00
|
|
|
|
2014-02-26 04:36:54 +04:00
|
|
|
void
|
|
|
|
MediaResource::Destroy()
|
|
|
|
{
|
|
|
|
// If we're being destroyed on a non-main thread, we AddRef again and
|
|
|
|
// use a proxy to release the MediaResource on the main thread, where
|
|
|
|
// the MediaResource is deleted. This ensures we only delete the
|
|
|
|
// MediaResource on the main thread.
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
NS_ENSURE_TRUE_VOID(mainThread);
|
|
|
|
nsRefPtr<MediaResource> doomed(this);
|
|
|
|
if (NS_FAILED(NS_ProxyRelease(mainThread, doomed, true))) {
|
|
|
|
NS_WARNING("Failed to proxy release to main thread!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(MediaResource)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(MediaResource, Destroy())
|
|
|
|
NS_IMPL_QUERY_INTERFACE0(MediaResource)
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
ChannelMediaResource::ChannelMediaResource(MediaDecoder* aDecoder,
|
2013-02-15 05:10:58 +04:00
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsACString& aContentType)
|
|
|
|
: BaseMediaResource(aDecoder, aChannel, aURI, aContentType),
|
2009-11-05 16:55:35 +03:00
|
|
|
mOffset(0), mSuspendCount(0),
|
2011-09-30 03:34:37 +04:00
|
|
|
mReopenOnError(false), mIgnoreClose(false),
|
2013-06-27 15:30:41 +04:00
|
|
|
mCacheStream(MOZ_THIS_IN_INITIALIZER_LIST()),
|
2012-02-15 08:35:01 +04:00
|
|
|
mLock("ChannelMediaResource.mLock"),
|
2012-08-17 21:01:08 +04:00
|
|
|
mIgnoreResume(false),
|
2013-04-03 04:05:00 +04:00
|
|
|
mIsTransportSeekable(true)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2012-09-30 03:29:04 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gMediaResourceLog) {
|
|
|
|
gMediaResourceLog = PR_NewLogModule("MediaResource");
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::~ChannelMediaResource()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mListener) {
|
|
|
|
// Kill its reference to us since we're going away
|
|
|
|
mListener->Revoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
// ChannelMediaResource::Listener just observes the channel and
|
|
|
|
// forwards notifications to the ChannelMediaResource. We use multiple
|
2009-04-01 04:52:56 +04:00
|
|
|
// listener objects so that when we open a new stream for a seek we can
|
2012-02-15 08:35:01 +04:00
|
|
|
// disconnect the old listener from the ChannelMediaResource and hook up
|
2009-04-01 04:52:56 +04:00
|
|
|
// a new listener, so notifications from the old channel are discarded
|
|
|
|
// and don't confuse us.
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ChannelMediaResource::Listener,
|
|
|
|
nsIRequestObserver, nsIStreamListener, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor)
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Listener::OnStartRequest(nsIRequest* aRequest,
|
2009-04-01 04:52:56 +04:00
|
|
|
nsISupports* aContext)
|
|
|
|
{
|
2012-02-15 08:35:01 +04:00
|
|
|
if (!mResource)
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
2012-02-15 08:35:01 +04:00
|
|
|
return mResource->OnStartRequest(aRequest);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Listener::OnStopRequest(nsIRequest* aRequest,
|
2009-04-01 04:52:56 +04:00
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-02-15 08:35:01 +04:00
|
|
|
if (!mResource)
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
2012-02-15 08:35:01 +04:00
|
|
|
return mResource->OnStopRequest(aRequest, aStatus);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Listener::OnDataAvailable(nsIRequest* aRequest,
|
2009-11-05 16:55:35 +03:00
|
|
|
nsISupports* aContext,
|
2009-04-01 04:52:56 +04:00
|
|
|
nsIInputStream* aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2012-02-15 08:35:01 +04:00
|
|
|
if (!mResource)
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
2012-02-15 08:35:01 +04:00
|
|
|
return mResource->OnDataAvailable(aRequest, aStream, aCount);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Listener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIChannel* aNewChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIAsyncVerifyRedirectCallback* cb)
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
2010-08-05 06:15:55 +04:00
|
|
|
nsresult rv = NS_OK;
|
2012-02-15 08:35:01 +04:00
|
|
|
if (mResource)
|
|
|
|
rv = mResource->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Listener::GetInterface(const nsIID & aIID, void **aResult)
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::OnStartRequest(nsIRequest* aRequest)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
2009-09-30 01:32:30 +04:00
|
|
|
nsresult status;
|
|
|
|
nsresult rv = aRequest->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-02-13 08:08:55 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
2011-03-29 00:18:45 +04: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-30 01:32:30 +04:00
|
|
|
if (status == NS_ERROR_DOM_BAD_URI) {
|
2009-04-01 04:52:56 +04:00
|
|
|
mDecoder->NetworkError();
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(aRequest);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool seekable = false;
|
2009-04-01 04:52:56 +04:00
|
|
|
if (hc) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t responseStatus = 0;
|
2009-09-30 01:32:30 +04:00
|
|
|
hc->GetResponseStatus(&responseStatus);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded = false;
|
2009-09-30 01:32:30 +04: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".
|
2013-04-24 13:18:56 +04:00
|
|
|
if (responseStatus == HTTP_REQUESTED_RANGE_NOT_SATISFIABLE_CODE) {
|
|
|
|
// OnStopRequest will not be fired, so we need to do some of its
|
|
|
|
// work here.
|
|
|
|
mCacheStream.NotifyDataEnded(status);
|
|
|
|
} else {
|
2009-09-30 01:32:30 +04:00
|
|
|
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-02 06:35:17 +04:00
|
|
|
nsAutoCString ranges;
|
2009-04-01 04:52:56 +04:00
|
|
|
hc->GetResponseHeader(NS_LITERAL_CSTRING("Accept-Ranges"),
|
|
|
|
ranges);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool acceptsRanges = ranges.EqualsLiteral("bytes");
|
2013-08-01 08:02:40 +04:00
|
|
|
// True if this channel will not return an unbounded amount of data
|
|
|
|
bool dataIsBounded = false;
|
|
|
|
|
|
|
|
int64_t contentLength = -1;
|
|
|
|
hc->GetContentLength(&contentLength);
|
|
|
|
if (contentLength >= 0 && responseStatus == HTTP_OK_CODE) {
|
|
|
|
// "OK" status means Content-Length is for the whole resource.
|
|
|
|
// Since that's bounded, we know we have a finite-length resource.
|
|
|
|
dataIsBounded = true;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
if (mOffset == 0) {
|
2010-01-27 15:29:26 +03: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-04-01 04:52:56 +04:00
|
|
|
// 1) The Media resource metadata if possible (done by the decoder itself).
|
2010-01-27 15:29:26 +03: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-02 06:35:17 +04:00
|
|
|
nsAutoCString durationText;
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec = NS_OK;
|
2010-01-27 15:29:26 +03:00
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("Content-Duration"), durationText);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("X-AMZ-Meta-Content-Duration"), durationText);
|
|
|
|
}
|
2010-01-27 15:29:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = hc->GetResponseHeader(NS_LITERAL_CSTRING("X-Content-Duration"), durationText);
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2013-08-01 08:02:40 +04:00
|
|
|
// If there is a Content-Duration header with a valid value, record
|
|
|
|
// the duration.
|
2009-04-01 04:52:56 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-03-25 14:14:19 +03:00
|
|
|
double duration = durationText.ToDouble(&ec);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (ec == NS_OK && duration >= 0) {
|
2011-04-14 02:12:23 +04:00
|
|
|
mDecoder->SetDuration(duration);
|
2013-08-01 08:02:40 +04:00
|
|
|
// We know the resource must be bounded.
|
|
|
|
dataIsBounded = true;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 16:55:35 +03:00
|
|
|
|
2013-08-01 08:02:40 +04:00
|
|
|
// Assume Range requests have a bounded upper limit unless the
|
|
|
|
// Content-Range header tells us otherwise.
|
|
|
|
bool boundedSeekLimit = true;
|
2012-09-30 03:29:04 +04:00
|
|
|
// Check response code for byte-range requests (seeking, chunk requests).
|
|
|
|
if (!mByteRange.IsNull() && (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);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Content-Range header text should be parse-able.
|
|
|
|
CMLOG("Error processing \'Content-Range' for "
|
2012-12-07 03:27:08 +04:00
|
|
|
"HTTP_PARTIAL_RESPONSE_CODE: rv[%x] channel[%p] decoder[%p]",
|
|
|
|
rv, hc.get(), mDecoder);
|
2012-09-30 03:29:04 +04:00
|
|
|
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.
|
2013-08-01 08:02:40 +04:00
|
|
|
if (rangeTotal == -1) {
|
|
|
|
boundedSeekLimit = false;
|
2012-09-30 03:29:04 +04:00
|
|
|
} else {
|
2013-08-01 08:02:40 +04:00
|
|
|
mCacheStream.NotifyDataLength(rangeTotal);
|
2012-09-30 03:29:04 +04:00
|
|
|
}
|
|
|
|
mCacheStream.NotifyDataStarted(rangeStart);
|
|
|
|
|
|
|
|
mOffset = rangeStart;
|
2013-01-23 23:24:41 +04:00
|
|
|
// We received 'Content-Range', so the server accepts range requests.
|
2012-09-30 03:29:04 +04:00
|
|
|
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-04-01 04:52:56 +04:00
|
|
|
mCacheStream.NotifyDataStarted(0);
|
2009-05-18 06:02:20 +04:00
|
|
|
mOffset = 0;
|
2011-02-21 06:38:29 +03:00
|
|
|
|
|
|
|
// The server claimed it supported range requests. It lied.
|
2011-09-30 03:34:37 +04:00
|
|
|
acceptsRanges = false;
|
2009-11-05 16:55:35 +03:00
|
|
|
} else if (mOffset == 0 &&
|
2009-04-01 04:52:56 +04:00
|
|
|
(responseStatus == HTTP_OK_CODE ||
|
|
|
|
responseStatus == HTTP_PARTIAL_RESPONSE_CODE)) {
|
2013-08-01 08:02:40 +04:00
|
|
|
if (contentLength >= 0) {
|
|
|
|
mCacheStream.NotifyDataLength(contentLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-18 06:02:20 +04: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-04-01 04:52:56 +04: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;
|
2013-08-01 08:02:40 +04:00
|
|
|
if (seekable && boundedSeekLimit) {
|
|
|
|
// If range requests are supported, and we did not see an unbounded
|
|
|
|
// upper range limit, we assume the resource is bounded.
|
|
|
|
dataIsBounded = true;
|
2011-08-01 22:11:20 +04:00
|
|
|
}
|
2013-08-01 08:02:40 +04:00
|
|
|
|
|
|
|
mDecoder->SetInfinite(!dataIsBounded);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2012-11-30 17:17:54 +04:00
|
|
|
mCacheStream.SetTransportSeekable(seekable);
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-02-05 01:34:23 +04:00
|
|
|
mIsTransportSeekable = seekable;
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->Start();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2011-09-30 03:34:37 +04:00
|
|
|
mReopenOnError = false;
|
2014-10-05 21:29:00 +04:00
|
|
|
mIgnoreClose = false;
|
2012-08-17 21:01:08 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Re-suspend the channel if it needs to be suspended
|
2011-04-01 13:43:29 +04:00
|
|
|
// No need to call PossiblySuspend here since the channel is
|
2012-02-24 05:07:35 +04:00
|
|
|
// definitely in the right state for us in OnStartRequest.
|
2009-04-01 04:52:56 +04:00
|
|
|
mChannel->Suspend();
|
2011-09-30 03:34:37 +04:00
|
|
|
mIgnoreResume = false;
|
2009-04-01 04:52:56 +04: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.
|
2014-11-28 02:17:18 +03:00
|
|
|
owner->DownloadProgressed();
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-05 01:34:23 +04:00
|
|
|
bool
|
|
|
|
ChannelMediaResource::IsTransportSeekable()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return mIsTransportSeekable;
|
|
|
|
}
|
|
|
|
|
2012-09-30 03:29:04 +04: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);
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
CMLOG("Received bytes [%lld] to [%lld] of [%lld] for decoder[%p]",
|
|
|
|
aRangeStart, aRangeEnd, aRangeTotal, mDecoder);
|
2012-09-30 03:29:04 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::OnStopRequest(nsIRequest* aRequest, nsresult aStatus)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
2009-05-18 06:02:20 +04:00
|
|
|
NS_ASSERTION(mSuspendCount == 0,
|
|
|
|
"How can OnStopRequest fire while we're suspended?");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->Stop();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-10-15 06:20:49 +04: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 &&
|
2012-11-30 17:17:54 +04:00
|
|
|
(mOffset == 0 || mCacheStream.IsTransportSeekable())) {
|
2009-10-15 06:20:49 +04:00
|
|
|
// 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-30 03:34:37 +04:00
|
|
|
nsresult rv = CacheClientSeek(mOffset, false);
|
2009-05-18 06:02:20 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
// If the reopen/reseek fails, just fall through and treat this
|
|
|
|
// error as fatal.
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-05-18 06:02:20 +04:00
|
|
|
|
|
|
|
if (!mIgnoreClose) {
|
|
|
|
mCacheStream.NotifyDataEnded(aStatus);
|
2011-02-15 02:21:32 +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.
|
2012-02-24 05:07:35 +04:00
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
2011-02-15 02:21:32 +03:00
|
|
|
|
2012-02-24 05:07:35 +04:00
|
|
|
if (loadFlags & nsIRequest::LOAD_BACKGROUND) {
|
|
|
|
ModifyLoadFlags(loadFlags & ~nsIRequest::LOAD_BACKGROUND);
|
2011-02-15 02:21:32 +03:00
|
|
|
}
|
2009-05-18 06:02:20 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::OnChannelRedirect(nsIChannel* aOld, nsIChannel* aNew,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags)
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
2014-02-26 04:36:54 +04:00
|
|
|
mChannel = aNew;
|
2014-05-11 22:43:00 +04:00
|
|
|
return SetupChannelHeaders();
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CopySegmentClosure {
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource* mResource;
|
2009-05-14 01:52:50 +04:00
|
|
|
};
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_METHOD
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::CopySegmentToCache(nsIInputStream *aInStream,
|
2009-04-01 04:52:56 +04:00
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-05-14 01:52:50 +04:00
|
|
|
CopySegmentClosure* closure = static_cast<CopySegmentClosure*>(aClosure);
|
2010-09-13 12:45:50 +04:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
closure->mResource->mDecoder->NotifyDataArrived(aFromSegment, aCount, closure->mResource->mOffset);
|
2010-09-13 12:45:50 +04:00
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
// Keep track of where we're up to.
|
2013-11-21 07:02:42 +04:00
|
|
|
RESOURCE_LOG("%p [ChannelMediaResource]: CopySegmentToCache at mOffset [%lld] add "
|
|
|
|
"[%d] bytes for decoder[%p]",
|
|
|
|
closure->mResource, closure->mResource->mOffset, aCount,
|
|
|
|
closure->mResource->mDecoder);
|
2012-02-15 08:35:01 +04:00
|
|
|
closure->mResource->mOffset += aCount;
|
2012-12-07 03:27:08 +04:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
closure->mResource->mCacheStream.NotifyDataReceived(aCount, aFromSegment,
|
|
|
|
closure->mPrincipal);
|
2009-04-01 04:52:56 +04:00
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::OnDataAvailable(nsIRequest* aRequest,
|
2009-04-01 04:52:56 +04:00
|
|
|
nsIInputStream* aStream,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->AddBytes(aCount);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
CopySegmentClosure closure;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
2014-02-26 04:36:54 +04:00
|
|
|
if (secMan && mChannel) {
|
2014-08-07 03:05:40 +04:00
|
|
|
secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(closure.mPrincipal));
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
2012-02-15 08:35:01 +04:00
|
|
|
closure.mResource = this;
|
2009-05-14 01:52:50 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = aCount;
|
2009-04-01 04:52:56 +04:00
|
|
|
while (count > 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t read;
|
2009-11-05 16:55:35 +03:00
|
|
|
nsresult rv = aStream->ReadSegments(CopySegmentToCache, &closure, count,
|
2009-04-01 04:52:56 +04: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 11:39:21 +04:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::Open(nsIStreamListener **aStreamListener)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2012-12-13 23:42:45 +04:00
|
|
|
if (!mChannelStatistics) {
|
|
|
|
mChannelStatistics = new MediaChannelStatistics();
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult rv = mCacheStream.Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-05-18 06:02:20 +04:00
|
|
|
NS_ASSERTION(mOffset == 0, "Who set mOffset already?");
|
2009-10-09 15:48:30 +04:00
|
|
|
|
2014-02-26 04:36:54 +04:00
|
|
|
if (!mChannel) {
|
2009-10-09 15:48:30 +04:00
|
|
|
// 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-18 06:02:20 +04:00
|
|
|
return OpenChannel(aStreamListener);
|
2009-03-30 05:19:51 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::OpenChannel(nsIStreamListener** aStreamListener)
|
2009-03-30 05:19:51 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2014-02-26 04:36:54 +04:00
|
|
|
NS_ENSURE_TRUE(mChannel, NS_ERROR_NULL_POINTER);
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(!mListener, "Listener should have been removed by now");
|
2009-03-30 05:19:51 +04:00
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
if (aStreamListener) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aStreamListener = nullptr;
|
2008-10-30 08:20:08 +03:00
|
|
|
}
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2013-05-16 07:22:34 +04:00
|
|
|
if (mByteRange.IsNull()) {
|
|
|
|
// We're not making a byte range request, so set the content length,
|
|
|
|
// if it's available as an HTTP header. This ensures that MediaResource
|
|
|
|
// wrapping objects for platform libraries that expect to know
|
|
|
|
// the length of a resource can get it before OnStartRequest() fires.
|
|
|
|
nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(mChannel);
|
|
|
|
if (hc) {
|
|
|
|
int64_t cl = -1;
|
|
|
|
if (NS_SUCCEEDED(hc->GetContentLength(&cl)) && cl != -1) {
|
|
|
|
mCacheStream.NotifyDataLength(cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
mListener = new Listener(this);
|
|
|
|
NS_ENSURE_TRUE(mListener, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-01 07:19:00 +04:00
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
if (aStreamListener) {
|
|
|
|
*aStreamListener = mListener;
|
2009-03-30 05:19:51 +04:00
|
|
|
NS_ADDREF(*aStreamListener);
|
2008-10-30 08:20:08 +03:00
|
|
|
} else {
|
2014-05-11 22:43:00 +04:00
|
|
|
nsresult rv = mChannel->SetNotificationCallbacks(mListener.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-05-14 01:52:50 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsCOMPtr<nsIStreamListener> listener = mListener.get();
|
|
|
|
|
2009-01-24 14:00:17 +03:00
|
|
|
// Ensure that if we're loading cross domain, that the server is sending
|
|
|
|
// an authorizing Access-Control header.
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-01-24 14:00:17 +03:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
2012-10-02 09:03:24 +04:00
|
|
|
nsRefPtr<nsCORSListenerProxy> crossSiteListener =
|
2011-03-29 00:18:45 +04:00
|
|
|
new nsCORSListenerProxy(mListener,
|
|
|
|
element->NodePrincipal(),
|
2012-09-19 06:16:23 +04:00
|
|
|
false);
|
2009-12-15 23:08:59 +03:00
|
|
|
NS_ENSURE_TRUE(crossSiteListener, NS_ERROR_OUT_OF_MEMORY);
|
2014-05-11 22:43:00 +04:00
|
|
|
rv = crossSiteListener->Init(mChannel);
|
2009-01-24 14:00:17 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-05-11 22:43:00 +04:00
|
|
|
listener = crossSiteListener;
|
2009-01-25 11:07:43 +03:00
|
|
|
} else {
|
2014-05-11 22:43:00 +04:00
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
2009-04-01 04:52:56 +04:00
|
|
|
CheckLoadURIWithPrincipal(element->NodePrincipal(),
|
|
|
|
mURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
2009-01-25 11:07:43 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-04-01 07:19:00 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2014-05-11 22:43:00 +04:00
|
|
|
rv = SetupChannelHeaders();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-05 16:55:35 +03:00
|
|
|
|
2014-05-11 22:43:00 +04:00
|
|
|
rv = mChannel->AsyncOpen(listener, nullptr);
|
2008-10-30 08:20:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-31 06:30:23 +04:00
|
|
|
// Tell the media element that we are fetching data from a channel.
|
|
|
|
element->DownloadResumed(true);
|
2008-10-30 08:20:08 +03:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2014-05-11 22:43:00 +04:00
|
|
|
nsresult ChannelMediaResource::SetupChannelHeaders()
|
2009-05-14 01:52:50 +04: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-30 03:29:04 +04:00
|
|
|
// Use |mByteRange| for a specific chunk, or |mOffset| if seeking in a
|
|
|
|
// complete file download.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString rangeString("bytes=");
|
2012-09-30 03:29:04 +04:00
|
|
|
if (!mByteRange.IsNull()) {
|
|
|
|
rangeString.AppendInt(mByteRange.mStart);
|
|
|
|
mOffset = mByteRange.mStart;
|
|
|
|
} else {
|
|
|
|
rangeString.AppendInt(mOffset);
|
|
|
|
}
|
2014-05-22 07:48:52 +04:00
|
|
|
rangeString.Append('-');
|
2012-09-30 03:29:04 +04:00
|
|
|
if (!mByteRange.IsNull()) {
|
|
|
|
rangeString.AppendInt(mByteRange.mEnd);
|
|
|
|
}
|
2014-05-11 22:43:00 +04:00
|
|
|
nsresult rv = hc->SetRequestHeader(NS_LITERAL_CSTRING("Range"), rangeString, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-29 08:58:07 +04: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-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
2014-05-11 22:43:00 +04:00
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2014-05-11 22:43:00 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
2010-09-16 21:36:23 +04:00
|
|
|
element->SetRequestHeaders(hc);
|
2009-05-14 01:52:50 +04:00
|
|
|
} else {
|
2009-05-18 06:02:20 +04:00
|
|
|
NS_ASSERTION(mOffset == 0, "Don't know how to seek on this channel type");
|
2014-05-11 22:43:00 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
2014-05-11 22:43:00 +04:00
|
|
|
return NS_OK;
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::Close()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2008-11-06 23:53:20 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
mCacheStream.Close();
|
|
|
|
CloseChannel();
|
2008-10-19 11:39:21 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
already_AddRefed<nsIPrincipal> ChannelMediaResource::GetCurrentPrincipal()
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mCacheStream.GetCurrentPrincipal();
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-20 11:55:40 +04:00
|
|
|
bool ChannelMediaResource::CanClone()
|
|
|
|
{
|
|
|
|
return mCacheStream.IsAvailableForSharing();
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
already_AddRefed<MediaResource> ChannelMediaResource::CloneData(MediaDecoder* aDecoder)
|
2009-09-15 06:30:44 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2012-03-20 11:55:40 +04:00
|
|
|
NS_ASSERTION(mCacheStream.IsAvailableForSharing(), "Stream can't be cloned");
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
nsRefPtr<ChannelMediaResource> resource =
|
|
|
|
new ChannelMediaResource(aDecoder,
|
|
|
|
nullptr,
|
|
|
|
mURI,
|
|
|
|
GetContentType());
|
2012-02-15 08:35:01 +04:00
|
|
|
if (resource) {
|
2009-10-09 15:48:30 +04: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
|
2013-04-24 13:19:02 +04:00
|
|
|
// is already in the cache we don't create an unnecessary HTTP channel
|
2009-10-09 15:48:30 +04:00
|
|
|
// and perform a useless HTTP transaction.
|
2012-02-15 08:35:01 +04:00
|
|
|
resource->mSuspendCount = 1;
|
|
|
|
resource->mCacheStream.InitAsClone(&mCacheStream);
|
2013-01-23 23:24:41 +04:00
|
|
|
resource->mChannelStatistics = new MediaChannelStatistics(mChannelStatistics);
|
2012-12-13 23:42:45 +04:00
|
|
|
resource->mChannelStatistics->Stop();
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
2013-05-03 02:59:18 +04:00
|
|
|
return resource.forget();
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
void ChannelMediaResource::CloseChannel()
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-03-30 05:19:51 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->Stop();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->Revoke();
|
2012-07-30 18:20:58 +04:00
|
|
|
mListener = nullptr;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 04:36:54 +04:00
|
|
|
if (mChannel) {
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Resume the channel before we cancel it
|
2011-04-01 13:43:29 +04:00
|
|
|
PossiblyResume();
|
2009-04-01 04:52:56 +04: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
|
2012-11-29 04:26:39 +04:00
|
|
|
// to nsDocumentViewer::LoadComplete if our channel is the one
|
2009-04-01 04:52:56 +04:00
|
|
|
// 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 18:20:58 +04:00
|
|
|
mChannel = nullptr;
|
2008-11-18 11:19:32 +03:00
|
|
|
}
|
2009-01-22 02:54:40 +03:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::ReadFromCache(char* aBuffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t aOffset,
|
|
|
|
uint32_t aCount)
|
2010-08-05 11:40:35 +04:00
|
|
|
{
|
|
|
|
return mCacheStream.ReadFromCache(aBuffer, aOffset, aCount);
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::Read(char* aBuffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aBytes)
|
2009-01-22 02:54:40 +03:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2013-09-18 07:37:23 +04:00
|
|
|
int64_t offset = mCacheStream.Tell();
|
|
|
|
nsresult rv = mCacheStream.Read(aBuffer, aCount, aBytes);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DispatchBytesConsumed(*aBytes, offset);
|
|
|
|
}
|
|
|
|
return rv;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:54:52 +04:00
|
|
|
nsresult ChannelMediaResource::ReadAt(int64_t aOffset,
|
|
|
|
char* aBuffer,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2013-09-18 07:37:23 +04:00
|
|
|
nsresult rv = mCacheStream.ReadAt(aOffset, aBuffer, aCount, aBytes);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DispatchBytesConsumed(*aBytes, aOffset);
|
|
|
|
}
|
|
|
|
return rv;
|
2013-07-17 05:54:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult ChannelMediaResource::Seek(int32_t aWhence, int64_t aOffset)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
CMLOG("Seek requested for aOffset [%lld] for decoder [%p]",
|
|
|
|
aOffset, mDecoder);
|
2009-04-01 04:52:56 +04:00
|
|
|
return mCacheStream.Seek(aWhence, aOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t ChannelMediaResource::Tell()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
|
|
|
return mCacheStream.Tell();
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult ChannelMediaResource::GetCachedRanges(nsTArray<MediaByteRange>& aRanges)
|
2011-03-24 01:28:58 +03:00
|
|
|
{
|
|
|
|
return mCacheStream.GetCachedRanges(aRanges);
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
void ChannelMediaResource::Suspend(bool aCloseImmediately)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-10-09 15:46:23 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-05-18 06:03:37 +04:00
|
|
|
if (!element) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 04:36:54 +04:00
|
|
|
if (mChannel) {
|
2012-11-30 17:17:54 +04:00
|
|
|
if (aCloseImmediately && mCacheStream.IsTransportSeekable()) {
|
2009-05-18 06:02:20 +04:00
|
|
|
// Kill off our channel right now, but don't tell anyone about it.
|
2011-09-30 03:34:37 +04:00
|
|
|
mIgnoreClose = true;
|
2009-05-18 06:02:20 +04:00
|
|
|
CloseChannel();
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadSuspended();
|
2009-05-18 06:02:20 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->Stop();
|
2009-05-18 06:02:20 +04:00
|
|
|
}
|
2011-04-01 13:43:29 +04:00
|
|
|
PossiblySuspend();
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadSuspended();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-18 06:02:20 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
++mSuspendCount;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
void ChannelMediaResource::Resume()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-10-09 15:46:23 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-05-18 06:02:20 +04:00
|
|
|
NS_ASSERTION(mSuspendCount > 0, "Too many resumes!");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-05-18 06:03:37 +04:00
|
|
|
if (!element) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-09 15:48:30 +04:00
|
|
|
NS_ASSERTION(mSuspendCount > 0, "Resume without previous Suspend!");
|
2009-04-01 04:52:56 +04:00
|
|
|
--mSuspendCount;
|
2009-05-18 06:02:20 +04:00
|
|
|
if (mSuspendCount == 0) {
|
2014-02-26 04:36:54 +04:00
|
|
|
if (mChannel) {
|
2009-05-18 06:02:20 +04:00
|
|
|
// 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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
mChannelStatistics->Start();
|
2009-05-18 06:02:20 +04:00
|
|
|
}
|
|
|
|
// if an error occurs after Resume, assume it's because the server
|
|
|
|
// timed out the connection and we should reopen it.
|
2011-09-30 03:34:37 +04:00
|
|
|
mReopenOnError = true;
|
2011-04-01 13:43:29 +04:00
|
|
|
PossiblyResume();
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadResumed();
|
2009-05-18 06:02:20 +04:00
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t totalLength = mCacheStream.GetLength();
|
2009-09-21 07:47:47 +04: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-30 03:34:37 +04:00
|
|
|
CacheClientSeek(mOffset, false);
|
2014-09-24 11:26:00 +04:00
|
|
|
element->DownloadResumed();
|
|
|
|
} else {
|
|
|
|
// The channel remains dead. Do not notify DownloadResumed() which
|
|
|
|
// will leave the media element in NETWORK_LOADING state.
|
2009-09-21 07:47:47 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::RecreateChannel()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-10 05:28:24 +04:00
|
|
|
nsLoadFlags loadFlags =
|
|
|
|
nsICachingChannel::LOAD_BYPASS_LOCAL_CACHE_IF_BUSY |
|
|
|
|
(mLoadInBackground ? nsIRequest::LOAD_BACKGROUND : 0);
|
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// The decoder is being shut down, so don't bother opening a new channel
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-04-10 05:28:24 +04:00
|
|
|
if (!element) {
|
2009-04-01 04:52:56 +04:00
|
|
|
// The decoder is being shut down, so don't bother opening a new channel
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-10 05:28:24 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
|
|
|
NS_ENSURE_TRUE(loadGroup, NS_ERROR_NULL_POINTER);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2014-02-26 04:36:54 +04:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(mChannel),
|
2012-09-05 04:49:46 +04:00
|
|
|
mURI,
|
2014-09-21 20:37:32 +04:00
|
|
|
element,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_MEDIA,
|
2012-09-05 04:49:46 +04:00
|
|
|
loadGroup,
|
2014-09-21 20:37:32 +04:00
|
|
|
nullptr, // aCallbacks
|
2012-09-05 04:49:46 +04:00
|
|
|
loadFlags);
|
2014-09-02 02:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-05 04:49:46 +04:00
|
|
|
|
|
|
|
// 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.
|
2013-02-15 05:10:58 +04:00
|
|
|
NS_ASSERTION(!GetContentType().IsEmpty(),
|
2012-09-05 04:49:46 +04:00
|
|
|
"When recreating a channel, we should know the Content-Type.");
|
2013-02-15 05:10:58 +04:00
|
|
|
mChannel->SetContentType(GetContentType());
|
2012-09-05 04:49:46 +04:00
|
|
|
|
2014-05-11 22:43:00 +04:00
|
|
|
// Tell the cache to reset the download status when the channel is reopened.
|
|
|
|
mCacheStream.NotifyChannelRecreated();
|
|
|
|
|
2012-09-05 04:49:46 +04:00
|
|
|
return rv;
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::DoNotifyDataReceived()
|
2009-09-15 06:30:45 +04:00
|
|
|
{
|
|
|
|
mDataReceivedEvent.Revoke();
|
|
|
|
mDecoder->NotifyBytesDownloaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::CacheClientNotifyDataReceived()
|
2009-09-15 06:30:45 +04:00
|
|
|
{
|
2009-10-09 15:46:23 +04: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-15 06:30:45 +04:00
|
|
|
|
|
|
|
if (mDataReceivedEvent.IsPending())
|
|
|
|
return;
|
|
|
|
|
|
|
|
mDataReceivedEvent =
|
2012-02-15 08:35:01 +04:00
|
|
|
NS_NewNonOwningRunnableMethod(this, &ChannelMediaResource::DoNotifyDataReceived);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(mDataReceivedEvent.get());
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataEnded : public nsRunnable {
|
|
|
|
public:
|
2012-11-14 23:46:40 +04:00
|
|
|
DataEnded(MediaDecoder* aDecoder, nsresult aStatus) :
|
2009-09-15 06:30:45 +04:00
|
|
|
mDecoder(aDecoder), mStatus(aStatus) {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyDownloadEnded(mStatus);
|
2014-09-24 11:25:00 +04:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (owner) {
|
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
|
|
|
if (element) {
|
|
|
|
element->DownloadSuspended();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// NotifySuspendedStatusChanged will tell the element that download
|
|
|
|
// has been suspended "by the cache", which is true since we never download
|
|
|
|
// anything. The element can then transition to HAVE_ENOUGH_DATA.
|
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
|
|
|
}
|
2009-09-15 06:30:45 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2012-11-14 23:46:40 +04:00
|
|
|
nsRefPtr<MediaDecoder> mDecoder;
|
2014-09-24 11:25:00 +04:00
|
|
|
nsresult mStatus;
|
2009-09-15 06:30:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::CacheClientNotifyDataEnded(nsresult aStatus)
|
2009-09-15 06:30:45 +04:00
|
|
|
{
|
2014-05-23 23:53:17 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2009-10-09 15:46:23 +04:00
|
|
|
// 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-15 06:30:45 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new DataEnded(mDecoder, aStatus);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToCurrentThread(event);
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:12:42 +04:00
|
|
|
void
|
|
|
|
ChannelMediaResource::CacheClientNotifyPrincipalChanged()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
|
|
|
|
|
|
|
mDecoder->NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
ChannelMediaResource::CacheClientSeek(int64_t aOffset, bool aResume)
|
2009-09-15 06:30:44 +04:00
|
|
|
{
|
2009-10-09 15:46:23 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Don't call on non-main thread");
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
CMLOG("CacheClientSeek requested for aOffset [%lld] for decoder [%p]",
|
|
|
|
aOffset, mDecoder);
|
|
|
|
|
2013-04-03 04:05:00 +04:00
|
|
|
CloseChannel();
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2009-05-18 06:02:20 +04: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-07-25 09:50:13 +04:00
|
|
|
mOffset = aOffset;
|
|
|
|
|
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Close the existing channel to force the channel to be recreated at
|
|
|
|
// the correct offset upon resume.
|
2014-02-26 04:36:54 +04:00
|
|
|
if (mChannel) {
|
2012-07-25 09:50:13 +04:00
|
|
|
mIgnoreClose = true;
|
|
|
|
CloseChannel();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsresult rv = RecreateChannel();
|
2014-09-02 02:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return OpenChannel(nullptr);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
void
|
|
|
|
ChannelMediaResource::FlushCache()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
|
|
|
// Ensure that data in the cache's partial block is written to disk.
|
|
|
|
mCacheStream.FlushPartialBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaResource::NotifyLastByteRange()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
|
|
|
// Tell media cache that the last data has been downloaded.
|
|
|
|
// Note: subsequent seeks will require re-opening the channel etc.
|
|
|
|
mCacheStream.NotifyDataEnded(NS_OK);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::CacheClientSuspend()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2011-09-30 03:34:37 +04:00
|
|
|
Suspend(false);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-10-09 15:46:23 +04:00
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::CacheClientResume()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
Resume();
|
|
|
|
|
2009-10-09 15:46:23 +04:00
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
|
|
|
ChannelMediaResource::GetNextCachedData(int64_t aOffset)
|
2009-05-18 02:15:57 +04:00
|
|
|
{
|
|
|
|
return mCacheStream.GetNextCachedData(aOffset);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
|
|
|
ChannelMediaResource::GetCachedDataEnd(int64_t aOffset)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
return mCacheStream.GetCachedDataEnd(aOffset);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
ChannelMediaResource::IsDataCachedToEndOfResource(int64_t aOffset)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
return mCacheStream.IsDataCachedToEndOfStream(aOffset);
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:05:49 +04:00
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::EnsureCacheUpToDate()
|
2011-11-30 09:05:49 +04:00
|
|
|
{
|
|
|
|
mCacheStream.EnsureCacheUpdate();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-05-03 02:59:18 +04:00
|
|
|
ChannelMediaResource::IsSuspendedByCache()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2013-05-03 02:59:18 +04:00
|
|
|
return mCacheStream.AreAllStreamsForResourceSuspended();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::IsSuspended()
|
2010-08-13 04:41:47 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2010-08-13 04:41:47 +04:00
|
|
|
return mSuspendCount > 0;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
void
|
2012-11-14 23:46:40 +04:00
|
|
|
ChannelMediaResource::SetReadMode(MediaCacheStream::ReadMode aMode)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
mCacheStream.SetReadMode(aMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
ChannelMediaResource::SetPlaybackRate(uint32_t aBytesPerSecond)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
mCacheStream.SetPlaybackRate(aBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Pin()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
mCacheStream.Pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::Unpin()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
mCacheStream.Unpin();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::GetDownloadRate(bool* aIsReliable)
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-12-13 23:42:45 +04:00
|
|
|
return mChannelStatistics->GetRate(aIsReliable);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::GetLength()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
return mCacheStream.GetLength();
|
2009-01-22 02:54:40 +03:00
|
|
|
}
|
|
|
|
|
2011-04-01 13:43:29 +04:00
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::PossiblySuspend()
|
2011-04-01 13:43:29 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isPending = false;
|
2011-04-01 13:43:29 +04:00
|
|
|
nsresult rv = mChannel->IsPending(&isPending);
|
|
|
|
if (NS_SUCCEEDED(rv) && isPending) {
|
|
|
|
mChannel->Suspend();
|
2011-09-30 03:34:37 +04:00
|
|
|
mIgnoreResume = false;
|
2011-04-01 13:43:29 +04:00
|
|
|
} else {
|
2011-09-30 03:34:37 +04:00
|
|
|
mIgnoreResume = true;
|
2011-04-01 13:43:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 08:35:01 +04:00
|
|
|
ChannelMediaResource::PossiblyResume()
|
2011-04-01 13:43:29 +04:00
|
|
|
{
|
|
|
|
if (!mIgnoreResume) {
|
|
|
|
mChannel->Resume();
|
|
|
|
} else {
|
2011-09-30 03:34:37 +04:00
|
|
|
mIgnoreResume = false;
|
2011-04-01 13:43:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 08:31:53 +04:00
|
|
|
class FileMediaResource : public BaseMediaResource
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-02-15 05:10:58 +04:00
|
|
|
FileMediaResource(MediaDecoder* aDecoder,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsACString& aContentType) :
|
|
|
|
BaseMediaResource(aDecoder, aChannel, aURI, aContentType),
|
2012-09-04 04:05:41 +04:00
|
|
|
mSize(-1),
|
2012-08-29 07:55:57 +04:00
|
|
|
mLock("FileMediaResource.mLock"),
|
|
|
|
mSizeInitialized(false)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
}
|
2012-02-15 08:35:01 +04:00
|
|
|
~FileMediaResource()
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Main thread
|
2008-10-30 08:20:08 +03:00
|
|
|
virtual nsresult Open(nsIStreamListener** aStreamListener);
|
2008-10-19 11:39:21 +04:00
|
|
|
virtual nsresult Close();
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual void Suspend(bool aCloseImmediately) {}
|
2009-04-01 04:52:56 +04:00
|
|
|
virtual void Resume() {}
|
2009-05-14 01:52:50 +04:00
|
|
|
virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal();
|
2012-03-20 11:55:40 +04:00
|
|
|
virtual bool CanClone();
|
2013-05-03 02:59:18 +04:00
|
|
|
virtual already_AddRefed<MediaResource> CloneData(MediaDecoder* aDecoder);
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// These methods are called off the main thread.
|
|
|
|
|
|
|
|
// Other thread
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual void SetReadMode(MediaCacheStream::ReadMode aMode) {}
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual void SetPlaybackRate(uint32_t aBytesPerSecond) {}
|
|
|
|
virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
|
2013-07-17 05:54:52 +04:00
|
|
|
virtual nsresult ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes);
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual nsresult Seek(int32_t aWhence, int64_t aOffset);
|
|
|
|
virtual int64_t Tell();
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Any thread
|
|
|
|
virtual void Pin() {}
|
|
|
|
virtual void Unpin() {}
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual double GetDownloadRate(bool* aIsReliable)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
// The data's all already here
|
2011-09-30 03:34:37 +04:00
|
|
|
*aIsReliable = true;
|
2009-04-01 04:52:56 +04:00
|
|
|
return 100*1024*1024; // arbitray, use 100MB/s
|
|
|
|
}
|
2012-08-29 07:55:57 +04:00
|
|
|
virtual int64_t GetLength() {
|
|
|
|
MutexAutoLock lock(mLock);
|
2013-01-08 13:52:24 +04:00
|
|
|
|
|
|
|
EnsureSizeInitialized();
|
2012-09-04 04:05:41 +04:00
|
|
|
return mSizeInitialized ? mSize : 0;
|
2012-08-29 07:55:57 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual int64_t GetNextCachedData(int64_t aOffset)
|
2009-05-18 02:15:57 +04:00
|
|
|
{
|
2012-09-04 04:05:41 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-01-08 13:52:24 +04:00
|
|
|
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2009-05-18 02:15:57 +04:00
|
|
|
return (aOffset < mSize) ? aOffset : -1;
|
|
|
|
}
|
2012-09-04 04:05:41 +04:00
|
|
|
virtual int64_t GetCachedDataEnd(int64_t aOffset) {
|
|
|
|
MutexAutoLock lock(mLock);
|
2013-01-08 13:52:24 +04:00
|
|
|
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2013-01-15 16:22:03 +04:00
|
|
|
return std::max(aOffset, mSize);
|
2012-09-04 04:05:41 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual bool IsDataCachedToEndOfResource(int64_t aOffset) { return true; }
|
2014-09-24 11:25:00 +04:00
|
|
|
virtual bool IsSuspendedByCache() { return true; }
|
|
|
|
virtual bool IsSuspended() { return true; }
|
2013-02-05 01:34:23 +04:00
|
|
|
virtual bool IsTransportSeekable() MOZ_OVERRIDE { return true; }
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult GetCachedRanges(nsTArray<MediaByteRange>& aRanges);
|
2011-03-24 01:28:58 +03:00
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
virtual size_t SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
// Might be useful to track in the future:
|
|
|
|
// - mInput
|
|
|
|
return BaseMediaResource::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:54:52 +04:00
|
|
|
protected:
|
|
|
|
// These Unsafe variants of Read and Seek perform their operations
|
|
|
|
// without acquiring mLock. The caller must obtain the lock before
|
|
|
|
// calling. The implmentation of Read, Seek and ReadAt obtains the
|
|
|
|
// lock before calling these Unsafe variants to read or seek.
|
|
|
|
nsresult UnsafeRead(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
|
|
|
|
nsresult UnsafeSeek(int32_t aWhence, int64_t aOffset);
|
2008-10-19 11:39:21 +04:00
|
|
|
private:
|
2012-08-29 07:55:57 +04:00
|
|
|
// Ensures mSize is initialized, if it can be.
|
2012-09-04 04:05:41 +04:00
|
|
|
// mLock must be held when this is called, and mInput must be non-null.
|
|
|
|
void EnsureSizeInitialized();
|
2012-08-29 07:55:57 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// The file size, or -1 if not known. Immutable after Open().
|
2012-09-04 04:05:41 +04:00
|
|
|
// Can be used from any thread.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mSize;
|
2009-04-01 04:52:56 +04: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-04 04:05:41 +04: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-04-01 08:29:02 +04:00
|
|
|
Mutex mLock;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2008-10-19 11:39:21 +04: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
|
2013-01-08 13:52:24 +04:00
|
|
|
// thread.
|
2008-10-19 11:39:21 +04:00
|
|
|
nsCOMPtr<nsIInputStream> mInput;
|
2012-08-29 07:55:57 +04: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 11:39:21 +04:00
|
|
|
};
|
|
|
|
|
2012-09-04 04:05:41 +04:00
|
|
|
void FileMediaResource::EnsureSizeInitialized()
|
2012-08-29 07:55:57 +04:00
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2012-09-04 04:05:41 +04:00
|
|
|
NS_ASSERTION(mInput, "Must have file input stream");
|
|
|
|
if (mSizeInitialized) {
|
2012-08-29 07:55:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSizeInitialized = true;
|
|
|
|
// Get the file size and inform the decoder.
|
|
|
|
uint64_t size;
|
|
|
|
nsresult res = mInput->Available(&size);
|
2012-09-28 10:57:33 +04:00
|
|
|
if (NS_SUCCEEDED(res) && size <= INT64_MAX) {
|
2012-08-29 07:55:57 +04:00
|
|
|
mSize = (int64_t)size;
|
2013-11-05 07:19:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new DataEnded(mDecoder, NS_OK);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2012-08-29 07:55:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult FileMediaResource::GetCachedRanges(nsTArray<MediaByteRange>& aRanges)
|
2011-03-24 01:28:58 +03:00
|
|
|
{
|
2012-08-29 07:55:57 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-01-08 13:52:24 +04:00
|
|
|
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2011-03-24 01:28:58 +03:00
|
|
|
if (mSize == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-15 08:35:01 +04:00
|
|
|
aRanges.AppendElement(MediaByteRange(0, mSize));
|
2011-03-24 01:28:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult FileMediaResource::Open(nsIStreamListener** aStreamListener)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
if (aStreamListener) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aStreamListener = nullptr;
|
2008-10-30 08:20:08 +03:00
|
|
|
}
|
|
|
|
|
2011-04-14 16:04:12 +04:00
|
|
|
nsresult rv = NS_OK;
|
2008-10-30 08:20:08 +03: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-05 01:53:52 +04:00
|
|
|
if (fc) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fc->GetFile(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-23 14:42:14 +04:00
|
|
|
|
2012-09-05 01:53:52 +04:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(mInput), file);
|
|
|
|
} else if (IsBlobURI(mURI)) {
|
|
|
|
rv = NS_GetStreamForBlobURI(mURI, getter_AddRefs(mInput));
|
|
|
|
}
|
2008-10-30 08:20:08 +03:00
|
|
|
} else {
|
2009-11-05 16:55:35 +03:00
|
|
|
// Ensure that we never load a local file from some page on a
|
2009-01-25 11:07:54 +03:00
|
|
|
// web server.
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_FAILURE);
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-01-25 11:07:54 +03:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(element->NodePrincipal(),
|
|
|
|
mURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
rv = mChannel->Open(getter_AddRefs(mInput));
|
|
|
|
}
|
2008-10-19 11:39:21 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mSeekable = do_QueryInterface(mInput);
|
2008-10-30 08:20:08 +03: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 11:39:21 +04:00
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
return NS_OK;
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
nsresult FileMediaResource::Close()
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2013-01-08 13:52:24 +04:00
|
|
|
// Since mChennel is only accessed by main thread, there is no necessary to
|
|
|
|
// take the lock.
|
2014-02-26 04:36:54 +04:00
|
|
|
if (mChannel) {
|
2009-04-01 04:52:56 +04:00
|
|
|
mChannel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
2012-07-30 18:20:58 +04:00
|
|
|
mChannel = nullptr;
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
already_AddRefed<nsIPrincipal> FileMediaResource::GetCurrentPrincipal()
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
2014-02-26 04:36:54 +04:00
|
|
|
if (!secMan || !mChannel)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-08-07 03:05:40 +04:00
|
|
|
secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
|
2009-05-14 01:52:50 +04:00
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-20 11:55:40 +04:00
|
|
|
bool FileMediaResource::CanClone()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
already_AddRefed<MediaResource> FileMediaResource::CloneData(MediaDecoder* aDecoder)
|
2009-09-15 06:30:44 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
// The decoder is being shut down, so we can't clone
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-09-15 06:30:44 +04:00
|
|
|
if (!element) {
|
|
|
|
// The decoder is being shut down, so we can't clone
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(loadGroup, nullptr);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv =
|
2014-09-21 20:37:32 +04:00
|
|
|
NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mURI,
|
|
|
|
element,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_MEDIA,
|
|
|
|
loadGroup);
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
nsRefPtr<MediaResource> resource(new FileMediaResource(aDecoder, channel, mURI, GetContentType()));
|
|
|
|
return resource.forget();
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileMediaResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
|
2010-08-05 11:40:35 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-01-08 13:52:24 +04:00
|
|
|
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t offset = 0;
|
2010-08-05 11:40:35 +04: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 19:56:38 +04:00
|
|
|
uint32_t bytesRead = 0;
|
2010-08-05 11:40:35 +04:00
|
|
|
do {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t x = 0;
|
|
|
|
uint32_t bytesToRead = aCount - bytesRead;
|
2010-08-05 11:40:35 +04: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 19:56:38 +04:00
|
|
|
nsresult FileMediaResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2013-09-18 07:37:23 +04:00
|
|
|
nsresult rv;
|
|
|
|
int64_t offset = 0;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mSeekable->Tell(&offset);
|
|
|
|
rv = UnsafeRead(aBuffer, aCount, aBytes);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DispatchBytesConsumed(*aBytes, offset);
|
|
|
|
}
|
|
|
|
return rv;
|
2013-07-17 05:54:52 +04:00
|
|
|
}
|
2013-01-08 13:52:24 +04:00
|
|
|
|
2013-07-17 05:54:52 +04:00
|
|
|
nsresult FileMediaResource::UnsafeRead(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2009-02-05 11:02:21 +03:00
|
|
|
return mInput->Read(aBuffer, aCount, aBytes);
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:54:52 +04:00
|
|
|
nsresult FileMediaResource::ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2013-09-18 07:37:23 +04:00
|
|
|
nsresult rv;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
rv = UnsafeSeek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = UnsafeRead(aBuffer, aCount, aBytes);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DispatchBytesConsumed(*aBytes, aOffset);
|
|
|
|
}
|
|
|
|
return rv;
|
2013-07-17 05:54:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileMediaResource::Seek(int32_t aWhence, int64_t aOffset)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2009-04-01 07:19:00 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-07-17 05:54:52 +04:00
|
|
|
return UnsafeSeek(aWhence, aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult FileMediaResource::UnsafeSeek(int32_t aWhence, int64_t aOffset)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
if (!mSeekable)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2009-04-01 04:52:56 +04:00
|
|
|
return mSeekable->Seek(aWhence, aOffset);
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t FileMediaResource::Tell()
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04: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-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-09-04 04:05:41 +04:00
|
|
|
EnsureSizeInitialized();
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t offset = 0;
|
2014-05-19 13:33:24 +04:00
|
|
|
// Return mSize as offset (end of stream) in case of error
|
|
|
|
if (!mSeekable || NS_FAILED(mSeekable->Tell(&offset)))
|
|
|
|
return mSize;
|
2008-10-19 11:39:21 +04:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
already_AddRefed<MediaResource>
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaResource::Create(MediaDecoder* aDecoder, nsIChannel* aChannel)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-11-05 16:55:35 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
2012-09-05 01:53:52 +04:00
|
|
|
"MediaResource::Open called on non-main thread");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-09-15 06:30:43 +04: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 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2013-02-15 05:10:58 +04:00
|
|
|
nsAutoCString contentType;
|
|
|
|
aChannel->GetContentType(contentType);
|
|
|
|
|
2009-09-15 06:30:43 +04:00
|
|
|
nsCOMPtr<nsIFileChannel> fc = do_QueryInterface(aChannel);
|
2013-05-03 02:59:18 +04:00
|
|
|
nsRefPtr<MediaResource> resource;
|
2012-09-05 01:53:52 +04:00
|
|
|
if (fc || IsBlobURI(uri)) {
|
2013-05-03 02:59:18 +04:00
|
|
|
resource = new FileMediaResource(aDecoder, aChannel, uri, contentType);
|
2013-09-23 13:53:36 +04:00
|
|
|
} else if (IsRtspURI(uri)) {
|
|
|
|
resource = new RtspMediaResource(aDecoder, aChannel, uri, contentType);
|
2013-05-03 02:59:18 +04:00
|
|
|
} else {
|
|
|
|
resource = new ChannelMediaResource(aDecoder, aChannel, uri, contentType);
|
2009-02-05 11:02:21 +03:00
|
|
|
}
|
2013-05-03 02:59:18 +04:00
|
|
|
return resource.forget();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-17 08:31:53 +04:00
|
|
|
void BaseMediaResource::MoveLoadsToBackground() {
|
2009-04-10 05:28:24 +04:00
|
|
|
NS_ASSERTION(!mLoadInBackground, "Why are you calling this more than once?");
|
2011-09-30 03:34:37 +04:00
|
|
|
mLoadInBackground = true;
|
2014-02-26 04:36:54 +04:00
|
|
|
if (!mChannel) {
|
2009-04-10 05:28:24 +04:00
|
|
|
// No channel, resource is probably already loaded.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-09 09:52:53 +04:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetMediaOwner();
|
|
|
|
if (!owner) {
|
|
|
|
NS_WARNING("Null owner in MediaResource::MoveLoadsToBackground()");
|
|
|
|
return;
|
|
|
|
}
|
2013-11-18 08:22:47 +04:00
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
2009-04-10 05:28:24 +04:00
|
|
|
if (!element) {
|
2012-02-15 08:35:01 +04:00
|
|
|
NS_WARNING("Null element in MediaResource::MoveLoadsToBackground()");
|
2009-04-10 05:28:24 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-15 02:21:32 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isPending = false;
|
2011-02-15 02:21:32 +03:00
|
|
|
if (NS_SUCCEEDED(mChannel->IsPending(&isPending)) &&
|
|
|
|
isPending) {
|
|
|
|
nsLoadFlags loadFlags;
|
2012-03-28 17:14:33 +04:00
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadFlags(&loadFlags);
|
2011-02-15 02:21:32 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
|
|
|
|
|
|
|
loadFlags |= nsIRequest::LOAD_BACKGROUND;
|
|
|
|
ModifyLoadFlags(loadFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 08:31:53 +04:00
|
|
|
void BaseMediaResource::ModifyLoadFlags(nsLoadFlags aFlags)
|
2011-02-15 02:21:32 +03:00
|
|
|
{
|
2009-04-10 05:28:24 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2012-03-28 17:14:33 +04:00
|
|
|
DebugOnly<nsresult> rv = mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2009-04-10 05:28:24 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadGroup() failed!");
|
2011-02-15 02:21:32 +03:00
|
|
|
|
2009-04-10 05:28:24 +04:00
|
|
|
nsresult status;
|
|
|
|
mChannel->GetStatus(&status);
|
2011-02-15 02:21:32 +03:00
|
|
|
|
2009-04-10 05:28:24 +04:00
|
|
|
// Note: if (NS_FAILED(status)), the channel won't be in the load group.
|
|
|
|
if (loadGroup &&
|
2011-02-15 02:21:32 +03:00
|
|
|
NS_SUCCEEDED(status)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = loadGroup->RemoveRequest(mChannel, nullptr, status);
|
2009-04-10 05:28:24 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveRequest() failed!");
|
2011-02-15 02:21:32 +03:00
|
|
|
}
|
2009-04-10 05:28:24 +04:00
|
|
|
|
2011-02-15 02:21:32 +03:00
|
|
|
rv = mChannel->SetLoadFlags(aFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "SetLoadFlags() failed!");
|
2009-04-10 05:28:24 +04:00
|
|
|
|
2011-02-15 02:21:32 +03:00
|
|
|
if (loadGroup &&
|
|
|
|
NS_SUCCEEDED(status)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = loadGroup->AddRequest(mChannel, nullptr);
|
2009-04-10 05:28:24 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AddRequest() failed!");
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 23:45:33 +04:00
|
|
|
|
2013-09-18 07:37:23 +04:00
|
|
|
class DispatchBytesConsumedEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
DispatchBytesConsumedEvent(MediaDecoder* aDecoder,
|
|
|
|
int64_t aNumBytes,
|
|
|
|
int64_t aOffset)
|
|
|
|
: mDecoder(aDecoder),
|
|
|
|
mNumBytes(aNumBytes),
|
|
|
|
mOffset(aOffset)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DispatchBytesConsumedEvent);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2013-09-18 07:37:23 +04:00
|
|
|
~DispatchBytesConsumedEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DispatchBytesConsumedEvent);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
public:
|
2013-09-18 07:37:23 +04:00
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyBytesConsumed(mNumBytes, mOffset);
|
|
|
|
// Drop ref to decoder on main thread, just in case this reference
|
|
|
|
// ends up being the last owning reference somehow.
|
|
|
|
mDecoder = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaDecoder> mDecoder;
|
|
|
|
int64_t mNumBytes;
|
|
|
|
int64_t mOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BaseMediaResource::DispatchBytesConsumed(int64_t aNumBytes, int64_t aOffset)
|
|
|
|
{
|
2014-03-03 10:57:10 +04:00
|
|
|
if (aNumBytes <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-18 07:37:23 +04:00
|
|
|
RefPtr<nsIRunnable> event(new DispatchBytesConsumedEvent(mDecoder, aNumBytes, aOffset));
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-09-18 07:37:23 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|