[GStreamer] Mock GStreamer realtime sources should keep a Ref of their mock realtime media sources
https://bugs.webkit.org/show_bug.cgi?id=194326

WrappedMockRealtimeVideoSource is a subclass of RealtimeMediaSource which is refcounted, we can't
use a unique_ptr on those.

Also changed m_wrappedSource type to its actual type so it is cleaner even if needed
to upcast it to RealtimeMediaSource so some method that are made private in the mock
can still be called.

Patch by Thibault Saunier <tsaunier@igalia.com> on 2019-07-12
Reviewed by Youenn Fablet.

This fixes MediaStream tests

* platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp:
(WebCore::WrappedMockRealtimeAudioSource::create):
(WebCore::WrappedMockRealtimeAudioSource::asRealtimeMediaSource):
(WebCore::WrappedMockRealtimeAudioSource::WrappedMockRealtimeAudioSource):
(WebCore::m_wrappedSource):
(WebCore::MockGStreamerAudioCaptureSource::startProducingData):
(WebCore::MockGStreamerAudioCaptureSource::settings):
(WebCore::MockGStreamerAudioCaptureSource::capabilities):
* platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h:
* platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp:
(WebCore::WrappedMockRealtimeVideoSource::create):
(WebCore::WrappedMockRealtimeVideoSource::asRealtimeMediaSource):
(WebCore::WrappedMockRealtimeVideoSource::WrappedMockRealtimeVideoSource):
(WebCore::m_wrappedSource):
(WebCore::MockGStreamerVideoCaptureSource::settings):
(WebCore::MockGStreamerVideoCaptureSource::capabilities):
* platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@247407 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 3d225fd..15d693c 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,37 @@
+2019-07-12  Thibault Saunier  <tsaunier@igalia.com>
+
+        [GStreamer] Mock GStreamer realtime sources should keep a Ref of their mock realtime media sources
+        https://bugs.webkit.org/show_bug.cgi?id=194326
+
+        WrappedMockRealtimeVideoSource is a subclass of RealtimeMediaSource which is refcounted, we can't
+        use a unique_ptr on those.
+
+        Also changed m_wrappedSource type to its actual type so it is cleaner even if needed
+        to upcast it to RealtimeMediaSource so some method that are made private in the mock
+        can still be called.
+
+        Reviewed by Youenn Fablet.
+
+        This fixes MediaStream tests
+
+        * platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp:
+        (WebCore::WrappedMockRealtimeAudioSource::create):
+        (WebCore::WrappedMockRealtimeAudioSource::asRealtimeMediaSource):
+        (WebCore::WrappedMockRealtimeAudioSource::WrappedMockRealtimeAudioSource):
+        (WebCore::m_wrappedSource):
+        (WebCore::MockGStreamerAudioCaptureSource::startProducingData):
+        (WebCore::MockGStreamerAudioCaptureSource::settings):
+        (WebCore::MockGStreamerAudioCaptureSource::capabilities):
+        * platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h:
+        * platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp:
+        (WebCore::WrappedMockRealtimeVideoSource::create):
+        (WebCore::WrappedMockRealtimeVideoSource::asRealtimeMediaSource):
+        (WebCore::WrappedMockRealtimeVideoSource::WrappedMockRealtimeVideoSource):
+        (WebCore::m_wrappedSource):
+        (WebCore::MockGStreamerVideoCaptureSource::settings):
+        (WebCore::MockGStreamerVideoCaptureSource::capabilities):
+        * platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h:
+
 2019-07-12  Alex Christensen  <achristensen@webkit.org>
 
         Change RELEASE_ASSERT in DocumentWriter::addData to ASSERT and early return
diff --git a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp
index 0f1f739..42cb9c6 100644
--- a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp
+++ b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.cpp
@@ -41,10 +41,14 @@
 
 class WrappedMockRealtimeAudioSource : public MockRealtimeAudioSource {
 public:
-    WrappedMockRealtimeAudioSource(String&& deviceID, String&& name, String&& hashSalt)
-        : MockRealtimeAudioSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt))
-        , m_src(nullptr)
+    static Ref<WrappedMockRealtimeAudioSource> create(String&& deviceID, String&& name, String&& hashSalt)
     {
+        return adoptRef(*new WrappedMockRealtimeAudioSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)));
+    }
+
+    RealtimeMediaSource& asRealtimeMediaSource()
+    {
+        return *this;
     }
 
     void start(GRefPtr<GstElement> src)
@@ -128,6 +132,12 @@
     uint32_t m_maximiumFrameCount;
     uint64_t m_samplesEmitted { 0 };
     uint64_t m_samplesRendered { 0 };
+
+private:
+    WrappedMockRealtimeAudioSource(String&& deviceID, String&& name, String&& hashSalt)
+        : MockRealtimeAudioSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt))
+    {
+    }
 };
 
 CaptureSourceOrError MockRealtimeAudioSource::create(String&& deviceID,
@@ -154,7 +164,7 @@
 
 MockGStreamerAudioCaptureSource::MockGStreamerAudioCaptureSource(String&& deviceID, String&& name, String&& hashSalt)
     : GStreamerAudioCaptureSource(String { deviceID }, String { name }, String { hashSalt })
-    , m_wrappedSource(std::make_unique<WrappedMockRealtimeAudioSource>(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)))
+    , m_wrappedSource(WrappedMockRealtimeAudioSource::create(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)))
 {
     m_wrappedSource->addObserver(*this);
 }
