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: 2 additions & 0 deletions docs/guide/media/media-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ The FFmpeg build is deliberately small, and carries only what this module plays:

Audio of any rate or layout is resampled to what WebRTC takes, which is 48 kHz 16-bit PCM in mono or stereo. Video that decodes to I420 — almost all 8-bit H.264, VP8, VP9 and MPEG-4 — reaches the encoder without being copied; anything else is converted first.

Video retains its color matrix, full or limited range, color primaries and transfer function through conversion and into the native WebRTC frame, including hardware readback. Missing matrix/range metadata defaults to BT.601 limited range; RGB and JPEG pixel formats imply full range. RGB is converted to BT.601 YUV. The negotiated encoder and receiver determine how this information is carried and displayed.

Sources can be local files, or live streams over RTSP (`rtsp://`), with RTP over UDP or TCP. Protocols that need TLS, such as `rtsps://` and `https://`, are not part of the build, and neither are HLS, DASH and RTMP. A source cannot reach any other protocol either, even one FFmpeg uses internally, so a URL passed on from a user cannot make the module fetch something else.

## Licensing
Expand Down
22 changes: 22 additions & 0 deletions webrtc-jni/src/main/cpp/include/webrtc_java_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ struct wj_i420_frame {
void * opaque;
};

/**
* Color description using ITU-T H.273 identifiers for primaries, transfer and
* matrix (2 means unspecified). Range: 0 unspecified, 1 limited, 2 full.
* Unknown identifiers are treated as unspecified by the receiving library.
*/
struct wj_color_space {
uint8_t primaries;
uint8_t transfer;
uint8_t matrix;
uint8_t range;
};

/**
* One chunk of decoded audio: interleaved signed 16-bit PCM in host byte
* order, exactly 10 ms long, which is the only chunk length WebRTC accepts.
Expand Down Expand Up @@ -303,6 +315,16 @@ struct webrtc_java_api {
* handle must not be used afterwards.
*/
void (*encoded_frames_release)(void * frames);

/**
* Like video_source_push, with a color description for the I420 pixels.
* A null color description has the same behavior as video_source_push.
* Ownership and error handling are identical. Appended in interface v1;
* callers must check size before accessing this member.
*/
int (*video_source_push_color)(void * source, const struct wj_i420_frame * frame,
const struct wj_color_space * color);

};

#ifdef __cplusplus
Expand Down
77 changes: 77 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_ColorProbe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.
*/

// The Java class exists only in media tests. Observe the actual WebRTC frame
// after the extension table and CustomVideoSource have handled it.
#include <jni.h>
#include "JavaBoundary.h"
#include "api/ExtensionApi.h"
#include "media/video/CustomVideoSource.h"
#include "api/make_ref_counted.h"
#include <cstring>

namespace {
class ColorSink : public webrtc::VideoSinkInterface<webrtc::VideoFrame> {
public:
jint values[7] = { -1, -1, -1, -1, -1, -1, -1 };
void OnFrame(const webrtc::VideoFrame & frame) override {
auto pixels = frame.video_frame_buffer()->ToI420();
values[0] = pixels->DataY()[0];
values[1] = pixels->DataU()[0];
values[2] = pixels->DataV()[0];
if (frame.color_space()) {
const auto & color = *frame.color_space();
values[3] = static_cast<int>(color.primaries());
values[4] = static_cast<int>(color.transfer());
values[5] = static_cast<int>(color.matrix());
values[6] = static_cast<int>(color.range());
}
}
};
}

extern "C" JNIEXPORT jintArray JNICALL Java_dev_onvoid_webrtc_media_player_ColorProbe_push
(JNIEnv * env, jclass, jintArray description, jboolean legacy)
{
return jni::CallFromJava(env, [&]() -> jintArray {
if (description == nullptr || env->GetArrayLength(description) != 7) return nullptr;
jint data[7];
env->GetIntArrayRegion(description, 0, 7, data);
if (env->ExceptionCheck()) return nullptr;
auto source = webrtc::make_ref_counted<jni::CustomVideoSource>(std::make_shared<jni::SyncClock>());
ColorSink sink;
auto * videoSource = static_cast<webrtc::VideoSourceInterface<webrtc::VideoFrame> *>(source.get());
videoSource->AddOrUpdateSink(&sink, webrtc::VideoSinkWants());
uint8_t y[32 * 32], u[16 * 16], v[16 * 16];
std::memset(y, data[0], sizeof(y));
std::memset(u, data[1], sizeof(u));
std::memset(v, data[2], sizeof(v));
wj_i420_frame frame = {};
frame.width = frame.height = 32;
frame.y = y; frame.u = u; frame.v = v;
frame.stride_y = 32; frame.stride_u = frame.stride_v = 16;
wj_color_space color = { static_cast<uint8_t>(data[3]), static_cast<uint8_t>(data[4]),
static_cast<uint8_t>(data[5]), static_cast<uint8_t>(data[6]) };
const auto * api = jni::GetExtensionApi();
int status = legacy ? api->video_source_push(source.get(), &frame)
: api->video_source_push_color(source.get(), &frame, &color);
videoSource->RemoveSink(&sink);
if (status != WEBRTC_JAVA_OK) return nullptr;
jintArray result = env->NewIntArray(7);
if (result != nullptr) env->SetIntArrayRegion(result, 0, 7, sink.values);
return result;
});
}
20 changes: 18 additions & 2 deletions webrtc-jni/src/main/cpp/src/api/ExtensionApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace jni
return error;
}

