forked from AgoraLibrary/flutter_agora_rtc_rawdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgoraAudioFrameObserver.mm
186 lines (163 loc) · 6.08 KB
/
AgoraAudioFrameObserver.mm
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
//
// AgoraAudioFrameObserver.mm
// react-native-agora-rawdata
//
// Created by LXH on 2020/11/10.
//
#import "AgoraAudioFrameObserver.h"
#import <AgoraRtcKit/IAgoraMediaEngine.h>
#import <AgoraRtcKit/IAgoraRtcEngine.h>
namespace agora {
class AudioFrameObserver : public media::IAudioFrameObserver {
public:
AudioFrameObserver(long long engineHandle, void *observer)
: engineHandle(engineHandle), observer(observer) {
auto rtcEngine = reinterpret_cast<rtc::IRtcEngine *>(engineHandle);
if (rtcEngine) {
util::AutoPtr<media::IMediaEngine> mediaEngine;
mediaEngine.queryInterface(rtcEngine, AGORA_IID_MEDIA_ENGINE);
if (mediaEngine) {
mediaEngine->registerAudioFrameObserver(this);
}
}
}
virtual ~AudioFrameObserver() {
auto rtcEngine = reinterpret_cast<rtc::IRtcEngine *>(engineHandle);
if (rtcEngine) {
util::AutoPtr<media::IMediaEngine> mediaEngine;
mediaEngine.queryInterface(rtcEngine, AGORA_IID_MEDIA_ENGINE);
if (mediaEngine) {
mediaEngine->registerAudioFrameObserver(nullptr);
}
}
}
public:
bool onRecordAudioFrame(AudioFrame &audioFrame) override {
@autoreleasepool {
AgoraAudioFrame *audioFrameApple = NativeToAppleAudioFrame(audioFrame);
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
if (observerApple.delegate != nil &&
[observerApple.delegate
respondsToSelector:@selector(onRecordAudioFrame:)]) {
return [observerApple.delegate onRecordAudioFrame:audioFrameApple];
}
}
return true;
}
bool onPlaybackAudioFrame(AudioFrame &audioFrame) override {
@autoreleasepool {
AgoraAudioFrame *audioFrameApple = NativeToAppleAudioFrame(audioFrame);
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
if (observerApple.delegate != nil &&
[observerApple.delegate
respondsToSelector:@selector(onPlaybackAudioFrame:)]) {
return [observerApple.delegate onPlaybackAudioFrame:audioFrameApple];
}
}
return true;
}
bool onMixedAudioFrame(AudioFrame &audioFrame) override {
@autoreleasepool {
AgoraAudioFrame *audioFrameApple = NativeToAppleAudioFrame(audioFrame);
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
if (observerApple.delegate != nil &&
[observerApple.delegate
respondsToSelector:@selector(onMixedAudioFrame:)]) {
return [observerApple.delegate onMixedAudioFrame:audioFrameApple];
}
}
return true;
}
bool onPlaybackAudioFrameBeforeMixing(unsigned int uid,
AudioFrame &audioFrame) override {
@autoreleasepool {
AgoraAudioFrame *audioFrameApple = NativeToAppleAudioFrame(audioFrame);
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
if (observerApple.delegate != nil &&
[observerApple.delegate respondsToSelector:@selector
(onPlaybackAudioFrameBeforeMixing:uid:)]) {
return [observerApple.delegate
onPlaybackAudioFrameBeforeMixing:audioFrameApple
uid:uid];
}
}
return true;
}
bool isMultipleChannelFrameWanted() override {
@autoreleasepool {
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
if (observerApple.delegate != nil &&
[observerApple.delegate
respondsToSelector:@selector(isMultipleChannelFrameWanted)]) {
return [observerApple.delegate isMultipleChannelFrameWanted];
}
}
return IAudioFrameObserver::isMultipleChannelFrameWanted();
}
bool onPlaybackAudioFrameBeforeMixingEx(const char *channelId,
unsigned int uid,
AudioFrame &audioFrame) override {
@autoreleasepool {
AgoraAudioFrame *audioFrameApple = NativeToAppleAudioFrame(audioFrame);
AgoraAudioFrameObserver *observerApple =
(__bridge AgoraAudioFrameObserver *)observer;
NSString *channelIdApple = [NSString stringWithUTF8String:channelId];
if (observerApple.delegate != nil &&
[observerApple.delegate respondsToSelector:@selector
(onPlaybackAudioFrameBeforeMixingEx:
channelId:uid:)]) {
return [observerApple.delegate
onPlaybackAudioFrameBeforeMixingEx:audioFrameApple
channelId:channelIdApple
uid:uid];
}
}
return IAudioFrameObserver::onPlaybackAudioFrameBeforeMixingEx(
channelId, uid, audioFrame);
}
private:
AgoraAudioFrame *NativeToAppleAudioFrame(AudioFrame &audioFrame) {
AgoraAudioFrame *audioFrameApple = [[AgoraAudioFrame alloc] init];
audioFrameApple.type = (AgoraAudioFrameType)audioFrame.type;
audioFrameApple.samples = audioFrame.samples;
audioFrameApple.bytesPerSample = audioFrame.bytesPerSample;
audioFrameApple.channels = audioFrame.channels;
audioFrameApple.samplesPerSec = audioFrame.samplesPerSec;
audioFrameApple.buffer = audioFrame.buffer;
audioFrameApple.renderTimeMs = audioFrame.renderTimeMs;
audioFrameApple.avsync_type = audioFrame.avsync_type;
return audioFrameApple;
}
private:
void *observer;
long long engineHandle;
};
}
@interface AgoraAudioFrameObserver ()
@property(nonatomic) agora::AudioFrameObserver *observer;
@end
@implementation AgoraAudioFrameObserver
- (instancetype)initWithEngineHandle:(NSUInteger)engineHandle {
if (self = [super init]) {
self.engineHandle = engineHandle;
}
return self;
}
- (void)registerAudioFrameObserver {
if (!_observer) {
_observer =
new agora::AudioFrameObserver(_engineHandle, (__bridge void *)self);
}
}
- (void)unregisterAudioFrameObserver {
if (_observer) {
delete _observer;
_observer = nullptr;
}
}
@end