Title: [211177] trunk/Source/WebCore
Revision
211177
Author
[email protected]
Date
2017-01-25 16:31:51 -0800 (Wed, 25 Jan 2017)

Log Message

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

Patch by Youenn Fablet <[email protected]> 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.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (211176 => 211177)


--- 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)
@@ -1,3 +1,36 @@
+2017-01-25  Youenn Fablet  <[email protected]>
+
+        [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.
+
 2017-01-25  Wenson Hsieh  <[email protected]>
 
         Introduce an item-provider-based pasteboard wrapper

Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.cpp (0 => 211177)


--- 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)
@@ -0,0 +1,216 @@
+/*
+ * 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)

Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSource.h (0 => 211177)


--- 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)
@@ -0,0 +1,89 @@
+/*
+ * 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)

Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.cpp (0 => 211177)


--- 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)
@@ -0,0 +1,102 @@
+/*
+ * 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 = "" 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)

Added: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingVideoSource.h (0 => 211177)


--- 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)
@@ -0,0 +1,81 @@
+/*
+ * 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)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to