1// Copyright 2013 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_ALSA_ALSA_INPUT_H_
6#define MEDIA_AUDIO_ALSA_ALSA_INPUT_H_
7
8#include <alsa/asoundlib.h>
9
10#include <string>
11
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "base/time/time.h"
16#include "media/audio/agc_audio_stream.h"
17#include "media/audio/audio_io.h"
18#include "media/audio/audio_parameters.h"
19
20namespace media {
21
22class AlsaWrapper;
23class AudioManagerBase;
24
25// Provides an input stream for audio capture based on the ALSA PCM interface.
26// This object is not thread safe and all methods should be invoked in the
27// thread that created the object.
28class AlsaPcmInputStream : public AgcAudioStream<AudioInputStream> {
29 public:
30  // Pass this to the constructor if you want to attempt auto-selection
31  // of the audio recording device.
32  static const char kAutoSelectDevice[];
33
34  // Create a PCM Output stream for the ALSA device identified by
35  // |device_name|. If unsure of what to use for |device_name|, use
36  // |kAutoSelectDevice|.
37  AlsaPcmInputStream(AudioManagerBase* audio_manager,
38                     const std::string& device_name,
39                     const AudioParameters& params,
40                     AlsaWrapper* wrapper);
41
42  virtual ~AlsaPcmInputStream();
43
44  // Implementation of AudioInputStream.
45  virtual bool Open() OVERRIDE;
46  virtual void Start(AudioInputCallback* callback) OVERRIDE;
47  virtual void Stop() OVERRIDE;
48  virtual void Close() OVERRIDE;
49  virtual double GetMaxVolume() OVERRIDE;
50  virtual void SetVolume(double volume) OVERRIDE;
51  virtual double GetVolume() OVERRIDE;
52  virtual bool IsMuted() OVERRIDE;
53
54 private:
55  // Logs the error and invokes any registered callbacks.
56  void HandleError(const char* method, int error);
57
58  // Reads one or more buffers of audio from the device, passes on to the
59  // registered callback and schedules the next read.
60  void ReadAudio();
61
62  // Recovers from any device errors if possible.
63  bool Recover(int error);
64
65  // Utility function for talking with the ALSA API.
66  snd_pcm_sframes_t GetCurrentDelay();
67
68  // Non-refcounted pointer back to the audio manager.
69  // The AudioManager indirectly holds on to stream objects, so we don't
70  // want circular references.  Additionally, stream objects live on the audio
71  // thread, which is owned by the audio manager and we don't want to addref
72  // the manager from that thread.
73  AudioManagerBase* audio_manager_;
74  std::string device_name_;
75  AudioParameters params_;
76  int bytes_per_buffer_;
77  AlsaWrapper* wrapper_;
78  base::TimeDelta buffer_duration_;  // Length of each recorded buffer.
79  AudioInputCallback* callback_;  // Valid during a recording session.
80  base::TimeTicks next_read_time_;  // Scheduled time for next read callback.
81  snd_pcm_t* device_handle_;  // Handle to the ALSA PCM recording device.
82  snd_mixer_t* mixer_handle_; // Handle to the ALSA microphone mixer.
83  snd_mixer_elem_t* mixer_element_handle_; // Handle to the capture element.
84  scoped_ptr<uint8[]> audio_buffer_;  // Buffer used for reading audio data.
85  bool read_callback_behind_schedule_;
86  scoped_ptr<AudioBus> audio_bus_;
87
88  // NOTE: Weak pointers must be invalidated before all other member variables.
89  base::WeakPtrFactory<AlsaPcmInputStream> weak_factory_;
90
91  DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream);
92};
93
94}  // namespace media
95
96#endif  // MEDIA_AUDIO_ALSA_ALSA_INPUT_H_
97