OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_LINUX_ALSA_INPUT_H_ |
| 6 #define MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ |
| 7 |
| 8 #include <alsa/asoundlib.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/task.h" |
| 14 #include "media/audio/audio_io.h" |
| 15 #include "media/audio/audio_parameters.h" |
| 16 |
| 17 class AlsaWrapper; |
| 18 |
| 19 // Provides an input stream for audio capture based on the ALSA PCM interface. |
| 20 // This object is not thread safe and all methods should be invoked in the |
| 21 // thread that created the object. |
| 22 class AlsaPcmInputStream : public AudioInputStream { |
| 23 public: |
| 24 // Pass this to the constructor if you want to attempt auto-selection |
| 25 // of the audio recording device. |
| 26 static const char* kAutoSelectDevice; |
| 27 |
| 28 // Create a PCM Output stream for the ALSA device identified by |
| 29 // |device_name|. If unsure of what to use for |device_name|, use |
| 30 // |kAutoSelectDevice|. |
| 31 AlsaPcmInputStream(const std::string& device_name, |
| 32 const AudioParameters& params, |
| 33 int samples_per_packet, |
| 34 AlsaWrapper* wrapper); |
| 35 |
| 36 // Implementation of AudioOutputStream. |
| 37 virtual bool Open(); |
| 38 virtual void Start(AudioInputCallback* callback); |
| 39 virtual void Stop(); |
| 40 virtual void Close(); |
| 41 |
| 42 private: |
| 43 // Logs the error and invokes any registered callbacks. |
| 44 void HandleError(const char* method, int error); |
| 45 |
| 46 // Reads one or more packets of audio from the device, passes on to the |
| 47 // registered callback and schedules the next read. |
| 48 void ReadAudio(); |
| 49 |
| 50 // Recovers from any device errors if possible. |
| 51 bool Recover(int error); |
| 52 |
| 53 std::string device_name_; |
| 54 AudioParameters params_; |
| 55 int samples_per_packet_; |
| 56 int bytes_per_packet_; |
| 57 AlsaWrapper* wrapper_; |
| 58 int packet_duration_ms_; // Length of each recorded packet in milliseconds. |
| 59 AudioInputCallback* callback_; // Valid during a recording session. |
| 60 base::Time next_read_time_; // Scheduled time for the next read callback. |
| 61 snd_pcm_t* device_handle_; // Handle to the ALSA PCM recording device. |
| 62 ScopedRunnableMethodFactory<AlsaPcmInputStream> task_factory_; |
| 63 scoped_ptr<uint8> audio_packet_; // Data buffer used for reading audio data. |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream); |
| 66 }; |
| 67 |
| 68 #endif // MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ |
OLD | NEW |