| 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 | #include <jni.h> |
| 18 |
| 19 | #include <android/log.h> |
| 20 |
| 21 | #include <cstdlib> |
| 22 |
| 23 | #include "opus.h" // NOLINT |
| 24 | #include "opus_multistream.h" // NOLINT |
| 25 |
| 26 | #define LOG_TAG "opus_jni" |
| 27 | #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \ |
| 28 | __VA_ARGS__)) |
| 29 |
| 30 | #define DECODER_FUNC(RETURN_TYPE, NAME, ...) \ |
| 31 | extern "C" { \ |
| 32 | JNIEXPORT RETURN_TYPE \ |
| 33 | Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME \ |
| 34 | (JNIEnv* env, jobject thiz, ##__VA_ARGS__);\ |
| 35 | } \ |
| 36 | JNIEXPORT RETURN_TYPE \ |
| 37 | Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_ ## NAME \ |
| 38 | (JNIEnv* env, jobject thiz, ##__VA_ARGS__)\ |
| 39 |
| 40 | #define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \ |
| 41 | extern "C" { \ |
| 42 | JNIEXPORT RETURN_TYPE \ |
| 43 | Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME \ |
| 44 | (JNIEnv* env, jobject thiz, ##__VA_ARGS__);\ |
| 45 | } \ |
| 46 | JNIEXPORT RETURN_TYPE \ |
| 47 | Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_ ## NAME \ |
| 48 | (JNIEnv* env, jobject thiz, ##__VA_ARGS__)\ |
| 49 |
| 50 | // JNI references for SimpleOutputBuffer class. |
| 51 | static jmethodID outputBufferInit; |
| 52 |
| 53 | static const int kBytesPerSample = 2; // opus fixed point uses 16 bit samples. |
| 54 | static const int kMaxOpusOutputPacketSizeSamples = 960 * 6; |
| 55 | static int channelCount; |
| 56 | static int errorCode; |
| 57 |
| 58 | DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount, |
| 59 | jint numStreams, jint numCoupled, jint gain, jbyteArray jStreamMap) { |
| 60 | int status = OPUS_INVALID_STATE; |
| 61 | ::channelCount = channelCount; |
| 62 | errorCode = 0; |
| 63 | jbyte* streamMapBytes = env->GetByteArrayElements(jStreamMap, 0); |
| 64 | uint8_t* streamMap = reinterpret_cast<uint8_t*>(streamMapBytes); |
| 65 | OpusMSDecoder* decoder = opus_multistream_decoder_create( |
| 66 | sampleRate, channelCount, numStreams, numCoupled, streamMap, &status); |
| 67 | env->ReleaseByteArrayElements(jStreamMap, streamMapBytes, 0); |
| 68 | if (!decoder || status != OPUS_OK) { |
| 69 | LOGE("Failed to create Opus Decoder; status=%s", opus_strerror(status)); |
| 70 | return 0; |
| 71 | } |
| 72 | status = opus_multistream_decoder_ctl(decoder, OPUS_SET_GAIN(gain)); |
| 73 | if (status != OPUS_OK) { |
| 74 | LOGE("Failed to set Opus header gain; status=%s", opus_strerror(status)); |
| 75 | return 0; |
| 76 | } |
| 77 |
| 78 | // Populate JNI References. |
| 79 | const jclass outputBufferClass = env->FindClass( |
| 80 | "com/google/android/exoplayer2/decoder/SimpleOutputBuffer"); |
| 81 | outputBufferInit = env->GetMethodID(outputBufferClass, "init", |
| 82 | "(JI)Ljava/nio/ByteBuffer;"); |
| 83 |
| 84 | return reinterpret_cast<intptr_t>(decoder); |
| 85 | } |
| 86 |
| 87 | DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs, |
| 88 | jobject jInputBuffer, jint inputSize, jobject jOutputBuffer) { |
| 89 | OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder); |
| 90 | const uint8_t* inputBuffer = |
| 91 | reinterpret_cast<const uint8_t*>( |
| 92 | env->GetDirectBufferAddress(jInputBuffer)); |
| 93 |
| 94 | const jint outputSize = |
| 95 | kMaxOpusOutputPacketSizeSamples * kBytesPerSample * channelCount; |
| 96 |
| 97 | env->CallObjectMethod(jOutputBuffer, outputBufferInit, jTimeUs, outputSize); |
| 98 | if (env->ExceptionCheck()) { |
| 99 | // Exception is thrown in Java when returning from the native call. |
| 100 | return -1; |
| 101 | } |
| 102 | const jobject jOutputBufferData = env->CallObjectMethod(jOutputBuffer, |
| 103 | outputBufferInit, jTimeUs, outputSize); |
| 104 | if (env->ExceptionCheck()) { |
| 105 | // Exception is thrown in Java when returning from the native call. |
| 106 | return -1; |
| 107 | } |
| 108 |
| 109 | int16_t* outputBufferData = reinterpret_cast<int16_t*>( |
| 110 | env->GetDirectBufferAddress(jOutputBufferData)); |
| 111 | int sampleCount = opus_multistream_decode(decoder, inputBuffer, inputSize, |
| 112 | outputBufferData, kMaxOpusOutputPacketSizeSamples, 0); |
| 113 | // record error code |
| 114 | errorCode = (sampleCount < 0) ? sampleCount : 0; |
| 115 | return (sampleCount < 0) ? sampleCount |
| 116 | : sampleCount * kBytesPerSample * channelCount; |
| 117 | } |
| 118 |
| 119 | DECODER_FUNC(jint, opusSecureDecode, jlong jDecoder, jlong jTimeUs, |
| 120 | jobject jInputBuffer, jint inputSize, jobject jOutputBuffer, |
| 121 | jint sampleRate, jobject mediaCrypto, jint inputMode, jbyteArray key, |
| 122 | jbyteArray javaIv, jint inputNumSubSamples, jintArray numBytesOfClearData, |
| 123 | jintArray numBytesOfEncryptedData) { |
| 124 | // Doesn't support |
| 125 | // Java client should have checked vpxSupportSecureDecode |
| 126 | // and avoid calling this |
| 127 | // return -2 (DRM Error) |
| 128 | return -2; |
| 129 | } |
| 130 |
| 131 | DECODER_FUNC(void, opusClose, jlong jDecoder) { |
| 132 | OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder); |
| 133 | opus_multistream_decoder_destroy(decoder); |
| 134 | } |
| 135 |
| 136 | DECODER_FUNC(void, opusReset, jlong jDecoder) { |
| 137 | OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder); |
| 138 | opus_multistream_decoder_ctl(decoder, OPUS_RESET_STATE); |
| 139 | } |
| 140 |
| 141 | DECODER_FUNC(jstring, opusGetErrorMessage, jlong jContext) { |
| 142 | return env->NewStringUTF(opus_strerror(errorCode)); |
| 143 | } |
| 144 |
| 145 | DECODER_FUNC(jint, opusGetErrorCode, jlong jContext) { |
| 146 | return errorCode; |
| 147 | } |
| 148 |
| 149 | LIBRARY_FUNC(jstring, opusIsSecureDecodeSupported) { |
| 150 | // Doesn't support |
| 151 | return 0; |
| 152 | } |
| 153 |
| 154 | LIBRARY_FUNC(jstring, opusGetVersion) { |
| 155 | return env->NewStringUTF(opus_get_version_string()); |
| 156 | } |
| 157 |