<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[211177] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/211177">211177</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2017-01-25 16:31:51 -0800 (Wed, 25 Jan 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[WebRTC] Add support for libwebrtc video incoming and outgoing video tracks
https://bugs.webkit.org/show_bug.cgi?id=167432

Patch by Youenn Fablet &lt;youenn@apple.com&gt; on 2017-01-25
Reviewed by Eric Carlson.

Not covered by tests right now, this will be covered by layout/mock tests.
RealtimeIncomingVideoSource consumes video frames given by libwebrtc.
RealtimeOutgoingVideoSource consumes video frames from WebCore tracks (capture typically) and feeds libwebrtc with them.

* platform/mediastream/mac/RealtimeIncomingVideoSource.cpp: Added.
(WebCore::RealtimeIncomingVideoSource::create):
(WebCore::RealtimeIncomingVideoSource::RealtimeIncomingVideoSource):
(WebCore::RealtimeIncomingVideoSource::startProducingData):
(WebCore::RealtimeIncomingVideoSource::stopProducingData):
(WebCore::RealtimeIncomingVideoSource::OnFrame):
(WebCore::RealtimeIncomingVideoSource::processNewSample):
(WebCore::drawImage):
(WebCore::RealtimeIncomingVideoSource::currentFrameImage):
(WebCore::RealtimeIncomingVideoSource::paintCurrentFrameInContext):
(WebCore::RealtimeIncomingVideoSource::capabilities):
(WebCore::RealtimeIncomingVideoSource::settings):
(WebCore::RealtimeIncomingVideoSource::supportedConstraints):
* platform/mediastream/mac/RealtimeIncomingVideoSource.h: Added.
* platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp: Added.
(WebCore::RealtimeOutgoingVideoSource::RealtimeOutgoingVideoSource):
(WebCore::RealtimeOutgoingVideoSource::GetStats):
(WebCore::RealtimeOutgoingVideoSource::AddOrUpdateSink):
(WebCore::RealtimeOutgoingVideoSource::RemoveSink):
(WebCore::RealtimeOutgoingVideoSource::sourceHasMoreMediaData):
* platform/mediastream/mac/RealtimeOutgoingVideoSource.h: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeIncomingVideoSourcecpp">trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeIncomingVideoSourceh">trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.h</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingVideoSourcecpp">trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingVideoSourceh">trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (211176 => 211177)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-01-26 00:24:19 UTC (rev 211176)
+++ trunk/Source/WebCore/ChangeLog        2017-01-26 00:31:51 UTC (rev 211177)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2017-01-25  Youenn Fablet  &lt;youenn@apple.com&gt;
+
+        [WebRTC] Add support for libwebrtc video incoming and outgoing video tracks
+        https://bugs.webkit.org/show_bug.cgi?id=167432
+
+        Reviewed by Eric Carlson.
+
+        Not covered by tests right now, this will be covered by layout/mock tests.
+        RealtimeIncomingVideoSource consumes video frames given by libwebrtc.
+        RealtimeOutgoingVideoSource consumes video frames from WebCore tracks (capture typically) and feeds libwebrtc with them.
+
+        * platform/mediastream/mac/RealtimeIncomingVideoSource.cpp: Added.
+        (WebCore::RealtimeIncomingVideoSource::create):
+        (WebCore::RealtimeIncomingVideoSource::RealtimeIncomingVideoSource):
+        (WebCore::RealtimeIncomingVideoSource::startProducingData):
+        (WebCore::RealtimeIncomingVideoSource::stopProducingData):
+        (WebCore::RealtimeIncomingVideoSource::OnFrame):
+        (WebCore::RealtimeIncomingVideoSource::processNewSample):
+        (WebCore::drawImage):
+        (WebCore::RealtimeIncomingVideoSource::currentFrameImage):
+        (WebCore::RealtimeIncomingVideoSource::paintCurrentFrameInContext):
+        (WebCore::RealtimeIncomingVideoSource::capabilities):
+        (WebCore::RealtimeIncomingVideoSource::settings):
+        (WebCore::RealtimeIncomingVideoSource::supportedConstraints):
+        * platform/mediastream/mac/RealtimeIncomingVideoSource.h: Added.
+        * platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp: Added.
+        (WebCore::RealtimeOutgoingVideoSource::RealtimeOutgoingVideoSource):
+        (WebCore::RealtimeOutgoingVideoSource::GetStats):
+        (WebCore::RealtimeOutgoingVideoSource::AddOrUpdateSink):
+        (WebCore::RealtimeOutgoingVideoSource::RemoveSink):
+        (WebCore::RealtimeOutgoingVideoSource::sourceHasMoreMediaData):
+        * platform/mediastream/mac/RealtimeOutgoingVideoSource.h: Added.
+
</ins><span class="cx"> 2017-01-25  Wenson Hsieh  &lt;wenson_hsieh@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Introduce an item-provider-based pasteboard wrapper
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeIncomingVideoSourcecpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.cpp (0 => 211177)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.cpp                                (rev 0)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.cpp        2017-01-26 00:31:51 UTC (rev 211177)
</span><span class="lines">@@ -0,0 +1,216 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of Google Inc. nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * &quot;AS IS&quot; 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 THE COPYRIGHT
+ * OWNER 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// FIXME: Do clean-up in the includes
+
+#include &quot;config.h&quot;
+#include &quot;RealtimeIncomingVideoSource.h&quot;
+
+#if USE(LIBWEBRTC)
+
+#include &quot;GraphicsContext.h&quot;
+#include &quot;ImageBuffer.h&quot;
+#include &quot;IntRect.h&quot;
+#include &quot;MediaSampleAVFObjC.h&quot;
+#include &lt;webrtc/common_video/include/corevideo_frame_buffer.h&gt;
+#include &lt;webrtc/common_video/libyuv/include/webrtc_libyuv.h&gt;
+#include &lt;wtf/MainThread.h&gt;
+
+#include &quot;CoreMediaSoftLink.h&quot;
+#include &quot;CoreVideoSoftLink.h&quot;
+
+namespace WebCore {
+
+Ref&lt;RealtimeIncomingVideoSource&gt; RealtimeIncomingVideoSource::create(rtc::scoped_refptr&lt;webrtc::VideoTrackInterface&gt;&amp;&amp; videoTrack, String&amp;&amp; trackId)
+{
+    const OSType imageFormat = kCVPixelFormatType_32BGRA;
+    RetainPtr&lt;CFNumberRef&gt; imageFormatNumber = adoptCF(CFNumberCreate(nullptr,  kCFNumberIntType,  &amp;imageFormat));
+
+    RetainPtr&lt;CFMutableDictionaryRef&gt; conformerOptions = adoptCF(CFDictionaryCreateMutable(0, 1, &amp;kCFTypeDictionaryKeyCallBacks, &amp;kCFTypeDictionaryValueCallBacks));
+    CFDictionarySetValue(conformerOptions.get(), kCVPixelBufferPixelFormatTypeKey, imageFormatNumber.get());
+    PixelBufferConformerCV conformer(conformerOptions.get());
+
+    auto source = adoptRef(*new RealtimeIncomingVideoSource(WTFMove(videoTrack), WTFMove(trackId), conformerOptions.get()));
+    source-&gt;startProducingData();
+    return source;
+}
+
+RealtimeIncomingVideoSource::RealtimeIncomingVideoSource(rtc::scoped_refptr&lt;webrtc::VideoTrackInterface&gt;&amp;&amp; videoTrack, String&amp;&amp; videoTrackId, CFMutableDictionaryRef conformerOptions)
+    : RealtimeMediaSource(WTFMove(videoTrackId), RealtimeMediaSource::Type::Video, String())
+    , m_videoTrack(WTFMove(videoTrack))
+    , m_conformer(conformerOptions)
+{
+    m_muted = !m_videoTrack;
+    m_currentSettings.setWidth(640);
+    m_currentSettings.setHeight(480);
+}
+
+void RealtimeIncomingVideoSource::startProducingData()
+{
+    if (m_isProducingData)
+        return;
+
+    m_isProducingData = true;
+    if (m_videoTrack)
+        m_videoTrack-&gt;AddOrUpdateSink(this, rtc::VideoSinkWants());
+}
+
+void RealtimeIncomingVideoSource::stopProducingData()
+{
+    if (!m_isProducingData)
+        return;
+
+    m_isProducingData = false;
+    if (m_videoTrack)
+        m_videoTrack-&gt;RemoveSink(this);
+}
+
+void RealtimeIncomingVideoSource::OnFrame(const webrtc::VideoFrame&amp; frame)
+{
+    if (!m_isProducingData)
+        return;
+
+    auto buffer = frame.video_frame_buffer();
+    CVPixelBufferRef pixelBuffer = static_cast&lt;CVPixelBufferRef&gt;(buffer-&gt;native_handle());
+
+    // FIXME: Convert timing information from VideoFrame to CMSampleTimingInfo.
+    // For the moment, we will pretend that frames should be rendered asap.
+    CMSampleTimingInfo timingInfo;
+    timingInfo.presentationTimeStamp = kCMTimeInvalid;
+    timingInfo.decodeTimeStamp = kCMTimeInvalid;
+    timingInfo.duration = kCMTimeInvalid;
+
+    CMVideoFormatDescriptionRef formatDescription;
+    OSStatus ostatus = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, &amp;formatDescription);
+    if (ostatus != noErr) {
+        LOG_ERROR(&quot;Failed to initialize CMVideoFormatDescription: %d&quot;, ostatus);
+        return;
+    }
+
+    CMSampleBufferRef sampleBuffer;
+    ostatus = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, formatDescription, &amp;timingInfo, &amp;sampleBuffer);
+    if (ostatus != noErr) {
+        LOG_ERROR(&quot;Failed to create the sample buffer: %d&quot;, ostatus);
+        return;
+    }
+    CFRelease(formatDescription);
+
+    RetainPtr&lt;CMSampleBufferRef&gt; sample = sampleBuffer;
+
+    CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true);
+    for (CFIndex i = 0; i &lt; CFArrayGetCount(attachmentsArray); ++i) {
+        CFMutableDictionaryRef attachments = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachmentsArray, i);
+        CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
+    }
+
+    unsigned width = frame.width();
+    unsigned height = frame.height();
+    RefPtr&lt;RealtimeIncomingVideoSource&gt; protectedThis(this);
+    callOnMainThread([protectedThis = WTFMove(protectedThis), sample = WTFMove(sample), width, height] {
+        protectedThis-&gt;processNewSample(sample.get(), width, height);
+    });
+}
+
+void RealtimeIncomingVideoSource::processNewSample(CMSampleBufferRef sample, unsigned width, unsigned height)
+{
+    m_buffer = sample;
+    if (width != m_currentSettings.width() || height != m_currentSettings.height()) {
+        m_currentSettings.setWidth(width);
+        m_currentSettings.setHeight(height);
+        settingsDidChange();
+    }
+
+    mediaDataUpdated(MediaSampleAVFObjC::create(sample));
+}
+
+static inline void drawImage(ImageBuffer&amp; imageBuffer, CGImageRef image, const FloatRect&amp; rect)
+{
+    auto&amp; context = imageBuffer.context();
+    GraphicsContextStateSaver stateSaver(context);
+    context.translate(rect.x(), rect.y() + rect.height());
+    context.scale(FloatSize(1, -1));
+    context.setImageInterpolationQuality(InterpolationLow);
+    IntRect paintRect(IntPoint(0, 0), IntSize(rect.width(), rect.height()));
+    CGContextDrawImage(context.platformContext(), CGRectMake(0, 0, paintRect.width(), paintRect.height()), image);
+}
+
+RefPtr&lt;Image&gt; RealtimeIncomingVideoSource::currentFrameImage()
+{
+    if (!m_buffer)
+        return nullptr;
+
+    FloatRect rect(0, 0, m_currentSettings.width(), m_currentSettings.height());
+    auto imageBuffer = ImageBuffer::create(rect.size(), Unaccelerated);
+
+    auto pixelBuffer = static_cast&lt;CVPixelBufferRef&gt;(CMSampleBufferGetImageBuffer(m_buffer.get()));
+    drawImage(*imageBuffer, m_conformer.createImageFromPixelBuffer(pixelBuffer).get(), rect);
+
+    return ImageBuffer::sinkIntoImage(WTFMove(imageBuffer));
+}
+
+void RealtimeIncomingVideoSource::paintCurrentFrameInContext(GraphicsContext&amp; context, const FloatRect&amp; rect)
+{
+    if (context.paintingDisabled())
+        return;
+
+    if (!m_buffer)
+        return;
+
+    // FIXME: Can we optimize here the painting?
+    FloatRect fullRect(0, 0, m_currentSettings.width(), m_currentSettings.height());
+    auto imageBuffer = ImageBuffer::create(fullRect.size(), Unaccelerated);
+
+    auto pixelBuffer = static_cast&lt;CVPixelBufferRef&gt;(CMSampleBufferGetImageBuffer(m_buffer.get()));
+    drawImage(*imageBuffer, m_conformer.createImageFromPixelBuffer(pixelBuffer).get(), fullRect);
+
+    GraphicsContextStateSaver stateSaver(context);
+    context.setImageInterpolationQuality(InterpolationLow);
+    IntRect paintRect(IntPoint(0, 0), IntSize(rect.width(), rect.height()));
+    context.drawImage(*imageBuffer-&gt;copyImage(DontCopyBackingStore), rect);
+}
+
+RefPtr&lt;RealtimeMediaSourceCapabilities&gt; RealtimeIncomingVideoSource::capabilities() const
+{
+    return m_capabilities;
+}
+
+const RealtimeMediaSourceSettings&amp; RealtimeIncomingVideoSource::settings() const
+{
+    return m_currentSettings;
+}
+
+RealtimeMediaSourceSupportedConstraints&amp; RealtimeIncomingVideoSource::supportedConstraints()
+{
+    return m_supportedConstraints;
+}
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeIncomingVideoSourceh"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.h (0 => 211177)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.h                                (rev 0)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.h        2017-01-26 00:31:51 UTC (rev 211177)
</span><span class="lines">@@ -0,0 +1,89 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of Ericsson nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * &quot;AS IS&quot; 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 THE COPYRIGHT
+ * OWNER 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if USE(LIBWEBRTC)
+
+#include &quot;LibWebRTCMacros.h&quot;
+#include &quot;PixelBufferConformerCV.h&quot;
+#include &quot;RealtimeMediaSource.h&quot;
+#include &lt;webrtc/api/mediastreaminterface.h&gt;
+#include &lt;wtf/RetainPtr.h&gt;
+
+typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
+
+namespace WebCore {
+
+class CaptureDevice;
+
+class RealtimeIncomingVideoSource final : public RealtimeMediaSource, private rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt; {
+public:
+    static Ref&lt;RealtimeIncomingVideoSource&gt; create(rtc::scoped_refptr&lt;webrtc::VideoTrackInterface&gt;&amp;&amp;, String&amp;&amp;);
+    ~RealtimeIncomingVideoSource() { stopProducingData(); }
+
+private:
+    RealtimeIncomingVideoSource(rtc::scoped_refptr&lt;webrtc::VideoTrackInterface&gt;&amp;&amp;, String&amp;&amp;, CFMutableDictionaryRef);
+
+    // RealtimeMediaSource API
+    void startProducingData() final;
+    void stopProducingData()  final;
+
+    RefPtr&lt;RealtimeMediaSourceCapabilities&gt; capabilities() const final;
+    const RealtimeMediaSourceSettings&amp; settings() const final;
+
+    MediaConstraints&amp; constraints() { return *m_constraints.get(); }
+    RealtimeMediaSourceSupportedConstraints&amp; supportedConstraints();
+
+    void processNewSample(CMSampleBufferRef, unsigned, unsigned);
+    RefPtr&lt;Image&gt; currentFrameImage() final;
+
+    void paintCurrentFrameInContext(GraphicsContext&amp;, const FloatRect&amp;) final;
+
+    bool isProducingData() const final { return m_isProducingData &amp;&amp; m_buffer; }
+    bool applySize(const IntSize&amp;) final { return true; }
+
+    // rtc::VideoSinkInterface
+    void OnFrame(const webrtc::VideoFrame&amp;) final;
+
+    RefPtr&lt;Image&gt; m_currentImage;
+    RealtimeMediaSourceSettings m_currentSettings;
+    RealtimeMediaSourceSupportedConstraints m_supportedConstraints;
+    RefPtr&lt;RealtimeMediaSourceCapabilities&gt; m_capabilities;
+    RefPtr&lt;MediaConstraints&gt; m_constraints;
+    bool m_isProducingData { false };
+    rtc::scoped_refptr&lt;webrtc::VideoTrackInterface&gt; m_videoTrack;
+    RetainPtr&lt;CMSampleBufferRef&gt; m_buffer;
+    PixelBufferConformerCV m_conformer;
+};
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingVideoSourcecpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp (0 => 211177)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp                                (rev 0)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp        2017-01-26 00:31:51 UTC (rev 211177)
</span><span class="lines">@@ -0,0 +1,102 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted, provided that the following conditions
+ * are required to be met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 3.  Neither the name of Apple Inc. nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS &quot;AS IS&quot; 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 INC. AND ITS 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;RealtimeOutgoingVideoSource.h&quot;
+
+#if USE(LIBWEBRTC)
+
+#include &lt;webrtc/common_video/libyuv/include/webrtc_libyuv.h&gt;
+#include &lt;webrtc/media/base/videoframe.h&gt;
+
+#include &quot;CoreMediaSoftLink.h&quot;
+
+namespace WebCore {
+
+RealtimeOutgoingVideoSource::RealtimeOutgoingVideoSource(Ref&lt;RealtimeMediaSource&gt;&amp;&amp; videoSource)
+    : m_videoSource(WTFMove(videoSource))
+{
+    m_videoSource-&gt;addObserver(this);
+}
+
+bool RealtimeOutgoingVideoSource::GetStats(Stats*)
+{
+    return false;
+}
+
+void RealtimeOutgoingVideoSource::AddOrUpdateSink(rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt;* sink, const rtc::VideoSinkWants&amp;)
+{
+    // FIXME: support sinkWants
+    if (!m_sinks.contains(sink))
+        m_sinks.append(sink);
+}
+
+void RealtimeOutgoingVideoSource::RemoveSink(rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt;* sink)
+{
+    m_sinks.removeFirst(sink);
+}
+
+void RealtimeOutgoingVideoSource::sourceHasMoreMediaData(MediaSample&amp; sample)
+{
+    if (!m_sinks.size())
+        return;
+
+    ASSERT(sample.platformSample().type == PlatformSample::CMSampleBufferType);
+    auto pixelBuffer = static_cast&lt;CVPixelBufferRef&gt;(CMSampleBufferGetImageBuffer(sample.platformSample().sample.cmSampleBuffer));
+    auto pixelFormatType = CVPixelBufferGetPixelFormatType(pixelBuffer);
+
+    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
+    uint8_t* src = reinterpret_cast&lt;uint8_t*&gt;(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0));
+
+    // FIXME: Shouldn't we use RealtimeMediaSource::size()
+    const auto&amp; settings = m_videoSource-&gt;settings();
+
+    // FIXME: We should not need to allocate one buffer per frame.
+    auto dest = webrtc::I420Buffer::Create(settings.width(), settings.height());
+
+    if (pixelFormatType == kCVPixelFormatType_420YpCbCr8Planar) {
+        // We probably can memcpy the data directly
+        webrtc::ConvertToI420(webrtc::kI420, src, 0, 0, settings.width(), settings.height(), 0, webrtc::kVideoRotation_0, dest);
+    } else if (pixelFormatType == kCVPixelFormatType_32BGRA)
+        webrtc::ConvertToI420(webrtc::kARGB, src, 0, 0, settings.width(), settings.height(), 0, webrtc::kVideoRotation_0, dest);
+    else {
+        // FIXME: Mock source conversion works with kBGRA while regular camera works with kARGB
+        ASSERT(pixelFormatType == kCVPixelFormatType_32ARGB);
+        webrtc::ConvertToI420(webrtc::kBGRA, src, 0, 0, settings.width(), settings.height(), 0, webrtc::kVideoRotation_0, dest);
+    }
+
+    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
+
+    webrtc::VideoFrame frame(dest, 0, 0,  webrtc::kVideoRotation_0);
+    for (auto* sink : m_sinks)
+        sink-&gt;OnFrame(frame);
+}
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingVideoSourceh"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.h (0 => 211177)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.h                                (rev 0)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.h        2017-01-26 00:31:51 UTC (rev 211177)
</span><span class="lines">@@ -0,0 +1,81 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted, provided that the following conditions
+ * are required to be met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 3.  Neither the name of Apple Inc. nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS &quot;AS IS&quot; 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 INC. AND ITS 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if USE(LIBWEBRTC)
+
+#include &quot;LibWebRTCMacros.h&quot;
+#include &quot;RealtimeMediaSource.h&quot;
+#include &lt;webrtc/api/mediastreaminterface.h&gt;
+#include &lt;webrtc/base/optional.h&gt;
+#include &lt;webrtc/media/base/videosinkinterface.h&gt;
+
+namespace WebCore {
+
+class RealtimeOutgoingVideoSource final : public RefCounted&lt;RealtimeOutgoingVideoSource&gt;, public webrtc::VideoTrackSourceInterface, private RealtimeMediaSource::Observer {
+public:
+    static Ref&lt;RealtimeOutgoingVideoSource&gt; create(Ref&lt;RealtimeMediaSource&gt;&amp;&amp; videoSource) { return adoptRef(*new RealtimeOutgoingVideoSource(WTFMove(videoSource))); }
+
+    int AddRef() const final { ref(); return refCount(); }
+    int Release() const final { deref(); return refCount(); }
+
+private:
+    RealtimeOutgoingVideoSource(Ref&lt;RealtimeMediaSource&gt;&amp;&amp;);
+
+    // Notifier API
+    void RegisterObserver(webrtc::ObserverInterface*) final { }
+    void UnregisterObserver(webrtc::ObserverInterface*) final { }
+
+    // VideoTrackSourceInterface API
+    bool is_screencast() const final { return false; }
+    rtc::Optional&lt;bool&gt; needs_denoising() const final { return rtc::Optional&lt;bool&gt;(); }
+    bool GetStats(Stats*) final;
+
+    // MediaSourceInterface API
+    SourceState state() const final { return SourceState(); }
+    bool remote() const final { return true; }
+
+    // rtc::VideoSourceInterface&lt;webrtc::VideoFrame&gt; API
+    void AddOrUpdateSink(rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt;*, const rtc::VideoSinkWants&amp;) final;
+    void RemoveSink(rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt;*) final;
+
+    // RealtimeMediaSource::Observer API
+    bool preventSourceFromStopping() final { return false; }
+    void sourceStopped() final { }
+    void sourceMutedChanged() final { }
+    void sourceSettingsChanged() final { }
+    void sourceHasMoreMediaData(MediaSample&amp;) final;
+
+    Vector&lt;rtc::VideoSinkInterface&lt;webrtc::VideoFrame&gt;*&gt; m_sinks;
+    Ref&lt;RealtimeMediaSource&gt; m_videoSource;
+};
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre>
</div>
</div>

</body>
</html>