File: audio_capturer_mac.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (407 lines) | stat: -rw-r--r-- 13,333 bytes parent folder | download | duplicates (6)
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
398
399
400
401
402
403
404
405
406
407
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "remoting/host/audio_capturer_mac.h"

#include <memory>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/sys_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "remoting/base/host_settings.h"
#include "remoting/base/logging.h"
#include "remoting/host/host_setting_keys.h"
#include "remoting/host/mac/permission_utils.h"
#include "remoting/proto/audio.pb.h"

namespace remoting {

namespace {

// TODO(yuweih): Determine the device's sample rate. This probably still works
// with higher device sampling rate as AudioQueue will just downsample it.
constexpr AudioPacket::SamplingRate kSampleRate =
    AudioPacket::SAMPLING_RATE_44100;
constexpr int kBytesPerChannel = 2;
constexpr int kChannelsPerFrame = 2;  // Stereo
constexpr int kBytesPerFrame = kBytesPerChannel * kChannelsPerFrame;
constexpr float kBufferTimeDurationSec = 0.01f;  // 10ms
constexpr size_t kBufferByteSize =
    kSampleRate * kBytesPerFrame * kBufferTimeDurationSec;
constexpr int kAudioSilenceThreshold = 0;

// Total delay: kBufferTimeDurationSec * kNumberBuffers
constexpr int kNumberBuffers = 2;

// A set to keep track of valid instances as we can't pass WeakPtr to the buffer
// callback.
class AudioCapturerInstanceSet {
 public:
  static base::Lock& GetLock();

  // Note: Add() and Remove() acquire a lock while Contains() doesn't.
  static void Add(AudioCapturerMac* instance);
  static void Remove(AudioCapturerMac* instance);
  static bool Contains(AudioCapturerMac* instance);

 private:
  friend class base::NoDestructor<AudioCapturerInstanceSet>;

  AudioCapturerInstanceSet();
  ~AudioCapturerInstanceSet();
  static AudioCapturerInstanceSet* Get();

  base::flat_set<AudioCapturerMac*> instance_set_;
  base::Lock lock_;
};

// static
base::Lock& AudioCapturerInstanceSet::GetLock() {
  return Get()->lock_;
}

// static
void AudioCapturerInstanceSet::Add(AudioCapturerMac* instance) {
  base::AutoLock guard(GetLock());
  Get()->instance_set_.insert(instance);
}

// static
void AudioCapturerInstanceSet::Remove(AudioCapturerMac* instance) {
  base::AutoLock guard(GetLock());
  Get()->instance_set_.erase(instance);
}

// static
bool AudioCapturerInstanceSet::Contains(AudioCapturerMac* instance) {
  return base::Contains(Get()->instance_set_, instance);
}

AudioCapturerInstanceSet::AudioCapturerInstanceSet() = default;

AudioCapturerInstanceSet::~AudioCapturerInstanceSet() = default;

// static
AudioCapturerInstanceSet* AudioCapturerInstanceSet::Get() {
  static base::NoDestructor<AudioCapturerInstanceSet> instance_set;
  return instance_set.get();
}

}  // namespace

// static
std::vector<AudioCapturerMac::AudioDeviceInfo>
AudioCapturerMac::GetAudioDevices() {
  AudioObjectPropertyAddress property_address;
  property_address.mScope = kAudioObjectPropertyScopeGlobal;
  property_address.mElement = kAudioObjectPropertyElementMain;

  UInt32 property_size;

  // Get all audio device IDs (which are UInt32).
  property_address.mSelector = kAudioHardwarePropertyDevices;
  OSStatus result = AudioObjectGetPropertyDataSize(
      kAudioObjectSystemObject, &property_address, 0, NULL, &property_size);
  if (result != noErr) {
    LOG(ERROR)
        << "AudioObjectGetPropertyDataSize(kAudioHardwarePropertyDevices) "
        << "failed. Error: " << result;
    return {};
  }

  UInt32 num_devices = property_size / sizeof(AudioDeviceID);
  auto device_ids = std::make_unique<AudioDeviceID[]>(num_devices);
  result =
      AudioObjectGetPropertyData(kAudioObjectSystemObject, &property_address, 0,
                                 NULL, &property_size, device_ids.get());
  if (result != noErr) {
    LOG(ERROR) << "AudioObjectGetPropertyData(kAudioHardwarePropertyDevices) "
               << "failed. Error: " << result;
    return {};
  }

  std::vector<AudioDeviceInfo> audio_devices;

  for (UInt32 i = 0u; i < num_devices; i++) {
    AudioDeviceInfo audio_device;
    AudioDeviceID device_id = device_ids.get()[i];

    // Get the device name.
    property_address.mSelector = kAudioObjectPropertyName;
    base::apple::ScopedCFTypeRef<CFStringRef> device_name;
    property_size = sizeof(CFStringRef);
    result = AudioObjectGetPropertyData(device_id, &property_address, 0, NULL,
                                        &property_size,
                                        device_name.InitializeInto());
    if (result != noErr) {
      LOG(ERROR) << "AudioObjectGetPropertyData(" << device_id
                 << ", kAudioObjectPropertyName) "
                 << "failed. Error: " << result;
      continue;
    }
    audio_device.device_name = base::SysCFStringRefToUTF8(device_name.get());

    // Now find out its UID.
    property_address.mSelector = kAudioDevicePropertyDeviceUID;
    base::apple::ScopedCFTypeRef<CFStringRef> device_uid;
    property_size = sizeof(CFStringRef);
    result =
        AudioObjectGetPropertyData(device_id, &property_address, 0, NULL,
                                   &property_size, device_uid.InitializeInto());
    if (result != noErr) {
      LOG(ERROR) << "AudioObjectGetPropertyData(" << device_id
                 << ", kAudioDevicePropertyDeviceUID) "
                 << "failed. Error: " << result;
      continue;
    }
    audio_device.device_uid = base::SysCFStringRefToUTF8(device_uid.get());
    audio_devices.push_back(audio_device);
  }
  return audio_devices;
}

AudioCapturerMac::AudioCapturerMac(const std::string& audio_device_uid)
    : audio_device_uid_(audio_device_uid),
      silence_detector_(kAudioSilenceThreshold) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
  DCHECK(!audio_device_uid.empty());

