forked from AgoraLibrary/flutter_agora_rtc_rawdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioFrameObserver.cpp
173 lines (154 loc) · 6.62 KB
/
AudioFrameObserver.cpp
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
#include "AudioFrameObserver.h"
#include "VMUtil.h"
namespace agora {
AudioFrameObserver::AudioFrameObserver(JNIEnv *env, jobject jCaller,
long long engineHandle)
: jCallerRef(env->NewGlobalRef(jCaller)), engineHandle(engineHandle) {
jclass jCallerClass = env->GetObjectClass(jCallerRef);
jOnRecordAudioFrame =
env->GetMethodID(jCallerClass, "onRecordAudioFrame",
"(Lio/agora/rtc/rawdata/base/AudioFrame;)Z");
jOnPlaybackAudioFrame =
env->GetMethodID(jCallerClass, "onPlaybackAudioFrame",
"(Lio/agora/rtc/rawdata/base/AudioFrame;)Z");
jOnMixedAudioFrame =
env->GetMethodID(jCallerClass, "onMixedAudioFrame",
"(Lio/agora/rtc/rawdata/base/AudioFrame;)Z");
jOnPlaybackAudioFrameBeforeMixing =
env->GetMethodID(jCallerClass, "onPlaybackAudioFrameBeforeMixing",
"(ILio/agora/rtc/rawdata/base/AudioFrame;)Z");
jIsMultipleChannelFrameWanted =
env->GetMethodID(jCallerClass, "isMultipleChannelFrameWanted", "()Z");
jOnPlaybackAudioFrameBeforeMixingEx = env->GetMethodID(
jCallerClass, "onPlaybackAudioFrameBeforeMixingEx",
"(Ljava/lang/String;ILio/agora/rtc/rawdata/base/AudioFrame;)Z");
env->DeleteLocalRef(jCallerClass);
jclass jAudioFrame = env->FindClass("io/agora/rtc/rawdata/base/AudioFrame");
jAudioFrameClass = (jclass)env->NewGlobalRef(jAudioFrame);
jAudioFrameInit =
env->GetMethodID(jAudioFrameClass, "<init>", "(IIIII[BJI)V");
env->DeleteLocalRef(jAudioFrame);
env->GetJavaVM(&jvm);
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);
}
}
}
AudioFrameObserver::~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(this);
}
}
AttachThreadScoped ats(jvm);
ats.env()->DeleteGlobalRef(jCallerRef);
jOnRecordAudioFrame = nullptr;
jOnPlaybackAudioFrame = nullptr;
jOnMixedAudioFrame = nullptr;
jOnPlaybackAudioFrameBeforeMixing = nullptr;
jIsMultipleChannelFrameWanted = nullptr;
jOnPlaybackAudioFrameBeforeMixingEx = nullptr;
ats.env()->DeleteGlobalRef(jAudioFrameClass);
jAudioFrameInit = nullptr;
}
bool AudioFrameObserver::onRecordAudioFrame(AudioFrame &audioFrame) {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jbyteArray arr = NativeToJavaByteArray(env, audioFrame);
jobject obj = NativeToJavaAudioFrame(env, audioFrame, arr);
jboolean ret = env->CallBooleanMethod(jCallerRef, jOnRecordAudioFrame, obj);
env->GetByteArrayRegion(arr, 0, env->GetArrayLength(arr),
static_cast<jbyte *>(audioFrame.buffer));
env->DeleteLocalRef(arr);
env->DeleteLocalRef(obj);
return ret;
}
bool AudioFrameObserver::onPlaybackAudioFrame(AudioFrame &audioFrame) {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jbyteArray arr = NativeToJavaByteArray(env, audioFrame);
jobject obj = NativeToJavaAudioFrame(env, audioFrame, arr);
jboolean ret = env->CallBooleanMethod(jCallerRef, jOnPlaybackAudioFrame, obj);
env->GetByteArrayRegion(arr, 0, env->GetArrayLength(arr),
static_cast<jbyte *>(audioFrame.buffer));
env->DeleteLocalRef(arr);
env->DeleteLocalRef(obj);
return ret;
}
bool AudioFrameObserver::onMixedAudioFrame(AudioFrame &audioFrame) {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jbyteArray arr = NativeToJavaByteArray(env, audioFrame);
jobject obj = NativeToJavaAudioFrame(env, audioFrame, arr);
jboolean ret = env->CallBooleanMethod(jCallerRef, jOnMixedAudioFrame, obj);
env->GetByteArrayRegion(arr, 0, env->GetArrayLength(arr),
static_cast<jbyte *>(audioFrame.buffer));
env->DeleteLocalRef(arr);
env->DeleteLocalRef(obj);
return ret;
}
bool AudioFrameObserver::onPlaybackAudioFrameBeforeMixing(
unsigned int uid, AudioFrame &audioFrame) {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jbyteArray arr = NativeToJavaByteArray(env, audioFrame);
jobject obj = NativeToJavaAudioFrame(env, audioFrame, arr);
jboolean ret = env->CallBooleanMethod(
jCallerRef, jOnPlaybackAudioFrameBeforeMixing, uid, obj);
env->GetByteArrayRegion(arr, 0, env->GetArrayLength(arr),
static_cast<jbyte *>(audioFrame.buffer));
env->DeleteLocalRef(arr);
env->DeleteLocalRef(obj);
return ret;
}
bool AudioFrameObserver::isMultipleChannelFrameWanted() {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jboolean ret =
env->CallBooleanMethod(jCallerRef, jIsMultipleChannelFrameWanted);
return ret;
}
bool AudioFrameObserver::onPlaybackAudioFrameBeforeMixingEx(
const char *channelId, unsigned int uid, AudioFrame &audioFrame) {
AttachThreadScoped ats(jvm);
JNIEnv *env = ats.env();
jbyteArray arr = NativeToJavaByteArray(env, audioFrame);
jobject obj = NativeToJavaAudioFrame(env, audioFrame, arr);
jstring str = env->NewStringUTF(channelId);
jboolean ret = env->CallBooleanMethod(
jCallerRef, jOnPlaybackAudioFrameBeforeMixingEx, str, uid, obj);
env->GetByteArrayRegion(arr, 0, env->GetArrayLength(arr),
static_cast<jbyte *>(audioFrame.buffer));
env->DeleteLocalRef(arr);
env->DeleteLocalRef(obj);
env->DeleteLocalRef(str);
return ret;
}
jbyteArray AudioFrameObserver::NativeToJavaByteArray(JNIEnv *env,
AudioFrame &audioFrame) {
int length =
audioFrame.samples * audioFrame.channels * audioFrame.bytesPerSample;
jbyteArray jByteArray = env->NewByteArray(length);
if (audioFrame.buffer) {
env->SetByteArrayRegion(jByteArray, 0, length,
static_cast<const jbyte *>(audioFrame.buffer));
}
return jByteArray;
}
jobject AudioFrameObserver::NativeToJavaAudioFrame(JNIEnv *env,
AudioFrame &audioFrame,
jbyteArray jByteArray) {
return env->NewObject(jAudioFrameClass, jAudioFrameInit, (int)audioFrame.type,
audioFrame.samples, audioFrame.bytesPerSample,
audioFrame.channels, audioFrame.samplesPerSec,
jByteArray, audioFrame.renderTimeMs,
audioFrame.avsync_type);
}
} // namespace agora