diff options
| author | Roxie Linden <roxie@lindenlab.com> | 2024-02-05 16:47:47 -0800 | 
|---|---|---|
| committer | Roxie Linden <roxie@lindenlab.com> | 2024-02-22 23:11:37 -0800 | 
| commit | e4c3ca53188152bc12a75f807eabef3dc5e9248b (patch) | |
| tree | fcd19a5c8baf226c435702312b083bbe5a698fae | |
| parent | dfa77d942c52ddf55942afb497a64fbb1d2fccc0 (diff) | |
put observer-based tuning audio level calculation back
| -rw-r--r-- | indra/llwebrtc/llwebrtc.cpp | 53 | ||||
| -rw-r--r-- | indra/llwebrtc/llwebrtc_impl.h | 29 | 
2 files changed, 74 insertions, 8 deletions
| diff --git a/indra/llwebrtc/llwebrtc.cpp b/indra/llwebrtc/llwebrtc.cpp index f0d7f6ad8f..3273786242 100644 --- a/indra/llwebrtc/llwebrtc.cpp +++ b/indra/llwebrtc/llwebrtc.cpp @@ -40,6 +40,46 @@  namespace llwebrtc  { +LLAudioDeviceObserver::LLAudioDeviceObserver() : mMicrophoneEnergy(0.0), mSumVector {0} {} + +float LLAudioDeviceObserver::getMicrophoneEnergy() { return mMicrophoneEnergy; } + +void LLAudioDeviceObserver::OnCaptureData(const void    *audio_samples, +                                          const size_t   num_samples, +                                          const size_t   bytes_per_sample, +                                          const size_t   num_channels, +                                          const uint32_t samples_per_sec) +{ +    float        energy  = 0; +    const short *samples = (const short *) audio_samples; +    for (size_t index = 0; index < num_samples * num_channels; index++) +    { +        float sample = (static_cast<float>(samples[index]) / (float) 32767); +        energy += sample * sample; +    } + +    // smooth it. +    size_t buffer_size = sizeof(mSumVector) / sizeof(mSumVector[0]); +    float  totalSum    = 0; +    int    i; +    for (i = 0; i < (buffer_size - 1); i++) +    { +        mSumVector[i] = mSumVector[i + 1]; +        totalSum += mSumVector[i]; +    } +    mSumVector[i] = energy; +    totalSum += energy; +    mMicrophoneEnergy = std::sqrt(totalSum / (num_samples * buffer_size)); +} + +void LLAudioDeviceObserver::OnRenderData(const void    *audio_samples, +                                         const size_t   num_samples, +                                         const size_t   bytes_per_sample, +                                         const size_t   num_channels, +                                         const uint32_t samples_per_sec) +{ +} +  LLCustomProcessor::LLCustomProcessor() :      mSampleRateHz(0),       mNumChannels(0)  @@ -118,12 +158,13 @@ void LLWebRTCImpl::init()      mSignalingThread->SetName("WebRTCSignalingThread", nullptr);      mSignalingThread->Start(); +    mTuningAudioDeviceObserver = new LLAudioDeviceObserver;      mWorkerThread->PostTask(                              [this]()                              {                                   mTuningDeviceModule =  webrtc::CreateAudioDeviceWithDataObserver( -                                    webrtc::AudioDeviceModule::AudioLayer::kPlatformDefaultAudio, -                                                                          mTaskQueueFactory.get(), nullptr); +                                    webrtc::AudioDeviceModule::AudioLayer::kPlatformDefaultAudio, mTaskQueueFactory.get(), +                std::unique_ptr<webrtc::AudioDeviceDataObserver>(mTuningAudioDeviceObserver));                                  mTuningDeviceModule->Init();                                  mTuningDeviceModule->SetStereoRecording(true);                                  mTuningDeviceModule->SetStereoPlayout(true); @@ -164,9 +205,9 @@ void LLWebRTCImpl::init()                                      mPeerDeviceModule->InitPlayout();                                  }); -    mCustomProcessor = new LLCustomProcessor; +    mPeerCustomProcessor = new LLCustomProcessor;      webrtc::AudioProcessingBuilder apb; -    apb.SetCapturePostProcessing(std::unique_ptr<webrtc::CustomProcessing>(mCustomProcessor)); +    apb.SetCapturePostProcessing(std::unique_ptr<webrtc::CustomProcessing>(mPeerCustomProcessor));      rtc::scoped_refptr<webrtc::AudioProcessing> apm = apb.Create();      webrtc::AudioProcessing::Config             apm_config; @@ -471,9 +512,9 @@ void LLWebRTCImpl::setTuningMode(bool enable)      }  } -float LLWebRTCImpl::getTuningAudioLevel() { return -20 * log10f(mCustomProcessor->getMicrophoneEnergy()); } +float LLWebRTCImpl::getTuningAudioLevel() { return -20 * log10f(mTuningAudioDeviceObserver->getMicrophoneEnergy()); } -float LLWebRTCImpl::getPeerAudioLevel() { return -20 * log10f(mCustomProcessor->getMicrophoneEnergy()); } +float LLWebRTCImpl::getPeerAudioLevel() { return -20 * log10f(mPeerCustomProcessor->getMicrophoneEnergy()); }  //  // Helpers diff --git a/indra/llwebrtc/llwebrtc_impl.h b/indra/llwebrtc/llwebrtc_impl.h index 419482b751..7146267379 100644 --- a/indra/llwebrtc/llwebrtc_impl.h +++ b/indra/llwebrtc/llwebrtc_impl.h @@ -65,6 +65,30 @@ namespace llwebrtc  class LLWebRTCPeerConnectionImpl; +class LLAudioDeviceObserver : public webrtc::AudioDeviceDataObserver +{ +  public: +    LLAudioDeviceObserver(); + +    float getMicrophoneEnergy(); + +    void OnCaptureData(const void    *audio_samples, +                       const size_t   num_samples, +                       const size_t   bytes_per_sample, +                       const size_t   num_channels, +                       const uint32_t samples_per_sec) override; + +    void OnRenderData(const void    *audio_samples, +                      const size_t   num_samples, +                      const size_t   bytes_per_sample, +                      const size_t   num_channels, +                      const uint32_t samples_per_sec) override; + +  protected: +    float mSumVector[30];  // 300 ms of smoothing +    float mMicrophoneEnergy; +}; +  class LLCustomProcessor : public webrtc::CustomProcessing  {    public: @@ -93,7 +117,7 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, public webrtc::AudioDeviceS  {    public:      LLWebRTCImpl() :  -        mCustomProcessor(nullptr), mMute(true) +        mPeerCustomProcessor(nullptr), mMute(true)      {      }      ~LLWebRTCImpl() {} @@ -190,7 +214,8 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, public webrtc::AudioDeviceS      int32_t                                                    mRecordingDevice;      bool                                                       mMute; -    LLCustomProcessor *                                        mCustomProcessor; +    LLAudioDeviceObserver *                                    mTuningAudioDeviceObserver; +    LLCustomProcessor *                                        mPeerCustomProcessor;      // peer connections      std::vector<rtc::scoped_refptr<LLWebRTCPeerConnectionImpl>>     mPeerConnections; | 
