1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/encoder/OpusTrackEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 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 OpusTrackEncoder_h_ 1.10 +#define OpusTrackEncoder_h_ 1.11 + 1.12 +#include <stdint.h> 1.13 +#include <speex/speex_resampler.h> 1.14 +#include "TrackEncoder.h" 1.15 + 1.16 +struct OpusEncoder; 1.17 + 1.18 +namespace mozilla { 1.19 + 1.20 +// Opus meta data structure 1.21 +class OpusMetadata : public TrackMetadataBase 1.22 +{ 1.23 +public: 1.24 + // The ID Header of OggOpus. refer to http://wiki.xiph.org/OggOpus. 1.25 + nsTArray<uint8_t> mIdHeader; 1.26 + // The Comment Header of OggOpus. 1.27 + nsTArray<uint8_t> mCommentHeader; 1.28 + 1.29 + MetadataKind GetKind() const MOZ_OVERRIDE { return METADATA_OPUS; } 1.30 +}; 1.31 + 1.32 +class OpusTrackEncoder : public AudioTrackEncoder 1.33 +{ 1.34 +public: 1.35 + OpusTrackEncoder(); 1.36 + virtual ~OpusTrackEncoder(); 1.37 + 1.38 + already_AddRefed<TrackMetadataBase> GetMetadata() MOZ_OVERRIDE; 1.39 + 1.40 + nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_OVERRIDE; 1.41 + 1.42 +protected: 1.43 + int GetPacketDuration(); 1.44 + 1.45 + nsresult Init(int aChannels, int aSamplingRate) MOZ_OVERRIDE; 1.46 + 1.47 + /** 1.48 + * Get the samplerate of the data to be fed to the Opus encoder. This might be 1.49 + * different from the input samplerate if resampling occurs. 1.50 + */ 1.51 + int GetOutputSampleRate(); 1.52 + 1.53 +private: 1.54 + /** 1.55 + * The Opus encoder from libopus. 1.56 + */ 1.57 + OpusEncoder* mEncoder; 1.58 + 1.59 + /** 1.60 + * A local segment queue which takes the raw data out from mRawSegment in the 1.61 + * call of GetEncodedTrack(). Opus encoder only accepts GetPacketDuration() 1.62 + * samples from mSourceSegment every encoding cycle, thus it needs to be 1.63 + * global in order to store the leftover segments taken from mRawSegment. 1.64 + */ 1.65 + AudioSegment mSourceSegment; 1.66 + 1.67 + /** 1.68 + * Total samples of delay added by codec, can be queried by the encoder. From 1.69 + * the perspective of decoding, real data begins this many samples late, so 1.70 + * the encoder needs to append this many null samples to the end of stream, 1.71 + * in order to align the time of input and output. 1.72 + */ 1.73 + int mLookahead; 1.74 + 1.75 + /** 1.76 + * If the input sample rate does not divide 48kHz evenly, the input data are 1.77 + * resampled. 1.78 + */ 1.79 + SpeexResamplerState* mResampler; 1.80 + 1.81 + /** 1.82 + * Store the resampled frames that don't fit into an Opus packet duration. 1.83 + * They will be prepended to the resampled frames next encoding cycle. 1.84 + */ 1.85 + nsTArray<AudioDataValue> mResampledLeftover; 1.86 +}; 1.87 + 1.88 +} 1.89 +#endif