Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
extern "C" {
#endif
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injectDirectBuffer
(JNIEnv *, jobject, jobject, jint, jint, jint, jlong, jint, jboolean, jlong, jboolean);
(JNIEnv *, jobject, jobject, jint, jint, jint, jlong, jint, jboolean, jlong, jboolean, jintArray);

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injectByteArray
(JNIEnv *, jobject, jbyteArray, jint, jlong, jint, jboolean, jlong, jboolean);
(JNIEnv *, jobject, jbyteArray, jint, jlong, jint, jboolean, jlong, jboolean, jintArray);

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_dispose
(JNIEnv *, jobject);
Expand Down
13 changes: 7 additions & 6 deletions webrtc-jni/src/main/cpp/include/api/RTCRtpContributingSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#include <jni.h>

#include <cstdint>
#include <optional>

namespace jni
{
namespace RTCRtpContributingSource
Expand All @@ -35,15 +38,13 @@ namespace jni

jclass cls;
jmethodID ctor;
jfieldID timestamp;
jfieldID sourceId;
jfieldID sourceType;
jfieldID audioLevel;
jfieldID rtpTimestamp;
};

// Converts an RTP audio level in -dBov to the linear value between 0 and
// 1 of the Java classes. A source without a level reads as silence.
jdouble toLinearAudioLevel(const std::optional<uint8_t> & audioLevel);

JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpSource & source);
webrtc::RtpSource toNative(JNIEnv * env, const JavaRef<jobject> & source);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ namespace jni

jclass cls;
jmethodID ctor;
jfieldID voiceActivityFlag;
};

JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpSource & source);
webrtc::RtpSource toNative(JNIEnv * env, const JavaRef<jobject> & source);
}
}

Expand Down
38 changes: 32 additions & 6 deletions webrtc-jni/src/main/cpp/src/JNI_RTCEncodedAudioFrameInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@
#include <memory>
#include <optional>
#include <span>
#include <vector>

static std::vector<uint32_t> toCsrcs(JNIEnv * env, jintArray jSources)
{
std::vector<jint> sources(static_cast<size_t>(env->GetArrayLength(jSources)));

env->GetIntArrayRegion(jSources, 0, static_cast<jsize>(sources.size()), sources.data());

return std::vector<uint32_t>(sources.begin(), sources.end());
}

