2010-04-02 07:03:07 +04:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2013-05-03 04:39:19 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Include Windows headers required for enabling high precision timers.
|
2013-05-06 13:33:00 +04:00
|
|
|
#include "windows.h"
|
|
|
|
#include "mmsystem.h"
|
2013-05-03 04:39:19 +04:00
|
|
|
#endif
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2012-12-15 03:58:45 +04:00
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2015-03-10 19:59:30 +03:00
|
|
|
#include "MediaTimer.h"
|
2015-08-18 06:55:01 +03:00
|
|
|
#include "mediasink/DecodedAudioDataSink.h"
|
2015-09-02 09:14:40 +03:00
|
|
|
#include "mediasink/AudioSinkWrapper.h"
|
2015-09-07 14:32:04 +03:00
|
|
|
#include "mediasink/DecodedStream.h"
|
2010-04-02 07:03:07 +04:00
|
|
|
#include "nsTArray.h"
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "MediaDecoder.h"
|
|
|
|
#include "MediaDecoderReader.h"
|
2015-01-16 19:50:00 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2010-04-02 07:03:07 +04:00
|
|
|
#include "mozilla/mozalloc.h"
|
2010-04-27 12:53:44 +04:00
|
|
|
#include "VideoUtils.h"
|
2015-05-18 09:15:47 +03:00
|
|
|
#include "TimeUnits.h"
|
2012-01-19 22:30:29 +04:00
|
|
|
#include "nsDeque.h"
|
2012-04-30 07:12:42 +04:00
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "VideoSegment.h"
|
2012-08-21 08:06:46 +04:00
|
|
|
#include "ImageContainer.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsITimer.h"
|
2013-12-18 07:59:11 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "MediaShutdownManager.h"
|
2015-08-03 21:44:10 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
2014-02-18 02:53:53 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2012-11-30 17:17:54 +04:00
|
|
|
#include "prenv.h"
|
2011-09-27 04:25:41 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-02-09 12:04:38 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2014-08-20 11:26:00 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2014-11-26 23:09:00 +03:00
|
|
|
#include "DOMMediaStream.h"
|
2015-07-10 23:49:31 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2014-02-09 12:04:38 +04:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-11-16 07:25:26 +04:00
|
|
|
using namespace mozilla::dom;
|
2015-05-28 22:13:56 +03:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::media;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-04-15 21:12:14 +03:00
|
|
|
#define NS_DispatchToMainThread(...) CompileError_UseAbstractThreadDispatchInstead
|
2015-04-07 22:20:43 +03:00
|
|
|
|
2014-04-23 13:29:04 +04:00
|
|
|
// avoid redefined macro in unified build
|
2015-05-14 05:15:05 +03:00
|
|
|
#undef LOG
|
2014-04-23 13:29:04 +04:00
|
|
|
#undef DECODER_LOG
|
|
|
|
#undef VERBOSE_LOG
|
|
|
|
|
2015-05-14 05:15:05 +03:00
|
|
|
#define LOG(m, l, x, ...) \
|
2015-05-21 23:22:04 +03:00
|
|
|
MOZ_LOG(m, l, ("Decoder=%p " x, mDecoder.get(), ##__VA_ARGS__))
|
2014-08-20 11:26:00 +04:00
|
|
|
#define DECODER_LOG(x, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(gMediaDecoderLog, LogLevel::Debug, x, ##__VA_ARGS__)
|
2015-05-14 05:15:05 +03:00
|
|
|
#define VERBOSE_LOG(x, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(gMediaDecoderLog, LogLevel::Verbose, x, ##__VA_ARGS__)
|
2015-05-14 05:15:05 +03:00
|
|
|
#define SAMPLE_LOG(x, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(gMediaSampleLog, LogLevel::Debug, x, ##__VA_ARGS__)
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2014-08-20 11:26:00 +04:00
|
|
|
// Somehow MSVC doesn't correctly delete the comma before ##__VA_ARGS__
|
|
|
|
// when __VA_ARGS__ expands to nothing. This is a workaround for it.
|
|
|
|
#define DECODER_WARN_HELPER(a, b) NS_WARNING b
|
|
|
|
#define DECODER_WARN(x, ...) \
|
|
|
|
DECODER_WARN_HELPER(0, (nsPrintfCString("Decoder=%p " x, mDecoder.get(), ##__VA_ARGS__).get()))
|
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
// Certain constants get stored as member variables and then adjusted by various
|
|
|
|
// scale factors on a per-decoder basis. We want to make sure to avoid using these
|
|
|
|
// constants directly, so we put them in a namespace.
|
|
|
|
namespace detail {
|
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// If audio queue has less than this many usecs of decoded audio, we won't risk
|
2010-04-27 12:53:44 +04:00
|
|
|
// trying to decode the video, we'll skip decoding video up to the next
|
2010-11-28 23:06:38 +03:00
|
|
|
// keyframe. We may increase this value for an individual decoder if we
|
|
|
|
// encounter video frames which take a long time to decode.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t LOW_AUDIO_USECS = 300000;
|
2010-04-27 12:53:44 +04:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// If more than this many usecs of decoded audio is queued, we'll hold off
|
2010-11-28 23:06:38 +03:00
|
|
|
// decoding more audio. If we increase the low audio threshold (see
|
2011-04-14 02:12:23 +04:00
|
|
|
// LOW_AUDIO_USECS above) we'll also increase this value to ensure it's not
|
2010-11-28 23:06:38 +03:00
|
|
|
// less than the low audio threshold.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int64_t AMPLE_AUDIO_USECS = 1000000;
|
2010-05-13 04:59:42 +04:00
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2014-03-21 02:47:17 +04:00
|
|
|
// When we're only playing audio and we don't have a video stream, we divide
|
|
|
|
// AMPLE_AUDIO_USECS and LOW_AUDIO_USECS by the following value. This reduces
|
|
|
|
// the amount of decoded audio we buffer, reducing our memory usage. We only
|
|
|
|
// need to decode far ahead when we're decoding video using software decoding,
|
|
|
|
// as otherwise a long video decode could cause an audio underrun.
|
|
|
|
const int64_t NO_VIDEO_AMPLE_AUDIO_DIVISOR = 8;
|
|
|
|
|
2010-04-27 12:53:44 +04:00
|
|
|
// If we have fewer than LOW_VIDEO_FRAMES decoded frames, and
|
2014-03-11 07:44:08 +04:00
|
|
|
// we're not "prerolling video", we'll skip the video up to the next keyframe
|
2010-04-27 12:53:44 +04:00
|
|
|
// which is at or after the current playback position.
|
2015-07-03 10:29:30 +03:00
|
|
|
static const uint32_t LOW_VIDEO_FRAMES = 2;
|
2010-04-27 12:53:44 +04:00
|
|
|
|
2014-12-22 06:32:31 +03:00
|
|
|
// Threshold in usecs that used to check if we are low on decoded video.
|
2015-04-08 12:57:18 +03:00
|
|
|
// If the last video frame's end time |mDecodedVideoEndTime| is more than
|
|
|
|
// |LOW_VIDEO_THRESHOLD_USECS*mPlaybackRate| after the current clock in
|
|
|
|
// Advanceframe(), the video decode is lagging, and we skip to next keyframe.
|
|
|
|
static const int32_t LOW_VIDEO_THRESHOLD_USECS = 60000;
|
2014-12-22 06:32:31 +03:00
|
|
|
|
2010-05-31 08:02:00 +04:00
|
|
|
// Arbitrary "frame duration" when playing only audio.
|
2011-04-14 02:12:23 +04:00
|
|
|
static const int AUDIO_DURATION_USECS = 40000;
|
2010-05-31 08:02:00 +04:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// If we increase our "low audio threshold" (see LOW_AUDIO_USECS above), we
|
2010-11-28 23:06:38 +03:00
|
|
|
// use this as a factor in all our calculations. Increasing this will cause
|
|
|
|
// us to be more likely to increase our low audio threshold, and to
|
|
|
|
// increase it by more.
|
|
|
|
static const int THRESHOLD_FACTOR = 2;
|
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
namespace detail {
|
|
|
|
|
2011-03-24 01:28:57 +03:00
|
|
|
// If we have less than this much undecoded data available, we'll consider
|
|
|
|
// ourselves to be running low on undecoded data. We determine how much
|
|
|
|
// undecoded data we have remaining using the reader's GetBuffered()
|
|
|
|
// implementation.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int64_t LOW_DATA_THRESHOLD_USECS = 5000000;
|
2010-11-28 23:06:38 +03:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// LOW_DATA_THRESHOLD_USECS needs to be greater than AMPLE_AUDIO_USECS, otherwise
|
2011-03-24 01:28:57 +03:00
|
|
|
// the skip-to-keyframe logic can activate when we're running low on data.
|
2013-11-11 12:03:59 +04:00
|
|
|
static_assert(LOW_DATA_THRESHOLD_USECS > AMPLE_AUDIO_USECS,
|
|
|
|
"LOW_DATA_THRESHOLD_USECS is too small");
|
2010-11-28 23:06:38 +03:00
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// Amount of excess usecs of data to add in to the "should we buffer" calculation.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t EXHAUSTED_DATA_MARGIN_USECS = 60000;
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// If we enter buffering within QUICK_BUFFER_THRESHOLD_USECS seconds of starting
|
2011-03-24 01:28:57 +03:00
|
|
|
// decoding, we'll enter "quick buffering" mode, which exits a lot sooner than
|
|
|
|
// normal buffering mode. This exists so that if the decode-ahead exhausts the
|
|
|
|
// downloaded data while decode/playback is just starting up (for example
|
|
|
|
// after a seek while the media is still playing, or when playing a media
|
|
|
|
// as soon as it's load started), we won't necessarily stop for 30s and wait
|
|
|
|
// for buffering. We may actually be able to playback in this case, so exit
|
|
|
|
// buffering early and try to play. If it turns out we can't play, we'll fall
|
|
|
|
// back to buffering normally.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t QUICK_BUFFER_THRESHOLD_USECS = 2000000;
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
namespace detail {
|
|
|
|
|
2011-03-24 01:28:57 +03:00
|
|
|
// If we're quick buffering, we'll remain in buffering mode while we have less than
|
2011-04-14 02:12:23 +04:00
|
|
|
// QUICK_BUFFERING_LOW_DATA_USECS of decoded data available.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t QUICK_BUFFERING_LOW_DATA_USECS = 1000000;
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// If QUICK_BUFFERING_LOW_DATA_USECS is > AMPLE_AUDIO_USECS, we won't exit
|
2011-03-24 01:28:57 +03:00
|
|
|
// quick buffering in a timely fashion, as the decode pauses when it
|
2011-04-14 02:12:23 +04:00
|
|
|
// reaches AMPLE_AUDIO_USECS decoded data, and thus we'll never reach
|
|
|
|
// QUICK_BUFFERING_LOW_DATA_USECS.
|
2015-01-22 08:53:04 +03:00
|
|
|
static_assert(QUICK_BUFFERING_LOW_DATA_USECS <= AMPLE_AUDIO_USECS,
|
2013-11-11 12:03:59 +04:00
|
|
|
"QUICK_BUFFERING_LOW_DATA_USECS is too large");
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2015-01-22 08:53:04 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static TimeDuration UsecsToDuration(int64_t aUsecs) {
|
2014-10-28 01:31:00 +03:00
|
|
|
return TimeDuration::FromMicroseconds(aUsecs);
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int64_t DurationToUsecs(TimeDuration aDuration) {
|
|
|
|
return static_cast<int64_t>(aDuration.ToSeconds() * USECS_PER_S);
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2015-02-01 23:28:41 +03:00
|
|
|
static const uint32_t MIN_VIDEO_QUEUE_SIZE = 3;
|
|
|
|
static const uint32_t MAX_VIDEO_QUEUE_SIZE = 10;
|
2015-06-15 06:24:57 +03:00
|
|
|
static const uint32_t VIDEO_QUEUE_SEND_TO_COMPOSITOR_SIZE = 9999;
|
2015-02-01 23:28:41 +03:00
|
|
|
|
|
|
|
static uint32_t sVideoQueueDefaultSize = MAX_VIDEO_QUEUE_SIZE;
|
|
|
|
static uint32_t sVideoQueueHWAccelSize = MIN_VIDEO_QUEUE_SIZE;
|
2015-06-15 06:24:57 +03:00
|
|
|
static uint32_t sVideoQueueSendToCompositorSize = VIDEO_QUEUE_SEND_TO_COMPOSITOR_SIZE;
|
2015-02-01 23:28:41 +03:00
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaDecoderStateMachine::MediaDecoderStateMachine(MediaDecoder* aDecoder,
|
2012-11-28 06:34:53 +04:00
|
|
|
MediaDecoderReader* aReader,
|
|
|
|
bool aRealTime) :
|
2010-04-02 07:03:07 +04:00
|
|
|
mDecoder(aDecoder),
|
2015-07-16 21:13:49 +03:00
|
|
|
mTaskQueue(new TaskQueue(GetMediaThreadPool(MediaThreadType::PLAYBACK),
|
|
|
|
/* aSupportsTailDispatch = */ true)),
|
2015-05-01 02:21:14 +03:00
|
|
|
mWatchManager(this, mTaskQueue),
|
2015-07-03 10:33:56 +03:00
|
|
|
mProducerID(ImageContainer::AllocateProducerID()),
|
2015-03-10 19:59:30 +03:00
|
|
|
mRealTime(aRealTime),
|
|
|
|
mDispatchedStateMachine(false),
|
|
|
|
mDelayedScheduler(this),
|
2015-04-27 21:51:46 +03:00
|
|
|
mState(DECODER_STATE_DECODING_NONE, "MediaDecoderStateMachine::mState"),
|
2015-07-03 10:33:56 +03:00
|
|
|
mCurrentFrameID(0),
|
2015-06-03 05:01:55 +03:00
|
|
|
mObservedDuration(TimeUnit(), "MediaDecoderStateMachine::mObservedDuration"),
|
2011-08-25 03:42:23 +04:00
|
|
|
mFragmentEndTime(-1),
|
2010-05-06 06:31:02 +04:00
|
|
|
mReader(aReader),
|
2014-11-21 00:24:00 +03:00
|
|
|
mDecodedAudioEndTime(-1),
|
2010-05-31 08:02:00 +04:00
|
|
|
mVideoFrameEndTime(-1),
|
2014-12-22 06:32:31 +03:00
|
|
|
mDecodedVideoEndTime(-1),
|
2012-11-22 14:38:28 +04:00
|
|
|
mPlaybackRate(1.0),
|
2015-01-22 08:53:04 +03:00
|
|
|
mLowAudioThresholdUsecs(detail::LOW_AUDIO_USECS),
|
|
|
|
mAmpleAudioThresholdUsecs(detail::AMPLE_AUDIO_USECS),
|
2015-01-22 08:53:04 +03:00
|
|
|
mQuickBufferingLowDataThresholdUsecs(detail::QUICK_BUFFERING_LOW_DATA_USECS),
|
2015-01-13 14:56:38 +03:00
|
|
|
mIsAudioPrerolling(false),
|
|
|
|
mIsVideoPrerolling(false),
|
2012-04-30 07:12:42 +04:00
|
|
|
mAudioCaptured(false),
|
2011-09-30 03:34:37 +04:00
|
|
|
mPositionChangeQueued(false),
|
2015-04-27 21:51:46 +03:00
|
|
|
mAudioCompleted(false, "MediaDecoderStateMachine::mAudioCompleted"),
|
2015-06-08 20:56:00 +03:00
|
|
|
mNotifyMetadataBeforeFirstFrame(false),
|
2014-02-18 02:53:52 +04:00
|
|
|
mDispatchedEventToDecode(false),
|
2011-09-30 03:34:37 +04:00
|
|
|
mQuickBuffering(false),
|
2014-04-01 07:43:57 +04:00
|
|
|
mMinimizePreroll(false),
|
2011-09-30 03:34:37 +04:00
|
|
|
mDecodeThreadWaiting(false),
|
2014-06-18 09:07:02 +04:00
|
|
|
mDropAudioUntilNextDiscontinuity(false),
|
|
|
|
mDropVideoUntilNextDiscontinuity(false),
|
|
|
|
mDecodeToSeekTarget(false),
|
|
|
|
mCurrentTimeBeforeSeek(0),
|
2015-09-04 03:37:31 +03:00
|
|
|
mCorruptFrames(60),
|
2015-07-25 11:25:13 +03:00
|
|
|
mDecodingFirstFrame(true),
|
2015-01-16 18:56:19 +03:00
|
|
|
mSentLoadedMetadataEvent(false),
|
2015-04-08 02:20:20 +03:00
|
|
|
mSentFirstFrameLoadedEvent(false),
|
2015-07-06 06:36:26 +03:00
|
|
|
mSentPlaybackEndedEvent(false),
|
2015-09-07 14:32:16 +03:00
|
|
|
mStreamSink(new DecodedStream(mTaskQueue, mAudioQueue, mVideoQueue)),
|
2015-07-20 09:29:10 +03:00
|
|
|
mResource(aDecoder->GetResource()),
|
2015-07-17 08:00:26 +03:00
|
|
|
mBuffered(mTaskQueue, TimeIntervals(),
|
|
|
|
"MediaDecoderStateMachine::mBuffered (Mirror)"),
|
|
|
|
mEstimatedDuration(mTaskQueue, NullableTimeUnit(),
|
|
|
|
"MediaDecoderStateMachine::mEstimatedDuration (Mirror)"),
|
|
|
|
mExplicitDuration(mTaskQueue, Maybe<double>(),
|
|
|
|
"MediaDecoderStateMachine::mExplicitDuration (Mirror)"),
|
|
|
|
mPlayState(mTaskQueue, MediaDecoder::PLAY_STATE_LOADING,
|
|
|
|
"MediaDecoderStateMachine::mPlayState (Mirror)"),
|
|
|
|
mNextPlayState(mTaskQueue, MediaDecoder::PLAY_STATE_PAUSED,
|
|
|
|
"MediaDecoderStateMachine::mNextPlayState (Mirror)"),
|
|
|
|
mLogicallySeeking(mTaskQueue, false,
|
|
|
|
"MediaDecoderStateMachine::mLogicallySeeking (Mirror)"),
|
|
|
|
mVolume(mTaskQueue, 1.0, "MediaDecoderStateMachine::mVolume (Mirror)"),
|
|
|
|
mLogicalPlaybackRate(mTaskQueue, 1.0,
|
|
|
|
"MediaDecoderStateMachine::mLogicalPlaybackRate (Mirror)"),
|
|
|
|
mPreservesPitch(mTaskQueue, true,
|
|
|
|
"MediaDecoderStateMachine::mPreservesPitch (Mirror)"),
|
2015-08-06 13:05:30 +03:00
|
|
|
mSameOriginMedia(mTaskQueue, false,
|
|
|
|
"MediaDecoderStateMachine::mSameOriginMedia (Mirror)"),
|
2015-07-17 08:00:26 +03:00
|
|
|
mDuration(mTaskQueue, NullableTimeUnit(),
|
|
|
|
"MediaDecoderStateMachine::mDuration (Canonical"),
|
|
|
|
mIsShutdown(mTaskQueue, false,
|
|
|
|
"MediaDecoderStateMachine::mIsShutdown (Canonical)"),
|
|
|
|
mNextFrameStatus(mTaskQueue, MediaDecoderOwner::NEXT_FRAME_UNINITIALIZED,
|
|
|
|
"MediaDecoderStateMachine::mNextFrameStatus (Canonical)"),
|
|
|
|
mCurrentPosition(mTaskQueue, 0,
|
|
|
|
"MediaDecoderStateMachine::mCurrentPosition (Canonical)")
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2012-11-14 23:46:40 +04:00
|
|
|
MOZ_COUNT_CTOR(MediaDecoderStateMachine);
|
2011-07-12 07:39:34 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2011-11-08 05:38:17 +04:00
|
|
|
|
2015-05-01 01:46:49 +03:00
|
|
|
// Dispatch initialization that needs to happen on that task queue.
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(this, &MediaDecoderStateMachine::InitializationTask);
|
|
|
|
mTaskQueue->Dispatch(r.forget());
|
2015-04-27 21:51:46 +03:00
|
|
|
|
2015-02-01 23:28:41 +03:00
|
|
|
static bool sPrefCacheInit = false;
|
|
|
|
if (!sPrefCacheInit) {
|
|
|
|
sPrefCacheInit = true;
|
|
|
|
Preferences::AddUintVarCache(&sVideoQueueDefaultSize,
|
|
|
|
"media.video-queue.default-size",
|
|
|
|
MAX_VIDEO_QUEUE_SIZE);
|
|
|
|
Preferences::AddUintVarCache(&sVideoQueueHWAccelSize,
|
|
|
|
"media.video-queue.hw-accel-size",
|
|
|
|
MIN_VIDEO_QUEUE_SIZE);
|
2015-06-15 06:24:57 +03:00
|
|
|
Preferences::AddUintVarCache(&sVideoQueueSendToCompositorSize,
|
|
|
|
"media.video-queue.send-to-compositor-size",
|
|
|
|
VIDEO_QUEUE_SEND_TO_COMPOSITOR_SIZE);
|
2015-02-01 23:28:41 +03:00
|
|
|
}
|
2014-03-11 07:44:08 +04:00
|
|
|
|
2015-03-09 02:16:58 +03:00
|
|
|
mBufferingWait = IsRealTime() ? 0 : 15;
|
2015-01-22 08:53:04 +03:00
|
|
|
mLowDataThresholdUsecs = IsRealTime() ? 0 : detail::LOW_DATA_THRESHOLD_USECS;
|
2012-09-28 21:34:03 +04:00
|
|
|
|
2013-05-03 04:39:19 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Ensure high precision timers are enabled on Windows, otherwise the state
|
2015-03-18 00:23:12 +03:00
|
|
|
// machine isn't woken up at reliable intervals to set the next frame,
|
2013-05-03 04:39:19 +04:00
|
|
|
// and we drop frames while painting. Note that multiple calls to this
|
|
|
|
// function per-process is OK, provided each call is matched by a corresponding
|
|
|
|
// timeEndPeriod() call.
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
#endif
|
2015-06-10 05:55:56 +03:00
|
|
|
|
2015-08-06 05:14:15 +03:00
|
|
|
mAudioQueueListener = AudioQueue().PopEvent().Connect(
|
|
|
|
mTaskQueue, this, &MediaDecoderStateMachine::OnAudioPopped);
|
|
|
|
mVideoQueueListener = VideoQueue().PopEvent().Connect(
|
|
|
|
mTaskQueue, this, &MediaDecoderStateMachine::OnVideoPopped);
|
2015-09-02 09:14:40 +03:00
|
|
|
|
2015-09-07 06:38:34 +03:00
|
|
|
mMetadataManager.Connect(mReader->TimedMetadataEvent(), OwnerThread());
|
|
|
|
|
2015-09-09 05:12:35 +03:00
|
|
|
mMediaSink = CreateAudioSink();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaDecoderStateMachine::~MediaDecoderStateMachine()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2014-02-18 02:53:53 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
|
2012-11-14 23:46:40 +04:00
|
|
|
MOZ_COUNT_DTOR(MediaDecoderStateMachine);
|
2014-02-18 02:53:52 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mReader = nullptr;
|
2013-01-24 16:38:32 +04:00
|
|
|
|
2013-05-03 04:39:19 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
timeEndPeriod(1);
|
|
|
|
#endif
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2015-05-01 01:46:49 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::InitializationTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
// Connect mirrors.
|
2015-06-18 00:22:10 +03:00
|
|
|
mBuffered.Connect(mReader->CanonicalBuffered());
|
2015-06-03 01:29:50 +03:00
|
|
|
mEstimatedDuration.Connect(mDecoder->CanonicalEstimatedDuration());
|
2015-06-03 00:44:26 +03:00
|
|
|
mExplicitDuration.Connect(mDecoder->CanonicalExplicitDuration());
|
2015-05-01 01:46:49 +03:00
|
|
|
mPlayState.Connect(mDecoder->CanonicalPlayState());
|
|
|
|
mNextPlayState.Connect(mDecoder->CanonicalNextPlayState());
|
2015-05-07 18:19:46 +03:00
|
|
|
mLogicallySeeking.Connect(mDecoder->CanonicalLogicallySeeking());
|
2015-04-30 03:27:32 +03:00
|
|
|
mVolume.Connect(mDecoder->CanonicalVolume());
|
2015-05-01 21:39:42 +03:00
|
|
|
mLogicalPlaybackRate.Connect(mDecoder->CanonicalPlaybackRate());
|
2015-05-02 00:27:29 +03:00
|
|
|
mPreservesPitch.Connect(mDecoder->CanonicalPreservesPitch());
|
2015-08-06 13:05:30 +03:00
|
|
|
mSameOriginMedia.Connect(mDecoder->CanonicalSameOriginMedia());
|
2015-05-01 01:46:49 +03:00
|
|
|
|
|
|
|
// Initialize watchers.
|
2015-06-18 00:22:10 +03:00
|
|
|
mWatchManager.Watch(mBuffered, &MediaDecoderStateMachine::BufferedRangeUpdated);
|
2015-05-01 01:46:49 +03:00
|
|
|
mWatchManager.Watch(mState, &MediaDecoderStateMachine::UpdateNextFrameStatus);
|
|
|
|
mWatchManager.Watch(mAudioCompleted, &MediaDecoderStateMachine::UpdateNextFrameStatus);
|
2015-04-30 03:27:32 +03:00
|
|
|
mWatchManager.Watch(mVolume, &MediaDecoderStateMachine::VolumeChanged);
|
2015-05-01 21:39:42 +03:00
|
|
|
mWatchManager.Watch(mLogicalPlaybackRate, &MediaDecoderStateMachine::LogicalPlaybackRateChanged);
|
2015-05-02 00:27:29 +03:00
|
|
|
mWatchManager.Watch(mPreservesPitch, &MediaDecoderStateMachine::PreservesPitchChanged);
|
2015-06-03 01:29:50 +03:00
|
|
|
mWatchManager.Watch(mEstimatedDuration, &MediaDecoderStateMachine::RecomputeDuration);
|
2015-06-03 00:44:26 +03:00
|
|
|
mWatchManager.Watch(mExplicitDuration, &MediaDecoderStateMachine::RecomputeDuration);
|
2015-06-03 05:01:55 +03:00
|
|
|
mWatchManager.Watch(mObservedDuration, &MediaDecoderStateMachine::RecomputeDuration);
|
2015-05-07 18:19:33 +03:00
|
|
|
mWatchManager.Watch(mPlayState, &MediaDecoderStateMachine::PlayStateChanged);
|
2015-05-07 18:19:46 +03:00
|
|
|
mWatchManager.Watch(mLogicallySeeking, &MediaDecoderStateMachine::LogicallySeekingChanged);
|
2015-08-13 04:22:59 +03:00
|
|
|
mWatchManager.Watch(mSameOriginMedia, &MediaDecoderStateMachine::SameOriginMediaChanged);
|
2015-08-31 05:50:54 +03:00
|
|
|
|
|
|
|
// Propagate mSameOriginMedia to mDecodedStream.
|
|
|
|
SameOriginMediaChanged();
|
2015-05-01 01:46:49 +03:00
|
|
|
}
|
|
|
|
|
2015-09-09 05:12:35 +03:00
|
|
|
media::MediaSink*
|
|
|
|
MediaDecoderStateMachine::CreateAudioSink()
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaDecoderStateMachine> self = this;
|
|
|
|
auto audioSinkCreator = [self] () {
|
|
|
|
MOZ_ASSERT(self->OnTaskQueue());
|
|
|
|
return new DecodedAudioDataSink(
|
|
|
|
self->mAudioQueue, self->GetMediaTime(),
|
|
|
|
self->mInfo.mAudio, self->mDecoder->GetAudioChannel());
|
|
|
|
};
|
|
|
|
return new AudioSinkWrapper(mTaskQueue, audioSinkCreator);
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:32:33 +03:00
|
|
|
bool MediaDecoderStateMachine::HasFutureAudio()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2010-09-15 03:24:47 +04:00
|
|
|
NS_ASSERTION(HasAudio(), "Should only call HasFutureAudio() when we have audio");
|
|
|
|
// We've got audio ready to play if:
|
|
|
|
// 1. We've not completed playback of audio, and
|
|
|
|
// 2. we either have more than the threshold of decoded audio available, or
|
|
|
|
// we've completely decoded all audio (but not finished playing it yet
|
|
|
|
// as per 1).
|
|
|
|
return !mAudioCompleted &&
|
2014-08-18 12:09:19 +04:00
|
|
|
(AudioDecodedUsecs() >
|
|
|
|
mLowAudioThresholdUsecs * mPlaybackRate ||
|
|
|
|
AudioQueue().IsFinished());
|
2010-05-13 04:59:42 +04:00
|
|
|
}
|
|
|
|
|
2015-05-06 07:32:33 +03:00
|
|
|
bool MediaDecoderStateMachine::HaveNextFrameData()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2010-09-15 03:24:47 +04:00
|
|
|
return (!HasAudio() || HasFutureAudio()) &&
|
2015-07-03 10:29:30 +03:00
|
|
|
(!HasVideo() || VideoQueue().GetSize() > 1);
|
2010-05-13 04:59:42 +04:00
|
|
|
}
|
|
|
|
|
2015-05-06 07:32:33 +03:00
|
|
|
int64_t MediaDecoderStateMachine::GetDecodedAudioDuration()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-04-28 05:12:50 +04:00
|
|
|
int64_t audioDecoded = AudioQueue().Duration();
|
2015-09-09 05:12:23 +03:00
|
|
|
if (mMediaSink->IsStarted()) {
|
2015-07-16 05:12:52 +03:00
|
|
|
audioDecoded += AudioEndTime() - GetMediaTime();
|
2011-01-13 04:06:15 +03:00
|
|
|
}
|
|
|
|
return audioDecoded;
|
|
|
|
}
|
|
|
|
|
2015-08-24 05:04:21 +03:00
|
|
|
void MediaDecoderStateMachine::DiscardStreamData()
|
2012-04-30 07:12:42 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-04-30 07:12:42 +04:00
|
|
|
|
2015-02-02 08:53:54 +03:00
|
|
|
const auto clockTime = GetClock();
|
|
|
|
while (true) {
|
2015-07-27 19:21:33 +03:00
|
|
|
const MediaData* a = AudioQueue().PeekFront();
|
|
|
|
|
2015-02-02 08:53:54 +03:00
|
|
|
// If we discard audio samples fed to the stream immediately, we will
|
|
|
|
// keep decoding audio samples till the end and consume a lot of memory.
|
|
|
|
// Therefore we only discard those behind the stream clock to throttle
|
|
|
|
// the decoding speed.
|
2015-08-20 08:02:43 +03:00
|
|
|
// Note we don't discard a sample when |a->mTime == clockTime| because that
|
|
|
|
// will discard the 1st sample when clockTime is still 0.
|
|
|
|
if (a && a->mTime < clockTime) {
|
2015-07-27 19:21:33 +03:00
|
|
|
nsRefPtr<MediaData> releaseMe = AudioQueue().PopFront();
|
2015-02-02 08:53:54 +03:00
|
|
|
continue;
|
2012-04-30 07:12:42 +04:00
|
|
|
}
|
2015-02-02 08:53:54 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-04-30 07:12:42 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
bool MediaDecoderStateMachine::HaveEnoughDecodedAudio(int64_t aAmpleAudioUSecs)
|
2012-04-30 07:12:42 +04:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-04-30 07:12:42 +04:00
|
|
|
|
2014-04-28 05:12:50 +04:00
|
|
|
if (AudioQueue().GetSize() == 0 ||
|
2012-04-30 07:12:42 +04:00
|
|
|
GetDecodedAudioDuration() < aAmpleAudioUSecs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-31 09:54:05 +03:00
|
|
|
// MDSM will ensure buffering level is high enough for playback speed at 1x
|
|
|
|
// at which the DecodedStream is playing.
|
2015-07-05 03:24:09 +03:00
|
|
|
return true;
|
2012-04-30 07:12:42 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
bool MediaDecoderStateMachine::HaveEnoughDecodedVideo()
|
2012-04-30 07:12:42 +04:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-04-30 07:12:42 +04:00
|
|
|
|
2015-07-03 10:29:30 +03:00
|
|
|
if (VideoQueue().GetSize() - 1 < GetAmpleVideoFrames() * mPlaybackRate) {
|
2012-04-30 07:12:42 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-05 03:24:09 +03:00
|
|
|
return true;
|
2012-04-30 07:12:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::NeedToDecodeVideo()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-06-10 06:56:27 +03:00
|
|
|
SAMPLE_LOG("NeedToDecodeVideo() isDec=%d decToTar=%d minPrl=%d seek=%d enufVid=%d",
|
|
|
|
IsVideoDecoding(), mDecodeToSeekTarget, mMinimizePreroll,
|
|
|
|
mState == DECODER_STATE_SEEKING,
|
|
|
|
HaveEnoughDecodedVideo());
|
2014-06-18 09:07:02 +04:00
|
|
|
return IsVideoDecoding() &&
|
|
|
|
((mState == DECODER_STATE_SEEKING && mDecodeToSeekTarget) ||
|
2015-07-22 09:28:14 +03:00
|
|
|
(IsDecodingFirstFrame() &&
|
2015-01-16 07:48:40 +03:00
|
|
|
IsVideoDecoding() && VideoQueue().GetSize() == 0) ||
|
2014-06-18 09:07:02 +04:00
|
|
|
(!mMinimizePreroll && !HaveEnoughDecodedVideo()));
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-12-22 06:32:31 +03:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::NeedToSkipToNextKeyframe()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-12-22 06:32:31 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-07-22 09:28:14 +03:00
|
|
|
if (IsDecodingFirstFrame()) {
|
2015-01-10 04:20:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-22 06:32:31 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_DECODING ||
|
|
|
|
mState == DECODER_STATE_BUFFERING ||
|
|
|
|
mState == DECODER_STATE_SEEKING);
|
|
|
|
|
2015-09-10 05:19:42 +03:00
|
|
|
// Since GetClock() can only be called after starting MediaSink, we return
|
|
|
|
// false quickly if it is not started because we won't fall behind playback
|
|
|
|
// when not consuming media data.
|
|
|
|
if (!mMediaSink->IsStarted()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-22 06:32:31 +03:00
|
|
|
// We are in seeking or buffering states, don't skip frame.
|
|
|
|
if (!IsVideoDecoding() || mState == DECODER_STATE_BUFFERING ||
|
|
|
|
mState == DECODER_STATE_SEEKING) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't skip frame for video-only decoded stream because the clock time of
|
|
|
|
// the stream relies on the video frame.
|
2015-03-03 05:43:38 +03:00
|
|
|
if (mAudioCaptured && !HasAudio()) {
|
2014-12-22 06:32:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:57:18 +03:00
|
|
|
// We'll skip the video decode to the next keyframe if we're low on
|
2014-12-22 06:32:31 +03:00
|
|
|
// audio, or if we're low on video, provided we're not running low on
|
|
|
|
// data to decode. If we're running low on downloaded data to decode,
|
|
|
|
// we won't start keyframe skipping, as we'll be pausing playback to buffer
|
|
|
|
// soon anyway and we'll want to be able to display frames immediately
|
2015-01-13 12:31:03 +03:00
|
|
|
// after buffering finishes. We ignore the low audio calculations for
|
|
|
|
// readers that are async, as since their audio decode runs on a different
|
|
|
|
// task queue it should never run low and skipping won't help their decode.
|
|
|
|
bool isLowOnDecodedAudio = !mReader->IsAsync() &&
|
|
|
|
!mIsAudioPrerolling && IsAudioDecoding() &&
|
2014-12-22 06:32:31 +03:00
|
|
|
(GetDecodedAudioDuration() <
|
|
|
|
mLowAudioThresholdUsecs * mPlaybackRate);
|
|
|
|
bool isLowOnDecodedVideo = !mIsVideoPrerolling &&
|
2015-04-08 12:57:18 +03:00
|
|
|
((GetClock() - mDecodedVideoEndTime) * mPlaybackRate >
|
|
|
|
LOW_VIDEO_THRESHOLD_USECS);
|
2015-01-13 12:31:03 +03:00
|
|
|
bool lowUndecoded = HasLowUndecodedData();
|
2015-04-08 12:57:18 +03:00
|
|
|
|
2015-01-13 12:31:03 +03:00
|
|
|
if ((isLowOnDecodedAudio || isLowOnDecodedVideo) && !lowUndecoded) {
|
|
|
|
DECODER_LOG("Skipping video decode to the next keyframe lowAudio=%d lowVideo=%d lowUndecoded=%d async=%d",
|
|
|
|
isLowOnDecodedAudio, isLowOnDecodedVideo, lowUndecoded, mReader->IsAsync());
|
2014-12-22 06:32:31 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::NeedToDecodeAudio()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-07-20 04:54:00 +04:00
|
|
|
SAMPLE_LOG("NeedToDecodeAudio() isDec=%d decToTar=%d minPrl=%d seek=%d enufAud=%d",
|
|
|
|
IsAudioDecoding(), mDecodeToSeekTarget, mMinimizePreroll,
|
|
|
|
mState == DECODER_STATE_SEEKING,
|
|
|
|
HaveEnoughDecodedAudio(mAmpleAudioThresholdUsecs * mPlaybackRate));
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
return IsAudioDecoding() &&
|
|
|
|
((mState == DECODER_STATE_SEEKING && mDecodeToSeekTarget) ||
|
2015-07-22 09:28:14 +03:00
|
|
|
(IsDecodingFirstFrame() &&
|
2015-01-16 07:48:40 +03:00
|
|
|
IsAudioDecoding() && AudioQueue().GetSize() == 0) ||
|
2014-06-18 09:07:02 +04:00
|
|
|
(!mMinimizePreroll &&
|
|
|
|
!HaveEnoughDecodedAudio(mAmpleAudioThresholdUsecs * mPlaybackRate) &&
|
|
|
|
(mState != DECODER_STATE_SEEKING || mDecodeToSeekTarget)));
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::IsAudioSeekComplete()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
SAMPLE_LOG("IsAudioSeekComplete() curTarVal=%d mAudDis=%d aqFin=%d aqSz=%d",
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.Exists(), mDropAudioUntilNextDiscontinuity, AudioQueue().IsFinished(), AudioQueue().GetSize());
|
2014-06-18 09:07:02 +04:00
|
|
|
return
|
|
|
|
!HasAudio() ||
|
2015-03-03 03:46:32 +03:00
|
|
|
(mCurrentSeek.Exists() &&
|
2014-06-18 09:07:02 +04:00
|
|
|
!mDropAudioUntilNextDiscontinuity &&
|
|
|
|
(AudioQueue().IsFinished() || AudioQueue().GetSize() > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::IsVideoSeekComplete()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
SAMPLE_LOG("IsVideoSeekComplete() curTarVal=%d mVidDis=%d vqFin=%d vqSz=%d",
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.Exists(), mDropVideoUntilNextDiscontinuity, VideoQueue().IsFinished(), VideoQueue().GetSize());
|
2014-06-18 09:07:02 +04:00
|
|
|
return
|
|
|
|
!HasVideo() ||
|
2015-03-03 03:46:32 +03:00
|
|
|
(mCurrentSeek.Exists() &&
|
2014-06-18 09:07:02 +04:00
|
|
|
!mDropVideoUntilNextDiscontinuity &&
|
|
|
|
(VideoQueue().IsFinished() || VideoQueue().GetSize() > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::OnAudioDecoded(AudioData* aAudioSample)
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<AudioData> audio(aAudioSample);
|
2014-06-18 09:07:02 +04:00
|
|
|
MOZ_ASSERT(audio);
|
2015-02-04 07:09:55 +03:00
|
|
|
mAudioDataRequest.Complete();
|
2015-06-07 01:01:23 +03:00
|
|
|
aAudioSample->AdjustForStartTime(StartTime());
|
2014-11-21 00:24:00 +03:00
|
|
|
mDecodedAudioEndTime = audio->GetEndTime();
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
SAMPLE_LOG("OnAudioDecoded [%lld,%lld] disc=%d",
|
|
|
|
(audio ? audio->mTime : -1),
|
|
|
|
(audio ? audio->GetEndTime() : -1),
|
|
|
|
(audio ? audio->mDiscontinuity : 0));
|
|
|
|
|
|
|
|
switch (mState) {
|
2014-12-30 10:16:48 +03:00
|
|
|
case DECODER_STATE_BUFFERING: {
|
2014-12-22 11:20:31 +03:00
|
|
|
// If we're buffering, this may be the sample we need to stop buffering.
|
2014-12-30 10:16:48 +03:00
|
|
|
// Save it and schedule the state machine.
|
|
|
|
Push(audio);
|
2014-12-22 11:20:31 +03:00
|
|
|
ScheduleStateMachine();
|
2014-12-30 10:16:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
case DECODER_STATE_DECODING: {
|
2014-11-20 00:01:10 +03:00
|
|
|
Push(audio);
|
2015-07-25 11:25:13 +03:00
|
|
|
if (MaybeFinishDecodeFirstFrame()) {
|
2015-07-22 09:28:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-12-30 10:16:48 +03:00
|
|
|
if (mIsAudioPrerolling && DonePrerollingAudio()) {
|
|
|
|
StopPrerollingAudio();
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-10 11:31:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
case DECODER_STATE_SEEKING: {
|
2015-03-03 03:46:32 +03:00
|
|
|
if (!mCurrentSeek.Exists()) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// We've received a sample from a previous decode. Discard it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (audio->mDiscontinuity) {
|
|
|
|
mDropAudioUntilNextDiscontinuity = false;
|
|
|
|
}
|
|
|
|
if (!mDropAudioUntilNextDiscontinuity) {
|
|
|
|
// We must be after the discontinuity; we're receiving samples
|
|
|
|
// at or after the seek target.
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mCurrentSeek.mTarget.mType == SeekTarget::PrevSyncPoint &&
|
|
|
|
mCurrentSeek.mTarget.mTime > mCurrentTimeBeforeSeek &&
|
2014-06-18 09:07:02 +04:00
|
|
|
audio->mTime < mCurrentTimeBeforeSeek) {
|
|
|
|
// We are doing a fastSeek, but we ended up *before* the previous
|
|
|
|
// playback position. This is surprising UX, so switch to an accurate
|
|
|
|
// seek and decode to the seek target. This is not conformant to the
|
|
|
|
// spec, fastSeek should always be fast, but until we get the time to
|
|
|
|
// change all Readers to seek to the keyframe after the currentTime
|
|
|
|
// in this case, we'll just decode forward. Bug 1026330.
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.mTarget.mType = SeekTarget::Accurate;
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mCurrentSeek.mTarget.mType == SeekTarget::PrevSyncPoint) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// Non-precise seek; we can stop the seek at the first sample.
|
2015-04-01 04:44:36 +03:00
|
|
|
Push(audio);
|
2014-06-18 09:07:02 +04:00
|
|
|
} else {
|
|
|
|
// We're doing an accurate seek. We must discard
|
|
|
|
// MediaData up to the one containing exact seek target.
|
2014-11-20 00:01:10 +03:00
|
|
|
if (NS_FAILED(DropAudioUpToSeekTarget(audio))) {
|
2014-06-18 09:07:02 +04:00
|
|
|
DecodeError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckIfSeekComplete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Ignore other cases.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::Push(AudioData* aSample)
|
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
MOZ_ASSERT(aSample);
|
|
|
|
// TODO: Send aSample to MSG and recalculate readystate before pushing,
|
|
|
|
// otherwise AdvanceFrame may pop the sample before we have a chance
|
|
|
|
// to reach playing.
|
|
|
|
AudioQueue().Push(aSample);
|
2015-04-01 04:44:36 +03:00
|
|
|
UpdateNextFrameStatus();
|
|
|
|
DispatchDecodeTasksIfNeeded();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::PushFront(AudioData* aSample)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
MOZ_ASSERT(aSample);
|
|
|
|
|
|
|
|
AudioQueue().PushFront(aSample);
|
|
|
|
UpdateNextFrameStatus();
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::Push(VideoData* aSample)
|
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
MOZ_ASSERT(aSample);
|
|
|
|
// TODO: Send aSample to MSG and recalculate readystate before pushing,
|
|
|
|
// otherwise AdvanceFrame may pop the sample before we have a chance
|
|
|
|
// to reach playing.
|
2015-07-03 10:33:56 +03:00
|
|
|
aSample->mFrameID = ++mCurrentFrameID;
|
2014-06-18 09:07:02 +04:00
|
|
|
VideoQueue().Push(aSample);
|
2015-04-01 04:44:36 +03:00
|
|
|
UpdateNextFrameStatus();
|
|
|
|
DispatchDecodeTasksIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::PushFront(VideoData* aSample)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
MOZ_ASSERT(aSample);
|
|
|
|
|
2015-07-03 10:33:56 +03:00
|
|
|
aSample->mFrameID = ++mCurrentFrameID;
|
2015-04-01 04:44:36 +03:00
|
|
|
VideoQueue().PushFront(aSample);
|
|
|
|
UpdateNextFrameStatus();
|
|
|
|
}
|
|
|
|
|
2015-06-10 05:55:56 +03:00
|
|
|
void
|
2015-08-13 15:22:48 +03:00
|
|
|
MediaDecoderStateMachine::OnAudioPopped(const nsRefPtr<MediaData>& aSample)
|
2015-04-01 04:44:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-06-10 05:55:56 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-07-23 09:14:16 +03:00
|
|
|
mDecoder->UpdatePlaybackOffset(std::max<int64_t>(0, aSample->mOffset));
|
2015-04-01 04:44:36 +03:00
|
|
|
UpdateNextFrameStatus();
|
2015-06-10 05:55:56 +03:00
|
|
|
DispatchAudioDecodeTaskIfNeeded();
|
2015-04-01 04:44:36 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 05:55:56 +03:00
|
|
|
void
|
2015-08-13 15:22:48 +03:00
|
|
|
MediaDecoderStateMachine::OnVideoPopped(const nsRefPtr<MediaData>& aSample)
|
2015-04-01 04:44:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-06-10 05:55:56 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-07-23 09:14:16 +03:00
|
|
|
mDecoder->UpdatePlaybackOffset(aSample->mOffset);
|
2015-04-01 04:44:36 +03:00
|
|
|
UpdateNextFrameStatus();
|
2015-06-10 05:55:56 +03:00
|
|
|
DispatchVideoDecodeTaskIfNeeded();
|
2014-11-03 11:20:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::OnNotDecoded(MediaData::Type aType,
|
2014-12-09 01:45:36 +03:00
|
|
|
MediaDecoderReader::NotDecodedReason aReason)
|
2014-11-03 11:20:14 +03:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-11-03 11:20:14 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
SAMPLE_LOG("OnNotDecoded (aType=%u, aReason=%u)", aType, aReason);
|
|
|
|
bool isAudio = aType == MediaData::AUDIO_DATA;
|
|
|
|
MOZ_ASSERT_IF(!isAudio, aType == MediaData::VIDEO_DATA);
|
2014-11-03 11:20:14 +03:00
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
if (isAudio) {
|
|
|
|
mAudioDataRequest.Complete();
|
|
|
|
} else {
|
|
|
|
mVideoDataRequest.Complete();
|
|
|
|
}
|
2015-03-06 08:49:00 +03:00
|
|
|
if (IsShutdown()) {
|
|
|
|
// Already shutdown;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2014-11-03 11:20:14 +03:00
|
|
|
// If this is a decode error, delegate to the generic error path.
|
2014-12-09 01:45:36 +03:00
|
|
|
if (aReason == MediaDecoderReader::DECODE_ERROR) {
|
2014-06-18 09:07:02 +04:00
|
|
|
DecodeError();
|
2014-11-03 11:20:14 +03:00
|
|
|
return;
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:20:31 +03:00
|
|
|
// If the decoder is waiting for data, we tell it to call us back when the
|
|
|
|
// data arrives.
|
2014-12-09 01:45:36 +03:00
|
|
|
if (aReason == MediaDecoderReader::WAITING_FOR_DATA) {
|
2014-12-22 11:20:31 +03:00
|
|
|
MOZ_ASSERT(mReader->IsWaitForDataSupported(),
|
|
|
|
"Readers that send WAITING_FOR_DATA need to implement WaitForData");
|
2015-05-06 03:17:35 +03:00
|
|
|
nsRefPtr<MediaDecoderStateMachine> self = this;
|
2015-08-11 01:42:45 +03:00
|
|
|
WaitRequestRef(aType).Begin(InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::WaitForData, aType)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__,
|
2015-05-22 22:28:20 +03:00
|
|
|
[self] (MediaData::Type aType) -> void {
|
|
|
|
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
|
|
|
|
self->WaitRequestRef(aType).Complete();
|
|
|
|
self->DispatchDecodeTasksIfNeeded();
|
|
|
|
},
|
|
|
|
[self] (WaitForDataRejectValue aRejection) -> void {
|
|
|
|
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
|
|
|
|
self->WaitRequestRef(aRejection.mType).Complete();
|
|
|
|
}));
|
2015-05-06 03:17:35 +03:00
|
|
|
|
2014-11-03 11:20:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-09 01:45:36 +03:00
|
|
|
if (aReason == MediaDecoderReader::CANCELED) {
|
2014-07-20 04:54:00 +04:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-03 11:20:14 +03:00
|
|
|
// This is an EOS. Finish off the queue, and then handle things based on our
|
|
|
|
// state.
|
2014-12-09 01:45:36 +03:00
|
|
|
MOZ_ASSERT(aReason == MediaDecoderReader::END_OF_STREAM);
|
2014-11-03 11:20:14 +03:00
|
|
|
if (!isAudio && mState == DECODER_STATE_SEEKING &&
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.Exists() && mFirstVideoFrameAfterSeek) {
|
2014-11-03 11:20:14 +03:00
|
|
|
// Null sample. Hit end of stream. If we have decoded a frame,
|
|
|
|
// insert it into the queue so that we have something to display.
|
|
|
|
// We make sure to do this before invoking VideoQueue().Finish()
|
|
|
|
// below.
|
2015-04-01 04:44:36 +03:00
|
|
|
Push(mFirstVideoFrameAfterSeek);
|
2014-11-20 00:01:10 +03:00
|
|
|
mFirstVideoFrameAfterSeek = nullptr;
|
2014-11-03 11:20:14 +03:00
|
|
|
}
|
2014-12-30 10:16:48 +03:00
|
|
|
if (isAudio) {
|
|
|
|
AudioQueue().Finish();
|
|
|
|
StopPrerollingAudio();
|
|
|
|
} else {
|
|
|
|
VideoQueue().Finish();
|
|
|
|
StopPrerollingVideo();
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
switch (mState) {
|
|
|
|
case DECODER_STATE_BUFFERING:
|
|
|
|
case DECODER_STATE_DECODING: {
|
2015-07-25 11:25:13 +03:00
|
|
|
if (MaybeFinishDecodeFirstFrame()) {
|
2015-07-22 09:28:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
CheckIfDecodeComplete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case DECODER_STATE_SEEKING: {
|
2015-03-03 03:46:32 +03:00
|
|
|
if (!mCurrentSeek.Exists()) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// We've received a sample from a previous decode. Discard it.
|
|
|
|
return;
|
|
|
|
}
|
2014-11-03 11:20:14 +03:00
|
|
|
|
|
|
|
if (isAudio) {
|
|
|
|
mDropAudioUntilNextDiscontinuity = false;
|
|
|
|
} else {
|
|
|
|
mDropVideoUntilNextDiscontinuity = false;
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2014-11-03 11:20:14 +03:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
CheckIfSeekComplete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2015-07-25 11:25:13 +03:00
|
|
|
bool
|
2014-11-06 12:52:44 +03:00
|
|
|
MediaDecoderStateMachine::MaybeFinishDecodeFirstFrame()
|
2014-11-03 11:20:14 +03:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-11-03 11:20:14 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-07-25 11:25:13 +03:00
|
|
|
if (!IsDecodingFirstFrame() ||
|
|
|
|
(IsAudioDecoding() && AudioQueue().GetSize() == 0) ||
|
2014-11-03 11:20:14 +03:00
|
|
|
(IsVideoDecoding() && VideoQueue().GetSize() == 0)) {
|
2015-07-25 11:25:13 +03:00
|
|
|
return false;
|
2014-11-03 11:20:14 +03:00
|
|
|
}
|
2015-07-25 11:25:13 +03:00
|
|
|
FinishDecodeFirstFrame();
|
|
|
|
if (!mQueuedSeek.Exists()) {
|
|
|
|
return false;
|
2014-11-03 11:20:14 +03:00
|
|
|
}
|
2015-07-25 11:25:13 +03:00
|
|
|
|
|
|
|
// We can now complete the pending seek.
|
|
|
|
mPendingSeek.Steal(mQueuedSeek);
|
|
|
|
SetState(DECODER_STATE_SEEKING);
|
|
|
|
ScheduleStateMachine();
|
|
|
|
return true;
|
2014-11-03 11:20:14 +03:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::OnVideoDecoded(VideoData* aVideoSample)
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<VideoData> video(aVideoSample);
|
2015-06-07 01:01:23 +03:00
|
|
|
MOZ_ASSERT(video);
|
2015-02-04 07:09:55 +03:00
|
|
|
mVideoDataRequest.Complete();
|
2015-06-07 01:01:23 +03:00
|
|
|
aVideoSample->AdjustForStartTime(StartTime());
|
2014-12-22 06:32:31 +03:00
|
|
|
mDecodedVideoEndTime = video ? video->GetEndTime() : mDecodedVideoEndTime;
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
SAMPLE_LOG("OnVideoDecoded [%lld,%lld] disc=%d",
|
|
|
|
(video ? video->mTime : -1),
|
|
|
|
(video ? video->GetEndTime() : -1),
|
|
|
|
(video ? video->mDiscontinuity : 0));
|
|
|
|
|
|
|
|
switch (mState) {
|
2014-12-30 10:16:48 +03:00
|
|
|
case DECODER_STATE_BUFFERING: {
|
2014-12-22 11:20:31 +03:00
|
|
|
// If we're buffering, this may be the sample we need to stop buffering.
|
2014-12-30 10:16:48 +03:00
|
|
|
// Save it and schedule the state machine.
|
|
|
|
Push(video);
|
2014-12-22 11:20:31 +03:00
|
|
|
ScheduleStateMachine();
|
2014-12-30 10:16:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
case DECODER_STATE_DECODING: {
|
2014-11-20 00:01:10 +03:00
|
|
|
Push(video);
|
2015-07-25 11:25:13 +03:00
|
|
|
if (MaybeFinishDecodeFirstFrame()) {
|
2015-07-22 09:28:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-12-30 10:16:48 +03:00
|
|
|
if (mIsVideoPrerolling && DonePrerollingVideo()) {
|
|
|
|
StopPrerollingVideo();
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:21:09 +03:00
|
|
|
// Schedule the state machine to send stream data as soon as possible or
|
|
|
|
// the VideoQueue() is empty before the Push().
|
|
|
|
// VideoQueue() is empty implies the state machine thread doesn't have
|
|
|
|
// precise time information about video frames. Once the first video
|
|
|
|
// frame pushed in the queue, schedule the state machine as soon as
|
|
|
|
// possible to render the video frame or delay the state machine thread
|
|
|
|
// accurately.
|
2015-08-24 05:04:21 +03:00
|
|
|
if (VideoQueue().GetSize() == 1) {
|
2015-05-13 13:21:09 +03:00
|
|
|
ScheduleStateMachine();
|
|
|
|
}
|
|
|
|
|
2015-01-13 12:31:03 +03:00
|
|
|
// For non async readers, if the requested video sample was slow to
|
|
|
|
// arrive, increase the amount of audio we buffer to ensure that we
|
|
|
|
// don't run out of audio. This is unnecessary for async readers,
|
|
|
|
// since they decode audio and video on different threads so they
|
|
|
|
// are unlikely to run out of decoded audio.
|
|
|
|
if (mReader->IsAsync()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
TimeDuration decodeTime = TimeStamp::Now() - mVideoDecodeStartTime;
|
2015-07-25 11:25:13 +03:00
|
|
|
if (!IsDecodingFirstFrame() &&
|
|
|
|
THRESHOLD_FACTOR * DurationToUsecs(decodeTime) > mLowAudioThresholdUsecs &&
|
2014-06-18 09:07:02 +04:00
|
|
|
!HasLowUndecodedData())
|
|
|
|
{
|
|
|
|
mLowAudioThresholdUsecs =
|
2015-01-22 08:53:04 +03:00
|
|
|
std::min(THRESHOLD_FACTOR * DurationToUsecs(decodeTime), mAmpleAudioThresholdUsecs);
|
2014-06-18 09:07:02 +04:00
|
|
|
mAmpleAudioThresholdUsecs = std::max(THRESHOLD_FACTOR * mLowAudioThresholdUsecs,
|
|
|
|
mAmpleAudioThresholdUsecs);
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Slow video decode, set mLowAudioThresholdUsecs=%lld mAmpleAudioThresholdUsecs=%lld",
|
2014-06-18 09:07:02 +04:00
|
|
|
mLowAudioThresholdUsecs, mAmpleAudioThresholdUsecs);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case DECODER_STATE_SEEKING: {
|
2015-03-03 03:46:32 +03:00
|
|
|
if (!mCurrentSeek.Exists()) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// We've received a sample from a previous decode. Discard it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mDropVideoUntilNextDiscontinuity) {
|
|
|
|
if (video->mDiscontinuity) {
|
|
|
|
mDropVideoUntilNextDiscontinuity = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!mDropVideoUntilNextDiscontinuity) {
|
|
|
|
// We must be after the discontinuity; we're receiving samples
|
|
|
|
// at or after the seek target.
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mCurrentSeek.mTarget.mType == SeekTarget::PrevSyncPoint &&
|
|
|
|
mCurrentSeek.mTarget.mTime > mCurrentTimeBeforeSeek &&
|
2014-06-18 09:07:02 +04:00
|
|
|
video->mTime < mCurrentTimeBeforeSeek) {
|
|
|
|
// We are doing a fastSeek, but we ended up *before* the previous
|
|
|
|
// playback position. This is surprising UX, so switch to an accurate
|
|
|
|
// seek and decode to the seek target. This is not conformant to the
|
|
|
|
// spec, fastSeek should always be fast, but until we get the time to
|
|
|
|
// change all Readers to seek to the keyframe after the currentTime
|
|
|
|
// in this case, we'll just decode forward. Bug 1026330.
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.mTarget.mType = SeekTarget::Accurate;
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mCurrentSeek.mTarget.mType == SeekTarget::PrevSyncPoint) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// Non-precise seek; we can stop the seek at the first sample.
|
2015-04-01 04:44:36 +03:00
|
|
|
Push(video);
|
2014-06-18 09:07:02 +04:00
|
|
|
} else {
|
|
|
|
// We're doing an accurate seek. We still need to discard
|
|
|
|
// MediaData up to the one containing exact seek target.
|
2014-11-20 00:01:10 +03:00
|
|
|
if (NS_FAILED(DropVideoUpToSeekTarget(video))) {
|
2014-06-18 09:07:02 +04:00
|
|
|
DecodeError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckIfSeekComplete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Ignore other cases.
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::CheckIfSeekComplete()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-04-21 01:35:52 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_SEEKING);
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
const bool videoSeekComplete = IsVideoSeekComplete();
|
|
|
|
if (HasVideo() && !videoSeekComplete) {
|
|
|
|
// We haven't reached the target. Ensure we have requested another sample.
|
|
|
|
if (NS_FAILED(EnsureVideoDecodeTaskQueued())) {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_WARN("Failed to request video during seek");
|
2014-06-18 09:07:02 +04:00
|
|
|
DecodeError();
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
const bool audioSeekComplete = IsAudioSeekComplete();
|
|
|
|
if (HasAudio() && !audioSeekComplete) {
|
|
|
|
// We haven't reached the target. Ensure we have requested another sample.
|
|
|
|
if (NS_FAILED(EnsureAudioDecodeTaskQueued())) {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_WARN("Failed to request audio during seek");
|
2014-06-18 09:07:02 +04:00
|
|
|
DecodeError();
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
SAMPLE_LOG("CheckIfSeekComplete() audioSeekComplete=%d videoSeekComplete=%d",
|
|
|
|
audioSeekComplete, videoSeekComplete);
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
if (audioSeekComplete && videoSeekComplete) {
|
|
|
|
mDecodeToSeekTarget = false;
|
2015-04-21 01:35:52 +03:00
|
|
|
SeekCompleted();
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 11:31:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::IsAudioDecoding()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
return HasAudio() && !AudioQueue().IsFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::IsVideoDecoding()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
return HasVideo() && !VideoQueue().IsFinished();
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::CheckIfDecodeComplete()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-25 01:13:00 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-03-24 02:28:43 +03:00
|
|
|
if (IsShutdown() ||
|
2014-03-11 14:06:23 +04:00
|
|
|
mState == DECODER_STATE_SEEKING ||
|
|
|
|
mState == DECODER_STATE_COMPLETED) {
|
2014-03-11 07:44:09 +04:00
|
|
|
// Don't change our state if we've already been shutdown, or we're seeking,
|
|
|
|
// since we don't want to abort the shutdown or seek processes.
|
2014-03-11 07:44:09 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
if (!IsVideoDecoding() && !IsAudioDecoding()) {
|
2014-03-11 07:44:09 +04:00
|
|
|
// We've finished decoding all active streams,
|
|
|
|
// so move to COMPLETED state.
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_COMPLETED);
|
2014-03-11 07:44:10 +04:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2014-03-11 07:44:09 +04:00
|
|
|
ScheduleStateMachine();
|
|
|
|
}
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("CheckIfDecodeComplete %scompleted",
|
2014-04-23 13:29:04 +04:00
|
|
|
((mState == DECODER_STATE_COMPLETED) ? "" : "NOT "));
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 23:12:00 +03:00
|
|
|
bool MediaDecoderStateMachine::IsPlaying() const
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-09-10 06:31:30 +03:00
|
|
|
return mMediaSink->IsPlaying();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
nsresult MediaDecoderStateMachine::Init(MediaDecoderStateMachine* aCloneDonor)
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2014-02-18 02:53:52 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaDecoderReader* cloneReader = nullptr;
|
2010-09-21 04:49:50 +04:00
|
|
|
if (aCloneDonor) {
|
2014-04-22 15:58:00 +04:00
|
|
|
cloneReader = aCloneDonor->mReader;
|
2010-09-21 04:49:50 +04:00
|
|
|
}
|
2014-02-18 02:53:52 +04:00
|
|
|
|
2015-03-10 19:59:30 +03:00
|
|
|
nsresult rv = mReader->Init(cloneReader);
|
2014-06-18 09:07:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-07-23 13:39:09 +03:00
|
|
|
ScheduleStateMachineCrossThread();
|
2014-06-18 09:07:02 +04:00
|
|
|
return NS_OK;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::StopPlayback()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("StopPlayback()");
|
2011-07-12 07:39:37 +04:00
|
|
|
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-07-01 00:22:53 +03:00
|
|
|
mDecoder->DispatchPlaybackStopped();
|
2011-01-18 03:53:18 +03:00
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
if (IsPlaying()) {
|
2015-07-03 10:33:56 +03:00
|
|
|
RenderVideoFrames(1);
|
2015-09-10 06:31:30 +03:00
|
|
|
mMediaSink->SetPlaying(false);
|
|
|
|
MOZ_ASSERT(!IsPlaying());
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
2014-03-11 07:44:10 +04:00
|
|
|
|
2015-02-24 05:25:08 +03:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
void MediaDecoderStateMachine::MaybeStartPlayback()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-07-02 05:34:05 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_DECODING ||
|
|
|
|
mState == DECODER_STATE_COMPLETED);
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
if (IsPlaying()) {
|
|
|
|
// Logging this case is really spammy - don't do it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-25 06:35:06 +03:00
|
|
|
bool playStatePermits = mPlayState == MediaDecoder::PLAY_STATE_PLAYING;
|
2015-07-02 05:34:05 +03:00
|
|
|
if (!playStatePermits || mIsAudioPrerolling || mIsVideoPrerolling) {
|
|
|
|
DECODER_LOG("Not starting playback [playStatePermits: %d, "
|
|
|
|
"mIsAudioPrerolling: %d, mIsVideoPrerolling: %d]",
|
|
|
|
(int) playStatePermits, (int) mIsAudioPrerolling, (int) mIsVideoPrerolling);
|
2014-12-30 10:16:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-01-14 09:25:02 +04:00
|
|
|
|
2014-08-18 16:49:51 +04:00
|
|
|
if (mDecoder->CheckDecoderCanOffloadAudio()) {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Offloading playback");
|
2014-08-18 16:49:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
DECODER_LOG("MaybeStartPlayback() starting playback");
|
2015-07-01 00:22:53 +03:00
|
|
|
mDecoder->DispatchPlaybackStarted();
|
2015-09-09 05:12:23 +03:00
|
|
|
StartMediaSink();
|
2015-07-11 11:41:39 +03:00
|
|
|
|
2015-09-10 06:31:30 +03:00
|
|
|
if (!IsPlaying()) {
|
|
|
|
mMediaSink->SetPlaying(true);
|
|
|
|
MOZ_ASSERT(IsPlaying());
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::UpdatePlaybackPositionInternal(int64_t aTime)
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-23 02:05:19 +03:00
|
|
|
SAMPLE_LOG("UpdatePlaybackPositionInternal(%lld)", aTime);
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-05-23 02:05:19 +03:00
|
|
|
mCurrentPosition = aTime;
|
2015-05-08 03:06:05 +03:00
|
|
|
NS_ASSERTION(mCurrentPosition >= 0, "CurrentTime should be positive!");
|
2015-06-03 05:01:55 +03:00
|
|
|
mObservedDuration = std::max(mObservedDuration.Ref(),
|
|
|
|
TimeUnit::FromMicroseconds(mCurrentPosition.Ref()));
|
2011-02-01 05:57:13 +03:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::UpdatePlaybackPosition(int64_t aTime)
|
2011-02-01 05:57:13 +03:00
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2011-02-01 05:57:13 +03:00
|
|
|
UpdatePlaybackPositionInternal(aTime);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool fragmentEnded = mFragmentEndTime >= 0 && GetMediaTime() >= mFragmentEndTime;
|
2015-09-07 06:39:16 +03:00
|
|
|
mMetadataManager.DispatchMetadataIfNeeded(TimeUnit::FromMicroseconds(aTime));
|
2012-11-30 17:17:54 +04:00
|
|
|
|
2011-08-25 03:42:23 +04:00
|
|
|
if (fragmentEnded) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::ClearPositionChangeFlag()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-09-30 03:34:37 +04:00
|
|
|
mPositionChangeQueued = false;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-08-28 04:46:00 +04:00
|
|
|
static const char* const gMachineStateStr[] = {
|
|
|
|
"NONE",
|
|
|
|
"DECODING_METADATA",
|
|
|
|
"WAIT_FOR_RESOURCES",
|
2015-03-23 05:31:15 +03:00
|
|
|
"WAIT_FOR_CDM",
|
2014-08-28 04:46:00 +04:00
|
|
|
"DORMANT",
|
|
|
|
"DECODING",
|
|
|
|
"SEEKING",
|
|
|
|
"BUFFERING",
|
|
|
|
"COMPLETED",
|
2015-03-25 21:59:46 +03:00
|
|
|
"SHUTDOWN",
|
|
|
|
"ERROR"
|
2014-08-28 04:46:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void MediaDecoderStateMachine::SetState(State aState)
|
|
|
|
{
|
2015-03-25 01:13:00 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-08-28 04:46:00 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
if (mState == aState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DECODER_LOG("Change machine state from %s to %s",
|
|
|
|
gMachineStateStr[mState], gMachineStateStr[aState]);
|
|
|
|
|
|
|
|
mState = aState;
|
2015-04-08 02:20:20 +03:00
|
|
|
|
2015-06-30 00:27:16 +03:00
|
|
|
mIsShutdown = mState == DECODER_STATE_ERROR || mState == DECODER_STATE_SHUTDOWN;
|
|
|
|
|
2015-04-08 02:20:20 +03:00
|
|
|
// Clear state-scoped state.
|
|
|
|
mSentPlaybackEndedEvent = false;
|
2014-08-28 04:46:00 +04:00
|
|
|
}
|
|
|
|
|
2015-04-30 03:27:32 +03:00
|
|
|
void MediaDecoderStateMachine::VolumeChanged()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-04-30 03:27:32 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSink->SetVolume(mVolume);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:13:56 +03:00
|
|
|
void MediaDecoderStateMachine::RecomputeDuration()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-06-04 21:00:34 +03:00
|
|
|
TimeUnit duration;
|
2015-06-03 00:44:26 +03:00
|
|
|
if (mExplicitDuration.Ref().isSome()) {
|
|
|
|
double d = mExplicitDuration.Ref().ref();
|
|
|
|
if (IsNaN(d)) {
|
|
|
|
// We have an explicit duration (which means that we shouldn't look at
|
|
|
|
// any other duration sources), but the duration isn't ready yet.
|
|
|
|
return;
|
|
|
|
}
|
2015-06-04 21:00:34 +03:00
|
|
|
// We don't fire duration changed for this case because it should have
|
|
|
|
// already been fired on the main thread when the explicit duration was set.
|
2015-06-03 05:01:55 +03:00
|
|
|
duration = TimeUnit::FromSeconds(d);
|
2015-06-03 01:29:50 +03:00
|
|
|
} else if (mEstimatedDuration.Ref().isSome()) {
|
2015-06-03 05:01:55 +03:00
|
|
|
duration = mEstimatedDuration.Ref().ref();
|
2015-06-03 00:44:26 +03:00
|
|
|
} else if (mInfo.mMetadataDuration.isSome()) {
|
2015-06-03 05:01:55 +03:00
|
|
|
duration = mInfo.mMetadataDuration.ref();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duration < mObservedDuration.Ref()) {
|
|
|
|
duration = mObservedDuration;
|
|
|
|
}
|
2015-05-23 02:05:19 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(duration.ToMicroseconds() >= 0);
|
2015-06-08 21:30:59 +03:00
|
|
|
mDuration = Some(duration);
|
2015-05-28 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2013-06-10 16:22:05 +04:00
|
|
|
void MediaDecoderStateMachine::SetDormant(bool aDormant)
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-05 04:34:46 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2013-06-10 16:22:05 +04:00
|
|
|
|
2015-03-24 02:28:43 +03:00
|
|
|
if (IsShutdown()) {
|
2015-03-05 04:33:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:22:05 +04:00
|
|
|
if (!mReader) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("SetDormant=%d", aDormant);
|
|
|
|
|
2013-06-10 16:22:05 +04:00
|
|
|
if (aDormant) {
|
2015-03-05 04:33:40 +03:00
|
|
|
if (mState == DECODER_STATE_SEEKING) {
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mQueuedSeek.Exists()) {
|
2015-03-05 04:33:40 +03:00
|
|
|
// Keep latest seek target
|
2015-03-03 03:46:32 +03:00
|
|
|
} else if (mPendingSeek.Exists()) {
|
|
|
|
mQueuedSeek.Steal(mPendingSeek);
|
|
|
|
} else if (mCurrentSeek.Exists()) {
|
|
|
|
mQueuedSeek.Steal(mCurrentSeek);
|
2015-03-05 04:33:40 +03:00
|
|
|
} else {
|
2015-05-08 03:06:05 +03:00
|
|
|
mQueuedSeek.mTarget = SeekTarget(mCurrentPosition,
|
2015-03-03 03:46:32 +03:00
|
|
|
SeekTarget::Accurate,
|
|
|
|
MediaDecoderEventVisibility::Suppressed);
|
|
|
|
// XXXbholley - Nobody is listening to this promise. Do we need to pass it
|
|
|
|
// back to MediaDecoder when we come out of dormant?
|
|
|
|
nsRefPtr<MediaDecoder::SeekPromise> unused = mQueuedSeek.mPromise.Ensure(__func__);
|
2015-01-13 02:08:55 +03:00
|
|
|
}
|
2015-03-05 04:33:40 +03:00
|
|
|
} else {
|
2015-05-08 03:06:05 +03:00
|
|
|
mQueuedSeek.mTarget = SeekTarget(mCurrentPosition,
|
2015-03-03 03:46:32 +03:00
|
|
|
SeekTarget::Accurate,
|
|
|
|
MediaDecoderEventVisibility::Suppressed);
|
|
|
|
// XXXbholley - Nobody is listening to this promise. Do we need to pass it
|
|
|
|
// back to MediaDecoder when we come out of dormant?
|
|
|
|
nsRefPtr<MediaDecoder::SeekPromise> unused = mQueuedSeek.mPromise.Ensure(__func__);
|
2015-01-13 02:08:55 +03:00
|
|
|
}
|
2015-04-15 21:12:14 +03:00
|
|
|
mPendingSeek.RejectIfExists(__func__);
|
|
|
|
mCurrentSeek.RejectIfExists(__func__);
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_DORMANT);
|
2015-03-16 06:51:02 +03:00
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
2015-03-19 19:53:01 +03:00
|
|
|
|
|
|
|
Reset();
|
2015-03-19 19:43:08 +03:00
|
|
|
|
|
|
|
// Note that we do not wait for the decode task queue to go idle before
|
|
|
|
// queuing the ReleaseMediaResources task - instead, we disconnect promises,
|
|
|
|
// reset state, and put a ResetDecode in the decode task queue. Any tasks
|
|
|
|
// that run after ResetDecode are supposed to run with a clean slate. We rely
|
|
|
|
// on that in other places (i.e. seeking), so it seems reasonable to rely on
|
|
|
|
// it here as well.
|
2015-04-07 22:20:43 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(mReader, &MediaDecoderReader::ReleaseMediaResources);
|
2015-04-15 21:12:14 +03:00
|
|
|
DecodeTaskQueue()->Dispatch(r.forget());
|
2013-06-10 16:22:05 +04:00
|
|
|
} else if ((aDormant != true) && (mState == DECODER_STATE_DORMANT)) {
|
|
|
|
ScheduleStateMachine();
|
2015-07-25 11:25:13 +03:00
|
|
|
mDecodingFirstFrame = true;
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_DECODING_NONE);
|
2013-06-10 16:22:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::Shutdown()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-25 01:12:08 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Once we've entered the shutdown state here there's no going back.
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Change state before issuing shutdown request to threads so those
|
|
|
|
// threads can start exiting cleanly during the Shutdown call.
|
2015-03-10 19:59:30 +03:00
|
|
|
ScheduleStateMachine();
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_SHUTDOWN);
|
2015-05-06 04:08:09 +03:00
|
|
|
|
|
|
|
mQueuedSeek.RejectIfExists(__func__);
|
|
|
|
mPendingSeek.RejectIfExists(__func__);
|
|
|
|
mCurrentSeek.RejectIfExists(__func__);
|
|
|
|
|
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSink->Shutdown();
|
2015-09-02 09:14:40 +03:00
|
|
|
|
2015-06-07 00:42:40 +03:00
|
|
|
// Shut down our start time rendezvous.
|
|
|
|
if (mStartTimeRendezvous) {
|
|
|
|
mStartTimeRendezvous->Destroy();
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:08:09 +03:00
|
|
|
// Put a task in the decode queue to shutdown the reader.
|
|
|
|
// the queue to spin down.
|
2015-08-11 01:42:45 +03:00
|
|
|
InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__, &MediaDecoderReader::Shutdown)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-06 04:08:09 +03:00
|
|
|
&MediaDecoderStateMachine::FinishShutdown,
|
|
|
|
&MediaDecoderStateMachine::FinishShutdown);
|
|
|
|
DECODER_LOG("Shutdown started");
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::StartDecoding()
|
2011-03-24 01:28:57 +03:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-07-22 09:28:14 +03:00
|
|
|
if (mState == DECODER_STATE_DECODING && !mDecodingFirstFrame) {
|
2014-03-11 07:44:09 +04:00
|
|
|
return;
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_DECODING);
|
2014-03-11 07:44:08 +04:00
|
|
|
|
2015-07-25 11:25:13 +03:00
|
|
|
if (mDecodingFirstFrame &&
|
|
|
|
(IsRealTime() || mSentFirstFrameLoadedEvent)) {
|
2015-07-26 04:57:11 +03:00
|
|
|
if (IsRealTime()) {
|
|
|
|
FinishDecodeFirstFrame();
|
|
|
|
} else {
|
|
|
|
// We're resuming from dormant state, so we don't need to request
|
|
|
|
// the first samples in order to determine the media start time,
|
|
|
|
// we have the start time from last time we loaded.
|
|
|
|
// FinishDecodeFirstFrame will be launched upon completion of the seek when
|
|
|
|
// we have data ready to play.
|
|
|
|
MOZ_ASSERT(mQueuedSeek.Exists() && mSentFirstFrameLoadedEvent,
|
|
|
|
"Return from dormant must have queued seek");
|
|
|
|
}
|
2015-07-25 11:25:13 +03:00
|
|
|
if (mQueuedSeek.Exists()) {
|
|
|
|
mPendingSeek.Steal(mQueuedSeek);
|
|
|
|
SetState(DECODER_STATE_SEEKING);
|
|
|
|
ScheduleStateMachine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
CheckIfDecodeComplete();
|
|
|
|
if (mState == DECODER_STATE_COMPLETED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
// Reset other state to pristine values before starting decode.
|
2014-12-30 10:16:48 +03:00
|
|
|
mIsAudioPrerolling = !DonePrerollingAudio();
|
|
|
|
mIsVideoPrerolling = !DonePrerollingVideo();
|
2014-03-11 07:44:08 +04:00
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
// Ensure that we've got tasks enqueued to decode data if we need to.
|
2014-03-11 07:44:10 +04:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
ScheduleStateMachine();
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
void MediaDecoderStateMachine::NotifyWaitingForResourcesStatusChanged()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-10-06 07:03:14 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-03-22 02:32:40 +03:00
|
|
|
DECODER_LOG("NotifyWaitingForResourcesStatusChanged");
|
2015-03-23 05:31:15 +03:00
|
|
|
|
|
|
|
if (mState == DECODER_STATE_WAIT_FOR_RESOURCES) {
|
2015-03-22 02:32:40 +03:00
|
|
|
// Try again.
|
2015-03-23 05:31:15 +03:00
|
|
|
SetState(DECODER_STATE_DECODING_NONE);
|
2015-03-22 02:32:40 +03:00
|
|
|
ScheduleStateMachine();
|
2015-03-23 05:31:15 +03:00
|
|
|
} else if (mState == DECODER_STATE_WAIT_FOR_CDM &&
|
|
|
|
!mReader->IsWaitingOnCDMResource()) {
|
2015-07-25 11:25:13 +03:00
|
|
|
StartDecoding();
|
2015-03-23 05:31:15 +03:00
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2015-05-07 18:19:33 +03:00
|
|
|
void MediaDecoderStateMachine::PlayStateChanged()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-12-30 10:16:48 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-05-07 18:19:33 +03:00
|
|
|
// This method used to be a Play() method invoked by MediaDecoder when the
|
|
|
|
// play state became PLAY_STATE_PLAYING. As such, it doesn't have any work to
|
|
|
|
// do for other state changes. That could change.
|
|
|
|
if (mPlayState != MediaDecoder::PLAY_STATE_PLAYING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
// Once we start playing, we don't want to minimize our prerolling, as we
|
|
|
|
// assume the user is likely to want to keep playing in future. This needs to
|
|
|
|
// happen before we invoke StartDecoding().
|
|
|
|
if (mMinimizePreroll) {
|
|
|
|
mMinimizePreroll = false;
|
|
|
|
DispatchDecodeTasksIfNeeded();
|
|
|
|
}
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
// Some state transitions still happen synchronously on the main thread. So
|
|
|
|
// if the main thread invokes Play() and then Seek(), the seek will initiate
|
|
|
|
// synchronously on the main thread, and the asynchronous PlayInternal task
|
|
|
|
// will arrive when it's no longer valid. The proper thing to do is to move
|
2015-03-18 00:23:12 +03:00
|
|
|
// all state transitions to the state machine task queue, but for now we just
|
2014-12-30 10:16:48 +03:00
|
|
|
// make sure that none of the possible main-thread state transitions (Seek(),
|
|
|
|
// SetDormant(), and Shutdown()) have not occurred.
|
|
|
|
if (mState != DECODER_STATE_DECODING && mState != DECODER_STATE_BUFFERING &&
|
|
|
|
mState != DECODER_STATE_COMPLETED)
|
|
|
|
{
|
|
|
|
DECODER_LOG("Unexpected state - Bailing out of PlayInternal()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-24 01:28:57 +03:00
|
|
|
// When asked to play, switch to decoding state only if
|
|
|
|
// we are currently buffering. In other cases, we'll start playing anyway
|
|
|
|
// when the state machine notices the decoder's state change to PLAYING.
|
2010-04-02 07:03:07 +04:00
|
|
|
if (mState == DECODER_STATE_BUFFERING) {
|
2014-12-30 10:16:48 +03:00
|
|
|
StartDecoding();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
2014-12-30 10:16:48 +03:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
ScheduleStateMachine();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2015-05-07 18:19:46 +03:00
|
|
|
void MediaDecoderStateMachine::LogicallySeekingChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
ScheduleStateMachine();
|
|
|
|
}
|
|
|
|
|
2015-08-13 04:22:59 +03:00
|
|
|
void MediaDecoderStateMachine::SameOriginMediaChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-09-07 14:32:16 +03:00
|
|
|
mStreamSink->SetSameOrigin(mSameOriginMedia);
|
2015-08-13 04:22:59 +03:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:22:10 +03:00
|
|
|
void MediaDecoderStateMachine::BufferedRangeUpdated()
|
2012-01-06 10:40:51 +04:00
|
|
|
{
|
2015-06-17 01:37:48 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2012-01-06 10:40:51 +04:00
|
|
|
|
2015-06-17 02:36:42 +03:00
|
|
|
// While playing an unseekable stream of unknown duration, mObservedDuration
|
|
|
|
// is updated (in AdvanceFrame()) as we play. But if data is being downloaded
|
|
|
|
// faster than played, mObserved won't reflect the end of playable data
|
2012-01-06 10:40:51 +04:00
|
|
|
// since we haven't played the frame at the end of buffered data. So update
|
2015-06-17 02:36:42 +03:00
|
|
|
// mObservedDuration here as new data is downloaded to prevent such a lag.
|
2015-06-18 00:22:10 +03:00
|
|
|
if (!mBuffered.Ref().IsInvalid()) {
|
2015-05-18 09:15:47 +03:00
|
|
|
bool exists;
|
2015-06-18 00:22:10 +03:00
|
|
|
media::TimeUnit end{mBuffered.Ref().GetEnd(&exists)};
|
2015-05-18 09:15:47 +03:00
|
|
|
if (exists) {
|
2015-06-17 02:36:42 +03:00
|
|
|
mObservedDuration = std::max(mObservedDuration.Ref(), end);
|
2012-01-06 10:40:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 03:46:32 +03:00
|
|
|
nsRefPtr<MediaDecoder::SeekPromise>
|
|
|
|
MediaDecoderStateMachine::Seek(SeekTarget aTarget)
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-30 17:17:54 +04:00
|
|
|
|
2015-03-24 02:28:43 +03:00
|
|
|
if (IsShutdown()) {
|
2015-03-03 03:46:32 +03:00
|
|
|
return MediaDecoder::SeekPromise::CreateAndReject(/* aIgnored = */ true, __func__);
|
2014-08-25 02:58:00 +04:00
|
|
|
}
|
|
|
|
|
2012-11-30 17:17:54 +04:00
|
|
|
// We need to be able to seek both at a transport level and at a media level
|
|
|
|
// to seek.
|
2014-06-23 14:08:34 +04:00
|
|
|
if (!mDecoder->IsMediaSeekable()) {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_WARN("Seek() function should not be called on a non-seekable state machine");
|
2015-03-03 03:46:32 +03:00
|
|
|
return MediaDecoder::SeekPromise::CreateAndReject(/* aIgnored = */ true, __func__);
|
2012-11-30 17:17:54 +04:00
|
|
|
}
|
2014-11-06 12:52:44 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(mState > DECODER_STATE_DECODING_METADATA,
|
|
|
|
"We should have got duration already");
|
|
|
|
|
2015-07-25 11:42:41 +03:00
|
|
|
if (mState < DECODER_STATE_DECODING ||
|
|
|
|
(IsDecodingFirstFrame() && !mReader->ForceZeroStartTime())) {
|
2014-11-06 12:52:44 +03:00
|
|
|
DECODER_LOG("Seek() Not Enough Data to continue at this stage, queuing seek");
|
2015-04-15 21:12:14 +03:00
|
|
|
mQueuedSeek.RejectIfExists(__func__);
|
2015-03-03 03:46:32 +03:00
|
|
|
mQueuedSeek.mTarget = aTarget;
|
|
|
|
return mQueuedSeek.mPromise.Ensure(__func__);
|
2014-11-06 12:52:44 +03:00
|
|
|
}
|
2015-04-15 21:12:14 +03:00
|
|
|
mQueuedSeek.RejectIfExists(__func__);
|
|
|
|
mPendingSeek.RejectIfExists(__func__);
|
2015-03-03 03:46:32 +03:00
|
|
|
mPendingSeek.mTarget = aTarget;
|
2014-04-01 07:39:04 +04:00
|
|
|
|
2015-03-03 03:46:32 +03:00
|
|
|
DECODER_LOG("Changed state to SEEKING (to %lld)", mPendingSeek.mTarget.mTime);
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_SEEKING);
|
2011-07-12 07:39:32 +04:00
|
|
|
ScheduleStateMachine();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-03-03 03:46:32 +03:00
|
|
|
return mPendingSeek.mPromise.Ensure(__func__);
|
2015-02-20 22:05:56 +03:00
|
|
|
}
|
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
void MediaDecoderStateMachine::StopMediaSink()
|
2011-07-12 07:39:10 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-12-04 14:59:36 +04:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
if (mMediaSink->IsStarted()) {
|
|
|
|
DECODER_LOG("Stop MediaSink");
|
|
|
|
mMediaSink->Stop();
|
|
|
|
mMediaSinkPromise.DisconnectIfExists();
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
void
|
2014-03-11 07:44:10 +04:00
|
|
|
MediaDecoderStateMachine::DispatchDecodeTasksIfNeeded()
|
2014-03-11 07:44:10 +04:00
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:10 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-03-17 06:12:20 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
if (mState != DECODER_STATE_DECODING &&
|
|
|
|
mState != DECODER_STATE_BUFFERING &&
|
|
|
|
mState != DECODER_STATE_SEEKING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
// NeedToDecodeAudio() can go from false to true while we hold the
|
|
|
|
// monitor, but it can't go from true to false. This can happen because
|
|
|
|
// NeedToDecodeAudio() takes into account the amount of decoded audio
|
|
|
|
// that's been written to the AudioStream but not played yet. So if we
|
|
|
|
// were calling NeedToDecodeAudio() twice and we thread-context switch
|
|
|
|
// between the calls, audio can play, which can affect the return value
|
|
|
|
// of NeedToDecodeAudio() giving inconsistent results. So we cache the
|
|
|
|
// value returned by NeedToDecodeAudio(), and make decisions
|
|
|
|
// based on the cached value. If NeedToDecodeAudio() has
|
|
|
|
// returned false, and then subsequently returns true and we're not
|
|
|
|
// playing, it will probably be OK since we don't need to consume data
|
|
|
|
// anyway.
|
|
|
|
|
|
|
|
const bool needToDecodeAudio = NeedToDecodeAudio();
|
|
|
|
const bool needToDecodeVideo = NeedToDecodeVideo();
|
2014-03-11 07:44:10 +04:00
|
|
|
|
|
|
|
// If we're in completed state, we should not need to decode anything else.
|
|
|
|
MOZ_ASSERT(mState != DECODER_STATE_COMPLETED ||
|
2014-03-11 07:44:10 +04:00
|
|
|
(!needToDecodeAudio && !needToDecodeVideo));
|
2014-03-11 07:44:10 +04:00
|
|
|
|
2015-04-25 06:35:06 +03:00
|
|
|
bool needIdle = !IsLogicallyPlaying() &&
|
2014-04-15 07:01:34 +04:00
|
|
|
mState != DECODER_STATE_SEEKING &&
|
2014-03-11 07:44:10 +04:00
|
|
|
!needToDecodeAudio &&
|
2014-03-28 08:50:02 +04:00
|
|
|
!needToDecodeVideo &&
|
|
|
|
!IsPlaying();
|
2014-03-11 07:44:10 +04:00
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
SAMPLE_LOG("DispatchDecodeTasksIfNeeded needAudio=%d audioStatus=%s needVideo=%d videoStatus=%s needIdle=%d",
|
|
|
|
needToDecodeAudio, AudioRequestStatus(),
|
|
|
|
needToDecodeVideo, VideoRequestStatus(),
|
2014-07-20 04:54:00 +04:00
|
|
|
needIdle);
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
if (needToDecodeAudio) {
|
|
|
|
EnsureAudioDecodeTaskQueued();
|
|
|
|
}
|
|
|
|
if (needToDecodeVideo) {
|
|
|
|
EnsureVideoDecodeTaskQueued();
|
|
|
|
}
|
|
|
|
|
2014-05-19 06:23:00 +04:00
|
|
|
if (needIdle) {
|
2015-05-06 03:56:00 +03:00
|
|
|
DECODER_LOG("Dispatching SetIdle() audioQueue=%lld videoQueue=%lld",
|
2015-02-24 02:43:16 +03:00
|
|
|
GetDecodedAudioDuration(),
|
|
|
|
VideoQueue().Duration());
|
2015-05-06 03:56:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> task = NS_NewRunnableMethod(mReader, &MediaDecoderReader::SetIdle);
|
2015-04-15 21:12:14 +03:00
|
|
|
DecodeTaskQueue()->Dispatch(task.forget());
|
2014-03-11 07:44:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:34:34 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::InitiateSeek()
|
2014-03-11 07:44:09 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
|
2015-04-15 21:12:14 +03:00
|
|
|
mCurrentSeek.RejectIfExists(__func__);
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.Steal(mPendingSeek);
|
2015-03-03 03:05:54 +03:00
|
|
|
|
|
|
|
// Bound the seek time to be inside the media range.
|
2015-06-08 21:30:59 +03:00
|
|
|
int64_t end = Duration().ToMicroseconds();
|
2015-03-03 03:05:54 +03:00
|
|
|
NS_ASSERTION(end != -1, "Should know end time by now");
|
2015-05-23 02:05:19 +03:00
|
|
|
int64_t seekTime = mCurrentSeek.mTarget.mTime;
|
2015-03-03 03:05:54 +03:00
|
|
|
seekTime = std::min(seekTime, end);
|
2015-05-23 02:05:19 +03:00
|
|
|
seekTime = std::max(int64_t(0), seekTime);
|
|
|
|
NS_ASSERTION(seekTime >= 0 && seekTime <= end,
|
2015-03-03 03:05:54 +03:00
|
|
|
"Can only seek in range [0,duration]");
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.mTarget.mTime = seekTime;
|
2015-03-03 03:05:54 +03:00
|
|
|
|
2015-02-21 02:34:34 +03:00
|
|
|
mDropAudioUntilNextDiscontinuity = HasAudio();
|
|
|
|
mDropVideoUntilNextDiscontinuity = HasVideo();
|
|
|
|
mCurrentTimeBeforeSeek = GetMediaTime();
|
|
|
|
|
|
|
|
// Stop playback now to ensure that while we're outside the monitor
|
|
|
|
// dispatching SeekingStarted, playback doesn't advance and mess with
|
2015-05-08 03:06:05 +03:00
|
|
|
// mCurrentPosition that we've setting to seekTime here.
|
2015-02-21 02:34:34 +03:00
|
|
|
StopPlayback();
|
2015-03-03 03:46:32 +03:00
|
|
|
UpdatePlaybackPositionInternal(mCurrentSeek.mTarget.mTime);
|
2015-02-21 02:34:34 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> startEvent =
|
|
|
|
NS_NewRunnableMethodWithArg<MediaDecoderEventVisibility>(
|
|
|
|
mDecoder,
|
|
|
|
&MediaDecoder::SeekingStarted,
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.mTarget.mEventVisibility);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(startEvent.forget());
|
2015-02-21 02:34:34 +03:00
|
|
|
|
2015-03-19 19:53:01 +03:00
|
|
|
// Reset our state machine and decoding pipeline before seeking.
|
|
|
|
Reset();
|
2015-02-21 02:34:34 +03:00
|
|
|
|
|
|
|
// Do the seek.
|
2015-05-06 03:25:14 +03:00
|
|
|
nsRefPtr<MediaDecoderStateMachine> self = this;
|
2015-08-11 01:42:45 +03:00
|
|
|
mSeekRequest.Begin(InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::Seek, mCurrentSeek.mTarget.mTime,
|
|
|
|
Duration().ToMicroseconds())
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__,
|
2015-05-22 22:28:20 +03:00
|
|
|
[self] (int64_t) -> void {
|
|
|
|
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
|
|
|
|
self->mSeekRequest.Complete();
|
|
|
|
// We must decode the first samples of active streams, so we can determine
|
|
|
|
// the new stream time. So dispatch tasks to do that.
|
|
|
|
self->mDecodeToSeekTarget = true;
|
|
|
|
self->DispatchDecodeTasksIfNeeded();
|
|
|
|
}, [self] (nsresult aResult) -> void {
|
|
|
|
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
|
|
|
|
self->mSeekRequest.Complete();
|
|
|
|
MOZ_ASSERT(NS_FAILED(aResult), "Cancels should also disconnect mSeekRequest");
|
|
|
|
self->DecodeError();
|
|
|
|
}));
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
nsresult
|
|
|
|
MediaDecoderStateMachine::DispatchAudioDecodeTaskIfNeeded()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:10 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-03-06 08:49:00 +03:00
|
|
|
if (IsShutdown()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
if (NeedToDecodeAudio()) {
|
|
|
|
return EnsureAudioDecodeTaskQueued();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
nsresult
|
|
|
|
MediaDecoderStateMachine::EnsureAudioDecodeTaskQueued()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
SAMPLE_LOG("EnsureAudioDecodeTaskQueued isDecoding=%d status=%s",
|
|
|
|
IsAudioDecoding(), AudioRequestStatus());
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2015-02-05 04:12:51 +03:00
|
|
|
if (mState != DECODER_STATE_DECODING &&
|
|
|
|
mState != DECODER_STATE_BUFFERING &&
|
|
|
|
mState != DECODER_STATE_SEEKING) {
|
2014-03-11 07:44:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
if (!IsAudioDecoding() || mAudioDataRequest.Exists() ||
|
2015-02-21 02:34:34 +03:00
|
|
|
mAudioWaitRequest.Exists() || mSeekRequest.Exists()) {
|
2015-02-05 04:12:51 +03:00
|
|
|
return NS_OK;
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2015-07-20 09:28:29 +03:00
|
|
|
RequestAudioData();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::RequestAudioData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
|
2015-02-05 04:12:51 +03:00
|
|
|
SAMPLE_LOG("Queueing audio task - queued=%i, decoder-queued=%o",
|
|
|
|
AudioQueue().GetSize(), mReader->SizeOfAudioQueueInFrames());
|
|
|
|
|
2015-07-20 09:28:29 +03:00
|
|
|
if (mSentFirstFrameLoadedEvent) {
|
2015-08-11 01:42:45 +03:00
|
|
|
mAudioDataRequest.Begin(InvokeAsync(DecodeTaskQueue(), mReader.get(),
|
|
|
|
__func__, &MediaDecoderReader::RequestAudioData)
|
2015-07-20 09:28:29 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaDecoderStateMachine::OnAudioDecoded,
|
|
|
|
&MediaDecoderStateMachine::OnAudioNotDecoded));
|
|
|
|
} else {
|
|
|
|
mAudioDataRequest.Begin(
|
2015-08-11 01:42:45 +03:00
|
|
|
InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::RequestAudioData)
|
2015-07-20 09:28:29 +03:00
|
|
|
->Then(OwnerThread(), __func__, mStartTimeRendezvous.get(),
|
|
|
|
&StartTimeRendezvous::ProcessFirstSample<AudioDataPromise>,
|
|
|
|
&StartTimeRendezvous::FirstSampleRejected<AudioData>)
|
|
|
|
->CompletionPromise()
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaDecoderStateMachine::OnAudioDecoded,
|
|
|
|
&MediaDecoderStateMachine::OnAudioNotDecoded)
|
|
|
|
);
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
nsresult
|
|
|
|
MediaDecoderStateMachine::DispatchVideoDecodeTaskIfNeeded()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-03-11 07:44:10 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-03-06 08:49:00 +03:00
|
|
|
if (IsShutdown()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:10 +04:00
|
|
|
if (NeedToDecodeVideo()) {
|
|
|
|
return EnsureVideoDecodeTaskQueued();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
nsresult
|
2014-03-11 07:44:09 +04:00
|
|
|
MediaDecoderStateMachine::EnsureVideoDecodeTaskQueued()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
SAMPLE_LOG("EnsureVideoDecodeTaskQueued isDecoding=%d status=%s",
|
|
|
|
IsVideoDecoding(), VideoRequestStatus());
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2015-02-05 04:12:51 +03:00
|
|
|
if (mState != DECODER_STATE_DECODING &&
|
|
|
|
mState != DECODER_STATE_BUFFERING &&
|
|
|
|
mState != DECODER_STATE_SEEKING) {
|
2012-01-19 22:30:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2015-02-04 07:09:55 +03:00
|
|
|
if (!IsVideoDecoding() || mVideoDataRequest.Exists() ||
|
2015-02-21 02:34:34 +03:00
|
|
|
mVideoWaitRequest.Exists() || mSeekRequest.Exists()) {
|
2015-02-05 04:12:51 +03:00
|
|
|
return NS_OK;
|
2011-11-08 05:38:17 +04:00
|
|
|
}
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2015-07-20 09:28:29 +03:00
|
|
|
RequestVideoData();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::RequestVideoData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
AssertCurrentThreadInMonitor();
|
2015-02-05 04:12:51 +03:00
|
|
|
|
|
|
|
// Time the video decode, so that if it's slow, we can increase our low
|
|
|
|
// audio threshold to reduce the chance of an audio underrun while we're
|
|
|
|
// waiting for a video decode to complete.
|
|
|
|
mVideoDecodeStartTime = TimeStamp::Now();
|
|
|
|
|
2015-07-20 09:28:29 +03:00
|
|
|
bool skipToNextKeyFrame = mSentFirstFrameLoadedEvent &&
|
|
|
|
NeedToSkipToNextKeyframe();
|
|
|
|
int64_t currentTime = mState == DECODER_STATE_SEEKING ? 0 : GetMediaTime();
|
|
|
|
|
2015-02-05 04:12:51 +03:00
|
|
|
SAMPLE_LOG("Queueing video task - queued=%i, decoder-queued=%o, skip=%i, time=%lld",
|
|
|
|
VideoQueue().GetSize(), mReader->SizeOfVideoQueueInFrames(), skipToNextKeyFrame,
|
|
|
|
currentTime);
|
|
|
|
|
2015-07-20 09:28:29 +03:00
|
|
|
if (mSentFirstFrameLoadedEvent) {
|
|
|
|
mVideoDataRequest.Begin(
|
2015-08-11 01:42:45 +03:00
|
|
|
InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::RequestVideoData,
|
2015-08-24 04:32:49 +03:00
|
|
|
skipToNextKeyFrame, currentTime)
|
2015-07-20 09:28:29 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaDecoderStateMachine::OnVideoDecoded,
|
|
|
|
&MediaDecoderStateMachine::OnVideoNotDecoded));
|
|
|
|
} else {
|
|
|
|
mVideoDataRequest.Begin(
|
2015-08-11 01:42:45 +03:00
|
|
|
InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::RequestVideoData,
|
2015-08-24 04:32:49 +03:00
|
|
|
skipToNextKeyFrame, currentTime)
|
2015-07-20 09:28:29 +03:00
|
|
|
->Then(OwnerThread(), __func__, mStartTimeRendezvous.get(),
|
|
|
|
&StartTimeRendezvous::ProcessFirstSample<VideoDataPromise>,
|
|
|
|
&StartTimeRendezvous::FirstSampleRejected<VideoData>)
|
|
|
|
->CompletionPromise()
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaDecoderStateMachine::OnVideoDecoded,
|
|
|
|
&MediaDecoderStateMachine::OnVideoNotDecoded));
|
|
|
|
}
|
2012-01-19 22:30:29 +04:00
|
|
|
}
|
2011-11-08 05:38:17 +04:00
|
|
|
|
2015-07-22 04:54:06 +03:00
|
|
|
void
|
2015-09-09 05:12:23 +03:00
|
|
|
MediaDecoderStateMachine::StartMediaSink()
|
2011-07-12 07:39:10 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-12-04 14:59:36 +04:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
if (!mMediaSink->IsStarted()) {
|
2013-12-11 09:03:30 +04:00
|
|
|
mAudioCompleted = false;
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSink->Start(GetMediaTime(), mInfo);
|
2015-07-22 04:54:06 +03:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
auto promise = mMediaSink->OnEnded(TrackInfo::kAudioTrack);
|
2015-09-07 06:56:52 +03:00
|
|
|
if (promise) {
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSinkPromise.Begin(promise->Then(
|
2015-07-22 04:54:06 +03:00
|
|
|
OwnerThread(), __func__, this,
|
2015-09-09 05:12:23 +03:00
|
|
|
&MediaDecoderStateMachine::OnMediaSinkComplete,
|
|
|
|
&MediaDecoderStateMachine::OnMediaSinkError));
|
2015-09-07 06:56:52 +03:00
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 05:12:50 +04:00
|
|
|
int64_t MediaDecoderStateMachine::AudioDecodedUsecs()
|
2010-09-15 03:24:47 +04:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2010-09-15 03:24:47 +04:00
|
|
|
NS_ASSERTION(HasAudio(),
|
2011-04-14 02:12:23 +04:00
|
|
|
"Should only call AudioDecodedUsecs() when we have audio");
|
2010-09-15 03:24:47 +04:00
|
|
|
// The amount of audio we have decoded is the amount of audio data we've
|
|
|
|
// already decoded and pushed to the hardware, plus the amount of audio
|
|
|
|
// data waiting to be pushed to the hardware.
|
2015-09-09 05:12:23 +03:00
|
|
|
int64_t pushed = mMediaSink->IsStarted() ? (AudioEndTime() - GetMediaTime()) : 0;
|
2015-01-06 04:58:04 +03:00
|
|
|
|
|
|
|
// Currently for real time streams, AudioQueue().Duration() produce
|
|
|
|
// wrong values (Bug 1114434), so we use frame counts to calculate duration.
|
|
|
|
if (IsRealTime()) {
|
|
|
|
return pushed + FramesToUsecs(AudioQueue().FrameCount(), mInfo.mAudio.mRate).value();
|
|
|
|
}
|
2014-04-28 05:12:50 +04:00
|
|
|
return pushed + AudioQueue().Duration();
|
2010-09-15 03:24:47 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 05:12:50 +04:00
|
|
|
bool MediaDecoderStateMachine::HasLowDecodedData(int64_t aAudioUsecs)
|
2011-01-18 03:53:18 +03:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-12-22 11:20:31 +03:00
|
|
|
MOZ_ASSERT(mReader->UseBufferingHeuristics());
|
2011-03-24 01:28:57 +03:00
|
|
|
// We consider ourselves low on decoded data if we're low on audio,
|
|
|
|
// provided we've not decoded to the end of the audio stream, or
|
2013-11-19 18:01:14 +04:00
|
|
|
// if we're low on video frames, provided
|
2011-03-24 01:28:57 +03:00
|
|
|
// we've not decoded to the end of the video stream.
|
2014-06-18 09:07:02 +04:00
|
|
|
return ((IsAudioDecoding() && AudioDecodedUsecs() < aAudioUsecs) ||
|
|
|
|
(IsVideoDecoding() &&
|
2014-04-28 05:12:50 +04:00
|
|
|
static_cast<uint32_t>(VideoQueue().GetSize()) < LOW_VIDEO_FRAMES));
|
2011-01-18 03:53:18 +03:00
|
|
|
}
|
2010-09-15 03:24:47 +04:00
|
|
|
|
2015-02-27 00:37:03 +03:00
|
|
|
bool MediaDecoderStateMachine::OutOfDecodedAudio()
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-02-27 00:37:03 +03:00
|
|
|
return IsAudioDecoding() && !AudioQueue().IsFinished() &&
|
|
|
|
AudioQueue().GetSize() == 0 &&
|
2015-09-09 05:12:23 +03:00
|
|
|
!mMediaSink->HasUnplayedFrames(TrackInfo::kAudioTrack);
|
2015-02-27 00:37:03 +03:00
|
|
|
}
|
|
|
|
|
2014-04-28 05:12:50 +04:00
|
|
|
bool MediaDecoderStateMachine::HasLowUndecodedData()
|
2011-03-24 01:28:57 +03:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-19 18:01:14 +04:00
|
|
|
return HasLowUndecodedData(mLowDataThresholdUsecs);
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2014-11-12 07:50:21 +03:00
|
|
|
bool MediaDecoderStateMachine::HasLowUndecodedData(int64_t aUsecs)
|
2011-03-24 01:28:57 +03:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-07-22 09:28:14 +03:00
|
|
|
NS_ASSERTION(mState >= DECODER_STATE_DECODING && !IsDecodingFirstFrame(),
|
2015-06-18 00:22:10 +03:00
|
|
|
"Must have loaded first frame for mBuffered to be valid");
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2015-06-18 00:22:10 +03:00
|
|
|
// If we don't have a duration, mBuffered is probably not going to have
|
2015-02-12 21:48:14 +03:00
|
|
|
// a useful buffered range. Return false here so that we don't get stuck in
|
|
|
|
// buffering mode for live streams.
|
2015-06-08 21:30:59 +03:00
|
|
|
if (Duration().IsInfinite()) {
|
2015-02-12 21:48:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:22:10 +03:00
|
|
|
if (mBuffered.Ref().IsInvalid()) {
|
2015-05-18 09:15:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-05 14:57:43 +03:00
|
|
|
|
2014-11-12 07:50:21 +03:00
|
|
|
int64_t endOfDecodedVideoData = INT64_MAX;
|
|
|
|
if (HasVideo() && !VideoQueue().AtEndOfStream()) {
|
|
|
|
endOfDecodedVideoData = VideoQueue().Peek() ? VideoQueue().Peek()->GetEndTime() : mVideoFrameEndTime;
|
|
|
|
}
|
|
|
|
int64_t endOfDecodedAudioData = INT64_MAX;
|
|
|
|
if (HasAudio() && !AudioQueue().AtEndOfStream()) {
|
2014-11-21 00:24:00 +03:00
|
|
|
// mDecodedAudioEndTime could be -1 when no audio samples are decoded.
|
|
|
|
// But that is fine since we consider ourself as low in decoded data when
|
|
|
|
// we don't have any decoded audio samples at all.
|
|
|
|
endOfDecodedAudioData = mDecodedAudioEndTime;
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
2014-11-12 07:50:21 +03:00
|
|
|
int64_t endOfDecodedData = std::min(endOfDecodedVideoData, endOfDecodedAudioData);
|
2015-06-08 21:30:59 +03:00
|
|
|
if (Duration().ToMicroseconds() < endOfDecodedData) {
|
2015-05-18 09:15:47 +03:00
|
|
|
// Our duration is not up to date. No point buffering.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
media::TimeInterval interval(media::TimeUnit::FromMicroseconds(endOfDecodedData),
|
2015-06-08 21:30:59 +03:00
|
|
|
media::TimeUnit::FromMicroseconds(std::min(endOfDecodedData + aUsecs, Duration().ToMicroseconds())));
|
2015-06-18 00:22:10 +03:00
|
|
|
return endOfDecodedData != INT64_MAX && !mBuffered.Ref().Contains(interval);
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::DecodeError()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-24 02:15:54 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-03-24 02:28:43 +03:00
|
|
|
if (IsShutdown()) {
|
2014-06-18 09:07:02 +04:00
|
|
|
// Already shutdown.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:28:43 +03:00
|
|
|
// Change state to error, which will cause the state machine to wait until
|
|
|
|
// the MediaDecoder shuts it down.
|
|
|
|
SetState(DECODER_STATE_ERROR);
|
2015-03-25 03:21:21 +03:00
|
|
|
ScheduleStateMachine();
|
2015-03-24 02:28:43 +03:00
|
|
|
DECODER_WARN("Decode error, changed state to ERROR");
|
|
|
|
|
|
|
|
// MediaDecoder::DecodeError notifies the owner, and then shuts down the state
|
|
|
|
// machine.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &MediaDecoder::DecodeError);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(event.forget());
|
2014-03-11 07:44:09 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:44:09 +04:00
|
|
|
void
|
2015-03-20 22:53:32 +03:00
|
|
|
MediaDecoderStateMachine::OnMetadataRead(MetadataHolder* aMetadata)
|
2014-03-11 07:44:09 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-08-21 07:37:00 +04:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_DECODING_METADATA);
|
2015-04-30 03:07:55 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-03-20 22:53:32 +03:00
|
|
|
mMetadataRequest.Complete();
|
2014-06-23 14:08:34 +04:00
|
|
|
|
2015-07-20 09:29:10 +03:00
|
|
|
// Set mode to PLAYBACK after reading metadata.
|
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
|
2015-03-20 22:53:32 +03:00
|
|
|
mDecoder->SetMediaSeekable(mReader->IsMediaSeekable());
|
|
|
|
mInfo = aMetadata->mInfo;
|
|
|
|
mMetadataTags = aMetadata->mTags.forget();
|
2015-06-07 00:48:07 +03:00
|
|
|
nsRefPtr<MediaDecoderStateMachine> self = this;
|
2015-06-07 00:42:40 +03:00
|
|
|
|
2015-06-07 00:42:40 +03:00
|
|
|
// Set up the start time rendezvous if it doesn't already exist (which is
|
|
|
|
// generally the case, unless we're coming out of dormant mode).
|
|
|
|
if (!mStartTimeRendezvous) {
|
2015-07-16 21:31:21 +03:00
|
|
|
mStartTimeRendezvous = new StartTimeRendezvous(OwnerThread(), HasAudio(), HasVideo(),
|
2015-06-07 00:42:40 +03:00
|
|
|
mReader->ForceZeroStartTime() || IsRealTime());
|
2015-06-07 01:00:36 +03:00
|
|
|
|
2015-07-16 21:31:21 +03:00
|
|
|
mStartTimeRendezvous->AwaitStartTime()->Then(OwnerThread(), __func__,
|
2015-06-07 01:00:36 +03:00
|
|
|
[self] () -> void {
|
|
|
|
NS_ENSURE_TRUE_VOID(!self->IsShutdown());
|
2015-06-18 00:22:10 +03:00
|
|
|
self->mReader->DispatchSetStartTime(self->StartTime());
|
2015-06-07 01:00:36 +03:00
|
|
|
},
|
|
|
|
[] () -> void { NS_WARNING("Setting start time on reader failed"); }
|
|
|
|
);
|
2015-06-07 00:42:40 +03:00
|
|
|
}
|
|
|
|
|
2015-06-07 00:48:07 +03:00
|
|
|
if (mInfo.mMetadataDuration.isSome()) {
|
2015-05-31 07:18:12 +03:00
|
|
|
RecomputeDuration();
|
2015-06-07 00:48:07 +03:00
|
|
|
} else if (mInfo.mUnadjustedMetadataEndTime.isSome()) {
|
2015-07-16 21:31:21 +03:00
|
|
|
mStartTimeRendezvous->AwaitStartTime()->Then(OwnerThread(), __func__,
|
2015-06-07 00:48:07 +03:00
|
|
|
[self] () -> void {
|
|
|
|
NS_ENSURE_TRUE_VOID(!self->IsShutdown());
|
|
|
|
TimeUnit unadjusted = self->mInfo.mUnadjustedMetadataEndTime.ref();
|
|
|
|
TimeUnit adjustment = TimeUnit::FromMicroseconds(self->StartTime());
|
|
|
|
self->mInfo.mMetadataDuration.emplace(unadjusted - adjustment);
|
|
|
|
self->RecomputeDuration();
|
|
|
|
}, [] () -> void { NS_WARNING("Adjusting metadata end time failed"); }
|
|
|
|
);
|
2015-05-31 07:18:12 +03:00
|
|
|
}
|
|
|
|
|
2015-02-01 23:28:41 +03:00
|
|
|
if (HasVideo()) {
|
|
|
|
DECODER_LOG("Video decode isAsync=%d HWAccel=%d videoQueueSize=%d",
|
|
|
|
mReader->IsAsync(),
|
2015-03-09 06:17:30 +03:00
|
|
|
mReader->VideoIsHardwareAccelerated(),
|
|
|
|
GetAmpleVideoFrames());
|
2011-07-12 07:39:23 +04:00
|
|
|
}
|
2015-02-01 23:28:41 +03:00
|
|
|
|
2015-06-08 20:56:00 +03:00
|
|
|
// In general, we wait until we know the duration before notifying the decoder.
|
|
|
|
// However, we notify unconditionally in this case without waiting for the start
|
|
|
|
// time, since the caller might be waiting on metadataloaded to be fired before
|
|
|
|
// feeding in the CDM, which we need to decode the first frame (and
|
|
|
|
// thus get the metadata). We could fix this if we could compute the start
|
|
|
|
// time by demuxing without necessaring decoding.
|
2015-06-09 22:16:18 +03:00
|
|
|
mNotifyMetadataBeforeFirstFrame = mDuration.Ref().isSome() || mReader->IsWaitingOnCDMResource();
|
2015-06-08 20:56:00 +03:00
|
|
|
if (mNotifyMetadataBeforeFirstFrame) {
|
2014-11-06 12:52:44 +03:00
|
|
|
EnqueueLoadedMetadataEvent();
|
|
|
|
}
|
|
|
|
|
2015-03-20 22:53:32 +03:00
|
|
|
if (mReader->IsWaitingOnCDMResource()) {
|
|
|
|
// Metadata parsing was successful but we're still waiting for CDM caps
|
|
|
|
// to become available so that we can build the correct decryptor/decoder.
|
|
|
|
SetState(DECODER_STATE_WAIT_FOR_CDM);
|
2015-03-22 02:32:40 +03:00
|
|
|
return;
|
2014-11-06 12:52:44 +03:00
|
|
|
}
|
2015-03-20 22:53:32 +03:00
|
|
|
|
2015-07-25 11:25:13 +03:00
|
|
|
StartDecoding();
|
|
|
|
|
2014-11-06 12:52:44 +03:00
|
|
|
ScheduleStateMachine();
|
2015-03-20 22:53:32 +03:00
|
|
|
}
|
2014-11-06 12:52:44 +03:00
|
|
|
|
2015-03-20 22:53:32 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::OnMetadataNotRead(ReadMetadataFailureReason aReason)
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-20 22:53:32 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_DECODING_METADATA);
|
2015-04-30 03:07:55 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-03-20 22:53:32 +03:00
|
|
|
mMetadataRequest.Complete();
|
|
|
|
|
|
|
|
if (aReason == ReadMetadataFailureReason::WAITING_FOR_RESOURCES) {
|
|
|
|
SetState(DECODER_STATE_WAIT_FOR_RESOURCES);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aReason == ReadMetadataFailureReason::METADATA_ERROR);
|
|
|
|
DECODER_WARN("Decode metadata failed, shutting down decoder");
|
|
|
|
DecodeError();
|
|
|
|
}
|
2014-11-06 12:52:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::EnqueueLoadedMetadataEvent()
|
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-11-06 12:52:44 +03:00
|
|
|
nsAutoPtr<MediaInfo> info(new MediaInfo());
|
|
|
|
*info = mInfo;
|
2015-03-05 04:33:40 +03:00
|
|
|
MediaDecoderEventVisibility visibility = mSentLoadedMetadataEvent?
|
|
|
|
MediaDecoderEventVisibility::Suppressed :
|
|
|
|
MediaDecoderEventVisibility::Observable;
|
2014-11-06 12:52:44 +03:00
|
|
|
nsCOMPtr<nsIRunnable> metadataLoadedEvent =
|
2015-03-05 04:33:40 +03:00
|
|
|
new MetadataEventRunner(mDecoder, info, mMetadataTags, visibility);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(metadataLoadedEvent.forget());
|
2015-01-16 18:56:19 +03:00
|
|
|
mSentLoadedMetadataEvent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::EnqueueFirstFrameLoadedEvent()
|
|
|
|
{
|
2015-04-07 22:11:41 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-01-16 18:56:19 +03:00
|
|
|
nsAutoPtr<MediaInfo> info(new MediaInfo());
|
|
|
|
*info = mInfo;
|
2015-03-05 04:33:40 +03:00
|
|
|
MediaDecoderEventVisibility visibility = mSentFirstFrameLoadedEvent?
|
|
|
|
MediaDecoderEventVisibility::Suppressed :
|
|
|
|
MediaDecoderEventVisibility::Observable;
|
2015-01-16 18:56:19 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2015-03-05 04:33:40 +03:00
|
|
|
new FirstFrameLoadedEventRunner(mDecoder, info, visibility);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(event.forget());
|
2015-01-16 18:56:19 +03:00
|
|
|
mSentFirstFrameLoadedEvent = true;
|
2014-11-06 12:52:44 +03:00
|
|
|
}
|
|
|
|
|
2015-07-22 09:28:14 +03:00
|
|
|
bool
|
|
|
|
MediaDecoderStateMachine::IsDecodingFirstFrame()
|
|
|
|
{
|
|
|
|
return mState == DECODER_STATE_DECODING && mDecodingFirstFrame;
|
|
|
|
}
|
|
|
|
|
2015-07-25 11:25:13 +03:00
|
|
|
void
|
2014-11-06 12:52:44 +03:00
|
|
|
MediaDecoderStateMachine::FinishDecodeFirstFrame()
|
2014-06-18 09:07:02 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-04-30 03:07:55 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
2014-11-06 12:52:44 +03:00
|
|
|
DECODER_LOG("FinishDecodeFirstFrame");
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2015-01-16 18:56:19 +03:00
|
|
|
if (!IsRealTime() && !mSentFirstFrameLoadedEvent) {
|
2015-07-03 10:33:56 +03:00
|
|
|
RenderVideoFrames(1);
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
|
2015-06-08 21:30:59 +03:00
|
|
|
// If we don't know the duration by this point, we assume infinity, per spec.
|
2015-06-09 22:16:18 +03:00
|
|
|
if (mDuration.Ref().isNothing()) {
|
|
|
|
mDuration = Some(TimeUnit::FromInfinity());
|
2015-06-08 21:30:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DECODER_LOG("Media duration %lld, "
|
2014-08-20 11:26:00 +04:00
|
|
|
"transportSeekable=%d, mediaSeekable=%d",
|
2015-06-08 21:30:59 +03:00
|
|
|
Duration().ToMicroseconds(), mDecoder->IsTransportSeekable(), mDecoder->IsMediaSeekable());
|
2011-07-12 07:39:23 +04:00
|
|
|
|
2015-07-25 11:28:14 +03:00
|
|
|
if (HasAudio() && !HasVideo() && !mSentFirstFrameLoadedEvent) {
|
2014-03-21 02:47:17 +04:00
|
|
|
// We're playing audio only. We don't need to worry about slow video
|
|
|
|
// decodes causing audio underruns, so don't buffer so much audio in
|
|
|
|
// order to reduce memory usage.
|
|
|
|
mAmpleAudioThresholdUsecs /= NO_VIDEO_AMPLE_AUDIO_DIVISOR;
|
|
|
|
mLowAudioThresholdUsecs /= NO_VIDEO_AMPLE_AUDIO_DIVISOR;
|
2015-01-22 08:53:04 +03:00
|
|
|
mQuickBufferingLowDataThresholdUsecs /= NO_VIDEO_AMPLE_AUDIO_DIVISOR;
|
2014-03-21 02:47:17 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 12:52:44 +03:00
|
|
|
// Get potentially updated metadata
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
mReader->ReadUpdatedMetadata(&mInfo);
|
|
|
|
}
|
|
|
|
|
2015-06-08 20:56:00 +03:00
|
|
|
if (!mNotifyMetadataBeforeFirstFrame) {
|
|
|
|
// If we didn't have duration and/or start time before, we should now.
|
2015-01-16 18:56:19 +03:00
|
|
|
EnqueueLoadedMetadataEvent();
|
2014-11-06 12:52:44 +03:00
|
|
|
}
|
2015-06-08 20:56:00 +03:00
|
|
|
EnqueueFirstFrameLoadedEvent();
|
2014-03-11 07:44:09 +04:00
|
|
|
|
2015-07-22 09:28:14 +03:00
|
|
|
mDecodingFirstFrame = false;
|
2011-07-12 07:39:23 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::SeekCompleted()
|
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-06-18 09:07:02 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-04-21 01:35:52 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_SEEKING);
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2015-03-03 03:46:32 +03:00
|
|
|
int64_t seekTime = mCurrentSeek.mTarget.mTime;
|
|
|
|
int64_t newCurrentTime = seekTime;
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
// Setup timestamp state.
|
2015-07-27 19:21:33 +03:00
|
|
|
nsRefPtr<MediaData> video = VideoQueue().PeekFront();
|
2015-06-08 21:30:59 +03:00
|
|
|
if (seekTime == Duration().ToMicroseconds()) {
|
2015-07-09 06:07:57 +03:00
|
|
|
newCurrentTime = seekTime;
|
2014-06-18 09:07:02 +04:00
|
|
|
} else if (HasAudio()) {
|
2015-07-27 19:21:33 +03:00
|
|
|
MediaData* audio = AudioQueue().PeekFront();
|
2015-02-17 06:21:40 +03:00
|
|
|
// Though we adjust the newCurrentTime in audio-based, and supplemented
|
|
|
|
// by video. For better UX, should NOT bind the slide position to
|
|
|
|
// the first audio data timestamp directly.
|
|
|
|
// While seeking to a position where there's only either audio or video, or
|
|
|
|
// seeking to a position lies before audio or video, we need to check if
|
|
|
|
// seekTime is bounded in suitable duration. See Bug 1112438.
|
|
|
|
int64_t videoStart = video ? video->mTime : seekTime;
|
|
|
|
int64_t audioStart = audio ? audio->mTime : seekTime;
|
2015-07-09 06:07:57 +03:00
|
|
|
newCurrentTime = std::min(audioStart, videoStart);
|
2014-06-18 09:07:02 +04:00
|
|
|
} else {
|
|
|
|
newCurrentTime = video ? video->mTime : seekTime;
|
|
|
|
}
|
|
|
|
|
2015-07-25 11:42:41 +03:00
|
|
|
if (mDecodingFirstFrame) {
|
|
|
|
// We were resuming from dormant, or initiated a seek early.
|
|
|
|
// We can fire loadeddata now.
|
|
|
|
FinishDecodeFirstFrame();
|
|
|
|
}
|
|
|
|
|
2011-07-12 07:39:25 +04:00
|
|
|
// Change state to DECODING or COMPLETED now. SeekingStopped will
|
2012-11-14 23:46:40 +04:00
|
|
|
// call MediaDecoderStateMachine::Seek to reset our state to SEEKING
|
2011-07-12 07:39:25 +04:00
|
|
|
// if we need to seek again.
|
2011-07-12 07:39:34 +04:00
|
|
|
|
2015-07-20 09:29:10 +03:00
|
|
|
bool isLiveStream = mResource->IsLiveStream();
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mPendingSeek.Exists()) {
|
2015-01-16 21:58:00 +03:00
|
|
|
// A new seek target came in while we were processing the old one. No rest
|
|
|
|
// for the seeking.
|
|
|
|
DECODER_LOG("A new seek came along while we were finishing the old one - staying in SEEKING");
|
|
|
|
SetState(DECODER_STATE_SEEKING);
|
2015-06-08 21:30:59 +03:00
|
|
|
} else if (GetMediaTime() == Duration().ToMicroseconds() && !isLiveStream) {
|
2012-01-06 10:40:51 +04:00
|
|
|
// Seeked to end of media, move to COMPLETED state. Note we don't do
|
2015-07-03 10:29:30 +03:00
|
|
|
// this when playing a live stream, since the end of media will advance
|
2012-01-06 10:40:51 +04:00
|
|
|
// once we download more data!
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Changed state from SEEKING (to %lld) to COMPLETED", seekTime);
|
2014-03-11 07:44:10 +04:00
|
|
|
// Explicitly set our state so we don't decode further, and so
|
|
|
|
// we report playback ended to the media element.
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_COMPLETED);
|
2014-03-11 07:44:10 +04:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2011-07-12 07:39:25 +04:00
|
|
|
} else {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Changed state from SEEKING (to %lld) to DECODING", seekTime);
|
2011-07-12 07:39:25 +04:00
|
|
|
StartDecoding();
|
|
|
|
}
|
2014-04-01 07:39:04 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
// Ensure timestamps are up to date.
|
|
|
|
UpdatePlaybackPositionInternal(newCurrentTime);
|
2014-04-01 07:39:04 +04:00
|
|
|
|
|
|
|
// Try to decode another frame to detect if we're at the end...
|
2015-05-08 03:04:22 +03:00
|
|
|
DECODER_LOG("Seek completed, mCurrentPosition=%lld", mCurrentPosition.Ref());
|
2014-04-01 07:39:04 +04:00
|
|
|
|
2014-07-20 04:54:00 +04:00
|
|
|
// Reset quick buffering status. This ensures that if we began the
|
|
|
|
// seek while quick-buffering, we won't bypass quick buffering mode
|
|
|
|
// if we need to buffer after the seek.
|
|
|
|
mQuickBuffering = false;
|
|
|
|
|
2015-04-15 21:12:14 +03:00
|
|
|
mCurrentSeek.Resolve(mState == DECODER_STATE_COMPLETED, __func__);
|
2011-07-12 07:39:32 +04:00
|
|
|
ScheduleStateMachine();
|
2015-03-03 21:34:44 +03:00
|
|
|
|
|
|
|
if (video) {
|
2015-07-03 10:33:56 +03:00
|
|
|
RenderVideoFrames(1);
|
2015-03-03 21:34:44 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &MediaDecoder::Invalidate);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(event.forget());
|
2015-03-03 21:34:44 +03:00
|
|
|
}
|
2011-07-12 07:39:25 +04:00
|
|
|
}
|
|
|
|
|
2015-03-16 08:30:42 +03:00
|
|
|
class DecoderDisposer
|
|
|
|
{
|
2011-07-12 07:39:34 +04:00
|
|
|
public:
|
2015-03-16 08:30:42 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecoderDisposer)
|
|
|
|
DecoderDisposer(MediaDecoder* aDecoder, MediaDecoderStateMachine* aStateMachine)
|
2011-09-21 11:01:00 +04:00
|
|
|
: mDecoder(aDecoder), mStateMachine(aStateMachine) {}
|
2015-03-16 08:30:42 +03:00
|
|
|
|
|
|
|
void OnTaskQueueShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-06-18 09:07:02 +04:00
|
|
|
MOZ_ASSERT(mStateMachine);
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
mStateMachine->BreakCycles();
|
|
|
|
mDecoder->BreakCycles();
|
2012-07-30 18:20:58 +04:00
|
|
|
mStateMachine = nullptr;
|
|
|
|
mDecoder = nullptr;
|
2011-07-12 07:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-03-16 08:30:42 +03:00
|
|
|
virtual ~DecoderDisposer() {}
|
2012-11-14 23:46:40 +04:00
|
|
|
nsRefPtr<MediaDecoder> mDecoder;
|
2014-03-18 04:23:03 +04:00
|
|
|
nsRefPtr<MediaDecoderStateMachine> mStateMachine;
|
2011-07-12 07:39:34 +04:00
|
|
|
};
|
|
|
|
|
2015-09-07 14:32:16 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::DispatchShutdown()
|
|
|
|
{
|
|
|
|
mStreamSink->BeginShutdown();
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &MediaDecoderStateMachine::Shutdown);
|
|
|
|
OwnerThread()->Dispatch(runnable.forget());
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:43:21 +03:00
|
|
|
void
|
2014-12-13 01:22:23 +03:00
|
|
|
MediaDecoderStateMachine::FinishShutdown()
|
2014-12-09 22:43:21 +03:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-12-09 22:43:21 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
// The reader's listeners hold references to the state machine,
|
|
|
|
// creating a cycle which keeps the state machine and its shared
|
|
|
|
// thread pools alive. So break it here.
|
2015-08-06 05:14:15 +03:00
|
|
|
|
|
|
|
// Prevent dangling pointers by disconnecting the listeners.
|
|
|
|
mAudioQueueListener.Disconnect();
|
|
|
|
mVideoQueueListener.Disconnect();
|
2015-09-07 06:38:34 +03:00
|
|
|
mMetadataManager.Disconnect();
|
2014-12-09 22:43:21 +03:00
|
|
|
|
2015-04-25 06:35:06 +03:00
|
|
|
// Disconnect canonicals and mirrors before shutting down our task queue.
|
2015-06-18 00:22:10 +03:00
|
|
|
mBuffered.DisconnectIfConnected();
|
2015-06-03 01:29:50 +03:00
|
|
|
mEstimatedDuration.DisconnectIfConnected();
|
2015-06-03 00:44:26 +03:00
|
|
|
mExplicitDuration.DisconnectIfConnected();
|
2015-04-25 06:35:06 +03:00
|
|
|
mPlayState.DisconnectIfConnected();
|
|
|
|
mNextPlayState.DisconnectIfConnected();
|
2015-05-07 18:19:46 +03:00
|
|
|
mLogicallySeeking.DisconnectIfConnected();
|
2015-04-30 03:27:32 +03:00
|
|
|
mVolume.DisconnectIfConnected();
|
2015-05-01 21:39:42 +03:00
|
|
|
mLogicalPlaybackRate.DisconnectIfConnected();
|
2015-05-02 00:27:29 +03:00
|
|
|
mPreservesPitch.DisconnectIfConnected();
|
2015-08-06 13:05:30 +03:00
|
|
|
mSameOriginMedia.DisconnectIfConnected();
|
2015-06-09 22:16:18 +03:00
|
|
|
mDuration.DisconnectAll();
|
2015-06-30 00:27:16 +03:00
|
|
|
mIsShutdown.DisconnectAll();
|
2015-04-01 04:44:36 +03:00
|
|
|
mNextFrameStatus.DisconnectAll();
|
2015-05-08 03:04:22 +03:00
|
|
|
mCurrentPosition.DisconnectAll();
|
2015-04-01 04:44:36 +03:00
|
|
|
|
2015-05-01 02:21:14 +03:00
|
|
|
// Shut down the watch manager before shutting down our task queue.
|
|
|
|
mWatchManager.Shutdown();
|
|
|
|
|
2014-12-09 22:43:21 +03:00
|
|
|
MOZ_ASSERT(mState == DECODER_STATE_SHUTDOWN,
|
|
|
|
"How did we escape from the shutdown state?");
|
|
|
|
// We must daisy-chain these events to destroy the decoder. We must
|
|
|
|
// destroy the decoder on the main thread, but we can't destroy the
|
|
|
|
// decoder while this thread holds the decoder monitor. We can't
|
|
|
|
// dispatch an event to the main thread to destroy the decoder from
|
|
|
|
// here, as the event may run before the dispatch returns, and we
|
|
|
|
// hold the decoder monitor here. We also want to guarantee that the
|
|
|
|
// state machine is destroyed on the main thread, and so the
|
|
|
|
// event runner running this function (which holds a reference to the
|
|
|
|
// state machine) needs to finish and be released in order to allow
|
|
|
|
// that. So we dispatch an event to run after this event runner has
|
|
|
|
// finished and released its monitor/references. That event then will
|
|
|
|
// dispatch an event to the main thread to release the decoder and
|
|
|
|
// state machine.
|
2015-03-16 08:30:42 +03:00
|
|
|
DECODER_LOG("Shutting down state machine task queue");
|
|
|
|
RefPtr<DecoderDisposer> disposer = new DecoderDisposer(mDecoder, this);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->BeginShutdown()->Then(AbstractThread::MainThread(), __func__,
|
|
|
|
disposer.get(),
|
|
|
|
&DecoderDisposer::OnTaskQueueShutdown,
|
|
|
|
&DecoderDisposer::OnTaskQueueShutdown);
|
2014-12-09 22:43:21 +03:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
nsresult MediaDecoderStateMachine::RunStateMachine()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-10 19:59:30 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-03-18 00:23:12 +03:00
|
|
|
mDelayedScheduler.Reset(); // Must happen on state machine task queue.
|
2015-03-10 19:59:30 +03:00
|
|
|
mDispatchedStateMachine = false;
|
|
|
|
|
2015-07-20 09:29:10 +03:00
|
|
|
MediaResource* resource = mResource;
|
2012-02-15 08:35:01 +04:00
|
|
|
NS_ENSURE_TRUE(resource, NS_ERROR_NULL_POINTER);
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
switch (mState) {
|
2015-05-06 04:08:09 +03:00
|
|
|
case DECODER_STATE_ERROR:
|
|
|
|
case DECODER_STATE_SHUTDOWN:
|
|
|
|
case DECODER_STATE_DORMANT:
|
2015-03-23 05:31:15 +03:00
|
|
|
case DECODER_STATE_WAIT_FOR_CDM:
|
2015-05-06 04:08:09 +03:00
|
|
|
case DECODER_STATE_WAIT_FOR_RESOURCES:
|
2013-06-10 16:22:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2014-08-21 07:37:00 +04:00
|
|
|
case DECODER_STATE_DECODING_NONE: {
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_DECODING_METADATA);
|
2015-03-20 22:53:32 +03:00
|
|
|
ScheduleStateMachine();
|
|
|
|
return NS_OK;
|
2011-07-12 07:39:32 +04:00
|
|
|
}
|
2013-01-24 16:38:32 +04:00
|
|
|
|
2014-08-21 07:37:00 +04:00
|
|
|
case DECODER_STATE_DECODING_METADATA: {
|
2015-03-20 22:53:32 +03:00
|
|
|
if (!mMetadataRequest.Exists()) {
|
2015-05-01 08:37:29 +03:00
|
|
|
DECODER_LOG("Dispatching AsyncReadMetadata");
|
2015-07-20 09:29:10 +03:00
|
|
|
// Set mode to METADATA since we are about to read metadata.
|
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_METADATA);
|
2015-08-11 01:42:45 +03:00
|
|
|
mMetadataRequest.Begin(InvokeAsync(DecodeTaskQueue(), mReader.get(), __func__,
|
|
|
|
&MediaDecoderReader::AsyncReadMetadata)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaDecoderStateMachine::OnMetadataRead,
|
|
|
|
&MediaDecoderStateMachine::OnMetadataNotRead));
|
2015-03-20 22:53:32 +03:00
|
|
|
|
|
|
|
}
|
2014-08-21 07:37:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
case DECODER_STATE_DECODING: {
|
2015-07-22 09:28:14 +03:00
|
|
|
if (IsDecodingFirstFrame()) {
|
2015-07-25 11:25:13 +03:00
|
|
|
// We haven't completed decoding our first frames, we can't start
|
|
|
|
// playback yet.
|
2015-07-22 09:28:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-25 06:35:06 +03:00
|
|
|
if (mPlayState != MediaDecoder::PLAY_STATE_PLAYING && IsPlaying())
|
2011-07-12 07:39:37 +04:00
|
|
|
{
|
|
|
|
// We're playing, but the element/decoder is in paused state. Stop
|
2014-03-11 07:44:09 +04:00
|
|
|
// playing!
|
2011-07-12 07:39:37 +04:00
|
|
|
StopPlayback();
|
|
|
|
}
|
|
|
|
|
2014-12-30 10:16:48 +03:00
|
|
|
// Start playback if necessary so that the clock can be properly queried.
|
|
|
|
MaybeStartPlayback();
|
2013-01-24 23:28:48 +04:00
|
|
|
|
2015-03-30 01:54:58 +03:00
|
|
|
UpdateRenderedVideoFrames();
|
2015-06-11 05:16:33 +03:00
|
|
|
NS_ASSERTION(!IsPlaying() ||
|
2015-05-07 18:19:46 +03:00
|
|
|
mLogicallySeeking ||
|
2015-06-17 05:31:05 +03:00
|
|
|
IsStateMachineScheduled(),
|
|
|
|
"Must have timer scheduled");
|
2011-07-12 07:39:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
case DECODER_STATE_BUFFERING: {
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
NS_ASSERTION(!mBufferingStart.IsNull(), "Must know buffering start time.");
|
|
|
|
|
2014-12-22 11:20:30 +03:00
|
|
|
// With buffering heuristics we will remain in the buffering state if
|
|
|
|
// we've not decoded enough data to begin playback, or if we've not
|
|
|
|
// downloaded a reasonable amount of data inside our buffering time.
|
|
|
|
if (mReader->UseBufferingHeuristics()) {
|
|
|
|
TimeDuration elapsed = now - mBufferingStart;
|
2015-03-23 13:08:05 +03:00
|
|
|
bool isLiveStream = resource->IsLiveStream();
|
2014-12-22 11:20:30 +03:00
|
|
|
if ((isLiveStream || !mDecoder->CanPlayThrough()) &&
|
|
|
|
elapsed < TimeDuration::FromSeconds(mBufferingWait * mPlaybackRate) &&
|
2015-01-22 08:53:04 +03:00
|
|
|
(mQuickBuffering ? HasLowDecodedData(mQuickBufferingLowDataThresholdUsecs)
|
2014-12-22 11:20:30 +03:00
|
|
|
: HasLowUndecodedData(mBufferingWait * USECS_PER_S)) &&
|
2015-07-22 15:58:00 +03:00
|
|
|
mResource->IsExpectingMoreData())
|
2014-12-22 11:20:30 +03:00
|
|
|
{
|
|
|
|
DECODER_LOG("Buffering: wait %ds, timeout in %.3lfs %s",
|
|
|
|
mBufferingWait, mBufferingWait - elapsed.ToSeconds(),
|
|
|
|
(mQuickBuffering ? "(quick exit)" : ""));
|
2015-03-10 19:59:30 +03:00
|
|
|
ScheduleStateMachineIn(USECS_PER_S);
|
2014-12-22 11:20:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-12-22 11:20:31 +03:00
|
|
|
} else if (OutOfDecodedAudio() || OutOfDecodedVideo()) {
|
2014-12-22 11:20:31 +03:00
|
|
|
MOZ_ASSERT(mReader->IsWaitForDataSupported(),
|
|
|
|
"Don't yet have a strategy for non-heuristic + non-WaitForData");
|
2014-12-22 11:20:30 +03:00
|
|
|
DispatchDecodeTasksIfNeeded();
|
2015-02-04 07:09:55 +03:00
|
|
|
MOZ_ASSERT_IF(!mMinimizePreroll && OutOfDecodedAudio(), mAudioDataRequest.Exists() || mAudioWaitRequest.Exists());
|
|
|
|
MOZ_ASSERT_IF(!mMinimizePreroll && OutOfDecodedVideo(), mVideoDataRequest.Exists() || mVideoWaitRequest.Exists());
|
2014-12-22 11:20:31 +03:00
|
|
|
DECODER_LOG("In buffering mode, waiting to be notified: outOfAudio: %d, "
|
2015-02-04 07:09:55 +03:00
|
|
|
"mAudioStatus: %s, outOfVideo: %d, mVideoStatus: %s",
|
|
|
|
OutOfDecodedAudio(), AudioRequestStatus(),
|
|
|
|
OutOfDecodedVideo(), VideoRequestStatus());
|
2011-07-12 07:39:32 +04:00
|
|
|
return NS_OK;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:20:30 +03:00
|
|
|
DECODER_LOG("Changed state from BUFFERING to DECODING");
|
|
|
|
DECODER_LOG("Buffered for %.3lfs", (now - mBufferingStart).ToSeconds());
|
|
|
|
StartDecoding();
|
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
NS_ASSERTION(IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
case DECODER_STATE_SEEKING: {
|
2015-03-03 03:46:32 +03:00
|
|
|
if (mPendingSeek.Exists()) {
|
2015-02-21 02:34:34 +03:00
|
|
|
InitiateSeek();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-07-12 07:39:32 +04:00
|
|
|
}
|
2011-01-18 03:53:18 +03:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
case DECODER_STATE_COMPLETED: {
|
2015-06-11 05:16:33 +03:00
|
|
|
if (mPlayState != MediaDecoder::PLAY_STATE_PLAYING && IsPlaying()) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
2011-07-12 07:39:32 +04:00
|
|
|
// Play the remaining media. We want to run AdvanceFrame() at least
|
|
|
|
// once to ensure the current playback position is advanced to the
|
|
|
|
// end of the media, and so that we update the readyState.
|
2015-07-03 10:29:30 +03:00
|
|
|
if (VideoQueue().GetSize() > 1 ||
|
2014-03-11 07:44:09 +04:00
|
|
|
(HasAudio() && !mAudioCompleted) ||
|
2015-09-07 14:32:16 +03:00
|
|
|
(mAudioCaptured && !mStreamSink->IsFinished()))
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-06-11 05:16:33 +03:00
|
|
|
// Start playback if necessary to play the remaining media.
|
2015-08-27 09:20:18 +03:00
|
|
|
MaybeStartPlayback();
|
2015-03-30 01:54:58 +03:00
|
|
|
UpdateRenderedVideoFrames();
|
2015-06-11 05:16:33 +03:00
|
|
|
NS_ASSERTION(!IsPlaying() ||
|
2015-05-07 18:19:46 +03:00
|
|
|
mLogicallySeeking ||
|
2015-06-17 05:31:05 +03:00
|
|
|
IsStateMachineScheduled(),
|
2011-07-12 07:39:34 +04:00
|
|
|
"Must have timer scheduled");
|
2011-07-12 07:39:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
// StopPlayback in order to reset the IsPlaying() state so audio
|
|
|
|
// is restarted correctly.
|
|
|
|
StopPlayback();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
if (mState != DECODER_STATE_COMPLETED) {
|
2011-07-12 07:39:34 +04:00
|
|
|
// While we're presenting a frame we can change state. Whatever changed
|
|
|
|
// our state should have scheduled another state machine run.
|
2011-07-12 07:39:32 +04:00
|
|
|
NS_ASSERTION(IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
2013-01-14 09:25:02 +04:00
|
|
|
|
2015-04-25 06:35:06 +03:00
|
|
|
if (mPlayState == MediaDecoder::PLAY_STATE_PLAYING &&
|
2015-04-08 02:20:20 +03:00
|
|
|
!mSentPlaybackEndedEvent)
|
|
|
|
{
|
2015-07-16 05:12:52 +03:00
|
|
|
int64_t clockTime = std::max(AudioEndTime(), mVideoFrameEndTime);
|
2015-06-08 21:30:59 +03:00
|
|
|
clockTime = std::max(int64_t(0), std::max(clockTime, Duration().ToMicroseconds()));
|
2011-07-12 07:39:32 +04:00
|
|
|
UpdatePlaybackPosition(clockTime);
|
2013-12-06 16:01:33 +04:00
|
|
|
|
2015-04-08 02:20:20 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &MediaDecoder::PlaybackEnded);
|
2015-04-15 21:12:14 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(event.forget());
|
2015-04-08 02:20:20 +03:00
|
|
|
|
|
|
|
mSentPlaybackEndedEvent = true;
|
2015-07-16 05:14:42 +03:00
|
|
|
|
2015-09-02 09:14:40 +03:00
|
|
|
// MediaSink::GetEndTime() must be called before stopping playback.
|
2015-09-09 05:12:23 +03:00
|
|
|
StopMediaSink();
|
2015-07-19 16:25:02 +03:00
|
|
|
}
|
2015-07-16 05:14:42 +03:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
return NS_OK;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-30 03:23:04 +04:00
|
|
|
void
|
2015-03-19 19:53:01 +03:00
|
|
|
MediaDecoderStateMachine::Reset()
|
2014-06-30 03:23:04 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-09-03 08:01:00 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-03-19 19:53:01 +03:00
|
|
|
DECODER_LOG("MediaDecoderStateMachine::Reset");
|
2014-06-30 03:23:04 +04:00
|
|
|
|
2015-03-19 19:53:01 +03:00
|
|
|
// We should be resetting because we're seeking, shutting down, or entering
|
|
|
|
// dormant state. We could also be in the process of going dormant, and have
|
|
|
|
// just switched to exiting dormant before we finished entering dormant,
|
|
|
|
// hence the DECODING_NONE case below.
|
2015-03-24 02:28:43 +03:00
|
|
|
MOZ_ASSERT(IsShutdown() ||
|
|
|
|
mState == DECODER_STATE_SEEKING ||
|
2015-03-19 19:53:01 +03:00
|
|
|
mState == DECODER_STATE_DORMANT ||
|
|
|
|
mState == DECODER_STATE_DECODING_NONE);
|
2014-06-30 03:23:04 +04:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
// Stop the audio thread. Otherwise, MediaSink might be accessing AudioQueue
|
2015-03-19 19:53:01 +03:00
|
|
|
// outside of the decoder monitor while we are clearing the queue and causes
|
|
|
|
// crash for no samples to be popped.
|
2015-09-09 05:12:23 +03:00
|
|
|
StopMediaSink();
|
2014-06-30 03:23:04 +04:00
|
|
|
|
2015-03-19 19:53:01 +03:00
|
|
|
mVideoFrameEndTime = -1;
|
|
|
|
mDecodedVideoEndTime = -1;
|
|
|
|
mDecodedAudioEndTime = -1;
|
|
|
|
mAudioCompleted = false;
|
|
|
|
AudioQueue().Reset();
|
|
|
|
VideoQueue().Reset();
|
|
|
|
mFirstVideoFrameAfterSeek = nullptr;
|
|
|
|
mDropAudioUntilNextDiscontinuity = true;
|
|
|
|
mDropVideoUntilNextDiscontinuity = true;
|
|
|
|
mDecodeToSeekTarget = false;
|
2015-02-17 18:50:49 +03:00
|
|
|
|
2015-03-20 22:53:32 +03:00
|
|
|
mMetadataRequest.DisconnectIfExists();
|
2015-02-21 02:34:34 +03:00
|
|
|
mAudioDataRequest.DisconnectIfExists();
|
|
|
|
mAudioWaitRequest.DisconnectIfExists();
|
|
|
|
mVideoDataRequest.DisconnectIfExists();
|
|
|
|
mVideoWaitRequest.DisconnectIfExists();
|
|
|
|
mSeekRequest.DisconnectIfExists();
|
2015-02-17 18:50:49 +03:00
|
|
|
|
2015-04-07 22:20:43 +03:00
|
|
|
nsCOMPtr<nsIRunnable> resetTask =
|
2015-02-21 02:34:34 +03:00
|
|
|
NS_NewRunnableMethod(mReader, &MediaDecoderReader::ResetDecode);
|
2015-04-15 21:12:14 +03:00
|
|
|
DecodeTaskQueue()->Dispatch(resetTask.forget());
|
2015-02-17 18:50:49 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 18:39:49 +03:00
|
|
|
bool MediaDecoderStateMachine::CheckFrameValidity(VideoData* aData)
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-07-03 10:33:56 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
2012-04-30 07:12:42 +04:00
|
|
|
|
2015-08-20 18:39:49 +03:00
|
|
|
// If we've sent this frame before then only return the valid state,
|
|
|
|
// don't update the statistics.
|
|
|
|
if (aData->mSentToCompositor) {
|
|
|
|
return !aData->mImage || aData->mImage->IsValid();
|
|
|
|
}
|
|
|
|
|
2015-07-03 10:33:56 +03:00
|
|
|
// Update corrupt-frames statistics
|
|
|
|
if (aData->mImage && !aData->mImage->IsValid()) {
|
|
|
|
MediaDecoder::FrameStatistics& frameStats = mDecoder->GetFrameStatistics();
|
|
|
|
frameStats.NotifyCorruptFrame();
|
|
|
|
// If more than 10% of the last 30 frames have been corrupted, then try disabling
|
|
|
|
// hardware acceleration. We use 10 as the corrupt value because RollingMean<>
|
|
|
|
// only supports integer types.
|
|
|
|
mCorruptFrames.insert(10);
|
2015-08-20 18:43:36 +03:00
|
|
|
if (mReader->VideoIsHardwareAccelerated() &&
|
2015-09-04 03:37:31 +03:00
|
|
|
frameStats.GetPresentedFrames() > 60 &&
|
|
|
|
mCorruptFrames.mean() >= 2 /* 20% */) {
|
2015-04-07 22:20:43 +03:00
|
|
|
nsCOMPtr<nsIRunnable> task =
|
|
|
|
NS_NewRunnableMethod(mReader, &MediaDecoderReader::DisableHardwareAcceleration);
|
2015-04-15 21:12:14 +03:00
|
|
|
DecodeTaskQueue()->Dispatch(task.forget());
|
2015-08-20 18:43:36 +03:00
|
|
|
mCorruptFrames.clear();
|
2015-07-10 23:49:31 +03:00
|
|
|
gfxCriticalNote << "Too many dropped/corrupted frames, disabling DXVA";
|
2015-07-03 10:33:56 +03:00
|
|
|
}
|
2015-08-20 18:39:49 +03:00
|
|
|
return false;
|
2015-07-03 10:33:56 +03:00
|
|
|
} else {
|
|
|
|
mCorruptFrames.insert(0);
|
2015-08-20 18:39:49 +03:00
|
|
|
return true;
|
2015-07-03 10:33:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaDecoderStateMachine::RenderVideoFrames(int32_t aMaxFrames,
|
|
|
|
int64_t aClockTime,
|
|
|
|
const TimeStamp& aClockTimeStamp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
|
|
|
|
VideoFrameContainer* container = mDecoder->GetVideoFrameContainer();
|
2015-07-27 19:21:33 +03:00
|
|
|
nsAutoTArray<nsRefPtr<MediaData>,16> frames;
|
2015-07-03 10:33:56 +03:00
|
|
|
VideoQueue().GetFirstElements(aMaxFrames, &frames);
|
|
|
|
if (frames.IsEmpty() || !container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<ImageContainer::NonOwningImage,16> images;
|
|
|
|
TimeStamp lastFrameTime;
|
|
|
|
for (uint32_t i = 0; i < frames.Length(); ++i) {
|
2015-07-27 19:21:33 +03:00
|
|
|
VideoData* frame = frames[i]->As<VideoData>();
|
2015-08-20 18:39:49 +03:00
|
|
|
|
|
|
|
bool valid = CheckFrameValidity(frame);
|
2015-07-03 10:33:56 +03:00
|
|
|
frame->mSentToCompositor = true;
|
|
|
|
|
2015-08-20 18:39:49 +03:00
|
|
|
if (!valid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-03 10:33:56 +03:00
|
|
|
int64_t frameTime = frame->mTime;
|
|
|
|
if (frameTime < 0) {
|
|
|
|
// Frame times before the start time are invalid; drop such frames
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-20 18:39:49 +03:00
|
|
|
|
2015-07-03 10:33:56 +03:00
|
|
|
TimeStamp t;
|
|
|
|
if (aMaxFrames > 1) {
|
|
|
|
MOZ_ASSERT(!aClockTimeStamp.IsNull());
|
|
|
|
int64_t delta = frame->mTime - aClockTime;
|
|
|
|
t = aClockTimeStamp +
|
|
|
|
TimeDuration::FromMicroseconds(delta / mPlaybackRate);
|
|
|
|
if (!lastFrameTime.IsNull() && t <= lastFrameTime) {
|
|
|
|
// Timestamps out of order; drop the new frame. In theory we should
|
|
|
|
// probably replace the previous frame with the new frame if the
|
|
|
|
// timestamps are equal, but this is a corrupt video file already so
|
|
|
|
// never mind.
|
|
|
|
continue;
|
2015-03-12 12:14:51 +03:00
|
|
|
}
|
2015-07-03 10:33:56 +03:00
|
|
|
lastFrameTime = t;
|
2015-03-12 12:14:04 +03:00
|
|
|
}
|
2015-07-03 10:33:56 +03:00
|
|
|
|
|
|
|
ImageContainer::NonOwningImage* img = images.AppendElement();
|
|
|
|
img->mTimeStamp = t;
|
|
|
|
img->mImage = frame->mImage;
|
|
|
|
img->mFrameID = frame->mFrameID;
|
|
|
|
img->mProducerID = mProducerID;
|
|
|
|
|
2015-08-12 02:02:41 +03:00
|
|
|
VERBOSE_LOG("playing video frame %lld (id=%x) (queued=%i, state-machine=%i, decoder-queued=%i)",
|
2015-07-03 10:33:56 +03:00
|
|
|
frame->mTime, frame->mFrameID,
|
|
|
|
VideoQueue().GetSize() + mReader->SizeOfVideoQueueInFrames(),
|
|
|
|
VideoQueue().GetSize(), mReader->SizeOfVideoQueueInFrames());
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
2015-07-03 10:33:56 +03:00
|
|
|
|
2015-07-27 19:21:33 +03:00
|
|
|
container->SetCurrentFrames(frames[0]->As<VideoData>()->mDisplay, images);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2015-09-10 05:19:42 +03:00
|
|
|
int64_t
|
|
|
|
MediaDecoderStateMachine::GetClock(TimeStamp* aTimeStamp) const
|
2013-12-12 16:33:01 +04:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-09-10 05:19:42 +03:00
|
|
|
int64_t clockTime = mMediaSink->GetPosition(aTimeStamp);
|
|
|
|
NS_ASSERTION(GetMediaTime() <= clockTime, "Clock should go forwards.");
|
|
|
|
return clockTime;
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2015-03-30 01:54:58 +03:00
|
|
|
void MediaDecoderStateMachine::UpdateRenderedVideoFrames()
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2015-06-11 05:16:33 +03:00
|
|
|
if (!IsPlaying() || mLogicallySeeking) {
|
2012-11-22 14:38:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-02 08:58:31 +03:00
|
|
|
if (mAudioCaptured) {
|
2015-08-24 05:04:21 +03:00
|
|
|
DiscardStreamData();
|
2015-01-08 08:36:29 +03:00
|
|
|
}
|
|
|
|
|
2015-03-30 03:40:06 +03:00
|
|
|
TimeStamp nowTime;
|
2015-03-30 06:00:57 +03:00
|
|
|
const int64_t clockTime = GetClock(&nowTime);
|
2011-07-12 07:39:32 +04:00
|
|
|
// Skip frames up to the frame at the playback position, and figure out
|
2015-07-03 10:29:30 +03:00
|
|
|
// the time remaining until it's time to display the next frame and drop
|
|
|
|
// the current frame.
|
2015-03-30 06:00:57 +03:00
|
|
|
NS_ASSERTION(clockTime >= 0, "Should have positive clock time.");
|
2015-07-03 10:29:30 +03:00
|
|
|
int64_t remainingTime = AUDIO_DURATION_USECS;
|
2014-04-28 05:12:50 +04:00
|
|
|
if (VideoQueue().GetSize() > 0) {
|
2015-07-27 19:21:33 +03:00
|
|
|
nsRefPtr<MediaData> currentFrame = VideoQueue().PopFront();
|
2015-07-03 10:29:30 +03:00
|
|
|
int32_t framesRemoved = 0;
|
|
|
|
while (VideoQueue().GetSize() > 0) {
|
2015-07-27 19:21:33 +03:00
|
|
|
MediaData* nextFrame = VideoQueue().PeekFront();
|
2015-07-03 10:29:30 +03:00
|
|
|
if (!IsRealTime() && nextFrame->mTime > clockTime) {
|
|
|
|
remainingTime = nextFrame->mTime - clockTime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++framesRemoved;
|
2015-07-27 19:21:33 +03:00
|
|
|
if (!currentFrame->As<VideoData>()->mSentToCompositor) {
|
2015-03-03 07:46:48 +03:00
|
|
|
mDecoder->NotifyDecodedFrames(0, 0, 1);
|
2015-07-03 10:29:30 +03:00
|
|
|
VERBOSE_LOG("discarding video frame mTime=%lld clock_time=%lld",
|
|
|
|
currentFrame->mTime, clockTime);
|
2015-03-03 07:46:48 +03:00
|
|
|
}
|
2015-07-03 10:29:30 +03:00
|
|
|
currentFrame = VideoQueue().PopFront();
|
2015-07-03 10:33:56 +03:00
|
|
|
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
2015-07-03 10:29:30 +03:00
|
|
|
VideoQueue().PushFront(currentFrame);
|
|
|
|
if (framesRemoved > 0) {
|
|
|
|
mVideoFrameEndTime = currentFrame->GetEndTime();
|
2015-07-03 10:33:56 +03:00
|
|
|
MediaDecoder::FrameStatistics& frameStats = mDecoder->GetFrameStatistics();
|
|
|
|
frameStats.NotifyPresentedFrame();
|
2011-07-12 07:39:32 +04:00
|
|
|
}
|
|
|
|
}
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2015-06-15 06:24:57 +03:00
|
|
|
RenderVideoFrames(sVideoQueueSendToCompositorSize, clockTime, nowTime);
|
2015-07-03 10:33:56 +03:00
|
|
|
|
2011-07-12 07:39:32 +04:00
|
|
|
// Check to see if we don't have enough data to play up to the next frame.
|
|
|
|
// If we don't, switch to buffering mode.
|
|
|
|
if (mState == DECODER_STATE_DECODING &&
|
2015-04-25 06:35:06 +03:00
|
|
|
mPlayState == MediaDecoder::PLAY_STATE_PLAYING &&
|
2015-07-22 15:58:00 +03:00
|
|
|
mResource->IsExpectingMoreData()) {
|
2014-12-22 11:20:31 +03:00
|
|
|
bool shouldBuffer;
|
|
|
|
if (mReader->UseBufferingHeuristics()) {
|
|
|
|
shouldBuffer = HasLowDecodedData(remainingTime + EXHAUSTED_DATA_MARGIN_USECS) &&
|
|
|
|
(JustExitedQuickBuffering() || HasLowUndecodedData());
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mReader->IsWaitForDataSupported());
|
2015-02-04 07:09:55 +03:00
|
|
|
shouldBuffer = (OutOfDecodedAudio() && mAudioWaitRequest.Exists()) ||
|
|
|
|
(OutOfDecodedVideo() && mVideoWaitRequest.Exists());
|
2014-12-22 11:20:31 +03:00
|
|
|
}
|
|
|
|
if (shouldBuffer) {
|
2013-11-19 18:01:14 +04:00
|
|
|
StartBuffering();
|
2014-03-24 15:58:11 +04:00
|
|
|
// Don't go straight back to the state machine loop since that might
|
|
|
|
// cause us to start decoding again and we could flip-flop between
|
|
|
|
// decoding and quick-buffering.
|
2015-03-10 19:59:30 +03:00
|
|
|
ScheduleStateMachineIn(USECS_PER_S);
|
2013-11-19 18:01:14 +04:00
|
|
|
return;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
2011-07-12 07:39:32 +04:00
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-03-03 05:43:38 +03:00
|
|
|
// Cap the current time to the larger of the audio and video end time.
|
|
|
|
// This ensures that if we're running off the system clock, we don't
|
|
|
|
// advance the clock to after the media end time.
|
2015-07-16 05:12:52 +03:00
|
|
|
if (mVideoFrameEndTime != -1 || AudioEndTime() != -1) {
|
2015-03-03 05:43:38 +03:00
|
|
|
// These will be non -1 if we've displayed a video frame, or played an audio frame.
|
2015-07-16 05:12:52 +03:00
|
|
|
int64_t t = std::min(clockTime, std::max(mVideoFrameEndTime, AudioEndTime()));
|
2015-03-03 05:43:38 +03:00
|
|
|
// FIXME: Bug 1091422 - chained ogg files hit this assertion.
|
|
|
|
//MOZ_ASSERT(t >= GetMediaTime());
|
|
|
|
if (t > GetMediaTime()) {
|
|
|
|
UpdatePlaybackPosition(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Note we have to update playback position before releasing the monitor.
|
|
|
|
// Otherwise, MediaDecoder::AddOutputStream could kick in when we are outside
|
|
|
|
// the monitor and get a staled value from GetCurrentTimeUs() which hits the
|
|
|
|
// assertion in GetClock().
|
|
|
|
|
2015-07-03 10:29:30 +03:00
|
|
|
int64_t delay = std::max<int64_t>(1, remainingTime / mPlaybackRate);
|
|
|
|
ScheduleStateMachineIn(delay);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
nsresult
|
|
|
|
MediaDecoderStateMachine::DropVideoUpToSeekTarget(VideoData* aSample)
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<VideoData> video(aSample);
|
2014-08-07 04:02:56 +04:00
|
|
|
MOZ_ASSERT(video);
|
2015-03-28 00:53:37 +03:00
|
|
|
DECODER_LOG("DropVideoUpToSeekTarget() frame [%lld, %lld]",
|
|
|
|
video->mTime, video->GetEndTime());
|
2015-03-03 03:46:32 +03:00
|
|
|
MOZ_ASSERT(mCurrentSeek.Exists());
|
|
|
|
const int64_t target = mCurrentSeek.mTarget.mTime;
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
// If the frame end time is less than the seek target, we won't want
|
|
|
|
// to display this frame after the seek, so discard it.
|
|
|
|
if (target >= video->GetEndTime()) {
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("DropVideoUpToSeekTarget() pop video frame [%lld, %lld] target=%lld",
|
2014-06-18 09:07:02 +04:00
|
|
|
video->mTime, video->GetEndTime(), target);
|
|
|
|
mFirstVideoFrameAfterSeek = video;
|
|
|
|
} else {
|
|
|
|
if (target >= video->mTime && video->GetEndTime() >= target) {
|
|
|
|
// The seek target lies inside this frame's time slice. Adjust the frame's
|
|
|
|
// start time to match the seek target. We do this by replacing the
|
|
|
|
// first frame with a shallow copy which has the new timestamp.
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<VideoData> temp = VideoData::ShallowCopyUpdateTimestamp(video, target);
|
2014-06-18 09:07:02 +04:00
|
|
|
video = temp;
|
|
|
|
}
|
|
|
|
mFirstVideoFrameAfterSeek = nullptr;
|
|
|
|
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("DropVideoUpToSeekTarget() found video frame [%lld, %lld] containing target=%lld",
|
2014-06-18 09:07:02 +04:00
|
|
|
video->mTime, video->GetEndTime(), target);
|
|
|
|
|
2015-04-01 04:44:36 +03:00
|
|
|
PushFront(video);
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2014-11-20 00:01:10 +03:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaDecoderStateMachine::DropAudioUpToSeekTarget(AudioData* aSample)
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<AudioData> audio(aSample);
|
2014-06-18 09:07:02 +04:00
|
|
|
MOZ_ASSERT(audio &&
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.Exists() &&
|
|
|
|
mCurrentSeek.mTarget.mType == SeekTarget::Accurate);
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
CheckedInt64 startFrame = UsecsToFrames(audio->mTime,
|
|
|
|
mInfo.mAudio.mRate);
|
2015-03-03 03:46:32 +03:00
|
|
|
CheckedInt64 targetFrame = UsecsToFrames(mCurrentSeek.mTarget.mTime,
|
2014-06-18 09:07:02 +04:00
|
|
|
mInfo.mAudio.mRate);
|
|
|
|
if (!startFrame.isValid() || !targetFrame.isValid()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (startFrame.value() + audio->mFrames <= targetFrame.value()) {
|
|
|
|
// Our seek target lies after the frames in this AudioData. Don't
|
|
|
|
// push it onto the audio queue, and keep decoding forwards.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (startFrame.value() > targetFrame.value()) {
|
|
|
|
// The seek target doesn't lie in the audio block just after the last
|
|
|
|
// audio frames we've seen which were before the seek target. This
|
|
|
|
// could have been the first audio data we've seen after seek, i.e. the
|
|
|
|
// seek terminated after the seek target in the audio stream. Just
|
|
|
|
// abort the audio decode-to-target, the state machine will play
|
|
|
|
// silence to cover the gap. Typically this happens in poorly muxed
|
|
|
|
// files.
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_WARN("Audio not synced after seek, maybe a poorly muxed file?");
|
2015-04-01 04:44:36 +03:00
|
|
|
Push(audio);
|
2014-06-18 09:07:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The seek target lies somewhere in this AudioData's frames, strip off
|
|
|
|
// any frames which lie before the seek target, so we'll begin playback
|
|
|
|
// exactly at the seek target.
|
|
|
|
NS_ASSERTION(targetFrame.value() >= startFrame.value(),
|
|
|
|
"Target must at or be after data start.");
|
|
|
|
NS_ASSERTION(targetFrame.value() < startFrame.value() + audio->mFrames,
|
|
|
|
"Data must end after target.");
|
|
|
|
|
|
|
|
int64_t framesToPrune = targetFrame.value() - startFrame.value();
|
|
|
|
if (framesToPrune > audio->mFrames) {
|
|
|
|
// We've messed up somehow. Don't try to trim frames, the |frames|
|
|
|
|
// variable below will overflow.
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_WARN("Can't prune more frames that we have!");
|
2014-06-18 09:07:02 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
uint32_t frames = audio->mFrames - static_cast<uint32_t>(framesToPrune);
|
|
|
|
uint32_t channels = audio->mChannels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> audioData(new AudioDataValue[frames * channels]);
|
|
|
|
memcpy(audioData.get(),
|
|
|
|
audio->mAudioData.get() + (framesToPrune * channels),
|
|
|
|
frames * channels * sizeof(AudioDataValue));
|
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, mInfo.mAudio.mRate);
|
|
|
|
if (!duration.isValid()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<AudioData> data(new AudioData(audio->mOffset,
|
2015-03-03 03:46:32 +03:00
|
|
|
mCurrentSeek.mTarget.mTime,
|
2014-11-20 00:01:10 +03:00
|
|
|
duration.value(),
|
|
|
|
frames,
|
|
|
|
audioData.forget(),
|
|
|
|
channels,
|
|
|
|
audio->mRate));
|
2015-04-01 04:44:36 +03:00
|
|
|
PushFront(data);
|
2014-06-18 09:07:02 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-30 03:07:55 +03:00
|
|
|
void MediaDecoderStateMachine::UpdateNextFrameStatus()
|
|
|
|
{
|
2015-04-01 04:44:36 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-04-27 21:51:46 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2015-04-01 04:44:36 +03:00
|
|
|
MediaDecoderOwner::NextFrameStatus status;
|
|
|
|
const char* statusString;
|
2015-07-22 09:28:14 +03:00
|
|
|
if (mState <= DECODER_STATE_WAIT_FOR_CDM || IsDecodingFirstFrame()) {
|
2015-06-12 03:54:13 +03:00
|
|
|
status = MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
statusString = "NEXT_FRAME_UNAVAILABLE";
|
|
|
|
} else if (IsBuffering()) {
|
2015-04-01 04:44:36 +03:00
|
|
|
status = MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE_BUFFERING;
|
|
|
|
statusString = "NEXT_FRAME_UNAVAILABLE_BUFFERING";
|
|
|
|
} else if (IsSeeking()) {
|
|
|
|
status = MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE_SEEKING;
|
|
|
|
statusString = "NEXT_FRAME_UNAVAILABLE_SEEKING";
|
|
|
|
} else if (HaveNextFrameData()) {
|
|
|
|
status = MediaDecoderOwner::NEXT_FRAME_AVAILABLE;
|
|
|
|
statusString = "NEXT_FRAME_AVAILABLE";
|
|
|
|
} else {
|
|
|
|
status = MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
statusString = "NEXT_FRAME_UNAVAILABLE";
|
2012-12-19 08:48:32 +04:00
|
|
|
}
|
2015-04-01 04:44:36 +03:00
|
|
|
|
|
|
|
if (status != mNextFrameStatus) {
|
|
|
|
DECODER_LOG("Changed mNextFrameStatus to %s", statusString);
|
|
|
|
}
|
|
|
|
|
|
|
|
mNextFrameStatus = status;
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
bool MediaDecoderStateMachine::JustExitedQuickBuffering()
|
2011-03-24 01:28:57 +03:00
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2011-03-24 01:28:57 +03:00
|
|
|
return !mDecodeStartTime.IsNull() &&
|
|
|
|
mQuickBuffering &&
|
2012-09-18 22:23:59 +04:00
|
|
|
(TimeStamp::Now() - mDecodeStartTime) < TimeDuration::FromMicroseconds(QUICK_BUFFER_THRESHOLD_USECS);
|
2011-03-24 01:28:57 +03:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaDecoderStateMachine::StartBuffering()
|
2010-07-23 02:48:32 +04:00
|
|
|
{
|
2015-03-25 03:12:54 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-03-03 05:55:33 +04:00
|
|
|
|
2014-03-03 05:49:52 +04:00
|
|
|
if (mState != DECODER_STATE_DECODING) {
|
|
|
|
// We only move into BUFFERING state if we're actually decoding.
|
|
|
|
// If we're currently doing something else, we don't need to buffer,
|
|
|
|
// and more importantly, we shouldn't overwrite mState to interrupt
|
|
|
|
// the current operation, as that could leave us in an inconsistent
|
|
|
|
// state!
|
|
|
|
return;
|
|
|
|
}
|
2010-07-23 02:48:32 +04:00
|
|
|
|
2011-07-12 07:39:37 +04:00
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
|
|
|
|
2011-03-24 01:28:57 +03:00
|
|
|
TimeDuration decodeDuration = TimeStamp::Now() - mDecodeStartTime;
|
|
|
|
// Go into quick buffering mode provided we've not just left buffering using
|
|
|
|
// a "quick exit". This stops us flip-flopping between playing and buffering
|
|
|
|
// when the download speed is similar to the decode speed.
|
|
|
|
mQuickBuffering =
|
|
|
|
!JustExitedQuickBuffering() &&
|
2011-04-14 02:12:23 +04:00
|
|
|
decodeDuration < UsecsToDuration(QUICK_BUFFER_THRESHOLD_USECS);
|
2011-03-24 01:28:57 +03:00
|
|
|
mBufferingStart = TimeStamp::Now();
|
|
|
|
|
2014-08-28 04:46:00 +04:00
|
|
|
SetState(DECODER_STATE_BUFFERING);
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Changed state from DECODING to BUFFERING, decoded for %.3lfs",
|
2014-04-23 13:29:04 +04:00
|
|
|
decodeDuration.ToSeconds());
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaDecoder::Statistics stats = mDecoder->GetStatistics();
|
2014-08-20 11:26:00 +04:00
|
|
|
DECODER_LOG("Playback rate: %.1lfKB/s%s download rate: %.1lfKB/s%s",
|
2013-11-21 07:02:42 +04:00
|
|
|
stats.mPlaybackRate/1024, stats.mPlaybackRateReliable ? "" : " (unreliable)",
|
2014-04-23 13:29:04 +04:00
|
|
|
stats.mDownloadRate/1024, stats.mDownloadRateReliable ? "" : " (unreliable)");
|
2010-07-23 02:48:32 +04:00
|
|
|
}
|
2011-03-24 01:28:58 +03:00
|
|
|
|
2015-04-30 03:07:55 +03:00
|
|
|
void MediaDecoderStateMachine::ScheduleStateMachineWithLockAndWakeDecoder()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-10 19:59:30 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
DispatchAudioDecodeTaskIfNeeded();
|
|
|
|
DispatchVideoDecodeTaskIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-06 07:32:33 +03:00
|
|
|
MediaDecoderStateMachine::ScheduleStateMachine()
|
|
|
|
{
|
2015-05-06 04:17:51 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2013-11-04 02:11:09 +04:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-03-10 19:59:30 +03:00
|
|
|
if (mDispatchedStateMachine) {
|
|
|
|
return;
|
2012-12-04 14:59:36 +04:00
|
|
|
}
|
2015-03-10 19:59:30 +03:00
|
|
|
mDispatchedStateMachine = true;
|
2012-12-04 14:59:36 +04:00
|
|
|
|
2015-04-07 22:20:43 +03:00
|
|
|
nsCOMPtr<nsIRunnable> task =
|
2015-03-10 19:59:30 +03:00
|
|
|
NS_NewRunnableMethod(this, &MediaDecoderStateMachine::RunStateMachine);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2011-07-12 07:39:34 +04:00
|
|
|
}
|
|
|
|
|
2015-03-10 19:59:30 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::ScheduleStateMachineIn(int64_t aMicroseconds)
|
2011-07-12 07:39:34 +04:00
|
|
|
{
|
2015-03-10 19:59:30 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue()); // mDelayedScheduler.Ensure() may Disconnect()
|
2015-03-10 19:59:30 +03:00
|
|
|
// the promise, which must happen on the state
|
2015-03-18 00:23:12 +03:00
|
|
|
// machine task queue.
|
2015-03-10 19:59:30 +03:00
|
|
|
MOZ_ASSERT(aMicroseconds > 0);
|
|
|
|
if (mDispatchedStateMachine) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-10 19:59:30 +03:00
|
|
|
|
2015-03-10 19:59:30 +03:00
|
|
|
// Real-time weirdness.
|
|
|
|
if (IsRealTime()) {
|
|
|
|
aMicroseconds = std::min(aMicroseconds, int64_t(40000));
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
TimeStamp target = now + TimeDuration::FromMicroseconds(aMicroseconds);
|
|
|
|
|
|
|
|
SAMPLE_LOG("Scheduling state machine for %lf ms from now", (target - now).ToMilliseconds());
|
|
|
|
mDelayedScheduler.Ensure(target);
|
2011-07-12 07:39:32 +04:00
|
|
|
}
|
2011-11-08 05:38:17 +04:00
|
|
|
|
2015-03-18 01:14:56 +03:00
|
|
|
bool MediaDecoderStateMachine::OnDecodeTaskQueue() const
|
2014-02-18 02:53:52 +04:00
|
|
|
{
|
2014-12-02 08:51:03 +03:00
|
|
|
return !DecodeTaskQueue() || DecodeTaskQueue()->IsCurrentThreadIn();
|
2014-02-18 02:53:52 +04:00
|
|
|
}
|
|
|
|
|
2015-03-18 00:23:12 +03:00
|
|
|
bool MediaDecoderStateMachine::OnTaskQueue() const
|
2011-11-08 05:38:17 +04:00
|
|
|
{
|
2015-07-16 21:31:21 +03:00
|
|
|
return OwnerThread()->IsCurrentThreadIn();
|
2011-11-08 05:38:17 +04:00
|
|
|
}
|
2012-11-14 23:45:33 +04:00
|
|
|
|
2014-07-11 11:11:00 +04:00
|
|
|
bool MediaDecoderStateMachine::IsStateMachineScheduled() const
|
2011-11-08 05:38:17 +04:00
|
|
|
{
|
2015-05-06 07:32:33 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-10 19:59:30 +03:00
|
|
|
return mDispatchedStateMachine || mDelayedScheduler.IsScheduled();
|
2011-11-08 05:38:17 +04:00
|
|
|
}
|
|
|
|
|
2015-05-01 21:39:42 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::LogicalPlaybackRateChanged()
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2015-05-01 21:39:42 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2012-11-22 14:38:28 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-05-01 21:39:42 +03:00
|
|
|
if (mLogicalPlaybackRate == 0) {
|
|
|
|
// This case is handled in MediaDecoder by pausing playback.
|
2012-11-22 14:38:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:39:42 +03:00
|
|
|
mPlaybackRate = mLogicalPlaybackRate;
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSink->SetPlaybackRate(mPlaybackRate);
|
2015-03-30 05:14:03 +03:00
|
|
|
|
|
|
|
ScheduleStateMachine();
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2015-05-02 00:27:29 +03:00
|
|
|
void MediaDecoderStateMachine::PreservesPitchChanged()
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2015-05-02 00:27:29 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2012-11-22 14:38:28 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSink->SetPreservesPitch(mPreservesPitch);
|
2014-04-01 07:43:57 +04:00
|
|
|
}
|
2013-12-20 07:24:42 +04:00
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
bool MediaDecoderStateMachine::IsShutdown()
|
2012-11-07 02:33:01 +04:00
|
|
|
{
|
2015-06-30 00:27:16 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
return mIsShutdown;
|
2012-11-07 02:33:01 +04:00
|
|
|
}
|
|
|
|
|
2015-07-16 05:12:52 +03:00
|
|
|
int64_t
|
|
|
|
MediaDecoderStateMachine::AudioEndTime() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
AssertCurrentThreadInMonitor();
|
2015-09-09 05:12:23 +03:00
|
|
|
if (mMediaSink->IsStarted()) {
|
|
|
|
return mMediaSink->GetEndTime(TrackInfo::kAudioTrack);
|
2015-07-16 05:13:27 +03:00
|
|
|
}
|
2015-09-02 09:14:40 +03:00
|
|
|
MOZ_ASSERT(!HasAudio());
|
2015-07-17 05:18:04 +03:00
|
|
|
return -1;
|
2015-07-16 05:12:52 +03:00
|
|
|
}
|
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
void MediaDecoderStateMachine::OnMediaSinkComplete()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-04-08 02:10:51 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-07-22 04:54:06 +03:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSinkPromise.Complete();
|
2015-09-09 05:12:35 +03:00
|
|
|
// Set true only when we have audio.
|
|
|
|
mAudioCompleted = mInfo.HasAudio();
|
|
|
|
// To notify PlaybackEnded as soon as possible.
|
|
|
|
ScheduleStateMachine();
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
void MediaDecoderStateMachine::OnMediaSinkError()
|
2014-09-30 01:42:00 +04:00
|
|
|
{
|
2015-03-18 00:23:12 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-25 06:40:40 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-09-30 01:42:00 +04:00
|
|
|
|
2015-09-09 05:12:23 +03:00
|
|
|
mMediaSinkPromise.Complete();
|
2015-09-09 05:12:35 +03:00
|
|
|
// Set true only when we have audio.
|
|
|
|
mAudioCompleted = mInfo.HasAudio();
|
2014-09-30 01:42:00 +04:00
|
|
|
|
2014-10-07 17:55:00 +04:00
|
|
|
// Make the best effort to continue playback when there is video.
|
|
|
|
if (HasVideo()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise notify media decoder/element about this error for it makes
|
|
|
|
// no sense to play an audio-only file without sound output.
|
2015-02-23 01:16:28 +03:00
|
|
|
DecodeError();
|
2014-09-30 01:42:00 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 05:12:35 +03:00
|
|
|
void
|
|
|
|
MediaDecoderStateMachine::SetAudioCaptured(bool aCaptured)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
if (aCaptured == mAudioCaptured) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup current playback parameters.
|
|
|
|
MediaSink::PlaybackParams params = mMediaSink->GetPlaybackParams();
|
|
|
|
|
|
|
|
// Stop and shut down the existing sink.
|
|
|
|
StopMediaSink();
|
|
|
|
mMediaSink->Shutdown();
|
|
|
|
|
|
|
|
// Create a new sink according to whether audio is captured.
|
|
|
|
// TODO: We can't really create a new DecodedStream until OutputStreamManager
|
|
|
|
// is extracted. It is tricky that the implementation of DecodedStream
|
|
|
|
// happens to allow reuse after shutdown without creating a new one.
|
|
|
|
mMediaSink = aCaptured ? mStreamSink : CreateAudioSink();
|
|
|
|
|
|
|
|
// Restore playback parameters.
|
|
|
|
mMediaSink->SetPlaybackParams(params);
|
|
|
|
|
2015-09-10 06:31:30 +03:00
|
|
|
// We don't need to call StartMediaSink() here because IsPlaying() is now
|
|
|
|
// always in sync with the playing state of MediaSink. It will be started in
|
|
|
|
// MaybeStartPlayback() in the next cycle if necessary.
|
2015-09-09 05:12:35 +03:00
|
|
|
|
|
|
|
mAudioCaptured = aCaptured;
|
|
|
|
ScheduleStateMachine();
|
|
|
|
}
|
|
|
|
|
2015-03-09 06:17:30 +03:00
|
|
|
uint32_t MediaDecoderStateMachine::GetAmpleVideoFrames() const
|
|
|
|
{
|
2015-04-30 03:07:55 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-03-09 06:17:30 +03:00
|
|
|
AssertCurrentThreadInMonitor();
|
|
|
|
return (mReader->IsAsync() && mReader->VideoIsHardwareAccelerated())
|
|
|
|
? std::max<uint32_t>(sVideoQueueHWAccelSize, MIN_VIDEO_QUEUE_SIZE)
|
|
|
|
: std::max<uint32_t>(sVideoQueueDefaultSize, MIN_VIDEO_QUEUE_SIZE);
|
|
|
|
}
|
|
|
|
|
2015-05-28 09:17:30 +03:00
|
|
|
void MediaDecoderStateMachine::AddOutputStream(ProcessedMediaStream* aStream,
|
|
|
|
bool aFinishWhenEnded)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
DECODER_LOG("AddOutputStream aStream=%p!", aStream);
|
2015-09-07 14:32:16 +03:00
|
|
|
mStreamSink->AddOutput(aStream, aFinishWhenEnded);
|
2015-09-09 05:12:35 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethodWithArg<bool>(
|
|
|
|
this, &MediaDecoderStateMachine::SetAudioCaptured, true);
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2015-05-28 09:17:30 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:17 +03:00
|
|
|
void MediaDecoderStateMachine::RemoveOutputStream(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
DECODER_LOG("RemoveOutputStream=%p!", aStream);
|
2015-09-07 14:32:16 +03:00
|
|
|
mStreamSink->RemoveOutput(aStream);
|
|
|
|
if (!mStreamSink->HasConsumers()) {
|
2015-09-09 05:12:35 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethodWithArg<bool>(
|
|
|
|
this, &MediaDecoderStateMachine::SetAudioCaptured, false);
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2015-07-24 15:28:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-04-23 13:29:04 +04:00
|
|
|
// avoid redefined macro in unified build
|
2015-05-14 05:15:05 +03:00
|
|
|
#undef LOG
|
2014-04-23 13:29:04 +04:00
|
|
|
#undef DECODER_LOG
|
|
|
|
#undef VERBOSE_LOG
|
2014-08-20 11:26:00 +04:00
|
|
|
#undef DECODER_WARN
|
|
|
|
#undef DECODER_WARN_HELPER
|
2015-04-07 22:20:43 +03:00
|
|
|
|
|
|
|
#undef NS_DispatchToMainThread
|