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/media/media-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ player.play();
::: info
The player takes over the reader it is given. That reader must not be used or closed afterwards; closing the player releases it.

It also keeps the native side of the media sources it feeds alive until it is closed, so disposing of a source or its track while the player runs is safe.
It also keeps the native side of the media sources it feeds alive until it is closed, so disposing of a source or its track while the player runs is safe. A source that was already disposed is rejected with `IllegalStateException` before the reader is taken over; the caller can still use or close that reader. Source acquisition is protected against concurrent disposal: the constructor either acquires its own reference or rejects the disposed source.
:::

## Decoding in Hardware
Expand Down
32 changes: 32 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_NativeApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ extern "C" {
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_releaseFrames
(JNIEnv *, jclass, jlong);

/*
* Class: dev_onvoid_webrtc_internal_NativeApi
* Method: retainVideoSource
* Signature: (Ldev/onvoid/webrtc/media/video/CustomVideoSource;)J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_retainVideoSource
(JNIEnv *, jclass, jobject);

/*
* Class: dev_onvoid_webrtc_internal_NativeApi
* Method: releaseVideoSource
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_releaseVideoSource
(JNIEnv *, jclass, jlong);

/*
* Class: dev_onvoid_webrtc_internal_NativeApi
* Method: retainAudioSource
* Signature: (Ldev/onvoid/webrtc/media/audio/CustomAudioSource;)J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_retainAudioSource
(JNIEnv *, jclass, jobject);

/*
* Class: dev_onvoid_webrtc_internal_NativeApi
* Method: releaseAudioSource
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_releaseAudioSource
(JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions webrtc-jni/src/main/cpp/include/webrtc_java_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
*
* 1. Java side: dev.onvoid.webrtc.internal.NativeApi.tableAddress() returns
* the address of a "struct webrtc_java_api" owned by this library, and
* NativeApi.handleOf(source) returns the native handle of a
* CustomVideoSource or CustomAudioSource.
* NativeApi.retainVideoSource(source) or retainAudioSource(source)
* returns a retained handle of a CustomVideoSource or CustomAudioSource.
* Release that reference with the matching source_release() when done.
* 2. The extension receives both as jlong values and casts them back.
*
* There is no link-time dependency between the two native libraries, so the
Expand Down
54 changes: 54 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_NativeApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "JNI_NativeApi.h"
#include "JavaBoundary.h"
#include "NativeHandle.h"
#include "media/audio/CustomAudioSource.h"
#include "media/video/CustomVideoSource.h"

#include "api/ExtensionApi.h"
#include "api/EncodedFrameTransformer.h"
Expand All @@ -38,6 +41,57 @@ JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_tableAddress
});
}

JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_retainVideoSource
(JNIEnv * env, jclass caller, jobject javaSource)
{
return jni::CallFromJava(env, [&]() -> jlong {
if (javaSource == nullptr) {
return 0;
}

jni::NativeLease<jni::CustomVideoSource> source(env, javaSource);
CHECK_HANDLEV(source, 0);

// AddRef while the lease still defers any concurrent dispose.
source->AddRef();

return reinterpret_cast<jlong>(source.get());
});
}

JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_retainAudioSource
(JNIEnv * env, jclass caller, jobject javaSource)
{
return jni::CallFromJava(env, [&]() -> jlong {
if (javaSource == nullptr) {
return 0;
}

jni::NativeLease<jni::CustomAudioSource> source(env, javaSource);
CHECK_HANDLEV(source, 0);

source->AddRef();

return reinterpret_cast<jlong>(source.get());
});
}

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

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

JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_internal_NativeApi_version
(JNIEnv * env, jclass caller)
{
Expand Down
60 changes: 60 additions & 0 deletions webrtc-media/src/main/cpp/include/JNI_DecoderFault.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2026 Alex Andres
*
* 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.
*/

#include <jni.h>
/* Header for class dev_onvoid_webrtc_media_player_DecoderFault */

#ifndef _Included_dev_onvoid_webrtc_media_player_DecoderFault
#define _Included_dev_onvoid_webrtc_media_player_DecoderFault
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: dev_onvoid_webrtc_media_player_DecoderFault
* Method: arm
* Signature: (ZI)V
*/
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_arm
(JNIEnv *, jclass, jboolean, jint);

/*
* Class: dev_onvoid_webrtc_media_player_DecoderFault
* Method: disarm
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_disarm
(JNIEnv *, jclass);

/*
* Class: dev_onvoid_webrtc_media_player_DecoderFault
* Method: retries
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_retries
(JNIEnv *, jclass);

/*
* Class: dev_onvoid_webrtc_media_player_DecoderFault
* Method: observerLifecycle
* Signature: (Ljava/lang/String;J)Z
*/
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_observerLifecycle
(JNIEnv *, jclass, jstring, jlong);

#ifdef __cplusplus
}
#endif
#endif
8 changes: 8 additions & 0 deletions webrtc-media/src/main/cpp/include/JNI_PlaybackProbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ extern "C" {
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_waitingPushes
(JNIEnv *, jclass);

/*
* Class: dev_onvoid_webrtc_media_player_PlaybackProbe
* Method: waitingPlayers
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_waitingPlayers
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
Expand Down
42 changes: 42 additions & 0 deletions webrtc-media/src/main/cpp/include/media/DecoderFault.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2026 Alex Andres
*
* 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.
*/

#ifndef WEBRTC_JAVA_MEDIA_DECODER_FAULT_H_
#define WEBRTC_JAVA_MEDIA_DECODER_FAULT_H_

extern "C" {
#include <libavcodec/packet.h>
}

namespace ffmpeg
{
// Test-only JNI callers arm one fault for one stream at a time. Disarm
// only after the player has closed. Normal playback pays an atomic load.
class DecoderFault
{
public:
// 1: reject a packet once with EAGAIN; 2: fail drain send;
// 3: fail receiving buffered output after the drain request;
// 4: reject the drain request once with EAGAIN.
static void Arm(bool video, int mode);
static void Disarm();
static int SendError(bool video, const AVPacket * packet);
static int ReceiveError(bool video);
static int Retries();
};
}

#endif
8 changes: 6 additions & 2 deletions webrtc-media/src/main/cpp/include/media/MediaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ namespace ffmpeg
bool PumpOnce(AVPacket * packet, bool * end_of_stream);

// Pushes everything the decoders still hold, which is what the end
// of a stream and a loop both need.
void DrainDecoders();
// of a stream and a loop both need. Returns false on a reported error.
bool DrainDecoders();

// Keeps an unaccepted packet alive while output is received, then
// retries it. A null packet drains the selected decoder.
bool SendToDecoder(bool video, const AVPacket * packet, bool at_end);

// Takes the decoded video and hands it to the pacer. At the end
// of the stream a seek that landed too late is not retried: the
Expand Down
8 changes: 7 additions & 1 deletion webrtc-media/src/main/cpp/include/media/PlaybackProbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ffmpeg
// once, and a thread held up by a full queue looks like one that is done.
//
// Only the tests read it, through JNI_PlaybackProbe, and it costs a player
// a single atomic update per seek and per wait for room in a queue. The
// an atomic update per seek, command wait and wait for room in a queue. The
// counts are those of all players, which tests one at a time can tell
// apart.
class PlaybackProbe
Expand All @@ -52,6 +52,12 @@ namespace ffmpeg

// How many pushes are waiting for room in a queue right now.
static int WaitingPushes();

// Counts decode threads waiting for a playback command, rather
// than stuck in decoding or waiting for the pacer at EOF.
static void BeginCommandWait();
static void EndCommandWait();
static int WaitingPlayers();
};
}