@@ -174,19 +184,19 @@
 void MockGStreamerAudioCaptureSource::startProducingData()
 {
     GStreamerAudioCaptureSource::startProducingData();
-    static_cast<WrappedMockRealtimeAudioSource*>(m_wrappedSource.get())->start(capturer()->source());
+    static_cast<WrappedMockRealtimeAudioSource&>(m_wrappedSource.get()).start(capturer()->source());
 }
 
 const RealtimeMediaSourceSettings& MockGStreamerAudioCaptureSource::settings()
 {
-    return m_wrappedSource->settings();
+    return m_wrappedSource->asRealtimeMediaSource().settings();
 }
 
 const RealtimeMediaSourceCapabilities& MockGStreamerAudioCaptureSource::capabilities()
 {
-    m_capabilities = m_wrappedSource->capabilities();
-    m_currentSettings = m_wrappedSource->settings();
-    return m_wrappedSource->capabilities();
+    m_capabilities = m_wrappedSource->asRealtimeMediaSource().capabilities();
+    m_currentSettings = m_wrappedSource->asRealtimeMediaSource().settings();
+    return m_wrappedSource->asRealtimeMediaSource().capabilities();
 }
 
 void MockGStreamerAudioCaptureSource::captureFailed()
diff --git a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h
index bd999f9..e3025c2 100644
--- a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h
+++ b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerAudioCaptureSource.h
@@ -27,6 +27,7 @@
 
 namespace WebCore {
 
+class WrappedMockRealtimeAudioSource;
 class MockGStreamerAudioCaptureSource final : public GStreamerAudioCaptureSource, RealtimeMediaSource::Observer {
 public:
     MockGStreamerAudioCaptureSource(String&& deviceID, String&& name, String&& hashSalt);
@@ -39,11 +40,10 @@
     void startProducingData() final;
     const RealtimeMediaSourceSettings& settings() final;
     const RealtimeMediaSourceCapabilities& capabilities() final;
+    void captureFailed() final;
+    void videoSampleAvailable(MediaSample&) final { };
 
-    void captureFailed();
-    std::unique_ptr<RealtimeMediaSource> m_wrappedSource;
-
-    void videoSampleAvailable(MediaSample&) override { };
+    Ref<WrappedMockRealtimeAudioSource> m_wrappedSource;
 };
 
 } // namespace WebCore
diff --git a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp
index 2c72f5b..a48fcc8 100644
--- a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp
+++ b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.cpp
@@ -33,9 +33,14 @@
 
 class WrappedMockRealtimeVideoSource : public MockRealtimeVideoSource {
 public:
-    WrappedMockRealtimeVideoSource(String&& deviceID, String&& name, String&& hashSalt)
-        : MockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt))
+    static Ref<WrappedMockRealtimeVideoSource> create(String&& deviceID, String&& name, String&& hashSalt)
     {
+        return adoptRef(*new WrappedMockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)));
+    }
+
+    RealtimeMediaSource& asRealtimeMediaSource()
+    {
+        return *this;
     }
 
     void updateSampleBuffer()
@@ -59,6 +64,12 @@
 
         videoSampleAvailable(MediaSampleGStreamer::create(WTFMove(gstSample), FloatSize(), String()));
     }
+
+private:
+    WrappedMockRealtimeVideoSource(String&& deviceID, String&& name, String&& hashSalt)
+        : MockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt))
+    {
+    }
 };
 
 CaptureSourceOrError MockRealtimeVideoSource::create(String&& deviceID,
@@ -97,7 +108,7 @@
 
 MockGStreamerVideoCaptureSource::MockGStreamerVideoCaptureSource(String&& deviceID, String&& name, String&& hashSalt)
     : GStreamerVideoCaptureSource(String { deviceID }, String { name }, String { hashSalt }, "appsrc")
-    , m_wrappedSource(std::make_unique<WrappedMockRealtimeVideoSource>(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)))
+    , m_wrappedSource(WrappedMockRealtimeVideoSource::create(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)))
 {
     m_wrappedSource->addObserver(*this);
 }
@@ -120,13 +131,13 @@
 
 const RealtimeMediaSourceSettings& MockGStreamerVideoCaptureSource::settings()
 {
-    return m_wrappedSource->settings();
+    return m_wrappedSource->asRealtimeMediaSource().settings();
 }
 
 const RealtimeMediaSourceCapabilities& MockGStreamerVideoCaptureSource::capabilities()
 {
-    m_capabilities = m_wrappedSource->capabilities();
-    m_currentSettings = m_wrappedSource->settings();
+    m_capabilities = m_wrappedSource->asRealtimeMediaSource().capabilities();
+    m_currentSettings = m_wrappedSource->asRealtimeMediaSource().settings();
     return m_capabilities.value();
 }
 
diff --git a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h
index 897a31c..cb18445 100644
--- a/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h
+++ b/Source/WebCore/platform/mediastream/gstreamer/MockGStreamerVideoCaptureSource.h
@@ -43,11 +43,11 @@
     void stopProducingData() final;
     void startProducingData() final;
     const RealtimeMediaSourceSettings& settings() final;
-    std::unique_ptr<RealtimeMediaSource> m_wrappedSource;
     const RealtimeMediaSourceCapabilities& capabilities() final;
-    void captureFailed() override;
+    void captureFailed() final;
+    void videoSampleAvailable(MediaSample&) final;
 
-    void videoSampleAvailable(MediaSample&) override;
+    Ref<WrappedMockRealtimeVideoSource> m_wrappedSource;
 };
 
 } // namespace WebCore