static void injectFrame(webrtc::EncodedAudioFrameInjectorInterface * injector,
std::span<const uint8_t> data, jint payloadType, jlong rtpTimestamp,
jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime)
jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime,
const std::vector<uint32_t> & csrcs)
{
std::optional<uint8_t> level;
std::optional<uint64_t> captureTime;
Expand All @@ -47,14 +58,15 @@ static void injectFrame(webrtc::EncodedAudioFrameInjectorInterface * injector,
auto frame = webrtc::CreateOutgoingAudioFrame(
webrtc::TransformableAudioFrameInterface::FrameType::kAudioFrameSpeech,
webrtc::PayloadType(payloadType), static_cast<uint32_t>(rtpTimestamp), data.data(), data.size(),
captureTime, 0, {}, "", std::nullopt, level);
captureTime, 0, csrcs, "", std::nullopt, level);

injector->InjectFrame(std::move(frame));
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injectDirectBuffer
(JNIEnv * env, jobject caller, jobject jBuffer, jint position, jint length, jint payloadType,
jlong rtpTimestamp, jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime)
jlong rtpTimestamp, jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime,
jintArray jContributingSources)
{
jni::CallFromJava(env, [&]() {
jni::NativeLease<webrtc::EncodedAudioFrameInjectorInterface> injector(env, caller);
Expand All @@ -75,9 +87,15 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injec
return;
}

const std::vector<uint32_t> csrcs = toCsrcs(env, jContributingSources);

if (env->ExceptionCheck()) {
return;
}

// The frame copies the data, so the caller may reuse the buffer.
injectFrame(injector, std::span<const uint8_t>(address + position, static_cast<size_t>(length)),
payloadType, rtpTimestamp, audioLevel, hasAudioLevel, captureTimeMs, hasCaptureTime);
payloadType, rtpTimestamp, audioLevel, hasAudioLevel, captureTimeMs, hasCaptureTime, csrcs);
}
catch (...) {
ThrowCxxJavaException(env);
Expand All @@ -87,13 +105,20 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injec

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injectByteArray
(JNIEnv * env, jobject caller, jbyteArray jData, jint payloadType, jlong rtpTimestamp,
jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime)
jint audioLevel, jboolean hasAudioLevel, jlong captureTimeMs, jboolean hasCaptureTime,
jintArray jContributingSources)
{
jni::CallFromJava(env, [&]() {
jni::NativeLease<webrtc::EncodedAudioFrameInjectorInterface> injector(env, caller);
CHECK_HANDLE(injector);

try {
const std::vector<uint32_t> csrcs = toCsrcs(env, jContributingSources);

if (env->ExceptionCheck()) {
return;
}

auto releaseArray = [env, jData](jbyte * bytes) {
env->ReleaseByteArrayElements(jData, bytes, JNI_ABORT);
};
Expand All @@ -106,7 +131,8 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCEncodedAudioFrameInjector_injec
auto data = std::span<const uint8_t>(reinterpret_cast<const uint8_t *>(bytes.get()),
static_cast<size_t>(env->GetArrayLength(jData)));

injectFrame(injector, data, payloadType, rtpTimestamp, audioLevel, hasAudioLevel, captureTimeMs, hasCaptureTime);
injectFrame(injector, data, payloadType, rtpTimestamp, audioLevel, hasAudioLevel, captureTimeMs,
hasCaptureTime, csrcs);
}
catch (...) {
ThrowCxxJavaException(env);
Expand Down
40 changes: 14 additions & 26 deletions webrtc-jni/src/main/cpp/src/api/RTCRtpContributingSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,34 @@
*/

#include "api/RTCRtpContributingSource.h"
#include "JavaObject.h"
#include "JavaRef.h"
#include "JavaUtils.h"
#include "JNI_WebRTC.h"

#include <cmath>

namespace jni
{
namespace RTCRtpContributingSource
{
jdouble toLinearAudioLevel(const std::optional<uint8_t> & audioLevel)
{
// RFC 6464 reserves 127 for silence, which the formula of the WebRTC
// specification would not reach.
if (!audioLevel.has_value() || *audioLevel >= 127) {
return 0;
}

return std::pow(10.0, -static_cast<double>(*audioLevel) / 20.0);
}

JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpSource & source)
{
const auto javaClass = JavaClasses::get<JavaRTCRtpContributingSourceClass>(env);

jlong timestamp = static_cast<jlong>(source.timestamp().us());
jlong sourceId = static_cast<jlong>(source.source_id());
jdouble audioLevel = static_cast<jdouble>(source.audio_level().value_or(0));
jdouble audioLevel = toLinearAudioLevel(source.audio_level());
jlong rtpTimestamp = static_cast<jlong>(source.rtp_timestamp());

jobject object = env->NewObject(javaClass->cls, javaClass->ctor,
Expand All @@ -39,35 +51,11 @@ namespace jni
return JavaLocalRef<jobject>(env, object);
}

webrtc::RtpSource toNative(JNIEnv * env, const JavaRef<jobject> & source)
{
const auto javaClass = JavaClasses::get<JavaRTCRtpContributingSourceClass>(env);

JavaObject obj(env, source);

webrtc::RtpSource::Extensions extensions;
extensions.audio_level = static_cast<uint8_t>(obj.getDouble(javaClass->audioLevel));

return webrtc::RtpSource(
webrtc::Timestamp::Micros(static_cast<int64_t>(obj.getLong(javaClass->timestamp))),
static_cast<uint32_t>(obj.getLong(javaClass->sourceId)),
webrtc::RtpSourceType::CSRC,
static_cast<uint32_t>(obj.getLong(javaClass->rtpTimestamp)),
extensions
);
}

JavaRTCRtpContributingSourceClass::JavaRTCRtpContributingSourceClass(JNIEnv * env)
{
cls = FindClass(env, PKG"RTCRtpContributingSource");

ctor = GetMethod(env, cls, "<init>", "(JJDJ)V");

timestamp = GetFieldID(env, cls, "timestamp", "J");
sourceId = GetFieldID(env, cls, "sourceId", "J");
sourceType = GetFieldID(env, cls, "sourceType", "J");
audioLevel = GetFieldID(env, cls, "audioLevel", "D");
rtpTimestamp = GetFieldID(env, cls, "rtpTimestamp", "J");
}
}
}
24 changes: 1 addition & 23 deletions webrtc-jni/src/main/cpp/src/api/RTCRtpSynchronizationSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "api/RTCRtpSynchronizationSource.h"
#include "api/RTCRtpContributingSource.h"
#include "JavaObject.h"
#include "JavaRef.h"
#include "JavaUtils.h"
#include "JNI_WebRTC.h"
Expand All @@ -31,7 +30,7 @@ namespace jni

jlong timestamp = static_cast<jlong>(source.timestamp().us());
jlong sourceId = static_cast<jlong>(source.source_id());
jdouble audioLevel = static_cast<jdouble>(source.audio_level().value_or(0));
jdouble audioLevel = RTCRtpContributingSource::toLinearAudioLevel(source.audio_level());
jlong rtpTimestamp = static_cast<jlong>(source.rtp_timestamp());
jboolean voiceActivityFlag = false;

Expand All @@ -41,32 +40,11 @@ namespace jni
return JavaLocalRef<jobject>(env, object);
}

webrtc::RtpSource toNative(JNIEnv * env, const JavaRef<jobject> & source)
{
const auto javaClass = JavaClasses::get<JavaRTCRtpSynchronizationSourceClass>(env);
const auto parentClass = JavaClasses::get<RTCRtpContributingSource::JavaRTCRtpContributingSourceClass>(env);

JavaObject obj(env, source);

webrtc::RtpSource::Extensions extensions;
extensions.audio_level = static_cast<uint8_t>(obj.getDouble(parentClass->audioLevel));

return webrtc::RtpSource(
webrtc::Timestamp::Micros(static_cast<int64_t>(obj.getLong(parentClass->timestamp))),
static_cast<uint32_t>(obj.getLong(parentClass->sourceId)),
webrtc::RtpSourceType::SSRC,
static_cast<uint32_t>(obj.getLong(parentClass->rtpTimestamp)),
extensions
);
}

JavaRTCRtpSynchronizationSourceClass::JavaRTCRtpSynchronizationSourceClass(JNIEnv * env)
{
cls = FindClass(env, PKG"RTCRtpSynchronizationSource");

ctor = GetMethod(env, cls, "<init>", "(JJDJZ)V");

voiceActivityFlag = GetFieldID(env, cls, "voiceActivityFlag", "Z");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
public class RTCEncodedAudioFrameInjector extends DisposableNativeObject {

private static final int[] NO_SOURCES = new int[0];

/*
* The native observer set through this object, owned by it and freed when
* disposed.
Expand Down Expand Up @@ -87,6 +89,19 @@ public void injectFrame(ByteBuffer data, int payloadType, long rtpTimestamp) {
*/
public void injectFrame(ByteBuffer data, int payloadType, long rtpTimestamp, Integer audioLevel,
Long captureTimeMs) {
injectFrame(data, payloadType, rtpTimestamp, audioLevel, captureTimeMs, NO_SOURCES);
}

/**
* Sends an encoded frame like {@link #injectFrame(ByteBuffer, int, long,
* Integer, Long)}, naming the sources mixed into it. Nothing but a test can
* produce a received CSRC without this.
*
* @param contributingSources The CSRCs of the frame, at most 15, as
* unsigned 32-bit values.
*/
void injectFrame(ByteBuffer data, int payloadType, long rtpTimestamp, Integer audioLevel,
Long captureTimeMs, int[] contributingSources) {
Objects.requireNonNull(data, "data");

if (payloadType < 0 || payloadType > 127) {
Expand All @@ -104,11 +119,11 @@ public void injectFrame(ByteBuffer data, int payloadType, long rtpTimestamp, Int

if (data.isDirect()) {
injectDirectBuffer(data, data.position(), data.remaining(), payloadType, rtpTimestamp,
level, audioLevel != null, captureTime, captureTimeMs != null);
level, audioLevel != null, captureTime, captureTimeMs != null, contributingSources);
}
else {
injectByteArray(RTCDataChannel.copyWindow(data), payloadType, rtpTimestamp,
level, audioLevel != null, captureTime, captureTimeMs != null);
level, audioLevel != null, captureTime, captureTimeMs != null, contributingSources);
}
}

Expand All @@ -121,9 +136,11 @@ public void injectFrame(ByteBuffer data, int payloadType, long rtpTimestamp, Int
public native void dispose();

private native void injectDirectBuffer(ByteBuffer data, int position, int length, int payloadType,
long rtpTimestamp, int audioLevel, boolean hasAudioLevel, long captureTimeMs, boolean hasCaptureTime);
long rtpTimestamp, int audioLevel, boolean hasAudioLevel, long captureTimeMs, boolean hasCaptureTime,
int[] contributingSources);

private native void injectByteArray(byte[] data, int payloadType, long rtpTimestamp,
int audioLevel, boolean hasAudioLevel, long captureTimeMs, boolean hasCaptureTime);
int audioLevel, boolean hasAudioLevel, long captureTimeMs, boolean hasCaptureTime,
int[] contributingSources);

}
Loading
Loading