OLD | NEW |
---|---|
(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 // AudioSynchronizedStream allows arbitrary combinations of input and output | |
6 // devices running off different clocks and using different drivers, with | |
7 // potentially differing sample-rates. It implements AudioOutputStream | |
8 // and shuttles its synchronized I/O data using AudioSourceCallback. | |
9 // | |
10 // It is required to first acquire the native sample rate of the selected | |
11 // output device and then use the same rate when creating this object. | |
12 // | |
13 | |
14 // ............................................................................ | |
15 // Theory of Operation: | |
16 // . | |
17 // INPUT THREAD . OUTPUT THREAD | |
18 // +-----------------+ +------+ . | |
19 // | Input AudioUnit | --> | | . | |
20 // +-----------------+ | | . | |
21 // | FIFO | . | |
22 // | | +-----------+ | |
23 // | | -----> | Varispeed | | |
24 // | | +-----------+ | |
25 // +------+ . | | |
26 // . | +-----------+ | |
27 // . OnMoreIOData() --> | Output AU | | |
28 // . +-----------+ | |
29 // | |
30 // The input AudioUnit's InputProc is called on one thread which feeds the | |
31 // FIFO. The output AudioUnit's OutputProc is called on a second thread | |
32 // which pulls on the varispeed to get the current input data. The varispeed | |
33 // handles mismatches between input and output sample-rate and also clock drift | |
no longer working on chromium
2012/09/12 09:11:56
So the FIFO is thread safe?
Chris Rogers
2012/09/13 01:03:05
Good catch! The AudioFifo is currently *not* thre
| |
34 // between the input and output drivers. The varispeed consumes its data from | |
35 // the FIFO and adjusts its rate dynamically according to the amount | |
36 // of data buffered in the FIFO. If the FIFO starts getting too much data | |
37 // buffered then the varispeed will speed up slightly to compensate | |
38 // and similarly if the FIFO doesn't have enough data buffered then the | |
39 // varispeed will slow down slightly. | |
scherkus (not reviewing)
2012/09/12 14:05:29
can you move comment to be next to class?
Chris Rogers
2012/09/13 01:03:05
Done.
| |
40 // | |
41 // Finally, once the input data is available then OnMoreIOData() is called | |
42 // which is given this input, and renders the output which is finally sent | |
43 // to the Output AudioUnit. | |
44 | |
45 #ifndef MEDIA_AUDIO_MAC_AUDIO_SYNCHRONIZED_MAC_H_ | |
46 #define MEDIA_AUDIO_MAC_AUDIO_SYNCHRONIZED_MAC_H_ | |
47 | |
48 #include <AudioToolbox/AudioToolbox.h> | |
49 #include <AudioUnit/AudioUnit.h> | |
50 #include <CoreAudio/CoreAudio.h> | |
51 | |
52 #define CHROME 0 | |
53 | |
54 #if CHROME | |
no longer working on chromium
2012/09/12 09:11:56
what is this?
Chris Rogers
2012/09/13 01:03:05
Sorry, this is temporary which I need for testing
| |
55 #include "base/compiler_specific.h" | |
56 #include "media/audio/audio_io.h" | |
57 #include "media/audio/audio_parameters.h" | |
58 #include "media/base/audio_fifo.h" | |
59 #else | |
60 #include "audio_parameters.h" | |
61 #include "AudioFIFO.h" | |
62 #endif | |
63 | |
64 namespace media { | |
65 | |
66 class AudioManagerMac; | |
67 | |
68 class AudioSynchronizedStream : public AudioOutputStream { | |
69 public: | |
70 // The ctor takes all the usual parameters, plus |manager| which is the | |
71 // the audio manager who is creating this object. | |
72 AudioSynchronizedStream(AudioManagerMac* manager, | |
73 const AudioParameters& params, | |
74 AudioDeviceID input_id, | |
75 AudioDeviceID output_id); | |
76 | |
77 virtual ~AudioSynchronizedStream(); | |
78 | |
79 // Implementation of AudioOutputStream. | |
80 virtual bool Open() OVERRIDE; | |
81 virtual void Close() OVERRIDE; | |
82 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | |
83 virtual void Stop() OVERRIDE; | |
84 | |
85 // TODO(crogers): | |
86 // implement - or remove SetVolume()/GetVolume() from AudioOutputStream. | |
scherkus (not reviewing)
2012/09/12 14:05:29
agreed :)
| |
87 virtual void SetVolume(double volume) OVERRIDE {} | |
scherkus (not reviewing)
2012/09/12 14:05:29
de-inline virtual methods
Chris Rogers
2012/09/13 01:03:05
Done.
| |
88 virtual void GetVolume(double* volume) OVERRIDE {} | |
89 | |
90 OSStatus SetInputDeviceAsCurrent(AudioDeviceID input_id); | |
91 OSStatus SetOutputDeviceAsCurrent(AudioDeviceID output_id); | |
92 AudioDeviceID GetInputDeviceID() { return input_info_.id_; } | |
93 AudioDeviceID GetOutputDeviceID() { return output_info_.id_; } | |
94 | |
95 bool IsRunning(); | |
96 | |
97 private: | |
98 // Initialization. | |
99 OSStatus CreateAudioUnits(); | |
100 OSStatus SetupInput(AudioDeviceID input_id); | |
101 OSStatus EnableIO(); | |
102 OSStatus SetupOutput(AudioDeviceID output_id); | |
103 OSStatus SetupCallbacks(); | |
104 OSStatus SetupStreamFormats(); | |
105 | |
106 void ComputeThruOffset(); | |
107 | |
108 // AudioUnit callbacks. | |
109 static OSStatus InputProc(void* user_data, | |
110 AudioUnitRenderActionFlags* io_action_flags, | |
111 const AudioTimeStamp* time_stamp, | |
112 UInt32 bus_number, | |
113 UInt32 number_of_frames, | |
114 AudioBufferList* io_data); | |
115 | |
116 static OSStatus VarispeedProc(void* user_data, | |
117 AudioUnitRenderActionFlags* io_action_flags, | |
118 const AudioTimeStamp* time_stamp, | |
119 UInt32 bus_number, | |
120 UInt32 number_of_frames, | |
121 AudioBufferList* io_data); | |
122 | |
123 static OSStatus OutputProc(void* user_data, | |
124 AudioUnitRenderActionFlags* io_action_flags, | |
125 const AudioTimeStamp* time_stamp, | |
126 UInt32 bus_number, | |
127 UInt32 number_of_frames, | |
128 AudioBufferList* io_data); | |
129 | |
130 // Our creator. | |
131 AudioManagerMac* manager_; | |
132 | |
133 // Pointer to the object that will provide the audio samples. | |
134 AudioSourceCallback* source_; | |
135 | |
136 // Values used in Open(). | |
137 AudioDeviceID input_id_; | |
138 AudioDeviceID output_id_; | |
139 | |
140 AudioBufferList* input_buffer_; | |
141 | |
142 struct AudioDeviceInfo { | |
no longer working on chromium
2012/09/12 09:11:56
it contains member functions in this struct, we sh
Chris Rogers
2012/09/13 01:03:05
Done.
| |
143 AudioDeviceInfo() : id_(kAudioDeviceUnknown) {} | |
144 void Initialize(AudioDeviceID inID, bool isInput); | |
145 bool IsInitialized() const { return id_ != kAudioDeviceUnknown; } | |
146 | |
147 AudioDeviceID id_; | |
148 bool is_input_; | |
149 UInt32 buffer_size_frames_; | |
no longer working on chromium
2012/09/12 09:11:56
initialize these values in the constructor.
Chris Rogers
2012/09/13 01:03:05
Done.
| |
150 }; | |
151 | |
152 AudioDeviceInfo input_info_; | |
153 AudioDeviceInfo output_info_; | |
154 | |
155 // Used for input to output buffering. | |
156 AudioFifo fifo_; | |
157 bool is_fifo_initialized_; | |
158 double fifo_rate_compensation_; | |
159 double output_sample_rate_; | |
160 | |
161 // AudioUnits | |
scherkus (not reviewing)
2012/09/12 14:05:29
period
Chris Rogers
2012/09/13 01:03:05
Done.
| |
162 AudioUnit input_unit_; | |
163 AudioUnit varispeed_unit_; | |
164 AudioUnit output_unit_; | |
165 | |
166 // Initial latency estimation. | |
167 Float64 first_input_time_; | |
scherkus (not reviewing)
2012/09/12 14:05:29
are we required to use these types vs. double?
Chris Rogers
2012/09/13 01:03:05
You're right - not in this case -- switched to dou
| |
168 Float64 first_output_time_; | |
169 Float64 in_to_out_sample_offset_; | |
170 | |
171 bool is_running_; | |
172 UInt32 hardware_frame_size_; | |
173 }; | |
scherkus (not reviewing)
2012/09/12 14:05:29
DISALLOW etc
Chris Rogers
2012/09/13 01:03:05
Done.
| |
174 | |
175 } // namespace media | |
176 | |
177 #endif // MEDIA_AUDIO_MAC_AUDIO_SYNCHRONIZED_MAC_H_ | |
OLD | NEW |