OpusTrackEncoder.h (3216B)
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 OpusTrackEncoder_h_ 7 #define OpusTrackEncoder_h_ 8 9 #include <speex/speex_resampler.h> 10 #include <stdint.h> 11 12 #include "TimeUnits.h" 13 #include "TrackEncoder.h" 14 15 struct OpusEncoder; 16 17 namespace mozilla { 18 19 // Opus meta data structure 20 class OpusMetadata : public TrackMetadataBase { 21 public: 22 // The ID Header of OggOpus. refer to http://wiki.xiph.org/OggOpus. 23 nsTArray<uint8_t> mIdHeader; 24 // The Comment Header of OggOpus. 25 nsTArray<uint8_t> mCommentHeader; 26 int32_t mChannels; 27 float mSamplingFrequency; 28 MetadataKind GetKind() const override { return METADATA_OPUS; } 29 }; 30 31 class OpusTrackEncoder : public AudioTrackEncoder { 32 public: 33 OpusTrackEncoder(TrackRate aTrackRate, 34 MediaQueue<EncodedFrame>& aEncodedDataQueue); 35 virtual ~OpusTrackEncoder(); 36 37 already_AddRefed<TrackMetadataBase> GetMetadata() override; 38 39 /** 40 * The encoder lookahead at 48k rate. 41 */ 42 int GetLookahead() const; 43 44 protected: 45 /** 46 * The number of frames, in the input rate mTrackRate, needed to fill an 47 * encoded opus packet. A frame is a sample per channel. 48 */ 49 int NumInputFramesPerPacket() const override; 50 51 nsresult Init(int aChannels) override; 52 53 /** 54 * Encodes buffered data and pushes it to mEncodedDataQueue. 55 */ 56 nsresult Encode(AudioSegment* aSegment) override; 57 58 /** 59 * The number of frames, in the output rate (see GetOutputSampleRate), needed 60 * to fill an encoded opus packet. A frame is a sample per channel. 61 */ 62 int NumOutputFramesPerPacket() const; 63 64 /** 65 * True if the input needs to be resampled to be fed to the underlying opus 66 * encoder. 67 */ 68 bool NeedsResampler() const; 69 70 public: 71 /** 72 * Get the samplerate of the data to be fed to the Opus encoder. This might be 73 * different from the input samplerate if resampling occurs. 74 */ 75 const TrackRate mOutputSampleRate; 76 77 private: 78 /** 79 * The Opus encoder from libopus. 80 */ 81 OpusEncoder* mEncoder; 82 83 /** 84 * Total samples of delay added by codec (in rate mOutputSampleRate), can 85 * be queried by the encoder. From the perspective of decoding, real data 86 * begins this many samples late, so the encoder needs to append this many 87 * null samples to the end of stream, in order to align the time of input and 88 * output. 89 */ 90 int mLookahead; 91 92 /** 93 * Number of mLookahead samples that has been written. When non-zero and equal 94 * to mLookahead, encoding is complete. 95 */ 96 int mLookaheadWritten; 97 98 /** 99 * If the input sample rate does not divide 48kHz evenly, the input data are 100 * resampled. 101 */ 102 SpeexResamplerState* mResampler; 103 104 /** 105 * Store the resampled frames that don't fit into an Opus packet duration. 106 * They will be prepended to the resampled frames next encoding cycle. 107 */ 108 nsTArray<AudioDataValue> mResampledLeftover; 109 110 /** 111 * Number of audio frames encoded, in kOpusSamplingRate. 112 */ 113 uint64_t mNumOutputFrames; 114 }; 115 116 } // namespace mozilla 117 118 #endif