2012-09-27 04:33:43 +00:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaOmxReader.h"
|
2012-09-27 04:33:43 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2012-09-27 04:33:43 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-03-02 19:14:44 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2012-09-27 04:33:43 +00:00
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "VideoUtils.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaOmxDecoder.h"
|
2012-11-19 15:11:21 +00:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2013-02-01 22:13:23 +00:00
|
|
|
#include "OmxDecoder.h"
|
|
|
|
#include "MPAPI.h"
|
2012-09-27 04:33:43 +00:00
|
|
|
|
2012-11-27 04:08:40 +00:00
|
|
|
#define MAX_DROPPED_FRAMES 25
|
2013-06-15 11:51:56 +00:00
|
|
|
// Try not to spend more than this much time in a single call to DecodeVideoFrame.
|
|
|
|
#define MAX_VIDEO_DECODE_SECONDS 3.0
|
2012-11-27 04:08:40 +00:00
|
|
|
|
2012-09-27 04:33:43 +00:00
|
|
|
using namespace android;
|
2012-11-14 19:45:33 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-27 04:33:43 +00:00
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
MediaOmxReader::MediaOmxReader(AbstractMediaDecoder *aDecoder) :
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoderReader(aDecoder),
|
2012-09-27 04:33:43 +00:00
|
|
|
mHasVideo(false),
|
|
|
|
mHasAudio(false),
|
|
|
|
mVideoSeekTimeUs(-1),
|
|
|
|
mAudioSeekTimeUs(-1),
|
2012-11-27 04:08:40 +00:00
|
|
|
mSkipCount(0)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaOmxReader::~MediaOmxReader()
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
ResetDecode();
|
2013-06-10 12:22:05 +00:00
|
|
|
mOmxDecoder.clear();
|
2012-09-27 04:33:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaOmxReader::Init(MediaDecoderReader* aCloneDonor)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:22:05 +00:00
|
|
|
bool MediaOmxReader::IsWaitingMediaResources()
|
|
|
|
{
|
2013-06-22 12:58:12 +00:00
|
|
|
if (!mOmxDecoder.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-10 12:22:05 +00:00
|
|
|
return mOmxDecoder->IsWaitingMediaResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaOmxReader::IsDormantNeeded()
|
|
|
|
{
|
|
|
|
if (!mOmxDecoder.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mOmxDecoder->IsDormantNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaOmxReader::ReleaseMediaResources()
|
|
|
|
{
|
|
|
|
ResetDecode();
|
2013-06-22 12:58:12 +00:00
|
|
|
if (mOmxDecoder.get()) {
|
|
|
|
mOmxDecoder->ReleaseMediaResources();
|
|
|
|
}
|
2013-06-10 12:22:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 19:40:07 +00:00
|
|
|
nsresult MediaOmxReader::ReadMetadata(VideoInfo* aInfo,
|
2013-05-02 22:59:18 +00:00
|
|
|
MetadataTags** aTags)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
*aTags = nullptr;
|
|
|
|
|
2013-02-16 02:27:18 +00:00
|
|
|
if (!mOmxDecoder.get()) {
|
2012-09-27 04:33:43 +00:00
|
|
|
mOmxDecoder = new OmxDecoder(mDecoder->GetResource(), mDecoder);
|
2013-04-04 22:33:45 +00:00
|
|
|
if (!mOmxDecoder->Init()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-27 04:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 12:22:05 +00:00
|
|
|
if (!mOmxDecoder->TryLoad()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWaitingMediaResources()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-27 04:33:43 +00:00
|
|
|
// Set the total duration (the max of the audio and video track).
|
|
|
|
int64_t durationUs;
|
|
|
|
mOmxDecoder->GetDuration(&durationUs);
|
|
|
|
if (durationUs) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-19 15:11:21 +00:00
|
|
|
mDecoder->SetMediaDuration(durationUs);
|
2012-09-27 04:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mOmxDecoder->HasVideo()) {
|
|
|
|
int32_t width, height;
|
|
|
|
mOmxDecoder->GetVideoParameters(&width, &height);
|
|
|
|
nsIntRect pictureRect(0, 0, width, height);
|
|
|
|
|
|
|
|
// Validate the container-reported frame and pictureRect sizes. This ensures
|
|
|
|
// that our video frame creation code doesn't overflow.
|
|
|
|
nsIntSize displaySize(width, height);
|
|
|
|
nsIntSize frameSize(width, height);
|
2012-11-28 19:40:07 +00:00
|
|
|
if (!VideoInfo::ValidateVideoRegion(frameSize, pictureRect, displaySize)) {
|
2012-09-27 04:33:43 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Video track's frame sizes will not overflow. Activate the video track.
|
|
|
|
mHasVideo = mInfo.mHasVideo = true;
|
|
|
|
mInfo.mDisplay = displaySize;
|
|
|
|
mPicture = pictureRect;
|
|
|
|
mInitialFrame = frameSize;
|
|
|
|
VideoFrameContainer* container = mDecoder->GetVideoFrameContainer();
|
|
|
|
if (container) {
|
|
|
|
container->SetCurrentFrame(gfxIntSize(displaySize.width, displaySize.height),
|
|
|
|
nullptr,
|
|
|
|
mozilla::TimeStamp::Now());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOmxDecoder->HasAudio()) {
|
|
|
|
int32_t numChannels, sampleRate;
|
|
|
|
mOmxDecoder->GetAudioParameters(&numChannels, &sampleRate);
|
|
|
|
mHasAudio = mInfo.mHasAudio = true;
|
|
|
|
mInfo.mAudioChannels = numChannels;
|
|
|
|
mInfo.mAudioRate = sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aInfo = mInfo;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resets all state related to decoding, emptying all buffers etc.
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaOmxReader::ResetDecode()
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoderReader::ResetDecode();
|
2012-09-27 04:33:43 +00:00
|
|
|
|
|
|
|
VideoFrameContainer* container = mDecoder->GetVideoFrameContainer();
|
|
|
|
if (container) {
|
|
|
|
container->ClearCurrentFrame();
|
|
|
|
}
|
2012-09-27 04:33:43 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaOmxReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2013-05-02 22:59:18 +00:00
|
|
|
int64_t aTimeThreshold)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
// Record number of frames decoded and parsed. Automatically update the
|
|
|
|
// stats counters using the AutoNotifyDecoded stack-based class.
|
|
|
|
uint32_t parsed = 0, decoded = 0;
|
2012-11-19 15:11:21 +00:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);
|
2012-09-27 04:33:43 +00:00
|
|
|
|
|
|
|
bool doSeek = mVideoSeekTimeUs != -1;
|
|
|
|
if (doSeek) {
|
|
|
|
aTimeThreshold = mVideoSeekTimeUs;
|
|
|
|
}
|
|
|
|
|
2013-06-15 11:51:56 +00:00
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
|
|
|
|
// Read next frame. Don't let this loop run for too long.
|
|
|
|
while ((TimeStamp::Now() - start) < TimeDuration::FromSeconds(MAX_VIDEO_DECODE_SECONDS)) {
|
2012-09-27 04:33:43 +00:00
|
|
|
MPAPI::VideoFrame frame;
|
|
|
|
frame.mGraphicBuffer = nullptr;
|
2012-11-27 04:08:40 +00:00
|
|
|
frame.mShouldSkip = false;
|
2012-09-27 04:33:43 +00:00
|
|
|
if (!mOmxDecoder->ReadVideo(&frame, aTimeThreshold, aKeyframeSkip, doSeek)) {
|
|
|
|
mVideoQueue.Finish();
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-15 11:51:56 +00:00
|
|
|
doSeek = false;
|
2012-09-27 04:33:43 +00:00
|
|
|
|
2013-06-05 23:43:43 +00:00
|
|
|
// Ignore empty buffer which stagefright media read will sporadically return
|
|
|
|
if (frame.mSize == 0 && !frame.mGraphicBuffer) {
|
2013-06-15 11:51:56 +00:00
|
|
|
continue;
|
2013-06-05 23:43:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 04:08:40 +00:00
|
|
|
parsed++;
|
|
|
|
if (frame.mShouldSkip && mSkipCount < MAX_DROPPED_FRAMES) {
|
|
|
|
mSkipCount++;
|
2013-06-15 11:51:56 +00:00
|
|
|
continue;
|
2012-11-27 04:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mSkipCount = 0;
|
|
|
|
|
2012-09-27 04:33:43 +00:00
|
|
|
mVideoSeekTimeUs = -1;
|
2013-06-15 11:51:56 +00:00
|
|
|
aKeyframeSkip = false;
|
2012-09-27 04:33:43 +00:00
|
|
|
|
|
|
|
nsIntRect picture = mPicture;
|
|
|
|
if (frame.Y.mWidth != mInitialFrame.width ||
|
|
|
|
frame.Y.mHeight != mInitialFrame.height) {
|
|
|
|
|
|
|
|
// Frame size is different from what the container reports. This is legal,
|
|
|
|
// and we will preserve the ratio of the crop rectangle as it
|
|
|
|
// was reported relative to the picture size reported by the container.
|
|
|
|
picture.x = (mPicture.x * frame.Y.mWidth) / mInitialFrame.width;
|
|
|
|
picture.y = (mPicture.y * frame.Y.mHeight) / mInitialFrame.height;
|
|
|
|
picture.width = (frame.Y.mWidth * mPicture.width) / mInitialFrame.width;
|
|
|
|
picture.height = (frame.Y.mHeight * mPicture.height) / mInitialFrame.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the approximate byte position in the stream.
|
|
|
|
int64_t pos = mDecoder->GetResource()->Tell();
|
|
|
|
|
|
|
|
VideoData *v;
|
|
|
|
if (!frame.mGraphicBuffer) {
|
|
|
|
|
|
|
|
VideoData::YCbCrBuffer b;
|
|
|
|
b.mPlanes[0].mData = static_cast<uint8_t *>(frame.Y.mData);
|
|
|
|
b.mPlanes[0].mStride = frame.Y.mStride;
|
|
|
|
b.mPlanes[0].mHeight = frame.Y.mHeight;
|
|
|
|
b.mPlanes[0].mWidth = frame.Y.mWidth;
|
|
|
|
b.mPlanes[0].mOffset = frame.Y.mOffset;
|
|
|
|
b.mPlanes[0].mSkip = frame.Y.mSkip;
|
|
|
|
|
|
|
|
b.mPlanes[1].mData = static_cast<uint8_t *>(frame.Cb.mData);
|
|
|
|
b.mPlanes[1].mStride = frame.Cb.mStride;
|
|
|
|
b.mPlanes[1].mHeight = frame.Cb.mHeight;
|
|
|
|
b.mPlanes[1].mWidth = frame.Cb.mWidth;
|
|
|
|
b.mPlanes[1].mOffset = frame.Cb.mOffset;
|
|
|
|
b.mPlanes[1].mSkip = frame.Cb.mSkip;
|
|
|
|
|
|
|
|
b.mPlanes[2].mData = static_cast<uint8_t *>(frame.Cr.mData);
|
|
|
|
b.mPlanes[2].mStride = frame.Cr.mStride;
|
|
|
|
b.mPlanes[2].mHeight = frame.Cr.mHeight;
|
|
|
|
b.mPlanes[2].mWidth = frame.Cr.mWidth;
|
|
|
|
b.mPlanes[2].mOffset = frame.Cr.mOffset;
|
|
|
|
b.mPlanes[2].mSkip = frame.Cr.mSkip;
|
|
|
|
|
|
|
|
v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
|
|
|
pos,
|
|
|
|
frame.mTimeUs,
|
|
|
|
frame.mTimeUs+1, // We don't know the end time.
|
|
|
|
b,
|
|
|
|
frame.mKeyFrame,
|
|
|
|
-1,
|
|
|
|
picture);
|
|
|
|
} else {
|
|
|
|
v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
|
|
|
pos,
|
|
|
|
frame.mTimeUs,
|
|
|
|
frame.mTimeUs+1, // We don't know the end time.
|
|
|
|
frame.mGraphicBuffer,
|
|
|
|
frame.mKeyFrame,
|
|
|
|
-1,
|
|
|
|
picture);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
NS_WARNING("Unable to create VideoData");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded++;
|
|
|
|
NS_ASSERTION(decoded <= parsed, "Expect to decode fewer frames than parsed in MediaPlugin...");
|
|
|
|
|
2013-04-08 18:35:30 +00:00
|
|
|
mVideoQueue.Push(v);
|
2012-09-27 04:33:43 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-24 12:38:32 +00:00
|
|
|
void MediaOmxReader::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset)
|
|
|
|
{
|
|
|
|
android::OmxDecoder *omxDecoder = mOmxDecoder.get();
|
|
|
|
|
|
|
|
if (omxDecoder) {
|
|
|
|
omxDecoder->NotifyDataArrived(aBuffer, aLength, aOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaOmxReader::DecodeAudioData()
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
// This is the approximate byte position in the stream.
|
|
|
|
int64_t pos = mDecoder->GetResource()->Tell();
|
|
|
|
|
|
|
|
// Read next frame
|
|
|
|
MPAPI::AudioFrame frame;
|
|
|
|
if (!mOmxDecoder->ReadAudio(&frame, mAudioSeekTimeUs)) {
|
|
|
|
mAudioQueue.Finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mAudioSeekTimeUs = -1;
|
|
|
|
|
|
|
|
// Ignore empty buffer which stagefright media read will sporadically return
|
|
|
|
if (frame.mSize == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frame.mSize/2] );
|
|
|
|
memcpy(buffer.get(), frame.mData, frame.mSize);
|
|
|
|
|
|
|
|
uint32_t frames = frame.mSize / (2 * frame.mAudioChannels);
|
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, frame.mAudioSampleRate);
|
|
|
|
if (!duration.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioQueue.Push(new AudioData(pos,
|
|
|
|
frame.mTimeUs,
|
|
|
|
duration.value(),
|
|
|
|
frames,
|
|
|
|
buffer.forget(),
|
|
|
|
frame.mAudioChannels));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaOmxReader::Seek(int64_t aTarget, int64_t aStartTime, int64_t aEndTime, int64_t aCurrentTime)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
mVideoQueue.Reset();
|
|
|
|
mAudioQueue.Reset();
|
|
|
|
|
|
|
|
mAudioSeekTimeUs = mVideoSeekTimeUs = aTarget;
|
|
|
|
|
|
|
|
return DecodeToTarget(aTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t BytesToTime(int64_t offset, uint64_t length, uint64_t durationUs) {
|
|
|
|
double perc = double(offset) / double(length);
|
|
|
|
if (perc > 1.0)
|
|
|
|
perc = 1.0;
|
|
|
|
return uint64_t(double(durationUs) * perc);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
nsresult MediaOmxReader::GetBuffered(mozilla::dom::TimeRanges* aBuffered, int64_t aStartTime)
|
2012-09-27 04:33:43 +00:00
|
|
|
{
|
2013-02-16 02:27:18 +00:00
|
|
|
if (!mOmxDecoder.get())
|
2012-09-27 04:33:43 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
MediaResource* stream = mOmxDecoder->GetResource();
|
|
|
|
|
|
|
|
int64_t durationUs = 0;
|
|
|
|
mOmxDecoder->GetDuration(&durationUs);
|
|
|
|
|
|
|
|
// Nothing to cache if the media takes 0us to play.
|
|
|
|
if (!durationUs)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Special case completely cached files. This also handles local files.
|
|
|
|
if (stream->IsDataCachedToEndOfResource(0)) {
|
|
|
|
aBuffered->Add(0, durationUs);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t totalBytes = stream->GetLength();
|
|
|
|
|
|
|
|
// If we can't determine the total size, pretend that we have nothing
|
|
|
|
// buffered. This will put us in a state of eternally-low-on-undecoded-data
|
|
|
|
// which is not get, but about the best we can do.
|
|
|
|
if (totalBytes == -1)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
int64_t startOffset = stream->GetNextCachedData(0);
|
|
|
|
while (startOffset >= 0) {
|
|
|
|
int64_t endOffset = stream->GetCachedDataEnd(startOffset);
|
|
|
|
// Bytes [startOffset..endOffset] are cached.
|
|
|
|
NS_ASSERTION(startOffset >= 0, "Integer underflow in GetBuffered");
|
|
|
|
NS_ASSERTION(endOffset >= 0, "Integer underflow in GetBuffered");
|
|
|
|
|
|
|
|
uint64_t startUs = BytesToTime(startOffset, totalBytes, durationUs);
|
|
|
|
uint64_t endUs = BytesToTime(endOffset, totalBytes, durationUs);
|
|
|
|
if (startUs != endUs) {
|
|
|
|
aBuffered->Add((double)startUs / USECS_PER_S, (double)endUs / USECS_PER_S);
|
|
|
|
}
|
|
|
|
startOffset = stream->GetNextCachedData(endOffset);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-14 19:45:33 +00:00
|
|
|
|
2013-04-11 00:58:25 +00:00
|
|
|
void MediaOmxReader::OnDecodeThreadFinish() {
|
|
|
|
if (mOmxDecoder.get()) {
|
|
|
|
mOmxDecoder->Pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaOmxReader::OnDecodeThreadStart() {
|
|
|
|
if (mOmxDecoder.get()) {
|
2013-05-02 22:59:18 +00:00
|
|
|
DebugOnly<nsresult> result = mOmxDecoder->Play();
|
2013-04-11 00:58:25 +00:00
|
|
|
NS_ASSERTION(result == NS_OK, "OmxDecoder should be in play state to continue decoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|