Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef MOZILLA_AUDIONODESTREAM_H_ |
michael@0 | 7 | #define MOZILLA_AUDIONODESTREAM_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "MediaStreamGraph.h" |
michael@0 | 10 | #include "mozilla/dom/AudioNodeBinding.h" |
michael@0 | 11 | #include "AudioSegment.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | |
michael@0 | 15 | namespace dom { |
michael@0 | 16 | struct ThreeDPoint; |
michael@0 | 17 | class AudioParamTimeline; |
michael@0 | 18 | class DelayNodeEngine; |
michael@0 | 19 | class AudioContext; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | class ThreadSharedFloatArrayBufferList; |
michael@0 | 23 | class AudioNodeEngine; |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * An AudioNodeStream produces one audio track with ID AUDIO_TRACK. |
michael@0 | 27 | * The start time of the AudioTrack is aligned to the start time of the |
michael@0 | 28 | * AudioContext's destination node stream, plus some multiple of BLOCK_SIZE |
michael@0 | 29 | * samples. |
michael@0 | 30 | * |
michael@0 | 31 | * An AudioNodeStream has an AudioNodeEngine plugged into it that does the |
michael@0 | 32 | * actual audio processing. AudioNodeStream contains the glue code that |
michael@0 | 33 | * integrates audio processing with the MediaStreamGraph. |
michael@0 | 34 | */ |
michael@0 | 35 | class AudioNodeStream : public ProcessedMediaStream { |
michael@0 | 36 | typedef dom::ChannelCountMode ChannelCountMode; |
michael@0 | 37 | typedef dom::ChannelInterpretation ChannelInterpretation; |
michael@0 | 38 | |
michael@0 | 39 | public: |
michael@0 | 40 | typedef mozilla::dom::AudioContext AudioContext; |
michael@0 | 41 | |
michael@0 | 42 | enum { AUDIO_TRACK = 1 }; |
michael@0 | 43 | |
michael@0 | 44 | typedef nsAutoTArray<AudioChunk, 1> OutputChunks; |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * Transfers ownership of aEngine to the new AudioNodeStream. |
michael@0 | 48 | */ |
michael@0 | 49 | AudioNodeStream(AudioNodeEngine* aEngine, |
michael@0 | 50 | MediaStreamGraph::AudioNodeStreamKind aKind, |
michael@0 | 51 | TrackRate aSampleRate) |
michael@0 | 52 | : ProcessedMediaStream(nullptr), |
michael@0 | 53 | mEngine(aEngine), |
michael@0 | 54 | mSampleRate(aSampleRate), |
michael@0 | 55 | mKind(aKind), |
michael@0 | 56 | mNumberOfInputChannels(2), |
michael@0 | 57 | mMarkAsFinishedAfterThisBlock(false), |
michael@0 | 58 | mAudioParamStream(false), |
michael@0 | 59 | mMuted(false) |
michael@0 | 60 | { |
michael@0 | 61 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 62 | mChannelCountMode = ChannelCountMode::Max; |
michael@0 | 63 | mChannelInterpretation = ChannelInterpretation::Speakers; |
michael@0 | 64 | // AudioNodes are always producing data |
michael@0 | 65 | mHasCurrentData = true; |
michael@0 | 66 | MOZ_COUNT_CTOR(AudioNodeStream); |
michael@0 | 67 | } |
michael@0 | 68 | ~AudioNodeStream(); |
michael@0 | 69 | |
michael@0 | 70 | // Control API |
michael@0 | 71 | /** |
michael@0 | 72 | * Sets a parameter that's a time relative to some stream's played time. |
michael@0 | 73 | * This time is converted to a time relative to this stream when it's set. |
michael@0 | 74 | */ |
michael@0 | 75 | void SetStreamTimeParameter(uint32_t aIndex, AudioContext* aContext, |
michael@0 | 76 | double aStreamTime); |
michael@0 | 77 | void SetDoubleParameter(uint32_t aIndex, double aValue); |
michael@0 | 78 | void SetInt32Parameter(uint32_t aIndex, int32_t aValue); |
michael@0 | 79 | void SetTimelineParameter(uint32_t aIndex, const dom::AudioParamTimeline& aValue); |
michael@0 | 80 | void SetThreeDPointParameter(uint32_t aIndex, const dom::ThreeDPoint& aValue); |
michael@0 | 81 | void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList>&& aBuffer); |
michael@0 | 82 | // This consumes the contents of aData. aData will be emptied after this returns. |
michael@0 | 83 | void SetRawArrayData(nsTArray<float>& aData); |
michael@0 | 84 | void SetChannelMixingParameters(uint32_t aNumberOfChannels, |
michael@0 | 85 | ChannelCountMode aChannelCountMoe, |
michael@0 | 86 | ChannelInterpretation aChannelInterpretation); |
michael@0 | 87 | ChannelInterpretation GetChannelInterpretation() |
michael@0 | 88 | { |
michael@0 | 89 | return mChannelInterpretation; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void SetAudioParamHelperStream() |
michael@0 | 93 | { |
michael@0 | 94 | MOZ_ASSERT(!mAudioParamStream, "Can only do this once"); |
michael@0 | 95 | mAudioParamStream = true; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | virtual AudioNodeStream* AsAudioNodeStream() MOZ_OVERRIDE { return this; } |
michael@0 | 99 | |
michael@0 | 100 | // Graph thread only |
michael@0 | 101 | void SetStreamTimeParameterImpl(uint32_t aIndex, MediaStream* aRelativeToStream, |
michael@0 | 102 | double aStreamTime); |
michael@0 | 103 | void SetChannelMixingParametersImpl(uint32_t aNumberOfChannels, |
michael@0 | 104 | ChannelCountMode aChannelCountMoe, |
michael@0 | 105 | ChannelInterpretation aChannelInterpretation); |
michael@0 | 106 | virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) MOZ_OVERRIDE; |
michael@0 | 107 | TrackTicks GetCurrentPosition(); |
michael@0 | 108 | bool IsAudioParamStream() const |
michael@0 | 109 | { |
michael@0 | 110 | return mAudioParamStream; |
michael@0 | 111 | } |
michael@0 | 112 | void Mute() { |
michael@0 | 113 | mMuted = true; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | void Unmute() { |
michael@0 | 117 | mMuted = false; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | const OutputChunks& LastChunks() const |
michael@0 | 121 | { |
michael@0 | 122 | return mLastChunks; |
michael@0 | 123 | } |
michael@0 | 124 | virtual bool MainThreadNeedsUpdates() const MOZ_OVERRIDE |
michael@0 | 125 | { |
michael@0 | 126 | // Only source and external streams need updates on the main thread. |
michael@0 | 127 | return (mKind == MediaStreamGraph::SOURCE_STREAM && mFinished) || |
michael@0 | 128 | mKind == MediaStreamGraph::EXTERNAL_STREAM; |
michael@0 | 129 | } |
michael@0 | 130 | virtual bool IsIntrinsicallyConsumed() const MOZ_OVERRIDE |
michael@0 | 131 | { |
michael@0 | 132 | return true; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // Any thread |
michael@0 | 136 | AudioNodeEngine* Engine() { return mEngine; } |
michael@0 | 137 | TrackRate SampleRate() const { return mSampleRate; } |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Convert a time in seconds on the destination stream to seconds |
michael@0 | 141 | * on this stream. |
michael@0 | 142 | */ |
michael@0 | 143 | double TimeFromDestinationTime(AudioNodeStream* aDestination, |
michael@0 | 144 | double aSeconds); |
michael@0 | 145 | /** |
michael@0 | 146 | * Convert a time in seconds on the destination stream to TrackTicks |
michael@0 | 147 | * on this stream. |
michael@0 | 148 | */ |
michael@0 | 149 | TrackTicks TicksFromDestinationTime(MediaStream* aDestination, |
michael@0 | 150 | double aSeconds); |
michael@0 | 151 | /** |
michael@0 | 152 | * Get the destination stream time in seconds corresponding to a position on |
michael@0 | 153 | * this stream. |
michael@0 | 154 | */ |
michael@0 | 155 | double DestinationTimeFromTicks(AudioNodeStream* aDestination, |
michael@0 | 156 | TrackTicks aPosition); |
michael@0 | 157 | |
michael@0 | 158 | size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; |
michael@0 | 159 | size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; |
michael@0 | 160 | |
michael@0 | 161 | void SizeOfAudioNodesIncludingThis(MallocSizeOf aMallocSizeOf, |
michael@0 | 162 | AudioNodeSizes& aUsage) const; |
michael@0 | 163 | |
michael@0 | 164 | protected: |
michael@0 | 165 | void AdvanceOutputSegment(); |
michael@0 | 166 | void FinishOutput(); |
michael@0 | 167 | void AccumulateInputChunk(uint32_t aInputIndex, const AudioChunk& aChunk, |
michael@0 | 168 | AudioChunk* aBlock, |
michael@0 | 169 | nsTArray<float>* aDownmixBuffer); |
michael@0 | 170 | void UpMixDownMixChunk(const AudioChunk* aChunk, uint32_t aOutputChannelCount, |
michael@0 | 171 | nsTArray<const void*>& aOutputChannels, |
michael@0 | 172 | nsTArray<float>& aDownmixBuffer); |
michael@0 | 173 | |
michael@0 | 174 | uint32_t ComputedNumberOfChannels(uint32_t aInputChannelCount); |
michael@0 | 175 | void ObtainInputBlock(AudioChunk& aTmpChunk, uint32_t aPortIndex); |
michael@0 | 176 | |
michael@0 | 177 | // The engine that will generate output for this node. |
michael@0 | 178 | nsAutoPtr<AudioNodeEngine> mEngine; |
michael@0 | 179 | // The last block produced by this node. |
michael@0 | 180 | OutputChunks mLastChunks; |
michael@0 | 181 | // The stream's sampling rate |
michael@0 | 182 | const TrackRate mSampleRate; |
michael@0 | 183 | // Whether this is an internal or external stream |
michael@0 | 184 | MediaStreamGraph::AudioNodeStreamKind mKind; |
michael@0 | 185 | // The number of input channels that this stream requires. 0 means don't care. |
michael@0 | 186 | uint32_t mNumberOfInputChannels; |
michael@0 | 187 | // The mixing modes |
michael@0 | 188 | ChannelCountMode mChannelCountMode; |
michael@0 | 189 | ChannelInterpretation mChannelInterpretation; |
michael@0 | 190 | // Whether the stream should be marked as finished as soon |
michael@0 | 191 | // as the current time range has been computed block by block. |
michael@0 | 192 | bool mMarkAsFinishedAfterThisBlock; |
michael@0 | 193 | // Whether the stream is an AudioParamHelper stream. |
michael@0 | 194 | bool mAudioParamStream; |
michael@0 | 195 | // Whether the stream is muted. Access only on the MediaStreamGraph thread. |
michael@0 | 196 | bool mMuted; |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | #endif /* MOZILLA_AUDIONODESTREAM_H_ */ |