<!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>[212144] 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/212144">212144</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2017-02-10 13:29:32 -0800 (Fri, 10 Feb 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[WebRTC] Implement Outgoing libwebrtc audio source support
https://bugs.webkit.org/show_bug.cgi?id=168118

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

No new automated tests as we need the audio rendering to work to test the whole loop.

Using an AudioSampleDataSource to convert the captured data to libwebrtc expected format.
Capturing and pushing data happens in the capture thread.
Pulling of converted data happens in libwebrtc thread.

Introducing LibWebRTCAudioFormat.h to centralize libwbebrtc expected audio format.

* WebCore.xcodeproj/project.pbxproj:
* platform/audio/WebAudioBufferList.cpp:
(WebCore::WebAudioBufferList::WebAudioBufferList): Missing initialization leads to assertion failure.
* platform/audio/mac/AudioSampleBufferList.cpp:
(WebCore::AudioSampleBufferList::copyFrom): In case of interleaved channels, there is one buffer but two channels.
* platform/audio/mac/AudioSampleDataSource.cpp:
(WebCore::AudioSampleDataSource::pushSamples):
(WebCore::AudioSampleDataSource::pullAvalaibleSamplesAsChunks):
* platform/audio/mac/AudioSampleDataSource.h:
* platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h: Added.
* platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp:
(WebCore::libwebrtcAudioFormat):
(WebCore::RealtimeOutgoingAudioSource::RealtimeOutgoingAudioSource):
(WebCore::RealtimeOutgoingAudioSource::sourceMutedChanged):
(WebCore::RealtimeOutgoingAudioSource::audioSamplesAvailable):
(WebCore::RealtimeOutgoingAudioSource::pullAudioData):
* platform/mediastream/mac/RealtimeOutgoingAudioSource.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreWebCorexcodeprojprojectpbxproj">trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioWebAudioBufferListcpp">trunk/Source/WebCore/platform/audio/WebAudioBufferList.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudiomacAudioSampleBufferListcpp">trunk/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudiomacAudioSampleDataSourcecpp">trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudiomacAudioSampleDataSourceh">trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingAudioSourcecpp">trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingAudioSourceh">trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreplatformmediastreamlibwebrtcLibWebRTCAudioFormath">trunk/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/ChangeLog        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2017-02-10  Youenn Fablet  &lt;youenn@apple.com&gt;
+
+        [WebRTC] Implement Outgoing libwebrtc audio source support
+        https://bugs.webkit.org/show_bug.cgi?id=168118
+
+        Reviewed by Eric Carlson.
+
+        No new automated tests as we need the audio rendering to work to test the whole loop.
+
+        Using an AudioSampleDataSource to convert the captured data to libwebrtc expected format.
+        Capturing and pushing data happens in the capture thread.
+        Pulling of converted data happens in libwebrtc thread.
+
+        Introducing LibWebRTCAudioFormat.h to centralize libwbebrtc expected audio format.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/audio/WebAudioBufferList.cpp:
+        (WebCore::WebAudioBufferList::WebAudioBufferList): Missing initialization leads to assertion failure.
+        * platform/audio/mac/AudioSampleBufferList.cpp:
+        (WebCore::AudioSampleBufferList::copyFrom): In case of interleaved channels, there is one buffer but two channels.
+        * platform/audio/mac/AudioSampleDataSource.cpp:
+        (WebCore::AudioSampleDataSource::pushSamples):
+        (WebCore::AudioSampleDataSource::pullAvalaibleSamplesAsChunks):
+        * platform/audio/mac/AudioSampleDataSource.h:
+        * platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h: Added.
+        * platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp:
+        (WebCore::libwebrtcAudioFormat):
+        (WebCore::RealtimeOutgoingAudioSource::RealtimeOutgoingAudioSource):
+        (WebCore::RealtimeOutgoingAudioSource::sourceMutedChanged):
+        (WebCore::RealtimeOutgoingAudioSource::audioSamplesAvailable):
+        (WebCore::RealtimeOutgoingAudioSource::pullAudioData):
+        * platform/mediastream/mac/RealtimeOutgoingAudioSource.h:
+
</ins><span class="cx"> 2017-02-10  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         HTMLConstructionSiteTask::Insert should never be called on a node with a parent
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCorexcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -8876,6 +8876,7 @@
</span><span class="cx">                 41CF8BE61D46222C00707DC9 /* FetchInternals.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = FetchInternals.js; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 41D015C80F4B5C71004A662F /* ContentType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentType.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 41D015C90F4B5C71004A662F /* ContentType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContentType.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                41D51BB21E4E2E8100131A5B /* LibWebRTCAudioFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LibWebRTCAudioFormat.h; path = libwebrtc/LibWebRTCAudioFormat.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 41E1B1CA0FF5986900576B3B /* AbstractWorker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AbstractWorker.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 41E1B1CB0FF5986900576B3B /* AbstractWorker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractWorker.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 41E1B1CC0FF5986900576B3B /* AbstractWorker.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = AbstractWorker.idl; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -16845,6 +16846,7 @@
</span><span class="cx">                 415747421E38698000E914D8 /* libwebrtc */ = {
</span><span class="cx">                         isa = PBXGroup;
</span><span class="cx">                         children = (
</span><ins>+                                41D51BB21E4E2E8100131A5B /* LibWebRTCAudioFormat.h */,
</ins><span class="cx">                                 415080341E3F00AA0051D75D /* LibWebRTCAudioModule.cpp */,
</span><span class="cx">                                 415080351E3F00AA0051D75D /* LibWebRTCAudioModule.h */,
</span><span class="cx">                                 415747431E38699E00E914D8 /* LibWebRTCMacros.h */,
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioWebAudioBufferListcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/WebAudioBufferList.cpp (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/WebAudioBufferList.cpp        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/audio/WebAudioBufferList.cpp        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -64,6 +64,7 @@
</span><span class="cx"> 
</span><span class="cx">     for (uint32_t buffer = 0; buffer &lt; m_list-&gt;mNumberBuffers; ++buffer) {
</span><span class="cx">         m_list-&gt;mBuffers[buffer].mData = data;
</span><ins>+        m_list-&gt;mBuffers[buffer].mDataByteSize = bytesPerBuffer;
</ins><span class="cx">         data += bytesPerBuffer;
</span><span class="cx">     }
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudiomacAudioSampleBufferListcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -260,7 +260,7 @@
</span><span class="cx">     propertyDataSize = sizeof(outputFormat);
</span><span class="cx">     AudioConverterGetProperty(converter, kAudioConverterCurrentOutputStreamDescription, &amp;propertyDataSize, &amp;outputFormat);
</span><span class="cx"> 
</span><del>-    ASSERT(outputFormat.mChannelsPerFrame == m_bufferList-&gt;bufferCount());
</del><ins>+    ASSERT(CAAudioStreamDescription(outputFormat).numberOfChannelStreams() == m_bufferList-&gt;bufferCount());
</ins><span class="cx">     for (uint32_t i = 0; i &lt; m_bufferList-&gt;bufferCount(); ++i) {
</span><span class="cx">         ASSERT(m_bufferList-&gt;buffer(i)-&gt;mData);
</span><span class="cx">         ASSERT(m_bufferList-&gt;buffer(i)-&gt;mDataByteSize);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudiomacAudioSampleDataSourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -201,7 +201,7 @@
</span><span class="cx"> {
</span><span class="cx">     std::unique_lock&lt;Lock&gt; lock(m_lock, std::try_to_lock);
</span><span class="cx">     ASSERT(is&lt;WebAudioBufferList&gt;(audioData));
</span><del>-    pushSamplesInternal(downcast&lt;WebAudioBufferList&gt;(audioData), sampleTime, sampleCount);
</del><ins>+    pushSamplesInternal(*downcast&lt;WebAudioBufferList&gt;(audioData).list(), sampleTime, sampleCount);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool AudioSampleDataSource::pullSamplesInternal(AudioBufferList&amp; buffer, size_t&amp; sampleCount, uint64_t timeStamp, double /*hostTime*/, PullMode mode)
</span><span class="lines">@@ -286,6 +286,32 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool AudioSampleDataSource::pullAvalaibleSamplesAsChunks(AudioBufferList&amp; buffer, size_t sampleCountPerChunk, uint64_t timeStamp, Function&lt;void()&gt;&amp;&amp; consumeFilledBuffer)
+{
+    std::unique_lock&lt;Lock&gt; lock(m_lock, std::try_to_lock);
+    if (!lock.owns_lock() || !m_ringBuffer)
+        return false;
+
+    ASSERT(buffer.mNumberBuffers == m_ringBuffer-&gt;channelCount());
+    if (buffer.mNumberBuffers != m_ringBuffer-&gt;channelCount())
+        return false;
+
+    uint64_t startFrame = 0;
+    uint64_t endFrame = 0;
+    m_ringBuffer-&gt;getCurrentFrameBounds(startFrame, endFrame);
+    if (timeStamp &lt; startFrame)
+        return false;
+
+    startFrame = timeStamp;
+    while (endFrame - startFrame &gt;= sampleCountPerChunk) {
+        if (m_ringBuffer-&gt;fetch(&amp;buffer, sampleCountPerChunk, startFrame, CARingBuffer::Copy))
+            return false;
+        consumeFilledBuffer();
+        startFrame += sampleCountPerChunk;
+    }
+    return true;
+}
+
</ins><span class="cx"> bool AudioSampleDataSource::pullSamples(AudioBufferList&amp; buffer, size_t sampleCount, uint64_t timeStamp, double hostTime, PullMode mode)
</span><span class="cx"> {
</span><span class="cx">     std::unique_lock&lt;Lock&gt; lock(m_lock, std::try_to_lock);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudiomacAudioSampleDataSourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -59,6 +59,8 @@
</span><span class="cx">     bool pullSamples(AudioSampleBufferList&amp;, size_t, uint64_t, double, PullMode);
</span><span class="cx">     bool pullSamples(AudioBufferList&amp;, size_t, uint64_t, double, PullMode);
</span><span class="cx"> 
</span><ins>+    bool pullAvalaibleSamplesAsChunks(AudioBufferList&amp;, size_t frameCount, uint64_t timeStamp, Function&lt;void()&gt;&amp;&amp;);
+
</ins><span class="cx">     void setPaused(bool);
</span><span class="cx"> 
</span><span class="cx">     void setVolume(float volume) { m_volume = volume; }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreamlibwebrtcLibWebRTCAudioFormath"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h (0 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h                                (rev 0)
+++ trunk/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCAudioFormat.h        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -0,0 +1,46 @@
</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.
+ *
+ * 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. OR 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)
+
+namespace WebCore {
+
+namespace LibWebRTCAudioFormat {
+
+static const size_t sampleRate = 48000;
+static const size_t chunkSampleCount = 480;
+static const size_t sampleSize = 16;
+static const size_t sampleByteSize = 2;
+static const bool isFloat = false;
+static const bool isBigEndian = false;
+static const bool isNonInterleaved = false;
+
+}
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingAudioSourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.cpp        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -31,15 +31,68 @@
</span><span class="cx"> 
</span><span class="cx"> #if USE(LIBWEBRTC)
</span><span class="cx"> 
</span><del>-#include &quot;NotImplemented.h&quot;
</del><ins>+#include &quot;CAAudioStreamDescription.h&quot;
+#include &quot;LibWebRTCAudioFormat.h&quot;
+#include &quot;LibWebRTCUtils.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-void RealtimeOutgoingAudioSource::audioSamplesAvailable(const MediaTime&amp;, const PlatformAudioData&amp;, const AudioStreamDescription&amp;, size_t)
</del><ins>+static inline AudioStreamBasicDescription libwebrtcAudioFormat(size_t channelCount)
</ins><span class="cx"> {
</span><del>-    notImplemented();
</del><ins>+    AudioStreamBasicDescription streamFormat;
+    FillOutASBDForLPCM(streamFormat, LibWebRTCAudioFormat::sampleRate, channelCount, LibWebRTCAudioFormat::sampleSize, LibWebRTCAudioFormat::sampleSize, LibWebRTCAudioFormat::isFloat, LibWebRTCAudioFormat::isBigEndian, LibWebRTCAudioFormat::isNonInterleaved);
+    return streamFormat;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+RealtimeOutgoingAudioSource::RealtimeOutgoingAudioSource(Ref&lt;RealtimeMediaSource&gt;&amp;&amp; audioSource)
+    : m_audioSource(WTFMove(audioSource))
+    , m_sampleConverter(AudioSampleDataSource::create(LibWebRTCAudioFormat::sampleRate * 2))
+{
+    m_audioSource-&gt;addObserver(*this);
+}
+
+void RealtimeOutgoingAudioSource::sourceMutedChanged()
+{
+    m_isMuted = m_audioSource-&gt;muted();
+}
+
+void RealtimeOutgoingAudioSource::audioSamplesAvailable(const MediaTime&amp; time, const PlatformAudioData&amp; audioData, const AudioStreamDescription&amp; streamDescription, size_t sampleCount)
+{
+    if (m_inputStreamDescription != streamDescription) {
+        m_inputStreamDescription = toCAAudioStreamDescription(streamDescription);
+        auto status  = m_sampleConverter-&gt;setInputFormat(m_inputStreamDescription);
+        ASSERT_UNUSED(status, !status);
+
+        status = m_sampleConverter-&gt;setOutputFormat(libwebrtcAudioFormat(streamDescription.numberOfChannels()));
+        ASSERT(!status);
+    }
+    m_sampleConverter-&gt;pushSamples(time, audioData, sampleCount);
+
+    callOnWebRTCSignalingThread([protectedThis = makeRef(*this)] {
+        protectedThis-&gt;pullAudioData();
+    });
+}
+
+void RealtimeOutgoingAudioSource::pullAudioData()
+{
+    size_t bufferSize = LibWebRTCAudioFormat::chunkSampleCount * LibWebRTCAudioFormat::sampleByteSize * m_inputStreamDescription.numberOfChannels();
+    m_audioBuffer.reserveCapacity(bufferSize);
+
+    AudioBufferList bufferList;
+    bufferList.mNumberBuffers = 1;
+    bufferList.mBuffers[0].mNumberChannels = m_inputStreamDescription.numberOfChannels();
+    bufferList.mBuffers[0].mDataByteSize = bufferSize;
+    bufferList.mBuffers[0].mData = m_audioBuffer.data();
+
+    m_sampleConverter-&gt;pullAvalaibleSamplesAsChunks(bufferList, LibWebRTCAudioFormat::chunkSampleCount, m_startFrame, [this] {
+        m_startFrame += LibWebRTCAudioFormat::chunkSampleCount;
+        if (m_isMuted)
+            return;
+        for (auto sink : m_sinks)
+            sink-&gt;OnData(m_audioBuffer.data(), LibWebRTCAudioFormat::sampleSize, LibWebRTCAudioFormat::sampleRate, m_inputStreamDescription.numberOfChannels(), LibWebRTCAudioFormat::chunkSampleCount);
+    });
+}
+
</ins><span class="cx"> } // namespace WebCore
</span><span class="cx"> 
</span><span class="cx"> #endif // USE(LIBWEBRTC)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreammacRealtimeOutgoingAudioSourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.h (212143 => 212144)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.h        2017-02-10 21:24:43 UTC (rev 212143)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeOutgoingAudioSource.h        2017-02-10 21:29:32 UTC (rev 212144)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> 
</span><span class="cx"> #if USE(LIBWEBRTC)
</span><span class="cx"> 
</span><ins>+#include &quot;AudioSampleDataSource.h&quot;
</ins><span class="cx"> #include &quot;LibWebRTCMacros.h&quot;
</span><span class="cx"> #include &quot;RealtimeMediaSource.h&quot;
</span><span class="cx"> #include &lt;webrtc/api/mediastreaminterface.h&gt;
</span><span class="lines">@@ -49,7 +50,7 @@
</span><span class="cx">     void setTrack(rtc::scoped_refptr&lt;webrtc::AudioTrackInterface&gt;&amp;&amp; track) { m_track = WTFMove(track); }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    explicit RealtimeOutgoingAudioSource(Ref&lt;RealtimeMediaSource&gt;&amp;&amp; audioSource) : m_audioSource(WTFMove(audioSource)) { m_audioSource-&gt;addObserver(*this); }
</del><ins>+    explicit RealtimeOutgoingAudioSource(Ref&lt;RealtimeMediaSource&gt;&amp;&amp;);
</ins><span class="cx"> 
</span><span class="cx">     virtual void AddSink(webrtc::AudioTrackSinkInterface* sink) { m_sinks.append(sink); }
</span><span class="cx">     virtual void RemoveSink(webrtc::AudioTrackSinkInterface* sink) { m_sinks.removeFirst(sink); }
</span><span class="lines">@@ -63,17 +64,22 @@
</span><span class="cx"> 
</span><span class="cx">     // RealtimeMediaSource::Observer API
</span><span class="cx">     void sourceStopped() final { }
</span><del>-    void sourceMutedChanged() final { }
</del><ins>+    void sourceMutedChanged() final;
</ins><span class="cx">     void sourceSettingsChanged() final { }
</span><span class="cx">     bool preventSourceFromStopping() final { return false; }
</span><span class="cx">     void audioSamplesAvailable(const MediaTime&amp;, const PlatformAudioData&amp;, const AudioStreamDescription&amp;, size_t) final;
</span><span class="cx"> 
</span><del>-    void convertAndSendMonoSamples();
-    void convertAndSendStereoSamples();
</del><ins>+    void pullAudioData();
</ins><span class="cx"> 
</span><span class="cx">     Vector&lt;webrtc::AudioTrackSinkInterface*&gt; m_sinks;
</span><span class="cx">     Ref&lt;RealtimeMediaSource&gt; m_audioSource;
</span><span class="cx">     rtc::scoped_refptr&lt;webrtc::AudioTrackInterface&gt; m_track;
</span><ins>+    Ref&lt;AudioSampleDataSource&gt; m_sampleConverter;
+    CAAudioStreamDescription m_inputStreamDescription;
+
+    Vector&lt;uint16_t&gt; m_audioBuffer;
+    uint64_t m_startFrame { 0 };
+    bool m_isMuted { false };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre>
</div>
</div>

</body>
</html>