[ Mac WK2 ] Layout Test fast/mediastream/MediaStreamTrack-getSettings.html is a flaky failure
https://bugs.webkit.org/show_bug.cgi?id=196400
<rdar://problem/49567579>

Reviewed by Youenn Fablet.

Source/WebCore:

No new tests, this fixes a broken test.

* platform/mediastream/mac/MockRealtimeAudioSourceMac.h:
* platform/mediastream/mac/MockRealtimeAudioSourceMac.mm:
(WebCore::MockRealtimeAudioSourceMac::reconfigure): Reconfigure buffers as well.
(WebCore::MockRealtimeAudioSourceMac::render): Call reconfigure if the buffer isn't
configured correctly.
(WebCore::MockRealtimeAudioSourceMac::settingsDidChange): Call reconfigure.

* platform/mock/MockRealtimeAudioSource.cpp:
(WebCore::MockRealtimeAudioSource::MockRealtimeAudioSource): Set sample rate to default.

LayoutTests:

* fast/mediastream/MediaStreamTrack-getSettings.html: Cleanup test.
* platform/mac-wk2/TestExpectations: Unskip test.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@250918 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 5e9793e..ddbecd5 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,14 @@
+2019-10-09  Eric Carlson  <eric.carlson@apple.com>
+
+        [ Mac WK2 ] Layout Test fast/mediastream/MediaStreamTrack-getSettings.html is a flaky failure
+        https://bugs.webkit.org/show_bug.cgi?id=196400
+        <rdar://problem/49567579>
+
+        Reviewed by Youenn Fablet.
+
+        * fast/mediastream/MediaStreamTrack-getSettings.html: Cleanup test.
+        * platform/mac-wk2/TestExpectations: Unskip test.
+
 2019-10-09  Dean Jackson  <dino@apple.com>
 
         Layout Test webgl/2.0.0/conformance/glsl/misc/shaders-with-invariance.html is failing since ANGLE roll
diff --git a/LayoutTests/fast/mediastream/MediaStreamTrack-getSettings.html b/LayoutTests/fast/mediastream/MediaStreamTrack-getSettings.html
index b35fbd3..d86458f 100644
--- a/LayoutTests/fast/mediastream/MediaStreamTrack-getSettings.html
+++ b/LayoutTests/fast/mediastream/MediaStreamTrack-getSettings.html
@@ -4,8 +4,7 @@
         <script src="../../resources/js-test-pre.js"></script>
         <script src="./resources/getUserMedia-helper.js"></script>
         <script>
-            var mediaStream;
-            var track;
+            let track;
 
             function limitPrecision(value, precision)
             {
@@ -14,19 +13,24 @@
                 return value;
             }
             
+            function forEachNativeProperty(list, func)
+            {
+                for (let property in list) {
+                    if (list.hasOwnProperty(property, list[property]) || list.__proto__.hasOwnProperty(property))
+                        func(property, list[property]);
+                }
+            }
+            
             function listTrackSettings(track)
             {
                 debug(`${track.kind} track settings:`);
-                settings = track.getSettings();
-                for (var property in settings) {
-                    if (settings.hasOwnProperty(property) || settings.__proto__.hasOwnProperty(property)) {
-                        if (property == "deviceId")
-                            value = "&lt;UUID>";
-                        else
-                            value = limitPrecision(settings[property], 3);
-                        debug(`  settings.${property} = ${value}`);
-                    }
-                }
+                forEachNativeProperty(track.getSettings(), (property, value) => {
+                    if (property == "deviceId")
+                        value = "&lt;UUID>";
+                    else
+                        value = limitPrecision(value, 3);
+                    debug(`  settings.${property} = ${value}`);
+                });
 
                 debug("");
             }
@@ -34,33 +38,23 @@
             function checkTrackSettings(t)
             {
                 track = t;
-
-                capabilities = track.getCapabilities();
-                settings = track.getSettings();
-                for (var property in settings) {
-                    if (settings.hasOwnProperty(property) || settings.__proto__.hasOwnProperty(property)) {
-                        shouldBeTrue(`"${property}" in track.getCapabilities()`);
-                    }
-                }
+                forEachNativeProperty(track.getSettings(), (property, value) => {
+                    shouldBeTrue(`"${property}" in track.getCapabilities()`);
+                });
             }
 
