sndio_input.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_SNDIO_SNDIO_INPUT_H_
  5. #define MEDIA_AUDIO_SNDIO_SNDIO_INPUT_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <sndio.h>
  9. #include "base/compiler_specific.h"
  10. #include "base/macros.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "media/audio/agc_audio_stream.h"
  14. #include "media/audio/audio_io.h"
  15. #include "media/audio/audio_device_description.h"
  16. #include "media/base/audio_parameters.h"
  17. namespace media {
  18. class AudioManagerBase;
  19. // call-backs invoked from C libraries, thus requiring C linkage
  20. extern "C" {
  21. // Invoked (on the real-time thread) at each sound card clock tick
  22. void sndio_in_onmove(void *arg, int delta);
  23. // Invoked (on the real-time thread) whenever the volume changes
  24. void sndio_in_onvol(void *arg, unsigned int vol);
  25. // Real-time thread entry point
  26. void *sndio_in_threadstart(void *arg);
  27. }
  28. // Provides an input stream for audio capture based on the SNDIO PCM interface.
  29. // This object is not thread safe and all methods should be invoked in the
  30. // thread that created the object.
  31. class SndioAudioInputStream : public AgcAudioStream<AudioInputStream> {
  32. public:
  33. // Pass this to the constructor if you want to attempt auto-selection
  34. // of the audio recording device.
  35. static const char kAutoSelectDevice[];
  36. // Create a PCM Output stream for the SNDIO device identified by
  37. // |device_name|. If unsure of what to use for |device_name|, use
  38. // |kAutoSelectDevice|.
  39. SndioAudioInputStream(AudioManagerBase* audio_manager,
  40. const std::string& device_name,
  41. const AudioParameters& params);
  42. ~SndioAudioInputStream() override;
  43. // Implementation of AudioInputStream.
  44. bool Open() override;
  45. void Start(AudioInputCallback* callback) override;
  46. void Stop() override;
  47. void Close() override;
  48. double GetMaxVolume() override;
  49. void SetVolume(double volume) override;
  50. double GetVolume() override;
  51. bool IsMuted() override;
  52. // C-linkage call-backs are friends to access private data
  53. friend void sndio_in_onmove(void *arg, int delta);
  54. friend void sndio_in_onvol(void *arg, unsigned int vol);
  55. friend void *sndio_in_threadstart(void *arg);
  56. private:
  57. // Logs the error and invokes any registered callbacks.
  58. void HandleError(const char* method, int error);
  59. // Reads one or more buffers of audio from the device, passes on to the
  60. // registered callback and schedules the next read.
  61. void ReadAudio();
  62. // Recovers from any device errors if possible.
  63. bool Recover(int error);
  64. // Non-refcounted pointer back to the audio manager.
  65. // The AudioManager indirectly holds on to stream objects, so we don't
  66. // want circular references. Additionally, stream objects live on the audio
  67. // thread, which is owned by the audio manager and we don't want to addref
  68. // the manager from that thread.
  69. AudioManagerBase* audio_manager_;
  70. std::string device_name_;
  71. AudioParameters params_;
  72. int bytes_per_buffer_;
  73. base::TimeDelta buffer_duration_; // Length of each recorded buffer.
  74. AudioInputCallback* callback_; // Valid during a recording session.
  75. base::TimeTicks next_read_time_; // Scheduled time for next read callback.
  76. struct sio_hdl* device_handle_; // Handle to the SNDIO PCM recording device.
  77. std::unique_ptr<uint8_t[]> audio_buffer_; // Buffer used for reading audio data.
  78. bool read_callback_behind_schedule_;
  79. std::unique_ptr<AudioBus> audio_bus_;
  80. int hw_delay_;
  81. int sndio_rec_bufsize_;
  82. int sndio_rec_bufsz_;
  83. // High priority thread running RealTimeThread()
  84. pthread_t thread_;
  85. DISALLOW_COPY_AND_ASSIGN(SndioAudioInputStream);
  86. };
  87. } // namespace media
  88. #endif // MEDIA_AUDIO_SNDIO_SNDIO_INPUT_H_