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.
5 #ifndef MEDIA_AUDIO_MAC_AUDIO_INPUT_MAC_H_
6 #define MEDIA_AUDIO_MAC_AUDIO_INPUT_MAC_H_
8 #include <AudioToolbox/AudioFormat.h>
9 #include <AudioToolbox/AudioQueue.h>
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"
20 class AudioManagerMac
;
22 // Implementation of AudioInputStream for Mac OS X using the audio queue service
23 // present in OS 10.5 and later. Design reflects PCMQueueOutAudioOutputStream.
24 class PCMQueueInAudioInputStream
: public AudioInputStream
{
26 // Parameters as per AudioManager::MakeAudioInputStream.
27 PCMQueueInAudioInputStream(AudioManagerMac
* manager
,
28 const AudioParameters
& params
);
29 ~PCMQueueInAudioInputStream() override
;
31 // Implementation of AudioInputStream.
33 void Start(AudioInputCallback
* callback
) override
;
35 void Close() override
;
36 double GetMaxVolume() override
;
37 void SetVolume(double volume
) override
;
38 double GetVolume() override
;
39 bool SetAutomaticGainControl(bool enabled
) override
;
40 bool GetAutomaticGainControl() override
;
41 bool IsMuted() override
;
44 // Issue the OnError to |callback_|;
45 void HandleError(OSStatus err
);
47 // Allocates and prepares the memory that will be used for recording.
50 // Sends a buffer to the audio driver for recording.
51 OSStatus
QueueNextBuffer(AudioQueueBufferRef audio_buffer
);
53 // Callback from OS, delegates to non-static version below.
54 static void HandleInputBufferStatic(
56 AudioQueueRef audio_queue
,
57 AudioQueueBufferRef audio_buffer
,
58 const AudioTimeStamp
* start_time
,
60 const AudioStreamPacketDescription
* desc
);
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
,
67 const AudioStreamPacketDescription
* packet_desc
);
69 static const int kNumberBuffers
= 3;
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.
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_
;
88 scoped_ptr
<media::AudioBus
> audio_bus_
;
90 DISALLOW_COPY_AND_ASSIGN(PCMQueueInAudioInputStream
);
95 #endif // MEDIA_AUDIO_MAC_AUDIO_INPUT_MAC_H_