2015-05-11 13:54:10 +03: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: */
|
|
|
|
/* 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(MediaDataDemuxer_h)
|
|
|
|
#define MediaDataDemuxer_h
|
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2015-05-11 13:54:10 +03:00
|
|
|
#include "MediaData.h"
|
|
|
|
#include "MediaInfo.h"
|
2016-09-12 05:22:20 +03:00
|
|
|
#include "MediaResult.h"
|
2015-05-11 13:54:10 +03:00
|
|
|
#include "TimeUnits.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-05-11 13:54:10 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class MediaTrackDemuxer;
|
|
|
|
class TrackMetadataHolder;
|
|
|
|
|
|
|
|
// Allows reading the media data: to retrieve the metadata and demux samples.
|
|
|
|
// MediaDataDemuxer isn't designed to be thread safe.
|
|
|
|
// When used by the MediaFormatDecoder, care is taken to ensure that the demuxer
|
|
|
|
// will never be called from more than one thread at once.
|
|
|
|
class MediaDataDemuxer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDataDemuxer)
|
|
|
|
|
2017-02-28 08:24:57 +03:00
|
|
|
typedef
|
|
|
|
MozPromise<MediaResult, MediaResult, /* IsExclusive = */ true> InitPromise;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Initializes the demuxer. Other methods cannot be called unless
|
|
|
|
// initialization has completed and succeeded.
|
|
|
|
// Typically a demuxer will wait to parse the metadata before resolving the
|
2015-09-15 06:03:48 +03:00
|
|
|
// promise. The promise must not be resolved until sufficient data is
|
|
|
|
// supplied. For example, an incomplete metadata would cause the promise to be
|
|
|
|
// rejected should no more data be coming, while the demuxer would wait
|
|
|
|
// otherwise.
|
2015-10-18 08:24:48 +03:00
|
|
|
virtual RefPtr<InitPromise> Init() = 0;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Returns true if a aType track type is available.
|
|
|
|
virtual bool HasTrackType(TrackInfo::TrackType aType) const = 0;
|
|
|
|
|
|
|
|
// Returns the number of tracks of aType type available. A value of
|
|
|
|
// 0 indicates that no such type is available.
|
|
|
|
virtual uint32_t GetNumberTracks(TrackInfo::TrackType aType) const = 0;
|
|
|
|
|
|
|
|
// Returns the MediaTrackDemuxer associated with aTrackNumber aType track.
|
|
|
|
// aTrackNumber is not to be confused with the Track ID.
|
|
|
|
// aTrackNumber must be constrained between 0 and GetNumberTracks(aType) - 1
|
|
|
|
// The actual Track ID is to be retrieved by calling
|
|
|
|
// MediaTrackDemuxer::TrackInfo.
|
2017-01-27 15:20:37 +03:00
|
|
|
virtual already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(
|
|
|
|
TrackInfo::TrackType aType, uint32_t aTrackNumber) = 0;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Returns true if the underlying resource allows seeking.
|
|
|
|
virtual bool IsSeekable() const = 0;
|
|
|
|
|
2016-02-04 07:31:21 +03:00
|
|
|
// Returns true if the underlying resource can only seek within buffered
|
|
|
|
// ranges.
|
|
|
|
virtual bool IsSeekableOnlyInBufferedRanges() const { return false; }
|
|
|
|
|
2015-05-11 13:54:10 +03:00
|
|
|
// Returns the media's crypto information, or nullptr if media isn't
|
|
|
|
// encrypted.
|
|
|
|
virtual UniquePtr<EncryptionInfo> GetCrypto()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-07 04:34:32 +03:00
|
|
|
// Notifies the demuxer that the underlying resource has received more data
|
|
|
|
// since the demuxer was initialized.
|
2015-05-11 13:54:10 +03:00
|
|
|
// The demuxer can use this mechanism to inform all track demuxers that new
|
2015-10-07 04:34:32 +03:00
|
|
|
// data is available and to refresh its buffered range.
|
|
|
|
virtual void NotifyDataArrived() { }
|
2015-05-11 13:54:10 +03:00
|
|
|
|
2015-10-07 04:34:32 +03:00
|
|
|
// Notifies the demuxer that the underlying resource has had data removed
|
|
|
|
// since the demuxer was initialized.
|
2015-05-25 08:09:16 +03:00
|
|
|
// The demuxer can use this mechanism to inform all track demuxers to update
|
2015-10-07 04:34:32 +03:00
|
|
|
// its buffered range.
|
2017-08-29 09:03:19 +03:00
|
|
|
// This will be called should the demuxer be used with MediaSource.
|
2015-05-25 08:09:16 +03:00
|
|
|
virtual void NotifyDataRemoved() { }
|
|
|
|
|
2015-06-12 02:26:58 +03:00
|
|
|
// Indicate to MediaFormatReader if it should compute the start time
|
|
|
|
// of the demuxed data. If true (default) the first sample returned will be
|
|
|
|
// used as reference time base.
|
|
|
|
virtual bool ShouldComputeStartTime() const { return true; }
|
|
|
|
|
2015-05-11 13:54:10 +03:00
|
|
|
protected:
|
|
|
|
virtual ~MediaDataDemuxer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaTrackDemuxer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTrackDemuxer)
|
|
|
|
|
2017-01-27 15:20:37 +03:00
|
|
|
class SamplesHolder
|
|
|
|
{
|
2015-05-11 13:54:10 +03:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SamplesHolder)
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<MediaRawData>> mSamples;
|
2015-05-11 13:54:10 +03:00
|
|
|
private:
|
2017-01-27 15:20:37 +03:00
|
|
|
~SamplesHolder() { }
|
2015-05-11 13:54:10 +03:00
|
|
|
};
|
|
|
|
|
2017-01-27 15:20:37 +03:00
|
|
|
class SkipFailureHolder
|
|
|
|
{
|
2015-05-11 13:54:10 +03:00
|
|
|
public:
|
2016-09-12 05:22:20 +03:00
|
|
|
SkipFailureHolder(const MediaResult& aFailure, uint32_t aSkipped)
|
2015-05-11 13:54:10 +03:00
|
|
|
: mFailure(aFailure)
|
|
|
|
, mSkipped(aSkipped)
|
|
|
|
{}
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaResult mFailure;
|
2015-05-11 13:54:10 +03:00
|
|
|
uint32_t mSkipped;
|
|
|
|
};
|
|
|
|
|
2017-01-27 15:20:37 +03:00
|
|
|
typedef MozPromise<media::TimeUnit, MediaResult, /* IsExclusive = */ true>
|
|
|
|
SeekPromise;
|
|
|
|
typedef MozPromise<RefPtr<SamplesHolder>, MediaResult,
|
|
|
|
/* IsExclusive = */ true>
|
|
|
|
SamplesPromise;
|
|
|
|
typedef MozPromise<uint32_t, SkipFailureHolder, /* IsExclusive = */ true>
|
|
|
|
SkipAccessPointPromise;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Returns the TrackInfo (a.k.a Track Description) for this track.
|
|
|
|
// The TrackInfo returned will be:
|
|
|
|
// TrackInfo::kVideoTrack -> VideoInfo.
|
|
|
|
// TrackInfo::kAudioTrack -> AudioInfo.
|
|
|
|
// respectively.
|
|
|
|
virtual UniquePtr<TrackInfo> GetInfo() const = 0;
|
|
|
|
|
|
|
|
// Seeks to aTime. Upon success, SeekPromise will be resolved with the
|
|
|
|
// actual time seeked to. Typically the random access point time
|
2016-11-13 07:13:51 +03:00
|
|
|
virtual RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) = 0;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Returns the next aNumSamples sample(s) available.
|
|
|
|
// If only a lesser amount of samples is available, only those will be
|
|
|
|
// returned.
|
|
|
|
// A aNumSamples value of -1 indicates to return all remaining samples.
|
|
|
|
// A video sample is typically made of a single video frame while an audio
|
|
|
|
// sample will contains multiple audio frames.
|
2015-10-18 08:24:48 +03:00
|
|
|
virtual RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) = 0;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
2015-06-12 02:26:58 +03:00
|
|
|
// Returns true if a call to GetSamples() may block while waiting on the
|
|
|
|
// underlying resource to return the data.
|
|
|
|
// This is used by the MediaFormatReader to determine if buffering heuristics
|
|
|
|
// should be used.
|
|
|
|
virtual bool GetSamplesMayBlock() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:54:10 +03:00
|
|
|
// Cancel all pending actions (Seek, GetSamples) and reset current state
|
|
|
|
// All pending promises are to be rejected with CANCEL.
|
|
|
|
// The next call to GetSamples would return the first sample available in the
|
|
|
|
// track.
|
|
|
|
virtual void Reset() = 0;
|
|
|
|
|
|
|
|
// Returns timestamp of next random access point or an error if the demuxer
|
|
|
|
// can't report this.
|
|
|
|
virtual nsresult GetNextRandomAccessPoint(media::TimeUnit* aTime)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns timestamp of previous random access point or an error if the
|
|
|
|
// demuxer can't report this.
|
|
|
|
virtual nsresult GetPreviousRandomAccessPoint(media::TimeUnit* aTime)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip frames until the next Random Access Point located after
|
|
|
|
// aTimeThreshold.
|
|
|
|
// The first frame returned by the next call to GetSamples() will be the
|
|
|
|
// first random access point found after aTimeThreshold.
|
|
|
|
// Upon success, returns the number of frames skipped.
|
2016-11-13 07:13:51 +03:00
|
|
|
virtual RefPtr<SkipAccessPointPromise>
|
|
|
|
SkipToNextRandomAccessPoint(const media::TimeUnit& aTimeThreshold) = 0;
|
2015-05-11 13:54:10 +03:00
|
|
|
|
|
|
|
// Gets the resource's offset used for the last Seek() or GetSample().
|
|
|
|
// A negative value indicates that this functionality isn't supported.
|
|
|
|
virtual int64_t GetResourceOffset() const
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual TrackInfo::TrackType GetType() const
|
|
|
|
{
|
|
|
|
return GetInfo()->GetType();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual media::TimeIntervals GetBuffered() = 0;
|
|
|
|
|
2016-04-22 08:48:38 +03:00
|
|
|
// By default, it is assumed that the entire resource can be evicted once
|
|
|
|
// all samples have been demuxed.
|
|
|
|
virtual int64_t GetEvictionOffset(const media::TimeUnit& aTime)
|
|
|
|
{
|
|
|
|
return INT64_MAX;
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:54:10 +03:00
|
|
|
// If the MediaTrackDemuxer and MediaDataDemuxer hold cross references.
|
|
|
|
// BreakCycles must be overridden.
|
|
|
|
virtual void BreakCycles()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-01-27 15:20:37 +03:00
|
|
|
virtual ~MediaTrackDemuxer() { }
|
2015-05-11 13:54:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MediaDataDemuxer_h
|