  stream_description_.mSampleRate = kSampleRate;
  stream_description_.mFormatID = kAudioFormatLinearPCM;
  stream_description_.mFormatFlags =
      kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  stream_description_.mBytesPerPacket = kBytesPerFrame;
  stream_description_.mFramesPerPacket = 1;
  stream_description_.mBytesPerFrame = kBytesPerFrame;
  stream_description_.mChannelsPerFrame = kChannelsPerFrame;
  stream_description_.mBitsPerChannel = 8 * kBytesPerChannel;
  stream_description_.mReserved = 0;

  AudioCapturerInstanceSet::Add(this);
}

AudioCapturerMac::~AudioCapturerMac() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  AudioCapturerInstanceSet::Remove(this);

  DisposeInputQueue();
}

bool AudioCapturerMac::Start(const PacketCapturedCallback& callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!callback_);
  DCHECK(callback);

  caller_task_runner_ = base::SequencedTaskRunner::GetCurrentDefault();

  if (!StartInputQueue()) {
    return false;
  }

  callback_ = callback;
  return true;
}

// static
void AudioCapturerMac::HandleInputBufferOnAQThread(
    void* user_data,
    AudioQueueRef aq,
    AudioQueueBufferRef buffer,
    const AudioTimeStamp* start_time,
    UInt32 num_packets,
    const AudioStreamPacketDescription* packet_descs) {
  AudioCapturerMac* capturer = reinterpret_cast<AudioCapturerMac*>(user_data);

  {
    base::AutoLock guard(AudioCapturerInstanceSet::GetLock());
    if (!AudioCapturerInstanceSet::Contains(capturer)) {
      // The capturer has been destroyed.
      return;
    }
    capturer->caller_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&AudioCapturerMac::HandleInputBuffer,
                       capturer->weak_factory_.GetWeakPtr(), aq, buffer));
  }
}

void AudioCapturerMac::HandleInputBuffer(AudioQueueRef aq,
                                         AudioQueueBufferRef buffer) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!is_started_) {
    LOG(WARNING) << "Playback has been stopped.";
    return;
  }

  DCHECK_EQ(input_queue_, aq);
  DCHECK(callback_);

  if (!silence_detector_.IsSilence(
          reinterpret_cast<const int16_t*>(buffer->mAudioData),
          buffer->mAudioDataByteSize / sizeof(int16_t) / kChannelsPerFrame)) {
    auto packet = std::make_unique<AudioPacket>();
    packet->add_data(buffer->mAudioData, buffer->mAudioDataByteSize);
    packet->set_encoding(AudioPacket::ENCODING_RAW);
    packet->set_sampling_rate(kSampleRate);
    packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
    packet->set_channels(AudioPacket::CHANNELS_STEREO);
    callback_.Run(std::move(packet));
  }

  // Recycle the buffer.
  // Only the first 2 params are needed for recording.
  OSStatus err = AudioQueueEnqueueBuffer(input_queue_, buffer, 0, NULL);
  HandleError(err, "AudioQueueEnqueueBuffer");
}

