author Karl Tomlinson <>
Thu, 13 Aug 2015 16:13:34 +1200
changeset 259222 76997b5a67b80c2d06e564d4b57445dda3348f38
parent 259221 6f427969796d8d3a1e2c680a00759957b0e32fe1
child 259223 81b4ff5388418014f6a4f51f6060d5d116b9905d
permissions -rw-r--r--
bug 1197043 use flags to distinguish between external streams and events r=padenot MediaStreamAudioDestinationNode does not need any main thread events because mDOMStream provides GetCurrentTime to consumers. MediaRecoder also does not use main thread current time.

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at */


#include "MediaStreamGraph.h"
#include "mozilla/dom/AudioNodeBinding.h"
#include "AudioSegment.h"

namespace mozilla {

namespace dom {
struct ThreeDPoint;
class AudioParamTimeline;
class AudioContext;
} // namespace dom

class ThreadSharedFloatArrayBufferList;
class AudioNodeEngine;

 * An AudioNodeStream produces one audio track with ID AUDIO_TRACK.
 * The start time of the AudioTrack is aligned to the start time of the
 * AudioContext's destination node stream, plus some multiple of BLOCK_SIZE
 * samples.
 * An AudioNodeStream has an AudioNodeEngine plugged into it that does the
 * actual audio processing. AudioNodeStream contains the glue code that
 * integrates audio processing with the MediaStreamGraph.
class AudioNodeStream : public ProcessedMediaStream
  typedef dom::ChannelCountMode ChannelCountMode;
  typedef dom::ChannelInterpretation ChannelInterpretation;

  typedef mozilla::dom::AudioContext AudioContext;

  enum { AUDIO_TRACK = 1 };

  typedef nsAutoTArray<AudioChunk, 1> OutputChunks;

  // Flags re main thread updates and stream output.
  typedef unsigned Flags;
  enum : Flags {
    // Internal AudioNodeStreams can only pass their output to another
    // AudioNode, whereas external AudioNodeStreams can pass their output
    // to other ProcessedMediaStreams or hardware audio output.
    EXTERNAL_OUTPUT = 1U << 2,
   * Create a stream that will process audio for an AudioNode.
   * Takes ownership of aEngine.
  static already_AddRefed<AudioNodeStream>
  Create(MediaStreamGraph* aGraph, AudioNodeEngine* aEngine, Flags aKind);

   * Transfers ownership of aEngine to the new AudioNodeStream.
  AudioNodeStream(AudioNodeEngine* aEngine,
                  Flags aFlags,
                  TrackRate aSampleRate,
                  AudioContext::AudioContextId aContextId);


  // Control API
   * Sets a parameter that's a time relative to some stream's played time.
   * This time is converted to a time relative to this stream when it's set.
  void SetStreamTimeParameter(uint32_t aIndex, AudioContext* aContext,
                              double aStreamTime);
  void SetDoubleParameter(uint32_t aIndex, double aValue);
  void SetInt32Parameter(uint32_t aIndex, int32_t aValue);
  void SetTimelineParameter(uint32_t aIndex, const dom::AudioParamTimeline& aValue);
  void SetThreeDPointParameter(uint32_t aIndex, const dom::ThreeDPoint& aValue);
  void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList>&& aBuffer);
  // This consumes the contents of aData.  aData will be emptied after this returns.
  void SetRawArrayData(nsTArray<float>& aData);
  void SetChannelMixingParameters(uint32_t aNumberOfChannels,
                                  ChannelCountMode aChannelCountMoe,
                                  ChannelInterpretation aChannelInterpretation);
  void SetPassThrough(bool aPassThrough);
  ChannelInterpretation GetChannelInterpretation()
    return mChannelInterpretation;

  void SetAudioParamHelperStream()
    MOZ_ASSERT(!mAudioParamStream, "Can only do this once");
    mAudioParamStream = true;

  virtual AudioNodeStream* AsAudioNodeStream() override { return this; }

  // Graph thread only
  void SetStreamTimeParameterImpl(uint32_t aIndex, MediaStream* aRelativeToStream,
                                  double aStreamTime);
  void SetChannelMixingParametersImpl(uint32_t aNumberOfChannels,
                                      ChannelCountMode aChannelCountMoe,
                                      ChannelInterpretation aChannelInterpretation);
  virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) override;
   * Produce the next block of output, before input is provided.
   * ProcessInput() will be called later, and it then should not change
   * the output.  This is used only for DelayNodeEngine in a feedback loop.
  void ProduceOutputBeforeInput(GraphTime aFrom);
  StreamTime GetCurrentPosition();
  bool IsAudioParamStream() const
    return mAudioParamStream;

  const OutputChunks& LastChunks() const
    return mLastChunks;
  virtual bool MainThreadNeedsUpdates() const override
    return ((mFlags & NEED_MAIN_THREAD_FINISHED) && mFinished) ||
  virtual bool IsIntrinsicallyConsumed() const override
    return true;

  // Any thread
  AudioNodeEngine* Engine() { return mEngine; }
  TrackRate SampleRate() const { return mSampleRate; }
  AudioContext::AudioContextId AudioContextId() const override { return mAudioContextId; }

   * Convert a time in seconds on the destination stream to ticks
   * on this stream, including fractional position between ticks.
  double FractionalTicksFromDestinationTime(AudioNodeStream* aDestination,
                                            double aSeconds);
   * Convert a time in seconds on the destination stream to StreamTime
   * on this stream.
  StreamTime TicksFromDestinationTime(MediaStream* aDestination,
                                      double aSeconds);
   * Get the destination stream time in seconds corresponding to a position on
   * this stream.
  double DestinationTimeFromTicks(AudioNodeStream* aDestination,
                                  StreamTime aPosition);

  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;

  void SizeOfAudioNodesIncludingThis(MallocSizeOf aMallocSizeOf,
                                     AudioNodeSizes& aUsage) const;

  void AdvanceOutputSegment();
  void FinishOutput();
  void AccumulateInputChunk(uint32_t aInputIndex, const AudioChunk& aChunk,
                            AudioChunk* aBlock,
                            nsTArray<float>* aDownmixBuffer);
  void UpMixDownMixChunk(const AudioChunk* aChunk, uint32_t aOutputChannelCount,
                         nsTArray<const void*>& aOutputChannels,
                         nsTArray<float>& aDownmixBuffer);

  uint32_t ComputedNumberOfChannels(uint32_t aInputChannelCount);
  void ObtainInputBlock(AudioChunk& aTmpChunk, uint32_t aPortIndex);

  // The engine that will generate output for this node.
  nsAutoPtr<AudioNodeEngine> mEngine;
  // The last block produced by this node.
  OutputChunks mLastChunks;
  // The stream's sampling rate
  const TrackRate mSampleRate;
  // This is necessary to be able to find all the nodes for a given
  // AudioContext. It is set on the main thread, in the constructor.
  const AudioContext::AudioContextId mAudioContextId;
  // Whether this is an internal or external stream
  const Flags mFlags;
  // The number of input channels that this stream requires. 0 means don't care.
  uint32_t mNumberOfInputChannels;
  // The mixing modes
  ChannelCountMode mChannelCountMode;
  ChannelInterpretation mChannelInterpretation;
  // Whether the stream should be marked as finished as soon
  // as the current time range has been computed block by block.
  bool mMarkAsFinishedAfterThisBlock;
  // Whether the stream is an AudioParamHelper stream.
  bool mAudioParamStream;
  // Whether the stream just passes its input through.
  bool mPassThrough;

} // namespace mozilla