You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2016 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #import "RTCAudioSessionConfiguration.h"
  11. #import "RTCAudioSession.h"
  12. #import "helpers/RTCDispatcher.h"
  13. #import "helpers/UIDevice+RTCDevice.h"
  14. // Try to use mono to save resources. Also avoids channel format conversion
  15. // in the I/O audio unit. Initial tests have shown that it is possible to use
  16. // mono natively for built-in microphones and for BT headsets but not for
  17. // wired headsets. Wired headsets only support stereo as native channel format
  18. // but it is a low cost operation to do a format conversion to mono in the
  19. // audio unit. Hence, we will not hit a RTC_CHECK in
  20. // VerifyAudioParametersForActiveAudioSession() for a mismatch between the
  21. // preferred number of channels and the actual number of channels.
  22. const int kRTCAudioSessionPreferredNumberOfChannels = 1;
  23. // Preferred hardware sample rate (unit is in Hertz). The client sample rate
  24. // will be set to this value as well to avoid resampling the the audio unit's
  25. // format converter. Note that, some devices, e.g. BT headsets, only supports
  26. // 8000Hz as native sample rate.
  27. const double kRTCAudioSessionHighPerformanceSampleRate = 48000.0;
  28. // A lower sample rate will be used for devices with only one core
  29. // (e.g. iPhone 4). The goal is to reduce the CPU load of the application.
  30. const double kRTCAudioSessionLowComplexitySampleRate = 16000.0;
  31. // Use a hardware I/O buffer size (unit is in seconds) that matches the 10ms
  32. // size used by WebRTC. The exact actual size will differ between devices.
  33. // Example: using 48kHz on iPhone 6 results in a native buffer size of
  34. // ~10.6667ms or 512 audio frames per buffer. The FineAudioBuffer instance will
  35. // take care of any buffering required to convert between native buffers and
  36. // buffers used by WebRTC. It is beneficial for the performance if the native
  37. // size is as an even multiple of 10ms as possible since it results in "clean"
  38. // callback sequence without bursts of callbacks back to back.
  39. const double kRTCAudioSessionHighPerformanceIOBufferDuration = 0.02;
  40. // Use a larger buffer size on devices with only one core (e.g. iPhone 4).
  41. // It will result in a lower CPU consumption at the cost of a larger latency.
  42. // The size of 60ms is based on instrumentation that shows a significant
  43. // reduction in CPU load compared with 10ms on low-end devices.
  44. // TODO(henrika): monitor this size and determine if it should be modified.
  45. const double kRTCAudioSessionLowComplexityIOBufferDuration = 0.06;
  46. static RTCAudioSessionConfiguration *gWebRTCConfiguration = nil;
  47. @implementation RTCAudioSessionConfiguration
  48. @synthesize category = _category;
  49. @synthesize categoryOptions = _categoryOptions;
  50. @synthesize mode = _mode;
  51. @synthesize sampleRate = _sampleRate;
  52. @synthesize ioBufferDuration = _ioBufferDuration;
  53. @synthesize inputNumberOfChannels = _inputNumberOfChannels;
  54. @synthesize outputNumberOfChannels = _outputNumberOfChannels;
  55. - (instancetype)init {
  56. if (self = [super init]) {
  57. // Use a category which supports simultaneous recording and playback.
  58. // By default, using this category implies that our app’s audio is
  59. // nonmixable, hence activating the session will interrupt any other
  60. // audio sessions which are also nonmixable.
  61. _category = AVAudioSessionCategoryPlayAndRecord;
  62. _categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth;
  63. // Specify mode for two-way voice communication (e.g. VoIP).
  64. _mode = AVAudioSessionModeVoiceChat;
  65. // Set the session's sample rate or the hardware sample rate.
  66. // It is essential that we use the same sample rate as stream format
  67. // to ensure that the I/O unit does not have to do sample rate conversion.
  68. // Set the preferred audio I/O buffer duration, in seconds.
  69. NSUInteger processorCount = [NSProcessInfo processInfo].processorCount;
  70. // Use best sample rate and buffer duration if the CPU has more than one
  71. // core.
  72. if (processorCount > 1 && [UIDevice deviceType] != RTCDeviceTypeIPhone4S) {
  73. _sampleRate = kRTCAudioSessionHighPerformanceSampleRate;
  74. _ioBufferDuration = kRTCAudioSessionHighPerformanceIOBufferDuration;
  75. } else {
  76. _sampleRate = kRTCAudioSessionLowComplexitySampleRate;
  77. _ioBufferDuration = kRTCAudioSessionLowComplexityIOBufferDuration;
  78. }
  79. // We try to use mono in both directions to save resources and format
  80. // conversions in the audio unit. Some devices does only support stereo;
  81. // e.g. wired headset on iPhone 6.
  82. // TODO(henrika): add support for stereo if needed.
  83. _inputNumberOfChannels = kRTCAudioSessionPreferredNumberOfChannels;
  84. _outputNumberOfChannels = kRTCAudioSessionPreferredNumberOfChannels;
  85. }
  86. return self;
  87. }
  88. + (void)initialize {
  89. gWebRTCConfiguration = [[self alloc] init];
  90. }
  91. + (instancetype)currentConfiguration {
  92. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  93. RTCAudioSessionConfiguration *config =
  94. [[RTCAudioSessionConfiguration alloc] init];
  95. config.category = session.category;
  96. config.categoryOptions = session.categoryOptions;
  97. config.mode = session.mode;
  98. config.sampleRate = session.sampleRate;
  99. config.ioBufferDuration = session.IOBufferDuration;
  100. config.inputNumberOfChannels = session.inputNumberOfChannels;
  101. config.outputNumberOfChannels = session.outputNumberOfChannels;
  102. return config;
  103. }
  104. + (instancetype)webRTCConfiguration {
  105. @synchronized(self) {
  106. return (RTCAudioSessionConfiguration *)gWebRTCConfiguration;
  107. }
  108. }
  109. + (void)setWebRTCConfiguration:(RTCAudioSessionConfiguration *)configuration {
  110. @synchronized(self) {
  111. gWebRTCConfiguration = configuration;
  112. }
  113. }
  114. @end