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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsMediaStream.h"
|
|
|
|
#include "nsMediaDecoder.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileChannel.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2009-01-24 14:00:17 +03:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
|
|
|
#include "nsHTMLMediaElement.h"
|
|
|
|
#include "nsIDocument.h"
|
2009-04-01 04:52:56 +04:00
|
|
|
#include "nsDOMError.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsURILoader.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
#define HTTP_OK_CODE 200
|
|
|
|
#define HTTP_PARTIAL_RESPONSE_CODE 206
|
|
|
|
|
2009-04-11 13:39:24 +04:00
|
|
|
using mozilla::TimeStamp;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsMediaChannelStream::nsMediaChannelStream(nsMediaDecoder* aDecoder,
|
|
|
|
nsIChannel* aChannel, nsIURI* aURI)
|
|
|
|
: nsMediaStream(aDecoder, aChannel, aURI),
|
2009-11-05 16:55:35 +03:00
|
|
|
mOffset(0), mSuspendCount(0),
|
2009-05-18 06:02:20 +04:00
|
|
|
mReopenOnError(PR_FALSE), mIgnoreClose(PR_FALSE),
|
2009-04-01 04:52:56 +04:00
|
|
|
mCacheStream(this),
|
|
|
|
mLock(nsAutoLock::NewLock("media.channel.stream")),
|
2009-11-05 16:55:35 +03:00
|
|
|
mCacheSuspendCount(0)
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsMediaChannelStream::~nsMediaChannelStream()
|
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();
|
|
|
|
}
|
|
|
|
if (mLock) {
|
|
|
|
nsAutoLock::DestroyLock(mLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsMediaChannelStream::Listener just observes the channel and
|
|
|
|
// forwards notifications to the nsMediaChannelStream. We use multiple
|
|
|
|
// listener objects so that when we open a new stream for a seek we can
|
|
|
|
// disconnect the old listener from the nsMediaChannelStream and hook up
|
|
|
|
// a new listener, so notifications from the old channel are discarded
|
|
|
|
// and don't confuse us.
|
2009-05-14 01:52:50 +04:00
|
|
|
NS_IMPL_ISUPPORTS4(nsMediaChannelStream::Listener,
|
|
|
|
nsIRequestObserver, nsIStreamListener, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor)
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::Listener::OnStartRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_OK;
|
|
|
|
return mStream->OnStartRequest(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::Listener::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_OK;
|
|
|
|
return mStream->OnStopRequest(aRequest, aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-05 16:55:35 +03:00
|
|
|
nsMediaChannelStream::Listener::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
2009-04-01 04:52:56 +04:00
|
|
|
nsIInputStream* aStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_OK;
|
|
|
|
return mStream->OnDataAvailable(aRequest, aStream, aCount);
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
nsresult
|
2010-08-05 06:15:55 +04:00
|
|
|
nsMediaChannelStream::Listener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
|
|
|
nsIChannel* aNewChannel,
|
|
|
|
PRUint32 aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* cb)
|
2009-05-14 01:52:50 +04:00
|
|
|
{
|
2010-08-05 06:15:55 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mStream)
|
|
|
|
rv = mStream->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::Listener::GetInterface(const nsIID & aIID, void **aResult)
|
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::OnStartRequest(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
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);
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
|
|
|
// If the request was cancelled by nsCrossSiteListenerProxy due to failing
|
|
|
|
// the Access Control 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);
|
|
|
|
PRBool seekable = PR_FALSE;
|
|
|
|
if (hc) {
|
2009-11-05 16:55:35 +03:00
|
|
|
PRUint32 responseStatus = 0;
|
2009-09-30 01:32:30 +04:00
|
|
|
hc->GetResponseStatus(&responseStatus);
|
|
|
|
PRBool succeeded = PR_FALSE;
|
|
|
|
hc->GetRequestSucceeded(&succeeded);
|
|
|
|
|
|
|
|
if (!succeeded && NS_SUCCEEDED(status)) {
|
|
|
|
// HTTP-level error (e.g. 4xx); treat this as a fatal network-level error.
|
|
|
|
// We might get this on a seek.
|
|
|
|
// (Note that lower-level errors indicated by NS_FAILED(status) are
|
|
|
|
// handled in OnStopRequest.)
|
|
|
|
// A 416 error should treated as EOF here... it's possible
|
|
|
|
// that we don't get Content-Length, we read N bytes, then we
|
|
|
|
// suspend and resume, the resume reopens the channel and we seek to
|
|
|
|
// offset N, but there are no more bytes, so we get a 416
|
|
|
|
// "Requested Range Not Satisfiable".
|
|
|
|
if (responseStatus != HTTP_REQUESTED_RANGE_NOT_SATISFIABLE_CODE) {
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This disconnects our listener so we don't get any more data. We
|
|
|
|
// certainly don't want an error page to end up in our cache!
|
|
|
|
CloseChannel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsCAutoString ranges;
|
|
|
|
hc->GetResponseHeader(NS_LITERAL_CSTRING("Accept-Ranges"),
|
|
|
|
ranges);
|
2009-11-05 16:55:35 +03:00
|
|
|
PRBool acceptsRanges = ranges.EqualsLiteral("bytes");
|
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.
|
2009-04-01 04:52:56 +04:00
|
|
|
nsCAutoString durationText;
|
|
|
|
PRInt32 ec = 0;
|
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
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-01-17 06:03:00 +03:00
|
|
|
double duration = durationText.ToFloat(&ec);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (ec == NS_OK && duration >= 0) {
|
|
|
|
mDecoder->SetDuration(PRInt64(NS_round(duration*1000)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 16:55:35 +03:00
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
if (mOffset > 0 && responseStatus == HTTP_OK_CODE) {
|
2009-04-01 04:52:56 +04:00
|
|
|
// If we get an OK response but we were seeking, 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.
|
|
|
|
mCacheStream.NotifyDataStarted(0);
|
2009-05-18 06:02:20 +04:00
|
|
|
mOffset = 0;
|
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)) {
|
|
|
|
// We weren't seeking and got a valid response status,
|
|
|
|
// set the length of the content.
|
2010-08-31 00:20:39 +04:00
|
|
|
PRInt32 cl = -1;
|
2009-04-01 04:52:56 +04:00
|
|
|
hc->GetContentLength(&cl);
|
|
|
|
if (cl >= 0) {
|
|
|
|
mCacheStream.NotifyDataLength(cl);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
mDecoder->SetSeekable(seekable);
|
|
|
|
mCacheStream.SetSeekable(seekable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICachingChannel> cc = do_QueryInterface(aRequest);
|
|
|
|
if (cc) {
|
|
|
|
PRBool fromCache = PR_FALSE;
|
2009-09-30 01:32:30 +04:00
|
|
|
rv = cc->IsFromCache(&fromCache);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !fromCache) {
|
|
|
|
cc->SetCacheAsFile(PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2009-04-11 13:39:24 +04:00
|
|
|
mChannelStatistics.Start(TimeStamp::Now());
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
mReopenOnError = PR_FALSE;
|
|
|
|
mIgnoreClose = PR_FALSE;
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Re-suspend the channel if it needs to be suspended
|
|
|
|
mChannel->Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fires an initial progress event and sets up the stall counter so stall events
|
|
|
|
// fire if no download occurs within the required time frame.
|
|
|
|
mDecoder->Progress(PR_FALSE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::OnStopRequest(nsIRequest* aRequest, nsresult aStatus)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
nsAutoLock lock(mLock);
|
2009-04-11 13:39:24 +04:00
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
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 &&
|
|
|
|
(mOffset == 0 || mCacheStream.IsSeekable())) {
|
|
|
|
// If the stream did close normally, then if the server is seekable we'll
|
|
|
|
// just seek to the end of the resource and get an HTTP 416 error because
|
|
|
|
// there's nothing there, so this isn't bad.
|
2009-05-18 06:02:20 +04:00
|
|
|
nsresult rv = CacheClientSeek(mOffset, PR_FALSE);
|
|
|
|
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.
|
|
|
|
if (mLoadInBackground) {
|
|
|
|
mLoadInBackground = PR_FALSE;
|
|
|
|
|
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
nsresult rv = mChannel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
|
|
|
|
|
|
|
loadFlags &= ~nsIRequest::LOAD_BACKGROUND;
|
|
|
|
ModifyLoadFlags(loadFlags);
|
|
|
|
}
|
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
|
|
|
|
nsMediaChannelStream::OnChannelRedirect(nsIChannel* aOld, nsIChannel* aNew,
|
|
|
|
PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
mChannel = aNew;
|
|
|
|
SetupChannelHeaders();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CopySegmentClosure {
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsMediaChannelStream* mStream;
|
|
|
|
};
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_METHOD
|
|
|
|
nsMediaChannelStream::CopySegmentToCache(nsIInputStream *aInStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
|
|
|
PRUint32 aToOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aWriteCount)
|
|
|
|
{
|
2009-05-14 01:52:50 +04:00
|
|
|
CopySegmentClosure* closure = static_cast<CopySegmentClosure*>(aClosure);
|
2010-09-13 12:45:50 +04:00
|
|
|
|
|
|
|
closure->mStream->mDecoder->NotifyDataArrived(aFromSegment, aCount, closure->mStream->mOffset);
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
// Keep track of where we're up to
|
|
|
|
closure->mStream->mOffset += aCount;
|
2009-05-14 01:52:50 +04:00
|
|
|
closure->mStream->mCacheStream.NotifyDataReceived(aCount, aFromSegment,
|
|
|
|
closure->mPrincipal);
|
2009-04-01 04:52:56 +04:00
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
|
|
|
|
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
mChannelStatistics.AddBytes(aCount);
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
CopySegmentClosure closure;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
if (secMan && mChannel) {
|
|
|
|
secMan->GetChannelPrincipal(mChannel, getter_AddRefs(closure.mPrincipal));
|
|
|
|
}
|
|
|
|
closure.mStream = this;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
PRUint32 count = aCount;
|
|
|
|
while (count > 0) {
|
|
|
|
PRUint32 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
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
nsresult nsMediaChannelStream::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");
|
|
|
|
|
|
|
|
if (!mLock)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
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
|
|
|
|
|
|
|
if (!mChannel) {
|
|
|
|
// When we're a clone, the decoder might ask us to Open even though
|
|
|
|
// we haven't established an mChannel (because we might not need one)
|
|
|
|
NS_ASSERTION(!aStreamListener,
|
|
|
|
"Should have already been given a channel if we're to return a stream listener");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
return OpenChannel(aStreamListener);
|
2009-03-30 05:19:51 +04:00
|
|
|
}
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
nsresult nsMediaChannelStream::OpenChannel(nsIStreamListener** aStreamListener)
|
2009-03-30 05:19:51 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
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) {
|
|
|
|
*aStreamListener = nsnull;
|
|
|
|
}
|
2008-10-19 11:39:21 +04:00
|
|
|
|
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 {
|
2009-05-14 01:52:50 +04:00
|
|
|
mChannel->SetNotificationCallbacks(mListener.get());
|
|
|
|
|
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.
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
if (element->ShouldCheckAllowOrigin()) {
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult rv;
|
2009-12-15 23:08:59 +03:00
|
|
|
nsCrossSiteListenerProxy* crossSiteListener =
|
|
|
|
new nsCrossSiteListenerProxy(mListener,
|
|
|
|
element->NodePrincipal(),
|
|
|
|
mChannel,
|
|
|
|
PR_FALSE,
|
|
|
|
&rv);
|
|
|
|
listener = crossSiteListener;
|
|
|
|
NS_ENSURE_TRUE(crossSiteListener, NS_ERROR_OUT_OF_MEMORY);
|
2009-01-24 14:00:17 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-01-25 11:07:43 +03:00
|
|
|
} else {
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
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
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
SetupChannelHeaders();
|
2009-11-05 16:55:35 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult rv = mChannel->AsyncOpen(listener, nsnull);
|
2008-10-30 08:20:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
void nsMediaChannelStream::SetupChannelHeaders()
|
|
|
|
{
|
|
|
|
// 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) {
|
|
|
|
nsCAutoString rangeString("bytes=");
|
2009-05-18 06:02:20 +04:00
|
|
|
rangeString.AppendInt(mOffset);
|
2009-05-14 01:52:50 +04:00
|
|
|
rangeString.Append("-");
|
|
|
|
hc->SetRequestHeader(NS_LITERAL_CSTRING("Range"), rangeString, PR_FALSE);
|
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");
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
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");
|
2009-05-14 01:52:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult nsMediaChannelStream::Close()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
already_AddRefed<nsIPrincipal> nsMediaChannelStream::GetCurrentPrincipal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mCacheStream.GetCurrentPrincipal();
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsMediaStream* nsMediaChannelStream::CloneData(nsMediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsMediaChannelStream* stream = new nsMediaChannelStream(aDecoder, nsnull, mURI);
|
|
|
|
if (stream) {
|
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
|
|
|
|
// is already in the cache we don't create an unneccesary HTTP channel
|
|
|
|
// and perform a useless HTTP transaction.
|
|
|
|
stream->mSuspendCount = 1;
|
|
|
|
stream->mCacheSuspendCount = 1;
|
2009-09-15 06:30:44 +04:00
|
|
|
stream->mCacheStream.InitAsClone(&mCacheStream);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
void nsMediaChannelStream::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
|
|
|
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2009-04-11 13:39:24 +04:00
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->Revoke();
|
|
|
|
mListener = nsnull;
|
|
|
|
}
|
|
|
|
|
2008-10-19 11:39:21 +04:00
|
|
|
if (mChannel) {
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mSuspendCount > 0) {
|
|
|
|
// Resume the channel before we cancel it
|
|
|
|
mChannel->Resume();
|
|
|
|
}
|
|
|
|
// The status we use here won't be passed to the decoder, since
|
|
|
|
// we've already revoked the listener. It can however be passed
|
|
|
|
// to DocumentViewerImpl::LoadComplete if our channel is the one
|
|
|
|
// that kicked off creation of a video document. We don't want that
|
|
|
|
// document load to think there was an error.
|
|
|
|
// NS_ERROR_PARSED_DATA_CACHED is the best thing we have for that
|
|
|
|
// at the moment.
|
|
|
|
mChannel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
2008-10-19 11:39:21 +04:00
|
|
|
mChannel = nsnull;
|
2008-11-18 11:19:32 +03:00
|
|
|
}
|
2009-01-22 02:54:40 +03:00
|
|
|
}
|
|
|
|
|
2010-08-05 11:40:35 +04:00
|
|
|
nsresult nsMediaChannelStream::ReadFromCache(char* aBuffer,
|
|
|
|
PRInt64 aOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
return mCacheStream.ReadFromCache(aBuffer, aOffset, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsMediaChannelStream::Read(char* aBuffer,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* 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
|
|
|
|
2011-01-18 03:53:18 +03:00
|
|
|
PRInt64 pos = Tell();
|
|
|
|
PRInt64 endOfRead = pos + aCount;
|
|
|
|
if (endOfRead > mCacheStream.GetCachedDataEnd(pos) &&
|
|
|
|
!IsDataCachedToEndOfStream(pos)) {
|
|
|
|
// Our read will almost certainly block waiting for more data to download.
|
|
|
|
// Notify the decoder, so it can move to buffering state if need be.
|
|
|
|
mDecoder->NotifyDataExhausted();
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
return mCacheStream.Read(aBuffer, aCount, aBytes);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-11-05 16:55:35 +03:00
|
|
|
nsresult nsMediaChannelStream::Seek(PRInt32 aWhence, PRInt64 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");
|
|
|
|
|
|
|
|
return mCacheStream.Seek(aWhence, aOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 07:19:00 +04:00
|
|
|
PRInt64 nsMediaChannelStream::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();
|
|
|
|
}
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
void nsMediaChannelStream::Suspend(PRBool 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
|
|
|
|
2009-05-18 06:03:37 +04:00
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
// Shutting down; do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
if (mChannel) {
|
|
|
|
if (aCloseImmediately && mCacheStream.IsSeekable()) {
|
|
|
|
// Kill off our channel right now, but don't tell anyone about it.
|
|
|
|
mIgnoreClose = PR_TRUE;
|
|
|
|
CloseChannel();
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadSuspended();
|
2009-05-18 06:02:20 +04:00
|
|
|
} else if (mSuspendCount == 0) {
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
mChannelStatistics.Stop(TimeStamp::Now());
|
|
|
|
}
|
|
|
|
mChannel->Suspend();
|
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
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
void nsMediaChannelStream::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
|
|
|
|
2009-05-18 06:03:37 +04:00
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
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) {
|
|
|
|
if (mChannel) {
|
|
|
|
// Just wake up our existing channel
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
mChannelStatistics.Start(TimeStamp::Now());
|
|
|
|
}
|
|
|
|
// if an error occurs after Resume, assume it's because the server
|
|
|
|
// timed out the connection and we should reopen it.
|
|
|
|
mReopenOnError = PR_TRUE;
|
|
|
|
mChannel->Resume();
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadResumed();
|
2009-05-18 06:02:20 +04:00
|
|
|
} else {
|
2009-09-21 07:47:47 +04:00
|
|
|
PRInt64 totalLength = mCacheStream.GetLength();
|
|
|
|
// 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.
|
|
|
|
CacheClientSeek(mOffset, PR_FALSE);
|
|
|
|
}
|
2009-05-18 06:03:37 +04:00
|
|
|
element->DownloadResumed();
|
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
|
2009-09-15 06:30:44 +04:00
|
|
|
nsMediaChannelStream::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);
|
|
|
|
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
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
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
return NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
|
|
|
nsnull,
|
|
|
|
loadGroup,
|
|
|
|
nsnull,
|
|
|
|
loadFlags);
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
void
|
|
|
|
nsMediaChannelStream::DoNotifyDataReceived()
|
|
|
|
{
|
|
|
|
mDataReceivedEvent.Revoke();
|
|
|
|
mDecoder->NotifyBytesDownloaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMediaChannelStream::CacheClientNotifyDataReceived()
|
|
|
|
{
|
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 =
|
2010-04-21 03:21:35 +04:00
|
|
|
NS_NewNonOwningRunnableMethod(this, &nsMediaChannelStream::DoNotifyDataReceived);
|
2009-09-15 06:30:45 +04:00
|
|
|
NS_DispatchToMainThread(mDataReceivedEvent.get(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataEnded : public nsRunnable {
|
|
|
|
public:
|
|
|
|
DataEnded(nsMediaDecoder* aDecoder, nsresult aStatus) :
|
|
|
|
mDecoder(aDecoder), mStatus(aStatus) {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyDownloadEnded(mStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsMediaDecoder> mDecoder;
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMediaChannelStream::CacheClientNotifyDataEnded(nsresult aStatus)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new DataEnded(mDecoder, aStatus);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::CacheClientSeek(PRInt64 aOffset, PRBool aResume)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
CloseChannel();
|
|
|
|
|
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;
|
2009-10-09 15:48:30 +04:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
NS_ASSERTION(mCacheSuspendCount > 0, "CacheClientSeek(aResume=true) without previous CacheClientSuspend!");
|
|
|
|
--mCacheSuspendCount;
|
|
|
|
}
|
2009-05-18 06:02:20 +04:00
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsresult rv = RecreateChannel();
|
2009-04-01 04:52:56 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2009-05-18 06:02:20 +04:00
|
|
|
mOffset = aOffset;
|
|
|
|
return OpenChannel(nsnull);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsresult
|
|
|
|
nsMediaChannelStream::CacheClientSuspend()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
++mCacheSuspendCount;
|
|
|
|
}
|
2009-05-18 06:02:20 +04:00
|
|
|
Suspend(PR_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
|
|
|
|
nsMediaChannelStream::CacheClientResume()
|
2009-04-01 04:52:56 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
Resume();
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2009-10-09 15:48:30 +04:00
|
|
|
NS_ASSERTION(mCacheSuspendCount > 0, "CacheClientResume without previous CacheClientSuspend!");
|
2009-04-01 04:52:56 +04:00
|
|
|
--mCacheSuspendCount;
|
|
|
|
}
|
|
|
|
|
2009-10-09 15:46:23 +04:00
|
|
|
mDecoder->NotifySuspendedStatusChanged();
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-18 02:15:57 +04:00
|
|
|
PRInt64
|
|
|
|
nsMediaChannelStream::GetNextCachedData(PRInt64 aOffset)
|
|
|
|
{
|
|
|
|
return mCacheStream.GetNextCachedData(aOffset);
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
PRInt64
|
|
|
|
nsMediaChannelStream::GetCachedDataEnd(PRInt64 aOffset)
|
|
|
|
{
|
|
|
|
return mCacheStream.GetCachedDataEnd(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsMediaChannelStream::IsDataCachedToEndOfStream(PRInt64 aOffset)
|
|
|
|
{
|
|
|
|
return mCacheStream.IsDataCachedToEndOfStream(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsMediaChannelStream::IsSuspendedByCache()
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
return mCacheSuspendCount > 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:41:47 +04:00
|
|
|
PRBool
|
|
|
|
nsMediaChannelStream::IsSuspended()
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
return mSuspendCount > 0;
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
void
|
|
|
|
nsMediaChannelStream::SetReadMode(nsMediaCacheStream::ReadMode aMode)
|
|
|
|
{
|
|
|
|
mCacheStream.SetReadMode(aMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMediaChannelStream::SetPlaybackRate(PRUint32 aBytesPerSecond)
|
|
|
|
{
|
|
|
|
mCacheStream.SetPlaybackRate(aBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMediaChannelStream::Pin()
|
|
|
|
{
|
|
|
|
mCacheStream.Pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMediaChannelStream::Unpin()
|
|
|
|
{
|
|
|
|
mCacheStream.Unpin();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
nsMediaChannelStream::GetDownloadRate(PRPackedBool* aIsReliable)
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2009-04-11 13:39:24 +04:00
|
|
|
return mChannelStatistics.GetRate(TimeStamp::Now(), aIsReliable);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64
|
|
|
|
nsMediaChannelStream::GetLength()
|
|
|
|
{
|
|
|
|
return mCacheStream.GetLength();
|
2009-01-22 02:54:40 +03:00
|
|
|
}
|
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
class nsMediaFileStream : public nsMediaStream
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
|
|
|
public:
|
2009-03-30 05:20:32 +04:00
|
|
|
nsMediaFileStream(nsMediaDecoder* aDecoder, nsIChannel* aChannel, nsIURI* aURI) :
|
2009-04-01 04:52:56 +04:00
|
|
|
nsMediaStream(aDecoder, aChannel, aURI), mSize(-1),
|
|
|
|
mLock(nsAutoLock::NewLock("media.file.stream"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~nsMediaFileStream()
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mLock) {
|
|
|
|
nsAutoLock::DestroyLock(mLock);
|
|
|
|
}
|
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();
|
2009-05-18 06:02:20 +04:00
|
|
|
virtual void Suspend(PRBool 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();
|
2009-09-15 06:30:44 +04:00
|
|
|
virtual nsMediaStream* CloneData(nsMediaDecoder* aDecoder);
|
2010-08-05 11:40:35 +04:00
|
|
|
virtual nsresult ReadFromCache(char* aBuffer, PRInt64 aOffset, PRUint32 aCount);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// These methods are called off the main thread.
|
|
|
|
|
|
|
|
// Other thread
|
|
|
|
virtual void SetReadMode(nsMediaCacheStream::ReadMode aMode) {}
|
|
|
|
virtual void SetPlaybackRate(PRUint32 aBytesPerSecond) {}
|
2008-10-19 11:39:21 +04:00
|
|
|
virtual nsresult Read(char* aBuffer, PRUint32 aCount, PRUint32* aBytes);
|
|
|
|
virtual nsresult Seek(PRInt32 aWhence, PRInt64 aOffset);
|
|
|
|
virtual PRInt64 Tell();
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Any thread
|
|
|
|
virtual void Pin() {}
|
|
|
|
virtual void Unpin() {}
|
|
|
|
virtual double GetDownloadRate(PRPackedBool* aIsReliable)
|
|
|
|
{
|
|
|
|
// The data's all already here
|
|
|
|
*aIsReliable = PR_TRUE;
|
|
|
|
return 100*1024*1024; // arbitray, use 100MB/s
|
|
|
|
}
|
|
|
|
virtual PRInt64 GetLength() { return mSize; }
|
2009-05-18 02:15:57 +04:00
|
|
|
virtual PRInt64 GetNextCachedData(PRInt64 aOffset)
|
|
|
|
{
|
|
|
|
return (aOffset < mSize) ? aOffset : -1;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
virtual PRInt64 GetCachedDataEnd(PRInt64 aOffset) { return PR_MAX(aOffset, mSize); }
|
|
|
|
virtual PRBool IsDataCachedToEndOfStream(PRInt64 aOffset) { return PR_TRUE; }
|
|
|
|
virtual PRBool IsSuspendedByCache() { return PR_FALSE; }
|
2010-08-13 04:41:47 +04:00
|
|
|
virtual PRBool IsSuspended() { return PR_FALSE; }
|
2008-10-19 11:39:21 +04:00
|
|
|
|
|
|
|
private:
|
2009-04-01 04:52:56 +04:00
|
|
|
// The file size, or -1 if not known. Immutable after Open().
|
|
|
|
PRInt64 mSize;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// This lock protects mSeekable and mInput.
|
|
|
|
PRLock* mLock;
|
|
|
|
|
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
|
|
|
|
// thread.
|
|
|
|
nsCOMPtr<nsIInputStream> mInput;
|
|
|
|
};
|
|
|
|
|
2009-11-05 16:55:35 +03:00
|
|
|
class LoadedEvent : public nsRunnable
|
2009-02-05 11:02:21 +03:00
|
|
|
{
|
|
|
|
public:
|
2009-04-01 04:52:56 +04:00
|
|
|
LoadedEvent(nsMediaDecoder* aDecoder) :
|
|
|
|
mDecoder(aDecoder)
|
2009-02-05 11:02:21 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(LoadedEvent);
|
|
|
|
}
|
|
|
|
~LoadedEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(LoadedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mDecoder->NotifyDownloadEnded(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsMediaDecoder> mDecoder;
|
|
|
|
};
|
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
nsresult nsMediaFileStream::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) {
|
|
|
|
*aStreamListener = nsnull;
|
|
|
|
}
|
|
|
|
|
2008-10-19 11:39:21 +04:00
|
|
|
nsresult rv;
|
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));
|
|
|
|
if (!fc)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2009-11-05 16:55:35 +03:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2008-10-30 08:20:08 +03:00
|
|
|
rv = fc->GetFile(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2008-10-30 08:20:08 +03:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(mInput), file);
|
|
|
|
} 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.
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
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
|
|
|
|
2009-02-05 11:02:21 +03:00
|
|
|
// Get the file size and inform the decoder. Only files up to 4GB are
|
|
|
|
// supported here.
|
|
|
|
PRUint32 size;
|
|
|
|
rv = mInput->Available(&size);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-04-01 04:52:56 +04:00
|
|
|
mSize = size;
|
2009-02-05 11:02:21 +03:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new LoadedEvent(mDecoder);
|
2008-10-19 11:39:21 +04:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2008-10-30 08:20:08 +03:00
|
|
|
return NS_OK;
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
nsresult nsMediaFileStream::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");
|
|
|
|
|
2008-10-19 11:39:21 +04:00
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
if (mChannel) {
|
2009-04-01 04:52:56 +04:00
|
|
|
mChannel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
2008-10-19 11:39:21 +04:00
|
|
|
mChannel = nsnull;
|
|
|
|
mInput = nsnull;
|
|
|
|
mSeekable = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:52:50 +04:00
|
|
|
already_AddRefed<nsIPrincipal> nsMediaFileStream::GetCurrentPrincipal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!secMan || !mChannel)
|
|
|
|
return nsnull;
|
|
|
|
secMan->GetChannelPrincipal(mChannel, getter_AddRefs(principal));
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
nsMediaStream* nsMediaFileStream::CloneData(nsMediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
nsHTMLMediaElement* element = aDecoder->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
// The decoder is being shut down, so we can't clone
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = element->GetDocumentLoadGroup();
|
|
|
|
NS_ENSURE_TRUE(loadGroup, nsnull);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv =
|
|
|
|
NS_NewChannel(getter_AddRefs(channel), mURI, nsnull, loadGroup, nsnull, 0);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
return new nsMediaFileStream(aDecoder, channel, mURI);
|
|
|
|
}
|
|
|
|
|
2010-08-05 11:40:35 +04:00
|
|
|
nsresult nsMediaFileStream::ReadFromCache(char* aBuffer, PRInt64 aOffset, PRUint32 aCount)
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
if (!mInput || !mSeekable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
PRInt64 offset = 0;
|
|
|
|
nsresult res = mSeekable->Tell(&offset);
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
res = mSeekable->Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
do {
|
|
|
|
PRUint32 x = 0;
|
|
|
|
PRUint32 bytesToRead = aCount - bytesRead;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
nsresult nsMediaFileStream::Read(char* aBuffer, PRUint32 aCount, PRUint32* aBytes)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2009-02-05 11:02:21 +03:00
|
|
|
if (!mInput)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return mInput->Read(aBuffer, aCount, aBytes);
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2009-11-05 16:55:35 +03:00
|
|
|
nsresult nsMediaFileStream::Seek(PRInt32 aWhence, PRInt64 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
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
if (!mSeekable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return mSeekable->Seek(aWhence, aOffset);
|
2008-10-19 11:39:21 +04:00
|
|
|
}
|
|
|
|
|
2009-03-30 05:20:32 +04:00
|
|
|
PRInt64 nsMediaFileStream::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");
|
|
|
|
|
2008-10-19 11:39:21 +04:00
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
if (!mSeekable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PRInt64 offset = 0;
|
|
|
|
mSeekable->Tell(&offset);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:43 +04:00
|
|
|
nsMediaStream*
|
|
|
|
nsMediaStream::Create(nsMediaDecoder* aDecoder, nsIChannel* aChannel)
|
2008-10-19 11:39:21 +04:00
|
|
|
{
|
2009-11-05 16:55:35 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
2009-04-01 04:52:56 +04:00
|
|
|
"nsMediaStream::Open called on non-main thread");
|
|
|
|
|
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));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
2008-10-19 11:39:21 +04:00
|
|
|
|
2009-09-15 06:30:43 +04:00
|
|
|
nsCOMPtr<nsIFileChannel> fc = do_QueryInterface(aChannel);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (fc) {
|
2009-09-15 06:30:43 +04:00
|
|
|
return new nsMediaFileStream(aDecoder, aChannel, uri);
|
2009-02-05 11:02:21 +03:00
|
|
|
}
|
2009-09-15 06:30:43 +04:00
|
|
|
return new nsMediaChannelStream(aDecoder, aChannel, uri);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-10 05:28:24 +04:00
|
|
|
void nsMediaStream::MoveLoadsToBackground() {
|
|
|
|
NS_ASSERTION(!mLoadInBackground, "Why are you calling this more than once?");
|
|
|
|
mLoadInBackground = PR_TRUE;
|
|
|
|
if (!mChannel) {
|
|
|
|
// No channel, resource is probably already loaded.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsHTMLMediaElement* element = mDecoder->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
NS_WARNING("Null element in nsMediaStream::MoveLoadsToBackground()");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-15 02:21:32 +03:00
|
|
|
|
|
|
|
PRBool isPending = PR_FALSE;
|
|
|
|
if (NS_SUCCEEDED(mChannel->IsPending(&isPending)) &&
|
|
|
|
isPending) {
|
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
rv = mChannel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
|
|
|
|
|
|
|
|
loadFlags |= nsIRequest::LOAD_BACKGROUND;
|
|
|
|
ModifyLoadFlags(loadFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsMediaStream::ModifyLoadFlags(nsLoadFlags aFlags)
|
|
|
|
{
|
2009-04-10 05:28:24 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2011-02-15 02:21:32 +03:00
|
|
|
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)) {
|
2009-04-10 05:28:24 +04:00
|
|
|
rv = loadGroup->RemoveRequest(mChannel, nsnull, status);
|
|
|
|
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)) {
|
2009-04-10 05:28:24 +04:00
|
|
|
rv = loadGroup->AddRequest(mChannel, nsnull);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AddRequest() failed!");
|
|
|
|
}
|
|
|
|
}
|