-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAudioFrameObserver.cpp
172 lines (148 loc) · 6.4 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
#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");
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::rtc::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::rtc::AGORA_IID_MEDIA_ENGINE);
if (mediaEngine) {
mediaEngine->registerAudioFrameObserver(nullptr);
}
}
AttachThreadScoped ats(jvm);
ats.env()->DeleteGlobalRef(jCallerRef);
jOnRecordAudioFrame = nullptr;
jOnPlaybackAudioFrame = nullptr;
jOnMixedAudioFrame = nullptr;
jOnPlaybackAudioFrameBeforeMixing = nullptr;
ats.env()->DeleteGlobalRef(jAudioFrameClass);
jAudioFrameInit = nullptr;
}
bool AudioFrameObserver::onRecordAudioFrame(const char *channelId,
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(const char *channelId,
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(const char *channelId,
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(
const char *channelId, rtc::uid_t 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;
}
jbyteArray AudioFrameObserver::NativeToJavaByteArray(JNIEnv *env,
AudioFrame &audioFrame) {
int length = audioFrame.samplesPerChannel * 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.samplesPerChannel,
(int)audioFrame.bytesPerSample, audioFrame.channels,
audioFrame.samplesPerSec, jByteArray,
audioFrame.renderTimeMs, audioFrame.avsync_type);
}
bool AudioFrameObserver::onEarMonitoringAudioFrame(
media::IAudioFrameObserverBase::AudioFrame &audioFrame) {
return false;
}
int AudioFrameObserver::getObservedAudioFramePosition() { return 0; }
media::IAudioFrameObserverBase::AudioParams
AudioFrameObserver::getPlaybackAudioParams() {
return media::IAudioFrameObserverBase::AudioParams();
}
media::IAudioFrameObserverBase::AudioParams
AudioFrameObserver::getRecordAudioParams() {
return media::IAudioFrameObserverBase::AudioParams();
}
media::IAudioFrameObserverBase::AudioParams
AudioFrameObserver::getMixedAudioParams() {
return media::IAudioFrameObserverBase::AudioParams();
}
media::IAudioFrameObserverBase::AudioParams
AudioFrameObserver::getEarMonitoringAudioParams() {
return media::IAudioFrameObserverBase::AudioParams();
}
} // namespace agora