2014-05-18 07:05:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "GMPVideoEncoderChild.h"
|
2015-02-10 13:48:42 +03:00
|
|
|
#include "GMPContentChild.h"
|
2014-05-18 07:05:46 +04:00
|
|
|
#include <stdio.h>
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-05-18 07:05:46 +04:00
|
|
|
#include "GMPVideoEncodedFrameImpl.h"
|
|
|
|
#include "GMPVideoi420FrameImpl.h"
|
2015-06-09 23:38:05 +03:00
|
|
|
#include "runnable_utils.h"
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
2015-02-10 13:48:42 +03:00
|
|
|
GMPVideoEncoderChild::GMPVideoEncoderChild(GMPContentChild* aPlugin)
|
2015-06-09 23:38:05 +03:00
|
|
|
: GMPSharedMemManager(aPlugin)
|
|
|
|
, mPlugin(aPlugin)
|
|
|
|
, mVideoEncoder(nullptr)
|
|
|
|
, mVideoHost(this)
|
|
|
|
, mNeedShmemIntrCount(0)
|
|
|
|
, mPendingEncodeComplete(false)
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPlugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPVideoEncoderChild::~GMPVideoEncoderChild()
|
|
|
|
{
|
2015-06-09 23:38:05 +03:00
|
|
|
MOZ_ASSERT(!mNeedShmemIntrCount);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPVideoEncoderChild::Init(GMPVideoEncoder* aEncoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEncoder, "Cannot initialize video encoder child without a video encoder!");
|
|
|
|
mVideoEncoder = aEncoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPVideoHostImpl&
|
|
|
|
GMPVideoEncoderChild::Host()
|
|
|
|
{
|
|
|
|
return mVideoHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPVideoEncoderChild::Encoded(GMPVideoEncodedFrame* aEncodedFrame,
|
2014-07-11 07:36:21 +04:00
|
|
|
const uint8_t* aCodecSpecificInfo,
|
|
|
|
uint32_t aCodecSpecificInfoLength)
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPlugin->GMPMessageLoop() == MessageLoop::current());
|
|
|
|
|
|
|
|
auto ef = static_cast<GMPVideoEncodedFrameImpl*>(aEncodedFrame);
|
|
|
|
|
|
|
|
GMPVideoEncodedFrameData frameData;
|
|
|
|
ef->RelinquishFrameData(frameData);
|
|
|
|
|
2014-07-11 07:36:21 +04:00
|
|
|
nsTArray<uint8_t> codecSpecific;
|
|
|
|
codecSpecific.AppendElements(aCodecSpecificInfo, aCodecSpecificInfoLength);
|
2014-07-11 18:39:10 +04:00
|
|
|
SendEncoded(frameData, codecSpecific);
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
aEncodedFrame->Destroy();
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:35:01 +04:00
|
|
|
void
|
|
|
|
GMPVideoEncoderChild::Error(GMPErr aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPlugin->GMPMessageLoop() == MessageLoop::current());
|
|
|
|
|
|
|
|
SendError(aError);
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvInitEncode(const GMPVideoCodec& aCodecSettings,
|
2015-01-16 22:58:52 +03:00
|
|
|
InfallibleTArray<uint8_t>&& aCodecSpecific,
|
2014-05-18 07:05:46 +04:00
|
|
|
const int32_t& aNumberOfCores,
|
|
|
|
const uint32_t& aMaxPayloadSize)
|
|
|
|
{
|
|
|
|
if (!mVideoEncoder) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
2014-07-11 07:35:56 +04:00
|
|
|
mVideoEncoder->InitEncode(aCodecSettings,
|
|
|
|
aCodecSpecific.Elements(),
|
|
|
|
aCodecSpecific.Length(),
|
|
|
|
this,
|
|
|
|
aNumberOfCores,
|
|
|
|
aMaxPayloadSize);
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvEncode(const GMPVideoi420FrameData& aInputFrame,
|
2015-01-16 22:58:52 +03:00
|
|
|
InfallibleTArray<uint8_t>&& aCodecSpecificInfo,
|
|
|
|
InfallibleTArray<GMPVideoFrameType>&& aFrameTypes)
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
if (!mVideoEncoder) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
auto f = new GMPVideoi420FrameImpl(aInputFrame, &mVideoHost);
|
|
|
|
|
2014-07-11 05:43:04 +04:00
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
2014-07-11 07:36:21 +04:00
|
|
|
mVideoEncoder->Encode(f,
|
|
|
|
aCodecSpecificInfo.Elements(),
|
|
|
|
aCodecSpecificInfo.Length(),
|
|
|
|
aFrameTypes.Elements(),
|
|
|
|
aFrameTypes.Length());
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-07-02 06:26:35 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-01-16 22:58:52 +03:00
|
|
|
GMPVideoEncoderChild::RecvChildShmemForPool(Shmem&& aEncodedBuffer)
|
2014-07-02 06:26:35 +04:00
|
|
|
{
|
|
|
|
if (aEncodedBuffer.IsWritable()) {
|
2014-07-28 19:44:20 +04:00
|
|
|
mVideoHost.SharedMemMgr()->MgrDeallocShmem(GMPSharedMem::kGMPEncodedData,
|
2014-07-02 06:26:35 +04:00
|
|
|
aEncodedBuffer);
|
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-07-02 06:26:35 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvSetChannelParameters(const uint32_t& aPacketLoss,
|
|
|
|
const uint32_t& aRTT)
|
|
|
|
{
|
|
|
|
if (!mVideoEncoder) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
|
|
|
mVideoEncoder->SetChannelParameters(aPacketLoss, aRTT);
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvSetRates(const uint32_t& aNewBitRate,
|
|
|
|
const uint32_t& aFrameRate)
|
|
|
|
{
|
|
|
|
if (!mVideoEncoder) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
|
|
|
mVideoEncoder->SetRates(aNewBitRate, aFrameRate);
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvSetPeriodicKeyFrames(const bool& aEnable)
|
|
|
|
{
|
|
|
|
if (!mVideoEncoder) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
|
|
|
mVideoEncoder->SetPeriodicKeyFrames(aEnable);
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPVideoEncoderChild::RecvEncodingComplete()
|
|
|
|
{
|
2015-06-09 23:38:05 +03:00
|
|
|
MOZ_ASSERT(mPlugin->GMPMessageLoop() == MessageLoop::current());
|
|
|
|
|
|
|
|
if (mNeedShmemIntrCount) {
|
|
|
|
// There's a GMP blocked in Alloc() waiting for the CallNeedShem() to
|
|
|
|
// return a frame they can use. Don't call the GMP's EncodingComplete()
|
|
|
|
// now and don't delete the GMPVideoEncoderChild, defer processing the
|
|
|
|
// EncodingComplete() until once the Alloc() finishes.
|
|
|
|
mPendingEncodeComplete = true;
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-06-09 23:38:05 +03:00
|
|
|
}
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
if (!mVideoEncoder) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any return code. It is OK for this to fail without killing the process.
|
|
|
|
mVideoEncoder->EncodingComplete();
|
|
|
|
|
|
|
|
mVideoHost.DoneWithAPI();
|
|
|
|
|
|
|
|
mPlugin = nullptr;
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this);
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2015-06-09 23:38:05 +03:00
|
|
|
bool
|
|
|
|
GMPVideoEncoderChild::Alloc(size_t aSize,
|
|
|
|
Shmem::SharedMemory::SharedMemoryType aType,
|
|
|
|
Shmem* aMem)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPlugin->GMPMessageLoop() == MessageLoop::current());
|
|
|
|
|
|
|
|
bool rv;
|
|
|
|
#ifndef SHMEM_ALLOC_IN_CHILD
|
|
|
|
++mNeedShmemIntrCount;
|
|
|
|
rv = CallNeedShmem(aSize, aMem);
|
|
|
|
--mNeedShmemIntrCount;
|
2016-07-11 12:52:31 +03:00
|
|
|
if (mPendingEncodeComplete && mNeedShmemIntrCount == 0) {
|
|
|
|
mPendingEncodeComplete = false;
|
2016-05-05 11:45:00 +03:00
|
|
|
mPlugin->GMPMessageLoop()->PostTask(
|
|
|
|
NewRunnableMethod(this, &GMPVideoEncoderChild::RecvEncodingComplete));
|
2015-06-09 23:38:05 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef GMP_SAFE_SHMEM
|
|
|
|
rv = AllocShmem(aSize, aType, aMem);
|
|
|
|
#else
|
|
|
|
rv = AllocUnsafeShmem(aSize, aType, aMem);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPVideoEncoderChild::Dealloc(Shmem& aMem)
|
|
|
|
{
|
|
|
|
#ifndef SHMEM_ALLOC_IN_CHILD
|
|
|
|
SendParentShmemForPool(aMem);
|
|
|
|
#else
|
|
|
|
DeallocShmem(aMem);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|