|
| 1 | +/* |
| 2 | + * Copyright 2024 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import <Accelerate/Accelerate.h> |
| 18 | +#import "RTCAudioRendererAdapter+Private.h" |
| 19 | + |
| 20 | +#include <memory> |
| 21 | + |
| 22 | +namespace webrtc { |
| 23 | + |
| 24 | +class AudioRendererAdapter : public webrtc::AudioTrackSinkInterface { |
| 25 | + public: |
| 26 | + AudioRendererAdapter(RTC_OBJC_TYPE(RTCAudioRendererAdapter) * adapter) { adapter_ = adapter; } |
| 27 | + |
| 28 | + private: |
| 29 | + __weak RTC_OBJC_TYPE(RTCAudioRendererAdapter) * adapter_; |
| 30 | + |
| 31 | + void OnData(const void *audio_data, int bits_per_sample, int sample_rate, |
| 32 | + size_t number_of_channels, size_t number_of_frames, |
| 33 | + absl::optional<int64_t> absolute_capture_timestamp_ms) override { |
| 34 | + OSStatus status; |
| 35 | + AudioChannelLayout acl = {}; |
| 36 | + acl.mChannelLayoutTag = |
| 37 | + (number_of_channels == 2) ? kAudioChannelLayoutTag_Stereo : kAudioChannelLayoutTag_Mono; |
| 38 | + |
| 39 | + AudioStreamBasicDescription sd = { |
| 40 | + .mSampleRate = static_cast<Float64>(sample_rate), |
| 41 | + .mFormatID = kAudioFormatLinearPCM, |
| 42 | + .mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked, |
| 43 | + .mBytesPerPacket = static_cast<UInt32>(number_of_channels * 4), |
| 44 | + .mFramesPerPacket = 1, |
| 45 | + .mBytesPerFrame = static_cast<UInt32>(number_of_channels * 4), |
| 46 | + .mChannelsPerFrame = static_cast<UInt32>(number_of_channels), |
| 47 | + .mBitsPerChannel = 32, |
| 48 | + .mReserved = 0}; |
| 49 | + |
| 50 | + CMFormatDescriptionRef formatDescription = nullptr; |
| 51 | + status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &sd, sizeof(acl), &acl, 0, NULL, |
| 52 | + NULL, &formatDescription); |
| 53 | + if (status != noErr) { |
| 54 | + NSLog(@"RTCAudioTrack: Failed to create audio formatDescription description. Error: %d", |
| 55 | + (int)status); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + AVAudioFormat *format = |
| 60 | + [[AVAudioFormat alloc] initWithCMAudioFormatDescription:formatDescription]; |
| 61 | + CFRelease(formatDescription); |
| 62 | + |
| 63 | + AVAudioFrameCount frameCount = static_cast<AVAudioFrameCount>(number_of_frames); |
| 64 | + AVAudioPCMBuffer *pcmBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format |
| 65 | + frameCapacity:frameCount]; |
| 66 | + if (!pcmBuffer) { |
| 67 | + NSLog(@"Failed to create AVAudioPCMBuffer"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + pcmBuffer.frameLength = frameCount; |
| 72 | + const int16_t *inputData = static_cast<const int16_t *>(audio_data); |
| 73 | + const float scale = 1.0f / 32768.0f; |
| 74 | + |
| 75 | + dispatch_apply(number_of_channels, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), |
| 76 | + ^(size_t channel) { |
| 77 | + vDSP_vflt16(inputData + channel * number_of_frames, 1, |
| 78 | + pcmBuffer.floatChannelData[channel], 1, frameCount); |
| 79 | + vDSP_vsmul(pcmBuffer.floatChannelData[channel], 1, &scale, |
| 80 | + pcmBuffer.floatChannelData[channel], 1, frameCount); |
| 81 | + }); |
| 82 | + |
| 83 | + [adapter_.audioRenderer renderPCMBuffer:pcmBuffer]; |
| 84 | + } |
| 85 | +}; |
| 86 | +} // namespace webrtc |
| 87 | + |
| 88 | +@implementation RTC_OBJC_TYPE (RTCAudioRendererAdapter) { |
| 89 | + std::unique_ptr<webrtc::AudioRendererAdapter> _adapter; |
| 90 | +} |
| 91 | + |
| 92 | +@synthesize audioRenderer = _audioRenderer; |
| 93 | + |
| 94 | +- (instancetype)initWithNativeRenderer:(id<RTC_OBJC_TYPE(RTCAudioRenderer)>)audioRenderer { |
| 95 | + NSParameterAssert(audioRenderer); |
| 96 | + if (self = [super init]) { |
| 97 | + _audioRenderer = audioRenderer; |
| 98 | + _adapter.reset(new webrtc::AudioRendererAdapter(self)); |
| 99 | + } |
| 100 | + return self; |
| 101 | +} |
| 102 | + |
| 103 | +- (webrtc::AudioTrackSinkInterface *)nativeAudioRenderer { |
| 104 | + return _adapter.get(); |
| 105 | +} |
| 106 | + |
| 107 | +@end |
0 commit comments