1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/AudioNodeExternalInputStream.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 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_AUDIONODEEXTERNALINPUTSTREAM_H_ 1.10 +#define MOZILLA_AUDIONODEEXTERNALINPUTSTREAM_H_ 1.11 + 1.12 +#include "MediaStreamGraph.h" 1.13 +#include "AudioNodeStream.h" 1.14 + 1.15 +// Forward declaration for mResamplerMap 1.16 +typedef struct SpeexResamplerState_ SpeexResamplerState; 1.17 + 1.18 +namespace mozilla { 1.19 + 1.20 +/** 1.21 + * This is a MediaStream implementation that acts for a Web Audio node but 1.22 + * unlike other AudioNodeStreams, supports any kind of MediaStream as an 1.23 + * input --- handling any number of audio tracks, resampling them from whatever 1.24 + * sample rate they're using, and handling blocking of the input MediaStream. 1.25 + */ 1.26 +class AudioNodeExternalInputStream : public AudioNodeStream { 1.27 +public: 1.28 + AudioNodeExternalInputStream(AudioNodeEngine* aEngine, TrackRate aSampleRate); 1.29 + ~AudioNodeExternalInputStream(); 1.30 + 1.31 + virtual void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) MOZ_OVERRIDE; 1.32 + 1.33 +private: 1.34 + // For storing pointers and data about input tracks, like the last TrackTick which 1.35 + // was read, and the associated speex resampler. 1.36 + struct TrackMapEntry { 1.37 + ~TrackMapEntry(); 1.38 + 1.39 + /** 1.40 + * Resamples data from all chunks in aIterator and following, using mResampler, 1.41 + * adding the results to mResampledData. 1.42 + */ 1.43 + void ResampleInputData(AudioSegment* aSegment); 1.44 + /** 1.45 + * Resamples a set of channel buffers using mResampler, adding the results 1.46 + * to mResampledData. 1.47 + */ 1.48 + void ResampleChannels(const nsTArray<const void*>& aBuffers, 1.49 + uint32_t aInputDuration, 1.50 + AudioSampleFormat aFormat, 1.51 + float aVolume); 1.52 + 1.53 + // mEndOfConsumedInputTicks is the end of the input ticks that we've consumed. 1.54 + // 0 if we haven't consumed any yet. 1.55 + TrackTicks mEndOfConsumedInputTicks; 1.56 + // mEndOfLastInputIntervalInInputStream is the timestamp for the end of the 1.57 + // previous interval which was unblocked for both the input and output 1.58 + // stream, in the input stream's timeline, or -1 if there wasn't one. 1.59 + StreamTime mEndOfLastInputIntervalInInputStream; 1.60 + // mEndOfLastInputIntervalInOutputStream is the timestamp for the end of the 1.61 + // previous interval which was unblocked for both the input and output 1.62 + // stream, in the output stream's timeline, or -1 if there wasn't one. 1.63 + StreamTime mEndOfLastInputIntervalInOutputStream; 1.64 + /** 1.65 + * Number of samples passed to the resampler so far. 1.66 + */ 1.67 + TrackTicks mSamplesPassedToResampler; 1.68 + /** 1.69 + * Resampler being applied to this track. 1.70 + */ 1.71 + SpeexResamplerState* mResampler; 1.72 + /** 1.73 + * The track data that has been resampled to the rate of the 1.74 + * AudioNodeExternalInputStream. All data in these chunks is in floats (or null), 1.75 + * and has the number of channels given in mResamplerChannelCount. 1.76 + * mResampledData starts at zero in the stream's output track (so generally 1.77 + * it will consist of null data followed by actual data). 1.78 + */ 1.79 + AudioSegment mResampledData; 1.80 + /** 1.81 + * Number of channels used to create mResampler. 1.82 + */ 1.83 + uint32_t mResamplerChannelCount; 1.84 + /** 1.85 + * The ID for the track of the input stream this entry is for. 1.86 + */ 1.87 + TrackID mTrackID; 1.88 + }; 1.89 + 1.90 + nsTArray<TrackMapEntry> mTrackMap; 1.91 + // Amount of track data produced so far. A multiple of WEBAUDIO_BLOCK_SIZE. 1.92 + TrackTicks mCurrentOutputPosition; 1.93 + 1.94 + /** 1.95 + * Creates a TrackMapEntry for the track, if needed. Returns the index 1.96 + * of the TrackMapEntry or NoIndex if no entry is needed yet. 1.97 + */ 1.98 + uint32_t GetTrackMapEntry(const StreamBuffer::Track& aTrack, 1.99 + GraphTime aFrom); 1.100 +}; 1.101 + 1.102 +} 1.103 + 1.104 +#endif /* MOZILLA_AUDIONODESTREAM_H_ */