Improve use of NeverDestroyed
[WebKit-https.git] / Source / WebCore / platform / mock / mediasource / MockMediaPlayerMediaSource.cpp
index 2f23048..a5468b3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
 #if ENABLE(MEDIA_SOURCE)
 
-#include "ExceptionCodePlaceholder.h"
-#include "HTMLMediaSource.h"
 #include "MediaPlayer.h"
+#include "MediaSourcePrivateClient.h"
 #include "MockMediaSourcePrivate.h"
-#include <wtf/Functional.h>
 #include <wtf/MainThread.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
@@ -41,28 +40,21 @@ namespace WebCore {
 // MediaPlayer Enigne Support
 void MockMediaPlayerMediaSource::registerMediaEngine(MediaEngineRegistrar registrar)
 {
-    registrar(create, getSupportedTypes, supportsType, 0, 0, 0);
+    registrar([] (MediaPlayer* player) { return std::make_unique<MockMediaPlayerMediaSource>(player); }, getSupportedTypes,
+        supportsType, 0, 0, 0, 0);
 }
 
-PassOwnPtr<MediaPlayerPrivateInterface> MockMediaPlayerMediaSource::create(MediaPlayer* player)
+// FIXME: What does the word "cache" mean here?
+static const HashSet<String, ASCIICaseInsensitiveHash>& mimeTypeCache()
 {
-    return adoptPtr(new MockMediaPlayerMediaSource(player));
-}
-
-static HashSet<String> mimeTypeCache()
-{
-    DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
-    static bool isInitialized = false;
-
-    if (!isInitialized) {
-        isInitialized = true;
-        cache.add(ASCIILiteral("video/mock"));
-    }
-
+    static const auto cache = makeNeverDestroyed(HashSet<String, ASCIICaseInsensitiveHash> {
+        "video/mock",
+        "audio/mock",
+    });
     return cache;
 }
 
-void MockMediaPlayerMediaSource::getSupportedTypes(HashSet<String>& supportedTypes)
+void MockMediaPlayerMediaSource::getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& supportedTypes)
 {
     supportedTypes = mimeTypeCache();
 }
@@ -72,22 +64,24 @@ MediaPlayer::SupportsType MockMediaPlayerMediaSource::supportsType(const MediaEn
     if (!parameters.isMediaSource)
         return MediaPlayer::IsNotSupported;
 
-    if (!mimeTypeCache().contains(parameters.type))
+    auto containerType = parameters.type.containerType();
+    if (containerType.isEmpty() || !mimeTypeCache().contains(containerType))
         return MediaPlayer::IsNotSupported;
 
-    if (parameters.codecs.isEmpty())
+    auto codecs = parameters.type.parameter(ContentType::codecsParameter());
+    if (codecs.isEmpty())
         return MediaPlayer::MayBeSupported;
 
-    return parameters.codecs == "mock" ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
+    return codecs == "mock" ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
 }
 
 MockMediaPlayerMediaSource::MockMediaPlayerMediaSource(MediaPlayer* player)
     : m_player(player)
     , m_currentTime(MediaTime::zeroTime())
-    , m_duration(0)
     , m_readyState(MediaPlayer::HaveNothing)
     , m_networkState(MediaPlayer::Empty)
     , m_playing(false)
+    , m_seekCompleted(true)
 {
 }
 
@@ -100,11 +94,9 @@ void MockMediaPlayerMediaSource::load(const String&)
     ASSERT_NOT_REACHED();
 }
 
-void MockMediaPlayerMediaSource::load(const String&, PassRefPtr<HTMLMediaSource> source)
+void MockMediaPlayerMediaSource::load(const String&, MediaSourcePrivateClient* source)
 {
-    m_mediaSource = source;
-    m_mediaSourcePrivate = MockMediaSourcePrivate::create(this);
-    m_mediaSource->setPrivateAndOpen(*m_mediaSourcePrivate);
+    m_mediaSourcePrivate = MockMediaSourcePrivate::create(this, source);
 }
 
 void MockMediaPlayerMediaSource::cancelLoad()
@@ -114,7 +106,9 @@ void MockMediaPlayerMediaSource::cancelLoad()
 void MockMediaPlayerMediaSource::play()
 {
     m_playing = 1;
-    callOnMainThread(bind(&MockMediaPlayerMediaSource::advanceCurrentTime, this));
+    callOnMainThread([this] {
+        advanceCurrentTime();
+    });
 }
 
 void MockMediaPlayerMediaSource::pause()
@@ -122,9 +116,9 @@ void MockMediaPlayerMediaSource::pause()
     m_playing = 0;
 }
 
-IntSize MockMediaPlayerMediaSource::naturalSize() const
+FloatSize MockMediaPlayerMediaSource::naturalSize() const
 {
-    return IntSize();
+    return FloatSize();
 }
 
 bool MockMediaPlayerMediaSource::hasVideo() const
@@ -143,7 +137,7 @@ void MockMediaPlayerMediaSource::setVisible(bool)
 
 bool MockMediaPlayerMediaSource::seeking() const
 {
-    return false;
+    return !m_seekCompleted;
 }
 
 bool MockMediaPlayerMediaSource::paused() const
@@ -161,14 +155,17 @@ MediaPlayer::ReadyState MockMediaPlayerMediaSource::readyState() const
     return m_readyState;
 }
 
