2015-01-16 00:37:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2013, Mozilla Foundation and contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
2015-01-16 00:37:54 +03:00
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
#include "AnnexB.h"
|
2016-06-05 20:24:51 +03:00
|
|
|
#include "BigEndian.h"
|
2015-01-16 00:37:54 +03:00
|
|
|
#include "ClearKeyDecryptionManager.h"
|
|
|
|
#include "ClearKeyUtils.h"
|
|
|
|
#include "gmp-task-utils.h"
|
|
|
|
#include "VideoDecoder.h"
|
|
|
|
|
|
|
|
using namespace wmf;
|
2015-01-16 00:37:54 +03:00
|
|
|
|
|
|
|
VideoDecoder::VideoDecoder(GMPVideoHost *aHostAPI)
|
|
|
|
: mHostAPI(aHostAPI)
|
|
|
|
, mCallback(nullptr)
|
|
|
|
, mWorkerThread(nullptr)
|
|
|
|
, mMutex(nullptr)
|
|
|
|
, mNumInputTasks(0)
|
|
|
|
, mSentExtraData(false)
|
2015-08-20 01:06:36 +03:00
|
|
|
, mIsFlushing(false)
|
2015-03-22 21:59:42 +03:00
|
|
|
, mHasShutdown(false)
|
2015-01-16 00:37:54 +03:00
|
|
|
{
|
2015-05-06 02:40:40 +03:00
|
|
|
// We drop the ref in DecodingComplete().
|
|
|
|
AddRef();
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoDecoder::~VideoDecoder()
|
|
|
|
{
|
2015-04-13 04:39:49 +03:00
|
|
|
if (mMutex) {
|
|
|
|
mMutex->Destroy();
|
|
|
|
}
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::InitDecode(const GMPVideoCodec& aCodecSettings,
|
|
|
|
const uint8_t* aCodecSpecific,
|
|
|
|
uint32_t aCodecSpecificLength,
|
|
|
|
GMPVideoDecoderCallback* aCallback,
|
|
|
|
int32_t aCoreCount)
|
|
|
|
{
|
|
|
|
mCallback = aCallback;
|
|
|
|
assert(mCallback);
|
|
|
|
mDecoder = new WMFH264Decoder();
|
2015-09-01 08:31:48 +03:00
|
|
|
HRESULT hr = mDecoder->Init(aCoreCount);
|
2015-01-16 00:37:54 +03:00
|
|
|
if (FAILED(hr)) {
|
2015-04-20 11:07:24 +03:00
|
|
|
CK_LOGD("VideoDecoder::InitDecode failed to init WMFH264Decoder");
|
2015-01-16 00:37:54 +03:00
|
|
|
mCallback->Error(GMPGenericErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
auto err = GetPlatform()->createmutex(&mMutex);
|
2015-01-16 00:37:54 +03:00
|
|
|
if (GMP_FAILED(err)) {
|
2015-04-20 11:07:24 +03:00
|
|
|
CK_LOGD("VideoDecoder::InitDecode failed to create GMPMutex");
|
2015-01-16 00:37:54 +03:00
|
|
|
mCallback->Error(GMPGenericErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first byte is mPacketizationMode, which is only relevant for
|
|
|
|
// WebRTC/OpenH264 usecase.
|
|
|
|
const uint8_t* avcc = aCodecSpecific + 1;
|
|
|
|
const uint8_t* avccEnd = aCodecSpecific + aCodecSpecificLength;
|
|
|
|
mExtraData.insert(mExtraData.end(), avcc, avccEnd);
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
AnnexB::ConvertConfig(mExtraData, mAnnexB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::EnsureWorker()
|
|
|
|
{
|
|
|
|
if (!mWorkerThread) {
|
|
|
|
GetPlatform()->createthread(&mWorkerThread);
|
|
|
|
if (!mWorkerThread) {
|
|
|
|
mCallback->Error(GMPAllocErr);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::Decode(GMPVideoEncodedFrame* aInputFrame,
|
|
|
|
bool aMissingFrames,
|
|
|
|
const uint8_t* aCodecSpecificInfo,
|
|
|
|
uint32_t aCodecSpecificInfoLength,
|
|
|
|
int64_t aRenderTimeMs)
|
|
|
|
{
|
|
|
|
if (aInputFrame->BufferType() != GMP_BufferLength32) {
|
|
|
|
// Gecko should only send frames with 4 byte NAL sizes to GMPs.
|
|
|
|
mCallback->Error(GMPGenericErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
EnsureWorker();
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
{
|
|
|
|
AutoLock lock(mMutex);
|
|
|
|
mNumInputTasks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: we don't need the codec specific info on a per-frame basis.
|
|
|
|
// It's mostly useful for WebRTC use cases.
|
|
|
|
|
2015-12-01 08:13:58 +03:00
|
|
|
// Make a copy of the data, so we can release aInputFrame ASAP,
|
|
|
|
// to avoid too many shmem handles being held by the GMP process.
|
|
|
|
// If the GMP process holds on to too many shmem handles, the Gecko
|
|
|
|
// side can fail to allocate a shmem to send more input. This is
|
|
|
|
// particularly a problem in Gecko mochitests, which can open multiple
|
|
|
|
// actors at once which share the same pool of shmems.
|
|
|
|
DecodeData* data = new DecodeData();
|
|
|
|
Assign(data->mBuffer, aInputFrame->Buffer(), aInputFrame->Size());
|
|
|
|
data->mTimestamp = aInputFrame->TimeStamp();
|
|
|
|
data->mDuration = aInputFrame->Duration();
|
|
|
|
data->mIsKeyframe = (aInputFrame->FrameType() == kGMPKeyFrame);
|
|
|
|
const GMPEncryptedBufferMetadata* crypto = aInputFrame->GetDecryptionData();
|
|
|
|
if (crypto) {
|
|
|
|
data->mCrypto.Init(crypto);
|
|
|
|
}
|
|
|
|
aInputFrame->Destroy();
|
2015-05-06 02:40:40 +03:00
|
|
|
mWorkerThread->Post(WrapTaskRefCounted(this,
|
|
|
|
&VideoDecoder::DecodeTask,
|
2015-12-01 08:13:58 +03:00
|
|
|
data));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-12-01 08:13:58 +03:00
|
|
|
VideoDecoder::DecodeTask(DecodeData* aData)
|
2015-01-16 00:37:54 +03:00
|
|
|
{
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGD("VideoDecoder::DecodeTask");
|
2015-12-01 08:13:58 +03:00
|
|
|
AutoPtr<DecodeData> d(aData);
|
2015-01-16 00:37:54 +03:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoLock lock(mMutex);
|
|
|
|
mNumInputTasks--;
|
|
|
|
assert(mNumInputTasks >= 0);
|
|
|
|
}
|
|
|
|
|
2015-10-14 09:42:24 +03:00
|
|
|
if (mIsFlushing) {
|
|
|
|
CK_LOGD("VideoDecoder::DecodeTask rejecting frame: flushing.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 08:13:58 +03:00
|
|
|
if (!aData || !mHostAPI || !mDecoder) {
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGE("Decode job not set up correctly!");
|
2015-01-16 00:37:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 08:13:58 +03:00
|
|
|
std::vector<uint8_t>& buffer = aData->mBuffer;
|
|
|
|
if (aData->mCrypto.IsValid()) {
|
2015-01-16 00:37:54 +03:00
|
|
|
// Plugin host should have set up its decryptor/key sessions
|
|
|
|
// before trying to decode!
|
2015-01-16 00:37:54 +03:00
|
|
|
GMPErr rv =
|
2015-12-01 08:13:58 +03:00
|
|
|
ClearKeyDecryptionManager::Get()->Decrypt(buffer, aData->mCrypto);
|
2015-01-16 00:37:54 +03:00
|
|
|
|
|
|
|
if (GMP_FAILED(rv)) {
|
2015-03-22 21:59:42 +03:00
|
|
|
MaybeRunOnMainThread(WrapTask(mCallback, &GMPVideoDecoderCallback::Error, rv));
|
2015-01-16 00:37:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
AnnexB::ConvertFrameInPlace(buffer);
|
|
|
|
|
2015-12-01 08:13:58 +03:00
|
|
|
if (aData->mIsKeyframe) {
|
2015-01-16 00:37:54 +03:00
|
|
|
// We must send the SPS and PPS to Windows Media Foundation's decoder.
|
|
|
|
// Note: We do this *after* decryption, otherwise the subsample info
|
|
|
|
// would be incorrect.
|
|
|
|
buffer.insert(buffer.begin(), mAnnexB.begin(), mAnnexB.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = mDecoder->Input(buffer.data(),
|
|
|
|
buffer.size(),
|
2015-12-01 08:13:58 +03:00
|
|
|
aData->mTimestamp,
|
|
|
|
aData->mDuration);
|
2015-01-16 00:37:54 +03:00
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGD("VideoDecoder::DecodeTask() Input ret hr=0x%x\n", hr);
|
2015-01-16 00:37:54 +03:00
|
|
|
if (FAILED(hr)) {
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGE("VideoDecoder::DecodeTask() decode failed ret=0x%x%s\n",
|
2015-01-16 00:37:54 +03:00
|
|
|
hr,
|
|
|
|
((hr == MF_E_NOTACCEPTING) ? " (MF_E_NOTACCEPTING)" : ""));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (hr == S_OK) {
|
|
|
|
CComPtr<IMFSample> output;
|
|
|
|
hr = mDecoder->Output(&output);
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGD("VideoDecoder::DecodeTask() output ret=0x%x\n", hr);
|
2015-01-16 00:37:54 +03:00
|
|
|
if (hr == S_OK) {
|
2015-03-22 21:59:42 +03:00
|
|
|
MaybeRunOnMainThread(
|
2015-05-06 02:40:40 +03:00
|
|
|
WrapTaskRefCounted(this,
|
|
|
|
&VideoDecoder::ReturnOutput,
|
|
|
|
CComPtr<IMFSample>(output),
|
|
|
|
mDecoder->GetFrameWidth(),
|
|
|
|
mDecoder->GetFrameHeight(),
|
|
|
|
mDecoder->GetStride()));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
|
|
|
|
AutoLock lock(mMutex);
|
|
|
|
if (mNumInputTasks == 0) {
|
|
|
|
// We have run all input tasks. We *must* notify Gecko so that it will
|
|
|
|
// send us more data.
|
2015-03-22 21:59:42 +03:00
|
|
|
MaybeRunOnMainThread(
|
2015-02-28 00:23:33 +03:00
|
|
|
WrapTask(mCallback,
|
|
|
|
&GMPVideoDecoderCallback::InputDataExhausted));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FAILED(hr)) {
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGE("VideoDecoder::DecodeTask() output failed hr=0x%x\n", hr);
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-28 00:23:33 +03:00
|
|
|
VideoDecoder::ReturnOutput(IMFSample* aSample,
|
|
|
|
int32_t aWidth,
|
|
|
|
int32_t aHeight,
|
|
|
|
int32_t aStride)
|
2015-01-16 00:37:54 +03:00
|
|
|
{
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGD("[%p] VideoDecoder::ReturnOutput()\n", this);
|
2015-01-16 00:37:54 +03:00
|
|
|
assert(aSample);
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
GMPVideoFrame* f = nullptr;
|
2015-05-06 02:40:40 +03:00
|
|
|
auto err = mHostAPI->CreateFrame(kGMPI420VideoFrame, &f);
|
|
|
|
if (GMP_FAILED(err) || !f) {
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGE("Failed to create i420 frame!\n");
|
2015-01-16 00:37:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-05-06 02:40:40 +03:00
|
|
|
if (HasShutdown()) {
|
|
|
|
// Note: GMPVideoHost::CreateFrame() can process messages before returning,
|
|
|
|
// including a message that calls VideoDecoder::DecodingComplete(), i.e.
|
|
|
|
// we can shutdown during the call!
|
|
|
|
CK_LOGD("Shutdown while waiting on GMPVideoHost::CreateFrame()!\n");
|
|
|
|
f->Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
auto vf = static_cast<GMPVideoi420Frame*>(f);
|
|
|
|
|
2015-02-28 00:23:33 +03:00
|
|
|
hr = SampleToVideoFrame(aSample, aWidth, aHeight, aStride, vf);
|
2015-01-16 00:37:54 +03:00
|
|
|
ENSURE(SUCCEEDED(hr), /*void*/);
|
|
|
|
|
2015-02-28 00:23:33 +03:00
|
|
|
mCallback->Decoded(vf);
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
VideoDecoder::SampleToVideoFrame(IMFSample* aSample,
|
2015-02-28 00:23:33 +03:00
|
|
|
int32_t aWidth,
|
|
|
|
int32_t aHeight,
|
|
|
|
int32_t aStride,
|
2015-01-16 00:37:54 +03:00
|
|
|
GMPVideoi420Frame* aVideoFrame)
|
|
|
|
{
|
|
|
|
ENSURE(aSample != nullptr, E_POINTER);
|
|
|
|
ENSURE(aVideoFrame != nullptr, E_POINTER);
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
CComPtr<IMFMediaBuffer> mediaBuffer;
|
|
|
|
|
|
|
|
// Must convert to contiguous mediaBuffer to use IMD2DBuffer interface.
|
|
|
|
hr = aSample->ConvertToContiguousBuffer(&mediaBuffer);
|
|
|
|
ENSURE(SUCCEEDED(hr), hr);
|
|
|
|
|
|
|
|
// Try and use the IMF2DBuffer interface if available, otherwise fallback
|
|
|
|
// to the IMFMediaBuffer interface. Apparently IMF2DBuffer is more efficient,
|
|
|
|
// but only some systems (Windows 8?) support it.
|
|
|
|
BYTE* data = nullptr;
|
|
|
|
LONG stride = 0;
|
|
|
|
CComPtr<IMF2DBuffer> twoDBuffer;
|
|
|
|
hr = mediaBuffer->QueryInterface(static_cast<IMF2DBuffer**>(&twoDBuffer));
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = twoDBuffer->Lock2D(&data, &stride);
|
|
|
|
ENSURE(SUCCEEDED(hr), hr);
|
|
|
|
} else {
|
|
|
|
hr = mediaBuffer->Lock(&data, NULL, NULL);
|
|
|
|
ENSURE(SUCCEEDED(hr), hr);
|
2015-02-28 00:23:33 +03:00
|
|
|
stride = aStride;
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The V and U planes are stored 16-row-aligned, so we need to add padding
|
|
|
|
// to the row heights to ensure the Y'CbCr planes are referenced properly.
|
|
|
|
// YV12, planar format: [YYYY....][VVVV....][UUUU....]
|
|
|
|
// i.e., Y, then V, then U.
|
|
|
|
uint32_t padding = 0;
|
2015-02-28 00:23:33 +03:00
|
|
|
if (aHeight % 16 != 0) {
|
|
|
|
padding = 16 - (aHeight % 16);
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
2015-02-28 00:23:33 +03:00
|
|
|
int32_t y_size = stride * (aHeight + padding);
|
|
|
|
int32_t v_size = stride * (aHeight + padding) / 4;
|
2015-01-16 00:37:54 +03:00
|
|
|
int32_t halfStride = (stride + 1) / 2;
|
2015-02-28 00:23:33 +03:00
|
|
|
int32_t halfHeight = (aHeight + 1) / 2;
|
2015-01-16 00:37:54 +03:00
|
|
|
|
2015-05-06 02:40:40 +03:00
|
|
|
auto err = aVideoFrame->CreateEmptyFrame(stride, aHeight, stride, halfStride, halfStride);
|
|
|
|
ENSURE(GMP_SUCCEEDED(err), E_FAIL);
|
2015-01-16 00:37:54 +03:00
|
|
|
|
2015-05-06 02:40:40 +03:00
|
|
|
err = aVideoFrame->SetWidth(aWidth);
|
2015-01-16 00:37:54 +03:00
|
|
|
ENSURE(GMP_SUCCEEDED(err), E_FAIL);
|
2015-02-28 00:23:33 +03:00
|
|
|
err = aVideoFrame->SetHeight(aHeight);
|
2015-01-16 00:37:54 +03:00
|
|
|
ENSURE(GMP_SUCCEEDED(err), E_FAIL);
|
|
|
|
|
|
|
|
uint8_t* outBuffer = aVideoFrame->Buffer(kGMPYPlane);
|
|
|
|
ENSURE(outBuffer != nullptr, E_FAIL);
|
2015-02-28 00:23:33 +03:00
|
|
|
assert(aVideoFrame->AllocatedSize(kGMPYPlane) >= stride*aHeight);
|
|
|
|
memcpy(outBuffer, data, stride*aHeight);
|
2015-01-16 00:37:54 +03:00
|
|
|
|
|
|
|
outBuffer = aVideoFrame->Buffer(kGMPUPlane);
|
|
|
|
ENSURE(outBuffer != nullptr, E_FAIL);
|
|
|
|
assert(aVideoFrame->AllocatedSize(kGMPUPlane) >= halfStride*halfHeight);
|
|
|
|
memcpy(outBuffer, data+y_size, halfStride*halfHeight);
|
|
|
|
|
|
|
|
outBuffer = aVideoFrame->Buffer(kGMPVPlane);
|
|
|
|
ENSURE(outBuffer != nullptr, E_FAIL);
|
|
|
|
assert(aVideoFrame->AllocatedSize(kGMPVPlane) >= halfStride*halfHeight);
|
|
|
|
memcpy(outBuffer, data + y_size + v_size, halfStride*halfHeight);
|
|
|
|
|
|
|
|
if (twoDBuffer) {
|
|
|
|
twoDBuffer->Unlock2D();
|
|
|
|
} else {
|
|
|
|
mediaBuffer->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
LONGLONG hns = 0;
|
|
|
|
hr = aSample->GetSampleTime(&hns);
|
|
|
|
ENSURE(SUCCEEDED(hr), hr);
|
|
|
|
aVideoFrame->SetTimestamp(HNsToUsecs(hns));
|
|
|
|
|
|
|
|
hr = aSample->GetSampleDuration(&hns);
|
|
|
|
ENSURE(SUCCEEDED(hr), hr);
|
|
|
|
aVideoFrame->SetDuration(HNsToUsecs(hns));
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:06:36 +03:00
|
|
|
void
|
|
|
|
VideoDecoder::ResetCompleteTask()
|
|
|
|
{
|
|
|
|
mIsFlushing = false;
|
|
|
|
if (mCallback) {
|
|
|
|
MaybeRunOnMainThread(WrapTask(mCallback,
|
|
|
|
&GMPVideoDecoderCallback::ResetComplete));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
void
|
|
|
|
VideoDecoder::Reset()
|
|
|
|
{
|
2015-08-20 01:06:36 +03:00
|
|
|
mIsFlushing = true;
|
2015-04-13 04:39:49 +03:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->Reset();
|
|
|
|
}
|
2015-08-20 01:06:36 +03:00
|
|
|
|
|
|
|
// Schedule ResetComplete callback to run after existing frames have been
|
|
|
|
// flushed out of the task queue.
|
|
|
|
EnsureWorker();
|
|
|
|
mWorkerThread->Post(WrapTaskRefCounted(this,
|
|
|
|
&VideoDecoder::ResetCompleteTask));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::DrainTask()
|
|
|
|
{
|
2015-02-28 00:23:33 +03:00
|
|
|
mDecoder->Drain();
|
2015-01-16 00:37:54 +03:00
|
|
|
|
|
|
|
// Return any pending output.
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
while (hr == S_OK) {
|
|
|
|
CComPtr<IMFSample> output;
|
|
|
|
hr = mDecoder->Output(&output);
|
2015-01-16 00:37:54 +03:00
|
|
|
CK_LOGD("VideoDecoder::DrainTask() output ret=0x%x\n", hr);
|
2015-01-16 00:37:54 +03:00
|
|
|
if (hr == S_OK) {
|
2015-03-22 21:59:42 +03:00
|
|
|
MaybeRunOnMainThread(
|
2015-05-06 02:40:40 +03:00
|
|
|
WrapTaskRefCounted(this,
|
|
|
|
&VideoDecoder::ReturnOutput,
|
|
|
|
CComPtr<IMFSample>(output),
|
|
|
|
mDecoder->GetFrameWidth(),
|
|
|
|
mDecoder->GetFrameHeight(),
|
|
|
|
mDecoder->GetStride()));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-22 21:59:42 +03:00
|
|
|
MaybeRunOnMainThread(WrapTask(mCallback, &GMPVideoDecoderCallback::DrainComplete));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::Drain()
|
|
|
|
{
|
2015-04-13 04:39:49 +03:00
|
|
|
if (!mDecoder) {
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->DrainComplete();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-01-16 00:37:54 +03:00
|
|
|
EnsureWorker();
|
2015-05-06 02:40:40 +03:00
|
|
|
mWorkerThread->Post(WrapTaskRefCounted(this,
|
|
|
|
&VideoDecoder::DrainTask));
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoDecoder::DecodingComplete()
|
|
|
|
{
|
|
|
|
if (mWorkerThread) {
|
|
|
|
mWorkerThread->Join();
|
|
|
|
}
|
2015-03-22 21:59:42 +03:00
|
|
|
mHasShutdown = true;
|
|
|
|
|
2015-05-06 02:40:40 +03:00
|
|
|
// Release the reference we added in the constructor. There may be
|
|
|
|
// WrapRefCounted tasks that also hold references to us, and keep
|
|
|
|
// us alive a little longer.
|
|
|
|
Release();
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
2015-03-22 21:59:42 +03:00
|
|
|
|
|
|
|
void
|
2015-05-06 02:40:40 +03:00
|
|
|
VideoDecoder::MaybeRunOnMainThread(GMPTask* aTask)
|
2015-03-22 21:59:42 +03:00
|
|
|
{
|
|
|
|
class MaybeRunTask : public GMPTask
|
|
|
|
{
|
|
|
|
public:
|
2015-05-06 02:40:40 +03:00
|
|
|
MaybeRunTask(VideoDecoder* aDecoder, GMPTask* aTask)
|
2015-03-22 21:59:42 +03:00
|
|
|
: mDecoder(aDecoder), mTask(aTask)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void Run(void) {
|
|
|
|
if (mDecoder->HasShutdown()) {
|
|
|
|
CK_LOGD("Trying to dispatch to main thread after VideoDecoder has shut down");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTask->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Destroy()
|
|
|
|
{
|
|
|
|
mTask->Destroy();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-06 02:40:40 +03:00
|
|
|
RefPtr<VideoDecoder> mDecoder;
|
|
|
|
GMPTask* mTask;
|
2015-03-22 21:59:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
GetPlatform()->runonmainthread(new MaybeRunTask(this, aTask));
|
|
|
|
}
|