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

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

Issue 170233009: Initial implementation of AudioTrack, AudioTrackList, VideoTrack, and VideoTrackList. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@blink-master
Patch Set: Rebased, reworked impl, and addressed comments. Created 6 years, 9 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 026d69d245791efb86d1062b9b882bd5d27e650f..5af4ccca10cf0673551362bb5796ed93fcfe8426 100644
--- a/Source/core/html/HTMLMediaElement.cpp
+++ b/Source/core/html/HTMLMediaElement.cpp
@@ -52,9 +52,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::WebMimeRegistry;
+using blink::WebMediaPlayerClient;
namespace WebCore {
@@ -163,6 +168,66 @@ private:
HTMLMediaElement* m_mediaElement;
};
+static AtomicString VideoKindToString(WebMediaPlayerClient::VideoTrackKind kind)
Inactive 2014/03/08 03:05:59 Looks like this could return a const AtomicString&
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+{
+ switch (kind) {
+ case WebMediaPlayerClient::VideoTrackKindNone:
+ return AtomicString();
Inactive 2014/03/08 03:05:59 nullAtom? (same below)
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+ break;
Inactive 2014/03/08 03:05:59 Why the breaks after the return statements? (Sorry
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Removed. I think an earlier version of this functi
+ case WebMediaPlayerClient::VideoTrackKindAlternative:
+ return VideoTrack::alternativeKeyword();
+ break;
+ case WebMediaPlayerClient::VideoTrackKindCaptions:
+ return VideoTrack::captionsKeyword();
+ break;
+ case WebMediaPlayerClient::VideoTrackKindMain:
+ return VideoTrack::mainKeyword();
+ break;
+ case WebMediaPlayerClient::VideoTrackKindSign:
+ return VideoTrack::signKeyword();
+ break;
+ case WebMediaPlayerClient::VideoTrackKindSubtitles:
+ return VideoTrack::subtitlesKeyword();
+ break;
+ case WebMediaPlayerClient::VideoTrackKindCommentary:
+ return VideoTrack::commentaryKeyword();
+ break;
+ }
+
+ ASSERT_NOT_REACHED();
+ return AtomicString();
+}
+
+static AtomicString AudioKindToString(WebMediaPlayerClient::AudioTrackKind kind)
Inactive 2014/03/08 03:05:59 Looks like this could return a const AtomicString&
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+{
+ switch (kind) {
+ case WebMediaPlayerClient::AudioTrackKindNone:
+ return AtomicString();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindAlternative:
+ return AudioTrack::alternativeKeyword();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindDescriptions:
+ return AudioTrack::descriptionsKeyword();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindMain:
+ return AudioTrack::mainKeyword();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindMainDescriptions:
+ return AudioTrack::mainDescriptionsKeyword();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindTranslation:
+ return AudioTrack::translationKeyword();
+ break;
+ case WebMediaPlayerClient::AudioTrackKindCommentary:
+ return AudioTrack::commentaryKeyword();
+ break;
+ }
+
+ ASSERT_NOT_REACHED();
+ return AtomicString();
+}
+
static bool canLoadURL(const KURL& url, const ContentType& contentType, const String& keySystem)
{
DEFINE_STATIC_LOCAL(const String, codecs, ("codecs"));
@@ -277,6 +342,8 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum
, m_haveVisibleTextTrack(false)
, m_processingPreferenceChange(false)
, m_lastTextTrackUpdateTime(-1)
+ , m_audioTracks(nullptr)
Inactive 2014/03/08 03:05:59 Those are RefPtrs, not raw pointers. No need to ex
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+ , m_videoTracks(nullptr)
, m_textTracks(nullptr)
, m_ignoreTrackDisplayUpdate(0)
#if ENABLE(WEB_AUDIO)
@@ -309,6 +376,12 @@ HTMLMediaElement::~HTMLMediaElement()
if (m_textTracks)
m_textTracks->clearOwner();
+ if (m_audioTracks)
+ m_audioTracks->shutdown();
+
+ if (m_videoTracks)
+ m_videoTracks->shutdown();
+
if (m_mediaController) {
m_mediaController->removeMediaElement(this);
m_mediaController = nullptr;
@@ -368,6 +441,15 @@ void HTMLMediaElement::didMoveToNewDocument(Document& oldDocument)
oldDocument.decrementLoadEventDelayCount();
ActiveDOMObject::didMoveToNewExecutionContext(&document());
+
+ // Notify track lists so they don't stop firing events when moved to a new document
+ // and the original document gets destroyed.
+ if (m_audioTracks)
+ m_audioTracks->didMoveToNewExecutionContext(&document());
+
+ if (m_videoTracks)
+ m_videoTracks->didMoveToNewExecutionContext(&document());
+
HTMLElement::didMoveToNewDocument(oldDocument);
}
@@ -1580,7 +1662,12 @@ void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
}
if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
+ createPlaceholderTracksIfNecessary();
+
prepareMediaFragmentURI();
+
+ selectInitialTracksIfNecessary();
+
scheduleEvent(EventTypeNames::durationchange);
if (isVideo())
scheduleEvent(EventTypeNames::resize);
@@ -2201,6 +2288,116 @@ bool HTMLMediaElement::canPlay() const
return paused() || ended() || m_readyState < HAVE_METADATA;
}
+AudioTrackList* HTMLMediaElement::audioTracks()
Inactive 2014/03/08 03:05:59 Could return a reference as it never returns null.
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+{
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+
+ if (!m_audioTracks)
+ m_audioTracks = AudioTrackList::create(this);
+
+ return m_audioTracks.get();
+}
+
+void HTMLMediaElement::audioTrackChanged(const AtomicString& audioTrackID, bool enabled)
+{
+ WTF_LOG(Media, "HTMLMediaElement::audioTrackChanged('%s', %d)", audioTrackID.ascii().data(), enabled);
+ 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 (webMediaPlayer())
+ webMediaPlayer()->enabledAudioTrackChange(audioTrackID, enabled);
+}
+
+VideoTrackList* HTMLMediaElement::videoTracks()
Inactive 2014/03/08 03:05:59 Could return a reference as it never returns null.
acolwell GONE FROM CHROMIUM 2014/03/08 19:56:42 Done.
+{
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+
+ if (!m_videoTracks)
+ m_videoTracks = VideoTrackList::create(this);
+
+ return m_videoTracks.get();
+}
+
+void HTMLMediaElement::selectedVideoTrackChanged(const AtomicString& selectedTrackID)
+{
+ WTF_LOG(Media, "HTMLMediaElement::selectedVideoTrackChanged('%s')", selectedTrackID.ascii().data());
+ ASSERT(RuntimeEnabledFeatures::audioVideoTracksEnabled());
+
+ AtomicString unselectedTrackID;
+
+ int oldSelectedIndex = videoTracks()->selectedIndex();
+ if (oldSelectedIndex != -1)
+ unselectedTrackID = videoTracks()->anonymousIndexedGetter(oldSelectedIndex)->id();
+
+ ASSERT(unselectedTrackID != selectedTrackID);
+
+ videoTracks()->trackSelected(selectedTrackID);
+
+ // FIXME: Add call on m_mediaSource to notify it of track changes once the SourceBuffer.videoTracks attribute is added.
+
+ ASSERT(!unselectedTrackID.isEmpty() || !selectedTrackID.isEmpty());
+
+ if (webMediaPlayer())
+ webMediaPlayer()->selectedVideoTrackChange(unselectedTrackID, selectedTrackID);
+}
+
+void HTMLMediaElement::addVideoTrack(const AtomicString& 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 AtomicString& id)
+{
+ WTF_LOG(Media, "HTMLMediaElement::removeVideoTrack('%s')", id.ascii().data());
+ ASSERT(!id.isEmpty());
+
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ videoTracks()->remove(id);
+}
+
+void HTMLMediaElement::addAudioTrack(const AtomicString& 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 AtomicString& id)
+{
+ WTF_LOG(Media, "HTMLMediaElement::removeAudioTrack('%s')", id.ascii().data());
+ ASSERT(!id.isEmpty());
+
+ if (!RuntimeEnabledFeatures::audioVideoTracksEnabled())
+ return;
+
+ audioTracks()->remove(id);
+}
+
void HTMLMediaElement::mediaPlayerDidAddTextTrack(WebInbandTextTrack* webTrack)
{
if (!RuntimeEnabledFeatures::videoTrackEnabled())
@@ -2276,11 +2473,20 @@ 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();
}
+
+ if (m_audioTracks)
+ m_audioTracks->removeAll();
+
+ if (m_videoTracks)
+ m_videoTracks->removeAll();
}
PassRefPtr<TextTrack> HTMLMediaElement::addTextTrack(const AtomicString& kind, const AtomicString& label, const AtomicString& language, ExceptionState& exceptionState)
@@ -3589,6 +3795,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();
}
@@ -3647,4 +3855,32 @@ bool HTMLMediaElement::isInteractiveContent() const
return fastHasAttribute(controlsAttr);
}
+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);
+}
+
}

Powered by Google App Engine
This is Rietveld 408576698