|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | #include "JNI_AudioConverter.h" |
| 18 | +#include "JavaBoundary.h" |
18 | 19 | #include "NativeHandle.h" |
19 | 20 | #include "api/audio/audio_processing.h" |
20 | 21 | #include "media/audio/AudioConverter.h" |
|
26 | 27 | JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_convertInternal |
27 | 28 | (JNIEnv * env, jobject caller, jbyteArray src, jint nSrcSamples, jbyteArray dst, jint nDstSamples) |
28 | 29 | { |
29 | | - jni::NativeLease<jni::AudioConverter> converter(env, caller); |
30 | | - CHECK_HANDLE(converter); |
| 30 | + jni::CallFromJava(env, [&]() { |
| 31 | + jni::NativeLease<jni::AudioConverter> converter(env, caller); |
| 32 | + CHECK_HANDLE(converter); |
31 | 33 |
|
32 | | - if (src == nullptr || dst == nullptr) { |
33 | | - ThrowJavaException(env, "java/lang/NullPointerException", "Sample arrays must not be null"); |
34 | | - return; |
35 | | - } |
36 | | - |
37 | | - // The counts are samples over all channels. The converter checks them against its format, this the arrays. |
38 | | - const jlong bytesPerSample = sizeof(int16_t); |
39 | | - |
40 | | - if (nSrcSamples < 0 || nDstSamples < 0 || |
41 | | - env->GetArrayLength(src) < nSrcSamples * bytesPerSample || env->GetArrayLength(dst) < nDstSamples * bytesPerSample) { |
42 | | - ThrowJavaException(env, "java/lang/IllegalArgumentException", "Sample arrays too short for the requested samples"); |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - try { |
47 | | - auto releaseSrc = [env, src](jbyte * bytes) { |
48 | | - env->ReleaseByteArrayElements(src, bytes, JNI_ABORT); |
49 | | - }; |
50 | | - std::unique_ptr<jbyte, decltype(releaseSrc)> srcPtr(env->GetByteArrayElements(src, nullptr), releaseSrc); |
51 | | - if (!srcPtr) { |
| 34 | + if (src == nullptr || dst == nullptr) { |
| 35 | + ThrowJavaException(env, "java/lang/NullPointerException", "Sample arrays must not be null"); |
52 | 36 | return; |
53 | 37 | } |
54 | 38 |
|
55 | | - // Mode 0 writes the samples back when the VM handed out a copy. |
56 | | - auto releaseDst = [env, dst](jbyte * bytes) { |
57 | | - env->ReleaseByteArrayElements(dst, bytes, 0); |
58 | | - }; |
59 | | - std::unique_ptr<jbyte, decltype(releaseDst)> dstPtr(env->GetByteArrayElements(dst, nullptr), releaseDst); |
60 | | - if (!dstPtr) { |
| 39 | + // The counts are samples over all channels. The converter checks them against its format, this the arrays. |
| 40 | + const jlong bytesPerSample = sizeof(int16_t); |
| 41 | + |
| 42 | + if (nSrcSamples < 0 || nDstSamples < 0 || |
| 43 | + env->GetArrayLength(src) < nSrcSamples * bytesPerSample || env->GetArrayLength(dst) < nDstSamples * bytesPerSample) { |
| 44 | + ThrowJavaException(env, "java/lang/IllegalArgumentException", "Sample arrays too short for the requested samples"); |
61 | 45 | return; |
62 | 46 | } |
63 | 47 |
|
64 | | - converter->convert(reinterpret_cast<const int16_t *>(srcPtr.get()), nSrcSamples, reinterpret_cast<int16_t *>(dstPtr.get()), nDstSamples); |
65 | | - } |
66 | | - catch (const std::invalid_argument & e) { |
67 | | - ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what()); |
68 | | - } |
69 | | - catch (...) { |
70 | | - ThrowCxxJavaException(env); |
71 | | - } |
| 48 | + try { |
| 49 | + auto releaseSrc = [env, src](jbyte * bytes) { |
| 50 | + env->ReleaseByteArrayElements(src, bytes, JNI_ABORT); |
| 51 | + }; |
| 52 | + std::unique_ptr<jbyte, decltype(releaseSrc)> srcPtr(env->GetByteArrayElements(src, nullptr), releaseSrc); |
| 53 | + if (!srcPtr) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Mode 0 writes the samples back when the VM handed out a copy. |
| 58 | + auto releaseDst = [env, dst](jbyte * bytes) { |
| 59 | + env->ReleaseByteArrayElements(dst, bytes, 0); |
| 60 | + }; |
| 61 | + std::unique_ptr<jbyte, decltype(releaseDst)> dstPtr(env->GetByteArrayElements(dst, nullptr), releaseDst); |
| 62 | + if (!dstPtr) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + converter->convert(reinterpret_cast<const int16_t *>(srcPtr.get()), nSrcSamples, reinterpret_cast<int16_t *>(dstPtr.get()), nDstSamples); |
| 67 | + } |
| 68 | + catch (const std::invalid_argument & e) { |
| 69 | + ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what()); |
| 70 | + } |
| 71 | + catch (...) { |
| 72 | + ThrowCxxJavaException(env); |
| 73 | + } |
| 74 | + }); |
72 | 75 | } |
73 | 76 |
|
74 | 77 | JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_dispose |
75 | 78 | (JNIEnv * env, jobject caller) |
76 | 79 | { |
77 | | - jni::AudioConverter * converter = jni::TakeHandle<jni::AudioConverter>(env, caller); |
| 80 | + jni::CallFromJava(env, [&]() { |
| 81 | + jni::AudioConverter * converter = jni::TakeHandle<jni::AudioConverter>(env, caller); |
78 | 82 |
|
79 | | - if (converter == nullptr) { |
80 | | - return; |
81 | | - } |
| 83 | + if (converter == nullptr) { |
| 84 | + return; |
| 85 | + } |
82 | 86 |
|
83 | | - jni::ReleaseHandle(env, converter, [converter](bool) { |
84 | | - delete converter; |
| 87 | + jni::ReleaseHandle(env, converter, [converter](bool) { |
| 88 | + delete converter; |
| 89 | + }); |
85 | 90 | }); |
86 | 91 | } |
87 | 92 |
|
|
0 commit comments