Files
palemoon27/dom/html/TextTrackManager.cpp
T
roytam1 8cdf8ee29c import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1170958 - Feed a SourceMediaStream-backed dom stream instead of a raw SourceMediaStream in MediaManager. r=jesup (8670ff2711)
- Bug 1103188 - Remove identical override nsDOMUserMediaStream::Stop(). r=jib (54831f9b18)
- Bug 1103188 - Deprecate DOMMediaStream::Stop(). r=jib (36112afe82)
- Bug 1186813 - Replace nsBaseHashtable::EnumerateRead() calls in dom/media/ with iterators r=cpearce (cd0c4a34e8)
- Bug 1190337 - Log GPS status and SVs status if the 'gDebug_isLoggingEnabled' is true. r=garvank (c269f6f31d)
- Bug 1154435 - [Stumbler] FxOS Geo Stumbling for Mozilla Location Service. r=jdm (1a86f4dda5)
- Bug 1199395 - FxOS Stumbling gzip the stumbles to store more data. r=jdm (4d108665d9)
- Bug 1175860 - Add some documentation to UploadLastDir to make its workings clearer. r=baku (cdac9a7849)
- Bug 1210517 - Create nsVariant directly rather than via do_CreateInstance(). r=froydnj (df420cba8e)
- Bug 953265: make getUserMedia fake audio tones configurable in frequency via pref r=jib (67793ee005)
- Bug 1166293 - Use AsyncShutdown API to shut down media thread in non-e10s. r= jesup (1245d20b7e)
- Bug 1103188 - MediaStream WebIDL update with addTrack/removeTrack. r=smaug,jib (697791fd6f)
- Bug 1103188 - MediaStream::AddTrack/RemoveTrack implementation. r=roc (c8b02beb45)
- Bug 1170958 - Improve logging of MediaStreams and playback. r=roc (5fcb40437e)
- Bug 1170958 - Add DOMMediaStream::OwnedStreamListener. r=roc (afff077f93)
- Bug 1103188 - Break out MediaTrackListListener to an interface. r=roc (298b665f27)
- Bug 1198435 - Call RemoveMediaElementFromURITable before modifying mLoadingSrc, so that a future LookupMediaElementURITable won't access this element anymore. r=rillian (f2805c8dba)
- Bug 1141875 - Add flag to init gl_Position. - r=kamidphish (eeb333c02b)
- Bug 1128044 - Enforce packing restrictions for varyings. - r=kamidphish (17b9596a3d)
- Bug 1128044 - Only pack varyings that have static use in both shaders. - r=warnings-as-errors (f41708642a)
- Bug 1128044 - Use nsTArray since android doesn't support std::vector::data(). - r=bustage (be88a80844)
- Bug 1128044 - nsTArray::AppendElement doesn't accept init lists. - r=bustage (cdeafa867b)
- bit of Bug 1019209 - Allow GL initialization without Android bridge (3dba5dffa2)
- some reporter (3049ad6f6d)
- Bug 1206030 - Remove nsIDOMHTMLCanvasElement::MozFetchAsStream() f=Ms2ger r=jst (95e773b79f)
- Bug 1187174 - Use 'webgl2' not 'experimental-webgl2'. - r=kamidphish (a6c21752fc)
- Bug 1190777 - Add null checks to prevent bad dereferences. r=kamidphish (f67f0125ce)
- Bug 709490 - Part 1: Let ImageBridge transfer CanvasClient async. r=nical (a46ac7e71c)
- Bug 1150762 - Add pref for activating all ANGLE options. - r=kamidphish (6ab4d39827)
- Bug 1195401 - Use gfxPrefs (threadsafe) rather than crashing on debug builds for off-main-thread pref access. r=snorp (0d29cea59c)
- Bug 709490 - Part 2: Introduce OffscreenCanvas and let WebGL context work on workers. r=nical, r=jgilbert, r=jrmuizel, sr=ehsan (842aaa8328)
- Bug 709490 - Part 3: Transfer OffscreenCanvas from mainthread to workers. r=baku, r=sfink (91c24b0e08)
- Bug 709490 - Part 4: Mochitests for offscreencanvas. r=baku, r=jgilbert (4c439fd376)
- Bug 1173544 - Add tests for Canvas CSS/SVG Filters. r=mstange (04c01f1c11)
- fix (9c7ab9d870)
- Bug 709490 - Part 5: Add interfaces test. r=ehsan (2993581c89)
- Bug 709490 - Part 6: Add frame ID to CanvasClient so compositor could update frame correctly. r=roc (3e6554af1e)
- Bug 709490 - Part 7: If layer is not available, fallback to BasicCanvasLayer. r=roc (c0c0d04468)
- Bug 709490 - Part 8: Copy to a temp texture when readback from IOSurface. r=jgilbert (d1a4879a39)
- Bug 709490 - Part 9: Readback without blocking main thread. r=jgilbert (2430c6e2a5)
- Bug 709490 - Part 10: Using mechanism in RuntimeService to get pref in worker thread instead of gfxPref. r=baku (85d6dc2744)
- Bug 709490 - Part 11: Diabled test_offscreencanvas_many.html on gonk, android, windows and linux. r=jgilbert (5cd8f28063)
- Bug 1212663 - Use doxygen style comments in jsapi, r=Waldo (0e67283edf)
- Bug 1000922 - Use nsMainThreadPtrHandle instead of already_AddRefed and forget for callbacks in NativeOSFileInternals.cpp r=jdm (4a128db7a6)
- Bug 1169740 - Implement a TDZ-like behavior for |this| in derived class constructors. (r=jandem, r=jorendorff, inputs on nit resoulution from Waldo) (6d7df317e3)
- Bug 1211949 - check for allocation failure. r=nbp (94b8aac5e3)
- Bug 1209497 - OOM-crash if a consistent object table is impossible. r=jandem (e8ded0c3cb)
- Bug 1141863 - Part 1: Make |this| object creation account for new.target. (r=jandem, r=jorendorff) (9b4ec25d47)
- Bug 1141863 - Part 2: Implement ES6 SuperCall. (r=jandem, r=jorendorff) (1bbd2ba712)
- Bug 1141863 - Followup: Clean up proxy get traps to handle new |this| creation semantics. (rs=Waldo) CLOSED TREE (e7cd48b43c)
- Bug 1141863 - Last followup fix for a couple jstest failures. r=orange in a CLOSED TREE (8a9cff881a)
- Bug 1141863 - Followfollowfollowup: Remove redundant assert causing rooting hazards. (r=Waldo over IRC) CLOSED TREE (338b64ca87)
- Bug 1141863 - Tests. (r=jorendorff) (3957511169)
- Bug 1105463 - Implement default constructors for ES6 class definitions. (r=jorendorff) (8ead7f33a5)
- Bug 1105463 - Follow up: Fix erroneous syntax test. (r=theSheriffMadeMeDoIt) (425e678cf2)
- Bug 1212794 - Remove decompile-body functionality. r=till (9b87e5c0e4)
- Bug 1214970 - Don't emit nullptr atoms for class expressions with default constructors. (r=Waldo) (80ae19d6dc)
- Bug 1215744 - Unnamed class expressions shouldn't get a name property. (r=arai) (0ce0a96be4)
- Bug 1208747 - Move most of Stopwatch-related code to XPCOM-land (JSAPI-level);r=jandem (e28fa2f859)
- Bug 1184486 - Let PerformanceStats.jsm play nicer with process-per-tab. r=mconley (f0cf0d0eae)
- Bug 1198167 - nsPerformanceStatsService should wait for profile-before-change, not profile-before-shutdown. r=yoric (5ba3c98109)
- Bug 1199603 - Don't wait for shutdown to update nsPerformanceStats Telemetry. r=Mossop (110813977b)
- Bug 1205154 - Use channel->Open2() in js/xpconnect/src/XPCJSRuntime.cpp (r=sicking) (8efd629889)
- Bug 1208747 - Move most of Stopwatch-related code to XPCOM-land (XPCOM-level + XPConnect-level);r=froydnj (a1b1e83549)
- with some fixes
2022-10-20 11:04:02 +08:00

