2014-02-03 17:49:21 -08: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 04:12:37 -07: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 11:46:40 -08:00
|
|
|
#if !defined(MediaDecoderReader_h_)
|
|
|
|
#define MediaDecoderReader_h_
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2014-02-03 17:49:21 -08:00
|
|
|
#include "MediaInfo.h"
|
|
|
|
#include "MediaData.h"
|
|
|
|
#include "MediaQueue.h"
|
2014-02-05 15:11:25 -08:00
|
|
|
#include "AudioCompactor.h"
|
2012-11-06 14:33:01 -08:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-09-05 13:25:17 -07:00
|
|
|
namespace dom {
|
|
|
|
class TimeRanges;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:28 -07:00
|
|
|
// Encapsulates the decoding and reading of media data. Reading can only be
|
2011-10-20 01:08:59 -07:00
|
|
|
// done on the decode thread. Never hold the decoder monitor when
|
2011-07-11 20:39:28 -07: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 11:30:34 -08:00
|
|
|
class MediaDecoderReader {
|
2010-04-01 20:03:07 -07:00
|
|
|
public:
|
2012-11-19 07:11:21 -08:00
|
|
|
MediaDecoderReader(AbstractMediaDecoder* aDecoder);
|
2012-11-14 11:46:40 -08:00
|
|
|
virtual ~MediaDecoderReader();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
// Initializes the reader, returns NS_OK on success, or NS_ERROR_FAILURE
|
|
|
|
// on failure.
|
2012-11-14 11:46:40 -08:00
|
|
|
virtual nsresult Init(MediaDecoderReader* aCloneDonor) = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2013-06-10 05:22:05 -07: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 01:38:17 -07:00
|
|
|
// Release the decoder during shutdown
|
|
|
|
virtual void ReleaseDecoder() {};
|
2013-06-10 05:22:05 -07:00
|
|
|
|
2010-05-05 19:31:02 -07: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 16:34:37 -07: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-05 19:31:02 -07:00
|
|
|
// or an un-recoverable read error has occured.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool DecodeAudioData() = 0;
|
2010-05-05 19:31:02 -07: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 16:34:37 -07:00
|
|
|
// and aKeyframeSkip is true), but will not be added to the queue.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool DecodeVideoFrame(bool &aKeyframeSkip,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aTimeThreshold) = 0;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool HasAudio() = 0;
|
|
|
|
virtual bool HasVideo() = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-07-30 17:14:29 -07: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-26 22:22:38 -07:00
|
|
|
virtual nsresult ReadMetadata(MediaInfo* aInfo,
|
2012-11-08 16:40:08 -08:00
|
|
|
MetadataTags** aTags) = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2011-09-26 20:31:18 -07: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 13:45:38 -07:00
|
|
|
virtual VideoData* FindStartTime(int64_t& aOutStartTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-04-13 15:12:23 -07: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 08:56:38 -07:00
|
|
|
virtual nsresult Seek(int64_t aTime,
|
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t aCurrentTime) = 0;
|
2013-01-24 04:38:32 -08:00
|
|
|
|
2014-05-18 19:23:00 -07:00
|
|
|
// Called to move the reader into idle state. When the reader is
|
2014-03-10 20:44:10 -07: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-18 19:23:00 -07: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-10 20:44:10 -07:00
|
|
|
virtual void SetIdle() { }
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2013-08-29 02:43:44 -07: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 13:45:38 -07:00
|
|
|
protected:
|
2011-09-26 20:31:18 -07:00
|
|
|
// Queue of audio frames. This queue is threadsafe, and is accessed from
|
2011-07-11 20:39:28 -07:00
|
|
|
// the audio, decoder, state machine, and main threads.
|
2011-08-15 22:19:51 -07:00
|
|
|
MediaQueue<AudioData> mAudioQueue;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Queue of video frames. This queue is threadsafe, and is accessed from
|
2011-07-11 20:39:28 -07:00
|
|
|
// the decoder, state machine, and main threads.
|
2010-04-01 20:03:07 -07:00
|
|
|
MediaQueue<VideoData> mVideoQueue;
|
|
|
|
|
2014-02-05 15:11:25 -08:00
|
|
|
// 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;
|
|
|
|
|
2012-09-17 13:45:38 -07:00
|
|
|
public:
|
2010-08-05 00:40:35 -07:00
|
|
|
// Populates aBuffered with the time ranges which are buffered. aStartTime
|
2011-09-26 20:31:18 -07:00
|
|
|
// must be the presentation time of the first frame in the media, e.g.
|
2010-08-05 00:40:35 -07:00
|
|
|
// the media time corresponding to playback time/position 0. This function
|
2013-10-20 20:31:05 -07: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.
|
2013-03-02 11:14:44 -08:00
|
|
|
virtual nsresult GetBuffered(dom::TimeRanges* aBuffered,
|
2013-10-20 20:31:05 -07:00
|
|
|
int64_t aStartTime);
|
2010-08-05 00:40:35 -07:00
|
|
|
|
2014-03-19 14:33:12 -07:00
|
|
|
// Returns the number of bytes of memory allocated by structures/frames in
|
|
|
|
// the video queue.
|
|
|
|
size_t SizeOfVideoQueueInBytes() const;
|
2011-07-21 20:17:23 -07:00
|
|
|
|
2014-03-19 14:33:12 -07:00
|
|
|
// Returns the number of bytes of memory allocated by structures/frames in
|
|
|
|
// the audio queue.
|
|
|
|
size_t SizeOfAudioQueueInBytes() const;
|
2011-07-21 20:17:23 -07:00
|
|
|
|
2013-01-24 04:38:32 -08:00
|
|
|
// Only used by WebMReader and MediaOmxReader for now, so stub here rather
|
|
|
|
// than in every reader than inherits from MediaDecoderReader.
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) {}
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2012-09-17 13:45:38 -07:00
|
|
|
virtual MediaQueue<AudioData>& AudioQueue() { return mAudioQueue; }
|
|
|
|
virtual MediaQueue<VideoData>& VideoQueue() { return mVideoQueue; }
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2012-09-17 13:45:38 -07:00
|
|
|
// Returns a pointer to the decoder.
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder* GetDecoder() {
|
2012-09-17 13:45:38 -07:00
|
|
|
return mDecoder;
|
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2012-11-06 14:33:01 -08:00
|
|
|
AudioData* DecodeToFirstAudioData();
|
|
|
|
VideoData* DecodeToFirstVideoData();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2014-03-31 20:39:04 -07:00
|
|
|
// Decodes samples until we reach frames required to play at time aTarget
|
|
|
|
// (usecs). This also trims the samples to start exactly at aTarget,
|
|
|
|
// by discarding audio samples and adjusting start times of video frames.
|
2014-03-28 05:31:29 -07:00
|
|
|
nsresult DecodeToTarget(int64_t aTarget);
|
2014-03-28 02:36:10 -07:00
|
|
|
|
2014-04-21 06:30:00 -07:00
|
|
|
MediaInfo GetMediaInfo() { return mInfo; }
|
|
|
|
|
2014-03-31 20:39:04 -07:00
|
|
|
protected:
|
|
|
|
|
2011-07-11 20:39:28 -07:00
|
|
|
// Reference to the owning decoder object.
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder* mDecoder;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:28 -07:00
|
|
|
// Stores presentation info required for playback.
|
2013-09-26 22:22:38 -07:00
|
|
|
MediaInfo mInfo;
|
2013-08-29 02:43:44 -07: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-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
#endif
|