-            function gotStream(stream)
-            {
-                mediaStream = stream;
-
-                listTrackSettings(mediaStream.getVideoTracks()[0]);
-                listTrackSettings(mediaStream.getAudioTracks()[0]);
-
-                debug('According to the spec: "[every setting] MUST be a member of the set defined for that property by getCapabilities()"<br>');
-                checkTrackSettings(mediaStream.getVideoTracks()[0]);
-                checkTrackSettings(mediaStream.getAudioTracks()[0]);
-                finishJSTest();
-            }
-
-            function start()
+            async function start()
             {
                 description("Tests MediaStreamTrack.getSettings.");
-                getUserMedia("allow", {audio:true, video:true}, gotStream);
+                let stream = await navigator.mediaDevices.getUserMedia({audio:true, video:true});
+
+                listTrackSettings(stream.getVideoTracks()[0]);
+                listTrackSettings(stream.getAudioTracks()[0]);
+
+                debug('According to the spec: "[every setting] MUST be a member of the set defined for that property by getCapabilities()"<br>');
+                checkTrackSettings(stream.getVideoTracks()[0]);
+                checkTrackSettings(stream.getAudioTracks()[0]);
+                finishJSTest();
             }
 
             window.jsTestIsAsync = true;
diff --git a/LayoutTests/platform/mac-wk2/TestExpectations b/LayoutTests/platform/mac-wk2/TestExpectations
index c4f7f6d..88b0756 100644
--- a/LayoutTests/platform/mac-wk2/TestExpectations
+++ b/LayoutTests/platform/mac-wk2/TestExpectations
@@ -907,8 +907,6 @@
 
 webkit.org/b/194916 fast/mediastream/MediaStream-video-element.html [ Pass Failure ]
 
-webkit.org/b/196400 fast/mediastream/MediaStreamTrack-getSettings.html [ Pass Failure ]
-
 webkit.org/b/196403 imported/w3c/web-platform-tests/mediacapture-record/MediaRecorder-stop.html [ Pass Failure ]
 
 webkit.org/b/187391 accessibility/mac/async-increment-decrement-action.html [ Skip ]
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index cbd5658..7477731 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,23 @@
+2019-10-09  Eric Carlson  <eric.carlson@apple.com>
+
+        [ Mac WK2 ] Layout Test fast/mediastream/MediaStreamTrack-getSettings.html is a flaky failure
+        https://bugs.webkit.org/show_bug.cgi?id=196400
+        <rdar://problem/49567579>
+
+        Reviewed by Youenn Fablet.
+
+        No new tests, this fixes a broken test.
+
+        * platform/mediastream/mac/MockRealtimeAudioSourceMac.h:
+        * platform/mediastream/mac/MockRealtimeAudioSourceMac.mm:
+        (WebCore::MockRealtimeAudioSourceMac::reconfigure): Reconfigure buffers as well.
+        (WebCore::MockRealtimeAudioSourceMac::render): Call reconfigure if the buffer isn't
+        configured correctly.
+        (WebCore::MockRealtimeAudioSourceMac::settingsDidChange): Call reconfigure.
+
+        * platform/mock/MockRealtimeAudioSource.cpp:
+        (WebCore::MockRealtimeAudioSource::MockRealtimeAudioSource): Set sample rate to default.
+
 2019-10-09  Chris Dumez  <cdumez@apple.com>
 
         Unreviewed, address Darin's post-landing comments for r250912.
