pulse_input.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 <string>
9
10#include "base/threading/thread_checker.h"
11#include "media/audio/agc_audio_stream.h"
12#include "media/audio/audio_device_name.h"
13#include "media/audio/audio_io.h"
14#include "media/audio/audio_parameters.h"
15
16struct pa_context;
17struct pa_source_info;
18struct pa_stream;
19struct pa_threaded_mainloop;
20
21namespace media {
22
23class AudioManagerPulse;
24class SeekableBuffer;
25
26class PulseAudioInputStream : public AgcAudioStream<AudioInputStream> {
27 public:
28  PulseAudioInputStream(AudioManagerPulse* audio_manager,
29                        const std::string& device_name,
30                        const AudioParameters& params,
31                        pa_threaded_mainloop* mainloop,
32                        pa_context* context);
33
34  virtual ~PulseAudioInputStream();
35
36  // Implementation of AudioInputStream.
37  virtual bool Open() OVERRIDE;
38  virtual void Start(AudioInputCallback* callback) OVERRIDE;
39  virtual void Stop() OVERRIDE;
40  virtual void Close() OVERRIDE;
41  virtual double GetMaxVolume() OVERRIDE;
42  virtual void SetVolume(double volume) OVERRIDE;
43  virtual double GetVolume() OVERRIDE;
44
45 private:
46  // PulseAudio Callbacks.
47  static void ReadCallback(pa_stream* handle, size_t length, void* user_data);
48  static void StreamNotifyCallback(pa_stream* stream, void* user_data);
49  static void VolumeCallback(pa_context* context, const pa_source_info* info,
50                             int error, void* user_data);
51
52  // Helper for the ReadCallback.
53  void ReadData();
54
55  AudioManagerPulse* audio_manager_;
56  AudioInputCallback* callback_;
57  std::string device_name_;
58  AudioParameters params_;
59  int channels_;
60  double volume_;
61  bool stream_started_;
62
63  // Holds the data from the OS.
64  scoped_ptr<media::SeekableBuffer> buffer_;
65
66  // Temporary storage for recorded data. It gets a packet of data from
67  // |buffer_| and deliver the data to OnData() callback.
68  scoped_ptr<uint8[]> audio_data_buffer_;
69
70  // PulseAudio API structs.
71  pa_threaded_mainloop* pa_mainloop_; // Weak.
72  pa_context* pa_context_;  // Weak.
73  pa_stream* handle_;
74
75  // Flag indicating the state of the context has been changed.
76  bool context_state_changed_;
77
78  base::ThreadChecker thread_checker_;
79
80  DISALLOW_COPY_AND_ASSIGN(PulseAudioInputStream);
81};
82
83}  // namespace media
84
85#endif  // MEDIA_AUDIO_PULSE_PULSE_INPUT_H_
86