Commit 7d5f2347 authored by Karl Tomlinson's avatar Karl Tomlinson
Browse files

b=923301 use MediaStreamGraph to dispatch PlayingRefChangeHandlers r=roc

This reduces the number of events dispatched to the main thread
and ensures these run in order with other pending updates.

--HG--
extra : transplant_source : %5E%235%DEz%EB%7D%C6%C7%F8%AF%A3%84%1F%F9%5B%87D%B3%13
parent 7fd355fc
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -1043,7 +1043,8 @@ public:
   * Media graph thread only.
   * Dispatches a runnable that will run on the main thread after all
   * main-thread stream state has been next updated.
   * Should only be called during MediaStreamListener callbacks.
   * Should only be called during MediaStreamListener callbacks or during
   * ProcessedMediaStream::ProduceOutput().
   */
  void DispatchToMainThreadAfterStreamStateUpdate(already_AddRefed<nsIRunnable> aRunnable)
  {
+2 −1
Original line number Diff line number Diff line
@@ -122,7 +122,8 @@ public:
          mLeftOverData = INT32_MIN;
          nsRefPtr<PlayingRefChanged> refchanged =
            new PlayingRefChanged(aStream, PlayingRefChanged::RELEASE);
          NS_DispatchToMainThread(refchanged);
          aStream->Graph()->
            DispatchToMainThreadAfterStreamStateUpdate(refchanged.forget());
        }
        aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
        return;
+2 −1
Original line number Diff line number Diff line
@@ -88,7 +88,8 @@ public:
      if (mLeftOverData <= 0) {
        nsRefPtr<PlayingRefChanged> refchanged =
          new PlayingRefChanged(aStream, PlayingRefChanged::ADDREF);
        NS_DispatchToMainThread(refchanged);
        aStream->Graph()->
          DispatchToMainThreadAfterStreamStateUpdate(refchanged.forget());
      }
      mLeftOverData = mProcessor.MaxDelayFrames();
    } else if (mLeftOverData > 0) {
+1 −0
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ support-files =
[test_delayNodeCycles.html]
[test_delayNodeSmallMaxDelay.html]
[test_delayNodeTailIncrease.html]
[test_delayNodeTailWithDisconnect.html]
[test_delayNodeTailWithGain.html]
[test_delayNodeTailWithReconnect.html]
[test_delayNodeWithGain.html]
+99 −0
Original line number Diff line number Diff line
<!DOCTYPE HTML>
<html>
<head>
  <title>Test tail time lifetime of DelayNode after input is disconnected</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="webaudio.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

// Web Audio doesn't provide a means to precisely time disconnect()s but we
// can test that the output of delay nodes matches the output from their
// sources before they are disconnected.

SimpleTest.waitForExplicitFinish();

const signalLength = 128;
const bufferSize = 4096;
const sourceCount = bufferSize / signalLength;
// Delay should be long enough to allow CC to run
var delayBufferCount = 20;
const delayLength = delayBufferCount * bufferSize;

var sourceOutput = new Float32Array(bufferSize);
var delayOutputCount = 0;
var sources = [];

function onDelayOutput(e) {
  if (delayOutputCount < delayBufferCount) {
    delayOutputCount++;
    return;
  }

  compareBuffers(e.inputBuffer.getChannelData(0), sourceOutput);
  e.target.onaudioprocess = null;
  SimpleTest.finish();
}

function onSourceOutput(e) {
  // Record the first buffer
  e.inputBuffer.copyFromChannel(sourceOutput, 0);
  e.target.onaudioprocess = null;
}

function disconnectSources() {
  dump("disconnecting\n")
  for (var i = 0; i < sourceCount; ++i) {
    sources[i].disconnect();
  }

  SpecialPowers.forceGC();
  SpecialPowers.forceCC();
  dump("forced GC\n");
}

function startTest() {
  var ctx = new AudioContext();

  var sourceProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
  sourceProcessor.onaudioprocess = onSourceOutput;
  // Keep audioprocess events going after source disconnect.
  sourceProcessor.connect(ctx.destination);

  var delayProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
  delayProcessor.onaudioprocess = onDelayOutput;
  // Work around bug 916387.
  delayProcessor.connect(ctx.destination);

  var delayDuration = delayLength / ctx.sampleRate;
  for (var i = 0; i < sourceCount; ++i) {
    var delay = ctx.createDelay(delayDuration);
    delay.delayTime.value = delayDuration;
    delay.connect(delayProcessor);

    var source = ctx.createOscillator();
    source.frequency.value = 440 + 10 * i
    source.start(i * signalLength / ctx.sampleRate);
    source.stop((i + 1) * signalLength / ctx.sampleRate);
    source.connect(delay);
    source.connect(sourceProcessor);

    sources[i] = source;
  }

  // Assuming the above Web Audio operations have already scheduled an event
  // to run in stable state and start the graph thread, schedule a subsequent
  // event to disconnect the sources, which will remove main thread connection
  // references before it knows the graph thread has started using the source
  // streams.
  SimpleTest.executeSoon(disconnectSources);
};

startTest();
</script>
</pre>
</body>
</html>