michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_AUDIONODEEXTERNALINPUTSTREAM_H_ michael@0: #define MOZILLA_AUDIONODEEXTERNALINPUTSTREAM_H_ michael@0: michael@0: #include "MediaStreamGraph.h" michael@0: #include "AudioNodeStream.h" michael@0: michael@0: // Forward declaration for mResamplerMap michael@0: typedef struct SpeexResamplerState_ SpeexResamplerState; michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * This is a MediaStream implementation that acts for a Web Audio node but michael@0: * unlike other AudioNodeStreams, supports any kind of MediaStream as an michael@0: * input --- handling any number of audio tracks, resampling them from whatever michael@0: * sample rate they're using, and handling blocking of the input MediaStream. michael@0: */ michael@0: class AudioNodeExternalInputStream : public AudioNodeStream { michael@0: public: michael@0: AudioNodeExternalInputStream(AudioNodeEngine* aEngine, TrackRate aSampleRate); michael@0: ~AudioNodeExternalInputStream(); michael@0: michael@0: virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: // For storing pointers and data about input tracks, like the last TrackTick which michael@0: // was read, and the associated speex resampler. michael@0: struct TrackMapEntry { michael@0: ~TrackMapEntry(); michael@0: michael@0: /** michael@0: * Resamples data from all chunks in aIterator and following, using mResampler, michael@0: * adding the results to mResampledData. michael@0: */ michael@0: void ResampleInputData(AudioSegment* aSegment); michael@0: /** michael@0: * Resamples a set of channel buffers using mResampler, adding the results michael@0: * to mResampledData. michael@0: */ michael@0: void ResampleChannels(const nsTArray& aBuffers, michael@0: uint32_t aInputDuration, michael@0: AudioSampleFormat aFormat, michael@0: float aVolume); michael@0: michael@0: // mEndOfConsumedInputTicks is the end of the input ticks that we've consumed. michael@0: // 0 if we haven't consumed any yet. michael@0: TrackTicks mEndOfConsumedInputTicks; michael@0: // mEndOfLastInputIntervalInInputStream is the timestamp for the end of the michael@0: // previous interval which was unblocked for both the input and output michael@0: // stream, in the input stream's timeline, or -1 if there wasn't one. michael@0: StreamTime mEndOfLastInputIntervalInInputStream; michael@0: // mEndOfLastInputIntervalInOutputStream is the timestamp for the end of the michael@0: // previous interval which was unblocked for both the input and output michael@0: // stream, in the output stream's timeline, or -1 if there wasn't one. michael@0: StreamTime mEndOfLastInputIntervalInOutputStream; michael@0: /** michael@0: * Number of samples passed to the resampler so far. michael@0: */ michael@0: TrackTicks mSamplesPassedToResampler; michael@0: /** michael@0: * Resampler being applied to this track. michael@0: */ michael@0: SpeexResamplerState* mResampler; michael@0: /** michael@0: * The track data that has been resampled to the rate of the michael@0: * AudioNodeExternalInputStream. All data in these chunks is in floats (or null), michael@0: * and has the number of channels given in mResamplerChannelCount. michael@0: * mResampledData starts at zero in the stream's output track (so generally michael@0: * it will consist of null data followed by actual data). michael@0: */ michael@0: AudioSegment mResampledData; michael@0: /** michael@0: * Number of channels used to create mResampler. michael@0: */ michael@0: uint32_t mResamplerChannelCount; michael@0: /** michael@0: * The ID for the track of the input stream this entry is for. michael@0: */ michael@0: TrackID mTrackID; michael@0: }; michael@0: michael@0: nsTArray mTrackMap; michael@0: // Amount of track data produced so far. A multiple of WEBAUDIO_BLOCK_SIZE. michael@0: TrackTicks mCurrentOutputPosition; michael@0: michael@0: /** michael@0: * Creates a TrackMapEntry for the track, if needed. Returns the index michael@0: * of the TrackMapEntry or NoIndex if no entry is needed yet. michael@0: */ michael@0: uint32_t GetTrackMapEntry(const StreamBuffer::Track& aTrack, michael@0: GraphTime aFrom); michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* MOZILLA_AUDIONODESTREAM_H_ */