int ApiVideoSourcePush(void * source, const wj_i420_frame * frame)
int ApiVideoSourcePushColor(void * source, const wj_i420_frame * frame, const wj_color_space * color)
{
if (frame == nullptr) {
return WEBRTC_JAVA_ERR_HANDLE;
Expand Down Expand Up @@ -112,9 +112,19 @@ namespace jni
}
});

webrtc::ColorSpace colorSpace;

if (color != nullptr) {
colorSpace.set_primaries_from_uint8(color->primaries);
colorSpace.set_transfer_from_uint8(color->transfer);
colorSpace.set_matrix_from_uint8(color->matrix);
colorSpace.set_range_from_uint8(color->range);
}

webrtc::VideoFrame videoFrame = webrtc::VideoFrame::Builder()
.set_video_frame_buffer(buffer)
.set_rotation(rotation)
.set_color_space(color != nullptr ? &colorSpace : nullptr)
.build();

CustomVideoSource * videoSource = static_cast<CustomVideoSource *>(source);
Expand All @@ -129,6 +139,11 @@ namespace jni
return WEBRTC_JAVA_OK;
}

int ApiVideoSourcePush(void * source, const wj_i420_frame * frame)
{
return ApiVideoSourcePushColor(source, frame, nullptr);
}

int ApiAudioSourcePush(void * source, const wj_audio_chunk * chunk)
{
if (source == nullptr || chunk == nullptr || chunk->samples == nullptr) {
Expand Down Expand Up @@ -256,7 +271,8 @@ namespace jni
&ApiAudioSourceRelease,
&ApiEncodedObserverAdd,
&ApiEncodedObserverRemove,
&ApiEncodedFramesRelease
&ApiEncodedFramesRelease,
&ApiVideoSourcePushColor
};
}

Expand Down
6 changes: 6 additions & 0 deletions webrtc-media/src/main/cpp/include/media/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ namespace ffmpeg
bool IsHardware() const;

private:
friend struct ColorProbe;

void ResolveColor(AVFrame * frame) const;

// Resolves color properties and retains I420 or converts other formats.
int MakeI420(AVFrame * source, AVFrame ** frame);
// How many packets are kept, until the hardware has produced a
// frame, to be decoded again in software if it fails by then.
static constexpr size_t kMaxKeptPackets = 32;
Expand Down
82 changes: 82 additions & 0 deletions webrtc-media/src/main/cpp/src/JNI_ColorProbe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.
*/

// Test-only entry point: exercises the production color resolver and scaler
// with constant planes, avoiding lossy codecs and hardware-dependent results.
#include <jni.h>
#include "media/VideoDecoder.h"
#include "media/JniBoundary.h"
#include <cstring>

namespace ffmpeg {
struct ColorProbe {
static jintArray Convert(JNIEnv * env, jint format, jint matrix, jint range,
jint y, jint u, jint v)
{
VideoDecoder decoder;
AVFrame * source = av_frame_alloc();
AVFrame * converted = nullptr;
if (source == nullptr) return nullptr;
source->width = source->height = 32;
source->format = format == 0 ? AV_PIX_FMT_YUV444P
: format == 1 ? AV_PIX_FMT_RGB24
: format == 2 ? AV_PIX_FMT_YUVJ420P : AV_PIX_FMT_YUV420P;
source->colorspace = static_cast<AVColorSpace>(matrix);
source->color_range = static_cast<AVColorRange>(range);
source->color_primaries = AVCOL_PRI_BT709;
source->color_trc = AVCOL_TRC_BT709;
int error = av_frame_get_buffer(source, 32);
if (error >= 0) {
if (format == 1) {
for (int row = 0; row < 32; ++row) {
for (int col = 0; col < 32; ++col) {
uint8_t * pixel = source->data[0] + row * source->linesize[0] + col * 3;
pixel[0] = y; pixel[1] = u; pixel[2] = v;
}
}
}
else {
const int values[] = { y, u, v };
for (int plane = 0; plane < 3; ++plane) {
int size = format >= 2 && plane > 0 ? 16 : 32;
for (int row = 0; row < size; ++row) {
std::memset(source->data[plane] + row * source->linesize[plane], values[plane], size);
}
}
}
error = decoder.MakeI420(source, &converted);
}
jintArray result = nullptr;
if (error >= 0) {
jint values[] = { converted->data[0][0], converted->data[1][0], converted->data[2][0],
converted->color_primaries, converted->color_trc, converted->colorspace, converted->color_range };
result = env->NewIntArray(7);
if (result != nullptr) env->SetIntArrayRegion(result, 0, 7, values);
}
av_frame_free(&converted);
av_frame_free(&source);
return result;
}
};
}

extern "C" JNIEXPORT jintArray JNICALL Java_dev_onvoid_webrtc_media_player_ColorProbe_convert
(JNIEnv * env, jclass, jint format, jint matrix, jint range, jint y, jint u, jint v)
{
return ffmpeg::CallFromJava(env, static_cast<jintArray>(nullptr), [&]() {
return ffmpeg::ColorProbe::Convert(env, format, matrix, range, y, u, v);
});
}
9 changes: 8 additions & 1 deletion webrtc-media/src/main/cpp/src/media/MediaPacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,14 @@ namespace ffmpeg
// the push succeeds or fails, so this item must not free it as well.
item.frame = nullptr;

api_->video_source_push(video_source_, &pushed);
const wj_color_space color = {
static_cast<uint8_t>(frame->color_primaries),
static_cast<uint8_t>(frame->color_trc),
static_cast<uint8_t>(frame->colorspace),
static_cast<uint8_t>(frame->color_range)
};

api_->video_source_push_color(video_source_, &pushed, &color);
}

void MediaPacer::DeliverAudio(const AudioItem & item, int64_t mapped_us)
Expand Down
Loading
Loading