content/media/AudioNodeStream.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/AudioNodeStream.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_AUDIONODESTREAM_H_
    1.10 +#define MOZILLA_AUDIONODESTREAM_H_
    1.11 +
    1.12 +#include "MediaStreamGraph.h"
    1.13 +#include "mozilla/dom/AudioNodeBinding.h"
    1.14 +#include "AudioSegment.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +
    1.18 +namespace dom {
    1.19 +struct ThreeDPoint;
    1.20 +class AudioParamTimeline;
    1.21 +class DelayNodeEngine;
    1.22 +class AudioContext;
    1.23 +}
    1.24 +
    1.25 +class ThreadSharedFloatArrayBufferList;
    1.26 +class AudioNodeEngine;
    1.27 +
    1.28 +/**
    1.29 + * An AudioNodeStream produces one audio track with ID AUDIO_TRACK.
    1.30 + * The start time of the AudioTrack is aligned to the start time of the
    1.31 + * AudioContext's destination node stream, plus some multiple of BLOCK_SIZE
    1.32 + * samples.
    1.33 + *
    1.34 + * An AudioNodeStream has an AudioNodeEngine plugged into it that does the
    1.35 + * actual audio processing. AudioNodeStream contains the glue code that
    1.36 + * integrates audio processing with the MediaStreamGraph.
    1.37 + */
    1.38 +class AudioNodeStream : public ProcessedMediaStream {
    1.39 +  typedef dom::ChannelCountMode ChannelCountMode;
    1.40 +  typedef dom::ChannelInterpretation ChannelInterpretation;
    1.41 +
    1.42 +public:
    1.43 +  typedef mozilla::dom::AudioContext AudioContext;
    1.44 +
    1.45 +  enum { AUDIO_TRACK = 1 };
    1.46 +
    1.47 +  typedef nsAutoTArray<AudioChunk, 1> OutputChunks;
    1.48 +
    1.49 +  /**
    1.50 +   * Transfers ownership of aEngine to the new AudioNodeStream.
    1.51 +   */
    1.52 +  AudioNodeStream(AudioNodeEngine* aEngine,
    1.53 +                  MediaStreamGraph::AudioNodeStreamKind aKind,
    1.54 +                  TrackRate aSampleRate)
    1.55 +    : ProcessedMediaStream(nullptr),
    1.56 +      mEngine(aEngine),
    1.57 +      mSampleRate(aSampleRate),
    1.58 +      mKind(aKind),
    1.59 +      mNumberOfInputChannels(2),
    1.60 +      mMarkAsFinishedAfterThisBlock(false),
    1.61 +      mAudioParamStream(false),
    1.62 +      mMuted(false)
    1.63 +  {
    1.64 +    MOZ_ASSERT(NS_IsMainThread());
    1.65 +    mChannelCountMode = ChannelCountMode::Max;
    1.66 +    mChannelInterpretation = ChannelInterpretation::Speakers;
    1.67 +    // AudioNodes are always producing data
    1.68 +    mHasCurrentData = true;
    1.69 +    MOZ_COUNT_CTOR(AudioNodeStream);
    1.70 +  }
    1.71 +  ~AudioNodeStream();
    1.72 +
    1.73 +  // Control API
    1.74 +  /**
    1.75 +   * Sets a parameter that's a time relative to some stream's played time.
    1.76 +   * This time is converted to a time relative to this stream when it's set.
    1.77 +   */
    1.78 +  void SetStreamTimeParameter(uint32_t aIndex, AudioContext* aContext,
    1.79 +                              double aStreamTime);
    1.80 +  void SetDoubleParameter(uint32_t aIndex, double aValue);
    1.81 +  void SetInt32Parameter(uint32_t aIndex, int32_t aValue);
    1.82 +  void SetTimelineParameter(uint32_t aIndex, const dom::AudioParamTimeline& aValue);
    1.83 +  void SetThreeDPointParameter(uint32_t aIndex, const dom::ThreeDPoint& aValue);
    1.84 +  void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList>&& aBuffer);
    1.85 +  // This consumes the contents of aData.  aData will be emptied after this returns.
    1.86 +  void SetRawArrayData(nsTArray<float>& aData);
    1.87 +  void SetChannelMixingParameters(uint32_t aNumberOfChannels,
    1.88 +                                  ChannelCountMode aChannelCountMoe,
    1.89 +                                  ChannelInterpretation aChannelInterpretation);
    1.90 +  ChannelInterpretation GetChannelInterpretation()
    1.91 +  {
    1.92 +    return mChannelInterpretation;
    1.93 +  }
    1.94 +
    1.95 +  void SetAudioParamHelperStream()
    1.96 +  {
    1.97 +    MOZ_ASSERT(!mAudioParamStream, "Can only do this once");
    1.98 +    mAudioParamStream = true;
    1.99 +  }
   1.100 +
   1.101 +  virtual AudioNodeStream* AsAudioNodeStream() MOZ_OVERRIDE { return this; }
   1.102 +
   1.103 +  // Graph thread only
   1.104 +  void SetStreamTimeParameterImpl(uint32_t aIndex, MediaStream* aRelativeToStream,
   1.105 +                                  double aStreamTime);
   1.106 +  void SetChannelMixingParametersImpl(uint32_t aNumberOfChannels,
   1.107 +                                      ChannelCountMode aChannelCountMoe,
   1.108 +                                      ChannelInterpretation aChannelInterpretation);
   1.109 +  virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) MOZ_OVERRIDE;
   1.110 +  TrackTicks GetCurrentPosition();
   1.111 +  bool IsAudioParamStream() const
   1.112 +  {
   1.113 +    return mAudioParamStream;
   1.114 +  }
   1.115 +  void Mute() {
   1.116 +    mMuted = true;
   1.117 +  }
   1.118 +
   1.119 +  void Unmute() {
   1.120 +    mMuted = false;
   1.121 +  }
   1.122 +
   1.123 +  const OutputChunks& LastChunks() const
   1.124 +  {
   1.125 +    return mLastChunks;
   1.126 +  }
   1.127 +  virtual bool MainThreadNeedsUpdates() const MOZ_OVERRIDE
   1.128 +  {
   1.129 +    // Only source and external streams need updates on the main thread.
   1.130 +    return (mKind == MediaStreamGraph::SOURCE_STREAM && mFinished) ||
   1.131 +           mKind == MediaStreamGraph::EXTERNAL_STREAM;
   1.132 +  }
   1.133 +  virtual bool IsIntrinsicallyConsumed() const MOZ_OVERRIDE
   1.134 +  {
   1.135 +    return true;
   1.136 +  }
   1.137 +
   1.138 +  // Any thread
   1.139 +  AudioNodeEngine* Engine() { return mEngine; }
   1.140 +  TrackRate SampleRate() const { return mSampleRate; }
   1.141 +
   1.142 +  /**
   1.143 +   * Convert a time in seconds on the destination stream to seconds
   1.144 +   * on this stream.
   1.145 +   */
   1.146 +  double TimeFromDestinationTime(AudioNodeStream* aDestination,
   1.147 +                                 double aSeconds);
   1.148 +  /**
   1.149 +   * Convert a time in seconds on the destination stream to TrackTicks
   1.150 +   * on this stream.
   1.151 +   */
   1.152 +  TrackTicks TicksFromDestinationTime(MediaStream* aDestination,
   1.153 +                                      double aSeconds);
   1.154 +  /**
   1.155 +   * Get the destination stream time in seconds corresponding to a position on
   1.156 +   * this stream.
   1.157 +   */
   1.158 +  double DestinationTimeFromTicks(AudioNodeStream* aDestination,
   1.159 +                                  TrackTicks aPosition);
   1.160 +
   1.161 +  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
   1.162 +  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
   1.163 +
   1.164 +  void SizeOfAudioNodesIncludingThis(MallocSizeOf aMallocSizeOf,
   1.165 +                                     AudioNodeSizes& aUsage) const;
   1.166 +
   1.167 +protected:
   1.168 +  void AdvanceOutputSegment();
   1.169 +  void FinishOutput();
   1.170 +  void AccumulateInputChunk(uint32_t aInputIndex, const AudioChunk& aChunk,
   1.171 +                            AudioChunk* aBlock,
   1.172 +                            nsTArray<float>* aDownmixBuffer);
   1.173 +  void UpMixDownMixChunk(const AudioChunk* aChunk, uint32_t aOutputChannelCount,
   1.174 +                         nsTArray<const void*>& aOutputChannels,
   1.175 +                         nsTArray<float>& aDownmixBuffer);
   1.176 +
   1.177 +  uint32_t ComputedNumberOfChannels(uint32_t aInputChannelCount);
   1.178 +  void ObtainInputBlock(AudioChunk& aTmpChunk, uint32_t aPortIndex);
   1.179 +
   1.180 +  // The engine that will generate output for this node.
   1.181 +  nsAutoPtr<AudioNodeEngine> mEngine;
   1.182 +  // The last block produced by this node.
   1.183 +  OutputChunks mLastChunks;
   1.184 +  // The stream's sampling rate
   1.185 +  const TrackRate mSampleRate;
   1.186 +  // Whether this is an internal or external stream
   1.187 +  MediaStreamGraph::AudioNodeStreamKind mKind;
   1.188 +  // The number of input channels that this stream requires. 0 means don't care.
   1.189 +  uint32_t mNumberOfInputChannels;
   1.190 +  // The mixing modes
   1.191 +  ChannelCountMode mChannelCountMode;
   1.192 +  ChannelInterpretation mChannelInterpretation;
   1.193 +  // Whether the stream should be marked as finished as soon
   1.194 +  // as the current time range has been computed block by block.
   1.195 +  bool mMarkAsFinishedAfterThisBlock;
   1.196 +  // Whether the stream is an AudioParamHelper stream.
   1.197 +  bool mAudioParamStream;
   1.198 +  // Whether the stream is muted. Access only on the MediaStreamGraph thread.
   1.199 +  bool mMuted;
   1.200 +};
   1.201 +
   1.202 +}
   1.203 +
   1.204 +#endif /* MOZILLA_AUDIONODESTREAM_H_ */

mercurial