blob: acd1231fe2f7211b40046ff0bf904a57a0cb1b92 [file] [log] [blame]
henrik.lundin92a7a182017-03-07 09:58:551/*
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 10:18:3111#ifndef AUDIO_AUDIO_LEVEL_H_
12#define AUDIO_AUDIO_LEVEL_H_
henrik.lundin92a7a182017-03-07 09:58:5513
Markus Handell62872802020-07-06 13:15:0714#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/thread_annotations.h"
henrik.lundin92a7a182017-03-07 09:58:5516
17namespace webrtc {
18
19class AudioFrame;
20namespace voe {
21
Henrik Boströmd2c336f2019-07-03 15:11:1022// 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 09:58:5525class AudioLevel {
26 public:
27 AudioLevel();
28 ~AudioLevel();
Henrik Boströmd2c336f2019-07-03 15:11:1029 void Reset();
henrik.lundin92a7a182017-03-07 09:58:5530
Henrik Boströmd2c336f2019-07-03 15:11:1031 // 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 09:58:5535 // Called on "API thread(s)" from APIs like VoEBase::CreateChannel(),
Henrik Boströmd2c336f2019-07-03 15:11:1036 // VoEBase::StopSend().
henrik.lundin92a7a182017-03-07 09:58:5537 int16_t LevelFullRange() const;
Henrik Boströmd2c336f2019-07-03 15:11:1038 void ResetLevelFullRange();
zstein3c451862017-07-20 16:57:4239 // See the description for "totalAudioEnergy" in the WebRTC stats spec
Henrik Boströmd2c336f2019-07-03 15:11:1040 // (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 16:57:4251 double TotalEnergy() const;
52 double TotalDuration() const;
henrik.lundin92a7a182017-03-07 09:58:5553
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 16:57:4257 void ComputeLevel(const AudioFrame& audioFrame, double duration);
henrik.lundin92a7a182017-03-07 09:58:5558
59 private:
60 enum { kUpdateFrequency = 10 };
61
Markus Handell62872802020-07-06 13:15:0762 mutable Mutex mutex_;
henrik.lundin92a7a182017-03-07 09:58:5563
Markus Handell62872802020-07-06 13:15:0764 int16_t abs_max_ RTC_GUARDED_BY(mutex_);
65 int16_t count_ RTC_GUARDED_BY(mutex_);
66 int16_t current_level_full_range_ RTC_GUARDED_BY(mutex_);
zstein3c451862017-07-20 16:57:4267
Markus Handell62872802020-07-06 13:15:0768 double total_energy_ RTC_GUARDED_BY(mutex_) = 0.0;
69 double total_duration_ RTC_GUARDED_BY(mutex_) = 0.0;
henrik.lundin92a7a182017-03-07 09:58:5570};
71
72} // namespace voe
73} // namespace webrtc
74
Fredrik Solenberga8b7c7f2018-01-17 10:18:3175#endif // AUDIO_AUDIO_LEVEL_H_