summaryrefslogtreecommitdiffstats
path: root/media/audio/mac/audio_unified_mac.cc
blob: d1dc007e6a847ca86d97d8921c53b676903138e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/audio/mac/audio_unified_mac.h"

#include <CoreServices/CoreServices.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/mac/mac_logging.h"
#include "media/audio/mac/audio_manager_mac.h"

namespace media {

// TODO(crogers): support more than hard-coded stereo input.
// Ideally we would like to receive this value as a constructor argument.
static const int kDefaultInputChannels = 2;

AudioHardwareUnifiedStream::AudioHardwareUnifiedStream(
    AudioManagerMac* manager, const AudioParameters& params)
    : manager_(manager),
      source_(NULL),
      client_input_channels_(kDefaultInputChannels),
      volume_(1.0f),
      input_channels_(0),
      output_channels_(0),
      input_channels_per_frame_(0),
      output_channels_per_frame_(0),
      io_proc_id_(0),
      device_(kAudioObjectUnknown),
      is_playing_(false) {
  DCHECK(manager_);

  // A frame is one sample across all channels. In interleaved audio the per
  // frame fields identify the set of n |channels|. In uncompressed audio, a
  // packet is always one frame.
  format_.mSampleRate = params.sample_rate();
  format_.mFormatID = kAudioFormatLinearPCM;
  format_.mFormatFlags = kLinearPCMFormatFlagIsPacked |
                         kLinearPCMFormatFlagIsSignedInteger;
  format_.mBitsPerChannel = params.bits_per_sample();
  format_.mChannelsPerFrame = params.channels();
  format_.mFramesPerPacket = 1;
  format_.mBytesPerPacket = (format_.mBitsPerChannel * params.channels()) / 8;
  format_.mBytesPerFrame = format_.mBytesPerPacket;
  format_.mReserved = 0;

  // Calculate the number of sample frames per callback.
  number_of_frames_ = params.GetBytesPerBuffer() / format_.mBytesPerPacket;

  input_bus_ = AudioBus::Create(client_input_channels_,
                                params.frames_per_buffer());
  output_bus_ = AudioBus::Create(params);
}

AudioHardwareUnifiedStream::~AudioHardwareUnifiedStream() {
  DCHECK_EQ(device_, kAudioObjectUnknown);
}

bool AudioHardwareUnifiedStream::Open() {
  // Obtain the current output device selected by the user.
  AudioObjectPropertyAddress pa;
  pa.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
  pa.mScope = kAudioObjectPropertyScopeGlobal;
  pa.mElement = kAudioObjectPropertyElementMaster;

  UInt32 size = sizeof(device_);

  OSStatus result = AudioObjectGetPropertyData(
      kAudioObjectSystemObject,
      &pa,
      0,
      0,
      &size,
      &device_);

  if ((result != kAudioHardwareNoError) || (device_ == kAudioDeviceUnknown)) {
    LOG(ERROR) << "Cannot open unified AudioDevice.";
    return false;
  }

  // The requested sample-rate must match the hardware sample-rate.
  Float64 sample_rate = 0.0;
  size = sizeof(sample_rate);

  pa.mSelector = kAudioDevicePropertyNominalSampleRate;
  pa.mScope = kAudioObjectPropertyScopeWildcard;
  pa.mElement = kAudioObjectPropertyElementMaster;

  result = AudioObjectGetPropertyData(
      device_,
      &pa,
      0,
      0,
      &size,
      &sample_rate);

  if (result != noErr || sample_rate != format_.mSampleRate) {
    LOG(ERROR) << "Requested sample-rate: " << format_.mSampleRate
        <<  " must match the hardware sample-rate: " << sample_rate;
    return false;
  }

  // Configure buffer frame size.
  UInt32 frame_size = number_of_frames_;

  pa.mSelector = kAudioDevicePropertyBufferFrameSize;
  pa.mScope = kAudioDevicePropertyScopeInput;
  pa.mElement = kAudioObjectPropertyElementMaster;
  result = AudioObjectSetPropertyData(
      device_,
      &pa,
      0,
      0,
      sizeof(frame_size),
      &frame_size);

  if (result != noErr) {
    LOG(ERROR) << "Unable to set input buffer frame size: "  << frame_size;
    return false;
  }

  pa.mScope = kAudioDevicePropertyScopeOutput;
  result = AudioObjectSetPropertyData(
      device_,
      &pa,
      0,
      0,
      sizeof(frame_size),
      &frame_size);

  if (result != noErr) {
    LOG(ERROR) << "Unable to set output buffer frame size: "  << frame_size;
    return false;
  }

  DVLOG(1) << "Sample rate: " << sample_rate;
  DVLOG(1) << "Frame size: " << frame_size;

  // Determine the number of input and output channels.
  // We handle both the interleaved and non-interleaved cases.

  // Get input stream configuration.
  pa.mSelector = kAudioDevicePropertyStreamConfiguration;
  pa.mScope = kAudioDevicePropertyScopeInput;
  pa.mElement = kAudioObjectPropertyElementMaster;

  result = AudioObjectGetPropertyDataSize(device_, &pa, 0, 0, &size);
  OSSTATUS_DCHECK(result == noErr, result);

  if (result == noErr && size > 0) {
    // Allocate storage.
    scoped_ptr<uint8[]> input_list_storage(new uint8[size]);
    AudioBufferList& input_list =
        *reinterpret_cast<AudioBufferList*>(input_list_storage.get());

    result = AudioObjectGetPropertyData(
        device_,
        &pa,
        0,
        0,
        &size,
        &input_list);
    OSSTATUS_DCHECK(result == noErr, result);

    if (result == noErr) {
      // Determine number of input channels.
      input_channels_per_frame_ = input_list.mNumberBuffers > 0 ?
          input_list.mBuffers[0].mNumberChannels : 0;
      if (input_channels_per_frame_ == 1 && input_list.mNumberBuffers > 1) {
        // Non-interleaved.
        input_channels_ = input_list.mNumberBuffers;
      } else {
        // Interleaved.
        input_channels_ = input_channels_per_frame_;
      }
    }
  }

  DVLOG(1) << "Input channels: " << input_channels_;
  DVLOG(1) << "Input channels per frame: " << input_channels_per_frame_;

  // The hardware must have at least the requested input channels.
  if (result != noErr || client_input_channels_ > input_channels_) {
    LOG(ERROR) << "AudioDevice does not support requested input channels.";
    return false;
  }

  // Get output stream configuration.
  pa.mSelector = kAudioDevicePropertyStreamConfiguration;
  pa.mScope = kAudioDevicePropertyScopeOutput;
  pa.mElement = kAudioObjectPropertyElementMaster;

  result = AudioObjectGetPropertyDataSize(device_, &pa, 0, 0, &size);
  OSSTATUS_DCHECK(result == noErr, result);

  if (result == noErr && size > 0) {
    // Allocate storage.
    scoped_ptr<uint8[]> output_list_storage(new uint8[size]);
    AudioBufferList& output_list =
        *reinterpret_cast<AudioBufferList*>(output_list_storage.get());

    result = AudioObjectGetPropertyData(
        device_,
        &pa,
        0,
        0,
        &size,
        &output_list);
    OSSTATUS_DCHECK(result == noErr, result);

    if (result == noErr) {
      // Determine number of output channels.
      output_channels_per_frame_ = output_list.mBuffers[0].mNumberChannels;
      if (output_channels_per_frame_ == 1 && output_list.mNumberBuffers > 1) {
        // Non-interleaved.
        output_channels_ = output_list.mNumberBuffers;
      } else {
        // Interleaved.
        output_channels_ = output_channels_per_frame_;
      }
    }
  }

  DVLOG(1) << "Output channels: " << output_channels_;
  DVLOG(1) << "Output channels per frame: " << output_channels_per_frame_;

  // The hardware must have at least the requested output channels.
  if (result != noErr ||
      output_channels_ < static_cast<int>(format_.mChannelsPerFrame)) {
    LOG(ERROR) << "AudioDevice does not support requested output channels.";
    return false;
  }

  // Setup the I/O proc.
  result = AudioDeviceCreateIOProcID(device_, RenderProc, this, &io_proc_id_);
  if (result != noErr) {
    LOG(ERROR) << "Error creating IOProc.";
    return false;
  }

  return true;
}

void AudioHardwareUnifiedStream::Close() {
  DCHECK(!is_playing_);

  OSStatus result = AudioDeviceDestroyIOProcID(device_, io_proc_id_);
  OSSTATUS_DCHECK(result == noErr, result);

  io_proc_id_ = 0;
  device_ = kAudioObjectUnknown;

  // Inform the audio manager that we have been closed. This can cause our
  // destruction.
  manager_->ReleaseOutputStream(this);
}

void AudioHardwareUnifiedStream::Start(AudioSourceCallback* callback) {
  DCHECK(callback);
  DCHECK_NE(device_, kAudioObjectUnknown);
  DCHECK(!is_playing_);
  if (device_ == kAudioObjectUnknown || is_playing_)
    return;

  source_ = callback;

  OSStatus result = AudioDeviceStart(device_, io_proc_id_);
  OSSTATUS_DCHECK(result == noErr, result);

  if (result == noErr)
    is_playing_ = true;
}

void AudioHardwareUnifiedStream::Stop() {
  if (!is_playing_)
    return;

  if (device_ != kAudioObjectUnknown) {
    OSStatus result = AudioDeviceStop(device_, io_proc_id_);
    OSSTATUS_DCHECK(result == noErr, result);
  }

  is_playing_ = false;
  source_ = NULL;
}

void AudioHardwareUnifiedStream::SetVolume(double volume) {
  volume_ = static_cast<float>(volume);
  // TODO(crogers): set volume property
}

void AudioHardwareUnifiedStream::GetVolume(double* volume) {
  *volume = volume_;
}

// Pulls on our provider with optional input, asking it to render output.
// Note to future hackers of this function: Do not add locks here because this
// is running on a real-time thread (for low-latency).
OSStatus AudioHardwareUnifiedStream::Render(
    AudioDeviceID device,
    const AudioTimeStamp* now,
    const AudioBufferList* input_data,
    const AudioTimeStamp* input_time,
    AudioBufferList* output_data,
    const AudioTimeStamp* output_time) {
  // Convert the input data accounting for possible interleaving.
  // TODO(crogers): it's better to simply memcpy() if source is already planar.
  if (input_channels_ >= client_input_channels_) {
    for (int channel_index = 0; channel_index < client_input_channels_;
         ++channel_index) {
      float* source;

      int source_channel_index = channel_index;

      if (input_channels_per_frame_ > 1) {
        // Interleaved.
        source = static_cast<float*>(input_data->mBuffers[0].mData) +
            source_channel_index;
      } else {
        // Non-interleaved.
        source = static_cast<float*>(
            input_data->mBuffers[source_channel_index].mData);
      }

      float* p = input_bus_->channel(channel_index);
      for (int i = 0; i < number_of_frames_; ++i) {
        p[i] = *source;
        source += input_channels_per_frame_;
      }
    }
  } else if (input_channels_) {
    input_bus_->Zero();
  }

  // Give the client optional input data and have it render the output data.
  source_->OnMoreIOData(input_bus_.get(),
                        output_bus_.get(),
                        AudioBuffersState(0, 0));

  // TODO(crogers): handle final Core Audio 5.1 layout for 5.1 audio.

  // Handle interleaving as necessary.
  // TODO(crogers): it's better to simply memcpy() if dest is already planar.

  for (int channel_index = 0;
       channel_index < static_cast<int>(format_.mChannelsPerFrame);
       ++channel_index) {
    float* dest;

    int dest_channel_index = channel_index;

    if (output_channels_per_frame_ > 1) {
      // Interleaved.
      dest = static_cast<float*>(output_data->mBuffers[0].mData) +
          dest_channel_index;
    } else {
      // Non-interleaved.
      dest = static_cast<float*>(
          output_data->mBuffers[dest_channel_index].mData);
    }

    float* p = output_bus_->channel(channel_index);
    for (int i = 0; i < number_of_frames_; ++i) {
      *dest = p[i];
      dest += output_channels_per_frame_;
    }
  }

  return noErr;
}

OSStatus AudioHardwareUnifiedStream::RenderProc(
    AudioDeviceID device,
    const AudioTimeStamp* now,
    const AudioBufferList* input_data,
    const AudioTimeStamp* input_time,
    AudioBufferList* output_data,
    const AudioTimeStamp* output_time,
    void* user_data) {
  AudioHardwareUnifiedStream* audio_output =
      static_cast<AudioHardwareUnifiedStream*>(user_data);
  DCHECK(audio_output);
  if (!audio_output)
    return -1;

  return audio_output->Render(
      device,
      now,
      input_data,
      input_time,
      output_data,
      output_time);
}

}  // namespace media