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