[Mac] Add classes to manage audio samples
authoreric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Feb 2017 23:01:54 +0000 (23:01 +0000)
committereric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Feb 2017 23:01:54 +0000 (23:01 +0000)
https://bugs.webkit.org/show_bug.cgi?id=167739

Reviewed by Jer Noble.

No new tests, this code isn't used yet.

* WebCore.xcodeproj/project.pbxproj:
* platform/audio/mac/AudioSampleBufferList.cpp: Added.
(WebCore::AudioSampleBufferList::create):
(WebCore::AudioSampleBufferList::AudioSampleBufferList):
(WebCore::AudioSampleBufferList::~AudioSampleBufferList):
(WebCore::AudioSampleBufferList::setSampleCount):
(WebCore::AudioSampleBufferList::applyGain):
(WebCore::AudioSampleBufferList::mixFrom):
(WebCore::AudioSampleBufferList::copyFrom):
(WebCore::AudioSampleBufferList::copyTo):
(WebCore::AudioSampleBufferList::reset):
(WebCore::AudioSampleBufferList::zero):
(WebCore::AudioSampleBufferList::zeroABL):
(WebCore::AudioSampleBufferList::convertInput):
(WebCore::AudioSampleBufferList::audioConverterCallback):
(WebCore::AudioSampleBufferList::configureBufferListForStream):
* platform/audio/mac/AudioSampleBufferList.h: Added.
(WebCore::AudioSampleBufferList::streamDescription):
(WebCore::AudioSampleBufferList::bufferList):
(WebCore::AudioSampleBufferList::sampleCapacity):
(WebCore::AudioSampleBufferList::sampleCount):
(WebCore::AudioSampleBufferList::timestamp):
(WebCore::AudioSampleBufferList::hostTime):
(WebCore::AudioSampleBufferList::setTimes):
(WebCore::AudioSampleBufferList::audioBufferListSizeForStream):
* platform/audio/mac/AudioSampleDataSource.cpp: Added.
(WebCore::AudioSampleDataSource::create):
(WebCore::AudioSampleDataSource::AudioSampleDataSource):
(WebCore::AudioSampleDataSource::~AudioSampleDataSource):
(WebCore::AudioSampleDataSource::setPaused):
(WebCore::AudioSampleDataSource::setupConverter):
(WebCore::AudioSampleDataSource::setInputFormat):
(WebCore::AudioSampleDataSource::setOutputFormat):
(WebCore::AudioSampleDataSource::hostTime):
(WebCore::AudioSampleDataSource::pushSamplesInternal):
(WebCore::AudioSampleDataSource::pushSamples):
(WebCore::AudioSampleDataSource::pullSamplesInternal):
(WebCore::AudioSampleDataSource::pullSamples):
* platform/audio/mac/AudioSampleDataSource.h: Added.
(WebCore::AudioSampleDataSource::setVolume):
(WebCore::AudioSampleDataSource::volume):
(WebCore::AudioSampleDataSource::setMuted):
(WebCore::AudioSampleDataSource::muted):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211596 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp [new file with mode: 0644]
Source/WebCore/platform/audio/mac/AudioSampleBufferList.h [new file with mode: 0644]
Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp [new file with mode: 0644]
Source/WebCore/platform/audio/mac/AudioSampleDataSource.h [new file with mode: 0644]

index 8094ee1..88b8549 100644 (file)
@@ -1,3 +1,56 @@
+2017-02-02  Eric Carlson  <eric.carlson@apple.com>
+
+        [Mac] Add classes to manage audio samples
+        https://bugs.webkit.org/show_bug.cgi?id=167739
+
+        Reviewed by Jer Noble.
+
+        No new tests, this code isn't used yet.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/audio/mac/AudioSampleBufferList.cpp: Added.
+        (WebCore::AudioSampleBufferList::create):
+        (WebCore::AudioSampleBufferList::AudioSampleBufferList):
+        (WebCore::AudioSampleBufferList::~AudioSampleBufferList):
+        (WebCore::AudioSampleBufferList::setSampleCount):
+        (WebCore::AudioSampleBufferList::applyGain):
+        (WebCore::AudioSampleBufferList::mixFrom):
+        (WebCore::AudioSampleBufferList::copyFrom):
+        (WebCore::AudioSampleBufferList::copyTo):
+        (WebCore::AudioSampleBufferList::reset):
+        (WebCore::AudioSampleBufferList::zero):
+        (WebCore::AudioSampleBufferList::zeroABL):
+        (WebCore::AudioSampleBufferList::convertInput):
+        (WebCore::AudioSampleBufferList::audioConverterCallback):
+        (WebCore::AudioSampleBufferList::configureBufferListForStream):
+        * platform/audio/mac/AudioSampleBufferList.h: Added.
+        (WebCore::AudioSampleBufferList::streamDescription):
+        (WebCore::AudioSampleBufferList::bufferList):
+        (WebCore::AudioSampleBufferList::sampleCapacity):
+        (WebCore::AudioSampleBufferList::sampleCount):
+        (WebCore::AudioSampleBufferList::timestamp):
+        (WebCore::AudioSampleBufferList::hostTime):
+        (WebCore::AudioSampleBufferList::setTimes):
+        (WebCore::AudioSampleBufferList::audioBufferListSizeForStream):
+        * platform/audio/mac/AudioSampleDataSource.cpp: Added.
+        (WebCore::AudioSampleDataSource::create):
+        (WebCore::AudioSampleDataSource::AudioSampleDataSource):
+        (WebCore::AudioSampleDataSource::~AudioSampleDataSource):
+        (WebCore::AudioSampleDataSource::setPaused):
+        (WebCore::AudioSampleDataSource::setupConverter):
+        (WebCore::AudioSampleDataSource::setInputFormat):
+        (WebCore::AudioSampleDataSource::setOutputFormat):
+        (WebCore::AudioSampleDataSource::hostTime):
+        (WebCore::AudioSampleDataSource::pushSamplesInternal):
+        (WebCore::AudioSampleDataSource::pushSamples):
+        (WebCore::AudioSampleDataSource::pullSamplesInternal):
+        (WebCore::AudioSampleDataSource::pullSamples):
+        * platform/audio/mac/AudioSampleDataSource.h: Added.
+        (WebCore::AudioSampleDataSource::setVolume):
+        (WebCore::AudioSampleDataSource::volume):
+        (WebCore::AudioSampleDataSource::setMuted):
+        (WebCore::AudioSampleDataSource::muted):
+
 2017-02-02  Joseph Pecoraro  <pecoraro@apple.com>
 
         Support Performance API (performance.now(), UserTiming) in Workers
