gecko-dev/dom/media/mediasink/VideoSink.h
Chris Pearce 2be26b6b7c Bug 1443942 - Move code to toggle high res timers into VideoSink. r=jya
We have code in the MDSM to toggle on high resolution timers on Windows when we
start/stop playing because the VideoSink relies on being awoken by timers to
update the set of current frames in the compositor's queue, and on Windows 7 we
end up dropping frames due to the timer lag without this.

We assert in the MDSM's destructor that we've turned off high res timers (as
they cause needless battery drain, so we only want them on when we need them),
and the new test_mediarecorder_principals is hitting that assert on Windows. I
think we're missing turning them off when we create a new VideoSink for
outputting to the MSG. That affects the value returned by
MediaDecoderStateMachine->mVideoSink->IsPlaying(), which is what we use to
decide whether we should enable high resolution timers. We track whether we've
enabled high res timers in MDSM::mHiResTimersRequested, and that gets out of
sync with IsPlaying() when we re-create the MediaSink.

Rather than trying to handle all the permutations of places where we need to
turn off high resolution timers in the MDSM, we're better to move the code to
toggle high res timers into the VideoSink, as that's actually where we need to
be sure that we have high resolution timers enabled anyway. It's the VideoSink
after all that is relying on timers for frame update, not the MDSM.

Also remove the media.hi-res-timers.enabled pref, as we haven't needed it.

MozReview-Commit-ID: 9dNxcYxPDZH

--HG--
extra : rebase_source : 6e403d59bb5f1dd0241fe8298a823ba08b1670fb
2018-04-06 13:33:28 +12:00

171 lines
5.4 KiB
C++

/* -*- 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 VideoSink_h_
#define VideoSink_h_
#include "FrameStatistics.h"
#include "ImageContainer.h"
#include "MediaEventSource.h"
#include "MediaSink.h"
#include "MediaTimer.h"
#include "mozilla/AbstractThread.h"
#include "mozilla/MozPromise.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"
#include "VideoFrameContainer.h"
namespace mozilla {
class VideoFrameContainer;
template <class T> class MediaQueue;
namespace media {
class VideoSink : public MediaSink
{
typedef mozilla::layers::ImageContainer::ProducerID ProducerID;
public:
VideoSink(AbstractThread* aThread,
MediaSink* aAudioSink,
MediaQueue<VideoData>& aVideoQueue,
VideoFrameContainer* aContainer,
FrameStatistics& aFrameStats,
uint32_t aVQueueSentToCompositerSize);
const PlaybackParams& GetPlaybackParams() const override;
void SetPlaybackParams(const PlaybackParams& aParams) override;
RefPtr<GenericPromise> OnEnded(TrackType aType) override;
TimeUnit GetEndTime(TrackType aType) const override;
TimeUnit GetPosition(TimeStamp* aTimeStamp = nullptr) const override;
bool HasUnplayedFrames(TrackType aType) const override;
void SetPlaybackRate(double aPlaybackRate) override;
void SetVolume(double aVolume) override;
void SetPreservesPitch(bool aPreservesPitch) override;
void SetPlaying(bool aPlaying) override;
void Redraw(const VideoInfo& aInfo) override;
void Start(const TimeUnit& aStartTime, const MediaInfo& aInfo) override;
void Stop() override;
bool IsStarted() const override;
bool IsPlaying() const override;
void Shutdown() override;
nsCString GetDebugInfo() override;
private:
virtual ~VideoSink();
// VideoQueue listener related.
void OnVideoQueuePushed(RefPtr<VideoData>&& aSample);
void OnVideoQueueFinished();
void ConnectListener();
void DisconnectListener();
void EnsureHighResTimersOnOnlyIfPlaying();
// Sets VideoQueue images into the VideoFrameContainer. Called on the shared
// state machine thread. The first aMaxFrames (at most) are set.
// aClockTime and aClockTimeStamp are used as the baseline for deriving
// timestamps for the frames; when omitted, aMaxFrames must be 1 and
// a null timestamp is passed to the VideoFrameContainer.
// If the VideoQueue is empty, this does nothing.
void RenderVideoFrames(int32_t aMaxFrames, int64_t aClockTime = 0,
const TimeStamp& aClickTimeStamp = TimeStamp());
// Triggered while videosink is started, videosink becomes "playing" status,
// or VideoQueue event arrived.
void TryUpdateRenderedVideoFrames();
// If we have video, display a video frame if it's time for display has
// arrived, otherwise sleep until it's time for the next frame. Update the
// current frame time as appropriate, and trigger ready state update.
// Called on the shared state machine thread.
void UpdateRenderedVideoFrames();
void UpdateRenderedVideoFramesByTimer();
void MaybeResolveEndPromise();
void AssertOwnerThread() const
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
}
MediaQueue<VideoData>& VideoQueue() const {
return mVideoQueue;
}
const RefPtr<AbstractThread> mOwnerThread;
RefPtr<MediaSink> mAudioSink;
MediaQueue<VideoData>& mVideoQueue;
VideoFrameContainer* mContainer;
// Producer ID to help ImageContainer distinguish different streams of
// FrameIDs. A unique and immutable value per VideoSink.
const ProducerID mProducerID;
// Used to notify MediaDecoder's frame statistics
FrameStatistics& mFrameStats;
RefPtr<GenericPromise> mEndPromise;
MozPromiseHolder<GenericPromise> mEndPromiseHolder;
MozPromiseRequestHolder<GenericPromise> mVideoSinkEndRequest;
// The presentation end time of the last video frame which has been displayed.
TimeUnit mVideoFrameEndTime;
// Event listeners for VideoQueue
MediaEventListener mPushListener;
MediaEventListener mFinishListener;
// True if this sink is going to handle video track.
bool mHasVideo;
// Used to trigger another update of rendered frames in next round.
DelayedScheduler mUpdateScheduler;
// Max frame number sent to compositor at a time.
// Based on the pref value obtained in MDSM.
const uint32_t mVideoQueueSendToCompositorSize;
// Talos tests for the compositor require at least one frame in the
// video queue so that the compositor has something to composit during
// the talos test when the decode is stressed. We have a minimum size
// on the video queue in order to facilitate this talos test.
// Note: Normal playback should not have a queue size of more than 0,
// otherwise A/V sync will be ruined! *Only* make this non-zero for
// testing purposes.
const uint32_t mMinVideoQueueSize;
#ifdef XP_WIN
// Whether we've called timeBeginPeriod(1) to request high resolution
// timers. We request high resolution timers when playback starts, and
// turn them off when playback is paused. Enabling high resolution
// timers can cause higher CPU usage and battery drain on Windows 7,
// but reduces our frame drop rate.
bool mHiResTimersRequested;
#endif
};
} // namespace media
} // namespace mozilla
#endif