2012-06-01 00:54:23 +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 "MediaPluginReader.h"
|
2012-06-01 00:54:23 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "nsTimeRanges.h"
|
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "VideoUtils.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaPluginDecoder.h"
|
|
|
|
#include "MediaPluginHost.h"
|
|
|
|
#include "MediaDecoderStateMachine.h"
|
2012-11-19 15:11:21 +00:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2012-06-01 00:54:23 +00:00
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
2012-06-01 00:54:23 +00:00
|
|
|
|
2012-11-26 21:12:24 +00:00
|
|
|
MediaPluginReader::MediaPluginReader(AbstractMediaDecoder *aDecoder,
|
|
|
|
const nsACString& aContentType) :
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoderReader(aDecoder),
|
2012-11-26 21:12:24 +00:00
|
|
|
mType(aContentType),
|
2012-06-01 00:54:23 +00:00
|
|
|
mPlugin(NULL),
|
2012-09-19 00:34:36 +00:00
|
|
|
mHasAudio(false),
|
|
|
|
mHasVideo(false),
|
2012-06-01 00:54:23 +00:00
|
|
|
mVideoSeekTimeUs(-1),
|
|
|
|
mAudioSeekTimeUs(-1),
|
|
|
|
mLastVideoFrame(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaPluginReader::~MediaPluginReader()
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
ResetDecode();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaPluginReader::Init(MediaDecoderReader* aCloneDonor)
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:40:07 +00:00
|
|
|
nsresult MediaPluginReader::ReadMetadata(VideoInfo* aInfo,
|
2012-11-09 00:40:08 +00:00
|
|
|
MetadataTags** aTags)
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
if (!mPlugin) {
|
|
|
|
mPlugin = GetMediaPluginHost()->CreateDecoder(mDecoder->GetResource(), mType);
|
|
|
|
if (!mPlugin) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the total duration (the max of the audio and video track).
|
|
|
|
int64_t durationUs;
|
|
|
|
mPlugin->GetDuration(mPlugin, &durationUs);
|
|
|
|
if (durationUs) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-19 15:11:21 +00:00
|
|
|
mDecoder->SetMediaDuration(durationUs);
|
2012-06-01 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPlugin->HasVideo(mPlugin)) {
|
|
|
|
int32_t width, height;
|
|
|
|
mPlugin->GetVideoParameters(mPlugin, &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-06-01 00:54:23 +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),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2012-06-01 00:54:23 +00:00
|
|
|
mozilla::TimeStamp::Now());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPlugin->HasAudio(mPlugin)) {
|
|
|
|
int32_t numChannels, sampleRate;
|
|
|
|
mPlugin->GetAudioParameters(mPlugin, &numChannels, &sampleRate);
|
|
|
|
mHasAudio = mInfo.mHasAudio = true;
|
|
|
|
mInfo.mAudioChannels = numChannels;
|
|
|
|
mInfo.mAudioRate = sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aInfo = mInfo;
|
2012-07-31 00:14:29 +00:00
|
|
|
*aTags = nullptr;
|
2012-06-01 00:54:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resets all state related to decoding, emptying all buffers etc.
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaPluginReader::ResetDecode()
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
if (mLastVideoFrame) {
|
|
|
|
delete mLastVideoFrame;
|
|
|
|
mLastVideoFrame = NULL;
|
|
|
|
}
|
|
|
|
if (mPlugin) {
|
|
|
|
GetMediaPluginHost()->DestroyDecoder(mPlugin);
|
|
|
|
mPlugin = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaPluginReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aTimeThreshold)
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
// Record number of frames decoded and parsed. Automatically update the
|
|
|
|
// stats counters using the AutoNotifyDecoded stack-based class.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t parsed = 0, decoded = 0;
|
2012-11-19 15:11:21 +00:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);
|
2012-06-01 00:54:23 +00:00
|
|
|
|
|
|
|
// Throw away the currently buffered frame if we are seeking.
|
|
|
|
if (mLastVideoFrame && mVideoSeekTimeUs != -1) {
|
|
|
|
delete mLastVideoFrame;
|
|
|
|
mLastVideoFrame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read next frame
|
|
|
|
while (true) {
|
|
|
|
MPAPI::VideoFrame frame;
|
|
|
|
if (!mPlugin->ReadVideo(mPlugin, &frame, mVideoSeekTimeUs)) {
|
|
|
|
// We reached the end of the video stream. If we have a buffered
|
|
|
|
// video frame, push it the video queue using the total duration
|
|
|
|
// of the video as the end time.
|
|
|
|
if (mLastVideoFrame) {
|
|
|
|
int64_t durationUs;
|
|
|
|
mPlugin->GetDuration(mPlugin, &durationUs);
|
|
|
|
mLastVideoFrame->mEndTime = (durationUs > mLastVideoFrame->mTime)
|
|
|
|
? durationUs
|
|
|
|
: mLastVideoFrame->mTime;
|
|
|
|
mVideoQueue.Push(mLastVideoFrame);
|
|
|
|
mLastVideoFrame = NULL;
|
|
|
|
}
|
2012-06-01 00:54:52 +00:00
|
|
|
mVideoQueue.Finish();
|
2012-06-01 00:54:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mVideoSeekTimeUs = -1;
|
|
|
|
|
|
|
|
if (aKeyframeSkip) {
|
2012-08-10 08:15:20 +00:00
|
|
|
// Disable keyframe skipping for now as
|
|
|
|
// stagefright doesn't seem to be telling us
|
|
|
|
// when a frame is a keyframe.
|
|
|
|
#if 0
|
2012-06-01 00:54:23 +00:00
|
|
|
if (!frame.mKeyFrame) {
|
|
|
|
++parsed;
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-10 08:15:20 +00:00
|
|
|
#endif
|
2012-06-01 00:54:23 +00:00
|
|
|
aKeyframeSkip = false;
|
|
|
|
}
|
|
|
|
|
2012-11-30 02:37:00 +00:00
|
|
|
if (frame.mSize == 0)
|
|
|
|
return true;
|
|
|
|
|
2012-06-01 00:54:23 +00:00
|
|
|
VideoData::YCbCrBuffer b;
|
2012-08-22 15:56:38 +00:00
|
|
|
b.mPlanes[0].mData = static_cast<uint8_t *>(frame.Y.mData);
|
2012-06-01 00:54:23 +00:00
|
|
|
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;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
b.mPlanes[1].mData = static_cast<uint8_t *>(frame.Cb.mData);
|
2012-06-01 00:54:23 +00:00
|
|
|
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;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
b.mPlanes[2].mData = static_cast<uint8_t *>(frame.Cr.mData);
|
2012-06-01 00:54:23 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t pos = mDecoder->GetResource()->Tell();
|
2012-06-01 00:54:23 +00:00
|
|
|
|
|
|
|
VideoData *v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
|
|
|
pos,
|
|
|
|
frame.mTimeUs,
|
|
|
|
frame.mTimeUs+1, // We don't know the end time.
|
|
|
|
b,
|
|
|
|
frame.mKeyFrame,
|
|
|
|
-1,
|
|
|
|
picture);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
parsed++;
|
|
|
|
decoded++;
|
|
|
|
NS_ASSERTION(decoded <= parsed, "Expect to decode fewer frames than parsed in MediaPlugin...");
|
|
|
|
|
|
|
|
// Since MPAPI doesn't give us the end time of frames, we keep one frame
|
2012-11-14 19:46:40 +00:00
|
|
|
// buffered in MediaPluginReader and push it into the queue as soon
|
2012-06-01 00:54:23 +00:00
|
|
|
// we read the following frame so we can use that frame's start time as
|
|
|
|
// the end time of the buffered frame.
|
|
|
|
if (!mLastVideoFrame) {
|
|
|
|
mLastVideoFrame = v;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastVideoFrame->mEndTime = v->mTime;
|
|
|
|
|
|
|
|
// We have the start time of the next frame, so we can push the previous
|
|
|
|
// frame into the queue, except if the end time is below the threshold,
|
|
|
|
// in which case it wouldn't be displayed anyway.
|
|
|
|
if (mLastVideoFrame->mEndTime < aTimeThreshold) {
|
|
|
|
delete mLastVideoFrame;
|
|
|
|
mLastVideoFrame = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoQueue.Push(mLastVideoFrame);
|
|
|
|
|
|
|
|
// Buffer the current frame we just decoded.
|
|
|
|
mLastVideoFrame = v;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaPluginReader::DecodeAudioData()
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
// This is the approximate byte position in the stream.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t pos = mDecoder->GetResource()->Tell();
|
2012-06-01 00:54:23 +00:00
|
|
|
|
|
|
|
// Read next frame
|
|
|
|
MPAPI::AudioFrame frame;
|
|
|
|
if (!mPlugin->ReadAudio(mPlugin, &frame, mAudioSeekTimeUs)) {
|
2012-06-01 00:54:52 +00:00
|
|
|
mAudioQueue.Finish();
|
2012-06-01 00:54:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mAudioSeekTimeUs = -1;
|
|
|
|
|
2012-08-21 05:03:21 +00:00
|
|
|
// Ignore empty buffers which stagefright media read will sporadically return
|
|
|
|
if (frame.mSize == 0)
|
|
|
|
return true;
|
|
|
|
|
2012-06-01 00:54:23 +00:00
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frame.mSize/2] );
|
|
|
|
memcpy(buffer.get(), frame.mData, frame.mSize);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t frames = frame.mSize / (2 * frame.mAudioChannels);
|
2012-06-01 00:54:23 +00:00
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, frame.mAudioSampleRate);
|
|
|
|
if (!duration.isValid()) {
|
2012-08-21 05:03:21 +00:00
|
|
|
return false;
|
2012-06-01 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 MediaPluginReader::Seek(int64_t aTarget, int64_t aStartTime, int64_t aEndTime, int64_t aCurrentTime)
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
mVideoQueue.Erase();
|
|
|
|
mAudioQueue.Erase();
|
|
|
|
|
|
|
|
mAudioSeekTimeUs = mVideoSeekTimeUs = aTarget;
|
|
|
|
|
|
|
|
return DecodeToTarget(aTarget);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaPluginReader::GetBuffered(nsTimeRanges* aBuffered, int64_t aStartTime)
|
2012-06-01 00:54:23 +00:00
|
|
|
{
|
|
|
|
if (!mPlugin)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
MediaResource* stream = mDecoder->GetResource();
|
|
|
|
|
|
|
|
int64_t durationUs = 0;
|
|
|
|
mPlugin->GetDuration(mPlugin, &durationUs);
|
|
|
|
|
2012-09-02 22:56:29 +00:00
|
|
|
GetEstimatedBufferedTimeRanges(stream, durationUs, aBuffered);
|
|
|
|
|
2012-06-01 00:54:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-14 19:45:33 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|