FFmpegAudioEncoder.h (2970B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_MEDIA_PLATFORMS_FFMPEG_FFMPEGAUDIOENCODER_H_ 8 #define DOM_MEDIA_PLATFORMS_FFMPEG_FFMPEGAUDIOENCODER_H_ 9 10 #include "FFmpegDataEncoder.h" 11 #include "FFmpegLibWrapper.h" 12 #include "PlatformEncoderModule.h" 13 #include "TimedPacketizer.h" 14 15 // This must be the last header included 16 #include "FFmpegLibs.h" 17 #include "speex/speex_resampler.h" 18 19 namespace mozilla { 20 21 template <int V> 22 class FFmpegAudioEncoder : public FFmpegDataEncoder<V> {}; 23 24 template <> 25 class FFmpegAudioEncoder<LIBAV_VER> : public FFmpegDataEncoder<LIBAV_VER> { 26 public: 27 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FFmpegAudioEncoder, final); 28 29 FFmpegAudioEncoder(const FFmpegLibWrapper* aLib, AVCodecID aCodecID, 30 const RefPtr<TaskQueue>& aTaskQueue, 31 const EncoderConfig& aConfig); 32 33 RefPtr<InitPromise> Init() override; 34 35 nsCString GetDescriptionName() const override; 36 37 protected: 38 virtual ~FFmpegAudioEncoder() = default; 39 // Methods only called on mTaskQueue. 40 virtual MediaResult InitEncoder() override; 41 #if LIBAVCODEC_VERSION_MAJOR >= 58 42 Result<EncodedData, MediaResult> EncodeOnePacket(Span<float> aSamples, 43 media::TimeUnit aPts); 44 Result<EncodedData, MediaResult> EncodeInputWithModernAPIs( 45 RefPtr<const MediaData> aSample) override; 46 Result<MediaDataEncoder::EncodedData, MediaResult> DrainWithModernAPIs() 47 override; 48 #endif 49 virtual Result<RefPtr<MediaRawData>, MediaResult> ToMediaRawData( 50 AVPacket* aPacket) override; 51 Result<already_AddRefed<MediaByteBuffer>, MediaResult> GetExtraData( 52 AVPacket* aPacket) override; 53 // Most audio codecs (except PCM) require a very specific frame size. 54 Maybe<TimedPacketizer<float, float>> mPacketizer; 55 // A temporary buffer kept around for shuffling audio frames, resampling, 56 // packetization, etc. 57 nsTArray<float> mTempBuffer; 58 // The pts of the first packet this encoder has seen, to be able to properly 59 // mark encoder delay as such. 60 media::TimeUnit mFirstPacketPts{media::TimeUnit::Invalid()}; 61 struct ResamplerDestroy { 62 void operator()(SpeexResamplerState* aResampler); 63 }; 64 // Rate at which this instance has been configured, which might be different 65 // from the rate the underlying encoder is running at. 66 int mInputSampleRate = 0; 67 UniquePtr<SpeexResamplerState, ResamplerDestroy> mResampler; 68 uint64_t mPacketsDelivered = 0; 69 // Threshold under which a packet isn't returned to the encoder user, 70 // because it is known to be silent and DTX is enabled. 71 int mDtxThreshold = 0; 72 }; 73 74 } // namespace mozilla 75 76 #endif // DOM_MEDIA_PLATFORMS_FFMPEG_FFMPEGAUDIOENCODER_H_