bool AudioCapturerMac::StartInputQueue() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!input_queue_);
  DCHECK(!is_started_);

  if (mac::CanCaptureAudio()) {
    HOST_LOG << "Audio capture is allowed.";
  } else {
    HOST_LOG << "We have no audio capture permission. Requesting one...";
    mac::RequestAudioCapturePermission(base::BindOnce([](bool granted) {
      // We don't need to defer the AudioQueue setup process as the buffers will
      // start being filled up immediately after the user approves the request.
      if (granted) {
        HOST_LOG << "Audio capture permission granted.";
      } else {
        LOG(ERROR) << "Audio capture permission not granted.";
      }
    }));
  }

  // Setup input queue.
  // This runs on AudioQueue's internal thread. For some reason if we specify
  // inCallbackRunLoop to current thread, then the callback will never get
  // called.
  OSStatus err =
      AudioQueueNewInput(&stream_description_, &HandleInputBufferOnAQThread,
                         /* inUserData= */ this, /* inCallbackRunLoop= */ NULL,
                         kCFRunLoopCommonModes, 0, &input_queue_);

  if (HandleError(err, "AudioQueueNewInput")) {
    return false;
  }

  // Use the loopback device for input.
  HOST_LOG << "Using loopback device: " << audio_device_uid_;
  base::apple::ScopedCFTypeRef<CFStringRef> device_uid =
      base::SysUTF8ToCFStringRef(audio_device_uid_);
  CFStringRef unowned_device_uid = device_uid.get();
  err = AudioQueueSetProperty(input_queue_, kAudioQueueProperty_CurrentDevice,
                              &unowned_device_uid, sizeof(unowned_device_uid));
  if (HandleError(err,
                  "AudioQueueSetProperty(kAudioQueueProperty_CurrentDevice)")) {
    return false;
  }

  // Setup buffers.
  for (int i = 0; i < kNumberBuffers; i++) {
    // |buffer| will automatically be freed when |input_queue_| is released.
    AudioQueueBufferRef buffer;
    err = AudioQueueAllocateBuffer(input_queue_, kBufferByteSize, &buffer);
    if (HandleError(err, "AudioQueueAllocateBuffer")) {
      return false;
    }
    err = AudioQueueEnqueueBuffer(input_queue_, buffer, 0, NULL);
    if (HandleError(err, "AudioQueueEnqueueBuffer")) {
      return false;
    }
  }

  // Start input queue.
  err = AudioQueueStart(input_queue_, NULL);
  if (err == kAudioQueueErr_InvalidDevice) {
    LOG(ERROR) << "Loopback device " << audio_device_uid_
               << " could not be located";
    return false;
  }
  if (HandleError(err, "AudioQueueStart")) {
    return false;
  }
  is_started_ = true;

  silence_detector_.Reset(kSampleRate, kChannelsPerFrame);

  return true;
}

void AudioCapturerMac::DisposeInputQueue() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!input_queue_) {
    return;
  }

  OSStatus err;

  if (is_started_) {
    err = AudioQueueStop(input_queue_, /* Immediate */ true);
    if (err != noErr) {
      LOG(DFATAL) << "Failed to call AudioQueueStop, error code: " << err;
    }
    is_started_ = false;
  }

  err = AudioQueueDispose(input_queue_, /* Immediate */ true);
  if (err != noErr) {
    LOG(DFATAL) << "Failed to call AudioQueueDispose, error code: " << err;
  }
  input_queue_ = nullptr;
}

bool AudioCapturerMac::HandleError(OSStatus err, const char* function_name) {
  if (err != noErr) {
    LOG(DFATAL) << "Failed to call " << function_name
                << ", error code: " << err;
    DisposeInputQueue();
    return true;
  }
  return false;
}

// AudioCapturer

bool AudioCapturer::IsSupported() {
  if (HostSettings::GetInstance()
          ->GetString(kMacAudioCaptureDeviceUid)
          .empty()) {
    HOST_LOG << kMacAudioCaptureDeviceUid << " is not set or not a string. "
             << "Audio capturer will be disabled.";
    return false;
  }
  HOST_LOG << kMacAudioCaptureDeviceUid
           << " is set. Audio capturer will be enabled.";
  return true;
}

std::unique_ptr<AudioCapturer> AudioCapturer::Create() {
  std::string device_uid =
      HostSettings::GetInstance()->GetString(kMacAudioCaptureDeviceUid);
  if (device_uid.empty()) {
    // AudioCapturer::Create is still called even when IsSupported() returns
    // false.
    return nullptr;
  }
  return std::make_unique<AudioCapturerMac>(device_uid);
}

}  // namespace remoting