|
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/. */ |
|
5 |
|
6 #ifndef MOZILLA_AUDIONODEEXTERNALINPUTSTREAM_H_ |
|
7 #define MOZILLA_AUDIONODEEXTERNALINPUTSTREAM_H_ |
|
8 |
|
9 #include "MediaStreamGraph.h" |
|
10 #include "AudioNodeStream.h" |
|
11 |
|
12 // Forward declaration for mResamplerMap |
|
13 typedef struct SpeexResamplerState_ SpeexResamplerState; |
|
14 |
|
15 namespace mozilla { |
|
16 |
|
17 /** |
|
18 * This is a MediaStream implementation that acts for a Web Audio node but |
|
19 * unlike other AudioNodeStreams, supports any kind of MediaStream as an |
|
20 * input --- handling any number of audio tracks, resampling them from whatever |
|
21 * sample rate they're using, and handling blocking of the input MediaStream. |
|
22 */ |
|
23 class AudioNodeExternalInputStream : public AudioNodeStream { |
|
24 public: |
|
25 AudioNodeExternalInputStream(AudioNodeEngine* aEngine, TrackRate aSampleRate); |
|
26 ~AudioNodeExternalInputStream(); |
|
27 |
|
28 virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) MOZ_OVERRIDE; |
|
29 |
|
30 private: |
|
31 // For storing pointers and data about input tracks, like the last TrackTick which |
|
32 // was read, and the associated speex resampler. |
|
33 struct TrackMapEntry { |
|
34 ~TrackMapEntry(); |
|
35 |
|
36 /** |
|
37 * Resamples data from all chunks in aIterator and following, using mResampler, |
|
38 * adding the results to mResampledData. |
|
39 */ |
|
40 void ResampleInputData(AudioSegment* aSegment); |
|
41 /** |
|
42 * Resamples a set of channel buffers using mResampler, adding the results |
|
43 * to mResampledData. |
|
44 */ |
|
45 void ResampleChannels(const nsTArray<const void*>& aBuffers, |
|
46 uint32_t aInputDuration, |
|
47 AudioSampleFormat aFormat, |
|
48 float aVolume); |
|
49 |
|
50 // mEndOfConsumedInputTicks is the end of the input ticks that we've consumed. |
|
51 // 0 if we haven't consumed any yet. |
|
52 TrackTicks mEndOfConsumedInputTicks; |
|
53 // mEndOfLastInputIntervalInInputStream is the timestamp for the end of the |
|
54 // previous interval which was unblocked for both the input and output |
|
55 // stream, in the input stream's timeline, or -1 if there wasn't one. |
|
56 StreamTime mEndOfLastInputIntervalInInputStream; |
|
57 // mEndOfLastInputIntervalInOutputStream is the timestamp for the end of the |
|
58 // previous interval which was unblocked for both the input and output |
|
59 // stream, in the output stream's timeline, or -1 if there wasn't one. |
|
60 StreamTime mEndOfLastInputIntervalInOutputStream; |
|
61 /** |
|
62 * Number of samples passed to the resampler so far. |
|
63 */ |
|
64 TrackTicks mSamplesPassedToResampler; |
|
65 /** |
|
66 * Resampler being applied to this track. |
|
67 */ |
|
68 SpeexResamplerState* mResampler; |
|
69 /** |
|
70 * The track data that has been resampled to the rate of the |
|
71 * AudioNodeExternalInputStream. All data in these chunks is in floats (or null), |
|
72 * and has the number of channels given in mResamplerChannelCount. |
|
73 * mResampledData starts at zero in the stream's output track (so generally |
|
74 * it will consist of null data followed by actual data). |
|
75 */ |
|
76 AudioSegment mResampledData; |
|
77 /** |
|
78 * Number of channels used to create mResampler. |
|
79 */ |
|
80 uint32_t mResamplerChannelCount; |
|
81 /** |
|
82 * The ID for the track of the input stream this entry is for. |
|
83 */ |
|
84 TrackID mTrackID; |
|
85 }; |
|
86 |
|
87 nsTArray<TrackMapEntry> mTrackMap; |
|
88 // Amount of track data produced so far. A multiple of WEBAUDIO_BLOCK_SIZE. |
|
89 TrackTicks mCurrentOutputPosition; |
|
90 |
|
91 /** |
|
92 * Creates a TrackMapEntry for the track, if needed. Returns the index |
|
93 * of the TrackMapEntry or NoIndex if no entry is needed yet. |
|
94 */ |
|
95 uint32_t GetTrackMapEntry(const StreamBuffer::Track& aTrack, |
|
96 GraphTime aFrom); |
|
97 }; |
|
98 |
|
99 } |
|
100 |
|
101 #endif /* MOZILLA_AUDIONODESTREAM_H_ */ |