/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
bool CompareTextTracks::Equals(TextTrack* aOne, TextTrack* aTwo) const { // Two tracks can never be equal. If they have corresponding TrackElements // they would need to occupy the same tree position (impossible) and in the // case of tracks coming from AddTextTrack source we put the newest at the // last position, so they won't be equal as well. returnfalse;
}
bool CompareTextTracks::LessThan(TextTrack* aOne, TextTrack* aTwo) const { // Protect against nullptr TextTrack objects; treat them as // sorting toward the end. if (!aOne) { returnfalse;
} if (!aTwo) { returntrue;
}
TextTrackSource sourceOne = aOne->GetTextTrackSource();
TextTrackSource sourceTwo = aTwo->GetTextTrackSource(); if (sourceOne != sourceTwo) { return sourceOne == TextTrackSource::Track ||
(sourceOne == TextTrackSource::AddTextTrack &&
sourceTwo == TextTrackSource::MediaResourceSpecific);
} switch (sourceOne) { case TextTrackSource::Track: {
Maybe<uint32_t> positionOne = TrackChildPosition(aOne);
Maybe<uint32_t> positionTwo = TrackChildPosition(aTwo); // If either position one or positiontwo are Nothing then something has // gone wrong. In this case we should just put them at the back of the // list. return positionOne.isSome() && positionTwo.isSome() &&
*positionOne < *positionTwo;
} case TextTrackSource::AddTextTrack: // For AddTextTrack sources the tracks will already be in the correct // relative order in the source array. Assume we're called in iteration // order and can therefore always report aOne < aTwo to maintain the // original temporal ordering. returntrue; case TextTrackSource::MediaResourceSpecific: // No rules for Media Resource Specific tracks yet. break;
} returntrue;
}
WEBVTT_LOG("RemoveTextTrack TextTrack %p", aTextTrack);
mPendingTextTracks->RemoveTextTrack(aTextTrack); if (aPendingListOnly) { return;
}
mTextTracks->RemoveTextTrack(aTextTrack); // Remove the cues in mNewCues belong to aTextTrack.
TextTrackCueList* removeCueList = aTextTrack->GetCues(); if (removeCueList) {
WEBVTT_LOGV("RemoveTextTrack removeCuesNum=%d", removeCueList->Length()); for (uint32_t i = 0; i < removeCueList->Length(); ++i) {
mNewCues->RemoveCue(*((*removeCueList)[i]));
}
MaybeRunTimeMarchesOn();
}
}
nsIFrame* frame = mMediaElement->GetPrimaryFrame();
nsVideoFrame* videoFrame = do_QueryFrame(frame); if (!videoFrame) {
WEBVTT_LOG("Abort UpdateCueDisplay, because of no video frame."); return;
}
nsCOMPtr<nsIContent> overlay = videoFrame->GetCaptionOverlay(); if (!overlay) {
WEBVTT_LOG("Abort UpdateCueDisplay, because of no overlay."); return;
}
RefPtr<nsPIDOMWindowInner> window =
mMediaElement->OwnerDoc()->GetInnerWindow(); if (!window) {
WEBVTT_LOG("Abort UpdateCueDisplay, because of no window.");
}
// Steps 1 - 3: Perform automatic track selection for different TextTrack // Kinds.
PerformTrackSelection(ttKinds, std::size(ttKinds));
PerformTrackSelection(TextTrackKind::Descriptions);
PerformTrackSelection(TextTrackKind::Chapters);
// Step 4: Set all TextTracks with a kind of metadata that are disabled // to hidden. for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
TextTrack* track = (*mTextTracks)[i]; if (track->Kind() == TextTrackKind::Metadata && TrackIsDefault(track) &&
track->Mode() == TextTrackMode::Disabled) {
track->SetMode(TextTrackMode::Hidden);
}
}
// Step 3: If any TextTracks in candidates are showing then abort these steps. for (uint32_t i = 0; i < candidates.Length(); i++) { if (candidates[i]->Mode() == TextTrackMode::Showing) {
WEBVTT_LOGV("PerformTrackSelection Showing return kind %d", static_cast<int>(candidates[i]->Kind())); return;
}
}
// Step 4: Honor user preferences for track selection, otherwise, set the // first TextTrack in candidates with a default attribute to showing. // TODO: Bug 981691 - Honor user preferences for text track selection. for (uint32_t i = 0; i < candidates.Length(); i++) { if (TrackIsDefault(candidates[i]) &&
candidates[i]->Mode() == TextTrackMode::Disabled) {
candidates[i]->SetMode(TextTrackMode::Showing);
WEBVTT_LOGV("PerformTrackSelection set Showing kind %d", static_cast<int>(candidates[i]->Kind())); return;
}
}
}
void TextTrackManager::GetTextTracksOfKinds(TextTrackKind aTextTrackKinds[],
uint32_t size,
nsTArray<TextTrack*>& aTextTracks) { for (uint32_t i = 0; i < size; i++) {
GetTextTracksOfKind(aTextTrackKinds[i], aTextTracks);
}
}
void TextTrackManager::GetTextTracksOfKind(TextTrackKind aTextTrackKind,
nsTArray<TextTrack*>& aTextTracks) { if (!mTextTracks) { return;
} for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
TextTrack* textTrack = (*mTextTracks)[i]; if (textTrack->Kind() == aTextTrackKind) {
aTextTracks.AppendElement(textTrack);
}
}
}
NS_IMETHODIMP
TextTrackManager::HandleEvent(Event* aEvent) { if (!mTextTracks) { return NS_OK;
}
if (setDirty) { for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
((*mTextTracks)[i])->SetCuesDirty();
}
} if (updateDisplay) {
UpdateCueDisplay();
}
void TextTrackManager::DispatchTimeMarchesOn() { // Run the algorithm if no previous instance is still running, otherwise // enqueue the current playback position and whether only that changed // through its usual monotonic increase during normal playback; current // executing call upon completion will check queue for further 'work'. if (!mTimeMarchesOnDispatched && !IsShutdown()) {
WEBVTT_LOG("DispatchTimeMarchesOn"); if (nsPIDOMWindowInner* win = mMediaElement->OwnerDoc()->GetInnerWindow()) {
nsGlobalWindowInner::Cast(win)->Dispatch(
NewRunnableMethod("dom::TextTrackManager::TimeMarchesOn", this,
&TextTrackManager::TimeMarchesOn));
mTimeMarchesOnDispatched = true;
}
}
}
CycleCollectedJSContext* context = CycleCollectedJSContext::Get(); if (context && context->IsInStableOrMetaStableState()) { // FireTimeUpdate can be called while at stable state following a // current position change which triggered a state watcher in MediaDecoder // (see bug 1443429). // TimeMarchesOn() will modify JS attributes which is forbidden while in // stable state. So we dispatch a task to perform such operation later // instead.
DispatchTimeMarchesOn(); return;
}
WEBVTT_LOG("TimeMarchesOn");
// Early return if we don't have any TextTracks or shutting down. if (!mTextTracks || mTextTracks->Length() == 0 || IsShutdown() ||
!mMediaElement) { return;
}
if (mMediaElement->ReadyState() == HTMLMediaElement_Binding::HAVE_NOTHING) {
WEBVTT_LOG( "TimeMarchesOn return because media doesn't contain any data yet"); return;
}
if (mMediaElement->Seeking()) {
WEBVTT_LOG("TimeMarchesOn return during seeking"); return;
}
// Step 1, 2.
nsISupports* parentObject = mMediaElement->OwnerDoc()->GetParentObject(); if (NS_WARN_IF(!parentObject)) { return;
}
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentObject);
RefPtr<TextTrackCueList> currentCues = new TextTrackCueList(window);
RefPtr<TextTrackCueList> otherCues = new TextTrackCueList(window);
// The reason we collect other cues is (1) to change active cues to inactive, // (2) find missing cues, so we actually no need to process all cues. We just // need to handle cues which are in the time interval [lastTime:currentTime] // or [currentTime:lastTime] (seeking forward). That can help us to reduce the // size of other cues, which can improve execution time. auto start = std::min(mLastTimeMarchesOnCalled, currentPlaybackTime); auto end = std::max(mLastTimeMarchesOnCalled, currentPlaybackTime);
media::TimeInterval interval(start, end);
WEBVTT_LOGV("TimeMarchesOn Time interval [%f:%f]", start.ToSeconds(),
end.ToSeconds()); for (uint32_t idx = 0; idx < mTextTracks->Length(); ++idx) {
TextTrack* track = (*mTextTracks)[idx]; if (track) {
track->GetCurrentCuesAndOtherCues(currentCues, otherCues, interval);
}
}
// Step 4.
RefPtr<TextTrackCueList> missedCues = new TextTrackCueList(window); if (hasNormalPlayback) { for (uint32_t i = 0; i < otherCues->Length(); ++i) {
TextTrackCue* cue = (*otherCues)[i]; if (cue->StartTime() >= mLastTimeMarchesOnCalled.ToSeconds() &&
cue->EndTime() <= currentPlaybackTime.ToSeconds()) {
missedCues->AddCue(*cue);
}
}
}
// Step 7. Abort steps if condition 1, 2, 3 are satisfied. // 1. All of the cues in current cues have their active flag set. // 2. None of the cues in other cues have their active flag set. // 3. Missed cues is empty. bool c1 = true; for (uint32_t i = 0; i < currentCues->Length(); ++i) { if (!(*currentCues)[i]->GetActive()) {
c1 = false; break;
}
} bool c2 = true; for (uint32_t i = 0; i < otherCues->Length(); ++i) { if ((*otherCues)[i]->GetActive()) {
c2 = false; break;
}
} bool c3 = (missedCues->Length() == 0); if (c1 && c2 && c3) {
mLastTimeMarchesOnCalled = currentPlaybackTime;
WEBVTT_LOG("TimeMarchesOn step 7 return, mLastTimeMarchesOnCalled %lf",
mLastTimeMarchesOnCalled.ToSeconds()); return;
}
// Step 8. Respect PauseOnExit flag if not seek. if (hasNormalPlayback) { for (uint32_t i = 0; i < otherCues->Length(); ++i) {
TextTrackCue* cue = (*otherCues)[i]; if (cue && cue->PauseOnExit() && cue->GetActive()) {
WEBVTT_LOG("TimeMarchesOn pause the MediaElement");
mMediaElement->Pause(); break;
}
} for (uint32_t i = 0; i < missedCues->Length(); ++i) {
TextTrackCue* cue = (*missedCues)[i]; if (cue && cue->PauseOnExit()) {
WEBVTT_LOG("TimeMarchesOn pause the MediaElement");
mMediaElement->Pause(); break;
}
}
}
// Step 15. // Sort text tracks in the same order as the text tracks appear // in the media element's list of text tracks, and remove // duplicates.
TextTrackListInternal affectedTracks; // Step 13, 14.
nsTArray<RefPtr<SimpleTextTrackEvent>> eventList; // Step 9, 10. // For each text track cue in missed cues, prepare an event named // enter for the TextTrackCue object with the cue start time. for (uint32_t i = 0; i < missedCues->Length(); ++i) {
TextTrackCue* cue = (*missedCues)[i]; if (cue) {
WEBVTT_LOG("Prepare 'enter' event for cue %p [%f, %f] in missing cues",
cue, cue->StartTime(), cue->EndTime());
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(
u"enter"_ns, cue->StartTime(), cue->GetTrack(), cue);
eventList.InsertElementSorted(
event, CompareSimpleTextTrackEvents(mMediaElement));
affectedTracks.AddTextTrack(cue->GetTrack(),
CompareTextTracks(mMediaElement));
}
}
// Step 11, 17. for (uint32_t i = 0; i < otherCues->Length(); ++i) {
TextTrackCue* cue = (*otherCues)[i]; if (cue->GetActive() || missedCues->IsCueExist(cue)) { double time =
cue->StartTime() > cue->EndTime() ? cue->StartTime() : cue->EndTime();
WEBVTT_LOG("Prepare 'exit' event for cue %p [%f, %f] in other cues", cue,
cue->StartTime(), cue->EndTime());
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(u"exit"_ns, time, cue->GetTrack(), cue);
eventList.InsertElementSorted(
event, CompareSimpleTextTrackEvents(mMediaElement));
affectedTracks.AddTextTrack(cue->GetTrack(),
CompareTextTracks(mMediaElement));
}
cue->SetActive(false);
}
// Step 12, 17. for (uint32_t i = 0; i < currentCues->Length(); ++i) {
TextTrackCue* cue = (*currentCues)[i]; if (!cue->GetActive()) {
WEBVTT_LOG("Prepare 'enter' event for cue %p [%f, %f] in current cues",
cue, cue->StartTime(), cue->EndTime());
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(
u"enter"_ns, cue->StartTime(), cue->GetTrack(), cue);
eventList.InsertElementSorted(
event, CompareSimpleTextTrackEvents(mMediaElement));
affectedTracks.AddTextTrack(cue->GetTrack(),
CompareTextTracks(mMediaElement));
}
cue->SetActive(true);
}
// Fire the eventList for (uint32_t i = 0; i < eventList.Length(); ++i) {
eventList[i]->Dispatch();
}
// Step 16. for (uint32_t i = 0; i < affectedTracks.Length(); ++i) {
TextTrack* ttrack = affectedTracks[i]; if (ttrack) {
ttrack->DispatchAsyncTrustedEvent(u"cuechange"_ns);
HTMLTrackElement* trackElement = ttrack->GetTrackElement(); if (trackElement) {
trackElement->DispatchTrackRunnable(u"cuechange"_ns);
}
}
}
mLastTimeMarchesOnCalled = currentPlaybackTime;
// Step 18.
UpdateCueDisplay();
}
void TextTrackManager::NotifyCueUpdated(TextTrackCue* aCue) { // TODO: Add/Reorder the cue to mNewCues if we have some optimization?
WEBVTT_LOG("NotifyCueUpdated, cue=%p", aCue);
MaybeRunTimeMarchesOn(); // For the case "Texttrack.mode = hidden/showing", if the mode // changing between showing and hidden, TimeMarchesOn // doesn't render the cue. Call DispatchUpdateCueDisplay() explicitly.
DispatchUpdateCueDisplay();
}
void TextTrackManager::NotifyReset() { // https://html.spec.whatwg.org/multipage/media.html#text-track-cue-active-flag // This will unset all cues' active flag and update the cue display.
WEBVTT_LOG("NotifyReset");
mLastTimeMarchesOnCalled = media::TimeUnit::Zero(); for (uint32_t idx = 0; idx < mTextTracks->Length(); ++idx) {
(*mTextTracks)[idx]->SetCuesInactive();
}
UpdateCueDisplay();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.