audio_level.h 2.9 KB

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