pulse_input.h revision ab8f6f0bd665d3c1ff476eb06c58c42630e462d4
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MEDIA_AUDIO_PULSE_PULSE_INPUT_H_
6#define MEDIA_AUDIO_PULSE_PULSE_INPUT_H_
7
8#include <pulse/pulseaudio.h>
9#include <string>
10
11#include "base/threading/thread_checker.h"
12#include "media/audio/agc_audio_stream.h"
13#include "media/audio/audio_device_name.h"
14#include "media/audio/audio_io.h"
15#include "media/audio/audio_parameters.h"
16#include "media/base/audio_block_fifo.h"
17
18namespace media {
19
20class AudioManagerPulse;
21
22class PulseAudioInputStream : public AgcAudioStream<AudioInputStream> {
23 public:
24  PulseAudioInputStream(AudioManagerPulse* audio_manager,
25                        const std::string& device_name,
26                        const AudioParameters& params,
27                        pa_threaded_mainloop* mainloop,
28                        pa_context* context);
29
30  virtual ~PulseAudioInputStream();
31
32  // Implementation of AudioInputStream.
33  virtual bool Open() OVERRIDE;
34  virtual void Start(AudioInputCallback* callback) OVERRIDE;
35  virtual void Stop() OVERRIDE;
36  virtual void Close() OVERRIDE;
37  virtual double GetMaxVolume() OVERRIDE;
38  virtual void SetVolume(double volume) OVERRIDE;
39  virtual double GetVolume() OVERRIDE;
40  virtual bool IsMuted() OVERRIDE;
41
42 private:
43  // PulseAudio Callbacks.
44  static void ReadCallback(pa_stream* handle, size_t length, void* user_data);
45  static void StreamNotifyCallback(pa_stream* stream, void* user_data);
46  static void VolumeCallback(pa_context* context, const pa_source_info* info,
47                             int error, void* user_data);
48  static void MuteCallback(pa_context* context,
49                           const pa_source_info* info,
50                           int error,
51                           void* user_data);
52
53  // Helper for the ReadCallback.
54  void ReadData();
55
56  // Utility method used by GetVolume() and IsMuted().
57  bool GetSourceInformation(pa_source_info_cb_t callback);
58
59  AudioManagerPulse* audio_manager_;
60  AudioInputCallback* callback_;
61  std::string device_name_;
62  AudioParameters params_;
63  int channels_;
64  double volume_;
65  bool stream_started_;
66
67  // Set to true in IsMuted() if user has muted the selected microphone in the
68  // sound settings UI.
69  bool muted_;
70
71  // Holds the data from the OS.
72  AudioBlockFifo fifo_;
73
74  // PulseAudio API structs.
75  pa_threaded_mainloop* pa_mainloop_; // Weak.
76  pa_context* pa_context_;  // Weak.
77  pa_stream* handle_;
78
79  // Flag indicating the state of the context has been changed.
80  bool context_state_changed_;
81
82  base::ThreadChecker thread_checker_;
83
84  DISALLOW_COPY_AND_ASSIGN(PulseAudioInputStream);
85};
86
87}  // namespace media
88
89#endif  // MEDIA_AUDIO_PULSE_PULSE_INPUT_H_
90