audio_input_mac.h revision ab8f6f0bd665d3c1ff476eb06c58c42630e462d4
1// Copyright (c) 2012 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_MAC_AUDIO_INPUT_MAC_H_ 6#define MEDIA_AUDIO_MAC_AUDIO_INPUT_MAC_H_ 7 8#include <AudioToolbox/AudioFormat.h> 9#include <AudioToolbox/AudioQueue.h> 10 11#include "base/cancelable_callback.h" 12#include "base/compiler_specific.h" 13#include "base/time/time.h" 14#include "media/audio/audio_io.h" 15#include "media/audio/audio_parameters.h" 16 17namespace media { 18 19class AudioBus; 20class AudioManagerMac; 21 22// Implementation of AudioInputStream for Mac OS X using the audio queue service 23// present in OS 10.5 and later. Design reflects PCMQueueOutAudioOutputStream. 24class PCMQueueInAudioInputStream : public AudioInputStream { 25 public: 26 // Parameters as per AudioManager::MakeAudioInputStream. 27 PCMQueueInAudioInputStream(AudioManagerMac* manager, 28 const AudioParameters& params); 29 virtual ~PCMQueueInAudioInputStream(); 30 31 // Implementation of AudioInputStream. 32 virtual bool Open() OVERRIDE; 33 virtual void Start(AudioInputCallback* callback) OVERRIDE; 34 virtual void Stop() OVERRIDE; 35 virtual void Close() OVERRIDE; 36 virtual double GetMaxVolume() OVERRIDE; 37 virtual void SetVolume(double volume) OVERRIDE; 38 virtual double GetVolume() OVERRIDE; 39 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; 40 virtual bool GetAutomaticGainControl() OVERRIDE; 41 virtual bool IsMuted() OVERRIDE; 42 43 private: 44 // Issue the OnError to |callback_|; 45 void HandleError(OSStatus err); 46 47 // Allocates and prepares the memory that will be used for recording. 48 bool SetupBuffers(); 49 50 // Sends a buffer to the audio driver for recording. 51 OSStatus QueueNextBuffer(AudioQueueBufferRef audio_buffer); 52 53 // Callback from OS, delegates to non-static version below. 54 static void HandleInputBufferStatic( 55 void* data, 56 AudioQueueRef audio_queue, 57 AudioQueueBufferRef audio_buffer, 58 const AudioTimeStamp* start_time, 59 UInt32 num_packets, 60 const AudioStreamPacketDescription* desc); 61 62 // Handles callback from OS. Will be called on OS internal thread. 63 void HandleInputBuffer(AudioQueueRef audio_queue, 64 AudioQueueBufferRef audio_buffer, 65 const AudioTimeStamp* start_time, 66 UInt32 num_packets, 67 const AudioStreamPacketDescription* packet_desc); 68 69 static const int kNumberBuffers = 3; 70 71 // Manager that owns this stream, used for closing down. 72 AudioManagerMac* manager_; 73 // We use the callback mostly to periodically supply the recorded audio data. 74 AudioInputCallback* callback_; 75 // Structure that holds the stream format details such as bitrate. 76 AudioStreamBasicDescription format_; 77 // Handle to the OS audio queue object. 78 AudioQueueRef audio_queue_; 79 // Size of each of the buffers in |audio_buffers_| 80 uint32 buffer_size_bytes_; 81 // True iff Start() has been called successfully. 82 bool started_; 83 // Used to determine if we need to slow down |callback_| calls. 84 base::TimeTicks last_fill_; 85 // Used to defer Start() to workaround http://crbug.com/160920. 86 base::CancelableClosure deferred_start_cb_; 87 88 scoped_ptr<media::AudioBus> audio_bus_; 89 90 DISALLOW_COPY_AND_ASSIGN(PCMQueueInAudioInputStream); 91}; 92 93} // namespace media 94 95#endif // MEDIA_AUDIO_MAC_AUDIO_INPUT_MAC_H_ 96