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 @@ -49,7 +49,7 @@
/// which lets the browser's asynchronous `seeked`/`loadedmetadata` events fire while a
/// decode call appears synchronous to the caller.
///
/// **Encoding** uses the WebCodecs `VideoEncoder`/`AudioEncoder` (H.264/VP9 + AAC/Opus)
/// **Encoding** uses the WebCodecs `VideoEncoder`/`AudioEncoder` (H.264/VP8/VP9 + AAC/Opus)
/// muxed into MP4/WebM with the standalone `mp4-muxer`/`webm-muxer` libraries (loaded
/// from a CDN, the same mechanism the port uses for VideoJS). Frames are pushed as RGBA,
/// the muxer's in-memory buffer is handed back to Java on `close()` and written to the
Expand All @@ -62,10 +62,19 @@ public VideoCodec[] getAvailableEncoders() {
return new VideoCodec[0];
}
List<VideoCodec> out = new ArrayList<VideoCodec>();
out.add(new VideoCodec(CODEC_H264, "H.264 (WebCodecs)", "video/avc", true, true, false, true, -1, -1, new String[]{CONTAINER_MP4}));
out.add(new VideoCodec(CODEC_VP9, "VP9 (WebCodecs)", "video/vp9", true, true, false, true, -1, -1, new String[]{CONTAINER_WEBM}));
if (cn1AudioWebCodecsAvailable()) {
if (cn1VideoEncoderSupported("avc1.42001f")) {
out.add(new VideoCodec(CODEC_H264, "H.264 (WebCodecs)", "video/avc", true, true, false, true, -1, -1, new String[]{CONTAINER_MP4}));
}
if (cn1VideoEncoderSupported("vp8")) {
out.add(new VideoCodec(CODEC_VP8, "VP8 (WebCodecs)", "video/vp8", true, true, false, true, -1, -1, new String[]{CONTAINER_WEBM}));
}
if (cn1VideoEncoderSupported("vp09.00.10.08")) {
out.add(new VideoCodec(CODEC_VP9, "VP9 (WebCodecs)", "video/vp9", true, true, false, true, -1, -1, new String[]{CONTAINER_WEBM}));
}
if (cn1AudioWebCodecsAvailable() && cn1AudioEncoderSupported("mp4a.40.2")) {
out.add(new VideoCodec(CODEC_AAC, "AAC (WebCodecs)", "audio/mp4a-latm", false, true, false, false, -1, -1, new String[]{CONTAINER_MP4}));
}
if (cn1AudioWebCodecsAvailable() && cn1AudioEncoderSupported("opus")) {
out.add(new VideoCodec(CODEC_OPUS, "Opus (WebCodecs)", "audio/opus", false, true, false, false, -1, -1, new String[]{CONTAINER_WEBM}));
}
return out.toArray(new VideoCodec[out.size()]);
Expand Down Expand Up @@ -211,6 +220,24 @@ static final class Writer extends VideoWriter {
this.hasAudio = cfg.isHasAudio();
this.outPath = cfg.getPath();

boolean webm = CONTAINER_WEBM.equals(cfg.getContainer());
boolean mp4 = CONTAINER_MP4.equals(cfg.getContainer());
if (!webm && !mp4) {
throw new IOException("Unsupported JavaScript video container: " + cfg.getContainer());
}
if (webm && !CODEC_VP8.equals(cfg.getVideoCodec()) && !CODEC_VP9.equals(cfg.getVideoCodec())) {
throw new IOException("WebM requires VP8 or VP9 video, not " + cfg.getVideoCodec());
}
if (mp4 && !CODEC_H264.equals(cfg.getVideoCodec())) {
throw new IOException("MP4 requires H.264 video, not " + cfg.getVideoCodec());
}
if (hasAudio && webm && !CODEC_OPUS.equals(cfg.getAudioCodec())) {
throw new IOException("WebM requires Opus audio, not " + cfg.getAudioCodec());
}
if (hasAudio && mp4 && !CODEC_AAC.equals(cfg.getAudioCodec())) {
throw new IOException("MP4 requires AAC audio, not " + cfg.getAudioCodec());
}

cn1EncEnsureLibs();
long start = System.currentTimeMillis();
while (!cn1EncLibsReady(cfg.getContainer())) {
Expand All @@ -223,8 +250,12 @@ static final class Writer extends VideoWriter {
if (br <= 0) {
br = (int) Math.max(800000L, Math.min((long) (width * (long) height * Math.max(1f, frameRate) * 0.10), 100000000L));
}
int audioBr = cfg.getAudioBitRate();
if (hasAudio && audioBr <= 0) {
audioBr = 128000;
}
this.peer = cn1EncOpen(cfg.getContainer(), cfg.getVideoCodec(), width, height, Math.round(frameRate), br,
hasAudio, cfg.getAudioCodec(), cfg.getAudioBitRate(), cfg.getSampleRate(), cfg.getAudioChannels());
hasAudio, cfg.getAudioCodec(), audioBr, cfg.getSampleRate(), cfg.getAudioChannels());
if (peer == 0) {
throw new IOException("Failed to configure the WebCodecs encoder: " + safeError(0));
}
Expand Down Expand Up @@ -341,6 +372,36 @@ private static String safeError(int peer) {
@JSBody(params = {}, script = "return (typeof AudioEncoder !== 'undefined' && typeof AudioData !== 'undefined');")
private static native boolean cn1AudioWebCodecsAvailable();

@JSBody(params = {"codec"}, script =
"if (typeof VideoEncoder === 'undefined') { return false; }"
+ "var encoder = null;"
+ "try {"
+ " encoder = new VideoEncoder({ output: function() {}, error: function() {} });"
+ " var config = { codec: codec, width: 128, height: 96, bitrate: 800000, framerate: 6 };"
+ " if (codec.indexOf('avc1.') === 0) { config.avc = { format: 'avc' }; }"
+ " encoder.configure(config);"
+ " encoder.close();"
+ " return true;"
+ "} catch (e) {"
+ " try { if (encoder && encoder.state !== 'closed') { encoder.close(); } } catch (ignored) {}"
+ " return false;"
+ "}")
private static native boolean cn1VideoEncoderSupported(String codec);

@JSBody(params = {"codec"}, script =
"if (typeof AudioEncoder === 'undefined') { return false; }"
+ "var encoder = null;"
+ "try {"
+ " encoder = new AudioEncoder({ output: function() {}, error: function() {} });"
+ " encoder.configure({ codec: codec, sampleRate: 48000, numberOfChannels: 1, bitrate: 128000 });"
+ " encoder.close();"
+ " return true;"
+ "} catch (e) {"
+ " try { if (encoder && encoder.state !== 'closed') { encoder.close(); } } catch (ignored) {}"
+ " return false;"
+ "}")
private static native boolean cn1AudioEncoderSupported(String codec);

// ============================================================= JS: decoder

@JSBody(params = {"url"}, script =
Expand Down
18 changes: 18 additions & 0 deletions Ports/JavaScriptPort/src/main/webapp/port.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,24 @@ bindNative([
return (yield* cn1VideoIoHost({ op: "audioWebCodecsAvailable" })) ? 1 : 0;
});

bindNative([
"cn1_com_codename1_impl_html5_HTML5VideoIO_cn1VideoEncoderSupported_java_lang_String_R_boolean",
"cn1_com_codename1_impl_html5_HTML5VideoIO_cn1VideoEncoderSupported___java_lang_String_R_boolean"
], function*(codec) {
return (yield* cn1VideoIoHost({
op: "videoEncoderSupported", codec: jvm.toNativeString(codec)
})) ? 1 : 0;
});

bindNative([
"cn1_com_codename1_impl_html5_HTML5VideoIO_cn1AudioEncoderSupported_java_lang_String_R_boolean",
"cn1_com_codename1_impl_html5_HTML5VideoIO_cn1AudioEncoderSupported___java_lang_String_R_boolean"
], function*(codec) {
return (yield* cn1VideoIoHost({
op: "audioEncoderSupported", codec: jvm.toNativeString(codec)
})) ? 1 : 0;
});

bindNative([
"cn1_com_codename1_impl_html5_HTML5VideoIO_cn1VideoOpen_java_lang_String_R_int"
], function*(url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codenameone.examples.hellocodenameone.tests;

import com.codename1.io.FileSystemStorage;
Expand Down Expand Up @@ -78,23 +100,48 @@ private void runRoundTrip() {
return;
}

// Pick an available video codec (prefer H.264) and audio codec (prefer AAC).
String videoCodec = null;
String audioCodec = null;
// Pick a container-compatible pair. Audio and video codecs cannot be
// selected independently: Opus is valid for WebM but not MP4, while
// AAC is valid for MP4 but not WebM. Prefer a complete A/V round trip,
// then fall back to a video-only round trip when the browser has no
// compatible audio encoder.
boolean h264 = false;
boolean vp8 = false;
boolean vp9 = false;
boolean aac = false;
boolean opus = false;
for (VideoCodec c : io.getAvailableEncoders()) {
if (c.isVideo()) {
if (VideoIO.CODEC_H264.equals(c.getId())) {
videoCodec = c.getId();
} else if (videoCodec == null) {
videoCodec = c.getId();
}
} else {
if (VideoIO.CODEC_AAC.equals(c.getId())) {
audioCodec = c.getId();
} else if (audioCodec == null) {
audioCodec = c.getId();
}
}
String id = c.getId();
h264 |= VideoIO.CODEC_H264.equals(id);
vp8 |= VideoIO.CODEC_VP8.equals(id);
vp9 |= VideoIO.CODEC_VP9.equals(id);
aac |= VideoIO.CODEC_AAC.equals(id);
opus |= VideoIO.CODEC_OPUS.equals(id);
}

String videoCodec;
String audioCodec;
if (h264 && aac) {
videoCodec = VideoIO.CODEC_H264;
audioCodec = VideoIO.CODEC_AAC;
} else if (vp8 && opus) {
videoCodec = VideoIO.CODEC_VP8;
audioCodec = VideoIO.CODEC_OPUS;
} else if (vp9 && opus) {
videoCodec = VideoIO.CODEC_VP9;
audioCodec = VideoIO.CODEC_OPUS;
} else if (h264) {
videoCodec = VideoIO.CODEC_H264;
audioCodec = null;
} else if (vp8) {
videoCodec = VideoIO.CODEC_VP8;
audioCodec = null;
} else if (vp9) {
videoCodec = VideoIO.CODEC_VP9;
audioCodec = null;
} else {
videoCodec = null;
audioCodec = null;
}
if (videoCodec == null) {
skip("no-video-encoder-on-" + Display.getInstance().getPlatformName());
Expand All @@ -105,11 +152,6 @@ private void runRoundTrip() {
String container = webm ? VideoIO.CONTAINER_WEBM : VideoIO.CONTAINER_MP4;
String mime = webm ? "video/webm" : "video/mp4";
boolean withAudio = audioCodec != null;
if (webm && withAudio && VideoIO.CODEC_AAC.equals(audioCodec)) {
// AAC isn't a WebM audio codec; only keep audio if Opus is available.
withAudio = io.isEncoderSupported(VideoIO.CODEC_OPUS);
audioCodec = VideoIO.CODEC_OPUS;
}

String path = FileSystemStorage.getInstance().getAppHomePath()
+ "/cn1-videoio-roundtrip-" + System.currentTimeMillis() + (webm ? ".webm" : ".mp4");
Expand Down
Loading
Loading