Bug 1395922 - [P2] Make MediaFormatReader::SetCDMProxy asynchronously with a promise. r=cpearce,jya

MozReview-Commit-ID: 7RarmmlA0lo

--HG--
extra : rebase_source : 195f64f405aa19920ccf09ffeddb087877d653a4
This commit is contained in:
Kilik Kuo 2017-11-03 20:12:39 +08:00
Родитель 83013776bf
Коммит 23f6932509
4 изменённых файлов: 90 добавлений и 16 удалений

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

@ -1228,6 +1228,10 @@ MediaFormatReader::Shutdown()
mMetadataPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mSeekPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mSkipRequest.DisconnectIfExists();
mSetCDMPromise.RejectIfExists(
MediaResult(NS_ERROR_DOM_INVALID_STATE_ERR,
"MediaFormatReader is shutting down"),
__func__);
if (mAudio.HasPromise()) {
mAudio.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
@ -1319,22 +1323,66 @@ MediaFormatReader::Init()
}
void
MediaFormatReader::ResolveSetCDMPromiseIfDone(TrackType aTrack)
{
// When a CDM proxy is set, MFR would shutdown the existing MediaDataDecoder
// and would create new one for specific track in the next Update.
MOZ_ASSERT(OnTaskQueue());
if (mSetCDMForTracks.contains(aTrack)) {
MOZ_ASSERT(!mSetCDMPromise.IsEmpty());
mSetCDMForTracks -= aTrack;
if (mSetCDMForTracks.isEmpty()) {
mSetCDMPromise.Resolve(/* aIgnored = */ true, __func__);
}
}
}
RefPtr<SetCDMPromise>
MediaFormatReader::SetCDMProxy(CDMProxy* aProxy)
{
RefPtr<CDMProxy> proxy = aProxy;
RefPtr<MediaFormatReader> self = this;
nsCOMPtr<nsIRunnable> r =
NS_NewRunnableFunction("MediaFormatReader::SetCDMProxy", [=]() {
MOZ_ASSERT(self->OnTaskQueue());
self->mCDMProxy = proxy;
if (HasAudio()) {
self->ScheduleUpdate(TrackInfo::kAudioTrack);
}
if (HasVideo()) {
self->ScheduleUpdate(TrackInfo::kVideoTrack);
}
});
OwnerThread()->Dispatch(r.forget());
MOZ_ASSERT(OnTaskQueue());
LOGV("SetCDMProxy (%p)", aProxy);
if (mShutdown) {
return SetCDMPromise::CreateAndReject(
MediaResult(NS_ERROR_DOM_INVALID_STATE_ERR,
"MediaFormatReader is shutting down"),
__func__);
}
mSetCDMPromise.RejectIfExists(
MediaResult(NS_ERROR_DOM_INVALID_STATE_ERR,
"Another new CDM proxy is being set."),
__func__);
if (HasAudio()) {
mSetCDMForTracks += TrackInfo::kAudioTrack;
ScheduleUpdate(TrackInfo::kAudioTrack);
}
if (HasVideo()) {
mSetCDMForTracks += TrackInfo::kVideoTrack;
ScheduleUpdate(TrackInfo::kVideoTrack);
}
// Shutdown all decoders as switching CDM proxy indicates that it's
// inappropriate for the existing decoders to continue decoding via the old
// CDM proxy.
if (!mSetCDMForTracks.isEmpty()) {
ReleaseResources();
}
mCDMProxy = aProxy;
if (!mInitDone || mSetCDMForTracks.isEmpty()) {
// MFR is not initialized yet or demuxer is initialized without active
// audio and video, the promise can be resolved directly.
return SetCDMPromise::CreateAndResolve(/* aIgnored = */ true, __func__);
}
RefPtr<SetCDMPromise> p = mSetCDMPromise.Ensure(__func__);
return p;
}
bool
@ -2243,6 +2291,8 @@ MediaFormatReader::Update(TrackType aTrack)
auto& decoder = GetDecoderData(aTrack);
decoder.mUpdateScheduled = false;
ResolveSetCDMPromiseIfDone(aTrack);
if (!mInitDone) {
return;
}

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

@ -18,6 +18,7 @@
#include "MediaDataDemuxer.h"
#include "MediaMetadataManager.h"
#include "MediaPrefs.h"
#include "MediaPromiseDefs.h"
#include "nsAutoPtr.h"
#include "PDMFactory.h"
#include "SeekTarget.h"
@ -91,7 +92,6 @@ class MediaFormatReader final
static const bool IsExclusive = true;
typedef TrackInfo::TrackType TrackType;
typedef MozPromise<bool, MediaResult, IsExclusive> NotifyDataArrivedPromise;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaFormatReader)
public:
@ -194,7 +194,8 @@ public:
// cases like MSE.
bool UseBufferingHeuristics() const { return mTrackDemuxersMayBlock; }
void SetCDMProxy(CDMProxy* aProxy);
void ResolveSetCDMPromiseIfDone(TrackType aTrack);
RefPtr<SetCDMPromise> SetCDMProxy(CDMProxy* aProxy);
// Returns a string describing the state of the decoder data.
// Used for debugging purposes.
@ -792,6 +793,9 @@ private:
// Used in bug 1393399 for telemetry.
const MediaDecoderOwnerID mMediaDecoderOwnerID;
MozPromiseHolder<SetCDMPromise> mSetCDMPromise;
TrackSet mSetCDMForTracks{};
};
} // namespace mozilla

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

@ -0,0 +1,19 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MediaPromiseDefs_h_
#define MediaPromiseDefs_h_
#include "MediaResult.h"
#include "mozilla/MozPromise.h"
namespace mozilla {
using SetCDMPromise =
MozPromise<bool /* aIgnored */, MediaResult, /* IsExclusive */ true>;
} // namespace mozilla
#endif

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

@ -121,6 +121,7 @@ EXPORTS += [
'MediaMetadataManager.h',
'MediaMIMETypes.h',
'MediaPrefs.h',
'MediaPromiseDefs.h',
'MediaQueue.h',
'MediaRecorder.h',
'MediaResource.h',