blob: 430edb17033ef3701ece97db0d9ca751deabfcac [file] [log] [blame]
henrik.lundin92a7a182017-03-07 01:58:55 -08001/*
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
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010011#ifndef AUDIO_AUDIO_LEVEL_H_
12#define AUDIO_AUDIO_LEVEL_H_
henrik.lundin92a7a182017-03-07 01:58:55 -080013
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/thread_annotations.h"
henrik.lundin92a7a182017-03-07 01:58:55 -080016
17namespace webrtc {
18
19class AudioFrame;
20namespace voe {
21
Henrik Boströmd2c336f2019-07-03 17:11:10 +020022// This class is thread-safe. However, TotalEnergy() and TotalDuration() are
23// related, so if you call ComputeLevel() on a different thread than you read
24// these values, you still need to use lock to read them as a pair.
henrik.lundin92a7a182017-03-07 01:58:55 -080025class AudioLevel {
26 public:
27 AudioLevel();
28 ~AudioLevel();
Henrik Boströmd2c336f2019-07-03 17:11:10 +020029 void Reset();
henrik.lundin92a7a182017-03-07 01:58:55 -080030
Henrik Boströmd2c336f2019-07-03 17:11:10 +020031 // Returns the current audio level linearly [0,32767], which gets updated
32 // every "kUpdateFrequency+1" call to ComputeLevel() based on the maximum
33 // audio level of any audio frame, decaying by a factor of 1/4 each time
34 // LevelFullRange() gets updated.
henrik.lundin92a7a182017-03-07 01:58:55 -080035 // Called on "API thread(s)" from APIs like VoEBase::CreateChannel(),
Henrik Boströmd2c336f2019-07-03 17:11:10 +020036 // VoEBase::StopSend().
henrik.lundin92a7a182017-03-07 01:58:55 -080037 int16_t LevelFullRange() const;
Henrik Boströmd2c336f2019-07-03 17:11:10 +020038 void ResetLevelFullRange();
zstein3c451862017-07-20 09:57:42 -070039 // See the description for "totalAudioEnergy" in the WebRTC stats spec
Henrik Boströmd2c336f2019-07-03 17:11:10 +020040 // (https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy)
41 // In our implementation, the total audio energy increases by the
42 // energy-equivalent of LevelFullRange() at the time of ComputeLevel(), rather
43 // than the energy of the samples in that specific audio frame. As a result,
44 // we may report a higher audio energy and audio level than the spec mandates.
45 // TODO(https://crbug.com/webrtc/10784): We should either do what the spec
46 // says or update the spec to match our implementation. If we want to have a
47 // decaying audio level we should probably update both the spec and the
48 // implementation to reduce the complexity of the definition. If we want to
49 // continue to have decaying audio we should have unittests covering the
50 // behavior of the decay.
zstein3c451862017-07-20 09:57:42 -070051 double TotalEnergy() const;
52 double TotalDuration() const;
henrik.lundin92a7a182017-03-07 01:58:55 -080053
54 // Called on a native capture audio thread (platform dependent) from the
55 // AudioTransport::RecordedDataIsAvailable() callback.
56 // In Chrome, this method is called on the AudioInputDevice thread.
zstein3c451862017-07-20 09:57:42 -070057 void ComputeLevel(const AudioFrame& audioFrame, double duration);
henrik.lundin92a7a182017-03-07 01:58:55 -080058
59 private:
60 enum { kUpdateFrequency = 10 };
61
62 rtc::CriticalSection crit_sect_;
63
danilchapa37de392017-09-09 04:17:22 -070064 int16_t abs_max_ RTC_GUARDED_BY(crit_sect_);
65 int16_t count_ RTC_GUARDED_BY(crit_sect_);
danilchapa37de392017-09-09 04:17:22 -070066 int16_t current_level_full_range_ RTC_GUARDED_BY(crit_sect_);
zstein3c451862017-07-20 09:57:42 -070067
danilchapa37de392017-09-09 04:17:22 -070068 double total_energy_ RTC_GUARDED_BY(crit_sect_) = 0.0;
69 double total_duration_ RTC_GUARDED_BY(crit_sect_) = 0.0;
henrik.lundin92a7a182017-03-07 01:58:55 -080070};
71
72} // namespace voe
73} // namespace webrtc
74
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010075#endif // AUDIO_AUDIO_LEVEL_H_