audio_opus_encoder.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_AUDIO_AUDIO_OPUS_ENCODER_H_
  5. #define MEDIA_AUDIO_AUDIO_OPUS_ENCODER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "media/base/audio_bus.h"
  9. #include "media/base/audio_converter.h"
  10. #include "media/base/audio_encoder.h"
  11. #include "media/base/audio_timestamp_helper.h"
  12. #include "third_party/opus/src/include/opus.h"
  13. namespace media {
  14. class ChannelMixer;
  15. class ConvertingAudioFifo;
  16. using OpusEncoderDeleterType = void (*)(OpusEncoder* encoder_ptr);
  17. using OwnedOpusEncoder = std::unique_ptr<OpusEncoder, OpusEncoderDeleterType>;
  18. // Performs Opus encoding of the input audio. The input audio is converted to a
  19. // a format suitable for Opus before it is passed to the libopus encoder
  20. // instance to do the actual encoding.
  21. class MEDIA_EXPORT AudioOpusEncoder : public AudioEncoder {
  22. public:
  23. AudioOpusEncoder();
  24. AudioOpusEncoder(const AudioOpusEncoder&) = delete;
  25. AudioOpusEncoder& operator=(const AudioOpusEncoder&) = delete;
  26. ~AudioOpusEncoder() override;
  27. // AudioEncoder:
  28. void Initialize(const Options& options,
  29. OutputCB output_callback,
  30. EncoderStatusCB done_cb) override;
  31. void Encode(std::unique_ptr<AudioBus> audio_bus,
  32. base::TimeTicks capture_time,
  33. EncoderStatusCB done_cb) override;
  34. void Flush(EncoderStatusCB done_cb) override;
  35. static constexpr int kMinBitrate = 6000;
  36. private:
  37. friend class AudioEncodersTest;
  38. // Called synchronously by |fifo_| once enough audio frames have been
  39. // buffered in |fifo_|. Calls libopus to do actual encoding.
  40. void OnFifoOutput(AudioBus* audio_bus);
  41. CodecDescription PrepareExtraData();
  42. EncoderStatus::Or<OwnedOpusEncoder> CreateOpusEncoder();
  43. AudioParameters input_params_;
  44. // Output parameters after audio conversion. This may differ from the input
  45. // params in the number of channels, sample rate, and the frames per buffer.
  46. // (See CreateOpusInputParams() in the .cc file for details).
  47. AudioParameters converted_params_;
  48. std::unique_ptr<ConvertingAudioFifo> fifo_;
  49. // Used to mix incoming Encode() buffers to match the expect input channel
  50. // count.
  51. std::unique_ptr<ChannelMixer> mixer_;
  52. AudioParameters mixer_input_params_;
  53. // Buffer for passing AudioBus data from the converter to the encoder.
  54. std::vector<float> buffer_;
  55. // The actual libopus encoder instance. This is nullptr if creating the
  56. // encoder fails.
  57. OwnedOpusEncoder opus_encoder_;
  58. // Keeps track of the timestamps for the each |output_callback_|
  59. std::unique_ptr<AudioTimestampHelper> timestamp_tracker_;
  60. // Callback for reporting completion and status of the current Flush() or
  61. // Encoder()
  62. EncoderStatusCB current_done_cb_;
  63. // True if the next output needs to have extra_data in it, only happens once.
  64. bool need_to_emit_extra_data_ = true;
  65. };
  66. } // namespace media
  67. #endif // MEDIA_AUDIO_AUDIO_OPUS_ENCODER_H_