2013-11-20 13:04:33 -08: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/. */
|
|
|
|
|
|
|
|
#if !defined(MP4Reader_h_)
|
|
|
|
#define MP4Reader_h_
|
|
|
|
|
|
|
|
#include "MediaDecoderReader.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "PlatformDecoderModule.h"
|
|
|
|
#include "mp4_demuxer/mp4_demuxer.h"
|
2015-03-04 20:30:44 -08:00
|
|
|
#include "demuxer/TrackDemuxer.h"
|
2014-02-04 17:29:28 -08:00
|
|
|
#include "MediaTaskQueue.h"
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
#include <deque>
|
Bug 959440 - Various cleanups in MP4Reader. r=kinetik
Change PlatformDecoderModule::Create*Decoder() to take an
mp4_demuxer::{Audio,Video}DecoderConfig parameter, instead of enumerating all
parameters. This means the platform decoders can have more data if need be,
like the AACAudioConfig.
Change MediaDataDecoder::Input() to take an nsAutoPtr<MP4Sample>&. The sample
will be deleted by the caller (MP4Reader) if Input() returns DECODE_STATUS_OK,
but if the MediaDataDecoder wants to assume responsibility of the
lifecycle of the sample (say to enqueue it), it can forget() on the
nsAutoPtr& passed in and assume responsibility.
Call PlatformDecoderModule::Create() on the decode thread. This is a step
towards making these classes decode-thread only.
Add PlatformDecoderModule::Init(), which caches the pref's we need, since
PlatformDecoderModule::Create() is no longer called on the main thread, we can
no longer access them in there.
Add Init() method to MediaDataDecoder interface. This is so that we can call
MediaDataDecoder::Shutdown() to unblock the initialization of a decoder, if
that init needs to block.
Pass LayersBackend type to WMFVideoDecoder, so it knows whether to init DXVA.
2014-01-14 19:13:54 -08:00
|
|
|
#include "mozilla/Monitor.h"
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
class TimeRanges;
|
|
|
|
}
|
|
|
|
|
2015-04-09 04:14:55 -07:00
|
|
|
typedef std::deque<nsRefPtr<MediaRawData>> MediaSampleQueue;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
class MP4Stream;
|
|
|
|
|
2015-03-11 17:07:20 -07:00
|
|
|
#if defined(MOZ_GONK_MEDIACODEC) || defined(XP_WIN) || defined(MOZ_APPLEMEDIA) || defined(MOZ_FFMPEG)
|
|
|
|
#define MP4_READER_DORMANT_HEURISTIC
|
|
|
|
#else
|
|
|
|
#undef MP4_READER_DORMANT_HEURISTIC
|
|
|
|
#endif
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class MP4Reader final : public MediaDecoderReader
|
2013-11-20 13:04:33 -08:00
|
|
|
{
|
2014-07-19 17:54:00 -07:00
|
|
|
typedef mp4_demuxer::TrackType TrackType;
|
|
|
|
|
2013-11-20 13:04:33 -08:00
|
|
|
public:
|
2014-08-31 20:50:23 -07:00
|
|
|
explicit MP4Reader(AbstractMediaDecoder* aDecoder);
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
virtual ~MP4Reader();
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsresult Init(MediaDecoderReader* aCloneDonor) override;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual size_t SizeOfVideoQueueInFrames() override;
|
|
|
|
virtual size_t SizeOfAudioQueueInFrames() override;
|
2014-12-17 15:41:19 -08:00
|
|
|
|
2014-12-10 14:03:56 -08:00
|
|
|
virtual nsRefPtr<VideoDataPromise>
|
2015-03-21 09:28:04 -07:00
|
|
|
RequestVideoData(bool aSkipToNextKeyframe, int64_t aTimeThreshold) override;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsRefPtr<AudioDataPromise> RequestAudioData() override;
|
2014-07-19 17:54:00 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool HasAudio() override;
|
|
|
|
virtual bool HasVideo() override;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
virtual nsresult ReadMetadata(MediaInfo* aInfo,
|
2015-03-21 09:28:04 -07:00
|
|
|
MetadataTags** aTags) override;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void ReadUpdatedMetadata(MediaInfo* aInfo) override;
|
2014-11-06 01:52:44 -08:00
|
|
|
|
2014-12-16 01:52:57 -08:00
|
|
|
virtual nsRefPtr<SeekPromise>
|
2015-03-21 09:28:04 -07:00
|
|
|
Seek(int64_t aTime, int64_t aEndTime) override;
|
2014-06-23 03:08:34 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool IsMediaSeekable() override;
|
2014-06-23 03:08:34 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual int64_t GetEvictionOffset(double aTime) override;
|
2015-04-07 03:33:17 -07:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) override;
|
2014-08-18 19:13:55 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsresult GetBuffered(dom::TimeRanges* aBuffered) override;
|
2014-06-02 08:38:04 -07:00
|
|
|
|
2014-08-07 03:23:45 -07:00
|
|
|
// For Media Resource Management
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void SetIdle() override;
|
|
|
|
virtual bool IsWaitingMediaResources() override;
|
|
|
|
virtual bool IsDormantNeeded() override;
|
|
|
|
virtual void ReleaseMediaResources() override;
|
2014-11-04 05:14:00 -08:00
|
|
|
virtual void SetSharedDecoderManager(SharedDecoderManager* aManager)
|
2015-03-21 09:28:04 -07:00
|
|
|
override;
|
2014-07-29 23:53:34 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsresult ResetDecode() override;
|
2014-07-31 21:04:37 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsRefPtr<ShutdownPromise> Shutdown() override;
|
2014-08-03 00:28:39 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool IsAsync() const override { return true; }
|
2015-01-13 01:31:03 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool VideoIsHardwareAccelerated() const override;
|
2015-03-08 20:17:30 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void DisableHardwareAcceleration() override;
|
2015-03-12 02:14:51 -07:00
|
|
|
|
2015-04-27 20:07:22 -07:00
|
|
|
static bool IsVideoAccelerated(layers::LayersBackend aBackend);
|
|
|
|
|
2014-02-04 17:29:28 -08:00
|
|
|
private:
|
2013-11-20 13:04:33 -08:00
|
|
|
|
2015-01-11 13:24:26 -08:00
|
|
|
bool InitDemuxer();
|
2014-07-19 17:54:00 -07:00
|
|
|
void ReturnOutput(MediaData* aData, TrackType aTrack);
|
|
|
|
|
2015-03-22 19:31:15 -07:00
|
|
|
bool EnsureDecodersSetup();
|
|
|
|
|
2015-04-09 23:16:00 -07:00
|
|
|
bool CheckIfDecoderSetup();
|
|
|
|
|
2014-07-19 17:54:00 -07:00
|
|
|
// Sends input to decoder for aTrack, and output to the state machine,
|
|
|
|
// if necessary.
|
|
|
|
void Update(TrackType aTrack);
|
|
|
|
|
|
|
|
// Enqueues a task to call Update(aTrack) on the decoder task queue.
|
|
|
|
// Lock for corresponding track must be held.
|
|
|
|
void ScheduleUpdate(TrackType aTrack);
|
|
|
|
|
2014-07-29 23:53:34 -07:00
|
|
|
void ExtractCryptoInitData(nsTArray<uint8_t>& aInitData);
|
|
|
|
|
2013-11-27 14:12:22 -08:00
|
|
|
// Initializes mLayersBackendType if possible.
|
|
|
|
void InitLayersBackendType();
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
// Blocks until the demuxer produces an sample of specified type.
|
|
|
|
// Returns nullptr on error on EOS. Caller must delete sample.
|
2015-04-09 04:14:55 -07:00
|
|
|
already_AddRefed<MediaRawData> PopSample(mp4_demuxer::TrackType aTrack);
|
|
|
|
already_AddRefed<MediaRawData> PopSampleLocked(mp4_demuxer::TrackType aTrack);
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
bool SkipVideoDemuxToNextKeyFrame(int64_t aTimeThreshold, uint32_t& parsed);
|
|
|
|
|
2014-07-19 17:54:00 -07:00
|
|
|
// DecoderCallback proxies the MediaDataDecoderCallback calls to these
|
|
|
|
// functions.
|
2014-02-04 17:29:28 -08:00
|
|
|
void Output(mp4_demuxer::TrackType aType, MediaData* aSample);
|
|
|
|
void InputExhausted(mp4_demuxer::TrackType aTrack);
|
|
|
|
void Error(mp4_demuxer::TrackType aTrack);
|
|
|
|
void Flush(mp4_demuxer::TrackType aTrack);
|
2014-07-24 19:57:25 -07:00
|
|
|
void DrainComplete(mp4_demuxer::TrackType aTrack);
|
2014-08-12 22:13:28 -07:00
|
|
|
void UpdateIndex();
|
2015-03-23 20:45:17 -07:00
|
|
|
bool IsSupportedAudioMimeType(const nsACString& aMimeType);
|
|
|
|
bool IsSupportedVideoMimeType(const nsACString& aMimeType);
|
2014-08-07 03:23:45 -07:00
|
|
|
void NotifyResourcesStatusChanged();
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool IsWaitingOnCDMResource() override;
|
2014-02-04 17:29:28 -08:00
|
|
|
|
2015-01-19 18:20:43 -08:00
|
|
|
Microseconds GetNextKeyframeTime();
|
|
|
|
bool ShouldSkip(bool aSkipToNextKeyframe, int64_t aTimeThreshold);
|
|
|
|
|
2014-12-17 15:41:19 -08:00
|
|
|
size_t SizeOfQueue(TrackType aTrack);
|
|
|
|
|
2015-01-11 13:24:26 -08:00
|
|
|
nsRefPtr<MP4Stream> mStream;
|
2015-03-04 20:30:44 -08:00
|
|
|
nsRefPtr<mp4_demuxer::MP4Demuxer> mDemuxer;
|
2014-12-22 19:36:10 -08:00
|
|
|
nsRefPtr<PlatformDecoderModule> mPlatform;
|
2015-03-04 20:30:44 -08:00
|
|
|
mp4_demuxer::CryptoFile mCrypto;
|
2013-11-20 13:04:33 -08:00
|
|
|
|
2014-02-04 17:29:28 -08:00
|
|
|
class DecoderCallback : public MediaDataDecoderCallback {
|
|
|
|
public:
|
|
|
|
DecoderCallback(MP4Reader* aReader,
|
|
|
|
mp4_demuxer::TrackType aType)
|
|
|
|
: mReader(aReader)
|
|
|
|
, mType(aType)
|
|
|
|
{
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void Output(MediaData* aSample) override {
|
2014-02-04 17:29:28 -08:00
|
|
|
mReader->Output(mType, aSample);
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void InputExhausted() override {
|
2014-02-04 17:29:28 -08:00
|
|
|
mReader->InputExhausted(mType);
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void Error() override {
|
2014-02-04 17:29:28 -08:00
|
|
|
mReader->Error(mType);
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void DrainComplete() override {
|
2014-07-24 19:57:25 -07:00
|
|
|
mReader->DrainComplete(mType);
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void NotifyResourcesStatusChanged() override {
|
2014-08-07 03:23:45 -07:00
|
|
|
mReader->NotifyResourcesStatusChanged();
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void ReleaseMediaResources() override {
|
2014-08-07 03:23:45 -07:00
|
|
|
mReader->ReleaseMediaResources();
|
|
|
|
}
|
2014-02-04 17:29:28 -08:00
|
|
|
private:
|
|
|
|
MP4Reader* mReader;
|
|
|
|
mp4_demuxer::TrackType mType;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DecoderData {
|
2014-12-10 14:03:56 -08:00
|
|
|
DecoderData(MediaData::Type aType,
|
2014-02-04 17:29:28 -08:00
|
|
|
uint32_t aDecodeAhead)
|
2014-12-10 14:03:56 -08:00
|
|
|
: mType(aType)
|
|
|
|
, mMonitor(aType == MediaData::AUDIO_DATA ? "MP4 audio decoder data"
|
|
|
|
: "MP4 video decoder data")
|
2014-02-04 17:29:28 -08:00
|
|
|
, mNumSamplesInput(0)
|
|
|
|
, mNumSamplesOutput(0)
|
|
|
|
, mDecodeAhead(aDecodeAhead)
|
|
|
|
, mActive(false)
|
|
|
|
, mInputExhausted(false)
|
|
|
|
, mError(false)
|
|
|
|
, mIsFlushing(false)
|
2014-07-19 17:54:00 -07:00
|
|
|
, mUpdateScheduled(false)
|
2014-12-04 11:51:30 -08:00
|
|
|
, mDemuxEOS(false)
|
|
|
|
, mDrainComplete(false)
|
2014-07-19 17:54:00 -07:00
|
|
|
, mDiscontinuity(false)
|
2014-02-04 17:29:28 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:30:44 -08:00
|
|
|
nsAutoPtr<TrackDemuxer> mTrackDemuxer;
|
2014-02-04 17:29:28 -08:00
|
|
|
// The platform decoder.
|
2014-07-30 18:40:19 -07:00
|
|
|
nsRefPtr<MediaDataDecoder> mDecoder;
|
2014-02-04 17:29:28 -08:00
|
|
|
// TaskQueue on which decoder can choose to decode.
|
|
|
|
// Only non-null up until the decoder is created.
|
2015-02-14 19:08:15 -08:00
|
|
|
nsRefPtr<FlushableMediaTaskQueue> mTaskQueue;
|
2014-02-04 17:29:28 -08:00
|
|
|
// Callback that receives output and error notifications from the decoder.
|
|
|
|
nsAutoPtr<DecoderCallback> mCallback;
|
2014-07-19 17:54:00 -07:00
|
|
|
// Decoded samples returned my mDecoder awaiting being returned to
|
|
|
|
// state machine upon request.
|
|
|
|
nsTArray<nsRefPtr<MediaData> > mOutput;
|
2014-12-10 14:03:56 -08:00
|
|
|
// Disambiguate Audio vs Video.
|
|
|
|
MediaData::Type mType;
|
|
|
|
|
|
|
|
// These get overriden in the templated concrete class.
|
|
|
|
virtual bool HasPromise() = 0;
|
|
|
|
virtual void RejectPromise(MediaDecoderReader::NotDecodedReason aReason,
|
|
|
|
const char* aMethodName) = 0;
|
2014-07-19 17:54:00 -07:00
|
|
|
|
2014-02-04 17:29:28 -08:00
|
|
|
// Monitor that protects all non-threadsafe state; the primitives
|
|
|
|
// that follow.
|
|
|
|
Monitor mMonitor;
|
|
|
|
uint64_t mNumSamplesInput;
|
|
|
|
uint64_t mNumSamplesOutput;
|
|
|
|
uint32_t mDecodeAhead;
|
|
|
|
// Whether this stream exists in the media.
|
|
|
|
bool mActive;
|
|
|
|
bool mInputExhausted;
|
|
|
|
bool mError;
|
|
|
|
bool mIsFlushing;
|
2014-07-19 17:54:00 -07:00
|
|
|
bool mUpdateScheduled;
|
2014-12-04 11:51:30 -08:00
|
|
|
bool mDemuxEOS;
|
|
|
|
bool mDrainComplete;
|
2014-07-19 17:54:00 -07:00
|
|
|
bool mDiscontinuity;
|
2014-02-04 17:29:28 -08:00
|
|
|
};
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
template<typename PromiseType>
|
|
|
|
struct DecoderDataWithPromise : public DecoderData {
|
|
|
|
DecoderDataWithPromise(MediaData::Type aType, uint32_t aDecodeAhead) :
|
|
|
|
DecoderData(aType, aDecodeAhead)
|
|
|
|
{
|
|
|
|
mPromise.SetMonitor(&mMonitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaPromiseHolder<PromiseType> mPromise;
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
bool HasPromise() override { return !mPromise.IsEmpty(); }
|
2014-12-10 14:03:56 -08:00
|
|
|
void RejectPromise(MediaDecoderReader::NotDecodedReason aReason,
|
2015-03-21 09:28:04 -07:00
|
|
|
const char* aMethodName) override
|
2014-12-10 14:03:56 -08:00
|
|
|
{
|
|
|
|
mPromise.Reject(aReason, aMethodName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DecoderDataWithPromise<AudioDataPromise> mAudio;
|
|
|
|
DecoderDataWithPromise<VideoDataPromise> mVideo;
|
|
|
|
|
2014-07-29 23:53:34 -07:00
|
|
|
// Queued samples extracted by the demuxer, but not yet sent to the platform
|
2014-05-21 19:42:39 -07:00
|
|
|
// decoder.
|
2015-04-09 04:14:55 -07:00
|
|
|
nsRefPtr<MediaRawData> mQueuedVideoSample;
|
2014-02-04 17:29:28 -08:00
|
|
|
|
2014-07-19 17:54:00 -07:00
|
|
|
// Returns true when the decoder for this track needs input.
|
|
|
|
// aDecoder.mMonitor must be locked.
|
|
|
|
bool NeedInput(DecoderData& aDecoder);
|
|
|
|
|
2014-02-04 17:29:28 -08:00
|
|
|
// The last number of decoded output frames that we've reported to
|
|
|
|
// MediaDecoder::NotifyDecoded(). We diff the number of output video
|
|
|
|
// frames every time that DecodeVideoData() is called, and report the
|
|
|
|
// delta there.
|
|
|
|
uint64_t mLastReportedNumDecodedFrames;
|
|
|
|
|
|
|
|
DecoderData& GetDecoderData(mp4_demuxer::TrackType aTrack);
|
2013-11-20 13:04:33 -08:00
|
|
|
|
|
|
|
layers::LayersBackend mLayersBackendType;
|
|
|
|
|
2015-04-09 04:14:55 -07:00
|
|
|
// For use with InvokeAndRetry as an already_refed can't be converted to bool
|
|
|
|
nsRefPtr<MediaRawData> DemuxVideoSample();
|
|
|
|
nsRefPtr<MediaRawData> DemuxAudioSample();
|
|
|
|
|
2014-07-29 23:53:34 -07:00
|
|
|
// True if we've read the streams' metadata.
|
|
|
|
bool mDemuxerInitialized;
|
|
|
|
|
2015-02-11 00:10:00 -08:00
|
|
|
// True if we've gathered telemetry from an SPS.
|
|
|
|
bool mFoundSPSForTelemetry;
|
|
|
|
|
2014-07-29 23:53:34 -07:00
|
|
|
// Synchronized by decoder monitor.
|
|
|
|
bool mIsEncrypted;
|
2014-08-18 19:13:56 -07:00
|
|
|
|
|
|
|
bool mIndexReady;
|
2015-04-07 03:33:17 -07:00
|
|
|
int64_t mLastSeenEnd;
|
2014-12-29 16:10:16 -08:00
|
|
|
Monitor mDemuxerMonitor;
|
2014-11-04 05:14:00 -08:00
|
|
|
nsRefPtr<SharedDecoderManager> mSharedDecoderManager;
|
2015-01-29 00:50:48 -08:00
|
|
|
|
2015-03-11 17:07:20 -07:00
|
|
|
#if defined(MP4_READER_DORMANT_HEURISTIC)
|
2015-01-29 00:50:48 -08:00
|
|
|
const bool mDormantEnabled;
|
|
|
|
#endif
|
2013-11-20 13:04:33 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|