2017-06-19 10:50:09 +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/. */
|
|
|
|
|
|
|
|
#ifndef ChannelMediaDecoder_h_
|
|
|
|
#define ChannelMediaDecoder_h_
|
|
|
|
|
|
|
|
#include "MediaDecoder.h"
|
2017-06-20 13:10:56 +03:00
|
|
|
#include "MediaResourceCallback.h"
|
2017-10-04 17:30:32 +03:00
|
|
|
#include "MediaChannelStatistics.h"
|
2017-06-19 10:50:09 +03:00
|
|
|
|
2017-06-21 12:30:56 +03:00
|
|
|
class nsIChannel;
|
2017-06-23 09:12:41 +03:00
|
|
|
class nsIStreamListener;
|
2017-06-21 12:30:56 +03:00
|
|
|
|
2017-06-19 10:50:09 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2017-08-04 09:52:22 +03:00
|
|
|
class BaseMediaResource;
|
|
|
|
|
2017-06-19 10:50:09 +03:00
|
|
|
class ChannelMediaDecoder : public MediaDecoder
|
|
|
|
{
|
2017-06-20 13:10:56 +03:00
|
|
|
// Used to register with MediaResource to receive notifications which will
|
|
|
|
// be forwarded to MediaDecoder.
|
|
|
|
class ResourceCallback : public MediaResourceCallback
|
|
|
|
{
|
|
|
|
// Throttle calls to MediaDecoder::NotifyDataArrived()
|
|
|
|
// to be at most once per 500ms.
|
|
|
|
static const uint32_t sDelay = 500;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ResourceCallback(AbstractThread* aMainThread);
|
|
|
|
// Start to receive notifications from ResourceCallback.
|
|
|
|
void Connect(ChannelMediaDecoder* aDecoder);
|
|
|
|
// Called upon shutdown to stop receiving notifications.
|
|
|
|
void Disconnect();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* MediaResourceCallback functions */
|
2017-09-06 10:12:34 +03:00
|
|
|
AbstractThread* AbstractMainThread() const override;
|
2017-06-20 13:10:56 +03:00
|
|
|
MediaDecoderOwner* GetMediaOwner() const override;
|
2017-11-17 06:07:30 +03:00
|
|
|
void NotifyNetworkError(const MediaResult& aError) override;
|
2017-06-20 13:10:56 +03:00
|
|
|
void NotifyDataArrived() override;
|
|
|
|
void NotifyDataEnded(nsresult aStatus) override;
|
|
|
|
void NotifyPrincipalChanged() override;
|
2017-08-23 09:32:51 +03:00
|
|
|
void NotifySuspendedStatusChanged(bool aSuspendedByCache) override;
|
2017-06-20 13:10:56 +03:00
|
|
|
|
|
|
|
static void TimerCallback(nsITimer* aTimer, void* aClosure);
|
|
|
|
|
|
|
|
// The decoder to send notifications. Main-thread only.
|
|
|
|
ChannelMediaDecoder* mDecoder = nullptr;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
bool mTimerArmed = false;
|
|
|
|
const RefPtr<AbstractThread> mAbstractMainThread;
|
|
|
|
};
|
|
|
|
|
2017-06-30 05:11:42 +03:00
|
|
|
protected:
|
2017-12-01 05:33:44 +03:00
|
|
|
void OnPlaybackEvent(MediaPlaybackEvent&& aEvent) override;
|
2017-08-07 08:23:43 +03:00
|
|
|
void DurationChanged() override;
|
2017-08-09 06:06:29 +03:00
|
|
|
void MetadataLoaded(UniquePtr<MediaInfo> aInfo,
|
|
|
|
UniquePtr<MetadataTags> aTags,
|
|
|
|
MediaDecoderEventVisibility aEventVisibility) override;
|
|
|
|
|
2017-06-20 13:10:56 +03:00
|
|
|
RefPtr<ResourceCallback> mResourceCallback;
|
2017-08-04 09:52:22 +03:00
|
|
|
RefPtr<BaseMediaResource> mResource;
|
2017-06-20 13:10:56 +03:00
|
|
|
|
2017-06-19 10:50:09 +03:00
|
|
|
explicit ChannelMediaDecoder(MediaDecoderInit& aInit);
|
2017-06-20 13:10:27 +03:00
|
|
|
|
2017-10-25 08:57:39 +03:00
|
|
|
nsCString GetDebugInfo() override;
|
|
|
|
|
2017-10-16 23:55:26 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Create a decoder for the given aType. Returns null if we were unable
|
|
|
|
// to create the decoder, for example because the requested MIME type in
|
|
|
|
// the init struct was unsupported.
|
|
|
|
static already_AddRefed<ChannelMediaDecoder> Create(
|
|
|
|
MediaDecoderInit& aInit,
|
|
|
|
DecoderDoctorDiagnostics* aDiagnostics);
|
|
|
|
|
2017-06-20 13:10:56 +03:00
|
|
|
void Shutdown() override;
|
|
|
|
|
2017-08-04 10:29:55 +03:00
|
|
|
bool CanClone();
|
|
|
|
|
2017-06-20 13:10:27 +03:00
|
|
|
// Create a new decoder of the same type as this one.
|
2017-08-03 12:39:55 +03:00
|
|
|
already_AddRefed<ChannelMediaDecoder> Clone(MediaDecoderInit& aInit);
|
2017-06-21 12:30:56 +03:00
|
|
|
|
2017-08-03 12:48:26 +03:00
|
|
|
nsresult Load(nsIChannel* aChannel,
|
|
|
|
bool aIsPrivateBrowsing,
|
|
|
|
nsIStreamListener** aStreamListener);
|
2017-06-23 09:12:41 +03:00
|
|
|
|
2017-08-24 12:35:24 +03:00
|
|
|
void AddSizeOfResources(ResourceSizes* aSizes) override;
|
2017-08-24 11:58:06 +03:00
|
|
|
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override;
|
2017-08-23 19:06:23 +03:00
|
|
|
bool IsTransportSeekable() override;
|
2017-08-07 13:09:56 +03:00
|
|
|
void SetLoadInBackground(bool aLoadInBackground) override;
|
|
|
|
void Suspend() override;
|
|
|
|
void Resume() override;
|
|
|
|
|
2017-06-23 09:12:41 +03:00
|
|
|
private:
|
2017-11-30 06:21:14 +03:00
|
|
|
void DownloadProgressed();
|
2017-08-24 13:08:37 +03:00
|
|
|
void PinForSeek() override;
|
|
|
|
void UnpinForSeek() override;
|
2017-08-17 10:41:22 +03:00
|
|
|
|
2017-08-15 07:37:00 +03:00
|
|
|
// Create a new state machine to run this decoder.
|
|
|
|
MediaDecoderStateMachine* CreateStateMachine();
|
|
|
|
|
2017-08-04 09:52:22 +03:00
|
|
|
nsresult Load(BaseMediaResource* aOriginal);
|
2017-08-04 11:02:40 +03:00
|
|
|
|
|
|
|
// Called by MediaResource when the download has ended.
|
|
|
|
// Called on the main thread only. aStatus is the result from OnStopRequest.
|
|
|
|
void NotifyDownloadEnded(nsresult aStatus);
|
|
|
|
|
|
|
|
// Called by the MediaResource to keep track of the number of bytes read
|
|
|
|
// from the resource. Called on the main by an event runner dispatched
|
|
|
|
// by the MediaResource read functions.
|
|
|
|
void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset);
|
|
|
|
|
2017-08-07 06:48:43 +03:00
|
|
|
bool CanPlayThroughImpl() override final;
|
|
|
|
|
2017-08-17 10:20:40 +03:00
|
|
|
bool IsLiveStream() override final;
|
|
|
|
|
2017-08-07 08:23:43 +03:00
|
|
|
// The actual playback rate computation.
|
|
|
|
void ComputePlaybackRate();
|
|
|
|
|
|
|
|
// Something has changed that could affect the computed playback rate,
|
|
|
|
// so recompute it.
|
|
|
|
void UpdatePlaybackRate();
|
|
|
|
|
|
|
|
// Return statistics. This is used for progress events and other things.
|
|
|
|
// This can be called from any thread. It's only a snapshot of the
|
|
|
|
// current state, since other threads might be changing the state
|
|
|
|
// at any time.
|
|
|
|
MediaStatistics GetStatistics();
|
|
|
|
|
2017-12-01 09:36:35 +03:00
|
|
|
bool ShouldThrottleDownload(const MediaStatistics& aStats);
|
2017-08-07 08:23:43 +03:00
|
|
|
|
|
|
|
// Data needed to estimate playback data rate. The timeline used for
|
|
|
|
// this estimate is "decode time" (where the "current time" is the
|
|
|
|
// time of the last decoded video frame).
|
|
|
|
MediaChannelStatistics mPlaybackStatistics;
|
|
|
|
|
|
|
|
// Estimate of the current playback rate (bytes/second).
|
|
|
|
double mPlaybackBytesPerSecond = 0;
|
|
|
|
|
|
|
|
// True if mPlaybackBytesPerSecond is a reliable estimate.
|
|
|
|
bool mPlaybackRateReliable = true;
|
2017-08-24 13:08:37 +03:00
|
|
|
|
|
|
|
// True when our media stream has been pinned. We pin the stream
|
|
|
|
// while seeking.
|
|
|
|
bool mPinnedForSeek = false;
|
2017-12-01 06:26:03 +03:00
|
|
|
|
|
|
|
// Current playback position in the stream. This is (approximately)
|
|
|
|
// where we're up to playing back the stream. This is not adjusted
|
|
|
|
// during decoder seek operations, but it's updated at the end when we
|
|
|
|
// start playing back again.
|
|
|
|
int64_t mPlaybackPosition = 0;
|
2017-06-19 10:50:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // ChannelMediaDecoder_h_
|