Repository navigation
VideoIndicator
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not kept in the working file VideoIndicator.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official;
import com.velitask.sdk.FrameContext;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.IndicatorSkin;
import com.velitask.sdk.TickContext;
import com.velitask.sdk.geom.FitGeometry;
import com.velitask.sdk.geom.FitMode;
import com.velitask.sdk.properties.BooleanProperty;
import com.velitask.sdk.properties.BorderProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FitModeProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerSliderProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.VideoSourceProperty;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import org.abricos.core.state.Size;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
/* A video overlay (picture-in-picture): frames of a video source in a border, plus the sound of this source. */
public class VideoIndicator extends Indicator {
/* The stable indicator name; together with the plugin UID it forms the layer name in JSON. */
public static final String NAME = "video";
/* The prefix of the indicator's localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Adaptive source: off (false) by default. */
private final BooleanProperty mAdaptive
= new BooleanProperty(false, "adaptive", localized(KEY + ".adaptive.title"));
/* The selected video source; it is not saved in a template. */
private final VideoSourceProperty mVideo = new VideoSourceProperty();
/* Border: color, thickness, line style, pattern, corner radius. */
private final BorderProperty mBorder = new BorderProperty();
/* How the frame is fitted into the layer; COVER by default. */
private final FitModeProperty mFit = new FitModeProperty();
/* The source sound is on by default. */
private final BooleanProperty mAudioEnabled
= new BooleanProperty(true, "audioEnabled", localized(KEY + ".audioEnabled.title"));
/* Volume in percent: 0..100, 100 by default. */
private final IntegerSliderProperty mVolume
= new IntegerSliderProperty(100, "volume", localized(KEY + ".volume.title")) {
{
setRange(0, 100);
}
};
public VideoSourceProperty getVideo() {
return mVideo;
}
public BorderProperty getBorder() {
return mBorder;
}
@Override
public String getName() {
return NAME;
}
@Override
public String getTitle() {
return localized(KEY + ".title");
}
@Override
public String getDescription() {
return localized(KEY + ".description");
}
@Override
public String getTags() {
return localized(KEY + ".tags");
}
@Override
/* Default placement: bottom right corner, margin 50 at the right and bottom, size 480x270. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.RIGHT);
maket.setLayerMargin(null, null, 50d, 50d);
maket.setLayerSize(480d, 270d);
}
@Override
/* Property order = the order in JSON and in the settings panel. */
public IProperty[] defineProperties() {
return new IProperty[]{
mVideo, mAdaptive, mFit, mBorder, mAudioEnabled, mVolume
};
}
@Override
/* The border goes to the "Appearance" group, sound and volume go to the "Audio" group; the rest stays in the main group. */
public void configureDisplay(DisplayConfig config) {
config.set(mBorder, PropertyGroup.APPEARANCE);
config.set(mAudioEnabled, PropertyGroup.AUDIO);
config.set(mVolume, PropertyGroup.AUDIO);
}
@Override
/* Two skins change only the border: "Framed" (white, 4, no rounding) and "Rounded" (no line, radius 30). */
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("framed", localized(KEY + ".skin.framed"))
.set(mBorder.getColor().skin(Color.WHITE))
.set(mBorder.getThickness().skin(4))
.set(mBorder.getRadius().skin(0))
.build(),
IndicatorSkin.builder("rounded", localized(KEY + ".skin.rounded"))
.set(mBorder.getColor().skin(Color.WHITE))
.set(mBorder.getThickness().skin(0))
.set(mBorder.getRadius().skin(30))
.build()
};
}
@Override
/* Frame drawing: geometry calculation, getting the frame, clipping to the border, stroke. */
public void render(IndicatorContext indicatorContext) {
VideoContext ctx = (VideoContext) indicatorContext;
/* In the adaptive mode, with no suitable video at the current time, nothing is drawn. */
if (ctx.adaptive && ctx.video.sourceId == 0) {
return;
}
Graphics2D g = ctx.graphics;
/* The border thickness and radius are scaled together with the layer. */
int bw = (int) (ctx.border.thickness * ctx.scale);
int cr = (int) (ctx.border.radius * ctx.scale);
int innerW = Math.max(1, ctx.width - bw);
int innerH = Math.max(1, ctx.height - bw);
Size nat = ctx.video.videoSize();
int nw = nat != null ? nat.getWidthInt() : innerW;
int nh = nat != null ? nat.getHeightInt() : innerH;
/* d = {x, y, width, height} of the frame inside the inner area; the frame size comes from the video metadata. */
int[] d = FitGeometry.fit(ctx.fit, nw, nh, innerW, innerH);
/* The project time (UTC) is converted to the internal time of the video file. */
long rawTime = ctx.video.convertToRawTime(ctx.player.time);
/* In preview the time is clamped to the video range. */
if (ctx.player.isPreview) {
rawTime = ctx.video.clampRawToRange(rawTime);
}
/* Preview takes the nearest ready frame without waiting for the decoder; export takes the exact frame. */
BufferedImage frame = ctx.player.isPreview
? ctx.video.frameAtBestEffort(rawTime, d[2], d[3])
: ctx.video.frameAt(rawTime, d[2], d[3]);
if (frame == null) {
return;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
int x = bw / 2;
int y = bw / 2;
/* The frame is drawn inside a clip area (with rounding when the radius is > 0). */
Shape originalClip = g.getClip();
if (cr > 0) {
g.setClip(new RoundRectangle2D.Float(x, y, innerW, innerH, cr, cr));
} else {
g.setClip(x, y, innerW, innerH);
}
g.drawImage(frame, x + d[0], y + d[1], d[2], d[3], null);
g.setClip(originalClip);
/* The border is drawn over the frame only with a non-zero thickness; the line style and pattern are not used here. */
if (bw > 0) {
g.setColor(ctx.border.color);
g.setStroke(new BasicStroke(bw));
if (cr > 0) {
g.drawRoundRect(bw / 2, bw / 2, innerW, innerH, cr, cr);
} else {
g.drawRect(bw / 2, bw / 2, innerW, innerH);
}
}
}
/* A snapshot of the property values for a frame: border, video source, fit mode, adaptivity. */
public class VideoContext extends IndicatorContext {
public final BorderProperty.BorderContext border;
public final VideoSourceProperty.VideoSourceContext video;
final FitMode fit;
final boolean adaptive;
public VideoContext(FrameContext frame) {
super(frame);
adaptive = mAdaptive.get(frame);
/* The adaptive mode is passed to the source property before the source context is created. */
mVideo.setAdaptive(adaptive);
fit = mFit.get(frame);
border = mBorder.createContext(frame);
video = mVideo.createContext(frame);
}
}
@Override
/* Sound: the engine asks for a voice on every tick; null means no sound. */
public IndicatorContext.Voice resolveVoice(TickContext tick) {
if (!mAudioEnabled.get(tick)) {
return null;
}
mVideo.setAdaptive(mAdaptive.get(tick));
long id = mVideo.createContext(tick).sourceId;
return (id == 0) ? null : new IndicatorContext.Voice(id, mVolume.get(tick) / 100.0);
}
@Override
/* The context is created from the FrameContext of the current frame. */
public IndicatorContext createContext(FrameContext frame) {
return new VideoContext(frame);
}
}VideoIndicator is a video overlay ("picture-in-picture"): it shows frames of one of the project's video sources inside the layer, in a border with rounded corners if needed, and plays the sound of this source. The default placement is the bottom right corner, 480×270 with a margin of 50 at the right and bottom.
Data. The source is set by VideoSourceProperty and is stored only in the project state (it does not go into a template). For a frame the engine builds a VideoContext; from it come sourceId, the video size (videoSize()) and the frames (frameAt, frameAtBestEffort). The project time ctx.player.time is converted to the internal file time through convertToRawTime. In preview (ctx.player.isPreview) it is additionally clamped by clampRawToRange to the video bounds, and the frame is taken through frameAtBestEffort — the nearest available frame without waiting for the decoder. On export frameAt is used without clamping: the exact frame for the time.
Settings. Six properties in the order of defineProperties():
-
videoSource(VideoSourceProperty) — the choice of the video source. -
adaptive(BooleanProperty,false) — whentrue, the source is picked automatically on every frame: among the project's video sources the one whose interval contains the current time is chosen, and if there are several, the one that started later. If none fits, the indicator draws nothing. The flag is passed to the property by callingmVideo.setAdaptive(...)both inVideoContextand inresolveVoice. -
fit(FitModeProperty,COVER) — the fit mode:COVER,CONTAIN,FILL,NONE; the geometry is computed byFitGeometry.fit. -
border(BorderProperty) — the border; in thePropertyGroup.APPEARANCEgroup. -
audioEnabled(BooleanProperty,true) andvolume(IntegerSliderProperty, 0..100,100) — thePropertyGroup.AUDIOgroup.
Two skins ("framed" and "rounded") override only the color, thickness and radius of the border; the indicator has no text templates.
Sound. The resolveVoice(TickContext) method returns IndicatorContext.Voice(sourceId, volume / 100.0) or null: if audioEnabled is off or the source is not determined (sourceId == 0). Mixing and playback are done by the engine; the indicator only declares which source should sound and at what volume. This is the only way to control the sound: the sound flag and the volume of VideoSourceProperty itself are marked deprecated and are not read by the engine.
Drawing.
- If the mode is adaptive and no source is found — exit.
- The border thickness and radius are multiplied by
ctx.scale; the inner area is the layer minus the border thickness. -
FitGeometry.fitcomputes the position and size of the frame from the video size (if there is no metadata, the size is taken equal to the inner area). - A frame of the needed size is requested;
null— exit without drawing. - Anti-aliasing and bilinear interpolation are turned on; a clip area is set (with rounding when the radius is > 0), the frame is drawn into it, then the area is restored.
- When the border thickness is > 0, an outline (a rectangle or a rounded one) is drawn over it in the border color.
- The source is not stored in a template: after a template is applied, the source is chosen anew (or picked in the adaptive mode).
- The border's
styleanddashPatternare available in the settings but are not used when drawing: the outline is always solid. - In the
NONEmode the frame is requested at its own size and centered; the part of the frame that goes beyond the layer is clipped. - In preview the frame may lag slightly behind the requested time (best-effort); on export the frame is exact.
- Outside the video interval, on export the time is not clamped: the behavior is determined by the frame source.
- The indicator does not use sensors or sensor data.
- The save format is described in VideoIndicator JSON; the user pages are Video and Video JSON.