summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/mediastream/media_stream_audio_level_calculator.h
blob: 1a1f97e03a83dc5725998732c57d53cee7a9c3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIASTREAM_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIASTREAM_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_

#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "third_party/blink/renderer/platform/platform_export.h"

namespace media {
class AudioBus;
}

namespace blink {

// This class is used by the WebRtcAudioCapturer to calculate the level of the
// audio signal. And the audio level will be eventually used by the volume
// animation UI.
//
// The algorithm used by this class is the same as how it is done in
// third_party/webrtc/voice_engine/level_indicator.cc.
class PLATFORM_EXPORT MediaStreamAudioLevelCalculator {
 public:
  // Provides thread-safe access to the current signal level.  This object is
  // intended to be passed to modules running on other threads that poll for the
  // current signal level.
  class PLATFORM_EXPORT Level : public base::RefCountedThreadSafe<Level> {
   public:
    float GetCurrent() const;

   private:
    friend class MediaStreamAudioLevelCalculator;
    friend class base::RefCountedThreadSafe<Level>;

    Level();
    ~Level();

    void Set(float level);

    mutable base::Lock lock_;
    float level_;
  };

  MediaStreamAudioLevelCalculator();
  ~MediaStreamAudioLevelCalculator();

  const scoped_refptr<Level>& level() const { return level_; }

  // Scans the audio signal in |audio_bus| and computes a new signal level
  // exposed by Level.  If |assume_nonzero_energy| is true, then a completely
  // zero'ed-out |audio_bus| will be accounted for as having a very faint,
  // non-zero level.
  void Calculate(const media::AudioBus& audio_bus, bool assume_nonzero_energy);

 private:
  int counter_;
  float max_amplitude_;
  const scoped_refptr<Level> level_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIASTREAM_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_