/usr/include/thunderbird/MediaDecoderStateMachine.h is in thunderbird-dev 1:38.6.0+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
Each media element for a media file has one thread called the "audio thread".
The audio thread writes the decoded audio data to the audio
hardware. This is done in a separate thread to ensure that the
audio hardware gets a constant stream of data without
interruption due to decoding or display. At some point
AudioStream will be refactored to have a callback interface
where it asks for data and this thread will no longer be
needed.
The element/state machine also has a MediaTaskQueue which runs in a
SharedThreadPool that is shared with all other elements/decoders. The state
machine dispatches tasks to this to call into the MediaDecoderReader to
request decoded audio or video data. The Reader will callback with decoded
sampled when it has them available, and the state machine places the decoded
samples into its queues for the consuming threads to pull from.
The MediaDecoderReader can choose to decode asynchronously, or synchronously
and return requested samples synchronously inside it's Request*Data()
functions via callback. Asynchronous decoding is preferred, and should be
used for any new readers.
Synchronisation of state between the thread is done via a monitor owned
by MediaDecoder.
The lifetime of the audio thread is controlled by the state machine when
it runs on the shared state machine thread. When playback needs to occur
the audio thread is created and an event dispatched to run it. The audio
thread exits when audio playback is completed or no longer required.
A/V synchronisation is handled by the state machine. It examines the audio
playback time and compares this to the next frame in the queue of video
frames. If it is time to play the video frame it is then displayed, otherwise
it schedules the state machine to run again at the time of the next frame.
Frame skipping is done in the following ways:
1) The state machine will skip all frames in the video queue whose
display time is less than the current audio time. This ensures
the correct frame for the current time is always displayed.
2) The decode tasks will stop decoding interframes and read to the
next keyframe if it determines that decoding the remaining
interframes will cause playback issues. It detects this by:
a) If the amount of audio data in the audio queue drops
below a threshold whereby audio may start to skip.
b) If the video queue drops below a threshold where it
will be decoding video data that won't be displayed due
to the decode thread dropping the frame immediately.
TODO: In future we should only do this when the Reader is decoding
synchronously.
When hardware accelerated graphics is not available, YCbCr conversion
is done on the decode task queue when video frames are decoded.
The decode task queue pushes decoded audio and videos frames into two
separate queues - one for audio and one for video. These are kept
separate to make it easy to constantly feed audio data to the audio
hardware while allowing frame skipping of video data. These queues are
threadsafe, and neither the decode, audio, or state machine should
be able to monopolize them, and cause starvation of the other threads.
Both queues are bounded by a maximum size. When this size is reached
the decode tasks will no longer request video or audio depending on the
queue that has reached the threshold. If both queues are full, no more
decode tasks will be dispatched to the decode task queue, so other
decoders will have an opportunity to run.
During playback the audio thread will be idle (via a Wait() on the
monitor) if the audio queue is empty. Otherwise it constantly pops
audio data off the queue and plays it with a blocking write to the audio
hardware (via AudioStream).
*/
#if !defined(MediaDecoderStateMachine_h__)
#define MediaDecoderStateMachine_h__
#include "mozilla/Attributes.h"
#include "nsThreadUtils.h"
#include "MediaDecoder.h"
#include "mozilla/ReentrantMonitor.h"
#include "MediaDecoderReader.h"
#include "MediaDecoderOwner.h"
#include "MediaMetadataManager.h"
#include "MediaDecoderStateMachineScheduler.h"
#include "mozilla/RollingMean.h"
class nsITimer;
namespace mozilla {
class AudioSegment;
class VideoSegment;
class MediaTaskQueue;
class SharedThreadPool;
class AudioSink;
class MediaDecoderStateMachineScheduler;
// GetCurrentTime is defined in winbase.h as zero argument macro forwarding to
// GetTickCount() and conflicts with MediaDecoderStateMachine::GetCurrentTime
// implementation.
#ifdef GetCurrentTime
#undef GetCurrentTime
#endif
/*
The state machine class. This manages the decoding and seeking in the
MediaDecoderReader on the decode task queue, and A/V sync on the shared
state machine thread, and controls the audio "push" thread.
All internal state is synchronised via the decoder monitor. State changes
are either propagated by NotifyAll on the monitor (typically when state
changes need to be propagated to non-state machine threads) or by scheduling
the state machine to run another cycle on the shared state machine thread.
See MediaDecoder.h for more details.
*/
class MediaDecoderStateMachine
{
friend class AudioSink;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDecoderStateMachine)
public:
typedef MediaDecoder::DecodedStreamData DecodedStreamData;
MediaDecoderStateMachine(MediaDecoder* aDecoder,
MediaDecoderReader* aReader,
bool aRealTime = false);
nsresult Init(MediaDecoderStateMachine* aCloneDonor);
// Enumeration for the valid decoding states
enum State {
DECODER_STATE_DECODING_NONE,
DECODER_STATE_DECODING_METADATA,
DECODER_STATE_WAIT_FOR_RESOURCES,
DECODER_STATE_DECODING_FIRSTFRAME,
DECODER_STATE_DORMANT,
DECODER_STATE_DECODING,
DECODER_STATE_SEEKING,
DECODER_STATE_BUFFERING,
DECODER_STATE_COMPLETED,
DECODER_STATE_SHUTDOWN
};
State GetState() {
AssertCurrentThreadInMonitor();
return mState;
}
// Set the audio volume. The decoder monitor must be obtained before
// calling this.
void SetVolume(double aVolume);
void SetAudioCaptured();
// Check if the decoder needs to become dormant state.
bool IsDormantNeeded();
// Set/Unset dormant state.
void SetDormant(bool aDormant);
void Shutdown();
void ShutdownReader();
void FinishShutdown();
bool IsRealTime() const;
// Called from the main thread to get the duration. The decoder monitor
// must be obtained before calling this. It is in units of microseconds.
int64_t GetDuration();
// Time of the last frame in the media, in microseconds or INT64_MAX if
// media has an infinite duration.
// Accessed on state machine, decode, and main threads.
// Access controlled by decoder monitor.
int64_t GetEndTime();
// Called from the main thread to set the duration of the media resource
// if it is able to be obtained via HTTP headers. Called from the
// state machine thread to set the duration if it is obtained from the
// media metadata. The decoder monitor must be obtained before calling this.
// aDuration is in microseconds.
// A value of INT64_MAX will be treated as infinity.
void SetDuration(int64_t aDuration);
// Called while decoding metadata to set the end time of the media
// resource. The decoder monitor must be obtained before calling this.
// aEndTime is in microseconds.
void SetMediaEndTime(int64_t aEndTime);
// Called from main thread to update the duration with an estimated value.
// The duration is only changed if its significantly different than the
// the current duration, as the incoming duration is an estimate and so
// often is unstable as more data is read and the estimate is updated.
// Can result in a durationchangeevent. aDuration is in microseconds.
void UpdateEstimatedDuration(int64_t aDuration);
// Functions used by assertions to ensure we're calling things
// on the appropriate threads.
bool OnDecodeThread() const;
bool OnStateMachineThread() const;
MediaDecoderOwner::NextFrameStatus GetNextFrameStatus();
// Cause state transitions. These methods obtain the decoder monitor
// to synchronise the change of state, and to notify other threads
// that the state has changed.
void Play()
{
MOZ_ASSERT(NS_IsMainThread());
nsRefPtr<nsRunnable> r = NS_NewRunnableMethod(this, &MediaDecoderStateMachine::PlayInternal);
GetStateMachineThread()->Dispatch(r, NS_DISPATCH_NORMAL);
}
private:
// The actual work for the above, which happens asynchronously on the state
// machine thread.
void PlayInternal();
public:
// Seeks to the decoder to aTarget asynchronously.
// Must be called from the main thread.
void Seek(const SeekTarget& aTarget);
// Dispatches a task to the main thread to seek to mQueuedSeekTarget.
// This is threadsafe and can be called on any thread.
void EnqueueStartQueuedSeekTask();
// Seeks to the decoder to mQueuedSeekTarget asynchronously.
// Must be called from the main thread.
void StartQueuedSeek();
// Seeks to the decoder to aTarget asynchronously.
// Must be called from the main thread.
// The decoder monitor must be held with exactly one lock count.
void StartSeek(const SeekTarget& aTarget);
// Returns the current playback position in seconds.
// Called from the main thread to get the current frame time. The decoder
// monitor must be obtained before calling this.
double GetCurrentTime() const;
int64_t GetCurrentTimeUs() const;
// Clear the flag indicating that a playback position change event
// is currently queued. This is called from the main thread and must
// be called with the decode monitor held.
void ClearPositionChangeFlag();
// Update the playback position. This can result in a timeupdate event
// and an invalidate of the frame being dispatched asynchronously if
// there is no such event currently queued.
// Only called on the decoder thread. Must be called with
// the decode monitor held.
void UpdatePlaybackPosition(int64_t aTime);
// Causes the state machine to switch to buffering state, and to
// immediately stop playback and buffer downloaded data. Must be called
// with the decode monitor held. Called on the state machine thread and
// the main thread.
void StartBuffering();
// This is called on the state machine thread and audio thread.
// The decoder monitor must be obtained before calling this.
bool HasAudio() const {
AssertCurrentThreadInMonitor();
return mInfo.HasAudio();
}
// This is called on the state machine thread and audio thread.
// The decoder monitor must be obtained before calling this.
bool HasVideo() const {
AssertCurrentThreadInMonitor();
return mInfo.HasVideo();
}
// Should be called by main thread.
bool HaveNextFrameData();
// Must be called with the decode monitor held.
bool IsBuffering() const {
AssertCurrentThreadInMonitor();
return mState == DECODER_STATE_BUFFERING;
}
// Must be called with the decode monitor held.
bool IsSeeking() const {
AssertCurrentThreadInMonitor();
return mState == DECODER_STATE_SEEKING;
}
nsresult GetBuffered(dom::TimeRanges* aBuffered) {
// It's possible for JS to query .buffered before we've determined the start
// time from metadata, in which case the reader isn't ready to be asked this
// question.
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
if (mStartTime < 0) {
return NS_OK;
}
return mReader->GetBuffered(aBuffered);
}
void SetPlaybackRate(double aPlaybackRate);
void SetPreservesPitch(bool aPreservesPitch);
size_t SizeOfVideoQueue() {
if (mReader) {
return mReader->SizeOfVideoQueueInBytes();
}
return 0;
}
size_t SizeOfAudioQueue() {
if (mReader) {
return mReader->SizeOfAudioQueueInBytes();
}
return 0;
}
void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset);
// Returns the shared state machine thread.
nsIEventTarget* GetStateMachineThread() const;
// Calls ScheduleStateMachine() after taking the decoder lock. Also
// notifies the decoder thread in case it's waiting on the decoder lock.
void ScheduleStateMachineWithLockAndWakeDecoder();
// Schedules the shared state machine thread to run the state machine
// in aUsecs microseconds from now, if it's not already scheduled to run
// earlier, in which case the request is discarded.
nsresult ScheduleStateMachine(int64_t aUsecs = 0);
// Callback function registered with MediaDecoderStateMachineScheduler
// to run state machine cycles.
static nsresult TimeoutExpired(void* aClosure);
// Set the media fragment end time. aEndTime is in microseconds.
void SetFragmentEndTime(int64_t aEndTime);
// Drop reference to decoder. Only called during shutdown dance.
void BreakCycles() {
if (mReader) {
mReader->BreakCycles();
}
mDecoder = nullptr;
}
// If we're playing into a MediaStream, record the current point in the
// MediaStream and the current point in our media resource so later we can
// convert MediaStream playback positions to media resource positions. Best to
// call this while we're not playing (while the MediaStream is blocked). Can
// be called on any thread with the decoder monitor held.
void SetSyncPointForMediaStream();
// Called when the decoded stream is destroyed. |mPlayStartTime| and
// |mPlayDuration| are updated to provide a good base for calculating video
// stream time using the system clock.
void ResyncMediaStreamClock();
int64_t GetCurrentTimeViaMediaStreamSync() const;
// Copy queued audio/video data in the reader to any output MediaStreams that
// need it.
void SendStreamData();
void FinishStreamData();
bool HaveEnoughDecodedAudio(int64_t aAmpleAudioUSecs);
bool HaveEnoughDecodedVideo();
// Returns true if the state machine has shutdown or is in the process of
// shutting down. The decoder monitor must be held while calling this.
bool IsShutdown();
void QueueMetadata(int64_t aPublishTime,
nsAutoPtr<MediaInfo> aInfo,
nsAutoPtr<MetadataTags> aTags);
// Returns true if we're currently playing. The decoder monitor must
// be held.
bool IsPlaying() const;
// Dispatch DoNotifyWaitingForResourcesStatusChanged task to the task queue.
// Called when the reader may have acquired the hardware resources required
// to begin decoding. The decoder monitor must be held while calling this.
void NotifyWaitingForResourcesStatusChanged();
// Notifies the state machine that should minimize the number of samples
// decoded we preroll, until playback starts. The first time playback starts
// the state machine is free to return to prerolling normally. Note
// "prerolling" in this context refers to when we decode and buffer decoded
// samples in advance of when they're needed for playback.
void SetMinimizePrerollUntilPlaybackStarts();
void OnAudioDecoded(AudioData* aSample);
void OnVideoDecoded(VideoData* aSample);
void OnNotDecoded(MediaData::Type aType, MediaDecoderReader::NotDecodedReason aReason);
void OnAudioNotDecoded(MediaDecoderReader::NotDecodedReason aReason)
{
OnNotDecoded(MediaData::AUDIO_DATA, aReason);
}
void OnVideoNotDecoded(MediaDecoderReader::NotDecodedReason aReason)
{
OnNotDecoded(MediaData::VIDEO_DATA, aReason);
}
void OnSeekCompleted(int64_t aTime);
void OnSeekFailed(nsresult aResult);
void OnWaitForDataResolved(MediaData::Type aType)
{
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
WaitRequestRef(aType).Complete();
DispatchDecodeTasksIfNeeded();
}
void OnWaitForDataRejected(WaitForDataRejectValue aRejection)
{
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
WaitRequestRef(aRejection.mType).Complete();
}
// Resets all state related to decoding, emptying all buffers etc.
void ResetDecode();
private:
void AcquireMonitorAndInvokeDecodeError();
protected:
virtual ~MediaDecoderStateMachine();
void AssertCurrentThreadInMonitor() const { mDecoder->GetReentrantMonitor().AssertCurrentThreadIn(); }
void SetState(State aState);
// Inserts MediaData* samples into their respective MediaQueues.
// aSample must not be null.
void Push(AudioData* aSample);
void Push(VideoData* aSample);
class WakeDecoderRunnable : public nsRunnable {
public:
explicit WakeDecoderRunnable(MediaDecoderStateMachine* aSM)
: mMutex("WakeDecoderRunnable"), mStateMachine(aSM) {}
NS_IMETHOD Run() override
{
nsRefPtr<MediaDecoderStateMachine> stateMachine;
{
// Don't let Run() (called by media stream graph thread) race with
// Revoke() (called by decoder state machine thread)
MutexAutoLock lock(mMutex);
if (!mStateMachine)
return NS_OK;
stateMachine = mStateMachine;
}
stateMachine->ScheduleStateMachineWithLockAndWakeDecoder();
return NS_OK;
}
void Revoke()
{
MutexAutoLock lock(mMutex);
mStateMachine = nullptr;
}
Mutex mMutex;
// Protected by mMutex.
// We don't use an owning pointer here, because keeping mStateMachine alive
// would mean in some cases we'd have to destroy mStateMachine from this
// object, which would be problematic since MediaDecoderStateMachine can
// only be destroyed on the main thread whereas this object can be destroyed
// on the media stream graph thread.
MediaDecoderStateMachine* mStateMachine;
};
WakeDecoderRunnable* GetWakeDecoderRunnable();
MediaQueue<AudioData>& AudioQueue() { return mAudioQueue; }
MediaQueue<VideoData>& VideoQueue() { return mVideoQueue; }
nsresult FinishDecodeFirstFrame();
nsAutoPtr<MetadataTags> mMetadataTags;
// True if our buffers of decoded audio are not full, and we should
// decode more.
bool NeedToDecodeAudio();
// True if our buffers of decoded video are not full, and we should
// decode more.
bool NeedToDecodeVideo();
// Returns true if we've got less than aAudioUsecs microseconds of decoded
// and playable data. The decoder monitor must be held.
//
// May not be invoked when mReader->UseBufferingHeuristics() is false.
bool HasLowDecodedData(int64_t aAudioUsecs);
bool OutOfDecodedAudio();
bool OutOfDecodedVideo()
{
// In buffering mode, we keep the last already-played frame in the queue.
int emptyVideoSize = mState == DECODER_STATE_BUFFERING ? 1 : 0;
return IsVideoDecoding() && !VideoQueue().IsFinished() && VideoQueue().GetSize() <= emptyVideoSize;
}
// Returns true if we're running low on data which is not yet decoded.
// The decoder monitor must be held.
bool HasLowUndecodedData();
// Returns true if we have less than aUsecs of undecoded data available.
bool HasLowUndecodedData(int64_t aUsecs);
// Returns the number of unplayed usecs of audio we've got decoded and/or
// pushed to the hardware waiting to play. This is how much audio we can
// play without having to run the audio decoder. The decoder monitor
// must be held.
int64_t AudioDecodedUsecs();
// Returns true when there's decoded audio waiting to play.
// The decoder monitor must be held.
bool HasFutureAudio();
// Returns true if we recently exited "quick buffering" mode.
bool JustExitedQuickBuffering();
// Dispatches an asynchronous event to update the media element's ready state.
void UpdateReadyState();
// Resets playback timing data. Called when we seek, on the decode thread.
void ResetPlayback();
// Orders the Reader to stop decoding, and blocks until the Reader
// has stopped decoding and finished delivering samples, then calls
// ResetPlayback() to discard all enqueued data.
void FlushDecoding();
// Called when AudioSink reaches the end. |mPlayStartTime| and
// |mPlayDuration| are updated to provide a good base for calculating video
// stream time.
void ResyncAudioClock();
// Returns the audio clock, if we have audio, or -1 if we don't.
// Called on the state machine thread.
int64_t GetAudioClock() const;
// Get the video stream position, taking the |playbackRate| change into
// account. This is a position in the media, not the duration of the playback
// so far.
int64_t GetVideoStreamPosition() const;
// Return the current time, either the audio clock if available (if the media
// has audio, and the playback is possible), or a clock for the video.
// Called on the state machine thread.
int64_t GetClock() const;
nsresult DropAudioUpToSeekTarget(AudioData* aSample);
nsresult DropVideoUpToSeekTarget(VideoData* aSample);
void SetStartTime(int64_t aStartTimeUsecs);
// Update only the state machine's current playback position (and duration,
// if unknown). Does not update the playback position on the decoder or
// media element -- use UpdatePlaybackPosition for that. Called on the state
// machine thread, caller must hold the decoder lock.
void UpdatePlaybackPositionInternal(int64_t aTime);
// Pushes the image down the rendering pipeline. Called on the shared state
// machine thread. The decoder monitor must *not* be held when calling this.
void RenderVideoFrame(VideoData* aData, TimeStamp aTarget);
// If we have video, display a video frame if it's time for display has
// arrived, otherwise sleep until it's time for the next frame. Update the
// current frame time as appropriate, and trigger ready state update. The
// decoder monitor must be held with exactly one lock count. Called on the
// state machine thread.
void AdvanceFrame();
// Stops the audio thread. The decoder monitor must be held with exactly
// one lock count. Called on the state machine thread.
void StopAudioThread();
// Starts the audio thread. The decoder monitor must be held with exactly
// one lock count. Called on the state machine thread.
nsresult StartAudioThread();
// Sets internal state which causes playback of media to pause.
// The decoder monitor must be held.
void StopPlayback();
// If the conditions are right, sets internal state which causes playback
// of media to begin or resume.
// Must be called with the decode monitor held.
void MaybeStartPlayback();
// Moves the decoder into decoding state. Called on the state machine
// thread. The decoder monitor must be held.
void StartDecoding();
// Moves the decoder into the shutdown state, and dispatches an error
// event to the media element. This begins shutting down the decoder.
// The decoder monitor must be held. This is only called on the
// decode thread.
void DecodeError();
void StartWaitForResources();
// Dispatches a task to the decode task queue to begin decoding metadata.
// This is threadsafe and can be called on any thread.
// The decoder monitor must be held.
nsresult EnqueueDecodeMetadataTask();
// Dispatches a LoadedMetadataEvent.
// This is threadsafe and can be called on any thread.
// The decoder monitor must be held.
void EnqueueLoadedMetadataEvent();
void EnqueueFirstFrameLoadedEvent();
// Dispatches a task to the decode task queue to begin decoding content.
// This is threadsafe and can be called on any thread.
// The decoder monitor must be held.
nsresult EnqueueDecodeFirstFrameTask();
// Dispatches a task to the decode task queue to seek the decoder.
// The decoder monitor must be held.
nsresult EnqueueDecodeSeekTask();
nsresult DispatchAudioDecodeTaskIfNeeded();
// Ensures a to decode audio has been dispatched to the decode task queue.
// If a task to decode has already been dispatched, this does nothing,
// otherwise this dispatches a task to do the decode.
// This is called on the state machine or decode threads.
// The decoder monitor must be held.
nsresult EnsureAudioDecodeTaskQueued();
nsresult DispatchVideoDecodeTaskIfNeeded();
// Ensures a to decode video has been dispatched to the decode task queue.
// If a task to decode has already been dispatched, this does nothing,
// otherwise this dispatches a task to do the decode.
// The decoder monitor must be held.
nsresult EnsureVideoDecodeTaskQueued();
// Calls the reader's SetIdle(). This is only called in a task dispatched to
// the decode task queue, don't call it directly.
void SetReaderIdle();
// Re-evaluates the state and determines whether we need to dispatch
// events to run the decode, or if not whether we should set the reader
// to idle mode. This is threadsafe, and can be called from any thread.
// The decoder monitor must be held.
void DispatchDecodeTasksIfNeeded();
// Returns the "media time". This is the absolute time which the media
// playback has reached. i.e. this returns values in the range
// [mStartTime, mEndTime], and mStartTime will not be 0 if the media does
// not start at 0. Note this is different to the value returned
// by GetCurrentTime(), which is in the range [0,duration].
int64_t GetMediaTime() const {
AssertCurrentThreadInMonitor();
return mStartTime + mCurrentFrameTime;
}
// Returns an upper bound on the number of microseconds of audio that is
// decoded and playable. This is the sum of the number of usecs of audio which
// is decoded and in the reader's audio queue, and the usecs of unplayed audio
// which has been pushed to the audio hardware for playback. Note that after
// calling this, the audio hardware may play some of the audio pushed to
// hardware, so this can only be used as a upper bound. The decoder monitor
// must be held when calling this. Called on the decode thread.
int64_t GetDecodedAudioDuration();
// Load metadata. Called on the decode thread. The decoder monitor
// must be held with exactly one lock count.
nsresult DecodeMetadata();
// Wraps the call to DecodeMetadata(), signals a DecodeError() on failure.
void CallDecodeMetadata();
// Initiate first content decoding. Called on the decode thread.
// The decoder monitor must be held with exactly one lock count.
nsresult DecodeFirstFrame();
// Wraps the call to DecodeFirstFrame(), signals a DecodeError() on failure.
void CallDecodeFirstFrame();
// Checks whether we're finished decoding first audio and/or video packets,
// and switches to DECODING state if so.
void MaybeFinishDecodeFirstFrame();
// Seeks to mSeekTarget. Called on the decode thread. The decoder monitor
// must be held with exactly one lock count.
void DecodeSeek();
void CheckIfSeekComplete();
bool IsAudioSeekComplete();
bool IsVideoSeekComplete();
// Completes the seek operation, moves onto the next appropriate state.
void SeekCompleted();
// Queries our state to see whether the decode has finished for all streams.
// If so, we move into DECODER_STATE_COMPLETED and schedule the state machine
// to run.
// The decoder monitor must be held.
void CheckIfDecodeComplete();
// Copy audio from an AudioData packet to aOutput. This may require
// inserting silence depending on the timing of the audio packet.
void SendStreamAudio(AudioData* aAudio, DecodedStreamData* aStream,
AudioSegment* aOutput);
// State machine thread run function. Defers to RunStateMachine().
nsresult CallRunStateMachine();
// Performs one "cycle" of the state machine. Polls the state, and may send
// a video frame to be displayed, and generally manages the decode. Called
// periodically via timer to ensure the video stays in sync.
nsresult RunStateMachine();
bool IsStateMachineScheduled() const;
// Returns true if we're not playing and the decode thread has filled its
// decode buffers and is waiting. We can shut the decode thread down in this
// case as it may not be needed again.
bool IsPausedAndDecoderWaiting();
// These return true if the respective stream's decode has not yet reached
// the end of stream.
bool IsAudioDecoding();
bool IsVideoDecoding();
// Set the time that playback started from the system clock.
// Can only be called on the state machine thread.
void SetPlayStartTime(const TimeStamp& aTimeStamp);
// Update mAudioEndTime.
void OnAudioEndTimeUpdate(int64_t aAudioEndTime);
// Update mDecoder's playback offset.
void OnPlaybackOffsetUpdate(int64_t aPlaybackOffset);
// Called by the AudioSink to signal that all outstanding work is complete
// and the sink is shutting down.
void OnAudioSinkComplete();
// Called by the AudioSink to signal errors.
void OnAudioSinkError();
// The state machine may move into DECODING_METADATA if we are in
// DECODER_STATE_WAIT_FOR_RESOURCES.
void DoNotifyWaitingForResourcesStatusChanged();
// Return true if the video decoder's decode speed can not catch up the
// play time.
bool NeedToSkipToNextKeyframe();
// The decoder object that created this state machine. The state machine
// holds a strong reference to the decoder to ensure that the decoder stays
// alive once media element has started the decoder shutdown process, and has
// dropped its reference to the decoder. This enables the state machine to
// keep using the decoder's monitor until the state machine has finished
// shutting down, without fear of the monitor being destroyed. After
// shutting down, the state machine will then release this reference,
// causing the decoder to be destroyed. This is accessed on the decode,
// state machine, audio and main threads.
nsRefPtr<MediaDecoder> mDecoder;
// Used to schedule state machine cycles. This should never outlive
// the life cycle of the state machine.
const nsAutoPtr<MediaDecoderStateMachineScheduler> mScheduler;
// Time at which the last video sample was requested. If it takes too long
// before the sample arrives, we will increase the amount of audio we buffer.
// This is necessary for legacy synchronous decoders to prevent underruns.
TimeStamp mVideoDecodeStartTime;
// Queue of audio frames. This queue is threadsafe, and is accessed from
// the audio, decoder, state machine, and main threads.
MediaQueue<AudioData> mAudioQueue;
// Queue of video frames. This queue is threadsafe, and is accessed from
// the decoder, state machine, and main threads.
MediaQueue<VideoData> mVideoQueue;
// The decoder monitor must be obtained before modifying this state.
// NotifyAll on the monitor must be called when the state is changed so
// that interested threads can wake up and alter behaviour if appropriate
// Accessed on state machine, audio, main, and AV thread.
State mState;
// The task queue in which we run decode tasks. This is referred to as
// the "decode thread", though in practise tasks can run on a different
// thread every time they're called.
MediaTaskQueue* DecodeTaskQueue() const { return mReader->GetTaskQueue(); }
// The time that playback started from the system clock. This is used for
// timing the presentation of video frames when there's no audio.
// Accessed only via the state machine thread. Must be set via SetPlayStartTime.
TimeStamp mPlayStartTime;
// When we start writing decoded data to a new DecodedDataStream, or we
// restart writing due to PlaybackStarted(), we record where we are in the
// MediaStream and what that corresponds to in the media.
int64_t mSyncPointInMediaStream; // microseconds
int64_t mSyncPointInDecodedStream; // microseconds
// The amount of time we've spent playing already the media. The current
// playback position is therefore |Now() - mPlayStartTime +
// mPlayDuration|, which must be adjusted by mStartTime if used with media
// timestamps. Accessed on state machine and main threads. Access controlled
// by decoder monitor.
int64_t mPlayDuration;
// Time that buffering started. Used for buffering timeout and only
// accessed on the state machine thread. This is null while we're not
// buffering.
TimeStamp mBufferingStart;
// Start time of the media, in microseconds. This is the presentation
// time of the first frame decoded from the media, and is used to calculate
// duration and as a bounds for seeking. Accessed on state machine, decode,
// and main threads. Access controlled by decoder monitor.
int64_t mStartTime;
// Time of the last frame in the media, in microseconds. This is the
// end time of the last frame in the media. Accessed on state
// machine, decode, and main threads. Access controlled by decoder monitor.
// It will be set to -1 if the duration is infinite
int64_t mEndTime;
// Will be set when SetDuration has been called with a value != -1
// mDurationSet false doesn't indicate that we do not have a valid duration
// as mStartTime and mEndTime could have been set separately.
bool mDurationSet;
// Position to seek to in microseconds when the seek state transition occurs.
// The decoder monitor lock must be obtained before reading or writing
// this value. Accessed on main and decode thread.
SeekTarget mSeekTarget;
// Position to seek to in microseconds when DecodeFirstFrame completes.
// The decoder monitor lock must be obtained before reading or writing
// this value. Accessed on main and decode thread.
SeekTarget mQueuedSeekTarget;
// The position that we're currently seeking to. This differs from
// mSeekTarget, as mSeekTarget is the target we'll seek to next, whereas
// mCurrentSeekTarget is the position that the decode is in the process
// of seeking to.
// The decoder monitor lock must be obtained before reading or writing
// this value.
SeekTarget mCurrentSeekTarget;
// Media Fragment end time in microseconds. Access controlled by decoder monitor.
int64_t mFragmentEndTime;
// The audio sink resource. Used on state machine and audio threads.
RefPtr<AudioSink> mAudioSink;
// The reader, don't call its methods with the decoder monitor held.
// This is created in the state machine's constructor.
nsRefPtr<MediaDecoderReader> mReader;
// Accessed only on the state machine thread.
// Not an nsRevocableEventPtr since we must Revoke() it well before
// this object is destroyed, anyway.
// Protected by decoder monitor except during the SHUTDOWN state after the
// decoder thread has been stopped.
nsRevocableEventPtr<WakeDecoderRunnable> mPendingWakeDecoder;
// The time of the current frame in microseconds. This is referenced from
// 0 which is the initial playback position. Set by the state machine
// thread, and read-only from the main thread to get the current
// time value. Synchronised via decoder monitor.
int64_t mCurrentFrameTime;
// The presentation time of the first audio frame that was played in
// microseconds. We can add this to the audio stream position to determine
// the current audio time. Accessed on audio and state machine thread.
// Synchronized by decoder monitor.
int64_t mAudioStartTime;
// The end time of the last audio frame that's been pushed onto the audio
// hardware in microseconds. This will approximately be the end time of the
// audio stream, unless another frame is pushed to the hardware.
int64_t mAudioEndTime;
// The end time of the last decoded audio frame. This signifies the end of
// decoded audio data. Used to check if we are low in decoded data.
int64_t mDecodedAudioEndTime;
// The presentation end time of the last video frame which has been displayed
// in microseconds. Accessed from the state machine thread.
int64_t mVideoFrameEndTime;
// The end time of the last decoded video frame. Used to check if we are low
// on decoded video data.
int64_t mDecodedVideoEndTime;
// Volume of playback. 0.0 = muted. 1.0 = full volume. Read/Written
// from the state machine and main threads. Synchronised via decoder
// monitor.
double mVolume;
// Playback rate. 1.0 : normal speed, 0.5 : two times slower. Synchronized via
// decoder monitor.
double mPlaybackRate;
// Pitch preservation for the playback rate. Synchronized via decoder monitor.
bool mPreservesPitch;
// Time at which we started decoding. Synchronised via decoder monitor.
TimeStamp mDecodeStartTime;
// The maximum number of second we spend buffering when we are short on
// unbuffered data.
uint32_t mBufferingWait;
int64_t mLowDataThresholdUsecs;
// If we've got more than mAmpleVideoFrames decoded video frames waiting in
// the video queue, we will not decode any more video frames until some have
// been consumed by the play state machine thread.
uint32_t mAmpleVideoFrames;
// Low audio threshold. If we've decoded less than this much audio we
// consider our audio decode "behind", and we may skip video decoding
// in order to allow our audio decoding to catch up. We favour audio
// decoding over video. We increase this threshold if we're slow to
// decode video frames, in order to reduce the chance of audio underruns.
// Note that we don't ever reset this threshold, it only ever grows as
// we detect that the decode can't keep up with rendering.
int64_t mLowAudioThresholdUsecs;
// Our "ample" audio threshold. Once we've this much audio decoded, we
// pause decoding. If we increase mLowAudioThresholdUsecs, we'll also
// increase this too appropriately (we don't want mLowAudioThresholdUsecs
// to be greater than ampleAudioThreshold, else we'd stop decoding!).
// Note that we don't ever reset this threshold, it only ever grows as
// we detect that the decode can't keep up with rendering.
int64_t mAmpleAudioThresholdUsecs;
// If we're quick buffering, we'll remain in buffering mode while we have less than
// QUICK_BUFFERING_LOW_DATA_USECS of decoded data available.
int64_t mQuickBufferingLowDataThresholdUsecs;
// At the start of decoding we want to "preroll" the decode until we've
// got a few frames decoded before we consider whether decode is falling
// behind. Otherwise our "we're falling behind" logic will trigger
// unneccessarily if we start playing as soon as the first sample is
// decoded. These two fields store how many video frames and audio
// samples we must consume before are considered to be finished prerolling.
uint32_t AudioPrerollUsecs() const
{
if (mScheduler->IsRealTime()) {
return 0;
}
uint32_t result = mLowAudioThresholdUsecs * 2;
MOZ_ASSERT(result <= mAmpleAudioThresholdUsecs, "Prerolling will never finish");
return result;
}
uint32_t VideoPrerollFrames() const { return mScheduler->IsRealTime() ? 0 : mAmpleVideoFrames / 2; }
bool DonePrerollingAudio()
{
AssertCurrentThreadInMonitor();
return !IsAudioDecoding() || GetDecodedAudioDuration() >= AudioPrerollUsecs() * mPlaybackRate;
}
bool DonePrerollingVideo()
{
AssertCurrentThreadInMonitor();
return !IsVideoDecoding() ||
static_cast<uint32_t>(VideoQueue().GetSize()) >= VideoPrerollFrames() * mPlaybackRate;
}
void StopPrerollingAudio()
{
AssertCurrentThreadInMonitor();
if (mIsAudioPrerolling) {
mIsAudioPrerolling = false;
ScheduleStateMachine();
}
}
void StopPrerollingVideo()
{
AssertCurrentThreadInMonitor();
if (mIsVideoPrerolling) {
mIsVideoPrerolling = false;
ScheduleStateMachine();
}
}
// This temporarily stores the first frame we decode after we seek.
// This is so that if we hit end of stream while we're decoding to reach
// the seek target, we will still have a frame that we can display as the
// last frame in the media.
nsRefPtr<VideoData> mFirstVideoFrameAfterSeek;
// When we start decoding (either for the first time, or after a pause)
// we may be low on decoded data. We don't want our "low data" logic to
// kick in and decide that we're low on decoded data because the download
// can't keep up with the decode, and cause us to pause playback. So we
// have a "preroll" stage, where we ignore the results of our "low data"
// logic during the first few frames of our decode. This occurs during
// playback. The flags below are true when the corresponding stream is
// being "prerolled".
bool mIsAudioPrerolling;
bool mIsVideoPrerolling;
// Only one of a given pair of ({Audio,Video}DataPromise, WaitForDataPromise)
// should exist at any given moment.
MediaPromiseConsumerHolder<MediaDecoderReader::AudioDataPromise> mAudioDataRequest;
MediaPromiseConsumerHolder<MediaDecoderReader::WaitForDataPromise> mAudioWaitRequest;
const char* AudioRequestStatus()
{
if (mAudioDataRequest.Exists()) {
MOZ_DIAGNOSTIC_ASSERT(!mAudioWaitRequest.Exists());
return "pending";
} else if (mAudioWaitRequest.Exists()) {
return "waiting";
}
return "idle";
}
MediaPromiseConsumerHolder<MediaDecoderReader::WaitForDataPromise> mVideoWaitRequest;
MediaPromiseConsumerHolder<MediaDecoderReader::VideoDataPromise> mVideoDataRequest;
const char* VideoRequestStatus()
{
if (mVideoDataRequest.Exists()) {
MOZ_DIAGNOSTIC_ASSERT(!mVideoWaitRequest.Exists());
return "pending";
} else if (mVideoWaitRequest.Exists()) {
return "waiting";
}
return "idle";
}
MediaPromiseConsumerHolder<MediaDecoderReader::WaitForDataPromise>& WaitRequestRef(MediaData::Type aType)
{
return aType == MediaData::AUDIO_DATA ? mAudioWaitRequest : mVideoWaitRequest;
}
// True if we shouldn't play our audio (but still write it to any capturing
// streams). When this is true, mStopAudioThread is always true and
// the audio thread will never start again after it has stopped.
bool mAudioCaptured;
// True if an event to notify about a change in the playback
// position has been queued, but not yet run. It is set to false when
// the event is run. This allows coalescing of these events as they can be
// produced many times per second. Synchronised via decoder monitor.
// Accessed on main and state machine threads.
bool mPositionChangeQueued;
// True if the audio playback thread has finished. It is finished
// when either all the audio frames have completed playing, or we've moved
// into shutdown state, and the threads are to be
// destroyed. Written by the audio playback thread and read and written by
// the state machine thread. Synchronised via decoder monitor.
// When data is being sent to a MediaStream, this is true when all data has
// been written to the MediaStream.
bool mAudioCompleted;
// True if mDuration has a value obtained from an HTTP header, or from
// the media index/metadata. Accessed on the state machine thread.
bool mGotDurationFromMetaData;
// True if we've dispatched an event to the decode task queue to call
// DecodeThreadRun(). We use this flag to prevent us from dispatching
// unneccessary runnables, since the decode thread runs in a loop.
bool mDispatchedEventToDecode;
// False while audio thread should be running. Accessed state machine
// and audio threads. Syncrhonised by decoder monitor.
bool mStopAudioThread;
// If this is true while we're in buffering mode, we can exit early,
// as it's likely we may be able to playback. This happens when we enter
// buffering mode soon after the decode starts, because the decode-ahead
// ran fast enough to exhaust all data while the download is starting up.
// Synchronised via decoder monitor.
bool mQuickBuffering;
// True if we should not decode/preroll unnecessary samples, unless we're
// played. "Prerolling" in this context refers to when we decode and
// buffer decoded samples in advance of when they're needed for playback.
// This flag is set for preload=metadata media, and means we won't
// decode more than the first video frame and first block of audio samples
// for that media when we startup, or after a seek. When Play() is called,
// we reset this flag, as we assume the user is playing the media, so
// prerolling is appropriate then. This flag is used to reduce the overhead
// of prerolling samples for media elements that may not play, both
// memory and CPU overhead.
bool mMinimizePreroll;
// True if the decode thread has gone filled its buffers and is now
// waiting to be awakened before it continues decoding. Synchronized
// by the decoder monitor.
bool mDecodeThreadWaiting;
// These two flags are true when we need to drop decoded samples that
// we receive up to the next discontinuity. We do this when we seek;
// the first sample in each stream after the seek is marked as being
// a "discontinuity".
bool mDropAudioUntilNextDiscontinuity;
bool mDropVideoUntilNextDiscontinuity;
// True if we need to decode forwards to the seek target inside
// mCurrentSeekTarget.
bool mDecodeToSeekTarget;
// True if we've issued Seek() to the reader, but haven't yet received
// OnSeekCompleted. We should avoid trying to decode more audio/video
// until this completes.
bool mWaitingForDecoderSeek;
// True if we're in the process of canceling a seek. This allows us to avoid
// invoking CancelSeek() multiple times.
bool mCancelingSeek;
// We record the playback position before we seek in order to
// determine where the seek terminated relative to the playback position
// we were at before the seek.
int64_t mCurrentTimeBeforeSeek;
// Stores presentation info required for playback. The decoder monitor
// must be held when accessing this.
MediaInfo mInfo;
mozilla::MediaMetadataManager mMetadataManager;
MediaDecoderOwner::NextFrameStatus mLastFrameStatus;
mozilla::RollingMean<uint32_t, uint32_t> mCorruptFrames;
bool mDisabledHardwareAcceleration;
// mDecodingFrozenAtStateDecoding: turn on/off at
// SetDormant/Seek,Play.
bool mDecodingFrozenAtStateDecoding;
// True if we are back from DECODER_STATE_DORMANT state and
// LoadedMetadataEvent was already sent.
bool mSentLoadedMetadataEvent;
// True if we are back from DECODER_STATE_DORMANT state and
// FirstFrameLoadedEvent was already sent, then we can skip
// SetStartTime because the mStartTime already set before. Also we don't need
// to decode any audio/video since the MediaDecoder will trigger a seek
// operation soon.
bool mSentFirstFrameLoadedEvent;
};
} // namespace mozilla;
#endif
|