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