2014-02-04 05:49:21 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 23:46:40 +04:00
|
|
|
#if !defined(MediaDecoderReader_h_)
|
|
|
|
#define MediaDecoderReader_h_
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2014-02-04 05:49:21 +04:00
|
|
|
#include "MediaInfo.h"
|
|
|
|
#include "MediaData.h"
|
|
|
|
#include "MediaQueue.h"
|
2014-02-06 03:11:25 +04:00
|
|
|
#include "AudioCompactor.h"
|
2012-11-07 02:33:01 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-09-06 00:25:17 +04:00
|
|
|
namespace dom {
|
|
|
|
class TimeRanges;
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
class RequestSampleCallback;
|
2014-06-20 15:08:24 +04:00
|
|
|
class MediaDecoderReader;
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Encapsulates the decoding and reading of media data. Reading can either
|
|
|
|
// synchronous and done on the calling "decode" thread, or asynchronous and
|
|
|
|
// performed on a background thread, with the result being returned by
|
|
|
|
// callback. Never hold the decoder monitor when calling into this class.
|
|
|
|
// Unless otherwise specified, methods and fields of this class can only
|
|
|
|
// be accessed on the decode task queue.
|
2012-11-16 23:30:34 +04:00
|
|
|
class MediaDecoderReader {
|
2010-04-02 07:03:07 +04:00
|
|
|
public:
|
2014-06-18 09:07:02 +04:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDecoderReader)
|
|
|
|
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit MediaDecoderReader(AbstractMediaDecoder* aDecoder);
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2010-05-06 06:31:02 +04:00
|
|
|
// Initializes the reader, returns NS_OK on success, or NS_ERROR_FAILURE
|
|
|
|
// on failure.
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual nsresult Init(MediaDecoderReader* aCloneDonor) = 0;
|
2010-05-06 06:31:02 +04:00
|
|
|
|
2013-06-10 16:22:05 +04:00
|
|
|
// True if this reader is waiting media resource allocation
|
|
|
|
virtual bool IsWaitingMediaResources() { return false; }
|
2014-10-14 02:05:00 +04:00
|
|
|
// True if this reader is waiting for a Content Decryption Module to become
|
|
|
|
// available.
|
|
|
|
virtual bool IsWaitingOnCDMResource() { return false; }
|
2013-06-10 16:22:05 +04:00
|
|
|
// True when this reader need to become dormant state
|
|
|
|
virtual bool IsDormantNeeded() { return false; }
|
|
|
|
// Release media resources they should be released in dormant state
|
2014-06-18 09:07:02 +04:00
|
|
|
// The reader can be made usable again by calling ReadMetadata().
|
2014-09-02 06:22:06 +04:00
|
|
|
virtual void ReleaseMediaResources() {};
|
2014-06-18 09:07:02 +04:00
|
|
|
// Breaks reference-counted cycles. Called during shutdown.
|
|
|
|
// WARNING: If you override this, you must call the base implementation
|
|
|
|
// in your override.
|
|
|
|
virtual void BreakCycles();
|
|
|
|
|
|
|
|
// Destroys the decoding state. The reader cannot be made usable again.
|
|
|
|
// This is different from ReleaseMediaResources() as it is irreversable,
|
|
|
|
// whereas ReleaseMediaResources() is.
|
|
|
|
virtual void Shutdown();
|
|
|
|
|
|
|
|
virtual void SetCallback(RequestSampleCallback* aDecodedSampleCallback);
|
|
|
|
virtual void SetTaskQueue(MediaTaskQueue* aTaskQueue);
|
2013-06-10 16:22:05 +04:00
|
|
|
|
2010-05-06 06:31:02 +04:00
|
|
|
// Resets all state related to decoding, emptying all buffers etc.
|
2014-06-18 09:07:02 +04:00
|
|
|
// Cancels all pending Request*Data() request callbacks, and flushes the
|
|
|
|
// decode pipeline. The decoder must not call any of the callbacks for
|
|
|
|
// outstanding Request*Data() calls after this is called. Calls to
|
|
|
|
// Request*Data() made after this should be processed as usual.
|
|
|
|
// Normally this call preceedes a Seek() call, or shutdown.
|
|
|
|
// The first samples of every stream produced after a ResetDecode() call
|
|
|
|
// *must* be marked as "discontinuities". If it's not, seeking work won't
|
|
|
|
// properly!
|
2010-05-06 06:31:02 +04:00
|
|
|
virtual nsresult ResetDecode();
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Requests the Reader to call OnAudioDecoded() on aCallback with one
|
|
|
|
// audio sample. The decode should be performed asynchronously, and
|
|
|
|
// the callback can be performed on any thread. Don't hold the decoder
|
|
|
|
// monitor while calling this, as the implementation may try to wait
|
|
|
|
// on something that needs the monitor and deadlock.
|
|
|
|
virtual void RequestAudioData();
|
|
|
|
|
|
|
|
// Requests the Reader to call OnVideoDecoded() on aCallback with one
|
|
|
|
// video sample. The decode should be performed asynchronously, and
|
|
|
|
// the callback can be performed on any thread. Don't hold the decoder
|
|
|
|
// monitor while calling this, as the implementation may try to wait
|
|
|
|
// on something that needs the monitor and deadlock.
|
|
|
|
// If aSkipToKeyframe is true, the decode should skip ahead to the
|
|
|
|
// the next keyframe at or after aTimeThreshold microseconds.
|
|
|
|
virtual void RequestVideoData(bool aSkipToNextKeyframe,
|
|
|
|
int64_t aTimeThreshold);
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool HasAudio() = 0;
|
|
|
|
virtual bool HasVideo() = 0;
|
2010-05-06 06:31:02 +04:00
|
|
|
|
2014-10-06 07:03:14 +04:00
|
|
|
// A function that is called before ReadMetadata() call.
|
|
|
|
virtual void PreReadMetadata() {};
|
|
|
|
|
2012-07-31 04:14:29 +04: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 09:22:38 +04:00
|
|
|
virtual nsresult ReadMetadata(MediaInfo* aInfo,
|
2012-11-09 04:40:08 +04:00
|
|
|
MetadataTags** aTags) = 0;
|
2010-05-06 06:31:02 +04:00
|
|
|
|
2014-11-06 12:52:44 +03:00
|
|
|
// Fills aInfo with the latest cached data required to present the media,
|
|
|
|
// ReadUpdatedMetadata will always be called once ReadMetadata has succeeded.
|
|
|
|
virtual void ReadUpdatedMetadata(MediaInfo* aInfo) { };
|
|
|
|
|
2014-11-05 03:32:26 +03:00
|
|
|
// Requests the Reader to seek and call OnSeekCompleted on the callback
|
|
|
|
// once completed.
|
2011-04-14 02:12:23 +04: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.
|
2014-11-05 03:32:26 +03:00
|
|
|
virtual void Seek(int64_t aTime,
|
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t aCurrentTime) = 0;
|
2013-01-24 16:38:32 +04:00
|
|
|
|
2014-05-19 06:23:00 +04:00
|
|
|
// Called to move the reader into idle state. When the reader is
|
2014-03-11 07:44:10 +04:00
|
|
|
// created it is assumed to be active (i.e. not idle). When the media
|
|
|
|
// element is paused and we don't need to decode any more data, the state
|
|
|
|
// machine calls SetIdle() to inform the reader that its decoder won't be
|
2014-05-19 06:23:00 +04:00
|
|
|
// needed for a while. The reader can use these notifications to enter
|
|
|
|
// a low power state when the decoder isn't needed, if desired.
|
|
|
|
// This is most useful on mobile.
|
|
|
|
// Note: DecodeVideoFrame, DecodeAudioData, ReadMetadata and Seek should
|
|
|
|
// activate the decoder if necessary. The state machine only needs to know
|
|
|
|
// when to call SetIdle().
|
2014-03-11 07:44:10 +04:00
|
|
|
virtual void SetIdle() { }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2013-08-29 13:43:44 +04: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;
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:57:43 +03:00
|
|
|
// Populates aBuffered with the time ranges which are buffered. aStartTime
|
|
|
|
// must be the presentation time of the first frame in the media, e.g.
|
|
|
|
// the media time corresponding to playback time/position 0. This function
|
2013-10-21 07:31:05 +04:00
|
|
|
// is called on the main, decode, and state machine threads.
|
|
|
|
//
|
|
|
|
// This base implementation in MediaDecoderReader estimates the time ranges
|
|
|
|
// buffered by interpolating the cached byte ranges with the duration
|
|
|
|
// of the media. Reader subclasses should override this method if they
|
|
|
|
// can quickly calculate the buffered ranges more accurately.
|
|
|
|
//
|
|
|
|
// The primary advantage of this implementation in the reader base class
|
|
|
|
// is that it's a fast approximation, which does not perform any I/O.
|
|
|
|
//
|
|
|
|
// The OggReader relies on this base implementation not performing I/O,
|
|
|
|
// since in FirefoxOS we can't do I/O on the main thread, where this is
|
|
|
|
// called.
|
2014-11-05 14:57:43 +03:00
|
|
|
virtual nsresult GetBuffered(dom::TimeRanges* aBuffered,
|
|
|
|
int64_t aStartTime);
|
2010-08-05 11:40:35 +04:00
|
|
|
|
2014-10-28 23:30:36 +03:00
|
|
|
virtual int64_t ComputeStartTime(const VideoData* aVideo, const AudioData* aAudio);
|
|
|
|
|
2014-03-20 01:33:12 +04:00
|
|
|
// Returns the number of bytes of memory allocated by structures/frames in
|
|
|
|
// the video queue.
|
|
|
|
size_t SizeOfVideoQueueInBytes() const;
|
2011-07-22 07:17:23 +04:00
|
|
|
|
2014-03-20 01:33:12 +04:00
|
|
|
// Returns the number of bytes of memory allocated by structures/frames in
|
|
|
|
// the audio queue.
|
|
|
|
size_t SizeOfAudioQueueInBytes() const;
|
2011-07-22 07:17:23 +04:00
|
|
|
|
2013-01-24 16:38:32 +04:00
|
|
|
// Only used by WebMReader and MediaOmxReader for now, so stub here rather
|
|
|
|
// than in every reader than inherits from MediaDecoderReader.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) {}
|
2014-08-19 06:13:55 +04:00
|
|
|
virtual int64_t GetEvictionOffset(double aTime) { return -1; }
|
2010-09-13 12:45:50 +04:00
|
|
|
|
2012-09-18 00:45:38 +04:00
|
|
|
virtual MediaQueue<AudioData>& AudioQueue() { return mAudioQueue; }
|
|
|
|
virtual MediaQueue<VideoData>& VideoQueue() { return mVideoQueue; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2012-09-18 00:45:38 +04:00
|
|
|
// Returns a pointer to the decoder.
|
2012-11-19 19:11:21 +04:00
|
|
|
AbstractMediaDecoder* GetDecoder() {
|
2012-09-18 00:45:38 +04:00
|
|
|
return mDecoder;
|
|
|
|
}
|
2010-08-13 06:28:15 +04:00
|
|
|
|
2014-08-22 07:11:58 +04:00
|
|
|
// TODO: DEPRECATED. This uses synchronous decoding.
|
|
|
|
VideoData* DecodeToFirstVideoData();
|
|
|
|
|
2014-04-21 17:30:00 +04:00
|
|
|
MediaInfo GetMediaInfo() { return mInfo; }
|
|
|
|
|
2014-06-23 14:08:34 +04:00
|
|
|
// Indicates if the media is seekable.
|
|
|
|
// ReadMetada should be called before calling this method.
|
|
|
|
virtual bool IsMediaSeekable() = 0;
|
2014-07-30 10:53:34 +04:00
|
|
|
|
2014-11-04 01:16:34 +03:00
|
|
|
MediaTaskQueue* GetTaskQueue() {
|
|
|
|
return mTaskQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearDecoder() {
|
|
|
|
mDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-01 07:39:04 +04:00
|
|
|
protected:
|
2014-06-22 08:44:00 +04:00
|
|
|
virtual ~MediaDecoderReader();
|
2014-04-01 07:39:04 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Overrides of this function should decodes an unspecified amount of
|
|
|
|
// audio data, enqueuing the audio data in mAudioQueue. Returns true
|
|
|
|
// when there's more audio to decode, false if the audio is finished,
|
|
|
|
// end of file has been reached, or an un-recoverable read error has
|
|
|
|
// occured. This function blocks until the decode is complete.
|
|
|
|
virtual bool DecodeAudioData() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides of this function should read and decodes one video frame.
|
|
|
|
// Packets with a timestamp less than aTimeThreshold will be decoded
|
|
|
|
// (unless they're not keyframes and aKeyframeSkip is true), but will
|
|
|
|
// not be added to the queue. This function blocks until the decode
|
|
|
|
// is complete.
|
|
|
|
virtual bool DecodeVideoFrame(bool &aKeyframeSkip, int64_t aTimeThreshold) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSampleCallback* GetCallback() {
|
|
|
|
MOZ_ASSERT(mSampleDecodedCallback);
|
|
|
|
return mSampleDecodedCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue of audio frames. This queue is threadsafe, and is accessed from
|
|
|
|
// the audio, decoder, state machine, and main threads.
|
|
|
|
MediaQueue<AudioData> mAudioQueue;
|
|
|
|
|
|
|
|
// Queue of video frames. This queue is threadsafe, and is accessed from
|
|
|
|
// the decoder, state machine, and main threads.
|
|
|
|
MediaQueue<VideoData> mVideoQueue;
|
|
|
|
|
|
|
|
// An adapter to the audio queue which first copies data to buffers with
|
|
|
|
// minimal allocation slop and then pushes them to the queue. This is
|
|
|
|
// useful for decoders working with formats that give awkward numbers of
|
|
|
|
// frames such as mp3.
|
|
|
|
AudioCompactor mAudioCompactor;
|
|
|
|
|
2011-07-12 07:39:28 +04:00
|
|
|
// Reference to the owning decoder object.
|
2012-11-19 19:11:21 +04:00
|
|
|
AbstractMediaDecoder* mDecoder;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:28 +04:00
|
|
|
// Stores presentation info required for playback.
|
2013-09-27 09:22:38 +04:00
|
|
|
MediaInfo mInfo;
|
2013-08-29 13:43:44 +04: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;
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
nsRefPtr<RequestSampleCallback> mSampleDecodedCallback;
|
|
|
|
|
|
|
|
nsRefPtr<MediaTaskQueue> mTaskQueue;
|
|
|
|
|
|
|
|
// Flags whether a the next audio/video sample comes after a "gap" or
|
|
|
|
// "discontinuity" in the stream. For example after a seek.
|
|
|
|
bool mAudioDiscontinuity;
|
|
|
|
bool mVideoDiscontinuity;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Interface that callers to MediaDecoderReader::Request{Audio,Video}Data()
|
|
|
|
// must implement to receive the requested samples asynchronously.
|
|
|
|
// This object is refcounted, and cycles must be broken by calling
|
|
|
|
// BreakCycles() during shutdown.
|
|
|
|
class RequestSampleCallback {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RequestSampleCallback)
|
|
|
|
|
2014-11-03 11:20:14 +03:00
|
|
|
enum NotDecodedReason {
|
|
|
|
END_OF_STREAM,
|
2014-11-03 11:20:15 +03:00
|
|
|
DECODE_ERROR,
|
|
|
|
WAITING_FOR_DATA
|
2014-11-03 11:20:14 +03:00
|
|
|
};
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Receives the result of a RequestAudioData() call.
|
|
|
|
virtual void OnAudioDecoded(AudioData* aSample) = 0;
|
|
|
|
|
|
|
|
// Receives the result of a RequestVideoData() call.
|
|
|
|
virtual void OnVideoDecoded(VideoData* aSample) = 0;
|
|
|
|
|
2014-11-03 11:20:14 +03:00
|
|
|
// Called when a RequestAudioData() or RequestVideoData() call can't be
|
|
|
|
// fulfiled. The reason is passed as aReason.
|
|
|
|
virtual void OnNotDecoded(MediaData::Type aType, NotDecodedReason aReason) = 0;
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2014-11-05 03:32:26 +03:00
|
|
|
virtual void OnSeekCompleted(nsresult aResult) = 0;
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Called during shutdown to break any reference cycles.
|
|
|
|
virtual void BreakCycles() = 0;
|
|
|
|
|
2014-06-20 15:08:24 +04:00
|
|
|
protected:
|
2014-06-18 09:07:02 +04:00
|
|
|
virtual ~RequestSampleCallback() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A RequestSampleCallback implementation that can be passed to the
|
|
|
|
// MediaDecoderReader to block the thread requesting an audio sample until
|
|
|
|
// the audio decode is complete. This is used to adapt the asynchronous
|
|
|
|
// model of the MediaDecoderReader to a synchronous model.
|
|
|
|
class AudioDecodeRendezvous : public RequestSampleCallback {
|
|
|
|
public:
|
2014-11-03 11:20:14 +03:00
|
|
|
using RequestSampleCallback::NotDecodedReason;
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
AudioDecodeRendezvous();
|
|
|
|
~AudioDecodeRendezvous();
|
|
|
|
|
|
|
|
// RequestSampleCallback implementation. Called when decode is complete.
|
|
|
|
// Note: aSample is null at end of stream.
|
|
|
|
virtual void OnAudioDecoded(AudioData* aSample) MOZ_OVERRIDE;
|
|
|
|
virtual void OnVideoDecoded(VideoData* aSample) MOZ_OVERRIDE {}
|
2014-11-03 11:20:14 +03:00
|
|
|
virtual void OnNotDecoded(MediaData::Type aType, NotDecodedReason aReason) MOZ_OVERRIDE;
|
2014-11-05 03:32:26 +03:00
|
|
|
virtual void OnSeekCompleted(nsresult aResult) MOZ_OVERRIDE {};
|
2014-06-18 09:07:02 +04:00
|
|
|
virtual void BreakCycles() MOZ_OVERRIDE {};
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
// Returns failure on error, or NS_OK.
|
|
|
|
// If *aSample is null, EOS has been reached.
|
|
|
|
nsresult Await(nsAutoPtr<AudioData>& aSample);
|
|
|
|
|
|
|
|
// Interrupts a call to Wait().
|
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Monitor mMonitor;
|
|
|
|
nsresult mStatus;
|
|
|
|
nsAutoPtr<AudioData> mSample;
|
|
|
|
bool mHaveResult;
|
2010-04-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
#endif
|