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
2 changes: 1 addition & 1 deletion docs/guide/advanced/encoded-transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ receiver.setTransform(frame -> {
});
```

`setData` takes a `ByteBuffer` (its remaining bytes) or a `byte[]`, and the new payload may be larger or smaller than the old one. The payload is only copied into Java when `getData()` is called, so a transform that looks at the metadata alone costs no copy at all.
`setData` takes a `ByteBuffer` (its remaining bytes) or a `byte[]`, and the new payload may be larger or smaller than the old one. The payload is only copied into Java when `getData()` is called, so a transform that looks at the metadata alone costs no copy at all. Each transform thread reuses at most 256 KiB of direct scratch memory. Larger payloads use a temporary buffer sized to the frame, released for garbage collection when the transform returns. Workloads with frequent larger frames therefore allocate a buffer for each of them.

A frame, and the buffer `getData()` returned, is valid only while `transform` runs and only on its thread. The buffer's memory is reused for the next frame, so copy what you need to keep. Reading or changing a frame after the transform returned throws an `IllegalStateException`; its metadata stays readable.

Expand Down
4 changes: 3 additions & 1 deletion docs/guide/audio/audio-processing.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ With this approach, the WebRTC framework automatically applies the audio process

For more control, you can manually process audio streams:

Sample rates in `AudioProcessingStreamConfig` must be positive and divisible by 100 so each 10 ms frame contains a whole number of samples. `AudioConverter` and `AudioResampler` require rates from 100 to 384000 Hz, also divisible by 100; for example, 44100 Hz is accepted and 48099 Hz is rejected.

```java
import dev.onvoid.webrtc.media.audio.AudioProcessing;
import dev.onvoid.webrtc.media.audio.AudioProcessingConfig;
Expand Down Expand Up @@ -354,4 +356,4 @@ These statistics are particularly useful for monitoring the performance of echo

The `AudioProcessing` class provides powerful audio processing capabilities for real-time communications. By properly configuring and using these features, you can significantly improve the audio quality in your applications.

Remember that audio processing is CPU-intensive, so consider the performance implications when enabling multiple features, especially on resource-constrained devices.
Remember that audio processing is CPU-intensive, so consider the performance implications when enabling multiple features, especially on resource-constrained devices.
2 changes: 1 addition & 1 deletion docs/guide/audio/custom-audio-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The call checks its arguments and throws `IllegalArgumentException` rather than
- Samples must be **16-bit** signed PCM in the platform byte order, with the channels interleaved.
- The array must hold at least `frameCount * channels * 2` bytes. A longer array is fine; only that many bytes are read.
- One call carries one chunk of exactly **10 ms**, so `frameCount` must be `sampleRate / 100`. Split audio of any other length, such as 20 ms buffers, into 10 ms chunks first, or let the [client API](/guide/client-api) do it.
- The sample rate must lie between 8000 and 192000 Hz, and the channel count must be 1, 2, 4, 6 or 8.
- The sample rate must lie between 8000 and 192000 Hz and be divisible by 100, and the channel count must be 1, 2, 4, 6 or 8.
- A chunk may hold at most `CustomAudioSource.MAX_SAMPLES_PER_PUSH` samples counting every channel, which rules out the highest rates with many channels: 96 kHz with 8 channels or 192 kHz with 4 is the limit.
- `frameCount` counts frames, not samples and not bytes. A frame holds one sample per channel, so 10 ms at 48 kHz is 480 frames whether it is mono or stereo.

