Skip to content

Commit 6902a18

Browse files
committed
Merge branch 'm125_release' into livekit-prefixed-m125
2 parents 00fd89c + 7662c43 commit 6902a18

File tree

8 files changed

+256
-201
lines changed

8 files changed

+256
-201
lines changed

sdk/BUILD.gn

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,25 @@ if (is_ios || is_mac) {
939939
]
940940
}
941941

942+
rtc_library("audiorendereradapter_objc") {
943+
visibility = [ "*" ]
944+
sources = [
945+
"objc/api/RTCAudioRendererAdapter+Private.h",
946+
"objc/api/RTCAudioRendererAdapter.h",
947+
"objc/api/RTCAudioRendererAdapter.mm",
948+
]
949+
950+
configs += [ "..:common_objc" ]
951+
public_configs = [ ":common_config_objc" ]
952+
953+
deps = [
954+
":base_objc",
955+
":native_api",
956+
"../api:libjingle_peerconnection_api",
957+
"../api:media_stream_interface",
958+
]
959+
}
960+
942961
rtc_library("mediasource_objc") {
943962
sources = [
944963
"objc/api/peerconnection/RTCMediaSource+Private.h",
@@ -1150,6 +1169,7 @@ if (is_ios || is_mac) {
11501169
":objc_audio_device_module",
11511170
":videoframebuffer_objc",
11521171
":videorendereradapter_objc",
1172+
":audiorendereradapter_objc",
11531173
":videosource_objc",
11541174
":videotoolbox_objc",
11551175
"../api/crypto:frame_crypto_transformer",
@@ -1895,6 +1915,7 @@ if (is_ios || is_mac) {
18951915
"CoreMedia.framework",
18961916
"CoreVideo.framework",
18971917
"VideoToolbox.framework",
1918+
"Accelerate.framework",
18981919
]
18991920
}
19001921
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 "RTCAudioRendererAdapter.h"
18+
19+
#import "base/RTCAudioRenderer.h"
20+
21+
#include "api/media_stream_interface.h"
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
@interface RTC_OBJC_TYPE(RTCAudioRendererAdapter) ()
26+
27+
@property(nonatomic, readonly) id<RTC_OBJC_TYPE(RTCAudioRenderer)> audioRenderer;
28+
29+
@property(nonatomic, readonly) webrtc::AudioTrackSinkInterface *nativeAudioRenderer;
30+
31+
- (instancetype)initWithNativeRenderer:(id<RTC_OBJC_TYPE(RTCAudioRenderer)>)audioRenderer
32+
NS_DESIGNATED_INITIALIZER;
33+
34+
@end
35+
36+
NS_ASSUME_NONNULL_END
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 <Foundation/Foundation.h>
18+
19+
#import "RTCMacros.h"
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface RTC_OBJC_TYPE (RTCAudioRendererAdapter): NSObject
24+
25+
- (instancetype)init NS_UNAVAILABLE;
26+
27+
@end
28+
29+
NS_ASSUME_NONNULL_END
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

sdk/objc/api/peerconnection/RTCAudioTrack+Private.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ NS_ASSUME_NONNULL_BEGIN
2626
source:(RTC_OBJC_TYPE(RTCAudioSource) *)source
2727
trackId:(NSString *)trackId;
2828

29-
- (void)didCaptureSampleBuffer:(CMSampleBufferRef)sampleBuffer;
30-
3129
@end
3230

3331
NS_ASSUME_NONNULL_END

sdk/objc/api/peerconnection/RTCAudioTrack.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ RTC_OBJC_EXPORT
2424
/** The audio source for this audio track. */
2525
@property(nonatomic, readonly) RTC_OBJC_TYPE(RTCAudioSource) * source;
2626

27-
/** Register a renderer that will receive all audio CMSampleBuffers on this track.
28-
* Does not retain. */
2927
- (void)addRenderer:(id<RTC_OBJC_TYPE(RTCAudioRenderer)>)renderer;
3028

31-
/** Deregister a renderer */
3229
- (void)removeRenderer:(id<RTC_OBJC_TYPE(RTCAudioRenderer)>)renderer;
3330

31+
- (void)removeAllRenderers;
32+
3433
@end
3534

3635
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)