Backed out 35 changesets (bug 941158, bug 972518, bug 959520, bug 986063, bug 948895...
[gecko.git] / content / media / AudioNodeStream.h
blob71fc90e9c715880237d4fc2840d06c01f442c2d1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_AUDIONODESTREAM_H_
7 #define MOZILLA_AUDIONODESTREAM_H_
9 #include "MediaStreamGraph.h"
10 #include "mozilla/dom/AudioNodeBinding.h"
11 #include "AudioSegment.h"
13 namespace mozilla {
15 namespace dom {
16 struct ThreeDPoint;
17 class AudioParamTimeline;
18 class DelayNodeEngine;
19 class AudioContext;
22 class ThreadSharedFloatArrayBufferList;
23 class AudioNodeEngine;
25 /**
26 * An AudioNodeStream produces one audio track with ID AUDIO_TRACK.
27 * The start time of the AudioTrack is aligned to the start time of the
28 * AudioContext's destination node stream, plus some multiple of BLOCK_SIZE
29 * samples.
31 * An AudioNodeStream has an AudioNodeEngine plugged into it that does the
32 * actual audio processing. AudioNodeStream contains the glue code that
33 * integrates audio processing with the MediaStreamGraph.
35 class AudioNodeStream : public ProcessedMediaStream {
36 public:
37 typedef mozilla::dom::AudioContext AudioContext;
39 enum { AUDIO_TRACK = 1 };
41 typedef nsAutoTArray<AudioChunk, 1> OutputChunks;
43 /**
44 * Transfers ownership of aEngine to the new AudioNodeStream.
46 AudioNodeStream(AudioNodeEngine* aEngine,
47 MediaStreamGraph::AudioNodeStreamKind aKind,
48 TrackRate aSampleRate)
49 : ProcessedMediaStream(nullptr),
50 mEngine(aEngine),
51 mSampleRate(aSampleRate),
52 mKind(aKind),
53 mNumberOfInputChannels(2),
54 mMarkAsFinishedAfterThisBlock(false),
55 mAudioParamStream(false),
56 mMuted(false)
58 MOZ_ASSERT(NS_IsMainThread());
59 mChannelCountMode = dom::ChannelCountMode::Max;
60 mChannelInterpretation = dom::ChannelInterpretation::Speakers;
61 // AudioNodes are always producing data
62 mHasCurrentData = true;
63 MOZ_COUNT_CTOR(AudioNodeStream);
65 ~AudioNodeStream();
67 // Control API
68 /**
69 * Sets a parameter that's a time relative to some stream's played time.
70 * This time is converted to a time relative to this stream when it's set.
72 void SetStreamTimeParameter(uint32_t aIndex, AudioContext* aContext,
73 double aStreamTime);
74 void SetDoubleParameter(uint32_t aIndex, double aValue);
75 void SetInt32Parameter(uint32_t aIndex, int32_t aValue);
76 void SetTimelineParameter(uint32_t aIndex, const dom::AudioParamTimeline& aValue);
77 void SetThreeDPointParameter(uint32_t aIndex, const dom::ThreeDPoint& aValue);
78 void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer);
79 // This consumes the contents of aData. aData will be emptied after this returns.
80 void SetRawArrayData(nsTArray<float>& aData);
81 void SetChannelMixingParameters(uint32_t aNumberOfChannels,
82 dom::ChannelCountMode aChannelCountMoe,
83 dom::ChannelInterpretation aChannelInterpretation);
84 void SetAudioParamHelperStream()
86 MOZ_ASSERT(!mAudioParamStream, "Can only do this once");
87 mAudioParamStream = true;
90 virtual AudioNodeStream* AsAudioNodeStream() { return this; }
92 // Graph thread only
93 void SetStreamTimeParameterImpl(uint32_t aIndex, MediaStream* aRelativeToStream,
94 double aStreamTime);
95 void SetChannelMixingParametersImpl(uint32_t aNumberOfChannels,
96 dom::ChannelCountMode aChannelCountMoe,
97 dom::ChannelInterpretation aChannelInterpretation);
98 virtual void ProduceOutput(GraphTime aFrom, GraphTime aTo);
99 TrackTicks GetCurrentPosition();
100 bool IsAudioParamStream() const
102 return mAudioParamStream;
104 void Mute() {
105 mMuted = true;
108 void Unmute() {
109 mMuted = false;
112 const OutputChunks& LastChunks() const
114 return mLastChunks;
116 virtual bool MainThreadNeedsUpdates() const MOZ_OVERRIDE
118 // Only source and external streams need updates on the main thread.
119 return (mKind == MediaStreamGraph::SOURCE_STREAM && mFinished) ||
120 mKind == MediaStreamGraph::EXTERNAL_STREAM;
122 virtual bool IsIntrinsicallyConsumed() const MOZ_OVERRIDE
124 return true;
127 // Any thread
128 AudioNodeEngine* Engine() { return mEngine; }
129 TrackRate SampleRate() const { return mSampleRate; }
131 protected:
132 void AdvanceOutputSegment();
133 void FinishOutput();
134 void AccumulateInputChunk(uint32_t aInputIndex, const AudioChunk& aChunk,
135 AudioChunk* aBlock,
136 nsTArray<float>* aDownmixBuffer);
137 void UpMixDownMixChunk(const AudioChunk* aChunk, uint32_t aOutputChannelCount,
138 nsTArray<const void*>& aOutputChannels,
139 nsTArray<float>& aDownmixBuffer);
141 uint32_t ComputeFinalOuputChannelCount(uint32_t aInputChannelCount);
142 void ObtainInputBlock(AudioChunk& aTmpChunk, uint32_t aPortIndex);
144 // The engine that will generate output for this node.
145 nsAutoPtr<AudioNodeEngine> mEngine;
146 // The last block produced by this node.
147 OutputChunks mLastChunks;
148 // The stream's sampling rate
149 const TrackRate mSampleRate;
150 // Whether this is an internal or external stream
151 MediaStreamGraph::AudioNodeStreamKind mKind;
152 // The number of input channels that this stream requires. 0 means don't care.
153 uint32_t mNumberOfInputChannels;
154 // The mixing modes
155 dom::ChannelCountMode mChannelCountMode;
156 dom::ChannelInterpretation mChannelInterpretation;
157 // Whether the stream should be marked as finished as soon
158 // as the current time range has been computed block by block.
159 bool mMarkAsFinishedAfterThisBlock;
160 // Whether the stream is an AudioParamHelper stream.
161 bool mAudioParamStream;
162 // Whether the stream is muted. Access only on the MediaStreamGraph thread.
163 bool mMuted;
168 #endif /* MOZILLA_AUDIONODESTREAM_H_ */