-double MockMediaPlayerMediaSource::maxTimeSeekableDouble() const
+MediaTime MockMediaPlayerMediaSource::maxMediaTimeSeekable() const
 {
     return m_duration;
 }
 
-PassRefPtr<TimeRanges> MockMediaPlayerMediaSource::buffered() const
+std::unique_ptr<PlatformTimeRanges> MockMediaPlayerMediaSource::buffered() const
 {
-    return m_mediaSource ? m_mediaSource->buffered() : TimeRanges::create();
+    if (m_mediaSourcePrivate)
+        return m_mediaSourcePrivate->buffered();
+
+    return std::make_unique<PlatformTimeRanges>();
 }
 
 bool MockMediaPlayerMediaSource::didLoadingProgress() const
@@ -180,45 +177,54 @@ void MockMediaPlayerMediaSource::setSize(const IntSize&)
 {
 }
 
-void MockMediaPlayerMediaSource::paint(GraphicsContext*, const IntRect&)
+void MockMediaPlayerMediaSource::paint(GraphicsContext&, const FloatRect&)
 {
 }
 
-double MockMediaPlayerMediaSource::currentTimeDouble() const
+MediaTime MockMediaPlayerMediaSource::currentMediaTime() const
 {
-    return m_currentTime.toDouble();
+    return m_currentTime;
 }
 
-double MockMediaPlayerMediaSource::durationDouble() const
+MediaTime MockMediaPlayerMediaSource::durationMediaTime() const
 {
-    return m_duration;
+    return m_mediaSourcePrivate ? m_mediaSourcePrivate->duration() : MediaTime::zeroTime();
 }
 
-void MockMediaPlayerMediaSource::seekWithTolerance(double time, double negativeTolerance, double positiveTolerance)
+void MockMediaPlayerMediaSource::seekWithTolerance(const MediaTime& time, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance)
 {
     if (!negativeTolerance && !positiveTolerance) {
-        m_currentTime = MediaTime::createWithDouble(time);
-        m_mediaSourcePrivate->seekToTime(MediaTime::createWithDouble(time));
+        m_currentTime = time;
+        m_mediaSourcePrivate->seekToTime(time);
     } else
-        m_currentTime = m_mediaSourcePrivate->seekToTime(MediaTime::createWithDouble(time), MediaTime::createWithDouble(negativeTolerance), MediaTime::createWithDouble(positiveTolerance));
-    m_player->timeChanged();
+        m_currentTime = m_mediaSourcePrivate->seekToTime(time, negativeTolerance, positiveTolerance);
 
-    if (m_playing)
-        callOnMainThread(bind(&MockMediaPlayerMediaSource::advanceCurrentTime, this));
+    if (m_seekCompleted) {
+        m_player->timeChanged();
+
+        if (m_playing)
+            callOnMainThread([this] {
+                advanceCurrentTime();
+            });
+    }
 }
 
 void MockMediaPlayerMediaSource::advanceCurrentTime()
 {
-    RefPtr<TimeRanges> buffered = this->buffered();
-    size_t pos = buffered->find(m_currentTime.toDouble());
+    if (!m_mediaSourcePrivate)
+        return;
+
+    auto buffered = m_mediaSourcePrivate->buffered();
+    size_t pos = buffered->find(m_currentTime);
     if (pos == notFound)
         return;
 
-    m_currentTime = MediaTime::createWithDouble(std::min(m_duration, buffered->end(pos, IGNORE_EXCEPTION)));
+    bool ignoreError;
+    m_currentTime = std::min(m_duration, buffered->end(pos, ignoreError));
     m_player->timeChanged();
 }
 
-void MockMediaPlayerMediaSource::updateDuration(double duration)
+void MockMediaPlayerMediaSource::updateDuration(const MediaTime& duration)
 {
     if (m_duration == duration)
         return;
@@ -245,6 +251,45 @@ void MockMediaPlayerMediaSource::setNetworkState(MediaPlayer::NetworkState netwo
     m_player->networkStateChanged();
 }
 
+void MockMediaPlayerMediaSource::waitForSeekCompleted()
+{
+    m_seekCompleted = false;
+}
+
+void MockMediaPlayerMediaSource::seekCompleted()
+{
+    if (m_seekCompleted)
+        return;
+    m_seekCompleted = true;
+
+    m_player->timeChanged();
+
+    if (m_playing)
+        callOnMainThread([this] {
+            advanceCurrentTime();
+        });
+}
+
+unsigned long MockMediaPlayerMediaSource::totalVideoFrames()
+{
+    return m_mediaSourcePrivate ? m_mediaSourcePrivate->totalVideoFrames() : 0;
+}
+
+unsigned long MockMediaPlayerMediaSource::droppedVideoFrames()
+{
+    return m_mediaSourcePrivate ? m_mediaSourcePrivate->droppedVideoFrames() : 0;
+}
+
+unsigned long MockMediaPlayerMediaSource::corruptedVideoFrames()
+{
+    return m_mediaSourcePrivate ? m_mediaSourcePrivate->corruptedVideoFrames() : 0;
+}
+
+MediaTime MockMediaPlayerMediaSource::totalFrameDelay()
+{
+    return m_mediaSourcePrivate ? m_mediaSourcePrivate->totalFrameDelay() : MediaTime::zeroTime();
+}
+
 }
 
 #endif