#include "config.h"
#if ENABLE(VIDEO_TRACK)
#include "TextTrackCue.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "Event.h"
#include "Logging.h"
#include "NodeTraversal.h"
#include "Text.h"
#include "TextTrack.h"
#include "TextTrackCueList.h"
#include "VTTCue.h"
#include "VTTRegionList.h"
#include <wtf/MathExtras.h>
namespace WebCore {
static const int invalidCueIndex = -1;
const AtomicString& TextTrackCue::cueShadowPseudoId()
{
static NeverDestroyed<const AtomicString> cue("cue", AtomicString::ConstructFromLiteral);
return cue;
}
TextTrackCue::TextTrackCue(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end)
: m_startTime(start)
, m_endTime(end)
, m_cueIndex(invalidCueIndex)
, m_processingCueChanges(0)
, m_track(0)
, m_scriptExecutionContext(context)
, m_isActive(false)
, m_pauseOnExit(false)
{
ASSERT(m_scriptExecutionContext.isDocument());
}
void TextTrackCue::willChange()
{
if (++m_processingCueChanges > 1)
return;
if (m_track)
m_track->cueWillChange(this);
}
void TextTrackCue::didChange()
{
ASSERT(m_processingCueChanges);
if (--m_processingCueChanges)
return;
if (m_track)
m_track->cueDidChange(this);
}
TextTrack* TextTrackCue::track() const
{
return m_track;
}
void TextTrackCue::setTrack(TextTrack* track)
{
m_track = track;
}
void TextTrackCue::setId(const String& id)
{
if (m_id == id)
return;
willChange();
m_id = id;
didChange();
}
void TextTrackCue::setStartTime(double value)
{
if (m_startTime.toDouble() == value || value < 0)
return;
setStartTime(MediaTime::createWithDouble(value));
}
void TextTrackCue::setStartTime(const MediaTime& value)
{
willChange();
m_startTime = value;
didChange();
}
void TextTrackCue::setEndTime(double value)
{
if (m_endTime.toDouble() == value || value < 0)
return;
setEndTime(MediaTime::createWithDouble(value));
}
void TextTrackCue::setEndTime(const MediaTime& value)
{
willChange();
m_endTime = value;
didChange();
}
void TextTrackCue::setPauseOnExit(bool value)
{
if (m_pauseOnExit == value)
return;
m_pauseOnExit = value;
}
int TextTrackCue::cueIndex()
{
if (m_cueIndex == invalidCueIndex) {
ASSERT(track());
ASSERT(track()->cues());
if (TextTrackCueList* cueList = track()->cues())
m_cueIndex = cueList->getCueIndex(this);
}
return m_cueIndex;
}
void TextTrackCue::invalidateCueIndex()
{
m_cueIndex = invalidCueIndex;
}
bool TextTrackCue::dispatchEvent(Event& event)
{
if (!track() || track()->mode() == TextTrack::Mode::Disabled)
return false;
return EventTarget::dispatchEvent(event);
}
bool TextTrackCue::isActive()
{
return m_isActive && track() && track()->mode() != TextTrack::Mode::Disabled;
}
void TextTrackCue::setIsActive(bool active)
{
m_isActive = active;
}
bool TextTrackCue::isOrderedBefore(const TextTrackCue* other) const
{
return startMediaTime() < other->startMediaTime() || (startMediaTime() == other->startMediaTime() && endMediaTime() > other->endMediaTime());
}
bool TextTrackCue::cueContentsMatch(const TextTrackCue& cue) const
{
if (cueType() != cue.cueType())
return false;
if (id() != cue.id())
return false;
return true;
}
bool TextTrackCue::isEqual(const TextTrackCue& cue, TextTrackCue::CueMatchRules match) const
{
if (cueType() != cue.cueType())
return false;
if (match != IgnoreDuration && endMediaTime() != cue.endMediaTime())
return false;
if (!hasEquivalentStartTime(cue))
return false;
if (!cueContentsMatch(cue))
return false;
return true;
}
bool TextTrackCue::hasEquivalentStartTime(const TextTrackCue& cue) const
{
MediaTime startTimeVariance = MediaTime::zeroTime();
if (track())
startTimeVariance = track()->startTimeVariance();
else if (cue.track())
startTimeVariance = cue.track()->startTimeVariance();
return abs(abs(startMediaTime()) - abs(cue.startMediaTime())) <= startTimeVariance;
}
bool TextTrackCue::doesExtendCue(const TextTrackCue& cue) const
{
if (!cueContentsMatch(cue))
return false;
if (endMediaTime() != cue.startMediaTime())
return false;
return true;
}
}
#endif