diff --git a/Source/WebCore/platform/mediastream/mac/MockRealtimeAudioSourceMac.mm b/Source/WebCore/platform/mediastream/mac/MockRealtimeAudioSourceMac.mm
index 611ac16..07d6cd45 100644
--- a/Source/WebCore/platform/mediastream/mac/MockRealtimeAudioSourceMac.mm
+++ b/Source/WebCore/platform/mediastream/mac/MockRealtimeAudioSourceMac.mm
@@ -123,7 +123,11 @@
 void MockRealtimeAudioSourceMac::reconfigure()
 {
     ASSERT(!isMainThread());
-    m_maximiumFrameCount = WTF::roundUpToPowerOfTwo(renderInterval().seconds() * sampleRate() * 2);
+
+    auto rate = sampleRate();
+    ASSERT(rate);
+
+    m_maximiumFrameCount = WTF::roundUpToPowerOfTwo(renderInterval().seconds() * rate * 2);
     ASSERT(m_maximiumFrameCount);
 
     const int bytesPerFloat = sizeof(Float32);
@@ -132,19 +136,30 @@
     const bool isFloat = true;
     const bool isBigEndian = false;
     const bool isNonInterleaved = true;
-    FillOutASBDForLPCM(m_streamFormat, sampleRate(), channelCount, bitsPerByte * bytesPerFloat, bitsPerByte * bytesPerFloat, isFloat, isBigEndian, isNonInterleaved);
+    FillOutASBDForLPCM(m_streamFormat, rate, channelCount, bitsPerByte * bytesPerFloat, bitsPerByte * bytesPerFloat, isFloat, isBigEndian, isNonInterleaved);
 
     m_audioBufferList = makeUnique<WebAudioBufferList>(m_streamFormat, m_streamFormat.mBytesPerFrame * m_maximiumFrameCount);
 
     CMFormatDescriptionRef formatDescription;
     CMAudioFormatDescriptionCreate(NULL, &m_streamFormat, 0, NULL, 0, NULL, NULL, &formatDescription);
     m_formatDescription = adoptCF(formatDescription);
+
+    size_t sampleCount = 2 * rate;
+    m_bipBopBuffer.resize(sampleCount);
+    m_bipBopBuffer.fill(0);
+
+    size_t bipBopSampleCount = ceil(BipBopDuration * rate);
+    size_t bipStart = 0;
+    size_t bopStart = rate;
+
+    addHum(BipBopVolume, BipFrequency, rate, 0, m_bipBopBuffer.data() + bipStart, bipBopSampleCount);
+    addHum(BipBopVolume, BopFrequency, rate, 0, m_bipBopBuffer.data() + bopStart, bipBopSampleCount);
 }
 
 void MockRealtimeAudioSourceMac::render(Seconds delta)
 {
     ASSERT(!isMainThread());
-    if (!m_audioBufferList)
+    if (!m_audioBufferList || !m_bipBopBuffer.size())
         reconfigure();
 
     uint32_t totalFrameCount = alignTo16Bytes(delta.seconds() * sampleRate());
@@ -173,21 +188,7 @@
 {
     if (settings.contains(RealtimeMediaSourceSettings::Flag::SampleRate)) {
         m_workQueue->dispatch([this, protectedThis = makeRef(*this)] {
-            m_formatDescription = nullptr;
-            m_audioBufferList = nullptr;
-
-            auto rate = sampleRate();
-            size_t sampleCount = 2 * rate;
-
-            m_bipBopBuffer.grow(sampleCount);
-            m_bipBopBuffer.fill(0);
-
-            size_t bipBopSampleCount = ceil(BipBopDuration * rate);
-            size_t bipStart = 0;
-            size_t bopStart = rate;
-
-            addHum(BipBopVolume, BipFrequency, rate, 0, m_bipBopBuffer.data() + bipStart, bipBopSampleCount);
-            addHum(BipBopVolume, BopFrequency, rate, 0, m_bipBopBuffer.data() + bopStart, bipBopSampleCount);
+            reconfigure();
         });
     }
 
diff --git a/Source/WebCore/platform/mock/MockRealtimeAudioSource.cpp b/Source/WebCore/platform/mock/MockRealtimeAudioSource.cpp
index 83a5632..e03a652 100644
--- a/Source/WebCore/platform/mock/MockRealtimeAudioSource.cpp
+++ b/Source/WebCore/platform/mock/MockRealtimeAudioSource.cpp
@@ -68,6 +68,8 @@
     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(persistentID());
     ASSERT(device);
     m_device = *device;
+
+    setSampleRate(WTF::get<MockMicrophoneProperties>(m_device.properties).defaultSampleRate);
 }
 
 MockRealtimeAudioSource::~MockRealtimeAudioSource()