Expand Down
2 changes: 2 additions & 0 deletions docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,8 @@ DataChannel positions = peer.createChannel("positions", ChannelOptions.builder()
.build());
```

`maxRetransmits(int)` accepts 0 to 65535 retransmissions. `maxPacketLifeTime(Duration)` accepts nonnegative durations up to 65535 milliseconds; positive fractions of a millisecond are rounded down, so a duration below one millisecond becomes zero. The two limits exclude each other. Values outside these ranges are rejected before native processing.

`negotiatedId(int)` creates a channel both peers set up themselves with the same id, without announcing it; such a channel never shows up in `onChannel`. The [Data Channels](/guide/data/data-channels) guide explains the options in detail.

## WHIP and WHEP
Expand Down
5 changes: 4 additions & 1 deletion webrtc-jni/src/main/cpp/include/AllocationFault.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace jni
// code around it does when memory runs out, which it does not on demand.
//
// Only the tests arm it, through JNI_AllocationFault. A check that is not
// armed pays a single atomic load.
// armed pays a single flag load.
class AllocationFault
{
public:
Expand All @@ -33,6 +33,9 @@ namespace jni
// Fails the next encoded_observer_add() of the extension API, once,
// as an allocation for its handle does.
static void FailNextEncodedObserver();
// Fails the next handle lease, as inserting its map entry can.
static void FailNextNativeLease();
static void CheckNativeLease();
// Forgets a fault that has not fired.
static void Disarm();
// Throws std::bad_alloc if a fault is armed, and disarms it.
Expand Down
3 changes: 3 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_AllocationFault.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ extern "C" {
* Method: failNextListener
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_failNextNativeLease
(JNIEnv *, jclass);

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_failNextListener
(JNIEnv *, jclass);

Expand Down
16 changes: 16 additions & 0 deletions webrtc-jni/src/main/cpp/src/AllocationFault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace
{
// A lease failure belongs to the Java thread that armed it.
thread_local bool fail_native_lease = false;
std::atomic<bool> fail_listener{ false };
std::atomic<bool> fail_encoded_observer{ false };
}
Expand All @@ -37,8 +39,22 @@ namespace jni
fail_encoded_observer.store(true);
}

void AllocationFault::FailNextNativeLease()
{
fail_native_lease = true;
}

void AllocationFault::CheckNativeLease()
{
if (fail_native_lease) {
fail_native_lease = false;
throw std::bad_alloc();
}
}

void AllocationFault::Disarm()
{
fail_native_lease = false;
fail_listener.store(false);
fail_encoded_observer.store(false);
}
Expand Down
33 changes: 27 additions & 6 deletions webrtc-jni/src/main/cpp/src/JNI_AllocationFault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "JNI_AllocationFault.h"
#include "JavaBoundary.h"
#include "AllocationFault.h"

#include "api/EncodedFrameTransformer.h"
Expand All @@ -30,38 +31,58 @@ namespace
// The Java class this belongs to exists in the tests only, so that nothing but
// them can make an allocation fail.

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_failNextNativeLease
(JNIEnv * env, jclass caller)
{
jni::CallFromJava(env, [&]() {
jni::AllocationFault::FailNextNativeLease();
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_failNextListener
(JNIEnv * env, jclass caller)
{
jni::AllocationFault::FailNextListener();
jni::CallFromJava(env, [&]() {
jni::AllocationFault::FailNextListener();
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_failNextEncodedObserver
(JNIEnv * env, jclass caller)
{
jni::AllocationFault::FailNextEncodedObserver();
jni::CallFromJava(env, [&]() {
jni::AllocationFault::FailNextEncodedObserver();
});
}

JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_addEncodedObserver
(JNIEnv * env, jclass caller, jlong frames)
{
return reinterpret_cast<jlong>(jni::GetExtensionApi()->encoded_observer_add(reinterpret_cast<void *>(frames), &IgnoreFrame, nullptr));
return jni::CallFromJava(env, [&]() -> jlong {
return reinterpret_cast<jlong>(jni::GetExtensionApi()->encoded_observer_add(reinterpret_cast<void *>(frames), &IgnoreFrame, nullptr));
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_removeEncodedObserver
(JNIEnv * env, jclass caller, jlong observer)
{
jni::GetExtensionApi()->encoded_observer_remove(reinterpret_cast<void *>(observer));
jni::CallFromJava(env, [&]() {
jni::GetExtensionApi()->encoded_observer_remove(reinterpret_cast<void *>(observer));
});
}

JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_encodedObservers
(JNIEnv * env, jclass caller, jlong frames)
{
return static_cast<jint>(reinterpret_cast<jni::EncodedFrameTransformer *>(frames)->GetObserverCount());
return jni::CallFromJava(env, [&]() -> jint {
return static_cast<jint>(reinterpret_cast<jni::EncodedFrameTransformer *>(frames)->GetObserverCount());
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_AllocationFault_disarm
(JNIEnv * env, jclass caller)
{
jni::AllocationFault::Disarm();
jni::CallFromJava(env, [&]() {
jni::AllocationFault::Disarm();
});
}
28 changes: 15 additions & 13 deletions webrtc-jni/src/main/cpp/src/JNI_AudioConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,21 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_dispose
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_initialize
(JNIEnv * env, jobject caller, jint srcSampleRate, jint srcChannels, jint dstSampleRate, jint dstChannels)
{
// 10 ms frames
size_t srcFrames = webrtc::AudioProcessing::GetFrameSize(srcSampleRate);
size_t dstFrames = webrtc::AudioProcessing::GetFrameSize(dstSampleRate);
jni::CallFromJava(env, [&]() {
// 10 ms frames
size_t srcFrames = webrtc::AudioProcessing::GetFrameSize(srcSampleRate);
size_t dstFrames = webrtc::AudioProcessing::GetFrameSize(dstSampleRate);

try {
jni::AudioConverter * converter = jni::AudioConverter::create(srcFrames, srcChannels, dstFrames, dstChannels).release();
try {
jni::AudioConverter * converter = jni::AudioConverter::create(srcFrames, srcChannels, dstFrames, dstChannels).release();

SetHandle(env, caller, converter);
}
catch (const std::invalid_argument & e) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
SetHandle(env, caller, converter);
}
catch (const std::invalid_argument & e) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
});
}
19 changes: 11 additions & 8 deletions webrtc-jni/src/main/cpp/src/JNI_BuiltinAudioDecoderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "JNI_BuiltinAudioDecoderFactory.h"
#include "JavaBoundary.h"
#include "media/audio/codec/AudioCodecInfo.h"
#include "JavaUtils.h"

Expand All @@ -23,14 +24,16 @@
JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_audio_codec_BuiltinAudioDecoderFactory_getBuiltinCodecs
(JNIEnv * env, jclass caller)
{
try {
auto factory = webrtc::CreateBuiltinAudioDecoderFactory();
return jni::CallFromJava(env, [&]() -> jobjectArray {
try {
auto factory = webrtc::CreateBuiltinAudioDecoderFactory();

return jni::AudioCodecInfo::toJavaArray(env, factory->GetSupportedDecoders()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}
return jni::AudioCodecInfo::toJavaArray(env, factory->GetSupportedDecoders()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}

return nullptr;
return nullptr;
});
}
19 changes: 11 additions & 8 deletions webrtc-jni/src/main/cpp/src/JNI_BuiltinAudioEncoderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "JNI_BuiltinAudioEncoderFactory.h"
#include "JavaBoundary.h"
#include "media/audio/codec/AudioCodecInfo.h"
#include "JavaUtils.h"

Expand All @@ -23,14 +24,16 @@
JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_audio_codec_BuiltinAudioEncoderFactory_getBuiltinCodecs
(JNIEnv * env, jclass caller)
{
try {
auto factory = webrtc::CreateBuiltinAudioEncoderFactory();
return jni::CallFromJava(env, [&]() -> jobjectArray {
try {
auto factory = webrtc::CreateBuiltinAudioEncoderFactory();

return jni::AudioCodecInfo::toJavaArray(env, factory->GetSupportedEncoders()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}
return jni::AudioCodecInfo::toJavaArray(env, factory->GetSupportedEncoders()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}

return nullptr;
return nullptr;
});
}
19 changes: 11 additions & 8 deletions webrtc-jni/src/main/cpp/src/JNI_DefaultVideoDecoderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
*/

#include "JNI_DefaultVideoDecoderFactory.h"
#include "JavaBoundary.h"
#include "media/video/codec/DefaultVideoCodecFactories.h"
#include "media/video/codec/VideoCodecInfo.h"
#include "JavaUtils.h"

JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_video_codec_DefaultVideoDecoderFactory_getSupportedCodecsInternal
(JNIEnv * env, jclass caller)
{
try {
auto factory = jni::CreateDefaultVideoDecoderFactory();
return jni::CallFromJava(env, [&]() -> jobjectArray {
try {
auto factory = jni::CreateDefaultVideoDecoderFactory();

return jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}
return jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}

return nullptr;
return nullptr;
});
}
19 changes: 11 additions & 8 deletions webrtc-jni/src/main/cpp/src/JNI_DefaultVideoEncoderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
*/

#include "JNI_DefaultVideoEncoderFactory.h"
#include "JavaBoundary.h"
#include "media/video/codec/DefaultVideoCodecFactories.h"
#include "media/video/codec/VideoCodecInfo.h"
#include "JavaUtils.h"

JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_video_codec_DefaultVideoEncoderFactory_getSupportedCodecsInternal
(JNIEnv * env, jclass caller)
{
try {
auto factory = jni::CreateDefaultVideoEncoderFactory();
return jni::CallFromJava(env, [&]() -> jobjectArray {
try {
auto factory = jni::CreateDefaultVideoEncoderFactory();

return jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}
return jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats()).release();
}
catch (...) {
ThrowCxxJavaException(env);
}

return nullptr;
return nullptr;
});
}
35 changes: 19 additions & 16 deletions webrtc-jni/src/main/cpp/src/JNI_HardwareVideoDecoderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,36 @@
*/

#include "JNI_HardwareVideoDecoderFactory.h"
#include "JavaBoundary.h"
#include "media/video/codec/DefaultVideoCodecFactories.h"
#include "media/video/codec/VideoCodecInfo.h"
#include "JavaUtils.h"

JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory_getCodecsInternal
(JNIEnv * env, jclass caller)
{
try {
// One factory for both lists, as making one probes the hardware.
auto factory = jni::CreateHardwareVideoDecoderFactory();
return jni::CallFromJava(env, [&]() -> jobjectArray {
try {
// One factory for both lists, as making one probes the hardware.
auto factory = jni::CreateHardwareVideoDecoderFactory();

auto supported = jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats());
auto hardware = jni::VideoCodecInfo::toJavaArray(env, jni::GetHardwareVideoDecoderFormats(*factory));
auto arrayClass = jni::JavaLocalRef<jclass>(env, env->GetObjectClass(supported.get()));
auto supported = jni::VideoCodecInfo::toJavaArray(env, factory->GetSupportedFormats());
auto hardware = jni::VideoCodecInfo::toJavaArray(env, jni::GetHardwareVideoDecoderFormats(*factory));
auto arrayClass = jni::JavaLocalRef<jclass>(env, env->GetObjectClass(supported.get()));

jobjectArray codecs = env->NewObjectArray(2, arrayClass.get(), nullptr);
jobjectArray codecs = env->NewObjectArray(2, arrayClass.get(), nullptr);

ExceptionCheck(env);
ExceptionCheck(env);

env->SetObjectArrayElement(codecs, 0, supported.get());
env->SetObjectArrayElement(codecs, 1, hardware.get());
env->SetObjectArrayElement(codecs, 0, supported.get());
env->SetObjectArrayElement(codecs, 1, hardware.get());

return codecs;
}
catch (...) {
ThrowCxxJavaException(env);
}
return codecs;
}
catch (...) {
ThrowCxxJavaException(env);
}

return nullptr;
return nullptr;
});
}
Loading
Loading