2012-11-19 15:11:21 +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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef AbstractMediaDecoder_h_
|
|
|
|
#define AbstractMediaDecoder_h_
|
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-07-04 03:54:54 +00:00
|
|
|
#include "MediaInfo.h"
|
2012-11-19 15:11:21 +00:00
|
|
|
#include "nsISupports.h"
|
2012-11-30 13:17:54 +00:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-11-19 15:11:21 +00:00
|
|
|
|
|
|
|
namespace mozilla
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace layers
|
|
|
|
{
|
|
|
|
class ImageContainer;
|
|
|
|
}
|
|
|
|
class MediaResource;
|
|
|
|
class ReentrantMonitor;
|
|
|
|
class VideoFrameContainer;
|
2012-11-30 13:17:54 +00:00
|
|
|
class TimedMetadata;
|
2013-05-04 10:12:41 +00:00
|
|
|
class MediaDecoderOwner;
|
2014-08-08 02:44:04 +00:00
|
|
|
#ifdef MOZ_EME
|
2014-07-30 06:53:34 +00:00
|
|
|
class CDMProxy;
|
2014-08-08 02:44:04 +00:00
|
|
|
#endif
|
2012-11-30 13:17:54 +00:00
|
|
|
|
|
|
|
typedef nsDataHashtable<nsCStringHashKey, nsCString> MetadataTags;
|
2012-11-19 15:11:21 +00:00
|
|
|
|
2013-02-01 22:13:23 +00:00
|
|
|
static inline bool IsCurrentThread(nsIThread* aThread) {
|
|
|
|
return NS_GetCurrentThread() == aThread;
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:33:40 +00:00
|
|
|
enum class MediaDecoderEventVisibility : int8_t {
|
|
|
|
Observable,
|
|
|
|
Suppressed
|
|
|
|
};
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
/**
|
|
|
|
* The AbstractMediaDecoder class describes the public interface for a media decoder
|
|
|
|
* and is used by the MediaReader classes.
|
|
|
|
*/
|
|
|
|
class AbstractMediaDecoder : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Returns the monitor for other threads to synchronise access to
|
|
|
|
// state.
|
|
|
|
virtual ReentrantMonitor& GetReentrantMonitor() = 0;
|
|
|
|
|
|
|
|
// Returns true if the decoder is shut down.
|
|
|
|
virtual bool IsShutdown() const = 0;
|
|
|
|
|
|
|
|
virtual bool OnStateMachineThread() const = 0;
|
|
|
|
|
|
|
|
virtual bool OnDecodeThread() const = 0;
|
|
|
|
|
|
|
|
// Get the current MediaResource being used. Its URI will be returned
|
|
|
|
// by currentSrc. Returns what was passed to Load(), if Load() has been called.
|
|
|
|
virtual MediaResource* GetResource() const = 0;
|
|
|
|
|
|
|
|
// Called by the decode thread to keep track of the number of bytes read
|
|
|
|
// from the resource.
|
2013-09-18 03:37:23 +00:00
|
|
|
virtual void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) = 0;
|
2012-11-19 15:11:21 +00:00
|
|
|
|
2015-03-03 04:46:48 +00:00
|
|
|
// Increments the parsed, decoded and dropped frame counters by the passed in
|
|
|
|
// counts.
|
2012-11-19 15:11:21 +00:00
|
|
|
// Can be called on any thread.
|
2015-03-03 04:46:48 +00:00
|
|
|
virtual void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded,
|
|
|
|
uint32_t aDropped) = 0;
|
2012-11-19 15:11:21 +00:00
|
|
|
|
|
|
|
// Return the duration of the media in microseconds.
|
|
|
|
virtual int64_t GetMediaDuration() = 0;
|
|
|
|
|
|
|
|
// Set the duration of the media in microseconds.
|
|
|
|
virtual void SetMediaDuration(int64_t aDuration) = 0;
|
|
|
|
|
2013-05-03 07:48:37 +00:00
|
|
|
// Sets the duration of the media in microseconds. The MediaDecoder
|
|
|
|
// fires a durationchange event to its owner (e.g., an HTML audio
|
|
|
|
// tag).
|
2013-09-10 00:45:33 +00:00
|
|
|
virtual void UpdateEstimatedMediaDuration(int64_t aDuration) = 0;
|
2013-05-03 07:48:37 +00:00
|
|
|
|
2012-11-30 13:17:54 +00:00
|
|
|
// Set the media as being seekable or not.
|
|
|
|
virtual void SetMediaSeekable(bool aMediaSeekable) = 0;
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
virtual VideoFrameContainer* GetVideoFrameContainer() = 0;
|
|
|
|
virtual mozilla::layers::ImageContainer* GetImageContainer() = 0;
|
|
|
|
|
2012-11-30 13:17:54 +00:00
|
|
|
// Return true if the media layer supports seeking.
|
|
|
|
virtual bool IsTransportSeekable() = 0;
|
|
|
|
|
|
|
|
// Return true if the transport layer supports seeking.
|
2012-11-19 15:11:21 +00:00
|
|
|
virtual bool IsMediaSeekable() = 0;
|
|
|
|
|
2015-03-05 01:33:40 +00:00
|
|
|
virtual void MetadataLoaded(nsAutoPtr<MediaInfo> aInfo, nsAutoPtr<MetadataTags> aTags, MediaDecoderEventVisibility aEventVisibility) = 0;
|
2014-11-06 08:17:05 +00:00
|
|
|
virtual void QueueMetadata(int64_t aTime, nsAutoPtr<MediaInfo> aInfo, nsAutoPtr<MetadataTags> aTags) = 0;
|
2015-03-05 01:33:40 +00:00
|
|
|
virtual void FirstFrameLoaded(nsAutoPtr<MediaInfo> aInfo, MediaDecoderEventVisibility aEventVisibility) = 0;
|
2012-11-30 13:17:54 +00:00
|
|
|
|
2014-07-04 03:55:06 +00:00
|
|
|
virtual void RemoveMediaTracks() = 0;
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
// Set the media end time in microseconds
|
|
|
|
virtual void SetMediaEndTime(int64_t aTime) = 0;
|
|
|
|
|
2012-12-06 23:27:08 +00:00
|
|
|
// May be called by the reader to notify this decoder that the metadata from
|
|
|
|
// the media file has been read. Call on the decode thread only.
|
2012-11-19 15:11:21 +00:00
|
|
|
virtual void OnReadMetadataCompleted() = 0;
|
|
|
|
|
2013-05-04 10:12:41 +00:00
|
|
|
// Returns the owner of this media decoder. The owner should only be used
|
|
|
|
// on the main thread.
|
|
|
|
virtual MediaDecoderOwner* GetOwner() = 0;
|
|
|
|
|
2014-03-11 03:44:09 +00:00
|
|
|
// May be called by the reader to notify the decoder that the resources
|
|
|
|
// required to begin playback have been acquired. Can be called on any thread.
|
|
|
|
virtual void NotifyWaitingForResourcesStatusChanged() = 0;
|
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
// Called by the reader's MediaResource as data arrives over the network.
|
|
|
|
// Must be called on the main thread.
|
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) = 0;
|
|
|
|
|
2014-08-18 12:49:51 +00:00
|
|
|
// Set by Reader if the current audio track can be offloaded
|
|
|
|
virtual void SetPlatformCanOffloadAudio(bool aCanOffloadAudio) {}
|
|
|
|
|
|
|
|
// Called by Decoder/State machine to check audio offload condtions are met
|
|
|
|
virtual bool CheckDecoderCanOffloadAudio() { return false; }
|
2014-04-08 15:29:07 +00:00
|
|
|
|
|
|
|
// Called from HTMLMediaElement when owner document activity changes
|
|
|
|
virtual void SetElementVisibility(bool aIsVisible) {}
|
|
|
|
|
2014-11-06 08:08:04 +00:00
|
|
|
// Called by some MediaDecoderReader to determine if we can rely
|
|
|
|
// on the resource length to limit reads.
|
|
|
|
virtual bool HasInitializationData() { return false; }
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
// Stack based class to assist in notifying the frame statistics of
|
|
|
|
// parsed and decoded frames. Use inside video demux & decode functions
|
|
|
|
// to ensure all parsed and decoded frames are reported on all return paths.
|
|
|
|
class AutoNotifyDecoded {
|
|
|
|
public:
|
2015-03-03 04:46:46 +00:00
|
|
|
explicit AutoNotifyDecoded(AbstractMediaDecoder* aDecoder)
|
2015-03-03 04:46:48 +00:00
|
|
|
: mParsed(0), mDecoded(0), mDropped(0), mDecoder(aDecoder) {}
|
2012-11-19 15:11:21 +00:00
|
|
|
~AutoNotifyDecoded() {
|
2015-01-11 20:41:50 +00:00
|
|
|
if (mDecoder) {
|
2015-03-03 04:46:48 +00:00
|
|
|
mDecoder->NotifyDecodedFrames(mParsed, mDecoded, mDropped);
|
2015-01-11 20:41:50 +00:00
|
|
|
}
|
2012-11-19 15:11:21 +00:00
|
|
|
}
|
2015-03-03 04:46:46 +00:00
|
|
|
uint32_t mParsed;
|
|
|
|
uint32_t mDecoded;
|
2015-03-03 04:46:48 +00:00
|
|
|
uint32_t mDropped;
|
2015-03-03 04:46:46 +00:00
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
private:
|
|
|
|
AbstractMediaDecoder* mDecoder;
|
|
|
|
};
|
2014-07-30 06:53:34 +00:00
|
|
|
|
2014-08-08 02:44:04 +00:00
|
|
|
#ifdef MOZ_EME
|
2014-07-30 06:53:34 +00:00
|
|
|
virtual nsresult SetCDMProxy(CDMProxy* aProxy) { return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
virtual CDMProxy* GetCDMProxy() { return nullptr; }
|
2014-08-08 02:44:04 +00:00
|
|
|
#endif
|
2012-11-19 15:11:21 +00:00
|
|
|
};
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
class MetadataContainer
|
2012-11-30 13:17:54 +00:00
|
|
|
{
|
2014-11-06 08:17:05 +00:00
|
|
|
protected:
|
|
|
|
MetadataContainer(AbstractMediaDecoder* aDecoder,
|
|
|
|
nsAutoPtr<MediaInfo> aInfo,
|
2015-01-16 15:56:19 +00:00
|
|
|
nsAutoPtr<MetadataTags> aTags,
|
2015-03-05 01:33:40 +00:00
|
|
|
MediaDecoderEventVisibility aEventVisibility)
|
2014-11-06 08:17:05 +00:00
|
|
|
: mDecoder(aDecoder),
|
|
|
|
mInfo(aInfo),
|
2015-01-16 15:56:19 +00:00
|
|
|
mTags(aTags),
|
2015-03-05 01:33:40 +00:00
|
|
|
mEventVisibility(aEventVisibility)
|
2014-11-06 08:17:05 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
nsRefPtr<AbstractMediaDecoder> mDecoder;
|
|
|
|
nsAutoPtr<MediaInfo> mInfo;
|
|
|
|
nsAutoPtr<MetadataTags> mTags;
|
2015-03-05 01:33:40 +00:00
|
|
|
MediaDecoderEventVisibility mEventVisibility;
|
2014-11-06 08:17:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MetadataEventRunner : public nsRunnable, private MetadataContainer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MetadataEventRunner(AbstractMediaDecoder* aDecoder,
|
|
|
|
nsAutoPtr<MediaInfo> aInfo,
|
2015-01-16 15:56:19 +00:00
|
|
|
nsAutoPtr<MetadataTags> aTags,
|
2015-03-05 01:33:40 +00:00
|
|
|
MediaDecoderEventVisibility aEventVisibility = MediaDecoderEventVisibility::Observable)
|
|
|
|
: MetadataContainer(aDecoder, aInfo, aTags, aEventVisibility)
|
2012-11-30 13:17:54 +00:00
|
|
|
{}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHOD Run() override
|
2012-11-30 13:17:54 +00:00
|
|
|
{
|
2015-03-05 01:33:40 +00:00
|
|
|
mDecoder->MetadataLoaded(mInfo, mTags, mEventVisibility);
|
2012-11-30 13:17:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
class FirstFrameLoadedEventRunner : public nsRunnable, private MetadataContainer
|
2014-11-06 09:52:44 +00:00
|
|
|
{
|
2014-11-06 08:17:05 +00:00
|
|
|
public:
|
|
|
|
FirstFrameLoadedEventRunner(AbstractMediaDecoder* aDecoder,
|
2015-01-16 15:56:19 +00:00
|
|
|
nsAutoPtr<MediaInfo> aInfo,
|
2015-03-05 01:33:40 +00:00
|
|
|
MediaDecoderEventVisibility aEventVisibility = MediaDecoderEventVisibility::Observable)
|
|
|
|
: MetadataContainer(aDecoder, aInfo, nsAutoPtr<MetadataTags>(nullptr), aEventVisibility)
|
2014-11-06 09:52:44 +00:00
|
|
|
{}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHOD Run() override
|
2014-11-06 09:52:44 +00:00
|
|
|
{
|
2015-03-05 01:33:40 +00:00
|
|
|
mDecoder->FirstFrameLoaded(mInfo, mEventVisibility);
|
2014-11-06 09:52:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
class MetadataUpdatedEventRunner : public nsRunnable, private MetadataContainer
|
2014-11-06 09:52:44 +00:00
|
|
|
{
|
2014-11-06 08:17:05 +00:00
|
|
|
public:
|
|
|
|
MetadataUpdatedEventRunner(AbstractMediaDecoder* aDecoder,
|
|
|
|
nsAutoPtr<MediaInfo> aInfo,
|
2015-01-16 15:56:19 +00:00
|
|
|
nsAutoPtr<MetadataTags> aTags,
|
2015-03-05 01:33:40 +00:00
|
|
|
MediaDecoderEventVisibility aEventVisibility = MediaDecoderEventVisibility::Observable)
|
|
|
|
: MetadataContainer(aDecoder, aInfo, aTags, aEventVisibility)
|
2014-11-06 09:52:44 +00:00
|
|
|
{}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHOD Run() override
|
2014-11-06 09:52:44 +00:00
|
|
|
{
|
|
|
|
nsAutoPtr<MediaInfo> info(new MediaInfo());
|
|
|
|
*info = *mInfo;
|
2015-03-05 01:33:40 +00:00
|
|
|
mDecoder->MetadataLoaded(info, mTags, mEventVisibility);
|
|
|
|
mDecoder->FirstFrameLoaded(mInfo, mEventVisibility);
|
2014-11-06 09:52:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-04 03:55:06 +00:00
|
|
|
class RemoveMediaTracksEventRunner : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-09-01 03:50:23 +00:00
|
|
|
explicit RemoveMediaTracksEventRunner(AbstractMediaDecoder* aDecoder)
|
2014-07-04 03:55:06 +00:00
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHOD Run() override
|
2014-07-04 03:55:06 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mDecoder->RemoveMediaTracks();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<AbstractMediaDecoder> mDecoder;
|
|
|
|
};
|
2012-11-30 13:17:54 +00:00
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|