385 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/TextTrackManager.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/HTMLTrackElement.h"
#include "mozilla/dom/HTMLVideoElement.h"
#include "mozilla/dom/TextTrack.h"
#include "mozilla/dom/TextTrackCue.h"
#include "mozilla/dom/Event.h"
#include "mozilla/ClearOnShutdown.h"
#include "nsComponentManagerUtils.h"
#include "nsVariant.h"
#include "nsVideoFrame.h"
#include "nsIFrame.h"
#include "nsTArrayHelpers.h"
#include "nsIWebVTTParserWrapper.h"
namespace mozilla {
namespace dom {
CompareTextTracks::CompareTextTracks(HTMLMediaElement* aMediaElement)
{
mMediaElement = aMediaElement;
}
int32_t
CompareTextTracks::TrackChildPosition(TextTrack* aTextTrack) const {
HTMLTrackElement* trackElement = aTextTrack->GetTrackElement();
if (!trackElement) {
return -1;
}
return mMediaElement->IndexOf(trackElement);
}
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.
return false;
}
bool
CompareTextTracks::LessThan(TextTrack* aOne, TextTrack* aTwo) const
{
TextTrackSource sourceOne = aOne->GetTextTrackSource();
TextTrackSource sourceTwo = aTwo->GetTextTrackSource();
if (sourceOne != sourceTwo) {
return sourceOne == Track ||
(sourceOne == AddTextTrack && sourceTwo == MediaResourceSpecific);
}
switch (sourceOne) {
case Track: {
int32_t positionOne = TrackChildPosition(aOne);
int32_t positionTwo = TrackChildPosition(aTwo);
// If either position one or positiontwo are -1 then something has gone
// wrong. In this case we should just put them at the back of the list.
return positionOne != -1 && positionTwo != -1 &&
positionOne < positionTwo;
}
case 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.
return true;
case MediaResourceSpecific:
// No rules for Media Resource Specific tracks yet.
break;
}
return true;
}
NS_IMPL_CYCLE_COLLECTION(TextTrackManager, mMediaElement, mTextTracks,
mPendingTextTracks, mNewCues)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextTrackManager)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextTrackManager)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextTrackManager)
StaticRefPtr<nsIWebVTTParserWrapper> TextTrackManager::sParserWrapper;
TextTrackManager::TextTrackManager(HTMLMediaElement *aMediaElement)
: mMediaElement(aMediaElement)
, performedTrackSelection(false)
{
nsISupports* parentObject =
mMediaElement->OwnerDoc()->GetParentObject();
NS_ENSURE_TRUE_VOID(parentObject);
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(parentObject);
mNewCues = new TextTrackCueList(window);
mTextTracks = new TextTrackList(window, this);
mPendingTextTracks = new TextTrackList(window, this);
if (!sParserWrapper) {
nsCOMPtr<nsIWebVTTParserWrapper> parserWrapper =
do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID);
sParserWrapper = parserWrapper;
ClearOnShutdown(&sParserWrapper);
}
}
TextTrackManager::~TextTrackManager()
{
}
TextTrackList*
TextTrackManager::GetTextTracks() const
{
return mTextTracks;
}
already_AddRefed<TextTrack>
TextTrackManager::AddTextTrack(TextTrackKind aKind, const nsAString& aLabel,
const nsAString& aLanguage,
TextTrackMode aMode,
TextTrackReadyState aReadyState,
TextTrackSource aTextTrackSource)
{
if (!mMediaElement || !mTextTracks) {
return nullptr;
}
RefPtr<TextTrack> ttrack =
mTextTracks->AddTextTrack(aKind, aLabel, aLanguage, aMode, aReadyState,
aTextTrackSource, CompareTextTracks(mMediaElement));
AddCues(ttrack);
if (aTextTrackSource == Track) {
HonorUserPreferencesForTrackSelection();
}
return ttrack.forget();
}
void
TextTrackManager::AddTextTrack(TextTrack* aTextTrack)
{
if (!mMediaElement || !mTextTracks) {
return;
}
mTextTracks->AddTextTrack(aTextTrack, CompareTextTracks(mMediaElement));
AddCues(aTextTrack);
if (aTextTrack->GetTextTrackSource() == Track) {
HonorUserPreferencesForTrackSelection();
}
}
void
TextTrackManager::AddCues(TextTrack* aTextTrack)
{
if (!mNewCues) {
return;
}
TextTrackCueList* cueList = aTextTrack->GetCues();
if (cueList) {
bool dummy;
for (uint32_t i = 0; i < cueList->Length(); ++i) {
mNewCues->AddCue(*cueList->IndexedGetter(i, dummy));
}
}
}
void
TextTrackManager::RemoveTextTrack(TextTrack* aTextTrack, bool aPendingListOnly)
{
if (!mPendingTextTracks || !mTextTracks) {
return;
}
mPendingTextTracks->RemoveTextTrack(aTextTrack);
if (aPendingListOnly) {
return;
}
mTextTracks->RemoveTextTrack(aTextTrack);
}
void
TextTrackManager::DidSeek()
{
if (mTextTracks) {
mTextTracks->DidSeek();
}
}
void
TextTrackManager::UpdateCueDisplay()
{
if (!mMediaElement || !mTextTracks) {
return;
}
nsIFrame* frame = mMediaElement->GetPrimaryFrame();
nsVideoFrame* videoFrame = do_QueryFrame(frame);
if (!videoFrame) {
return;
}
nsCOMPtr<nsIContent> overlay = videoFrame->GetCaptionOverlay();
if (!overlay) {
return;
}
nsTArray<RefPtr<TextTrackCue> > activeCues;
mTextTracks->UpdateAndGetShowingCues(activeCues);
if (activeCues.Length() > 0) {
RefPtr<nsVariant> jsCues = new nsVariant();
jsCues->SetAsArray(nsIDataType::VTYPE_INTERFACE,
&NS_GET_IID(nsIDOMEventTarget),
activeCues.Length(),
static_cast<void*>(activeCues.Elements()));
nsPIDOMWindow* window = mMediaElement->OwnerDoc()->GetWindow();
if (window) {
sParserWrapper->ProcessCues(window, jsCues, overlay);
}
} else if (overlay->Length() > 0) {
nsContentUtils::SetNodeTextContent(overlay, EmptyString(), true);
}
}
void
TextTrackManager::AddCue(TextTrackCue& aCue)
{
if (mNewCues) {
mNewCues->AddCue(aCue);
}
}
void
TextTrackManager::PopulatePendingList()
{
if (!mTextTracks || !mPendingTextTracks || !mMediaElement) {
return;
}
uint32_t len = mTextTracks->Length();
bool dummy;
for (uint32_t index = 0; index < len; ++index) {
TextTrack* ttrack = mTextTracks->IndexedGetter(index, dummy);
if (ttrack && ttrack->Mode() != TextTrackMode::Disabled &&
ttrack->ReadyState() == TextTrackReadyState::Loading) {
mPendingTextTracks->AddTextTrack(ttrack,
CompareTextTracks(mMediaElement));
}
}
}
void
TextTrackManager::AddListeners()
{
if (mMediaElement) {
mMediaElement->AddEventListener(NS_LITERAL_STRING("resizevideocontrols"),
this, false, false);
}
}
void
TextTrackManager::HonorUserPreferencesForTrackSelection()
{
if (performedTrackSelection || !mTextTracks) {
return;
}
TextTrackKind ttKinds[] = { TextTrackKind::Captions,
TextTrackKind::Subtitles };
// Steps 1 - 3: Perform automatic track selection for different TextTrack
// Kinds.
PerformTrackSelection(ttKinds, ArrayLength(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);
}
}
performedTrackSelection = true;
}
bool
TextTrackManager::TrackIsDefault(TextTrack* aTextTrack)
{
HTMLTrackElement* trackElement = aTextTrack->GetTrackElement();
if (!trackElement) {
return false;
}
return trackElement->Default();
}
void
TextTrackManager::PerformTrackSelection(TextTrackKind aTextTrackKind)
{
TextTrackKind ttKinds[] = { aTextTrackKind };
PerformTrackSelection(ttKinds, ArrayLength(ttKinds));
}
void
TextTrackManager::PerformTrackSelection(TextTrackKind aTextTrackKinds[],
uint32_t size)
{
nsTArray<TextTrack*> candidates;
GetTextTracksOfKinds(aTextTrackKinds, size, candidates);
// 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) {
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);
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(nsIDOMEvent* aEvent)
{
if (!mTextTracks) {
return NS_OK;
}
nsAutoString type;
aEvent->GetType(type);
if (type.EqualsLiteral("resizevideocontrols")) {
for (uint32_t i = 0; i< mTextTracks->Length(); i++) {
((*mTextTracks)[i])->SetCuesDirty();
}
}
return NS_OK;
}
} // namespace dom
} // namespace mozilla