2013-05-21 09:14:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 et tw=78: */
|
|
|
|
/* 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/TextTrack.h"
|
|
|
|
#include "mozilla/dom/TextTrackBinding.h"
|
2014-01-27 10:10:06 -08:00
|
|
|
#include "mozilla/dom/TextTrackList.h"
|
2013-06-14 13:10:36 -07:00
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "mozilla/dom/TextTrackCueList.h"
|
2013-09-13 16:44:33 -07:00
|
|
|
#include "mozilla/dom/TextTrackRegion.h"
|
2013-06-14 13:10:36 -07:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2013-10-24 21:14:36 -07:00
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
2013-05-21 09:14:00 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-02-27 11:07:39 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_5(TextTrack,
|
2013-08-15 08:11:15 -07:00
|
|
|
nsDOMEventTargetHelper,
|
|
|
|
mParent,
|
|
|
|
mCueList,
|
2013-09-13 16:44:33 -07:00
|
|
|
mActiveCueList,
|
2014-02-27 11:07:39 -08:00
|
|
|
mTextTrackList,
|
|
|
|
mTrackElement)
|
2013-05-21 09:14:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(TextTrack, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TextTrack, nsDOMEventTargetHelper)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrack)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
2014-01-27 12:17:20 -08:00
|
|
|
TextTrack::TextTrack(nsISupports* aParent,
|
|
|
|
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-02-27 09:47:23 -08:00
|
|
|
TextTrackSource aTextTrackSource)
|
2013-06-14 13:10:36 -07:00
|
|
|
: mParent(aParent)
|
2014-02-27 09:47:23 -08:00
|
|
|
, mTextTrackSource(aTextTrackSource)
|
2013-06-14 13:10:36 -07:00
|
|
|
{
|
|
|
|
SetDefaultSettings();
|
2014-01-27 12:17:20 -08:00
|
|
|
mKind = aKind;
|
|
|
|
mLabel = aLabel;
|
|
|
|
mLanguage = aLanguage;
|
2014-03-13 10:18:06 -07:00
|
|
|
mMode = aMode;
|
2013-06-14 13:10:36 -07:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-05-21 09:14:00 -07:00
|
|
|
TextTrack::TextTrack(nsISupports* aParent,
|
2014-01-27 12:17:20 -08:00
|
|
|
TextTrackList* aTextTrackList,
|
2013-05-21 09:14:00 -07:00
|
|
|
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-02-27 09:47:23 -08:00
|
|
|
TextTrackSource aTextTrackSource)
|
2013-05-21 09:14:00 -07:00
|
|
|
: mParent(aParent)
|
2014-01-27 12:17:20 -08:00
|
|
|
, mTextTrackList(aTextTrackList)
|
2014-02-27 09:47:23 -08:00
|
|
|
, mTextTrackSource(aTextTrackSource)
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
2013-06-14 13:10:36 -07:00
|
|
|
SetDefaultSettings();
|
|
|
|
mKind = aKind;
|
|
|
|
mLabel = aLabel;
|
|
|
|
mLanguage = aLanguage;
|
2014-03-13 10:18:06 -07:00
|
|
|
mMode = aMode;
|
2013-05-21 09:14:00 -07:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:10:36 -07:00
|
|
|
void
|
|
|
|
TextTrack::SetDefaultSettings()
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
2013-06-14 13:10:36 -07:00
|
|
|
mKind = TextTrackKind::Subtitles;
|
|
|
|
mMode = TextTrackMode::Hidden;
|
|
|
|
mCueList = new TextTrackCueList(mParent);
|
|
|
|
mActiveCueList = new TextTrackCueList(mParent);
|
|
|
|
mCuePos = 0;
|
|
|
|
mDirty = false;
|
2014-03-13 11:29:32 -07:00
|
|
|
mReadyState = TextTrackReadyState::NotLoaded;
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
TextTrack::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return TextTrackBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrack::SetMode(TextTrackMode aValue)
|
|
|
|
{
|
2013-12-17 21:19:09 -08:00
|
|
|
if (mMode != aValue) {
|
|
|
|
mMode = aValue;
|
2014-01-27 12:17:20 -08:00
|
|
|
if (mTextTrackList) {
|
|
|
|
mTextTrackList->CreateAndDispatchChangeEvent();
|
2013-12-17 21:19:09 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrack::AddCue(TextTrackCue& aCue)
|
|
|
|
{
|
|
|
|
mCueList->AddCue(aCue);
|
2014-01-27 12:17:20 -08:00
|
|
|
if (mTextTrackList) {
|
|
|
|
HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
|
|
|
|
if (mediaElement) {
|
|
|
|
mediaElement->AddCue(aCue);
|
|
|
|
}
|
2014-01-15 08:30:58 -08:00
|
|
|
}
|
2013-06-14 13:10:36 -07:00
|
|
|
SetDirty();
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-28 10:31:43 -07:00
|
|
|
TextTrack::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv)
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
2013-06-28 10:31:43 -07:00
|
|
|
mCueList->RemoveCue(aCue, aRv);
|
2013-06-14 13:10:36 -07:00
|
|
|
SetDirty();
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrack::CueChanged(TextTrackCue& aCue)
|
|
|
|
{
|
|
|
|
//XXX: Implement Cue changed. Bug 867823.
|
|
|
|
}
|
|
|
|
|
2013-12-12 07:57:21 -08:00
|
|
|
void
|
|
|
|
TextTrack::UpdateActiveCueList()
|
2013-06-14 13:10:36 -07:00
|
|
|
{
|
2014-02-07 10:59:26 -08:00
|
|
|
if (!mTextTrackList) {
|
2014-01-27 12:17:20 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
|
|
|
|
if (!mediaElement) {
|
2013-12-12 07:57:21 -08:00
|
|
|
return;
|
2013-06-14 13:10:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are dirty, i.e. an event happened that may cause the sorted mCueList
|
|
|
|
// to have changed like a seek or an insert for a cue, than we need to rebuild
|
|
|
|
// the active cue list from scratch.
|
|
|
|
if (mDirty) {
|
|
|
|
mCuePos = 0;
|
2013-12-13 07:09:33 -08:00
|
|
|
mDirty = false;
|
2013-06-14 13:10:36 -07:00
|
|
|
mActiveCueList->RemoveAll();
|
|
|
|
}
|
|
|
|
|
2014-01-27 12:17:20 -08:00
|
|
|
double playbackTime = mediaElement->CurrentTime();
|
2013-06-14 13:10:36 -07:00
|
|
|
// Remove all the cues from the active cue list whose end times now occur
|
2013-12-13 07:09:33 -08:00
|
|
|
// earlier then the current playback time.
|
|
|
|
for (uint32_t i = mActiveCueList->Length(); i > 0; i--) {
|
|
|
|
if ((*mActiveCueList)[i - 1]->EndTime() < playbackTime) {
|
|
|
|
mActiveCueList->RemoveCueAt(i - 1);
|
|
|
|
}
|
2013-06-14 13:10:36 -07:00
|
|
|
}
|
|
|
|
// Add all the cues, starting from the position of the last cue that was
|
|
|
|
// added, that have valid start and end times for the current playback time.
|
|
|
|
// We can stop iterating safely once we encounter a cue that does not have
|
2013-12-13 07:09:33 -08:00
|
|
|
// a valid start time as the cue list is sorted.
|
|
|
|
for (; mCuePos < mCueList->Length() &&
|
|
|
|
(*mCueList)[mCuePos]->StartTime() <= playbackTime; mCuePos++) {
|
|
|
|
if ((*mCueList)[mCuePos]->EndTime() >= playbackTime) {
|
|
|
|
mActiveCueList->AddCue(*(*mCueList)[mCuePos]);
|
2013-06-14 13:10:36 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 07:57:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCueList*
|
|
|
|
TextTrack::GetActiveCues() {
|
2014-02-07 10:59:26 -08:00
|
|
|
if (mMode != TextTrackMode::Disabled) {
|
|
|
|
UpdateActiveCueList();
|
|
|
|
return mActiveCueList;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-06-14 13:10:36 -07:00
|
|
|
}
|
|
|
|
|
2013-12-12 07:57:21 -08:00
|
|
|
void
|
|
|
|
TextTrack::GetActiveCueArray(nsTArray<nsRefPtr<TextTrackCue> >& aCues)
|
|
|
|
{
|
2014-02-07 10:59:26 -08:00
|
|
|
if (mMode != TextTrackMode::Disabled) {
|
|
|
|
UpdateActiveCueList();
|
|
|
|
mActiveCueList->GetArray(aCues);
|
|
|
|
}
|
2013-12-12 07:57:21 -08:00
|
|
|
}
|
|
|
|
|
2014-03-13 11:29:32 -07:00
|
|
|
TextTrackReadyState
|
2013-10-24 21:14:36 -07:00
|
|
|
TextTrack::ReadyState() const
|
|
|
|
{
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-13 11:29:32 -07:00
|
|
|
TextTrack::SetReadyState(TextTrackReadyState aState)
|
2013-10-24 21:14:36 -07:00
|
|
|
{
|
|
|
|
mReadyState = aState;
|
2014-01-27 12:17:20 -08:00
|
|
|
|
|
|
|
if (!mTextTrackList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
|
2014-03-13 11:29:32 -07:00
|
|
|
if (mediaElement && (mReadyState == TextTrackReadyState::Loaded||
|
|
|
|
mReadyState == TextTrackReadyState::FailedToLoad)) {
|
2014-01-27 12:17:20 -08:00
|
|
|
mediaElement->RemoveTextTrack(this, true);
|
2013-10-24 21:14:36 -07:00
|
|
|
}
|
2013-10-24 21:14:36 -07:00
|
|
|
}
|
|
|
|
|
2014-01-27 10:10:06 -08:00
|
|
|
TextTrackList*
|
|
|
|
TextTrack::GetTextTrackList()
|
|
|
|
{
|
|
|
|
return mTextTrackList;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrack::SetTextTrackList(TextTrackList* aTextTrackList)
|
|
|
|
{
|
|
|
|
mTextTrackList = aTextTrackList;
|
|
|
|
}
|
|
|
|
|
2014-02-27 11:07:39 -08:00
|
|
|
HTMLTrackElement*
|
|
|
|
TextTrack::GetTrackElement() {
|
|
|
|
return mTrackElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrack::SetTrackElement(HTMLTrackElement* aTrackElement) {
|
|
|
|
mTrackElement = aTrackElement;
|
|
|
|
}
|
|
|
|
|
2013-05-21 09:14:00 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|