Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Side by Side Diff: media/audio/mac/audio_synchronized_mac.h

Issue 10909185: Add Mac OS X synchronized audio I/O back-end (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_SYNCHRONIZED_MAC_H_
6 #define MEDIA_AUDIO_MAC_AUDIO_SYNCHRONIZED_MAC_H_
7
8 #include <AudioToolbox/AudioToolbox.h>
9 #include <AudioUnit/AudioUnit.h>
10 #include <CoreAudio/CoreAudio.h>
11
12 #include "base/compiler_specific.h"
13 #include "base/synchronization/lock.h"
14 #include "media/audio/audio_io.h"
15 #include "media/audio/audio_parameters.h"
16 #include "media/base/audio_bus.h"
17 #include "media/base/audio_fifo.h"
18
19 namespace media {
20
21 class AudioManagerMac;
22
23 // AudioSynchronizedStream allows arbitrary combinations of input and output
24 // devices running off different clocks and using different drivers, with
25 // potentially differing sample-rates. It implements AudioOutputStream
26 // and shuttles its synchronized I/O data using AudioSourceCallback.
27 //
28 // It is required to first acquire the native sample rate of the selected
29 // output device and then use the same rate when creating this object.
30 //
31 // ............................................................................
32 // Theory of Operation:
33 // .
34 // INPUT THREAD . OUTPUT THREAD
35 // +-----------------+ +------+ .
36 // | Input AudioUnit | --> | | .
37 // +-----------------+ | | .
38 // | FIFO | .
39 // | | +-----------+
40 // | | -----> | Varispeed |
41 // | | +-----------+
42 // +------+ . |
43 // . | +-----------+
44 // . OnMoreIOData() --> | Output AU |
45 // . +-----------+
46 //
47 // The input AudioUnit's InputProc is called on one thread which feeds the
48 // FIFO. The output AudioUnit's OutputProc is called on a second thread
49 // which pulls on the varispeed to get the current input data. The varispeed
50 // handles mismatches between input and output sample-rate and also clock drift
51 // between the input and output drivers. The varispeed consumes its data from
52 // the FIFO and adjusts its rate dynamically according to the amount
53 // of data buffered in the FIFO. If the FIFO starts getting too much data
54 // buffered then the varispeed will speed up slightly to compensate
55 // and similarly if the FIFO doesn't have enough data buffered then the
56 // varispeed will slow down slightly.
57 //
58 // Finally, once the input data is available then OnMoreIOData() is called
59 // which is given this input, and renders the output which is finally sent
60 // to the Output AudioUnit.
61 //
scherkus (not reviewing) 2012/09/17 14:51:17 remove extra // line
Chris Rogers 2012/09/17 20:44:23 Done.
62 class AudioSynchronizedStream : public AudioOutputStream {
63 public:
64 // The ctor takes all the usual parameters, plus |manager| which is the
65 // the audio manager who is creating this object.
66 AudioSynchronizedStream(AudioManagerMac* manager,
67 const AudioParameters& params,
68 AudioDeviceID input_id,
69 AudioDeviceID output_id);
70
71 virtual ~AudioSynchronizedStream();
72
73 // Implementation of AudioOutputStream.
74 virtual bool Open() OVERRIDE;
75 virtual void Close() OVERRIDE;
76 virtual void Start(AudioSourceCallback* callback) OVERRIDE;
77 virtual void Stop() OVERRIDE;
78
79 virtual void SetVolume(double volume) OVERRIDE;
80 virtual void GetVolume(double* volume) OVERRIDE;
81
82 OSStatus SetInputDeviceAsCurrent(AudioDeviceID input_id);
83 OSStatus SetOutputDeviceAsCurrent(AudioDeviceID output_id);
84 AudioDeviceID GetInputDeviceID() { return input_info_.id_; }
no longer working on chromium 2012/09/17 20:47:46 for the inline accessors use styles like get_input
Chris Rogers 2012/09/17 22:00:42 This is not a strict style rule, just a recommenda
85 AudioDeviceID GetOutputDeviceID() { return output_info_.id_; }
no longer working on chromium 2012/09/17 20:47:46 ditto
86
87 bool IsRunning();
88
89 private:
90 // Initialization.
91 OSStatus CreateAudioUnits();
92 OSStatus SetupInput(AudioDeviceID input_id);
93 OSStatus EnableIO();
94 OSStatus SetupOutput(AudioDeviceID output_id);
95 OSStatus SetupCallbacks();
96 OSStatus SetupStreamFormats();
97 void AllocateInputData();
98
99 // Handlers for the AudioUnit callbacks.
100 OSStatus HandleInputCallback(AudioUnitRenderActionFlags* io_action_flags,
101 const AudioTimeStamp* time_stamp,
102 UInt32 bus_number,
103 UInt32 number_of_frames,
104 AudioBufferList* io_data);
105
106 OSStatus HandleVarispeedCallback(AudioUnitRenderActionFlags* io_action_flags,
107 const AudioTimeStamp* time_stamp,
108 UInt32 bus_number,
109 UInt32 number_of_frames,
110 AudioBufferList* io_data);
111
112 OSStatus HandleOutputCallback(AudioUnitRenderActionFlags* io_action_flags,
113 const AudioTimeStamp* time_stamp,
114 UInt32 bus_number,
115 UInt32 number_of_frames,
116 AudioBufferList* io_data);
117
118 // AudioUnit callbacks.
119 static OSStatus InputProc(void* user_data,
120 AudioUnitRenderActionFlags* io_action_flags,
121 const AudioTimeStamp* time_stamp,
122 UInt32 bus_number,
123 UInt32 number_of_frames,
124 AudioBufferList* io_data);
125
126 static OSStatus VarispeedProc(void* user_data,
127 AudioUnitRenderActionFlags* io_action_flags,
128 const AudioTimeStamp* time_stamp,
129 UInt32 bus_number,
130 UInt32 number_of_frames,
131 AudioBufferList* io_data);
132
133 static OSStatus OutputProc(void* user_data,
134 AudioUnitRenderActionFlags* io_action_flags,
135 const AudioTimeStamp* time_stamp,
136 UInt32 bus_number,
137 UInt32 number_of_frames,
138 AudioBufferList* io_data);
139
140 // Our creator.
141 AudioManagerMac* manager_;
142
143 // Client parameters.
144 AudioParameters params_;
145
146 double input_sample_rate_;
147 double output_sample_rate_;
148
149 // Pointer to the object that will provide the audio samples.
150 AudioSourceCallback* source_;
151
152 // Values used in Open().
153 AudioDeviceID input_id_;
154 AudioDeviceID output_id_;
155
156 // The input AudioUnit renders its data here.
157 AudioBufferList* input_buffer_list_;
158
159 // Holds the actual data for |input_buffer_list_|
scherkus (not reviewing) 2012/09/17 14:51:17 add .
Chris Rogers 2012/09/17 20:44:23 Done.
160 scoped_ptr<AudioBus> input_bus_;
161
162 class AudioDeviceInfo {
163 public:
164 AudioDeviceInfo()
165 : id_(kAudioDeviceUnknown),
166 is_input_(false),
167 buffer_size_frames_(0) {}
168 void Initialize(AudioDeviceID inID, bool isInput);
169 bool IsInitialized() const { return id_ != kAudioDeviceUnknown; }
170
171 AudioDeviceID id_;
172 bool is_input_;
173 UInt32 buffer_size_frames_;
174 };
175
176 AudioDeviceInfo input_info_;
177 AudioDeviceInfo output_info_;
178
179 // Used for input to output buffering.
180 AudioFifo fifo_;
181
182 double fifo_rate_compensation_;
183 double ave_delta_;
scherkus (not reviewing) 2012/09/17 14:51:17 what's ave_delta_? if it's average can you use av
Chris Rogers 2012/09/17 20:44:23 Added comment and changed name as you suggested.
184
185 // The optimal number of frames we'd like to keep in the FIFO at all times.
186 int target_fifo_frames_;
187
188 // AudioUnits.
189 AudioUnit input_unit_;
190 AudioUnit varispeed_unit_;
191 AudioUnit output_unit_;
192
193 double first_input_time_;
194
195 bool is_running_;
196 int hardware_buffer_size_;
197 int channels_;
198
199 DISALLOW_COPY_AND_ASSIGN(AudioSynchronizedStream);
200 };
201
202 } // namespace media
203
204 #endif // MEDIA_AUDIO_MAC_AUDIO_SYNCHRONIZED_MAC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698