2010-04-02 03:03:07 +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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: ML 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (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.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
* Chris Pearce <chris@pearce.org.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2010-05-06 02:31:02 +00:00
|
|
|
#if !defined(nsBuiltinDecoderReader_h_)
|
|
|
|
#define nsBuiltinDecoderReader_h_
|
2010-04-02 03:03:07 +00:00
|
|
|
|
|
|
|
#include <nsDeque.h>
|
2010-05-19 03:04:33 +00:00
|
|
|
#include "ImageLayers.h"
|
2010-04-02 03:03:07 +00:00
|
|
|
#include "nsSize.h"
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Stores info relevant to presenting media frames.
|
2010-05-19 03:04:33 +00:00
|
|
|
class nsVideoInfo {
|
|
|
|
public:
|
|
|
|
nsVideoInfo()
|
2011-06-23 22:08:54 +00:00
|
|
|
: mAudioRate(0),
|
2010-05-19 03:04:33 +00:00
|
|
|
mAudioChannels(0),
|
2011-01-28 06:36:03 +00:00
|
|
|
mDisplay(0,0),
|
2010-11-02 23:43:29 +00:00
|
|
|
mStereoMode(mozilla::layers::STEREO_MODE_MONO),
|
2011-09-29 23:34:37 +00:00
|
|
|
mHasAudio(false),
|
|
|
|
mHasVideo(false)
|
2010-05-19 03:04:33 +00:00
|
|
|
{}
|
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// Returns true if it's safe to use aPicture as the picture to be
|
2011-01-28 06:36:03 +00:00
|
|
|
// extracted inside a frame of size aFrame, and scaled up to and displayed
|
|
|
|
// at a size of aDisplay. You should validate the frame, picture, and
|
2011-06-23 22:08:54 +00:00
|
|
|
// display regions before using them to display video frames.
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool ValidateVideoRegion(const nsIntSize& aFrame,
|
2011-01-28 06:36:03 +00:00
|
|
|
const nsIntRect& aPicture,
|
|
|
|
const nsIntSize& aDisplay);
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Sample rate.
|
2010-05-19 03:04:33 +00:00
|
|
|
PRUint32 mAudioRate;
|
|
|
|
|
|
|
|
// Number of audio channels.
|
|
|
|
PRUint32 mAudioChannels;
|
|
|
|
|
2011-06-23 22:08:54 +00:00
|
|
|
// Size in pixels at which the video is rendered. This is after it has
|
|
|
|
// been scaled by its aspect ratio.
|
2011-01-28 06:36:03 +00:00
|
|
|
nsIntSize mDisplay;
|
|
|
|
|
2010-11-02 23:43:29 +00:00
|
|
|
// Indicates the frame layout for single track stereo videos.
|
|
|
|
mozilla::layers::StereoMode mStereoMode;
|
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True if we have an active audio bitstream.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHasAudio;
|
2010-05-19 03:04:33 +00:00
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True if we have an active video bitstream.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHasVideo;
|
2010-05-19 03:04:33 +00:00
|
|
|
};
|
|
|
|
|
2010-10-19 02:55:45 +00:00
|
|
|
#ifdef MOZ_TREMOR
|
|
|
|
#include <ogg/os_types.h>
|
|
|
|
typedef ogg_int32_t VorbisPCMValue;
|
2011-08-16 05:19:51 +00:00
|
|
|
typedef short AudioDataValue;
|
2010-10-19 02:55:45 +00:00
|
|
|
|
2011-08-16 05:19:51 +00:00
|
|
|
#define MOZ_AUDIO_DATA_FORMAT (nsAudioStream::FORMAT_S16_LE)
|
2010-10-19 02:55:45 +00:00
|
|
|
#define MOZ_CLIP_TO_15(x) ((x)<-32768?-32768:(x)<=32767?(x):32767)
|
2011-08-16 05:19:51 +00:00
|
|
|
// Convert the output of vorbis_synthesis_pcmout to a AudioDataValue
|
2010-10-19 02:55:45 +00:00
|
|
|
#define MOZ_CONVERT_VORBIS_SAMPLE(x) \
|
2011-08-16 05:19:51 +00:00
|
|
|
(static_cast<AudioDataValue>(MOZ_CLIP_TO_15((x)>>9)))
|
|
|
|
// Convert a AudioDataValue to a float for the Audio API
|
|
|
|
#define MOZ_CONVERT_AUDIO_SAMPLE(x) ((x)*(1.F/32768))
|
2011-03-30 05:37:42 +00:00
|
|
|
#define MOZ_SAMPLE_TYPE_S16LE 1
|
2010-10-19 02:55:45 +00:00
|
|
|
|
|
|
|
#else /*MOZ_VORBIS*/
|
|
|
|
|
|
|
|
typedef float VorbisPCMValue;
|
2011-08-16 05:19:51 +00:00
|
|
|
typedef float AudioDataValue;
|
2010-10-19 02:55:45 +00:00
|
|
|
|
2011-08-16 05:19:51 +00:00
|
|
|
#define MOZ_AUDIO_DATA_FORMAT (nsAudioStream::FORMAT_FLOAT32)
|
2010-10-19 02:55:45 +00:00
|
|
|
#define MOZ_CONVERT_VORBIS_SAMPLE(x) (x)
|
2011-08-16 05:19:51 +00:00
|
|
|
#define MOZ_CONVERT_AUDIO_SAMPLE(x) (x)
|
2011-03-30 05:37:42 +00:00
|
|
|
#define MOZ_SAMPLE_TYPE_FLOAT32 1
|
2010-10-19 02:55:45 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Holds chunk a decoded audio frames.
|
2011-08-16 05:19:51 +00:00
|
|
|
class AudioData {
|
2010-04-02 03:03:07 +00:00
|
|
|
public:
|
2011-08-16 05:19:51 +00:00
|
|
|
AudioData(PRInt64 aOffset,
|
2010-04-27 08:53:45 +00:00
|
|
|
PRInt64 aTime,
|
2010-04-02 03:03:07 +00:00
|
|
|
PRInt64 aDuration,
|
2011-09-27 03:31:18 +00:00
|
|
|
PRUint32 aFrames,
|
2011-08-16 05:19:51 +00:00
|
|
|
AudioDataValue* aData,
|
2010-04-02 03:03:07 +00:00
|
|
|
PRUint32 aChannels)
|
2010-04-27 08:53:45 +00:00
|
|
|
: mOffset(aOffset),
|
|
|
|
mTime(aTime),
|
2010-04-02 03:03:07 +00:00
|
|
|
mDuration(aDuration),
|
2011-09-27 03:31:18 +00:00
|
|
|
mFrames(aFrames),
|
2010-04-02 23:47:15 +00:00
|
|
|
mChannels(aChannels),
|
|
|
|
mAudioData(aData)
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
2011-08-16 05:19:51 +00:00
|
|
|
MOZ_COUNT_CTOR(AudioData);
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 05:19:51 +00:00
|
|
|
~AudioData()
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
2011-08-16 05:19:51 +00:00
|
|
|
MOZ_COUNT_DTOR(AudioData);
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Approximate byte offset of the end of the page on which this chunk
|
|
|
|
// ends.
|
2010-04-27 08:53:45 +00:00
|
|
|
const PRInt64 mOffset;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 mTime; // Start time of data in usecs.
|
2011-04-13 22:12:23 +00:00
|
|
|
const PRInt64 mDuration; // In usecs.
|
2011-09-27 03:31:18 +00:00
|
|
|
const PRUint32 mFrames;
|
2010-04-02 03:03:07 +00:00
|
|
|
const PRUint32 mChannels;
|
2011-08-16 05:19:51 +00:00
|
|
|
nsAutoArrayPtr<AudioDataValue> mAudioData;
|
2010-04-02 03:03:07 +00:00
|
|
|
};
|
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// Holds a decoded video frame, in YCbCr format. These are queued in the reader.
|
2010-04-02 03:03:07 +00:00
|
|
|
class VideoData {
|
|
|
|
public:
|
2010-05-19 03:04:33 +00:00
|
|
|
typedef mozilla::layers::ImageContainer ImageContainer;
|
|
|
|
typedef mozilla::layers::Image Image;
|
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// YCbCr data obtained from decoding the video. The index's are:
|
|
|
|
// 0 = Y
|
|
|
|
// 1 = Cb
|
|
|
|
// 2 = Cr
|
|
|
|
struct YCbCrBuffer {
|
|
|
|
struct Plane {
|
|
|
|
PRUint8* mData;
|
|
|
|
PRUint32 mWidth;
|
|
|
|
PRUint32 mHeight;
|
|
|
|
PRUint32 mStride;
|
|
|
|
};
|
|
|
|
|
|
|
|
Plane mPlanes[3];
|
|
|
|
};
|
2010-04-02 03:03:07 +00:00
|
|
|
|
|
|
|
// Constructs a VideoData object. Makes a copy of YCbCr data in aBuffer.
|
2010-05-19 03:04:33 +00:00
|
|
|
// aTimecode is a codec specific number representing the timestamp of
|
|
|
|
// the frame of video data. Returns nsnull if an error occurs. This may
|
|
|
|
// indicate that memory couldn't be allocated to create the VideoData
|
|
|
|
// object, or it may indicate some problem with the input data (e.g.
|
|
|
|
// negative stride).
|
|
|
|
static VideoData* Create(nsVideoInfo& aInfo,
|
|
|
|
ImageContainer* aContainer,
|
|
|
|
PRInt64 aOffset,
|
2010-04-27 08:53:45 +00:00
|
|
|
PRInt64 aTime,
|
2010-05-31 04:02:00 +00:00
|
|
|
PRInt64 aEndTime,
|
2010-05-06 02:31:02 +00:00
|
|
|
const YCbCrBuffer &aBuffer,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aKeyframe,
|
2011-06-23 22:08:54 +00:00
|
|
|
PRInt64 aTimecode,
|
|
|
|
nsIntRect aPicture);
|
2010-04-02 03:03:07 +00:00
|
|
|
|
|
|
|
// Constructs a duplicate VideoData object. This intrinsically tells the
|
|
|
|
// player that it does not need to update the displayed frame when this
|
|
|
|
// frame is played; this frame is identical to the previous.
|
2010-04-27 08:53:45 +00:00
|
|
|
static VideoData* CreateDuplicate(PRInt64 aOffset,
|
|
|
|
PRInt64 aTime,
|
2010-05-31 04:02:00 +00:00
|
|
|
PRInt64 aEndTime,
|
2010-05-06 02:31:02 +00:00
|
|
|
PRInt64 aTimecode)
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
2010-05-31 04:02:00 +00:00
|
|
|
return new VideoData(aOffset, aTime, aEndTime, aTimecode);
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~VideoData()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(VideoData);
|
|
|
|
}
|
|
|
|
|
2011-06-23 22:08:54 +00:00
|
|
|
// Dimensions at which to display the video frame. The picture region
|
|
|
|
// will be scaled to this size. This is should be the picture region's
|
|
|
|
// dimensions scaled with respect to its aspect ratio.
|
|
|
|
nsIntSize mDisplay;
|
|
|
|
|
2010-04-27 08:53:45 +00:00
|
|
|
// Approximate byte offset of the end of the frame in the media.
|
|
|
|
PRInt64 mOffset;
|
|
|
|
|
2011-04-13 22:12:23 +00:00
|
|
|
// Start time of frame in microseconds.
|
2010-04-02 03:03:07 +00:00
|
|
|
PRInt64 mTime;
|
|
|
|
|
2011-06-23 22:08:54 +00:00
|
|
|
// End time of frame in microseconds.
|
2010-05-31 04:02:00 +00:00
|
|
|
PRInt64 mEndTime;
|
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// Codec specific internal time code. For Ogg based codecs this is the
|
|
|
|
// granulepos.
|
|
|
|
PRInt64 mTimecode;
|
|
|
|
|
2010-05-19 03:04:33 +00:00
|
|
|
// This frame's image.
|
|
|
|
nsRefPtr<Image> mImage;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// When true, denotes that this frame is identical to the frame that
|
2010-04-02 03:03:07 +00:00
|
|
|
// came before; it's a duplicate. mBuffer will be empty.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDuplicate;
|
|
|
|
bool mKeyframe;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
public:
|
2010-05-31 04:02:00 +00:00
|
|
|
VideoData(PRInt64 aOffset, PRInt64 aTime, PRInt64 aEndTime, PRInt64 aTimecode)
|
2010-04-27 08:53:45 +00:00
|
|
|
: mOffset(aOffset),
|
|
|
|
mTime(aTime),
|
2010-05-31 04:02:00 +00:00
|
|
|
mEndTime(aEndTime),
|
2010-05-06 02:31:02 +00:00
|
|
|
mTimecode(aTimecode),
|
2011-09-29 23:34:37 +00:00
|
|
|
mDuplicate(true),
|
|
|
|
mKeyframe(false)
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(VideoData);
|
2010-07-20 01:29:30 +00:00
|
|
|
NS_ASSERTION(aEndTime >= aTime, "Frame must start before it ends.");
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 08:53:45 +00:00
|
|
|
VideoData(PRInt64 aOffset,
|
|
|
|
PRInt64 aTime,
|
2010-05-31 04:02:00 +00:00
|
|
|
PRInt64 aEndTime,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aKeyframe,
|
2011-06-23 22:08:54 +00:00
|
|
|
PRInt64 aTimecode,
|
|
|
|
nsIntSize aDisplay)
|
2011-07-05 01:31:28 +00:00
|
|
|
: mDisplay(aDisplay),
|
|
|
|
mOffset(aOffset),
|
2010-04-27 08:53:45 +00:00
|
|
|
mTime(aTime),
|
2010-05-31 04:02:00 +00:00
|
|
|
mEndTime(aEndTime),
|
2010-05-06 02:31:02 +00:00
|
|
|
mTimecode(aTimecode),
|
2011-09-29 23:34:37 +00:00
|
|
|
mDuplicate(false),
|
2011-07-05 01:31:28 +00:00
|
|
|
mKeyframe(aKeyframe)
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(VideoData);
|
2010-07-20 01:29:30 +00:00
|
|
|
NS_ASSERTION(aEndTime >= aTime, "Frame must start before it ends.");
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Thread and type safe wrapper around nsDeque.
|
|
|
|
template <class T>
|
|
|
|
class MediaQueueDeallocator : public nsDequeFunctor {
|
|
|
|
virtual void* operator() (void* anObject) {
|
|
|
|
delete static_cast<T*>(anObject);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> class MediaQueue : private nsDeque {
|
|
|
|
public:
|
2011-04-29 19:21:57 +00:00
|
|
|
typedef mozilla::ReentrantMonitorAutoEnter ReentrantMonitorAutoEnter;
|
|
|
|
typedef mozilla::ReentrantMonitor ReentrantMonitor;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
MediaQueue()
|
|
|
|
: nsDeque(new MediaQueueDeallocator<T>()),
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor("mediaqueue"),
|
2010-04-02 03:03:07 +00:00
|
|
|
mEndOfStream(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~MediaQueue() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PRInt32 GetSize() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return nsDeque::GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Push(T* aItem) {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
nsDeque::Push(aItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void PushFront(T* aItem) {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
nsDeque::PushFront(aItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* Pop() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return static_cast<T*>(nsDeque::Pop());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* PopFront() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return static_cast<T*>(nsDeque::PopFront());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* Peek() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return static_cast<T*>(nsDeque::Peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* PeekFront() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return static_cast<T*>(nsDeque::PeekFront());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Empty() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
nsDeque::Empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Erase() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
nsDeque::Erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
while (GetSize() > 0) {
|
|
|
|
T* x = PopFront();
|
|
|
|
delete x;
|
|
|
|
}
|
2011-09-29 23:34:37 +00:00
|
|
|
mEndOfStream = false;
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool AtEndOfStream() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-09-27 03:31:18 +00:00
|
|
|
return GetSize() == 0 && mEndOfStream;
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// Returns true if the media queue has had it last item added to it.
|
2010-09-14 23:24:47 +00:00
|
|
|
// This happens when the media stream has been completely decoded. Note this
|
|
|
|
// does not mean that the corresponding stream has finished playback.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsFinished() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-09-27 03:31:18 +00:00
|
|
|
return mEndOfStream;
|
2010-09-14 23:24:47 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Informs the media queue that it won't be receiving any more items.
|
2010-04-02 03:03:07 +00:00
|
|
|
void Finish() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-09-29 23:34:37 +00:00
|
|
|
mEndOfStream = true;
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Returns the approximate number of microseconds of items in the queue.
|
2010-04-02 03:03:07 +00:00
|
|
|
PRInt64 Duration() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
if (GetSize() < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
T* last = Peek();
|
|
|
|
T* first = PeekFront();
|
|
|
|
return last->mTime - first->mTime;
|
|
|
|
}
|
|
|
|
|
2011-07-22 03:17:23 +00:00
|
|
|
void LockedForEach(nsDequeFunctor& aFunctor) const {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
ForEach(aFunctor);
|
|
|
|
}
|
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
private:
|
2011-07-22 03:17:23 +00:00
|
|
|
mutable ReentrantMonitor mReentrantMonitor;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True when we've decoded the last frame of data in the
|
2011-09-27 03:31:18 +00:00
|
|
|
// bitstream for which we're queueing frame data.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mEndOfStream;
|
2010-04-02 03:03:07 +00:00
|
|
|
};
|
|
|
|
|
2011-07-12 03:39:28 +00:00
|
|
|
// Encapsulates the decoding and reading of media data. Reading can only be
|
2011-10-20 08:08:59 +00:00
|
|
|
// done on the decode thread. Never hold the decoder monitor when
|
2011-07-12 03:39:28 +00:00
|
|
|
// calling into this class. Unless otherwise specified, methods and fields of
|
|
|
|
// this class can only be accessed on the decode thread.
|
2010-05-06 02:31:02 +00:00
|
|
|
class nsBuiltinDecoderReader : public nsRunnable {
|
2010-04-02 03:03:07 +00:00
|
|
|
public:
|
2011-04-29 19:21:57 +00:00
|
|
|
typedef mozilla::ReentrantMonitor ReentrantMonitor;
|
|
|
|
typedef mozilla::ReentrantMonitorAutoEnter ReentrantMonitorAutoEnter;
|
2012-02-15 04:35:01 +00:00
|
|
|
typedef mozilla::VideoFrameContainer VideoFrameContainer;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
nsBuiltinDecoderReader(nsBuiltinDecoder* aDecoder);
|
2012-04-18 22:33:13 +00:00
|
|
|
virtual ~nsBuiltinDecoderReader();
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// Initializes the reader, returns NS_OK on success, or NS_ERROR_FAILURE
|
|
|
|
// on failure.
|
2010-09-21 00:49:50 +00:00
|
|
|
virtual nsresult Init(nsBuiltinDecoderReader* aCloneDonor) = 0;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
|
|
|
// Resets all state related to decoding, emptying all buffers etc.
|
|
|
|
virtual nsresult ResetDecode();
|
|
|
|
|
|
|
|
// Decodes an unspecified amount of audio data, enqueuing the audio data
|
2011-09-29 23:34:37 +00:00
|
|
|
// in mAudioQueue. Returns true when there's more audio to decode,
|
|
|
|
// false if the audio is finished, end of file has been reached,
|
2010-05-06 02:31:02 +00:00
|
|
|
// or an un-recoverable read error has occured.
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool DecodeAudioData() = 0;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
|
|
|
// Reads and decodes one video frame. Packets with a timestamp less
|
|
|
|
// than aTimeThreshold will be decoded (unless they're not keyframes
|
2011-09-29 23:34:37 +00:00
|
|
|
// and aKeyframeSkip is true), but will not be added to the queue.
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool DecodeVideoFrame(bool &aKeyframeSkip,
|
2010-05-06 02:31:02 +00:00
|
|
|
PRInt64 aTimeThreshold) = 0;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool HasAudio() = 0;
|
|
|
|
virtual bool HasVideo() = 0;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
2010-05-19 03:04:33 +00:00
|
|
|
// Read header data for all bitstreams in the file. Fills mInfo with
|
2010-04-02 03:03:07 +00:00
|
|
|
// the data required to present the media. Returns NS_OK on success,
|
|
|
|
// or NS_ERROR_FAILURE on failure.
|
2011-03-24 03:53:03 +00:00
|
|
|
virtual nsresult ReadMetadata(nsVideoInfo* aInfo) = 0;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Stores the presentation time of the first frame we'd be able to play if
|
|
|
|
// we started playback at the current position. Returns the first video
|
|
|
|
// frame, if we have video.
|
2011-05-08 21:10:28 +00:00
|
|
|
VideoData* FindStartTime(PRInt64& aOutStartTime);
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-04-13 22:12:23 +00:00
|
|
|
// Moves the decode head to aTime microseconds. aStartTime and aEndTime
|
|
|
|
// denote the start and end times of the media in usecs, and aCurrentTime
|
|
|
|
// is the current playback position in microseconds.
|
2010-08-19 22:50:37 +00:00
|
|
|
virtual nsresult Seek(PRInt64 aTime,
|
|
|
|
PRInt64 aStartTime,
|
|
|
|
PRInt64 aEndTime,
|
|
|
|
PRInt64 aCurrentTime) = 0;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Queue of audio frames. This queue is threadsafe, and is accessed from
|
2011-07-12 03:39:28 +00:00
|
|
|
// the audio, decoder, state machine, and main threads.
|
2011-08-16 05:19:51 +00:00
|
|
|
MediaQueue<AudioData> mAudioQueue;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Queue of video frames. This queue is threadsafe, and is accessed from
|
2011-07-12 03:39:28 +00:00
|
|
|
// the decoder, state machine, and main threads.
|
2010-04-02 03:03:07 +00:00
|
|
|
MediaQueue<VideoData> mVideoQueue;
|
|
|
|
|
2010-08-05 07:40:35 +00:00
|
|
|
// Populates aBuffered with the time ranges which are buffered. aStartTime
|
2011-09-27 03:31:18 +00:00
|
|
|
// must be the presentation time of the first frame in the media, e.g.
|
2010-08-05 07:40:35 +00:00
|
|
|
// the media time corresponding to playback time/position 0. This function
|
|
|
|
// should only be called on the main thread.
|
2010-08-25 08:43:00 +00:00
|
|
|
virtual nsresult GetBuffered(nsTimeRanges* aBuffered,
|
2010-08-05 07:40:35 +00:00
|
|
|
PRInt64 aStartTime) = 0;
|
2010-08-05 07:40:35 +00:00
|
|
|
|
2011-07-22 03:17:23 +00:00
|
|
|
class VideoQueueMemoryFunctor : public nsDequeFunctor {
|
|
|
|
public:
|
|
|
|
VideoQueueMemoryFunctor() : mResult(0) {}
|
|
|
|
|
|
|
|
virtual void* operator()(void* anObject) {
|
|
|
|
const VideoData* v = static_cast<const VideoData*>(anObject);
|
2011-08-02 03:21:52 +00:00
|
|
|
if (!v->mImage) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2011-07-22 03:17:23 +00:00
|
|
|
NS_ASSERTION(v->mImage->GetFormat() == mozilla::layers::Image::PLANAR_YCBCR,
|
|
|
|
"Wrong format?");
|
|
|
|
mozilla::layers::PlanarYCbCrImage* vi = static_cast<mozilla::layers::PlanarYCbCrImage*>(v->mImage.get());
|
|
|
|
|
|
|
|
mResult += vi->GetDataSize();
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
PRInt64 VideoQueueMemoryInUse() {
|
|
|
|
VideoQueueMemoryFunctor functor;
|
|
|
|
mVideoQueue.LockedForEach(functor);
|
|
|
|
return functor.mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AudioQueueMemoryFunctor : public nsDequeFunctor {
|
|
|
|
public:
|
|
|
|
AudioQueueMemoryFunctor() : mResult(0) {}
|
|
|
|
|
|
|
|
virtual void* operator()(void* anObject) {
|
2011-08-16 05:19:51 +00:00
|
|
|
const AudioData* audioData = static_cast<const AudioData*>(anObject);
|
2011-09-27 03:31:18 +00:00
|
|
|
mResult += audioData->mFrames * audioData->mChannels * sizeof(AudioDataValue);
|
2011-07-22 03:17:23 +00:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
PRInt64 AudioQueueMemoryInUse() {
|
|
|
|
AudioQueueMemoryFunctor functor;
|
|
|
|
mAudioQueue.LockedForEach(functor);
|
|
|
|
return functor.mResult;
|
|
|
|
}
|
|
|
|
|
2010-09-13 08:45:50 +00:00
|
|
|
// Only used by nsWebMReader for now, so stub here rather than in every
|
|
|
|
// reader than inherits from nsBuiltinDecoderReader.
|
2012-01-31 22:05:51 +00:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, PRUint32 aLength, PRInt64 aOffset) {}
|
2010-09-13 08:45:50 +00:00
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
protected:
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Pumps the decode until we reach frames required to play at time aTarget
|
|
|
|
// (usecs).
|
2010-08-13 02:28:15 +00:00
|
|
|
nsresult DecodeToTarget(PRInt64 aTarget);
|
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// Reader decode function. Matches DecodeVideoFrame() and
|
|
|
|
// DecodeAudioData().
|
2011-09-29 06:19:26 +00:00
|
|
|
typedef bool (nsBuiltinDecoderReader::*DecodeFn)();
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Calls aDecodeFn on *this until aQueue has an item, whereupon
|
|
|
|
// we return the first item.
|
2010-04-02 03:03:07 +00:00
|
|
|
template<class Data>
|
|
|
|
Data* DecodeToFirstData(DecodeFn aDecodeFn,
|
|
|
|
MediaQueue<Data>& aQueue);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
// Wrapper so that DecodeVideoFrame(bool&,PRInt64) can be called from
|
2010-04-02 03:03:07 +00:00
|
|
|
// DecodeToFirstData().
|
2011-09-29 06:19:26 +00:00
|
|
|
bool DecodeVideoFrame() {
|
|
|
|
bool f = false;
|
2010-05-06 02:31:02 +00:00
|
|
|
return DecodeVideoFrame(f, 0);
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 03:39:28 +00:00
|
|
|
// Reference to the owning decoder object.
|
2010-05-06 02:31:02 +00:00
|
|
|
nsBuiltinDecoder* mDecoder;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-07-12 03:39:28 +00:00
|
|
|
// Stores presentation info required for playback.
|
2010-05-19 03:04:33 +00:00
|
|
|
nsVideoInfo mInfo;
|
2010-04-02 03:03:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|