OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ | 5 #ifndef MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ |
6 #define MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ | 6 #define MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ |
7 | 7 |
8 #include <alsa/asoundlib.h> | 8 #include <alsa/asoundlib.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
16 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
17 | 17 |
18 class AlsaWrapper; | 18 class AlsaWrapper; |
| 19 class AudioManagerLinux; |
19 | 20 |
20 // Provides an input stream for audio capture based on the ALSA PCM interface. | 21 // Provides an input stream for audio capture based on the ALSA PCM interface. |
21 // This object is not thread safe and all methods should be invoked in the | 22 // This object is not thread safe and all methods should be invoked in the |
22 // thread that created the object. | 23 // thread that created the object. |
23 class AlsaPcmInputStream : public AudioInputStream { | 24 class AlsaPcmInputStream : public AudioInputStream { |
24 public: | 25 public: |
25 // Pass this to the constructor if you want to attempt auto-selection | 26 // Pass this to the constructor if you want to attempt auto-selection |
26 // of the audio recording device. | 27 // of the audio recording device. |
27 static const char* kAutoSelectDevice; | 28 static const char* kAutoSelectDevice; |
28 | 29 |
29 // Create a PCM Output stream for the ALSA device identified by | 30 // Create a PCM Output stream for the ALSA device identified by |
30 // |device_name|. If unsure of what to use for |device_name|, use | 31 // |device_name|. If unsure of what to use for |device_name|, use |
31 // |kAutoSelectDevice|. | 32 // |kAutoSelectDevice|. |
32 AlsaPcmInputStream(const std::string& device_name, | 33 AlsaPcmInputStream(AudioManagerLinux* audio_manager, |
| 34 const std::string& device_name, |
33 const AudioParameters& params, | 35 const AudioParameters& params, |
34 AlsaWrapper* wrapper); | 36 AlsaWrapper* wrapper); |
35 virtual ~AlsaPcmInputStream(); | 37 virtual ~AlsaPcmInputStream(); |
36 | 38 |
37 // Implementation of AudioOutputStream. | 39 // Implementation of AudioOutputStream. |
38 virtual bool Open() OVERRIDE; | 40 virtual bool Open() OVERRIDE; |
39 virtual void Start(AudioInputCallback* callback) OVERRIDE; | 41 virtual void Start(AudioInputCallback* callback) OVERRIDE; |
40 virtual void Stop() OVERRIDE; | 42 virtual void Stop() OVERRIDE; |
41 virtual void Close() OVERRIDE; | 43 virtual void Close() OVERRIDE; |
42 | 44 |
43 private: | 45 private: |
44 // Logs the error and invokes any registered callbacks. | 46 // Logs the error and invokes any registered callbacks. |
45 void HandleError(const char* method, int error); | 47 void HandleError(const char* method, int error); |
46 | 48 |
47 // Reads one or more packets of audio from the device, passes on to the | 49 // Reads one or more packets of audio from the device, passes on to the |
48 // registered callback and schedules the next read. | 50 // registered callback and schedules the next read. |
49 void ReadAudio(); | 51 void ReadAudio(); |
50 | 52 |
51 // Recovers from any device errors if possible. | 53 // Recovers from any device errors if possible. |
52 bool Recover(int error); | 54 bool Recover(int error); |
53 | 55 |
54 // Utility function for talking with the ALSA API. | 56 // Utility function for talking with the ALSA API. |
55 snd_pcm_sframes_t GetCurrentDelay(); | 57 snd_pcm_sframes_t GetCurrentDelay(); |
56 | 58 |
| 59 // Non-refcounted pointer back to the audio manager. |
| 60 // The AudioManager indirectly holds on to stream objects, so we don't |
| 61 // want circular references. Additionally, stream objects live on the audio |
| 62 // thread, which is owned by the audio manager and we don't want to addref |
| 63 // the manager from that thread. |
| 64 AudioManagerLinux* audio_manager_; |
57 std::string device_name_; | 65 std::string device_name_; |
58 AudioParameters params_; | 66 AudioParameters params_; |
59 int bytes_per_packet_; | 67 int bytes_per_packet_; |
60 AlsaWrapper* wrapper_; | 68 AlsaWrapper* wrapper_; |
61 int packet_duration_ms_; // Length of each recorded packet in milliseconds. | 69 int packet_duration_ms_; // Length of each recorded packet in milliseconds. |
62 AudioInputCallback* callback_; // Valid during a recording session. | 70 AudioInputCallback* callback_; // Valid during a recording session. |
63 base::Time next_read_time_; // Scheduled time for the next read callback. | 71 base::Time next_read_time_; // Scheduled time for the next read callback. |
64 snd_pcm_t* device_handle_; // Handle to the ALSA PCM recording device. | 72 snd_pcm_t* device_handle_; // Handle to the ALSA PCM recording device. |
65 base::WeakPtrFactory<AlsaPcmInputStream> weak_factory_; | 73 base::WeakPtrFactory<AlsaPcmInputStream> weak_factory_; |
66 scoped_array<uint8> audio_packet_; // Buffer used for reading audio data. | 74 scoped_array<uint8> audio_packet_; // Buffer used for reading audio data. |
67 bool read_callback_behind_schedule_; | 75 bool read_callback_behind_schedule_; |
68 | 76 |
69 DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream); | 77 DISALLOW_COPY_AND_ASSIGN(AlsaPcmInputStream); |
70 }; | 78 }; |
71 | 79 |
72 #endif // MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ | 80 #endif // MEDIA_AUDIO_LINUX_ALSA_INPUT_H_ |
OLD | NEW |