Bug 1421864 - move mChannelStatistics from ChannelMediaResource to MediaCacheStream. r=bechen,gerald

So it is callable from non-main thread.

MozReview-Commit-ID: atYmz4u2c9

--HG--
extra : rebase_source : 2e10064730b3e7e1ecb1a4fd65cf2e2da0390290
extra : source : 5680a6942f6985f9c6bbf284a9768ab910b37804
This commit is contained in:
JW Wang 2017-11-29 16:49:54 +08:00
Родитель cc1a11fe27
Коммит 0d7066652a
5 изменённых файлов: 39 добавлений и 44 удалений

Просмотреть файл

@ -34,18 +34,6 @@ ChannelMediaResource::ChannelMediaResource(MediaResourceCallback* aCallback,
{
}
ChannelMediaResource::ChannelMediaResource(
MediaResourceCallback* aCallback,
nsIChannel* aChannel,
nsIURI* aURI,
const MediaChannelStatistics& aStatistics)
: BaseMediaResource(aCallback, aChannel, aURI)
, mCacheStream(this, /* aIsPrivateBrowsing = */ false)
, mChannelStatistics(aStatistics)
, mSuspendAgent(mCacheStream)
{
}
ChannelMediaResource::~ChannelMediaResource()
{
MOZ_ASSERT(mClosed);
@ -297,7 +285,6 @@ ChannelMediaResource::OnStartRequest(nsIRequest* aRequest,
mCacheStream.NotifyDataStarted(mLoadID, startOffset, seekable, length);
mIsTransportSeekable = seekable;
mChannelStatistics.Start();
mSuspendAgent.Delegate(mChannel);
@ -378,8 +365,6 @@ ChannelMediaResource::OnStopRequest(nsIRequest* aRequest, nsresult aStatus)
"How can OnStopRequest fire while we're suspended?");
MOZ_DIAGNOSTIC_ASSERT(!mClosed);
mChannelStatistics.Stop();
// 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.
@ -443,18 +428,6 @@ ChannelMediaResource::OnDataAvailable(uint32_t aLoadID,
uint32_t aCount)
{
// This might happen off the main thread.
RefPtr<ChannelMediaResource> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
"ChannelMediaResource::OnDataAvailable", [self, aCount, aLoadID]() {
if (aLoadID != self->mLoadID) {
// Ignore data from the old channel.
return;
}
self->mChannelStatistics.AddBytes(aCount);
});
mCallback->AbstractMainThread()->Dispatch(r.forget());
Closure closure{ aLoadID, this };
uint32_t count = aCount;
while (count > 0) {
@ -590,7 +563,7 @@ ChannelMediaResource::CloneData(MediaResourceCallback* aCallback)
NS_ASSERTION(mCacheStream.IsAvailableForSharing(), "Stream can't be cloned");
RefPtr<ChannelMediaResource> resource =
new ChannelMediaResource(aCallback, nullptr, mURI, mChannelStatistics);
new ChannelMediaResource(aCallback, nullptr, mURI);
resource->mIsTransportSeekable = mIsTransportSeekable;
@ -608,7 +581,6 @@ ChannelMediaResource::CloneData(MediaResourceCallback* aCallback)
// mSuspendAgent.Suspend() accesses mCacheStream which is not ready
// until InitAsClone() is done.
resource->mSuspendAgent.Suspend();
resource->mChannelStatistics.Stop();
return resource.forget();
}
@ -617,8 +589,6 @@ void ChannelMediaResource::CloseChannel()
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
mChannelStatistics.Stop();
if (mChannel) {
mSuspendAgent.Revoke();
// The status we use here won't be passed to the decoder, since
@ -692,7 +662,6 @@ ChannelMediaResource::Suspend(bool aCloseImmediately)
if (mSuspendAgent.Suspend()) {
if (mChannel) {
mChannelStatistics.Stop();
element->DownloadSuspended();
}
}
@ -716,7 +685,6 @@ ChannelMediaResource::Resume()
if (mSuspendAgent.Resume()) {
if (mChannel) {
// Just wake up our existing channel
mChannelStatistics.Start();
element->DownloadResumed();
} else {
int64_t totalLength = GetLength();
@ -976,8 +944,7 @@ ChannelMediaResource::Unpin()
double
ChannelMediaResource::GetDownloadRate(bool* aIsReliable)
{
MOZ_ASSERT(NS_IsMainThread());
return mChannelStatistics.GetRate(aIsReliable);
return mCacheStream.GetDownloadRate(aIsReliable);
}
int64_t

Просмотреть файл

@ -8,7 +8,6 @@
#include "BaseMediaResource.h"
#include "MediaCache.h"
#include "MediaChannelStatistics.h"
#include "mozilla/Mutex.h"
#include "nsIChannelEventSink.h"
#include "nsIHttpChannel.h"
@ -68,11 +67,7 @@ public:
ChannelMediaResource(MediaResourceCallback* aDecoder,
nsIChannel* aChannel,
nsIURI* aURI,
bool aIsPrivateBrowsing);
ChannelMediaResource(MediaResourceCallback* aDecoder,
nsIChannel* aChannel,
nsIURI* aURI,
const MediaChannelStatistics& aStatistics);
bool aIsPrivateBrowsing = false);
~ChannelMediaResource();
// These are called on the main thread by MediaCache. These must
@ -138,7 +133,6 @@ public:
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override {
// Might be useful to track in the future:
// - mListener (seems minor)
// - mChannelStatistics (seems minor)
size_t size = BaseMediaResource::SizeOfExcludingThis(aMallocSizeOf);
size += mCacheStream.SizeOfExcludingThis(aMallocSizeOf);
@ -252,7 +246,6 @@ protected:
// Any thread access
MediaCacheStream mCacheStream;
MediaChannelStatistics mChannelStatistics;
ChannelSuspendAgent mSuspendAgent;
};

Просмотреть файл

@ -2033,6 +2033,8 @@ MediaCacheStream::NotifyDataStartedInternal(uint32_t aLoadID,
// Reset these flags since a new load has begun.
mChannelEnded = false;
mDidNotifyDataEnded = false;
UpdateDownloadStatistics(lock);
}
void
@ -2092,6 +2094,8 @@ MediaCacheStream::NotifyDataReceived(uint32_t aLoadID,
return;
}
mDownloadStatistics.AddBytes(aCount);
auto source = MakeSpan<const uint8_t>(aData, aCount);
// We process the data one block (or part of a block) at a time
@ -2179,6 +2183,16 @@ MediaCacheStream::FlushPartialBlockInternal(AutoLock& aLock, bool aNotifyAll)
}
}
void
MediaCacheStream::UpdateDownloadStatistics(AutoLock&)
{
if (mChannelEnded || mClientSuspended) {
mDownloadStatistics.Stop();
} else {
mDownloadStatistics.Start();
}
}
void
MediaCacheStream::NotifyDataEndedInternal(uint32_t aLoadID,
nsresult aStatus,
@ -2236,6 +2250,8 @@ MediaCacheStream::NotifyDataEndedInternal(uint32_t aLoadID,
mChannelEnded = true;
mMediaCache->QueueUpdate(lock);
UpdateDownloadStatistics(lock);
if (NS_FAILED(aStatus)) {
// Notify the client about this network error.
mDidNotifyDataEnded = true;
@ -2292,6 +2308,7 @@ MediaCacheStream::NotifyClientSuspended(bool aSuspended)
mClientSuspended = aSuspended;
// mClientSuspended changes the decision of reading streams.
mMediaCache->QueueUpdate(lock);
UpdateDownloadStatistics(lock);
}
});
OwnerThread()->Dispatch(r.forget());
@ -2865,6 +2882,8 @@ MediaCacheStream::InitAsClone(MediaCacheStream* aOriginal)
mPrincipal = aOriginal->mPrincipal;
mStreamLength = aOriginal->mStreamLength;
mIsTransportSeekable = aOriginal->mIsTransportSeekable;
mDownloadStatistics = aOriginal->mDownloadStatistics;
mDownloadStatistics.Stop();
// Cloned streams are initially suspended, since there is no channel open
// initially for a clone.
@ -2925,6 +2944,14 @@ nsresult MediaCacheStream::GetCachedRanges(MediaByteRangeSet& aRanges)
return NS_OK;
}
double
MediaCacheStream::GetDownloadRate(bool* aIsReliable)
{
// TODO: Assert non-main thread.
AutoLock lock(mMediaCache->Monitor());
return mDownloadStatistics.GetRate(aIsReliable);
}
nsCString
MediaCacheStream::GetDebugInfo()
{

Просмотреть файл

@ -15,6 +15,8 @@
#include "nsTArray.h"
#include "nsTHashtable.h"
#include "MediaChannelStatistics.h"
class nsIEventTarget;
class nsIPrincipal;
@ -315,6 +317,8 @@ public:
// are used, to ensure no data is evicted.
nsresult GetCachedRanges(MediaByteRangeSet& aRanges);
double GetDownloadRate(bool* aIsReliable);
// Reads from buffered data only. Will fail if not all data to be read is
// in the cache. Will not mark blocks as read. Can be called from the main
// thread. It's the caller's responsibility to wrap the call in a pin/unpin,
@ -462,6 +466,8 @@ private:
nsresult aStatus,
bool aReopenOnError);
void UpdateDownloadStatistics(AutoLock&);
// Instance of MediaCache to use with this MediaCacheStream.
RefPtr<MediaCache> mMediaCache;
@ -549,6 +555,8 @@ private:
// True if the client is suspended. Accessed on the owner thread only.
bool mClientSuspended = false;
MediaChannelStatistics mDownloadStatistics;
};
} // namespace mozilla

Просмотреть файл

@ -29,8 +29,8 @@ static const int64_t RELIABLE_DATA_THRESHOLD = 57 * 1460;
class MediaChannelStatistics {
public:
MediaChannelStatistics() = default;
MediaChannelStatistics(const MediaChannelStatistics&) = default;
MediaChannelStatistics& operator=(const MediaChannelStatistics&) = default;
void Reset() {
mLastStartTime = TimeStamp();