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 5 #ifndef MEDIA_AUDIO_SNDIO_SNDIO_INPUT_H_ 6 #define MEDIA_AUDIO_SNDIO_SNDIO_INPUT_H_ 7 8 #include <stdint.h> 9 #include <string> 10 #include <sndio.h> 11 12 #include "base/compiler_specific.h" 13 #include "base/macros.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_device_description.h" 19 #include "media/base/audio_parameters.h" 20 21 namespace media { 22 23 class AudioManagerBase; 24 25 // Implementation of AudioOutputStream using sndio(7) 26 class SndioAudioInputStream : public AgcAudioStream<AudioInputStream> { 27 public: 28 // Pass this to the constructor if you want to attempt auto-selection 29 // of the audio recording device. 30 static const char kAutoSelectDevice[]; 31 32 // Create a PCM Output stream for the SNDIO device identified by 33 // |device_name|. If unsure of what to use for |device_name|, use 34 // |kAutoSelectDevice|. 35 SndioAudioInputStream(AudioManagerBase* audio_manager, 36 const std::string& device_name, 37 const AudioParameters& params); 38 39 ~SndioAudioInputStream() override; 40 41 // Implementation of AudioInputStream. 42 bool Open() override; 43 void Start(AudioInputCallback* callback) override; 44 void Stop() override; 45 void Close() override; 46 double GetMaxVolume() override; 47 void SetVolume(double volume) override; 48 double GetVolume() override; 49 bool IsMuted() override; 50 void SetOutputDeviceForAec(const std::string& output_device_id) override; 51 52 private: 53 54 enum StreamState { 55 kClosed, // Not opened yet 56 kStopped, // Device opened, but not started yet 57 kRunning, // Started, device playing 58 kStopWait // Stopping, waiting for the real-time thread to exit 59 }; 60 61 // C-style call-backs 62 static void OnMoveCallback(void *arg, int delta); 63 static void* ThreadEntry(void *arg); 64 65 // Continuously moves data from the device to the consumer 66 void ThreadLoop(); 67 // Our creator, the audio manager needs to be notified when we close. 68 AudioManagerBase* manager; 69 // Parameters of the source 70 AudioParameters params; 71 // We store data here for consumer 72 std::unique_ptr<AudioBus> audio_bus; 73 // Call-back that consumes recorded data 74 AudioInputCallback* callback; // Valid during a recording session. 75 // Handle of the audio device 76 struct sio_hdl* hdl; 77 // Current state of the stream 78 enum StreamState state; 79 // High priority thread running ThreadLoop() 80 pthread_t thread; 81 // Number of frames buffered in the hardware 82 int hw_delay; 83 // Temporary buffer where data is stored sndio-compatible format 84 char* buffer; 85 86 DISALLOW_COPY_AND_ASSIGN(SndioAudioInputStream); 87 }; 88 89 } // namespace media 90 91 #endif // MEDIA_AUDIO_SNDIO_SNDIO_INPUT_H_ 92