1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef AUDIO_AUDIO_LEVEL_H_ 12*d9f75844SAndroid Build Coastguard Worker #define AUDIO_AUDIO_LEVEL_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h" 15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h" 16*d9f75844SAndroid Build Coastguard Worker 17*d9f75844SAndroid Build Coastguard Worker namespace webrtc { 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard Worker class AudioFrame; 20*d9f75844SAndroid Build Coastguard Worker namespace voe { 21*d9f75844SAndroid Build Coastguard Worker 22*d9f75844SAndroid Build Coastguard Worker // This class is thread-safe. However, TotalEnergy() and TotalDuration() are 23*d9f75844SAndroid Build Coastguard Worker // related, so if you call ComputeLevel() on a different thread than you read 24*d9f75844SAndroid Build Coastguard Worker // these values, you still need to use lock to read them as a pair. 25*d9f75844SAndroid Build Coastguard Worker class AudioLevel { 26*d9f75844SAndroid Build Coastguard Worker public: 27*d9f75844SAndroid Build Coastguard Worker AudioLevel(); 28*d9f75844SAndroid Build Coastguard Worker ~AudioLevel(); 29*d9f75844SAndroid Build Coastguard Worker void Reset(); 30*d9f75844SAndroid Build Coastguard Worker 31*d9f75844SAndroid Build Coastguard Worker // Returns the current audio level linearly [0,32767], which gets updated 32*d9f75844SAndroid Build Coastguard Worker // every "kUpdateFrequency+1" call to ComputeLevel() based on the maximum 33*d9f75844SAndroid Build Coastguard Worker // audio level of any audio frame, decaying by a factor of 1/4 each time 34*d9f75844SAndroid Build Coastguard Worker // LevelFullRange() gets updated. 35*d9f75844SAndroid Build Coastguard Worker // Called on "API thread(s)" from APIs like VoEBase::CreateChannel(), 36*d9f75844SAndroid Build Coastguard Worker // VoEBase::StopSend(). 37*d9f75844SAndroid Build Coastguard Worker int16_t LevelFullRange() const; 38*d9f75844SAndroid Build Coastguard Worker void ResetLevelFullRange(); 39*d9f75844SAndroid Build Coastguard Worker // See the description for "totalAudioEnergy" in the WebRTC stats spec 40*d9f75844SAndroid Build Coastguard Worker // (https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy) 41*d9f75844SAndroid Build Coastguard Worker // In our implementation, the total audio energy increases by the 42*d9f75844SAndroid Build Coastguard Worker // energy-equivalent of LevelFullRange() at the time of ComputeLevel(), rather 43*d9f75844SAndroid Build Coastguard Worker // than the energy of the samples in that specific audio frame. As a result, 44*d9f75844SAndroid Build Coastguard Worker // we may report a higher audio energy and audio level than the spec mandates. 45*d9f75844SAndroid Build Coastguard Worker // TODO(https://crbug.com/webrtc/10784): We should either do what the spec 46*d9f75844SAndroid Build Coastguard Worker // says or update the spec to match our implementation. If we want to have a 47*d9f75844SAndroid Build Coastguard Worker // decaying audio level we should probably update both the spec and the 48*d9f75844SAndroid Build Coastguard Worker // implementation to reduce the complexity of the definition. If we want to 49*d9f75844SAndroid Build Coastguard Worker // continue to have decaying audio we should have unittests covering the 50*d9f75844SAndroid Build Coastguard Worker // behavior of the decay. 51*d9f75844SAndroid Build Coastguard Worker double TotalEnergy() const; 52*d9f75844SAndroid Build Coastguard Worker double TotalDuration() const; 53*d9f75844SAndroid Build Coastguard Worker 54*d9f75844SAndroid Build Coastguard Worker // Called on a native capture audio thread (platform dependent) from the 55*d9f75844SAndroid Build Coastguard Worker // AudioTransport::RecordedDataIsAvailable() callback. 56*d9f75844SAndroid Build Coastguard Worker // In Chrome, this method is called on the AudioInputDevice thread. 57*d9f75844SAndroid Build Coastguard Worker void ComputeLevel(const AudioFrame& audioFrame, double duration); 58*d9f75844SAndroid Build Coastguard Worker 59*d9f75844SAndroid Build Coastguard Worker private: 60*d9f75844SAndroid Build Coastguard Worker enum { kUpdateFrequency = 10 }; 61*d9f75844SAndroid Build Coastguard Worker 62*d9f75844SAndroid Build Coastguard Worker mutable Mutex mutex_; 63*d9f75844SAndroid Build Coastguard Worker 64*d9f75844SAndroid Build Coastguard Worker int16_t abs_max_ RTC_GUARDED_BY(mutex_); 65*d9f75844SAndroid Build Coastguard Worker int16_t count_ RTC_GUARDED_BY(mutex_); 66*d9f75844SAndroid Build Coastguard Worker int16_t current_level_full_range_ RTC_GUARDED_BY(mutex_); 67*d9f75844SAndroid Build Coastguard Worker 68*d9f75844SAndroid Build Coastguard Worker double total_energy_ RTC_GUARDED_BY(mutex_) = 0.0; 69*d9f75844SAndroid Build Coastguard Worker double total_duration_ RTC_GUARDED_BY(mutex_) = 0.0; 70*d9f75844SAndroid Build Coastguard Worker }; 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard Worker } // namespace voe 73*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc 74*d9f75844SAndroid Build Coastguard Worker 75*d9f75844SAndroid Build Coastguard Worker #endif // AUDIO_AUDIO_LEVEL_H_ 76