index 337a4ea..0c9676d 100644 (file)
                073794FE19F5864E00E5A045 /* RTCNotifiersMock.h in Headers */ = {isa = PBXBuildFile; fileRef = 073794F819F5864E00E5A045 /* RTCNotifiersMock.h */; };
                07394EC81BAB2CCD00BE99CD /* MediaDevicesRequest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07394EC71BAB2CCD00BE99CD /* MediaDevicesRequest.cpp */; };
                07394ECA1BAB2CD700BE99CD /* MediaDevicesRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 07394EC91BAB2CD700BE99CD /* MediaDevicesRequest.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               073B87661E4385AC0071C0EC /* AudioSampleBufferList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 073B87621E43859D0071C0EC /* AudioSampleBufferList.cpp */; };
+               073B87671E4385AC0071C0EC /* AudioSampleBufferList.h in Headers */ = {isa = PBXBuildFile; fileRef = 073B87631E43859D0071C0EC /* AudioSampleBufferList.h */; };
+               073B87681E4385AC0071C0EC /* AudioSampleDataSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 073B87641E43859D0071C0EC /* AudioSampleDataSource.cpp */; };
+               073B87691E4385AC0071C0EC /* AudioSampleDataSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 073B87651E43859D0071C0EC /* AudioSampleDataSource.h */; };
                073BE34017D17E01002BD431 /* JSNavigatorUserMedia.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 073BE33E17D17E01002BD431 /* JSNavigatorUserMedia.cpp */; };
                073BE34117D17E01002BD431 /* JSNavigatorUserMedia.h in Headers */ = {isa = PBXBuildFile; fileRef = 073BE33F17D17E01002BD431 /* JSNavigatorUserMedia.h */; settings = {ATTRIBUTES = (Private, ); }; };
                073BE34E17D180B2002BD431 /* RTCSessionDescriptionDescriptor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07221BAB17CF0AD400848E51 /* RTCSessionDescriptionDescriptor.cpp */; };
                073B87561E40DCE50071C0EC /* AudioStreamDescription.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioStreamDescription.h; sourceTree = "<group>"; };
                073B87571E40DCFD0071C0EC /* CAAudioStreamDescription.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CAAudioStreamDescription.cpp; sourceTree = "<group>"; };
                073B87581E40DCFD0071C0EC /* CAAudioStreamDescription.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CAAudioStreamDescription.h; sourceTree = "<group>"; };
+               073B87621E43859D0071C0EC /* AudioSampleBufferList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AudioSampleBufferList.cpp; sourceTree = "<group>"; };
+               073B87631E43859D0071C0EC /* AudioSampleBufferList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioSampleBufferList.h; sourceTree = "<group>"; };
+               073B87641E43859D0071C0EC /* AudioSampleDataSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AudioSampleDataSource.cpp; sourceTree = "<group>"; };
+               073B87651E43859D0071C0EC /* AudioSampleDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioSampleDataSource.h; sourceTree = "<group>"; };
                073BE33E17D17E01002BD431 /* JSNavigatorUserMedia.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSNavigatorUserMedia.cpp; sourceTree = "<group>"; };
                073BE33F17D17E01002BD431 /* JSNavigatorUserMedia.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSNavigatorUserMedia.h; sourceTree = "<group>"; };
                0744ECEB1E0C4AE5000D0944 /* MockRealtimeAudioSourceMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MockRealtimeAudioSourceMac.h; sourceTree = "<group>"; };
                FD31604012B026A300C1A359 /* audio */ = {
                        isa = PBXGroup;
                        children = (
-                               073B87561E40DCE50071C0EC /* AudioStreamDescription.h */,
                                CD669D651D232DF4004D1866 /* cocoa */,
                                CD0EEE0D14743E48003EAFA2 /* ios */,
                                FD3160B012B0270700C1A359 /* mac */,
                                CDA79821170A22DC00D45C55 /* AudioSession.h */,
                                FD31605312B026F700C1A359 /* AudioSourceProvider.h */,
                                FD62F52D145898D80094B0ED /* AudioSourceProviderClient.h */,
+                               073B87561E40DCE50071C0EC /* AudioStreamDescription.h */,
                                FD31605412B026F700C1A359 /* AudioUtilities.cpp */,
                                FD31605512B026F700C1A359 /* AudioUtilities.h */,
                                FD31605612B026F700C1A359 /* Biquad.cpp */,
                FD3160B012B0270700C1A359 /* mac */ = {
                        isa = PBXGroup;
                        children = (
+                               073B87621E43859D0071C0EC /* AudioSampleBufferList.cpp */,
+                               073B87631E43859D0071C0EC /* AudioSampleBufferList.h */,
+                               073B87641E43859D0071C0EC /* AudioSampleDataSource.cpp */,
+                               073B87651E43859D0071C0EC /* AudioSampleDataSource.h */,
                                FD3160B512B0272A00C1A359 /* AudioBusMac.mm */,
                                FD3160B612B0272A00C1A359 /* AudioDestinationMac.cpp */,
                                FD3160B712B0272A00C1A359 /* AudioDestinationMac.h */,
                                2D8FEBDD143E3EF70072502B /* CSSCrossfadeValue.h in Headers */,
                                AA21ECCD0ABF0FC6002B834C /* CSSCursorImageValue.h in Headers */,
                                9444CBE41D8861990073A074 /* CSSCustomIdentValue.h in Headers */,
+                               073B87691E4385AC0071C0EC /* AudioSampleDataSource.h in Headers */,
                                BC779E141BB215BB00CAA8BF /* CSSCustomPropertyValue.h in Headers */,
                                4A9CC81816BB9AC600EC645A /* CSSDefaultStyleSheets.h in Headers */,
                                94476BDB1DFCAC0300690E23 /* CSSDeferredParser.h in Headers */,
                                6C4C96DF1AD4483500363F64 /* JSReadableByteStreamController.h in Headers */,
                                7C4C96DD1AD4483500365A50 /* JSReadableStream.h in Headers */,
                                6C4C96DF1AD4483500365A50 /* JSReadableStreamDefaultController.h in Headers */,
+                               073B87671E4385AC0071C0EC /* AudioSampleBufferList.h in Headers */,
                                7C4C96DF1AD4483500365A50 /* JSReadableStreamDefaultReader.h in Headers */,
                                4129DF861BB5B80C00322A16 /* JSReadableStreamPrivateConstructors.h in Headers */,
                                7E4C96DD1AD4483500365A51 /* JSReadableStreamSource.h in Headers */,
                                BE913D80181EF92400DCB09E /* TrackPrivateBase.h in Headers */,
                                FFAC30FE184FB145008C4F1E /* TrailingObjects.h in Headers */,
                                516071321BD8308B00DBC4F2 /* TransactionOperation.h in Headers */,
-                               07C046C21E425022007201E7 /* AudioSampleDataSource.h in Headers */,
                                49E911C40EF86D47009D0CAF /* TransformationMatrix.h in Headers */,
                                FB484F4D171F821E00040755 /* TransformFunctions.h in Headers */,
                                49E911CE0EF86D47009D0CAF /* TransformOperation.h in Headers */,
                                51F798EF1BE880E7008AE491 /* IDBIndexInfo.cpp in Sources */,
                                51E269361DD3BD97006B6A58 /* IDBIterateCursorData.cpp in Sources */,
                                5185FC941BB4C4E80012898F /* IDBKey.cpp in Sources */,
+                               073B87661E4385AC0071C0EC /* AudioSampleBufferList.cpp in Sources */,
                                5185FC961BB4C4E80012898F /* IDBKeyData.cpp in Sources */,
                                5185FC981BB4C4E80012898F /* IDBKeyPath.cpp in Sources */,
                                5185FC9A1BB4C4E80012898F /* IDBKeyRange.cpp in Sources */,
                                5160712E1BD8307800DBC4F2 /* IDBObjectStoreInfo.cpp in Sources */,
                                5185FCA31BB4C4E80012898F /* IDBOpenDBRequest.cpp in Sources */,
                                5185FCA81BB4C4E80012898F /* IDBRequest.cpp in Sources */,
+                               073B87681E4385AC0071C0EC /* AudioSampleDataSource.cpp in Sources */,
                                514129981C6976900059E714 /* IDBRequestCompletionEvent.cpp in Sources */,
                                510A58F91BACC7F200C19282 /* IDBRequestData.cpp in Sources */,
                                5145B1091BC48E2E00E86219 /* IDBResourceIdentifier.cpp in Sources */,
                                E1284BB210449FFA00EAEB52 /* JSPageTransitionEvent.cpp in Sources */,
                                FDA15EB112B03EE1003A583A /* JSPannerNode.cpp in Sources */,
                                E51A81DF17298D7700BFCA61 /* JSPerformance.cpp in Sources */,
-                               07C046C11E425022007201E7 /* AudioSampleDataSource.cpp in Sources */,
                                CB38FD511CCF938900592A3F /* JSPerformanceEntry.cpp in Sources */,
                                CB38FD571CD21E2A00592A3F /* JSPerformanceEntryCustom.cpp in Sources */,
                                A58C59D01E382EAC0047859C /* JSPerformanceMark.cpp in Sources */,
diff --git a/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp b/Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp
new file mode 100644 (file)
index 0000000..ec94ee9
--- /dev/null
@@ -0,0 +1,340 @@
+/*
+ * 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. ``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
+ * 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 "AudioSampleBufferList.h"
+
+#if ENABLE(MEDIA_STREAM)
+
+#include "Logging.h"
+#include "VectorMath.h"
+#include <Accelerate/Accelerate.h>
+#include <AudioToolbox/AudioConverter.h>
+
+namespace WebCore {
+
+using namespace VectorMath;
+
+Ref<AudioSampleBufferList> AudioSampleBufferList::create(const CAAudioStreamDescription& format, size_t maximumSampleCount)
+{
+    return adoptRef(*new AudioSampleBufferList(format, maximumSampleCount));
+}
+
+AudioSampleBufferList::AudioSampleBufferList(const CAAudioStreamDescription& format, size_t maximumSampleCount)
+{
+    m_internalFormat = std::make_unique<CAAudioStreamDescription>(format);
+
+    m_sampleCapacity = maximumSampleCount;
+    m_sampleCount = 0;
+    m_maxBufferSizePerChannel = maximumSampleCount * format.bytesPerFrame() / format.numberOfChannelStreams();
+
+    ASSERT(format.sampleRate() >= 0);
+
+    size_t bufferSize = format.numberOfChannelStreams() * m_maxBufferSizePerChannel;
+    ASSERT(bufferSize <= SIZE_MAX);
+    if (bufferSize > SIZE_MAX)
+        return;
+
+    m_bufferListBaseSize = audioBufferListSizeForStream(format);
+    ASSERT(m_bufferListBaseSize <= SIZE_MAX);
+    if (m_bufferListBaseSize > SIZE_MAX)
+        return;
+
+    size_t allocSize = m_bufferListBaseSize + bufferSize;
+    m_bufferList = std::unique_ptr<AudioBufferList>(static_cast<AudioBufferList*>(::operator new (allocSize)));
+
+    reset();
+}
+
+AudioSampleBufferList::~AudioSampleBufferList()
+{
+    m_internalFormat = nullptr;
+    m_bufferList = nullptr;
+}
+
+void AudioSampleBufferList::setSampleCount(size_t count)
+{
+    ASSERT(count <= m_sampleCapacity);
+    if (count <= m_sampleCapacity)
+        m_sampleCount = count;
+}
+
+void AudioSampleBufferList::applyGain(AudioBufferList& bufferList, float gain, AudioStreamDescription::PCMFormat format)
+{
+    for (uint32_t i = 0; i < bufferList.mNumberBuffers; ++i) {
+        switch (format) {
+        case AudioStreamDescription::Int16: {
+            int16_t* buffer = static_cast<int16_t*>(bufferList.mBuffers[i].mData);
+            int frameCount = bufferList.mBuffers[i].mDataByteSize / sizeof(int16_t);
+            for (int i = 0; i < frameCount; i++)
+                buffer[i] *= gain;
+            break;
+        }
+        case AudioStreamDescription::Int32: {
+            int32_t* buffer = static_cast<int32_t*>(bufferList.mBuffers[i].mData);
+            int frameCount = bufferList.mBuffers[i].mDataByteSize / sizeof(int32_t);
+            for (int i = 0; i < frameCount; i++)
+                buffer[i] *= gain;
+            break;
+            break;
+        }
+        case AudioStreamDescription::Float32: {
+            float* buffer = static_cast<float*>(bufferList.mBuffers[i].mData);
+            vDSP_vsmul(buffer, 1, &gain, buffer, 1, bufferList.mBuffers[i].mDataByteSize / sizeof(float));
+            break;
+        }
+        case AudioStreamDescription::Float64: {
+            double* buffer = static_cast<double*>(bufferList.mBuffers[i].mData);
+            double gainAsDouble = gain;
+            vDSP_vsmulD(buffer, 1, &gainAsDouble, buffer, 1, bufferList.mBuffers[i].mDataByteSize / sizeof(double));
+            break;
+        }
+        case AudioStreamDescription::None:
+            ASSERT_NOT_REACHED();
+            break;
+        }
+    }
+}
+
+void AudioSampleBufferList::applyGain(float gain)
+{
+    applyGain(*m_bufferList, gain, m_internalFormat->format());
+}
+
+OSStatus AudioSampleBufferList::mixFrom(const AudioSampleBufferList& source, size_t frameCount)
+{
+    ASSERT(source.streamDescription() == streamDescription());
+
+    if (source.streamDescription() != streamDescription())
+        return kAudio_ParamError;
+
+    if (frameCount > source.sampleCount())
+        frameCount = source.sampleCount();
+
+    if (frameCount > m_sampleCapacity)
+        return kAudio_ParamError;
+
+    m_sampleCount = frameCount;
+
+    AudioBufferList& sourceBuffer = source.bufferList();
+    for (uint32_t i = 0; i < m_bufferList->mNumberBuffers; i++) {
+        switch (m_internalFormat->format()) {
+        case AudioStreamDescription::Int16: {
+            int16_t* destination = static_cast<int16_t*>(m_bufferList->mBuffers[i].mData);
+            int16_t* source = static_cast<int16_t*>(sourceBuffer.mBuffers[i].mData);
+            for (size_t i = 0; i < frameCount; i++)
+                destination[i] += source[i];
+            break;
+        }
+        case AudioStreamDescription::Int32: {
+            int32_t* destination = static_cast<int32_t*>(m_bufferList->mBuffers[i].mData);
+            vDSP_vaddi(destination, 1, reinterpret_cast<int32_t*>(sourceBuffer.mBuffers[i].mData), 1, destination, 1, frameCount);
+            break;
+        }
+        case AudioStreamDescription::Float32: {
+            float* destination = static_cast<float*>(m_bufferList->mBuffers[i].mData);
+            vDSP_vadd(destination, 1, reinterpret_cast<float*>(sourceBuffer.mBuffers[i].mData), 1, destination, 1, frameCount);
+            break;
+        }
+        case AudioStreamDescription::Float64: {
+            double* destination = static_cast<double*>(m_bufferList->mBuffers[i].mData);
+            vDSP_vaddD(destination, 1, reinterpret_cast<double*>(sourceBuffer.mBuffers[i].mData), 1, destination, 1, frameCount);
+            break;
+        }
+        case AudioStreamDescription::None:
+            ASSERT_NOT_REACHED();
+            break;
+        }
+    }
+
+    return 0;
+}
+
+OSStatus AudioSampleBufferList::copyFrom(const AudioSampleBufferList& source, size_t frameCount)
+{
+    ASSERT(source.streamDescription() == streamDescription());
+
+    if (source.streamDescription() != streamDescription())
+        return kAudio_ParamError;
+
+    if (frameCount > source.sampleCount())
+        frameCount = source.sampleCount();
+
+    if (frameCount > m_sampleCapacity)
+        return kAudio_ParamError;
+
+    m_sampleCount = frameCount;
+
+    for (uint32_t i = 0; i < m_bufferList->mNumberBuffers; i++) {
+        uint8_t* sourceData = static_cast<uint8_t*>(source.bufferList().mBuffers[i].mData);
+        uint8_t* destination = static_cast<uint8_t*>(m_bufferList->mBuffers[i].mData);
+        memcpy(destination, sourceData, frameCount * m_internalFormat->bytesPerPacket());
+    }
+
+    return 0;
+}
+
+OSStatus AudioSampleBufferList::copyTo(AudioBufferList& buffer, size_t frameCount)
+{
+    if (frameCount > m_sampleCount)
+        return kAudio_ParamError;
+    if (buffer.mNumberBuffers > m_bufferList->mNumberBuffers)
+        return kAudio_ParamError;
+
+    for (uint32_t i = 0; i < buffer.mNumberBuffers; i++) {
+        uint8_t* sourceData = static_cast<uint8_t*>(m_bufferList->mBuffers[i].mData);
+        uint8_t* destination = static_cast<uint8_t*>(buffer.mBuffers[i].mData);
+        memcpy(destination, sourceData, frameCount * m_internalFormat->bytesPerPacket());
+    }
+
+    return 0;
+}
+
+void AudioSampleBufferList::reset()
+{
+    m_sampleCount = 0;
+    m_timestamp = 0;
+    m_hostTime = -1;
+
+    uint8_t* data = reinterpret_cast<uint8_t*>(m_bufferList.get()) + m_bufferListBaseSize;
+    m_bufferList->mNumberBuffers = m_internalFormat->numberOfChannelStreams();
+    for (uint32_t i = 0; i < m_bufferList->mNumberBuffers; ++i) {
+        auto& buffer = m_bufferList->mBuffers[i];
+        buffer.mData = data;
+        buffer.mDataByteSize = m_maxBufferSizePerChannel;
+        buffer.mNumberChannels = m_internalFormat->numberOfInterleavedChannels();
+        data = data + m_maxBufferSizePerChannel;
+    }
+}
+
+void AudioSampleBufferList::zero()
+{
+    zeroABL(*m_bufferList, m_internalFormat->bytesPerPacket() * m_sampleCapacity);
+}
+
+void AudioSampleBufferList::zeroABL(AudioBufferList& buffer, size_t byteCount)
+{
+    for (uint32_t i = 0; i < buffer.mNumberBuffers; ++i)
+        memset(buffer.mBuffers[i].mData, 0, byteCount);
+}
+
+OSStatus AudioSampleBufferList::convertInput(UInt32* ioNumberDataPackets, AudioBufferList* ioData)
+{
+    if (!ioNumberDataPackets || !ioData || !m_converterInputBuffer) {
+        LOG_ERROR("AudioSampleBufferList::reconfigureInput(%p) invalid input to AudioConverterInput", this);
+        return kAudioConverterErr_UnspecifiedError;
+    }
+
+    size_t packetCount = m_converterInputBuffer->mBuffers[0].mDataByteSize / m_converterInputBytesPerPacket;
+    if (*ioNumberDataPackets > m_sampleCapacity) {
+        LOG_ERROR("AudioSampleBufferList::convertInput(%p) not enough internal storage: needed = %u, available = %lu", this, *ioNumberDataPackets, m_sampleCapacity);
+        return kAudioConverterErr_InvalidInputSize;
+    }
+
+    *ioNumberDataPackets = static_cast<UInt32>(packetCount);
+    for (uint32_t i = 0; i < ioData->mNumberBuffers; ++i) {
+        ioData->mBuffers[i].mData = m_converterInputBuffer->mBuffers[i].mData;
+        ioData->mBuffers[i].mDataByteSize = m_converterInputBuffer->mBuffers[i].mDataByteSize;
+    }
+
+    return 0;
+}
+
+OSStatus AudioSampleBufferList::audioConverterCallback(AudioConverterRef, UInt32* ioNumberDataPackets, AudioBufferList* ioData, AudioStreamPacketDescription**, void* inRefCon)
+{
+    return static_cast<AudioSampleBufferList*>(inRefCon)->convertInput(ioNumberDataPackets, ioData);
+}
+
+OSStatus AudioSampleBufferList::copyFrom(AudioBufferList& source, AudioConverterRef converter)
+{
+    reset();
+
+    AudioStreamBasicDescription inputFormat;
+    UInt32 propertyDataSize = sizeof(inputFormat);
+    AudioConverterGetProperty(converter, kAudioConverterCurrentInputStreamDescription, &propertyDataSize, &inputFormat);
+    m_converterInputBytesPerPacket = inputFormat.mBytesPerPacket;
+    m_converterInputBuffer = &source;
+
+    auto* outputData = m_bufferList.get();
+
+#if !LOG_DISABLED
+    AudioStreamBasicDescription outputFormat;
+    propertyDataSize = sizeof(outputFormat);
+    AudioConverterGetProperty(converter, kAudioConverterCurrentOutputStreamDescription, &propertyDataSize, &outputFormat);
+
+    ASSERT(outputFormat.mChannelsPerFrame == outputData->mNumberBuffers);
+    for (uint32_t i = 0; i < outputData->mNumberBuffers; ++i) {
+        ASSERT(outputData->mBuffers[i].mData);
+        ASSERT(outputData->mBuffers[i].mDataByteSize);
+    }
+#endif
+
+    UInt32 samplesConverted = static_cast<UInt32>(m_sampleCapacity);
+    OSStatus err = AudioConverterFillComplexBuffer(converter, audioConverterCallback, this, &samplesConverted, outputData, nullptr);
+    if (err) {
+        LOG_ERROR("AudioSampleBufferList::copyFrom(%p) AudioConverterFillComplexBuffer returned error %d (%.4s)", this, err, (char*)&err);
+        m_sampleCount = std::min(m_sampleCapacity, static_cast<size_t>(samplesConverted));
+        zero();
+        return err;
+    }
+
+    m_sampleCount = samplesConverted;
+    return 0;
+}
+
+OSStatus AudioSampleBufferList::copyFrom(AudioSampleBufferList& source, AudioConverterRef converter)
+{
+    return copyFrom(source.bufferList(), converter);
+}
+
+OSStatus AudioSampleBufferList::copyFrom(CARingBuffer& ringBuffer, size_t sampleCount, uint64_t startFrame, CARingBuffer::FetchMode mode)
+{
+    reset();
+    if (ringBuffer.fetch(&bufferList(), sampleCount, startFrame, mode) != CARingBuffer::Ok)
+        return kAudio_ParamError;
+
+    m_sampleCount = sampleCount;
+    return 0;
+}
+
+void AudioSampleBufferList::configureBufferListForStream(AudioBufferList& bufferList, const CAAudioStreamDescription& format, uint8_t* bufferData, size_t sampleCount)
+{
+    size_t bufferCount = format.numberOfChannelStreams();
+    size_t channelCount = format.numberOfInterleavedChannels();
+    size_t bytesPerChannel = sampleCount * format.bytesPerFrame();
+
+    bufferList.mNumberBuffers = bufferCount;
+    for (unsigned i = 0; i < bufferCount; ++i) {
+        bufferList.mBuffers[i].mNumberChannels = channelCount;
+        bufferList.mBuffers[i].mDataByteSize = bytesPerChannel;
+        bufferList.mBuffers[i].mData = bufferData;
+        if (bufferData)
+            bufferData = bufferData + bytesPerChannel;
+    }
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(MEDIA_STREAM)
diff --git a/Source/WebCore/platform/audio/mac/AudioSampleBufferList.h b/Source/WebCore/platform/audio/mac/AudioSampleBufferList.h
new file mode 100644 (file)
index 0000000..0e8e24c
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * 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. ``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
+ * 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 ENABLE(MEDIA_STREAM)
+
+#include "CARingBuffer.h"
+#include <CoreAudio/CoreAudioTypes.h>
+#include <wtf/Lock.h>
+#include <wtf/RefCounted.h>
+#include <wtf/RefPtr.h>
+
+typedef struct AudioStreamBasicDescription AudioStreamBasicDescription;
+typedef struct OpaqueAudioConverter* AudioConverterRef;
+
+namespace WebCore {
+
+class AudioSampleBufferList : public RefCounted<AudioSampleBufferList> {
+public:
+    static Ref<AudioSampleBufferList> create(const CAAudioStreamDescription&, size_t);
+
+    ~AudioSampleBufferList();
+
+    static void configureBufferListForStream(AudioBufferList&, const CAAudioStreamDescription&, uint8_t*, size_t);
+    static inline size_t audioBufferListSizeForStream(const CAAudioStreamDescription&);
+
+    static void applyGain(AudioBufferList&, float, AudioStreamDescription::PCMFormat);
+    void applyGain(float);
+
+    OSStatus copyFrom(const AudioSampleBufferList&, size_t count = SIZE_MAX);
+    OSStatus copyFrom(AudioBufferList&, AudioConverterRef);
+    OSStatus copyFrom(AudioSampleBufferList&, AudioConverterRef);
+    OSStatus copyFrom(CARingBuffer&, size_t frameCount, uint64_t startFrame, CARingBuffer::FetchMode);
+
+    OSStatus mixFrom(const AudioSampleBufferList&, size_t count = SIZE_MAX);
+
+    OSStatus copyTo(AudioBufferList&, size_t count = SIZE_MAX);
+
+    const AudioStreamBasicDescription& streamDescription() const { return m_internalFormat->streamDescription(); }
+    AudioBufferList& bufferList() const { return *m_bufferList.get(); }
+
+    uint32_t sampleCapacity() const { return m_sampleCapacity; }
+    uint32_t sampleCount() const { return m_sampleCount; }
+    void setSampleCount(size_t);
+
+    uint64_t timestamp() const { return m_timestamp; }
+    double hostTime() const { return m_hostTime; }
+    void setTimes(uint64_t time, double hostTime) { m_timestamp = time; m_hostTime = hostTime; }
+
+    void reset();
+
+    static void zeroABL(AudioBufferList&, size_t);
+    void zero();
+
+protected:
+    AudioSampleBufferList(const CAAudioStreamDescription&, size_t);
+
+    static OSStatus audioConverterCallback(AudioConverterRef, UInt32*, AudioBufferList*, AudioStreamPacketDescription**, void*);
+    OSStatus convertInput(UInt32*, AudioBufferList*);
+
+    std::unique_ptr<CAAudioStreamDescription> m_internalFormat;
+
+    AudioSampleBufferList* m_converterInputBuffer2 { nullptr };
+    AudioBufferList* m_converterInputBuffer { nullptr };
+    uint32_t m_converterInputBytesPerPacket { 0 };
+
+    uint64_t m_timestamp { 0 };
+    double m_hostTime { -1 };
+    size_t m_sampleCount { 0 };
+    size_t m_sampleCapacity { 0 };
+    size_t m_maxBufferSizePerChannel { 0 };
+    size_t m_bufferListBaseSize { 0 };
+    std::unique_ptr<AudioBufferList> m_bufferList;
+};
+
+inline size_t AudioSampleBufferList::audioBufferListSizeForStream(const CAAudioStreamDescription& description)
+{
+    return offsetof(AudioBufferList, mBuffers) + (sizeof(AudioBuffer) * std::max<uint32_t>(1, description.numberOfChannelStreams()));
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(MEDIA_STREAM)
diff --git a/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp b/Source/WebCore/platform/audio/mac/AudioSampleDataSource.cpp
new file mode 100644 (file)
index 0000000..2914cf6
--- /dev/null
@@ -0,0 +1,338 @@
+/*
+ * 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. ``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
+ * 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 "AudioSampleDataSource.h"
+
+#if ENABLE(MEDIA_STREAM)
+
+#include "CAAudioStreamDescription.h"
+#include "CARingBuffer.h"
+#include "Logging.h"
+#include "MediaTimeAVFoundation.h"
+#include <AudioToolbox/AudioConverter.h>
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#include <mutex>
+#include <syslog.h>
+#include <wtf/CurrentTime.h>
+#include <wtf/StringPrintStream.h>
+
+#include "CoreMediaSoftLink.h"
+
+namespace WebCore {
+
+using namespace JSC;
+
+Ref<AudioSampleDataSource> AudioSampleDataSource::create(size_t maximumSampleCount)
+{
+    return adoptRef(*new AudioSampleDataSource(maximumSampleCount));
+}
+
+AudioSampleDataSource::AudioSampleDataSource(size_t maximumSampleCount)
+    : m_inputSampleOffset(MediaTime::invalidTime())
+    , m_maximumSampleCount(maximumSampleCount)
+{
+}
+
+AudioSampleDataSource::~AudioSampleDataSource()
+{
+    m_inputDescription = nullptr;
+    m_outputDescription = nullptr;
+    m_ringBuffer = nullptr;
+    if (m_converter) {
+        AudioConverterDispose(m_converter);
+        m_converter = nullptr;
+    }
+}
+
+void AudioSampleDataSource::setPaused(bool paused)
+{
+    std::lock_guard<Lock> lock(m_lock);
+
+    if (paused == m_paused)
+        return;
+
+    m_transitioningFromPaused = m_paused;
+    m_paused = paused;
+}
+
+OSStatus AudioSampleDataSource::setupConverter()
+{
+    ASSERT(m_inputDescription && m_outputDescription);
+
+    if (m_converter) {
+        AudioConverterDispose(m_converter);
+        m_converter = nullptr;
+    }
+
+    if (*m_inputDescription == *m_outputDescription)
+        return 0;
+
+    OSStatus err = AudioConverterNew(&m_inputDescription->streamDescription(), &m_outputDescription->streamDescription(), &m_converter);
+    if (err)
+        LOG_ERROR("AudioSampleDataSource::setupConverter(%p) - AudioConverterNew returned error %d (%.4s)", this, err, (char*)&err);
+
+    return err;
+
+}
+
+OSStatus AudioSampleDataSource::setInputFormat(const CAAudioStreamDescription& format)
+{
+    ASSERT(format.sampleRate() >= 0);
+
+    m_inputDescription = std::make_unique<CAAudioStreamDescription>(format);
+    if (m_outputDescription)
+        return setupConverter();
+
+    return 0;
+}
+
+OSStatus AudioSampleDataSource::setOutputFormat(const CAAudioStreamDescription& format)
+{
+    ASSERT(m_inputDescription);
+    ASSERT(format.sampleRate() >= 0);
+
+    m_outputDescription = std::make_unique<CAAudioStreamDescription>(format);
+    if (!m_ringBuffer)
+        m_ringBuffer = std::make_unique<CARingBuffer>();
+
+    m_ringBuffer->allocate(format, static_cast<size_t>(m_maximumSampleCount));
+    m_scratchBuffer = AudioSampleBufferList::create(m_outputDescription->streamDescription(), m_maximumSampleCount);
+
+    return setupConverter();
+}
+
+MediaTime AudioSampleDataSource::hostTime() const
+{
+    // Based on listing #2 from Apple Technical Q&A QA1398, modified to be thread-safe.
+    static double frequency;
+    static mach_timebase_info_data_t timebaseInfo;
+    static std::once_flag initializeTimerOnceFlag;
+    std::call_once(initializeTimerOnceFlag, [] {
+        kern_return_t kr = mach_timebase_info(&timebaseInfo);
+        frequency = 1e-9 * static_cast<double>(timebaseInfo.numer) / static_cast<double>(timebaseInfo.denom);
+        ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
+        ASSERT(timebaseInfo.denom);
+    });
+
+    return MediaTime::createWithDouble(mach_absolute_time() * frequency);
+}
+
+void AudioSampleDataSource::pushSamplesInternal(AudioBufferList& bufferList, const MediaTime& presentationTime, size_t sampleCount)
+{
+    ASSERT(m_lock.isHeld());
+
+    AudioBufferList* sampleBufferList;
+    if (m_converter) {
+        m_scratchBuffer->reset();
+        OSStatus err = m_scratchBuffer->copyFrom(bufferList, m_converter);
+        if (err)
+            return;
+
+        sampleBufferList = &m_scratchBuffer->bufferList();
+    } else
+        sampleBufferList = &bufferList;
+
+    MediaTime sampleTime = presentationTime;
+    if (m_inputSampleOffset == MediaTime::invalidTime()) {
+        m_inputSampleOffset = MediaTime(1 - sampleTime.timeValue(), sampleTime.timeScale());
+        if (m_inputSampleOffset.timeScale() != sampleTime.timeScale()) {
+            // FIXME: It should be possible to do this without calling CMTimeConvertScale.
+            m_inputSampleOffset = toMediaTime(CMTimeConvertScale(toCMTime(m_inputSampleOffset), sampleTime.timeScale(), kCMTimeRoundingMethod_Default));
+        }
+        LOG(MediaCaptureSamples, "@@ pushSamples: input sample offset is %lld, m_maximumSampleCount = %zu", m_inputSampleOffset.timeValue(), m_maximumSampleCount);
+    }
+    sampleTime += m_inputSampleOffset;
+
+#if !LOG_DISABLED
+    uint64_t startFrame1 = 0;
+    uint64_t endFrame1 = 0;
+    m_ringBuffer->getCurrentFrameBounds(startFrame1, endFrame1);
+#endif
+
+    m_ringBuffer->store(sampleBufferList, sampleCount, sampleTime.timeValue());
+    m_timeStamp = sampleTime.timeValue();
+
+    LOG(MediaCaptureSamples, "@@ pushSamples: added %ld samples for time = %s (was %s), mach time = %lld", sampleCount, toString(sampleTime).utf8().data(), toString(presentationTime).utf8().data(), mach_absolute_time());
+
+#if !LOG_DISABLED
+    uint64_t startFrame2 = 0;
+    uint64_t endFrame2 = 0;
+    m_ringBuffer->getCurrentFrameBounds(startFrame2, endFrame2);
+    LOG(MediaCaptureSamples, "@@ pushSamples: buffered range was [%lld .. %lld], is [%lld .. %lld]", startFrame1, endFrame1, startFrame2, endFrame2);
+#endif
+}
+
+void AudioSampleDataSource::pushSamples(const AudioStreamBasicDescription& sampleDescription, CMSampleBufferRef sampleBuffer)
+{
+    std::lock_guard<Lock> lock(m_lock);
+
+    ASSERT_UNUSED(sampleDescription, *m_inputDescription == sampleDescription);
+    ASSERT(m_ringBuffer);
+
+    size_t bufferSize = AudioSampleBufferList::audioBufferListSizeForStream(*m_inputDescription.get());
+    uint8_t bufferData[bufferSize];
+    AudioBufferList* bufferList = reinterpret_cast<AudioBufferList*>(bufferData);
+    bufferList->mNumberBuffers = m_inputDescription->numberOfInterleavedChannels();
+
+    CMBlockBufferRef buffer = nullptr;
+    OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nullptr, bufferList, bufferSize, kCFAllocatorSystemDefault, kCFAllocatorSystemDefault, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &buffer);
+    if (err) {
+        LOG_ERROR("AudioSampleDataSource::pushSamples(%p) - CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer returned error %d (%.4s)", this, err, (char*)&err);
+        return;
+    }
+
+    pushSamplesInternal(*bufferList, toMediaTime(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)), CMSampleBufferGetNumSamples(sampleBuffer));
+}
+
+void AudioSampleDataSource::pushSamples(const AudioStreamBasicDescription& sampleDescription, const MediaTime& sampleTime, void* audioData, size_t sampleCount)
+{
+    std::unique_lock<Lock> lock(m_lock, std::try_to_lock);
+    ASSERT(*m_inputDescription == sampleDescription);
+
+    CAAudioStreamDescription description(sampleDescription);
+    size_t bufferSize = AudioSampleBufferList::audioBufferListSizeForStream(description);
+    uint8_t bufferData[bufferSize];
+    AudioBufferList* bufferList = reinterpret_cast<AudioBufferList*>(bufferData);
+
+    AudioSampleBufferList::configureBufferListForStream(*bufferList, description, reinterpret_cast<uint8_t*>(audioData), sampleCount);
+    pushSamplesInternal(*bufferList, sampleTime, sampleCount);
+}
+
+bool AudioSampleDataSource::pullSamplesInternal(AudioBufferList& buffer, size_t& sampleCount, uint64_t timeStamp, double /*hostTime*/, PullMode mode)
+{
+    ASSERT(m_lock.isHeld());
+
+    ASSERT(buffer.mNumberBuffers == m_ringBuffer->channelCount());
+    if (buffer.mNumberBuffers != m_ringBuffer->channelCount()) {
+        AudioSampleBufferList::zeroABL(buffer, sampleCount);
+        sampleCount = 0;
+        return false;
+    }
+
+    if (!m_ringBuffer || m_muted || m_inputSampleOffset == MediaTime::invalidTime()) {
+        AudioSampleBufferList::zeroABL(buffer, sampleCount);
+        sampleCount = 0;
+        return false;
+    }
+
+    uint64_t startFrame = 0;
+    uint64_t endFrame = 0;
+    m_ringBuffer->getCurrentFrameBounds(startFrame, endFrame);
+
+    if (m_transitioningFromPaused) {
+        uint64_t buffered = endFrame - m_timeStamp;
+        if (buffered < sampleCount * 2) {
+            AudioSampleBufferList::zeroABL(buffer, sampleCount);
+            sampleCount = 0;
+            return false;
+        }
+
+        const double twentyMS = .02;
+        const double tenMS = .01;
+        const double fiveMS = .005;
+        double sampleRate = m_outputDescription->sampleRate();
+        if (buffered > sampleRate * twentyMS)
+            m_outputSampleOffset = m_timeStamp - sampleRate * twentyMS;
+        else if (buffered > sampleRate * tenMS)
+            m_outputSampleOffset = m_timeStamp - sampleRate * tenMS;
+        else if (buffered > sampleRate * fiveMS)
+            m_outputSampleOffset = m_timeStamp - sampleRate * fiveMS;
+        else
+            m_outputSampleOffset = m_timeStamp;
+
+        m_transitioningFromPaused = false;
+    }
+
+    timeStamp += m_outputSampleOffset;
+
+    LOG(MediaCaptureSamples, "** pullSamples: asking for %ld samples at time = %lld (was %lld)", sampleCount, timeStamp, timeStamp - m_outputSampleOffset);
+
+    int64_t framesAvailable = sampleCount;
+    if (timeStamp < startFrame || timeStamp + sampleCount > endFrame) {
+        if (timeStamp + sampleCount < startFrame || timeStamp > endFrame)
+            framesAvailable = 0;
+        else if (timeStamp < startFrame)
+            framesAvailable = timeStamp + sampleCount - startFrame;
+        else
+            framesAvailable = timeStamp + sampleCount - endFrame;
+
+        LOG(MediaCaptureSamples, "** pullSamplesInternal: sample %lld is not completely in range [%lld .. %lld], returning %lld frames", timeStamp, startFrame, endFrame, framesAvailable);
+
+        if (!framesAvailable) {
+            AudioSampleBufferList::zeroABL(buffer, sampleCount);
+            return false;
+        }
+    }
+
+    if (m_volume >= .95) {
+        m_ringBuffer->fetch(&buffer, sampleCount, timeStamp, mode == Copy ? CARingBuffer::Copy : CARingBuffer::Mix);
+        return true;
+    }
+
+    if (m_scratchBuffer->copyFrom(*m_ringBuffer.get(), sampleCount, timeStamp, mode == Copy ? CARingBuffer::Copy : CARingBuffer::Mix)) {
+        AudioSampleBufferList::zeroABL(buffer, sampleCount);
+        return false;
+    }
+
+    m_scratchBuffer->applyGain(m_volume);
+    if (m_scratchBuffer->copyTo(buffer, sampleCount))
+        AudioSampleBufferList::zeroABL(buffer, sampleCount);
+
+    return true;
+}
+
+bool AudioSampleDataSource::pullSamples(AudioBufferList& buffer, size_t sampleCount, uint64_t timeStamp, double hostTime, PullMode mode)
+{
+    std::unique_lock<Lock> lock(m_lock, std::try_to_lock);
+    if (!lock.owns_lock() || !m_ringBuffer) {
+        AudioSampleBufferList::zeroABL(buffer, sampleCount);
+        return false;
+    }
+
+    return pullSamplesInternal(buffer, sampleCount, timeStamp, hostTime, mode);
+}
+
+bool AudioSampleDataSource::pullSamples(AudioSampleBufferList& buffer, size_t sampleCount, uint64_t timeStamp, double hostTime, PullMode mode)
+{
+    std::unique_lock<Lock> lock(m_lock, std::try_to_lock);
+    if (!lock.owns_lock() || !m_ringBuffer) {
+        buffer.zero();
+        return false;
+    }
+
+    if (!pullSamplesInternal(buffer.bufferList(), sampleCount, timeStamp, hostTime, mode))
+        return false;
+
+    buffer.setTimes(timeStamp, hostTime);
+    buffer.setSampleCount(sampleCount);
+
+    return true;
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(MEDIA_STREAM)
diff --git a/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h b/Source/WebCore/platform/audio/mac/AudioSampleDataSource.h
new file mode 100644 (file)
index 0000000..decdeee
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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. ``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
+ * 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 ENABLE(MEDIA_STREAM)
+
+#include "AudioSampleBufferList.h"
+#include <CoreAudio/CoreAudioTypes.h>
+#include <wtf/Lock.h>
+#include <wtf/MediaTime.h>
+#include <wtf/RefCounted.h>
+#include <wtf/RefPtr.h>
+#include <wtf/text/WTFString.h>
+
+typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef;
+typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
+
+namespace WebCore {
+
+class CAAudioStreamDescription;
+class CARingBuffer;
+
+class AudioSampleDataSource : public RefCounted<AudioSampleDataSource> {
+public:
+    static Ref<AudioSampleDataSource> create(size_t);
+
+    ~AudioSampleDataSource();
+
+    OSStatus setInputFormat(const CAAudioStreamDescription&);
+    OSStatus setOutputFormat(const CAAudioStreamDescription&);
+
+    void pushSamples(const AudioStreamBasicDescription&, const MediaTime&, void*, size_t);
+    void pushSamples(const AudioStreamBasicDescription&, CMSampleBufferRef);
+
+    enum PullMode { Copy, Mix };
+    bool pullSamples(AudioSampleBufferList&, size_t, uint64_t, double, PullMode);
+    bool pullSamples(AudioBufferList&, size_t, uint64_t, double, PullMode);
+
+    void setPaused(bool);
+
+    void setVolume(float volume) { m_volume = volume; }
+    float volume() const { return m_volume; }
+
+    void setMuted(bool muted) { m_muted = muted; }
+    bool muted() const { return m_muted; }
+
+protected:
+    AudioSampleDataSource(size_t);
+
+    OSStatus setupConverter();
+    bool pullSamplesInternal(AudioBufferList&, size_t&, uint64_t, double, PullMode);
+
+    void pushSamplesInternal(AudioBufferList&, const MediaTime&, size_t frameCount);
+
+    std::unique_ptr<CAAudioStreamDescription> m_inputDescription;
+    std::unique_ptr<CAAudioStreamDescription> m_outputDescription;
+
+    MediaTime hostTime() const;
+
+    uint64_t m_timeStamp { 0 };
+    double m_hostTime { -1 };
+
+    MediaTime m_inputSampleOffset;
+    uint64_t m_outputSampleOffset { 0 };
+
+    AudioConverterRef m_converter;
+    RefPtr<AudioSampleBufferList> m_scratchBuffer;
+
+    std::unique_ptr<CARingBuffer> m_ringBuffer;
+    size_t m_maximumSampleCount { 0 };
+
+    Lock m_lock;
+    float m_volume { 1.0 };
+    bool m_muted { false };
+    bool m_paused { true };
+    bool m_transitioningFromPaused { true };
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(MEDIA_STREAM)