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 OpusTrackEncoder_h_ michael@0: #define OpusTrackEncoder_h_ michael@0: michael@0: #include michael@0: #include michael@0: #include "TrackEncoder.h" michael@0: michael@0: struct OpusEncoder; michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Opus meta data structure michael@0: class OpusMetadata : public TrackMetadataBase michael@0: { michael@0: public: michael@0: // The ID Header of OggOpus. refer to http://wiki.xiph.org/OggOpus. michael@0: nsTArray mIdHeader; michael@0: // The Comment Header of OggOpus. michael@0: nsTArray mCommentHeader; michael@0: michael@0: MetadataKind GetKind() const MOZ_OVERRIDE { return METADATA_OPUS; } michael@0: }; michael@0: michael@0: class OpusTrackEncoder : public AudioTrackEncoder michael@0: { michael@0: public: michael@0: OpusTrackEncoder(); michael@0: virtual ~OpusTrackEncoder(); michael@0: michael@0: already_AddRefed GetMetadata() MOZ_OVERRIDE; michael@0: michael@0: nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_OVERRIDE; michael@0: michael@0: protected: michael@0: int GetPacketDuration(); michael@0: michael@0: nsresult Init(int aChannels, int aSamplingRate) MOZ_OVERRIDE; michael@0: michael@0: /** michael@0: * Get the samplerate of the data to be fed to the Opus encoder. This might be michael@0: * different from the input samplerate if resampling occurs. michael@0: */ michael@0: int GetOutputSampleRate(); michael@0: michael@0: private: michael@0: /** michael@0: * The Opus encoder from libopus. michael@0: */ michael@0: OpusEncoder* mEncoder; michael@0: michael@0: /** michael@0: * A local segment queue which takes the raw data out from mRawSegment in the michael@0: * call of GetEncodedTrack(). Opus encoder only accepts GetPacketDuration() michael@0: * samples from mSourceSegment every encoding cycle, thus it needs to be michael@0: * global in order to store the leftover segments taken from mRawSegment. michael@0: */ michael@0: AudioSegment mSourceSegment; michael@0: michael@0: /** michael@0: * Total samples of delay added by codec, can be queried by the encoder. From michael@0: * the perspective of decoding, real data begins this many samples late, so michael@0: * the encoder needs to append this many null samples to the end of stream, michael@0: * in order to align the time of input and output. michael@0: */ michael@0: int mLookahead; michael@0: michael@0: /** michael@0: * If the input sample rate does not divide 48kHz evenly, the input data are michael@0: * resampled. michael@0: */ michael@0: SpeexResamplerState* mResampler; michael@0: michael@0: /** michael@0: * Store the resampled frames that don't fit into an Opus packet duration. michael@0: * They will be prepended to the resampled frames next encoding cycle. michael@0: */ michael@0: nsTArray mResampledLeftover; michael@0: }; michael@0: michael@0: } michael@0: #endif