2013-10-23 11:45:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set tw=80 expandtab softtabstop=2 ts=2 sw=2: */
|
|
|
|
|
|
|
|
/* 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"
|
2013-10-24 21:14:36 -07:00
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
2014-04-07 12:42:33 -07:00
|
|
|
#include "mozilla/dom/HTMLVideoElement.h"
|
2013-12-12 09:02:17 -08:00
|
|
|
#include "mozilla/dom/TextTrack.h"
|
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2014-04-07 12:42:33 -07:00
|
|
|
#include "mozilla/dom/Event.h"
|
2013-12-12 09:02:17 -08:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsVideoFrame.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsTArrayHelpers.h"
|
|
|
|
#include "nsIWebVTTParserWrapper.h"
|
2013-10-23 11:45:00 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-02-27 09:47:23 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(TextTrackManager, mMediaElement, mTextTracks,
|
|
|
|
mPendingTextTracks, mNewCues)
|
2014-04-07 12:42:33 -07:00
|
|
|
|
|
|
|
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)
|
2013-10-23 11:45:00 -07:00
|
|
|
|
2013-12-12 09:02:17 -08:00
|
|
|
StaticRefPtr<nsIWebVTTParserWrapper> TextTrackManager::sParserWrapper;
|
|
|
|
|
2013-10-23 11:45:00 -07:00
|
|
|
TextTrackManager::TextTrackManager(HTMLMediaElement *aMediaElement)
|
|
|
|
: mMediaElement(aMediaElement)
|
2014-02-28 12:40:00 -08:00
|
|
|
, performedTrackSelection(false)
|
2013-10-23 11:45:00 -07:00
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
bool hasHadScriptObject = true;
|
|
|
|
nsIScriptGlobalObject* scriptObject =
|
|
|
|
mMediaElement->OwnerDoc()->GetScriptHandlingObject(hasHadScriptObject);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE_VOID(scriptObject || !hasHadScriptObject);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(scriptObject);
|
|
|
|
mNewCues = new TextTrackCueList(window);
|
|
|
|
mTextTracks = new TextTrackList(window, this);
|
|
|
|
mPendingTextTracks = new TextTrackList(window, this);
|
2013-12-12 09:02:17 -08:00
|
|
|
|
|
|
|
if (!sParserWrapper) {
|
|
|
|
nsCOMPtr<nsIWebVTTParserWrapper> parserWrapper =
|
|
|
|
do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID);
|
|
|
|
sParserWrapper = parserWrapper;
|
|
|
|
ClearOnShutdown(&sParserWrapper);
|
|
|
|
}
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-24 19:09:15 -07:00
|
|
|
TextTrackManager::~TextTrackManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:45:00 -07:00
|
|
|
TextTrackList*
|
|
|
|
TextTrackManager::TextTracks() const
|
|
|
|
{
|
|
|
|
return mTextTracks;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TextTrack>
|
|
|
|
TextTrackManager::AddTextTrack(TextTrackKind aKind, const nsAString& aLabel,
|
2014-02-27 09:47:23 -08:00
|
|
|
const nsAString& aLanguage,
|
2014-03-13 10:18:06 -07:00
|
|
|
TextTrackMode aMode,
|
2014-03-13 11:41:21 -07:00
|
|
|
TextTrackReadyState aReadyState,
|
2014-02-27 09:47:23 -08:00
|
|
|
TextTrackSource aTextTrackSource)
|
2013-10-23 11:45:00 -07:00
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mMediaElement || !mTextTracks) {
|
2014-02-27 09:47:23 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-24 21:14:36 -07:00
|
|
|
nsRefPtr<TextTrack> ttrack =
|
2014-03-13 11:41:21 -07:00
|
|
|
mTextTracks->AddTextTrack(aKind, aLabel, aLanguage, aMode, aReadyState,
|
|
|
|
aTextTrackSource, CompareTextTracks(mMediaElement));
|
2014-01-06 10:03:50 -08:00
|
|
|
AddCues(ttrack);
|
2014-02-28 12:40:00 -08:00
|
|
|
|
|
|
|
if (aTextTrackSource == Track) {
|
|
|
|
HonorUserPreferencesForTrackSelection();
|
|
|
|
}
|
|
|
|
|
2013-10-24 21:14:36 -07:00
|
|
|
return ttrack.forget();
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackManager::AddTextTrack(TextTrack* aTextTrack)
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mMediaElement || !mTextTracks) {
|
2014-02-27 09:47:23 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mTextTracks->AddTextTrack(aTextTrack, CompareTextTracks(mMediaElement));
|
2014-01-06 10:03:50 -08:00
|
|
|
AddCues(aTextTrack);
|
2014-02-28 12:40:00 -08:00
|
|
|
if (aTextTrack->GetTextTrackSource() == Track) {
|
|
|
|
HonorUserPreferencesForTrackSelection();
|
|
|
|
}
|
2014-01-06 10:03:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackManager::AddCues(TextTrack* aTextTrack)
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mNewCues) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-06 10:03:50 -08:00
|
|
|
TextTrackCueList* cueList = aTextTrack->GetCues();
|
|
|
|
if (cueList) {
|
|
|
|
bool dummy;
|
|
|
|
for (uint32_t i = 0; i < cueList->Length(); ++i) {
|
|
|
|
mNewCues->AddCue(*cueList->IndexedGetter(i, dummy));
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-24 21:14:36 -07:00
|
|
|
TextTrackManager::RemoveTextTrack(TextTrack* aTextTrack, bool aPendingListOnly)
|
2013-10-23 11:45:00 -07:00
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mPendingTextTracks || !mTextTracks) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-24 21:14:36 -07:00
|
|
|
mPendingTextTracks->RemoveTextTrack(aTextTrack);
|
|
|
|
if (aPendingListOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:15:00 -07:00
|
|
|
mTextTracks->RemoveTextTrack(aTextTrack);
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackManager::DidSeek()
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (mTextTracks) {
|
|
|
|
mTextTracks->DidSeek();
|
|
|
|
}
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-12 09:02:17 -08:00
|
|
|
TextTrackManager::UpdateCueDisplay()
|
2013-10-23 11:45:00 -07:00
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mMediaElement || !mTextTracks) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 09:02:17 -08:00
|
|
|
nsIFrame* frame = mMediaElement->GetPrimaryFrame();
|
|
|
|
nsVideoFrame* videoFrame = do_QueryFrame(frame);
|
|
|
|
if (!videoFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> overlay = videoFrame->GetCaptionOverlay();
|
|
|
|
if (!overlay) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<TextTrackCue> > activeCues;
|
2014-02-27 07:02:40 -08:00
|
|
|
mTextTracks->UpdateAndGetShowingCues(activeCues);
|
2013-12-12 09:02:17 -08:00
|
|
|
|
|
|
|
if (activeCues.Length() > 0) {
|
|
|
|
nsCOMPtr<nsIWritableVariant> jsCues =
|
|
|
|
do_CreateInstance("@mozilla.org/variant;1");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-10-23 11:45:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-06 10:03:50 -08:00
|
|
|
void
|
|
|
|
TextTrackManager::AddCue(TextTrackCue& aCue)
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (mNewCues) {
|
|
|
|
mNewCues->AddCue(aCue);
|
|
|
|
}
|
2014-01-06 10:03:50 -08:00
|
|
|
}
|
|
|
|
|
2013-10-24 21:14:36 -07:00
|
|
|
void
|
|
|
|
TextTrackManager::PopulatePendingList()
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mTextTracks || !mPendingTextTracks || !mMediaElement) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-24 21:14:36 -07:00
|
|
|
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 &&
|
2014-03-13 11:29:32 -07:00
|
|
|
ttrack->ReadyState() == TextTrackReadyState::Loading) {
|
2014-02-27 09:47:23 -08:00
|
|
|
mPendingTextTracks->AddTextTrack(ttrack,
|
|
|
|
CompareTextTracks(mMediaElement));
|
2013-10-24 21:14:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 12:42:33 -07:00
|
|
|
void
|
|
|
|
TextTrackManager::AddListeners()
|
|
|
|
{
|
|
|
|
if (mMediaElement) {
|
|
|
|
mMediaElement->AddEventListener(NS_LITERAL_STRING("resizevideocontrols"),
|
|
|
|
this, false, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 12:40:00 -08:00
|
|
|
void
|
|
|
|
TextTrackManager::HonorUserPreferencesForTrackSelection()
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (performedTrackSelection || !mTextTracks) {
|
2014-02-28 12:40:00 -08:00
|
|
|
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)
|
|
|
|
{
|
2014-04-07 10:58:38 -07:00
|
|
|
if (!mTextTracks) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 12:40:00 -08:00
|
|
|
for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
|
|
|
|
TextTrack* textTrack = (*mTextTracks)[i];
|
|
|
|
if (textTrack->Kind() == aTextTrackKind) {
|
|
|
|
aTextTracks.AppendElement(textTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 12:42:33 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:45:00 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|