<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>251091</bug_id>
          
          <creation_ts>2023-01-24 09:08:51 -0800</creation_ts>
          <short_desc>Distorted audio after getUserMedia when playing with AudioWorkletNode</short_desc>
          <delta_ts>2023-02-07 09:49:40 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Audio</component>
          <version>Safari 16</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>iOS 16</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Major</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>235317</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="goldwaving">webkit</reporter>
          <assigned_to name="Chris Dumez">cdumez</assigned_to>
          <cc>cdumez</cc>
    
    <cc>eric.carlson</cc>
    
    <cc>jer.noble</cc>
    
    <cc>tobias.hegemann</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>youennf</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1927834</commentid>
    <comment_count>0</comment_count>
    <who name="goldwaving">webkit</who>
    <bug_when>2023-01-24 09:08:51 -0800</bug_when>
    <thetext>If audio is played with a connected AudioWorkletNode after calling mediaDevices.getUserMedia, audio is badly distorted.

A demonstration of the problem is given here: https://goldwave.com/audio.html

Choose the Get Media button, then choose the Play button.  Audio is badly distorted.  Reload the page, choose the Play button, audio is clear.

This issue occurs on iOS devices and MacOS all fully updated (Safari 16.3)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1929775</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-01-31 09:09:18 -0800</bug_when>
    <thetext>&lt;rdar://problem/104870451&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1930866</commentid>
    <comment_count>2</comment_count>
      <attachid>464823</attachid>
    <who name="Tobias Hegemann">tobias.hegemann</who>
    <bug_when>2023-02-03 07:43:38 -0800</bug_when>
    <thetext>Created attachment 464823
Simple test to recreate the audio worklet bug on iOS</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1930868</commentid>
    <comment_count>3</comment_count>
    <who name="Tobias Hegemann">tobias.hegemann</who>
    <bug_when>2023-02-03 07:50:27 -0800</bug_when>
    <thetext>We also encountered this issue on iOS 16.2

It affects the whole web audio context or its nodes, e.g. OscillatorNode, AudioBufferSourceNode etc.

It can be reproduced when using the first AudioWorklet inside an audio context.

I&apos;ve attached a simple test to reproduce and deployed it also on gh:

https://delude88.github.io/ios-audioworklet-sample-rate/

Be aware: It is playing sinus waves at full volume.

Besides: When closing and recreating another audio context, the issue is gone (until again the first AudioWorklet is used)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931388</commentid>
    <comment_count>4</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2023-02-06 08:47:13 -0800</bug_when>
    <thetext>Looking at https://goldwave.com/audio.html, the issue we experience is that the rendering quantum becomes 960 frames instead of 128 frames, which our code doesn&apos;t deal with well.

Normally, as soon as WebAudio is in use, we request that CoreAudio uses a rendering quantum of 128. However, it is not working here and that&apos;s causing the issue.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931395</commentid>
    <comment_count>5</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2023-02-06 09:14:54 -0800</bug_when>
    <thetext>It is because of this logic:
```
void RemoteAudioSessionProxyManager::updatePreferredBufferSizeForProcess()
{
#if ENABLE(MEDIA_STREAM)
    if (CoreAudioCaptureSourceFactory::singleton().isAudioCaptureUnitRunning()) {
        CoreAudioCaptureSourceFactory::singleton().whenAudioCaptureUnitIsNotRunning([weakThis = WeakPtr { *this }] {
            if (weakThis)
                weakThis-&gt;updatePreferredBufferSizeForProcess();
        });
        return;
    }
#endif
   // ...
```

