alsa_input.h 3.6 KB

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