Telegram-Android/TMessagesProj/jni/exoplayer/opus_jni.cc

180 lines
7 KiB
C++
Raw Permalink Normal View History

2018-07-30 04:07:02 +02:00
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-04-03 15:15:52 +02:00
#ifdef __ANDROID__
2018-07-30 04:07:02 +02:00
#include <android/log.h>
2023-04-03 15:15:52 +02:00
#endif
#include <jni.h>
2018-07-30 04:07:02 +02:00
2023-04-03 15:15:52 +02:00
#include <cstdint>
2018-07-30 04:07:02 +02:00
#include <cstdlib>
2023-04-03 15:15:52 +02:00
#include "opus.h" // NOLINT
2018-07-30 04:07:02 +02:00
#include "opus_multistream.h" // NOLINT
2023-04-03 15:15:52 +02:00
#ifdef __ANDROID__
2018-07-30 04:07:02 +02:00
#define LOG_TAG "opus_jni"
2023-04-03 15:15:52 +02:00
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#else // __ANDROID__
#define LOGE(...) \
do { \
} while (0)
#endif // __ANDROID__
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusDecoder_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__)
#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_opus_OpusLibrary_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__)
2018-07-30 04:07:02 +02:00
// JNI references for SimpleOutputBuffer class.
static jmethodID outputBufferInit;
2023-04-03 15:15:52 +02:00
static const int kBytesPerIntPcmSample = 2;
static const int kBytesPerFloatSample = 4;
2018-07-30 04:07:02 +02:00
static const int kMaxOpusOutputPacketSizeSamples = 960 * 6;
static int channelCount;
static int errorCode;
2023-04-03 15:15:52 +02:00
static bool outputFloat = false;
2018-07-30 04:07:02 +02:00
DECODER_FUNC(jlong, opusInit, jint sampleRate, jint channelCount,
2023-04-03 15:15:52 +02:00
jint numStreams, jint numCoupled, jint gain,
jbyteArray jStreamMap) {
int status = OPUS_INVALID_STATE;
::channelCount = channelCount;
errorCode = 0;
jbyte* streamMapBytes = env->GetByteArrayElements(jStreamMap, 0);
uint8_t* streamMap = reinterpret_cast<uint8_t*>(streamMapBytes);
OpusMSDecoder* decoder = opus_multistream_decoder_create(
sampleRate, channelCount, numStreams, numCoupled, streamMap, &status);
env->ReleaseByteArrayElements(jStreamMap, streamMapBytes, 0);
if (!decoder || status != OPUS_OK) {
LOGE("Failed to create Opus Decoder; status=%s", opus_strerror(status));
return 0;
}
status = opus_multistream_decoder_ctl(decoder, OPUS_SET_GAIN(gain));
if (status != OPUS_OK) {
LOGE("Failed to set Opus header gain; status=%s", opus_strerror(status));
return 0;
}
// Populate JNI References.
const jclass outputBufferClass = env->FindClass(
"com/google/android/exoplayer2/decoder/SimpleDecoderOutputBuffer");
outputBufferInit =
env->GetMethodID(outputBufferClass, "init", "(JI)Ljava/nio/ByteBuffer;");
return reinterpret_cast<intptr_t>(decoder);
2018-07-30 04:07:02 +02:00
}
DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs,
2020-07-26 10:03:38 +02:00
jobject jInputBuffer, jint inputSize, jobject jOutputBuffer) {
2023-04-03 15:15:52 +02:00
OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder);
const uint8_t* inputBuffer = reinterpret_cast<const uint8_t*>(
env->GetDirectBufferAddress(jInputBuffer));
const int byteSizePerSample =
outputFloat ? kBytesPerFloatSample : kBytesPerIntPcmSample;
const jint outputSize =
kMaxOpusOutputPacketSizeSamples * byteSizePerSample * channelCount;
env->CallObjectMethod(jOutputBuffer, outputBufferInit, jTimeUs, outputSize);
if (env->ExceptionCheck()) {
// Exception is thrown in Java when returning from the native call.
return -1;
}
const jobject jOutputBufferData = env->CallObjectMethod(
jOutputBuffer, outputBufferInit, jTimeUs, outputSize);
if (env->ExceptionCheck()) {
// Exception is thrown in Java when returning from the native call.
return -1;
}
int sampleCount;
if (outputFloat) {
float* outputBufferData = reinterpret_cast<float*>(
env->GetDirectBufferAddress(jOutputBufferData));
sampleCount = opus_multistream_decode_float(
decoder, inputBuffer, inputSize, outputBufferData,
kMaxOpusOutputPacketSizeSamples, 0);
} else {
int16_t* outputBufferData = reinterpret_cast<int16_t*>(
env->GetDirectBufferAddress(jOutputBufferData));
sampleCount = opus_multistream_decode(decoder, inputBuffer, inputSize,
outputBufferData,
kMaxOpusOutputPacketSizeSamples, 0);
}
// record error code
errorCode = (sampleCount < 0) ? sampleCount : 0;
return (sampleCount < 0) ? sampleCount
: sampleCount * byteSizePerSample * channelCount;
2018-07-30 04:07:02 +02:00
}
DECODER_FUNC(jint, opusSecureDecode, jlong jDecoder, jlong jTimeUs,
2020-07-26 10:03:38 +02:00
jobject jInputBuffer, jint inputSize, jobject jOutputBuffer,
2023-04-03 15:15:52 +02:00
jint sampleRate, jobject mediaCrypto, jint inputMode,
jbyteArray key, jbyteArray javaIv, jint inputNumSubSamples,
jintArray numBytesOfClearData, jintArray numBytesOfEncryptedData) {
// Doesn't support
// Java client should have checked vpxSupportSecureDecode
// and avoid calling this
// return -2 (DRM Error)
return -2;
2018-07-30 04:07:02 +02:00
}
DECODER_FUNC(void, opusClose, jlong jDecoder) {
2023-04-03 15:15:52 +02:00
OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder);
opus_multistream_decoder_destroy(decoder);
2018-07-30 04:07:02 +02:00
}
DECODER_FUNC(void, opusReset, jlong jDecoder) {
2023-04-03 15:15:52 +02:00
OpusMSDecoder* decoder = reinterpret_cast<OpusMSDecoder*>(jDecoder);
opus_multistream_decoder_ctl(decoder, OPUS_RESET_STATE);
2018-07-30 04:07:02 +02:00
}
DECODER_FUNC(jstring, opusGetErrorMessage, jlong jContext) {
2023-04-03 15:15:52 +02:00
return env->NewStringUTF(opus_strerror(errorCode));
2018-07-30 04:07:02 +02:00
}
2023-04-03 15:15:52 +02:00
DECODER_FUNC(jint, opusGetErrorCode, jlong jContext) { return errorCode; }
DECODER_FUNC(void, opusSetFloatOutput) { outputFloat = true; }
2018-07-30 04:07:02 +02:00
LIBRARY_FUNC(jstring, opusIsSecureDecodeSupported) {
2023-04-03 15:15:52 +02:00
// Doesn't support
return 0;
2018-07-30 04:07:02 +02:00
}
LIBRARY_FUNC(jstring, opusGetVersion) {
2023-04-03 15:15:52 +02:00
return env->NewStringUTF(opus_get_version_string());
2018-07-30 04:07:02 +02:00
}