audio_mixer_impl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
  11. #define MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include <vector>
  15. #include "api/audio/audio_frame.h"
  16. #include "api/audio/audio_mixer.h"
  17. #include "api/scoped_refptr.h"
  18. #include "modules/audio_mixer/frame_combiner.h"
  19. #include "modules/audio_mixer/output_rate_calculator.h"
  20. #include "rtc_base/constructor_magic.h"
  21. #include "rtc_base/race_checker.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. #include "rtc_base/thread_annotations.h"
  24. namespace webrtc {
  25. typedef std::vector<AudioFrame*> AudioFrameList;
  26. class AudioMixerImpl : public AudioMixer {
  27. public:
  28. struct SourceStatus {
  29. SourceStatus(Source* audio_source, bool is_mixed, float gain)
  30. : audio_source(audio_source), is_mixed(is_mixed), gain(gain) {}
  31. Source* audio_source = nullptr;
  32. bool is_mixed = false;
  33. float gain = 0.0f;
  34. // A frame that will be passed to audio_source->GetAudioFrameWithInfo.
  35. AudioFrame audio_frame;
  36. };
  37. using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
  38. // AudioProcessing only accepts 10 ms frames.
  39. static const int kFrameDurationInMs = 10;
  40. enum : int { kMaximumAmountOfMixedAudioSources = 3 };
  41. static rtc::scoped_refptr<AudioMixerImpl> Create();
  42. static rtc::scoped_refptr<AudioMixerImpl> Create(
  43. std::unique_ptr<OutputRateCalculator> output_rate_calculator,
  44. bool use_limiter);
  45. ~AudioMixerImpl() override;
  46. // AudioMixer functions
  47. bool AddSource(Source* audio_source) override;
  48. void RemoveSource(Source* audio_source) override;
  49. void Mix(size_t number_of_channels,
  50. AudioFrame* audio_frame_for_mixing) override
  51. RTC_LOCKS_EXCLUDED(mutex_);
  52. // Returns true if the source was mixed last round. Returns
  53. // false and logs an error if the source was never added to the
  54. // mixer.
  55. bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
  56. protected:
  57. AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,
  58. bool use_limiter);
  59. private:
  60. // Set mixing frequency through OutputFrequencyCalculator.
  61. void CalculateOutputFrequency();
  62. // Get mixing frequency.
  63. int OutputFrequency() const;
  64. // Compute what audio sources to mix from audio_source_list_. Ramp
  65. // in and out. Update mixed status. Mixes up to
  66. // kMaximumAmountOfMixedAudioSources audio sources.
  67. AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  68. // The critical section lock guards audio source insertion and
  69. // removal, which can be done from any thread. The race checker
  70. // checks that mixing is done sequentially.
  71. mutable Mutex mutex_;
  72. rtc::RaceChecker race_checker_;
  73. std::unique_ptr<OutputRateCalculator> output_rate_calculator_;
  74. // The current sample frequency and sample size when mixing.
  75. int output_frequency_ RTC_GUARDED_BY(race_checker_);
  76. size_t sample_size_ RTC_GUARDED_BY(race_checker_);
  77. // List of all audio sources. Note all lists are disjunct
  78. SourceStatusList audio_source_list_ RTC_GUARDED_BY(mutex_); // May be mixed.
  79. // Component that handles actual adding of audio frames.
  80. FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_);
  81. RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
  82. };
  83. } // namespace webrtc
  84. #endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_