Adds "tts" permission for Input Tools & XKB extension.
[chromium-blink-merge.git] / media / audio / alsa / alsa_input.h
blobb04e628aea5153b6c68d0839e9bb199d2bf78122
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.
5 #ifndef MEDIA_AUDIO_ALSA_ALSA_INPUT_H_
6 #define MEDIA_AUDIO_ALSA_ALSA_INPUT_H_
8 #include <alsa/asoundlib.h>
10 #include <string>
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"
20 namespace media {
22 class AlsaWrapper;
23 class AudioManagerBase;
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.
28 class 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[];
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);
42 ~AlsaPcmInputStream() override;
44 // Implementation of AudioInputStream.
45 bool Open() override;
46 void Start(AudioInputCallback* callback) override;
47 void Stop() override;
48 void Close() override;
49 double GetMaxVolume() override;
50 void SetVolume(double volume) override;
51 double GetVolume() override;
52 bool IsMuted() override;
54 private:
55 // Logs the error and invokes any registered callbacks.
56 void HandleError(const char* method, int error);
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();
62 // Recovers from any device errors if possible.
63 bool Recover(int error);
65 // Utility function for talking with the ALSA API.
66 snd_pcm_sframes_t GetCurrentDelay();
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_;
88 // NOTE: Weak pointers must be invalidated before all other member variables.
89 base::WeakPtrFactory<AlsaPcmInputStream> weak_factory_;
91 DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream);
94 } // namespace media
96 #endif // MEDIA_AUDIO_ALSA_ALSA_INPUT_H_