Webrtc混音接口使用

2023-03-17 15:05:48 浏览数 (1)

❝Webrtc的混音主要由AudioMixer实现,可混合多条音频流。❞

1. AudioMixer接口类

代码语言:javascript复制
class AudioMixer : public rtc::RefCountInterface {
 public:
  // A callback class that all mixer participants must inherit from/implement.
  class Source {
   public:
    enum class AudioFrameInfo {
      kNormal,  // The samples in audio_frame are valid and should be used.
      kMuted,   // The samples in audio_frame should not be used, but
                // should be implicitly interpreted as zero. Other
                // fields in audio_frame may be read and should
                // contain meaningful values.
      kError,   // The audio_frame will not be used.
    };

    // Overwrites |audio_frame|. The data_ field is overwritten with
    // 10 ms of new audio (either 1 or 2 interleaved channels) at
    // |sample_rate_hz|. All fields in |audio_frame| must be updated.
    virtual AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
                                                 AudioFrame* audio_frame) = 0;

    // A way for a mixer implementation to distinguish participants.
    virtual int Ssrc() const = 0;

    // A way for this source to say that GetAudioFrameWithInfo called
    // with this sample rate or higher will not cause quality loss.
    virtual int PreferredSampleRate() const = 0;

    virtual ~Source() {}
  };

  // Returns true if adding was successful. A source is never added
  // twice. Addition and removal can happen on different threads.
  virtual bool AddSource(Source* audio_source) = 0;

  // Removal is never attempted if a source has not been successfully
  // added to the mixer.
  virtual void RemoveSource(Source* audio_source) = 0;

  // Performs mixing by asking registered audio sources for audio. The
  // mixed result is placed in the provided AudioFrame. This method
  // will only be called from a single thread. The channels argument
  // specifies the number of channels of the mix result. The mixer
  // should mix at a rate that doesn't cause quality loss of the
  // sources' audio. The mixing rate is one of the rates listed in
  // AudioProcessing::NativeRate. All fields in
  // |audio_frame_for_mixing| must be updated.
  virtual void Mix(size_t number_of_channels,
                   AudioFrame* audio_frame_for_mixing) = 0;

 protected:
  // Since the mixer is reference counted, the destructor may be
  // called from any thread.
  ~AudioMixer() override {}
};

2. AudioMixer实现类

代码语言:javascript复制
class AudioMixerImpl : public AudioMixer {
 public:
  ...
  static rtc::scoped_refptr<AudioMixerImpl> Create();
  ...
  // AudioMixer functions
  bool AddSource(Source* audio_source) override;
  void RemoveSource(Source* audio_source) override;

  void Mix(size_t number_of_channels,
           AudioFrame* audio_frame_for_mixing) override
      RTC_LOCKS_EXCLUDED(crit_);
  ...
};

3. 混音实现

  通过使用AddSource接口添加不同的音频流,然后通过调用Mix接口进行混音操作,其中AudioFrame* audio_frame_for_mixing是混音数据。

  在AddSource添加的流(AudioMixer::Source)中,我们还要分别实现以下接口:

代码语言:javascript复制
// audio_frame必须在其实现中更新,用于AudioMixer的Mix接口回调。
virtual AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
                                             AudioFrame* audio_frame) = 0;
//混频器实现区分参与者混音的一种方法。
virtual int Ssrc() const = 0;

// 表示以此采样率或更高的采样率调用GetAudioFrameWithInfo的方式不会导致音质下降。
virtual int PreferredSampleRate() const = 0;

  将两个单声道16k的PCM文件混音简略代码:

代码语言:javascript复制
// 创建AudioMixer::Source实现类
class Source1 : public AudioMixer::Source 
{
public:
    AudioFrameInfo GetAudioFrameWithInfo(int, AudioFrame *audio_frame)
    {
        // 读取音频文件并填充数据
     audio_frame->UpdateFrame(0,
                              fileData, // 音频文件数据
                              16000/100,
                              16000,
                              AudioFrame::SpeechType::kNormalSpeech,
                              AudioFrame::VADActivity::kVadUnknown,
                              1);
     return AudioFrameInfo::kNormal;
    }  
  
 int Ssrc() const 
 {
  return -1;
 }
 
 int PreferredSampleRate() const
 {
  return 16000;
 }
};

// 创建混音实例
audioMixer = AudioMixerImpl::Create();
audioMixer->AddSource(new Source1);
audioMixer->AddDource(new Source2);

// 混音操作
while (true) {
    AudioFrame audioFrame;
 audioMixer.Mix(1, &audioFrame);
    
    /* 混音数据长度 */
    int outMixingSize = audioFrame.num_channels * audioFrame.sample_per_channel_;
    /* 混音数据 */
    const int16_t * outMixingData = audioFrame.data();
}

0 人点赞