Expand Down
87 changes: 87 additions & 0 deletions webrtc-media/src/main/cpp/src/JNI_DecoderFault.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2026 Alex Andres
*
* 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.
*/

#include "JNI_DecoderFault.h"
#include "media/DecoderFault.h"
#include "media/JniBoundary.h"
#include "media/MediaPlayer.h"

namespace
{
class CountingObserver : public ffmpeg::MediaPlayerObserver
{
public:
void OnThreadStart() override { ++starts; }
void OnThreadStop() override { ++stops; }
void OnStateChanged(int state) override
{
ordered = ordered && starts == 1 && stops == 0;
last_state = state;
}
void OnEndOfStream() override {}
void OnError(const std::string &) override {}
int starts = 0;
int stops = 0;
int last_state = ffmpeg::kIdle;
bool ordered = true;
};
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_arm
(JNIEnv *, jclass, jboolean video, jint mode)
{
ffmpeg::DecoderFault::Arm(video == JNI_TRUE, mode);
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_disarm
(JNIEnv *, jclass)
{
ffmpeg::DecoderFault::Disarm();
}

JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_retries
(JNIEnv *, jclass)
{
return ffmpeg::DecoderFault::Retries();
}

JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_player_DecoderFault_observerLifecycle
(JNIEnv * env, jclass, jstring source, jlong api)
{
return ffmpeg::CallFromJava<jboolean>(env, JNI_FALSE, [&]() -> jboolean {
ffmpeg::JavaStringChars chars(env, source);
if (chars.get() == nullptr) {
return JNI_FALSE;
}
auto reader = std::make_unique<ffmpeg::MediaReader>();
if (reader->Open(chars.utf8()) < 0) {
return JNI_FALSE;
}
// Native observers have no JVM detach flag to hide a double stop.
ffmpeg::MediaPlayer player(std::move(reader),
reinterpret_cast<const webrtc_java_api *>(api), nullptr, nullptr);
auto observer = std::make_unique<CountingObserver>();
auto * counts = observer.get();
player.SetObserver(std::move(observer));
if (player.Initialize() < 0) {
return JNI_FALSE;
}
player.Close();
player.Close();
return counts->starts == 1 && counts->stops == 1 && counts->ordered
&& counts->last_state == ffmpeg::kClosed ? JNI_TRUE : JNI_FALSE;
});
}
6 changes: 6 additions & 0 deletions webrtc-media/src/main/cpp/src/JNI_PlaybackProbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_waiting
{
return static_cast<jint>(ffmpeg::PlaybackProbe::WaitingPushes());
}

JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_waitingPlayers
(JNIEnv * env, jclass caller)
{
return static_cast<jint>(ffmpeg::PlaybackProbe::WaitingPlayers());
}
Loading
Loading