Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1223)

Unified Diff: Source/core/html/HTMLMediaElement.cpp

Issue 284513003: Implement AudioTrack, AudioTrackList, VideoTrack, and VideoTrackList (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: platform handle per track Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLMediaElement.cpp
diff --git a/Source/core/html/HTMLMediaElement.cpp b/Source/core/html/HTMLMediaElement.cpp
index 8fd4af03e209fc46bb74cb770e05de7302d4e013..778c74f5d0d15649b8e25b7f918954d79f16ef2e 100644
--- a/Source/core/html/HTMLMediaElement.cpp
+++ b/Source/core/html/HTMLMediaElement.cpp
@@ -51,9 +51,13 @@
#include "core/html/MediaFragmentURIParser.h"
#include "core/html/TimeRanges.h"
#include "core/html/shadow/MediaControls.h"
+#include "core/html/track/AudioTrack.h"
+#include "core/html/track/AudioTrackList.h"
#include "core/html/track/InbandTextTrack.h"
#include "core/html/track/TextTrackCueList.h"
#include "core/html/track/TextTrackList.h"
+#include "core/html/track/VideoTrack.h"
+#include "core/html/track/VideoTrackList.h"
#include "core/loader/FrameLoader.h"
#include "core/rendering/RenderVideo.h"
#include "core/rendering/RenderView.h"
@@ -85,6 +89,7 @@ using namespace std;
using blink::WebInbandTextTrack;
using blink::WebMediaPlayer;
using blink::WebMimeRegistry;
+using blink::WebMediaPlayerClient;
namespace WebCore {
@@ -169,6 +174,52 @@ private:
HTMLMediaElement* m_mediaElement;
};
+static const AtomicString& AudioKindToString(WebMediaPlayerClient::AudioTrackKind kind)
+{
+ switch (kind) {
+ case WebMediaPlayerClient::AudioTrackKindNone:
+ return emptyAtom;
+ case WebMediaPlayerClient::AudioTrackKindAlternative:
+ return AudioTrack::alternativeKeyword();
+ case WebMediaPlayerClient::AudioTrackKindDescriptions:
+ return AudioTrack::descriptionsKeyword();
+ case WebMediaPlayerClient::AudioTrackKindMain:
+ return AudioTrack::mainKeyword();
+ case WebMediaPlayerClient::AudioTrackKindMainDescriptions:
+ return AudioTrack::mainDescriptionsKeyword();
+ case WebMediaPlayerClient::AudioTrackKindTranslation:
+ return AudioTrack::translationKeyword();
+ case WebMediaPlayerClient::AudioTrackKindCommentary:
+ return AudioTrack::commentaryKeyword();
+ }
+
+ ASSERT_NOT_REACHED();
+ return emptyAtom;
+}
+
+static const AtomicString& VideoKindToString(WebMediaPlayerClient::VideoTrackKind kind)
+{
+ switch (kind) {
+ case WebMediaPlayerClient::VideoTrackKindNone:
+ return emptyAtom;
+ case WebMediaPlayerClient::VideoTrackKindAlternative:
+ return VideoTrack::alternativeKeyword();
+ case WebMediaPlayerClient::VideoTrackKindCaptions:
+ return VideoTrack::captionsKeyword();
+ case WebMediaPlayerClient::VideoTrackKindMain:
+ return VideoTrack::mainKeyword();
+ case WebMediaPlayerClient::VideoTrackKindSign:
+ return VideoTrack::signKeyword();
+ case WebMediaPlayerClient::VideoTrackKindSubtitles:
+ return VideoTrack::subtitlesKeyword();
+ case WebMediaPlayerClient::VideoTrackKindCommentary:
+ return VideoTrack::commentaryKeyword();
+ }
+
+ ASSERT_NOT_REACHED();
+ return emptyAtom;
+}
+
static bool canLoadURL(const KURL& url, const ContentType& contentType, const String& keySystem)
{
DEFINE_STATIC_LOCAL(const String, codecs, ("codecs"));
@@ -240,6 +291,7 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum
, m_loadTimer(this, &HTMLMediaElement::loadTimerFired)
, m_progressEventTimer(this, &HTMLMediaElement::progressEventTimerFired)
, m_playbackProgressTimer(this, &HTMLMediaElement::playbackProgressTimerFired)
+ , m_audioTracksTimer(this, &HTMLMediaElement::audioTracksTimerFired)
, m_playedTimeRanges()
, m_asyncEventQueue(GenericEventQueue::create(this))
, m_playbackRate(1.0f)
@@ -283,6 +335,8 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum
, m_haveVisibleTextTrack(false)
, m_processingPreferenceChange(false)
, m_lastTextTrackUpdateTime(-1)
+ , m_audioTracks(AudioTrackList::create(*this))
+ , m_videoTracks(VideoTrackList::create(*this))
, m_textTracks(nullptr)
, m_ignoreTrackDisplayUpdate(0)
#if ENABLE(WEB_AUDIO)
@@ -317,6 +371,9 @@ HTMLMediaElement::~HTMLMediaElement()
m_textTracks->clearOwner();
#endif
+ m_audioTracks->shutdown();
+ m_videoTracks->shutdown();
+
if (m_mediaController) {
m_mediaController->removeMediaElement(this);
m_mediaController = nullptr;
@@ -619,6 +676,7 @@ void HTMLMediaElement::prepareForLoad()
m_loadTimer.stop();
// FIXME: Figure out appropriate place to reset LoadTextTrackResource if necessary and set m_pendingActionFlags to 0 here.
m_pendingActionFlags &= ~LoadMediaResource;
+ m_audioTracksTimer.stop();
m_sentEndEvent = false;
m_sentStalledEvent = false;
m_haveFiredLoadedData = false;
@@ -1602,7 +1660,12 @@ void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
}
if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
+ createPlaceholderTracksIfNecessary();
+
prepareMediaFragmentURI();
+
+ selectInitialTracksIfNecessary();
+
scheduleEvent(EventTypeNames::durationchange);
if (isHTMLVideoElement(*this))
scheduleEvent(EventTypeNames::resize);
@@ -2219,6 +2282,107 @@ void HTMLMediaElement::togglePlayState()
}
}
+AudioTrackList& HTMLMediaElement::audioTracks()
+{
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+ return *m_audioTracks;
+}
+
+void HTMLMediaElement::audioTrackChanged(const String& audioTrackID)
+{
+ WTF_LOG(Media, "HTMLMediaElement::audioTrackChanged('%s')", audioTrackID.ascii().data());
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+ ASSERT(!audioTrackID.isEmpty());
+
+ audioTracks().scheduleChangeEvent();
+
+ // FIXME: Add call on m_mediaSource to notify it of track changes once the SourceBuffer.audioTracks attribute is added.
+
+ if (!m_audioTracksTimer.isActive())
+ m_audioTracksTimer.startOneShot(0, FROM_HERE);
+}
+
+void HTMLMediaElement::addAudioTrack(const String& id, blink::WebMediaPlayerClient::AudioTrackKind kind, const AtomicString& label, const AtomicString& language, bool enabled)
+{
+ AtomicString kindString = AudioKindToString(kind);
+ WTF_LOG(Media, "HTMLMediaElement::addAudioTrack('%s', '%s', '%s', '%s', %d)",
+ id.ascii().data(), kindString.ascii().data(), label.ascii().data(), language.ascii().data(), enabled);
+ ASSERT(!id.isEmpty());
+
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ RefPtr<AudioTrack> audioTrack = AudioTrack::create(this, id, kindString, label, language);
+ audioTracks().add(audioTrack.get());
+
+ if (enabled)
+ audioTrack->setEnabled(true);
+}
+
+void HTMLMediaElement::removeAudioTrack(const String& id)
+{
+ WTF_LOG(Media, "HTMLMediaElement::removeAudioTrack('%s')", id.ascii().data());
+ ASSERT(!id.isEmpty());
+
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ audioTracks().remove(id);
+}
+
+VideoTrackList& HTMLMediaElement::videoTracks()
+{
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+ return *m_videoTracks;
+}
+
+void HTMLMediaElement::selectedVideoTrackChanged(const String& selectedTrackID)
+{
+ WTF_LOG(Media, "HTMLMediaElement::selectedVideoTrackChanged('%s')", selectedTrackID.ascii().data());
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+
+ String unselectedTrackID;
+
+ int oldSelectedIndex = videoTracks().selectedIndex();
+ if (oldSelectedIndex != -1)
+ unselectedTrackID = videoTracks().anonymousIndexedGetter(oldSelectedIndex)->id();
+
+ ASSERT(unselectedTrackID != selectedTrackID);
+ ASSERT(!unselectedTrackID.isEmpty() || !selectedTrackID.isEmpty());
+
+ videoTracks().trackSelected(selectedTrackID);
+
+ // FIXME: Add call on m_mediaSource to notify it of track changes once the SourceBuffer.videoTracks attribute is added.
+
+ if (webMediaPlayer())
+ webMediaPlayer()->selectedVideoTrackChanged(selectedTrackID);
+}
+
+void HTMLMediaElement::addVideoTrack(const String& id, blink::WebMediaPlayerClient::VideoTrackKind kind, const AtomicString& label, const AtomicString& language, bool selected)
+{
+ AtomicString kindString = VideoKindToString(kind);
+ WTF_LOG(Media, "HTMLMediaElement::addVideoTrack('%s', '%s', '%s', '%s', %d)",
+ id.ascii().data(), kindString.ascii().data(), label.ascii().data(), language.ascii().data(), selected);
+ ASSERT(!id.isEmpty());
+
+ RefPtr<VideoTrack> videoTrack = VideoTrack::create(this, id, kindString, label, language);
+ videoTracks().add(videoTrack.get());
+
+ if (selected)
+ videoTrack->setSelected(true);
+}
+
+void HTMLMediaElement::removeVideoTrack(const String& id)
+{
+ WTF_LOG(Media, "HTMLMediaElement::removeVideoTrack('%s')", id.ascii().data());
+ ASSERT(!id.isEmpty());
+
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ videoTracks().remove(id);
acolwell GONE FROM CHROMIUM 2014/05/12 18:23:11 I think you either need to make remove() virtual a
+}
+
void HTMLMediaElement::mediaPlayerDidAddTextTrack(WebInbandTextTrack* webTrack)
{
// 4.8.10.12.2 Sourcing in-band text tracks
@@ -2288,11 +2452,17 @@ void HTMLMediaElement::removeTextTrack(TextTrack* track)
void HTMLMediaElement::forgetResourceSpecificTracks()
{
+ // Implements the "forget the media element's media-resource-specific tracks" algorithm.
+ // The order is explicitly specified as text, then audio, and finally video. Also
+ // 'removetrack' events should not be fired.
if (m_textTracks) {
TrackDisplayUpdateScope scope(this);
m_textTracks->removeAllInbandTracks();
closeCaptionTracksChanged();
}
+
+ m_audioTracks->removeAll();
+ m_videoTracks->removeAll();
}
PassRefPtrWillBeRawPtr<TextTrack> HTMLMediaElement::addTextTrack(const AtomicString& kind, const AtomicString& label, const AtomicString& language, ExceptionState& exceptionState)
@@ -3118,6 +3288,7 @@ void HTMLMediaElement::clearMediaPlayer(int flags)
stopPeriodicTimers();
m_loadTimer.stop();
+ m_audioTracksTimer.stop();
m_pendingActionFlags &= ~flags;
m_loadState = WaitingForSource;
@@ -3560,6 +3731,8 @@ void HTMLMediaElement::prepareMediaFragmentURI()
} else
m_fragmentEndTime = MediaPlayer::invalidTime();
+ // FIXME: Add support for selecting tracks by ID with the Media Fragments track dimension.
+
if (m_fragmentStartTime != MediaPlayer::invalidTime() && m_readyState < HAVE_FUTURE_DATA)
prepareToPlay();
}
@@ -3620,4 +3793,47 @@ void HTMLMediaElement::trace(Visitor* visitor)
HTMLElement::trace(visitor);
}
+void HTMLMediaElement::createPlaceholderTracksIfNecessary()
+{
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ // Create a placeholder audio track if |m_player| says it has audio but it didn't explicitly announce the tracks.
+ if (m_player->hasAudio() && !audioTracks().length())
+ addAudioTrack("audio", WebMediaPlayerClient::AudioTrackKindMain, "Audio Track", "", true);
+
+ // Create a placeholder video track if |m_player| says it has video but it didn't explicitly announce the tracks.
+ if (m_player->hasVideo() && !videoTracks().length())
+ addVideoTrack("video", WebMediaPlayerClient::VideoTrackKindMain, "Video Track", "", true);
+}
+
+void HTMLMediaElement::selectInitialTracksIfNecessary()
+{
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ // Enable the first audio track if an audio track hasn't been enabled yet.
+ if (audioTracks().length() > 0 && !audioTracks().hasEnabledTrack())
+ audioTracks().anonymousIndexedGetter(0)->setEnabled(true);
+
+ // Select the first video track if a video track hasn't been selected yet.
+ if (videoTracks().length() > 0 && videoTracks().selectedIndex() == -1)
+ videoTracks().anonymousIndexedGetter(0)->setSelected(true);
+}
+
+void HTMLMediaElement::audioTracksTimerFired(Timer<HTMLMediaElement>*)
+{
+ if (!webMediaPlayer())
+ return;
+
+ Vector<String> enabledTrackIDs;
+ for (unsigned i = 0; i < audioTracks().length(); ++i) {
+ RefPtr<AudioTrack> track = audioTracks().anonymousIndexedGetter(i);
+ if (track->enabled())
+ enabledTrackIDs.append(track->id());
+ }
+
+ webMediaPlayer()->enabledAudioTracksChanged(enabledTrackIDs);
+}
+
}

Powered by Google App Engine
This is Rietveld 408576698