<!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 <youenn@apple.com> 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 <youenn@apple.com>
+
+ [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 <wenson_hsieh@apple.com>
</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
+ * "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 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 "config.h"
+#include "RealtimeIncomingVideoSource.h"
+
+#if USE(LIBWEBRTC)
+
+#include "GraphicsContext.h"
+#include "ImageBuffer.h"
+#include "IntRect.h"
+#include "MediaSampleAVFObjC.h"
+#include <webrtc/common_video/include/corevideo_frame_buffer.h>
+#include <webrtc/common_video/libyuv/include/webrtc_libyuv.h>
+#include <wtf/MainThread.h>
+
+#include "CoreMediaSoftLink.h"
+#include "CoreVideoSoftLink.h"
+
+namespace WebCore {
+
+Ref<RealtimeIncomingVideoSource> RealtimeIncomingVideoSource::create(rtc::scoped_refptr<webrtc::VideoTrackInterface>&& videoTrack, String&& trackId)
+{
+ const OSType imageFormat = kCVPixelFormatType_32BGRA;
+ RetainPtr<CFNumberRef> imageFormatNumber = adoptCF(CFNumberCreate(nullptr, kCFNumberIntType, &imageFormat));
+
+ RetainPtr<CFMutableDictionaryRef> conformerOptions = adoptCF(CFDictionaryCreateMutable(0, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
+ CFDictionarySetValue(conformerOptions.get(), kCVPixelBufferPixelFormatTypeKey, imageFormatNumber.get());
+ PixelBufferConformerCV conformer(conformerOptions.get());
+
+ auto source = adoptRef(*new RealtimeIncomingVideoSource(WTFMove(videoTrack), WTFMove(trackId), conformerOptions.get()));
+ source->startProducingData();
+ return source;
+}
+
+RealtimeIncomingVideoSource::RealtimeIncomingVideoSource(rtc::scoped_refptr<webrtc::VideoTrackInterface>&& videoTrack, String&& 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->AddOrUpdateSink(this, rtc::VideoSinkWants());
+}
+
+void RealtimeIncomingVideoSource::stopProducingData()
+{
+ if (!m_isProducingData)
+ return;
+
+ m_isProducingData = false;
+ if (m_videoTrack)
+ m_videoTrack->RemoveSink(this);
+}
+
+void RealtimeIncomingVideoSource::OnFrame(const webrtc::VideoFrame& frame)
+{
+ if (!m_isProducingData)
+ return;
+
+ auto buffer = frame.video_frame_buffer();
+ CVPixelBufferRef pixelBuffer = static_cast<CVPixelBufferRef>(buffer->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, &formatDescription);
+ if (ostatus != noErr) {
+ LOG_ERROR("Failed to initialize CMVideoFormatDescription: %d", ostatus);
+ return;
+ }
+
+ CMSampleBufferRef sampleBuffer;
+ ostatus = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, formatDescription, &timingInfo, &sampleBuffer);
+ if (ostatus != noErr) {
+ LOG_ERROR("Failed to create the sample buffer: %d", ostatus);
+ return;
+ }
+ CFRelease(formatDescription);
+
+ RetainPtr<CMSampleBufferRef> sample = sampleBuffer;
+
+ CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true);
+ for (CFIndex i = 0; i < CFArrayGetCount(attachmentsArray); ++i) {
+ CFMutableDictionaryRef attachments = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachmentsArray, i);
+ CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
+ }
+
+ unsigned width = frame.width();
+ unsigned height = frame.height();
+ RefPtr<RealtimeIncomingVideoSource> protectedThis(this);
+ callOnMainThread([protectedThis = WTFMove(protectedThis), sample = WTFMove(sample), width, height] {
+ protectedThis->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& imageBuffer, CGImageRef image, const FloatRect& rect)
+{
+ auto& 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<Image> 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<CVPixelBufferRef>(CMSampleBufferGetImageBuffer(m_buffer.get()));
+ drawImage(*imageBuffer, m_conformer.createImageFromPixelBuffer(pixelBuffer).get(), rect);
+
+ return ImageBuffer::sinkIntoImage(WTFMove(imageBuffer));
+}
+
+void RealtimeIncomingVideoSource::paintCurrentFrameInContext(GraphicsContext& context, const FloatRect& 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<CVPixelBufferRef>(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->copyImage(DontCopyBackingStore), rect);
+}
+
+RefPtr<RealtimeMediaSourceCapabilities> RealtimeIncomingVideoSource::capabilities() const
+{
+ return m_capabilities;
+}
+
+const RealtimeMediaSourceSettings& RealtimeIncomingVideoSource::settings() const
+{
+ return m_currentSettings;
+}
+
+RealtimeMediaSourceSupportedConstraints& 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
+ * "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 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 "LibWebRTCMacros.h"
+#include "PixelBufferConformerCV.h"
+#include "RealtimeMediaSource.h"
+#include <webrtc/api/mediastreaminterface.h>
+#include <wtf/RetainPtr.h>
+
+typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
+
+namespace WebCore {
+
+class CaptureDevice;
+
+class RealtimeIncomingVideoSource final : public RealtimeMediaSource, private rtc::VideoSinkInterface<webrtc::VideoFrame> {
+public:
+ static Ref<RealtimeIncomingVideoSource> create(rtc::scoped_refptr<webrtc::VideoTrackInterface>&&, String&&);
+ ~RealtimeIncomingVideoSource() { stopProducingData(); }
+
+private:
+ RealtimeIncomingVideoSource(rtc::scoped_refptr<webrtc::VideoTrackInterface>&&, String&&, CFMutableDictionaryRef);
+
+ // RealtimeMediaSource API
+ void startProducingData() final;
+ void stopProducingData() final;
+
+ RefPtr<RealtimeMediaSourceCapabilities> capabilities() const final;
+ const RealtimeMediaSourceSettings& settings() const final;
+
+ MediaConstraints& constraints() { return *m_constraints.get(); }
+ RealtimeMediaSourceSupportedConstraints& supportedConstraints();
+
+ void processNewSample(CMSampleBufferRef, unsigned, unsigned);
+ RefPtr<Image> currentFrameImage() final;
+
+ void paintCurrentFrameInContext(GraphicsContext&, const FloatRect&) final;
+
+ bool isProducingData() const final { return m_isProducingData && m_buffer; }
+ bool applySize(const IntSize&) final { return true; }
+
+ // rtc::VideoSinkInterface
+ void OnFrame(const webrtc::VideoFrame&) final;
+
+ RefPtr<Image> m_currentImage;
+ RealtimeMediaSourceSettings m_currentSettings;
+ RealtimeMediaSourceSupportedConstraints m_supportedConstraints;
+ RefPtr<RealtimeMediaSourceCapabilities> m_capabilities;
+ RefPtr<MediaConstraints> m_constraints;
+ bool m_isProducingData { false };
+ rtc::scoped_refptr<webrtc::VideoTrackInterface> m_videoTrack;
+ RetainPtr<CMSampleBufferRef> 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 "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 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 "config.h"
+#include "RealtimeOutgoingVideoSource.h"
+
+#if USE(LIBWEBRTC)
+
+#include <webrtc/common_video/libyuv/include/webrtc_libyuv.h>
+#include <webrtc/media/base/videoframe.h>
+
+#include "CoreMediaSoftLink.h"
+
+namespace WebCore {
+
+RealtimeOutgoingVideoSource::RealtimeOutgoingVideoSource(Ref<RealtimeMediaSource>&& videoSource)
+ : m_videoSource(WTFMove(videoSource))
+{
+ m_videoSource->addObserver(this);
+}
+
+bool RealtimeOutgoingVideoSource::GetStats(Stats*)
+{
+ return false;
+}
+
+void RealtimeOutgoingVideoSource::AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, const rtc::VideoSinkWants&)
+{
+ // FIXME: support sinkWants
+ if (!m_sinks.contains(sink))
+ m_sinks.append(sink);
+}
+
+void RealtimeOutgoingVideoSource::RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink)
+{
+ m_sinks.removeFirst(sink);
+}
+
+void RealtimeOutgoingVideoSource::sourceHasMoreMediaData(MediaSample& sample)
+{
+ if (!m_sinks.size())
+ return;
+
+ ASSERT(sample.platformSample().type == PlatformSample::CMSampleBufferType);
+ auto pixelBuffer = static_cast<CVPixelBufferRef>(CMSampleBufferGetImageBuffer(sample.platformSample().sample.cmSampleBuffer));
+ auto pixelFormatType = CVPixelBufferGetPixelFormatType(pixelBuffer);
+
+ CVPixelBufferLockBaseAddress(pixelBuffer, 0);
+ uint8_t* src = reinterpret_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0));
+
+ // FIXME: Shouldn't we use RealtimeMediaSource::size()
+ const auto& settings = m_videoSource->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->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 "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 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 "LibWebRTCMacros.h"
+#include "RealtimeMediaSource.h"
+#include <webrtc/api/mediastreaminterface.h>
+#include <webrtc/base/optional.h>
+#include <webrtc/media/base/videosinkinterface.h>
+
+namespace WebCore {
+
+class RealtimeOutgoingVideoSource final : public RefCounted<RealtimeOutgoingVideoSource>, public webrtc::VideoTrackSourceInterface, private RealtimeMediaSource::Observer {
+public:
+ static Ref<RealtimeOutgoingVideoSource> create(Ref<RealtimeMediaSource>&& videoSource) { return adoptRef(*new RealtimeOutgoingVideoSource(WTFMove(videoSource))); }
+
+ int AddRef() const final { ref(); return refCount(); }
+ int Release() const final { deref(); return refCount(); }
+
+private:
+ RealtimeOutgoingVideoSource(Ref<RealtimeMediaSource>&&);
+
+ // Notifier API
+ void RegisterObserver(webrtc::ObserverInterface*) final { }
+ void UnregisterObserver(webrtc::ObserverInterface*) final { }
+
+ // VideoTrackSourceInterface API
+ bool is_screencast() const final { return false; }
+ rtc::Optional<bool> needs_denoising() const final { return rtc::Optional<bool>(); }
+ bool GetStats(Stats*) final;
+
+ // MediaSourceInterface API
+ SourceState state() const final { return SourceState(); }
+ bool remote() const final { return true; }
+
+ // rtc::VideoSourceInterface<webrtc::VideoFrame> API
+ void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>*, const rtc::VideoSinkWants&) final;
+ void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>*) final;
+
+ // RealtimeMediaSource::Observer API
+ bool preventSourceFromStopping() final { return false; }
+ void sourceStopped() final { }
+ void sourceMutedChanged() final { }
+ void sourceSettingsChanged() final { }
+ void sourceHasMoreMediaData(MediaSample&) final;
+
+ Vector<rtc::VideoSinkInterface<webrtc::VideoFrame>*> m_sinks;
+ Ref<RealtimeMediaSource> m_videoSource;
+};
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre>
</div>
</div>
</body>
</html>