2016-09-21 12:25:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=99: */
|
|
|
|
/* 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/. */
|
|
|
|
#include "VideoDecoderChild.h"
|
|
|
|
#include "VideoDecoderManagerChild.h"
|
|
|
|
#include "mozilla/layers/TextureClient.h"
|
|
|
|
#include "base/thread.h"
|
|
|
|
#include "MediaInfo.h"
|
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "GPUVideoImage.h"
|
2016-11-22 05:50:46 +03:00
|
|
|
#include "mozilla/layers/SynchronousTask.h"
|
2016-09-21 12:25:41 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using base::Thread;
|
|
|
|
using namespace ipc;
|
|
|
|
using namespace layers;
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
VideoDecoderChild::VideoDecoderChild()
|
|
|
|
: mThread(VideoDecoderManagerChild::GetManagerThread())
|
2016-11-22 07:43:45 +03:00
|
|
|
, mFlushTask(nullptr)
|
2016-11-02 23:55:07 +03:00
|
|
|
, mCanSend(false)
|
2016-10-03 11:20:35 +03:00
|
|
|
, mInitialized(false)
|
2016-10-03 11:21:07 +03:00
|
|
|
, mIsHardwareAccelerated(false)
|
2016-09-21 12:25:41 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoDecoderChild::~VideoDecoderChild()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
|
|
|
mInitPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::RecvOutput(const VideoDataIPDL& aData)
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
|
|
|
VideoInfo info(aData.display().width, aData.display().height);
|
|
|
|
|
|
|
|
// The Image here creates a TextureData object that takes ownership
|
|
|
|
// of the SurfaceDescriptor, and is responsible for making sure that
|
|
|
|
// it gets deallocated.
|
2016-11-02 23:55:07 +03:00
|
|
|
RefPtr<Image> image = new GPUVideoImage(GetManager(), aData.sd(), aData.display());
|
2016-09-21 12:25:41 +03:00
|
|
|
|
|
|
|
RefPtr<VideoData> video = VideoData::CreateFromImage(info,
|
|
|
|
aData.base().offset(),
|
|
|
|
aData.base().time(),
|
|
|
|
aData.base().duration(),
|
|
|
|
image,
|
|
|
|
aData.base().keyframe(),
|
|
|
|
aData.base().timecode(),
|
|
|
|
IntRect());
|
2016-11-13 06:09:35 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Output(video);
|
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::RecvInputExhausted()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-13 06:09:35 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->InputExhausted();
|
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::RecvDrainComplete()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-13 06:09:35 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->DrainComplete();
|
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::RecvError(const nsresult& aError)
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-13 06:09:35 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Error(aError);
|
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-10-03 11:21:07 +03:00
|
|
|
VideoDecoderChild::RecvInitComplete(const bool& aHardware, const nsCString& aHardwareReason)
|
2016-09-21 12:25:41 +03:00
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
|
|
|
mInitPromise.Resolve(TrackInfo::kVideoTrack, __func__);
|
2016-10-03 11:20:35 +03:00
|
|
|
mInitialized = true;
|
2016-10-03 11:21:07 +03:00
|
|
|
mIsHardwareAccelerated = aHardware;
|
|
|
|
mHardwareAcceleratedReason = aHardwareReason;
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::RecvInitFailed(const nsresult& aReason)
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
|
|
|
mInitPromise.Reject(aReason, __func__);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 05:50:46 +03:00
|
|
|
mozilla::ipc::IPCResult
|
|
|
|
VideoDecoderChild::RecvFlushComplete()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mFlushTask);
|
|
|
|
AutoCompleteTask complete(mFlushTask);
|
|
|
|
mFlushTask = nullptr;
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:25:41 +03:00
|
|
|
void
|
|
|
|
VideoDecoderChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
2016-10-03 11:20:35 +03:00
|
|
|
if (aWhy == AbnormalShutdown) {
|
2016-11-08 05:21:35 +03:00
|
|
|
// Defer reporting an error until we've recreated the manager so that
|
|
|
|
// it'll be safe for MediaFormatReader to recreate decoders
|
|
|
|
RefPtr<VideoDecoderChild> ref = this;
|
|
|
|
GetManager()->RunWhenRecreated(NS_NewRunnableFunction([=]() {
|
2016-11-13 06:09:35 +03:00
|
|
|
if (ref->mInitialized && ref->mCallback) {
|
2016-11-08 05:22:37 +03:00
|
|
|
ref->mCallback->Error(NS_ERROR_DOM_MEDIA_NEED_NEW_DECODER);
|
2016-11-08 05:21:35 +03:00
|
|
|
} else {
|
2016-11-08 05:22:37 +03:00
|
|
|
ref->mInitPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_NEED_NEW_DECODER, __func__);
|
2016-11-08 05:21:35 +03:00
|
|
|
}
|
|
|
|
}));
|
2016-10-03 11:20:35 +03:00
|
|
|
}
|
2016-11-22 05:50:46 +03:00
|
|
|
if (mFlushTask) {
|
|
|
|
AutoCompleteTask complete(mFlushTask);
|
|
|
|
mFlushTask = nullptr;
|
|
|
|
}
|
2016-09-21 12:25:41 +03:00
|
|
|
mCanSend = false;
|
|
|
|
}
|
|
|
|
|
2016-11-22 05:44:38 +03:00
|
|
|
bool
|
2016-09-21 12:25:41 +03:00
|
|
|
VideoDecoderChild::InitIPDL(MediaDataDecoderCallback* aCallback,
|
|
|
|
const VideoInfo& aVideoInfo,
|
2016-11-13 04:19:02 +03:00
|
|
|
const layers::TextureFactoryIdentifier& aIdentifier)
|
2016-09-21 12:25:41 +03:00
|
|
|
{
|
2016-11-02 23:55:07 +03:00
|
|
|
RefPtr<VideoDecoderManagerChild> manager = VideoDecoderManagerChild::GetSingleton();
|
2016-11-08 05:21:35 +03:00
|
|
|
// If the manager isn't available, then don't initialize mIPDLSelfRef and leave
|
|
|
|
// us in an error state. We'll then immediately reject the promise when Init()
|
|
|
|
// is called and the caller can try again. Hopefully by then the new manager is
|
|
|
|
// ready, or we've notified the caller of it being no longer available.
|
|
|
|
// If not, then the cycle repeats until we're ready.
|
|
|
|
if (!manager || !manager->CanSend()) {
|
2016-11-22 05:44:38 +03:00
|
|
|
return true;
|
2016-11-02 23:55:07 +03:00
|
|
|
}
|
2016-11-08 05:21:35 +03:00
|
|
|
|
2016-09-21 12:25:41 +03:00
|
|
|
mIPDLSelfRef = this;
|
|
|
|
mCallback = aCallback;
|
2016-11-22 05:44:38 +03:00
|
|
|
bool success = false;
|
|
|
|
if (manager->SendPVideoDecoderConstructor(this, aVideoInfo, aIdentifier, &success)) {
|
2016-11-02 23:55:07 +03:00
|
|
|
mCanSend = true;
|
|
|
|
}
|
2016-11-22 05:44:38 +03:00
|
|
|
return success;
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoderChild::DestroyIPDL()
|
|
|
|
{
|
|
|
|
if (mCanSend) {
|
|
|
|
PVideoDecoderChild::Send__delete__(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoderChild::IPDLActorDestroyed()
|
|
|
|
{
|
|
|
|
mIPDLSelfRef = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// MediaDataDecoder methods
|
|
|
|
|
|
|
|
RefPtr<MediaDataDecoder::InitPromise>
|
|
|
|
VideoDecoderChild::Init()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-08 05:21:35 +03:00
|
|
|
|
|
|
|
if (!mIPDLSelfRef) {
|
2016-09-21 12:25:41 +03:00
|
|
|
return MediaDataDecoder::InitPromise::CreateAndReject(
|
2016-11-08 05:21:35 +03:00
|
|
|
NS_ERROR_DOM_MEDIA_DECODE_ERR, __func__);
|
|
|
|
}
|
|
|
|
// If we failed to send this, then we'll still resolve the Init promise
|
|
|
|
// as ActorDestroy handles it.
|
|
|
|
if (mCanSend) {
|
2016-11-22 05:44:38 +03:00
|
|
|
SendInit();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
return mInitPromise.Ensure(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoderChild::Input(MediaRawData* aSample)
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
|
|
|
if (!mCanSend) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: It would be nice to add an allocator method to
|
|
|
|
// MediaDataDecoder so that the demuxer could write directly
|
|
|
|
// into shmem rather than requiring a copy here.
|
|
|
|
Shmem buffer;
|
|
|
|
if (!AllocShmem(aSample->Size(), Shmem::SharedMemory::TYPE_BASIC, &buffer)) {
|
2016-11-08 05:21:35 +03:00
|
|
|
mCallback->Error(NS_ERROR_DOM_MEDIA_DECODE_ERR);
|
2016-09-21 12:25:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer.get<uint8_t>(), aSample->Data(), aSample->Size());
|
|
|
|
|
|
|
|
MediaRawDataIPDL sample(MediaDataIPDL(aSample->mOffset,
|
|
|
|
aSample->mTime,
|
|
|
|
aSample->mTimecode,
|
|
|
|
aSample->mDuration,
|
|
|
|
aSample->mFrames,
|
|
|
|
aSample->mKeyframe),
|
|
|
|
buffer);
|
2016-11-08 05:21:35 +03:00
|
|
|
SendInput(sample);
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-22 05:50:46 +03:00
|
|
|
VideoDecoderChild::Flush(SynchronousTask* aTask)
|
2016-09-21 12:25:41 +03:00
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-08 05:21:35 +03:00
|
|
|
if (mCanSend) {
|
|
|
|
SendFlush();
|
2016-11-22 05:50:46 +03:00
|
|
|
mFlushTask = aTask;
|
|
|
|
} else {
|
|
|
|
AutoCompleteTask complete(aTask);
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoderChild::Drain()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-08 05:21:35 +03:00
|
|
|
if (mCanSend) {
|
|
|
|
SendDrain();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoderChild::Shutdown()
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-13 06:09:35 +03:00
|
|
|
mInitPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2016-11-08 05:21:35 +03:00
|
|
|
if (mCanSend) {
|
|
|
|
SendShutdown();
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
2016-10-03 11:20:35 +03:00
|
|
|
mInitialized = false;
|
2016-11-22 05:51:18 +03:00
|
|
|
mCallback = nullptr;
|
2016-09-21 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 11:21:07 +03:00
|
|
|
bool
|
|
|
|
VideoDecoderChild::IsHardwareAccelerated(nsACString& aFailureReason) const
|
|
|
|
{
|
|
|
|
aFailureReason = mHardwareAcceleratedReason;
|
|
|
|
return mIsHardwareAccelerated;
|
|
|
|
}
|
|
|
|
|
2016-10-03 11:20:54 +03:00
|
|
|
void
|
|
|
|
VideoDecoderChild::SetSeekThreshold(const media::TimeUnit& aTime)
|
|
|
|
{
|
|
|
|
AssertOnManagerThread();
|
2016-11-08 05:21:35 +03:00
|
|
|
if (mCanSend) {
|
|
|
|
SendSetSeekThreshold(aTime.ToMicroseconds());
|
2016-10-03 11:20:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:25:41 +03:00
|
|
|
void
|
|
|
|
VideoDecoderChild::AssertOnManagerThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mThread);
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:55:07 +03:00
|
|
|
VideoDecoderManagerChild*
|
|
|
|
VideoDecoderChild::GetManager()
|
|
|
|
{
|
|
|
|
if (!mCanSend) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<VideoDecoderManagerChild*>(Manager());
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:25:41 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|