If we&apos;re capturing (which we are here since we called getUserMedia), then we defer the setting of the preferred buffer size (128) until we&apos;re done capturing. Since capturing is ongoing, we just keep using 960, which breaks Web Audio.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931397</commentid>
    <comment_count>6</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2023-02-06 09:15:34 -0800</bug_when>
    <thetext>(In reply to Chris Dumez from comment #5)
&gt; It is because of this logic:
&gt; ```
&gt; void RemoteAudioSessionProxyManager::updatePreferredBufferSizeForProcess()
&gt; {
&gt; #if ENABLE(MEDIA_STREAM)
&gt;     if
&gt; (CoreAudioCaptureSourceFactory::singleton().isAudioCaptureUnitRunning()) {
&gt;        
&gt; CoreAudioCaptureSourceFactory::singleton().
&gt; whenAudioCaptureUnitIsNotRunning([weakThis = WeakPtr { *this }] {
&gt;             if (weakThis)
&gt;                 weakThis-&gt;updatePreferredBufferSizeForProcess();
&gt;         });
&gt;         return;
&gt;     }
&gt; #endif
&gt;    // ...
&gt; ```
&gt; 
&gt; If we&apos;re capturing (which we are here since we called getUserMedia), then we
&gt; defer the setting of the preferred buffer size (128) until we&apos;re done
&gt; capturing. Since capturing is ongoing, we just keep using 960, which breaks
&gt; Web Audio.

This is a regression from Youenn&apos;s Bug 235317.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931484</commentid>
    <comment_count>7</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2023-02-06 13:58:26 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/9711</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931756</commentid>
    <comment_count>8</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-02-07 09:49:36 -0800</bug_when>
    <thetext>Committed 259964@main (87395a602807): &lt;https://commits.webkit.org/259964@main&gt;

Reviewed commits have been landed. Closing PR #9711 and removing active labels.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>464823</attachid>
            <date>2023-02-03 07:43:38 -0800</date>
            <delta_ts>2023-02-03 07:43:38 -0800</delta_ts>
            <desc>Simple test to recreate the audio worklet bug on iOS</desc>
            <filename>sample-rate.html</filename>
            <type>text/html</type>
            <size>2829</size>
            <attacher name="Tobias Hegemann">tobias.hegemann</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29u
dGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFsLXNjYWxlPTEsIG1heGltdW0tc2NhbGU9
MSI+CiAgPHNjcmlwdCB0eXBlPSd3b3JrbGV0Jz4KICByZWdpc3RlclByb2Nlc3NvcignYW55LXdv
cmtsZXQnLCBjbGFzcyBBbnlXb3JrbGV0IGV4dGVuZHMgQXVkaW9Xb3JrbGV0UHJvY2Vzc29yIHsK
ICAgIGNvbnN0cnVjdG9yKCkgewogICAgICBzdXBlcigpOwogICAgICB0aGlzLnBoYXNlID0gMDsK
ICAgIH0KICAgIHByb2Nlc3MoaW5wdXQsIG91dHB1dCwgcGFyYW1ldGVycykgewogICAgICBjb25z
dCBjaGFubmVsID0gb3V0cHV0WzBdWzBdOwogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGNoYW5u
ZWwubGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGNoYW5uZWxbaV0gPSBNYXRoLnNpbih0aGlzLnBo
YXNlKTsKICAgICAgICAgIHRoaXMucGhhc2UgKz0gMC4xOwogICAgICB9CiAgICAgIHJldHVybiB0
cnVlOw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GV0LmNvbm5lY3QoYXVkaW9Db250ZXh0LmRlc3RpbmF0aW9uKTsK
ICAgICAgICByZXR1cm4gbmV3IFByb21pc2UoKHJlc29sdmUpID0+IHsKICAgICAgICAgIHNldFRp
bWVvdXQoKCkgPT4gewogICAgICAgICAgICB3b3JrbGV0LmRpc2Nvbm5lY3QoKTsKICAgICAgICAg
ICAgcmVzb2x2ZSgpOwogICAgICAgICAgfSwgMjAwMCk7CiAgICAgICAgfSkKICAgICAgfSk7CiAg
ICB9CiAgICBmdW5jdGlvbiBydW4oKSB7CiAgICAgIGNvbnN0IGF1ZGlvQ29udGV4dCA9IG5ldyBB
dWRpb0NvbnRleHQoKTsKICAgICAgYXVkaW9Db250ZXh0LnJlc3VtZSgpCiAgICAgICAgLnRoZW4o
KCkgPT4gewogICAgICAgICAgY29uc29sZS5sb2coIlBsYXlpbmcgb3NjaWxsYXRvciBmb3IgMiBz
ZWNvbmRzIik7CiAgICAgICAgICByZXR1cm4gcGxheU9zY2lsbGF0b3IoYXVkaW9Db250ZXh0KTsK
ICAgICAgICB9KQogICAgICAgIC50aGVuKCgpID0+IHsKICAgICAgICAgIGNvbnNvbGUubG9nKCJM
b2FkIGFuZCBwbGF5IHdvcmtsZXQgZm9yIDIgc2Vjb25kcyIpOwogICAgICAgICAgcmV0dXJuIHBs
YXlXb3JrbGV0KGF1ZGlvQ29udGV4dCk7CiAgICAgICAgfSkKICAgICAgICAudGhlbigoKSA9PiB7
CiAgICAgICAgICBjb25zb2xlLmxvZygiUGxheWluZyBvc2NpbGxhdG9yIGFnYWluIGZvciAyIHNl
Y29uZHMiKTsKICAgICAgICAgIHJldHVybiBwbGF5T3NjaWxsYXRvcihhdWRpb0NvbnRleHQpOwog
ICAgICAgIH0pCiAgICAgICAgLnRoZW4oKCkgPT4gewogICAgICAgICAgY29uc29sZS5sb2coIkZp
bmlzaGVkLCBjbGVhbmluZyB1cCIpOwogICAgICAgICAgYXVkaW9Db250ZXh0LmNsb3NlKCk7CiAg
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