diff --git a/sdk/objc/api/RTCAudioRendererAdapter.mm b/sdk/objc/api/RTCAudioRendererAdapter.mm index 67c05792ad..20b4a651b9 100644 --- a/sdk/objc/api/RTCAudioRendererAdapter.mm +++ b/sdk/objc/api/RTCAudioRendererAdapter.mm @@ -39,12 +39,12 @@ void OnData(const void *audio_data, int bits_per_sample, int sample_rate, AudioStreamBasicDescription sd = { .mSampleRate = static_cast(sample_rate), .mFormatID = kAudioFormatLinearPCM, - .mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked, - .mBytesPerPacket = static_cast(number_of_channels * 4), + .mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, + .mBytesPerPacket = static_cast(number_of_channels * 2), .mFramesPerPacket = 1, - .mBytesPerFrame = static_cast(number_of_channels * 4), + .mBytesPerFrame = static_cast(number_of_channels * 2), .mChannelsPerFrame = static_cast(number_of_channels), - .mBitsPerChannel = 32, + .mBitsPerChannel = 16, .mReserved = 0}; CMFormatDescriptionRef formatDescription = nullptr; @@ -69,16 +69,25 @@ void OnData(const void *audio_data, int bits_per_sample, int sample_rate, } pcmBuffer.frameLength = frameCount; + + // Handle both mono and stereo const int16_t *inputData = static_cast(audio_data); - const float scale = 1.0f / 32768.0f; - - dispatch_apply(number_of_channels, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), - ^(size_t channel) { - vDSP_vflt16(inputData + channel * number_of_frames, 1, - pcmBuffer.floatChannelData[channel], 1, frameCount); - vDSP_vsmul(pcmBuffer.floatChannelData[channel], 1, &scale, - pcmBuffer.floatChannelData[channel], 1, frameCount); - }); + if (number_of_channels == 1) { + // Mono: straight copy + memcpy(pcmBuffer.int16ChannelData[0], inputData, number_of_frames * sizeof(int16_t)); + } else if (number_of_channels == 2) { + // Stereo: manual deinterleave + int16_t *leftChannel = pcmBuffer.int16ChannelData[0]; + int16_t *rightChannel = pcmBuffer.int16ChannelData[1]; + + for (size_t i = 0; i < number_of_frames; i++) { + leftChannel[i] = inputData[i * 2]; + rightChannel[i] = inputData[i * 2 + 1]; + } + } else { + NSLog(@"Unsupported number of channels: %zu", number_of_channels); + return; + } [adapter_.audioRenderer renderPCMBuffer:pcmBuffer]; }