summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_input_stream_impl.h
blob: 64980a979fedb0a0c4ed2c3c78727c0fa1a2f919 (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
66
67
68
69
70
71
// Copyright (c) 2012 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 MEDIA_AUDIO_AUDIO_INPUT_STREAM_IMPL_H_
#define MEDIA_AUDIO_AUDIO_INPUT_STREAM_IMPL_H_

#include "base/compiler_specific.h"
#include "base/synchronization/lock.h"
#include "base/time.h"
#include "media/audio/audio_io.h"

namespace media {

// AudioInputStreamImpl implements platform-independent parts of the
// AudioInputStream interface. Each platform dependent implementation
// should derive from this class.
// TODO(henrika): we can probably break out more parts from our current
// AudioInputStream implementation and move out to this class.
class MEDIA_EXPORT AudioInputStreamImpl : public AudioInputStream {
 public:
  AudioInputStreamImpl();
  virtual ~AudioInputStreamImpl();

  // Sets the automatic gain control (AGC) to on or off. When AGC is enabled,
  // the microphone volume is queried periodically and the volume level is
  // provided in each AudioInputCallback::OnData() callback and fed to the
  // render-side AGC.
  virtual void SetAutomaticGainControl(bool enabled) OVERRIDE;

  // Gets the current automatic gain control state.
  virtual bool GetAutomaticGainControl() OVERRIDE;

 protected:
  // Stores a new volume level by asking the audio hardware.
  // This method only has an effect if AGC is enabled.
  void UpdateAgcVolume();

  // Gets the latest stored volume level if AGC is enabled and if
  // more than one second has passed since the volume was updated the last time.
  void QueryAgcVolume(double* normalized_volume);

 private:
  // Takes a volume sample and stores it in |normalized_volume_|.
  void GetNormalizedVolume();

  // True when automatic gain control is enabled, false otherwise.
  // Guarded by |lock_|.
  bool agc_is_enabled_;

  // Stores the maximum volume which is used for normalization to a volume
  // range of [0.0, 1.0].
  double max_volume_;

  // Contains last result of internal call to GetVolume(). We save resources
  // but not querying the capture volume for each callback. Guarded by |lock_|.
  // The range is normalized to [0.0, 1.0].
  double normalized_volume_;

  // Protects |agc_is_enabled_| and |volume_| .
  base::Lock lock_;

  // Keeps track of the last time the microphone volume level was queried.
  base::Time last_volume_update_time_;

  DISALLOW_COPY_AND_ASSIGN(AudioInputStreamImpl);
};

}  // namespace media

#endif  // MEDIA_AUDIO_AUDIO_INPUT_STREAM_IMPL_H_