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: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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
|
|
|
#if !defined(MediaDecoderReader_h_)
|
|
|
|
#define MediaDecoderReader_h_
|
2010-04-02 03:03:07 +00:00
|
|
|
|
|
|
|
#include <nsDeque.h>
|
|
|
|
#include "nsSize.h"
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2012-04-30 03:12:42 +00:00
|
|
|
#include "SharedBuffer.h"
|
2012-10-25 10:09:40 +00:00
|
|
|
#include "AudioSampleFormat.h"
|
2013-09-05 20:25:17 +00:00
|
|
|
#include "AbstractMediaDecoder.h"
|
|
|
|
#include "ImageTypes.h"
|
|
|
|
|
|
|
|
struct nsIntRect;
|
2012-11-06 22:33:01 +00:00
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-09-05 20:25:17 +00:00
|
|
|
namespace layers {
|
|
|
|
class Image;
|
|
|
|
class ImageContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
class TimeRanges;
|
|
|
|
}
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Stores info relevant to presenting media frames.
|
2012-11-28 19:40:07 +00:00
|
|
|
class VideoInfo {
|
2010-05-19 03:04:33 +00:00
|
|
|
public:
|
2012-11-28 19:40:07 +00:00
|
|
|
VideoInfo()
|
2013-09-27 05:22:38 +00:00
|
|
|
: mDisplay(0,0),
|
2012-11-14 19:45:33 +00:00
|
|
|
mStereoMode(STEREO_MODE_MONO),
|
2011-09-29 23:34:37 +00:00
|
|
|
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,
|
2012-11-06 22:33:01 +00:00
|
|
|
const nsIntRect& aPicture,
|
|
|
|
const nsIntSize& aDisplay);
|
2011-01-28 06:36:03 +00:00
|
|
|
|
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.
|
2012-11-14 19:45:33 +00:00
|
|
|
StereoMode mStereoMode;
|
2010-11-02 23:43:29 +00:00
|
|
|
|
2013-09-27 05:22:38 +00:00
|
|
|
// True if we have an active video bitstream.
|
|
|
|
bool mHasVideo;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AudioInfo {
|
|
|
|
public:
|
|
|
|
AudioInfo()
|
|
|
|
: mRate(44100),
|
|
|
|
mChannels(2),
|
|
|
|
mHasAudio(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Sample rate.
|
|
|
|
uint32_t mRate;
|
|
|
|
|
|
|
|
// Number of audio channels.
|
|
|
|
uint32_t mChannels;
|
|
|
|
|
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;
|
2013-09-27 05:22:38 +00:00
|
|
|
};
|
2010-05-19 03:04:33 +00:00
|
|
|
|
2013-09-27 05:22:38 +00:00
|
|
|
class MediaInfo {
|
|
|
|
public:
|
|
|
|
bool HasVideo() const
|
|
|
|
{
|
|
|
|
return mVideo.mHasVideo;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasAudio() const
|
|
|
|
{
|
|
|
|
return mAudio.mHasAudio;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasValidMedia() const
|
|
|
|
{
|
|
|
|
return HasVideo() || HasAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoInfo mVideo;
|
|
|
|
AudioInfo mAudio;
|
2010-05-19 03:04:33 +00:00
|
|
|
};
|
|
|
|
|
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:
|
2012-04-30 03:12:42 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
AudioData(int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aDuration,
|
|
|
|
uint32_t aFrames,
|
2011-08-16 05:19:51 +00:00
|
|
|
AudioDataValue* aData,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t 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
|
|
|
}
|
|
|
|
|
2012-04-30 03:12:42 +00:00
|
|
|
// If mAudioBuffer is null, creates it from mAudioData.
|
|
|
|
void EnsureAudioBuffer();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t GetEnd() { return mTime + mDuration; }
|
2012-04-30 03:12:42 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
// Approximate byte offset of the end of the page on which this chunk
|
|
|
|
// ends.
|
2012-08-22 15:56:38 +00:00
|
|
|
const int64_t mOffset;
|
2010-04-27 08:53:45 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mTime; // Start time of data in usecs.
|
|
|
|
const int64_t mDuration; // In usecs.
|
|
|
|
const uint32_t mFrames;
|
|
|
|
const uint32_t mChannels;
|
2012-04-30 03:12:42 +00:00
|
|
|
// At least one of mAudioBuffer/mAudioData must be non-null.
|
|
|
|
// mChannels channels, each with mFrames frames
|
|
|
|
nsRefPtr<SharedBuffer> mAudioBuffer;
|
|
|
|
// mFrames frames, each with mChannels values
|
2011-08-16 05:19:51 +00:00
|
|
|
nsAutoArrayPtr<AudioDataValue> mAudioData;
|
2010-04-02 03:03:07 +00:00
|
|
|
};
|
|
|
|
|
2012-09-27 04:33:43 +00:00
|
|
|
namespace layers {
|
|
|
|
class GraphicBufferLocked;
|
|
|
|
}
|
|
|
|
|
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:
|
2012-11-14 19:45:33 +00:00
|
|
|
typedef layers::ImageContainer ImageContainer;
|
|
|
|
typedef layers::Image Image;
|
2010-05-19 03:04:33 +00:00
|
|
|
|
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 {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t* mData;
|
|
|
|
uint32_t mWidth;
|
|
|
|
uint32_t mHeight;
|
|
|
|
uint32_t mStride;
|
|
|
|
uint32_t mOffset;
|
|
|
|
uint32_t mSkip;
|
2010-05-06 02:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Plane mPlanes[3];
|
|
|
|
};
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
// Constructs a VideoData object. If aImage is NULL, creates a new Image
|
|
|
|
// holding a copy of the YCbCr data passed in aBuffer. If aImage is not NULL,
|
|
|
|
// it's stored as the underlying video image and aBuffer is assumed to point
|
|
|
|
// to memory within aImage so no copy is made. 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).
|
2012-11-28 19:40:07 +00:00
|
|
|
static VideoData* Create(VideoInfo& aInfo,
|
2010-05-19 03:04:33 +00:00
|
|
|
ImageContainer* aContainer,
|
2013-03-13 20:11:15 +00:00
|
|
|
Image* aImage,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
2010-05-06 02:31:02 +00:00
|
|
|
const YCbCrBuffer &aBuffer,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aKeyframe,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aTimecode,
|
2011-06-23 22:08:54 +00:00
|
|
|
nsIntRect aPicture);
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
// Variant that always makes a copy of aBuffer
|
2012-11-28 19:40:07 +00:00
|
|
|
static VideoData* Create(VideoInfo& aInfo,
|
2012-09-27 04:33:43 +00:00
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
2013-03-13 20:11:15 +00:00
|
|
|
const YCbCrBuffer &aBuffer,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture);
|
|
|
|
|
|
|
|
// Variant to create a VideoData instance given an existing aImage
|
|
|
|
static VideoData* Create(VideoInfo& aInfo,
|
|
|
|
Image* aImage,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
const YCbCrBuffer &aBuffer,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture);
|
|
|
|
|
|
|
|
static VideoData* Create(VideoInfo& aInfo,
|
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
layers::GraphicBufferLocked* aBuffer,
|
2012-09-27 04:33:43 +00:00
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture);
|
2012-12-12 22:53:58 +00:00
|
|
|
|
|
|
|
static VideoData* CreateFromImage(VideoInfo& aInfo,
|
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
const nsRefPtr<Image>& aImage,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture);
|
2012-09-27 04:33:43 +00:00
|
|
|
|
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.
|
2012-08-22 15:56:38 +00:00
|
|
|
static VideoData* CreateDuplicate(int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t 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
|
|
|
}
|
|
|
|
|
2012-08-21 04:06:46 +00:00
|
|
|
~VideoData();
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t GetEnd() { return mEndTime; }
|
2012-04-30 03:12:42 +00:00
|
|
|
|
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.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mOffset;
|
2010-04-27 08:53:45 +00:00
|
|
|
|
2011-04-13 22:12:23 +00:00
|
|
|
// Start time of frame in microseconds.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mTime;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-06-23 22:08:54 +00:00
|
|
|
// End time of frame in microseconds.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mEndTime;
|
2010-05-31 04:02:00 +00:00
|
|
|
|
2010-05-06 02:31:02 +00:00
|
|
|
// Codec specific internal time code. For Ogg based codecs this is the
|
|
|
|
// granulepos.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mTimecode;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
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:
|
2012-08-22 15:56:38 +00:00
|
|
|
VideoData(int64_t aOffset, int64_t aTime, int64_t aEndTime, int64_t aTimecode);
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
VideoData(int64_t aOffset,
|
|
|
|
int64_t aTime,
|
|
|
|
int64_t aEndTime,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aKeyframe,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aTimecode,
|
2012-08-21 04:06:46 +00:00
|
|
|
nsIntSize aDisplay);
|
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);
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> class MediaQueue : private nsDeque {
|
|
|
|
public:
|
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"),
|
2012-09-19 00:34:36 +00:00
|
|
|
mEndOfStream(false)
|
2010-04-02 03:03:07 +00:00
|
|
|
{}
|
2012-11-06 22:33:01 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
~MediaQueue() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:33:01 +00:00
|
|
|
inline int32_t GetSize() {
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-04-02 03:03:07 +00:00
|
|
|
return nsDeque::GetSize();
|
|
|
|
}
|
2012-06-01 00:54:23 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-06-01 00:54:23 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-06-01 00:54:23 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
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());
|
|
|
|
}
|
2012-11-06 22:33:01 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
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());
|
|
|
|
}
|
2012-11-06 22:33:01 +00:00
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-09-19 00:34:36 +00:00
|
|
|
// Returns true if the media queue has had its 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.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t 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);
|
|
|
|
}
|
|
|
|
|
2012-04-30 03:12:42 +00:00
|
|
|
// Extracts elements from the queue into aResult, in order.
|
|
|
|
// Elements whose start time is before aTime are ignored.
|
2012-08-22 15:56:38 +00:00
|
|
|
void GetElementsAfter(int64_t aTime, nsTArray<T*>* aResult) {
|
2012-04-30 03:12:42 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
if (!GetSize())
|
|
|
|
return;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i;
|
2012-04-30 03:12:42 +00:00
|
|
|
for (i = GetSize() - 1; i > 0; --i) {
|
|
|
|
T* v = static_cast<T*>(ObjectAt(i));
|
|
|
|
if (v->GetEnd() < aTime)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Elements less than i have a end time before aTime. It's also possible
|
|
|
|
// that the element at i has a end time before aTime, but that's OK.
|
|
|
|
for (; i < GetSize(); ++i) {
|
|
|
|
aResult->AppendElement(static_cast<T*>(ObjectAt(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:13:23 +00:00
|
|
|
uint32_t FrameCount() {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
uint32_t frames = 0;
|
|
|
|
for (int32_t i = 0; i < GetSize(); ++i) {
|
|
|
|
T* v = static_cast<T*>(ObjectAt(i));
|
|
|
|
frames += v->mFrames;
|
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
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.
|
2012-11-16 19:30:34 +00:00
|
|
|
class MediaDecoderReader {
|
2010-04-02 03:03:07 +00:00
|
|
|
public:
|
2012-11-19 15:11:21 +00:00
|
|
|
MediaDecoderReader(AbstractMediaDecoder* aDecoder);
|
2012-11-14 19:46:40 +00:00
|
|
|
virtual ~MediaDecoderReader();
|
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.
|
2012-11-14 19:46:40 +00:00
|
|
|
virtual nsresult Init(MediaDecoderReader* aCloneDonor) = 0;
|
2010-05-06 02:31:02 +00:00
|
|
|
|
2013-06-10 12:22:05 +00:00
|
|
|
// True if this reader is waiting media resource allocation
|
|
|
|
virtual bool IsWaitingMediaResources() { return false; }
|
|
|
|
// True when this reader need to become dormant state
|
|
|
|
virtual bool IsDormantNeeded() { return false; }
|
|
|
|
// Release media resources they should be released in dormant state
|
|
|
|
virtual void ReleaseMediaResources() {};
|
2013-10-14 08:38:17 +00:00
|
|
|
// Release the decoder during shutdown
|
|
|
|
virtual void ReleaseDecoder() {};
|
2013-06-10 12:22:05 +00:00
|
|
|
|
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
|
|
|
|
2013-04-03 00:05:00 +00:00
|
|
|
#ifdef MOZ_DASH
|
2012-12-06 23:27:08 +00:00
|
|
|
// Steps to carry out at the start of the |DecodeLoop|.
|
|
|
|
virtual void PrepareToDecode() { }
|
2013-04-03 00:05:00 +00:00
|
|
|
#endif
|
2012-12-06 23:27:08 +00:00
|
|
|
|
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,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t 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
|
|
|
|
2012-07-31 00:14:29 +00:00
|
|
|
// Read header data for all bitstreams in the file. Fills aInfo with
|
|
|
|
// the data required to present the media, and optionally fills *aTags
|
|
|
|
// with tag metadata from the file.
|
|
|
|
// Returns NS_OK on success, or NS_ERROR_FAILURE on failure.
|
2013-09-27 05:22:38 +00:00
|
|
|
virtual nsresult ReadMetadata(MediaInfo* aInfo,
|
2012-11-09 00:40:08 +00:00
|
|
|
MetadataTags** aTags) = 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.
|
2012-09-17 20:45:38 +00:00
|
|
|
virtual VideoData* FindStartTime(int64_t& 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.
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual nsresult Seek(int64_t aTime,
|
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t aCurrentTime) = 0;
|
2013-01-24 12:38:32 +00:00
|
|
|
|
2012-12-18 08:49:58 +00:00
|
|
|
// Called when the decode thread is started, before calling any other
|
|
|
|
// decode, read metadata, or seek functions. Do any thread local setup
|
|
|
|
// in this function.
|
|
|
|
virtual void OnDecodeThreadStart() {}
|
2013-01-24 12:38:32 +00:00
|
|
|
|
2012-12-18 08:49:58 +00:00
|
|
|
// Called when the decode thread is about to finish, after all calls to
|
|
|
|
// any other decode, read metadata, or seek functions. Any backend specific
|
|
|
|
// thread local tear down must be done in this function. Note that another
|
|
|
|
// decode thread could start up and run in future.
|
|
|
|
virtual void OnDecodeThreadFinish() {}
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2013-08-29 09:43:44 +00:00
|
|
|
// Tell the reader that the data decoded are not for direct playback, so it
|
|
|
|
// can accept more files, in particular those which have more channels than
|
|
|
|
// available in the audio output.
|
|
|
|
void SetIgnoreAudioOutputFormat()
|
|
|
|
{
|
|
|
|
mIgnoreAudioOutputFormat = true;
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
protected:
|
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;
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
public:
|
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.
|
2013-03-02 19:14:44 +00:00
|
|
|
virtual nsresult GetBuffered(dom::TimeRanges* aBuffered,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t 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) {}
|
|
|
|
|
2012-08-21 04:06:46 +00:00
|
|
|
virtual void* operator()(void* anObject);
|
2011-07-22 03:17:23 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mResult;
|
2011-07-22 03:17:23 +00:00
|
|
|
};
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
virtual int64_t VideoQueueMemoryInUse() {
|
2011-07-22 03:17:23 +00:00
|
|
|
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);
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2011-07-22 03:17:23 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mResult;
|
2011-07-22 03:17:23 +00:00
|
|
|
};
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
virtual int64_t AudioQueueMemoryInUse() {
|
2011-07-22 03:17:23 +00:00
|
|
|
AudioQueueMemoryFunctor functor;
|
|
|
|
mAudioQueue.LockedForEach(functor);
|
|
|
|
return functor.mResult;
|
|
|
|
}
|
|
|
|
|
2013-01-24 12:38:32 +00:00
|
|
|
// Only used by WebMReader and MediaOmxReader for now, so stub here rather
|
|
|
|
// than in every reader than inherits from MediaDecoderReader.
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) {}
|
2010-09-13 08:45:50 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
virtual MediaQueue<AudioData>& AudioQueue() { return mAudioQueue; }
|
|
|
|
virtual MediaQueue<VideoData>& VideoQueue() { return mVideoQueue; }
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
// Returns a pointer to the decoder.
|
2012-11-19 15:11:21 +00:00
|
|
|
AbstractMediaDecoder* GetDecoder() {
|
2012-09-17 20:45:38 +00:00
|
|
|
return mDecoder;
|
|
|
|
}
|
2010-08-13 02:28:15 +00:00
|
|
|
|
2012-11-06 22:33:01 +00:00
|
|
|
AudioData* DecodeToFirstAudioData();
|
|
|
|
VideoData* DecodeToFirstVideoData();
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
protected:
|
|
|
|
// Pumps the decode until we reach frames required to play at time aTarget
|
|
|
|
// (usecs).
|
|
|
|
nsresult DecodeToTarget(int64_t aTarget);
|
|
|
|
|
2011-07-12 03:39:28 +00:00
|
|
|
// Reference to the owning decoder object.
|
2012-11-19 15:11:21 +00:00
|
|
|
AbstractMediaDecoder* mDecoder;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2011-07-12 03:39:28 +00:00
|
|
|
// Stores presentation info required for playback.
|
2013-09-27 05:22:38 +00:00
|
|
|
MediaInfo mInfo;
|
2013-08-29 09:43:44 +00:00
|
|
|
|
|
|
|
// Whether we should accept media that we know we can't play
|
|
|
|
// directly, because they have a number of channel higher than
|
|
|
|
// what we support.
|
|
|
|
bool mIgnoreAudioOutputFormat;
|
2010-04-02 03:03:07 +00:00
|
|
|
};
|
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
#endif
|