video backend
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// GraphicsObject model: Main container for layers and animation
|
||||
|
||||
import { context, config, pointerList, startProps } from '../state.js';
|
||||
import { VectorLayer, AudioTrack } from './layer.js';
|
||||
import { VectorLayer, AudioTrack, VideoLayer } from './layer.js';
|
||||
import { TempShape } from './shapes.js';
|
||||
import { AnimationCurve, Keyframe } from './animation.js';
|
||||
import { Widget } from '../widgets.js';
|
||||
@@ -45,8 +45,20 @@ class GraphicsObject extends Widget {
|
||||
this.name = this.idx;
|
||||
|
||||
this.currentFrameNum = 0; // LEGACY: kept for backwards compatibility
|
||||
this.currentTime = 0; // New: continuous time for AnimationData curves
|
||||
this._currentTime = 0; // Internal storage for currentTime
|
||||
this.currentLayer = 0;
|
||||
|
||||
// Make currentTime a getter/setter property
|
||||
Object.defineProperty(this, 'currentTime', {
|
||||
get: function() {
|
||||
return this._currentTime;
|
||||
},
|
||||
set: function(value) {
|
||||
this._currentTime = value;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
this._activeAudioTrack = null; // Reference to active audio track (if any)
|
||||
|
||||
// Initialize children and audioTracks based on initialChildType
|
||||
@@ -56,6 +68,9 @@ class GraphicsObject extends Widget {
|
||||
if (initialChildType === 'layer') {
|
||||
this.children = [new VectorLayer(uuid + "-L1", this)];
|
||||
this.currentLayer = 0; // Set first layer as active
|
||||
} else if (initialChildType === 'video') {
|
||||
this.children = [new VideoLayer(uuid + "-V1", "Video 1")];
|
||||
this.currentLayer = 0; // Set first video layer as active
|
||||
} else if (initialChildType === 'midi') {
|
||||
const midiTrack = new AudioTrack(uuid + "-M1", "MIDI 1", 'midi');
|
||||
this.audioTracks.push(midiTrack);
|
||||
@@ -103,7 +118,12 @@ class GraphicsObject extends Widget {
|
||||
graphicsObject.parent = pointerList[json.parent]
|
||||
}
|
||||
for (let layer of json.layers) {
|
||||
graphicsObject.layers.push(VectorLayer.fromJSON(layer, graphicsObject));
|
||||
if (layer.type === 'VideoLayer') {
|
||||
graphicsObject.layers.push(VideoLayer.fromJSON(layer));
|
||||
} else {
|
||||
// Default to VectorLayer
|
||||
graphicsObject.layers.push(VectorLayer.fromJSON(layer, graphicsObject));
|
||||
}
|
||||
}
|
||||
// Handle audioTracks (may not exist in older files)
|
||||
if (json.audioTracks) {
|
||||
@@ -177,9 +197,20 @@ class GraphicsObject extends Widget {
|
||||
|
||||
// Check visual layers
|
||||
for (let layer of this.layers) {
|
||||
// Check animation data duration
|
||||
if (layer.animationData && layer.animationData.duration > maxDuration) {
|
||||
maxDuration = layer.animationData.duration;
|
||||
}
|
||||
|
||||
// Check video layer clips (VideoLayer has clips like AudioTrack)
|
||||
if (layer.type === 'video' && layer.clips) {
|
||||
for (let clip of layer.clips) {
|
||||
const clipEnd = clip.startTime + clip.duration;
|
||||
if (clipEnd > maxDuration) {
|
||||
maxDuration = clipEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check audio tracks
|
||||
@@ -300,6 +331,12 @@ class GraphicsObject extends Widget {
|
||||
for (let layer of this.layers) {
|
||||
if (context.activeObject == this && !layer.visible) continue;
|
||||
|
||||
// Handle VideoLayer differently - call its draw method
|
||||
if (layer.type === 'video') {
|
||||
layer.draw(context);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Draw activeShape (shape being drawn in progress) for active layer only
|
||||
if (layer === context.activeLayer && layer.activeShape) {
|
||||
let cxt = {...context};
|
||||
|
||||
+266
-1
@@ -1245,4 +1245,269 @@ class AudioTrack {
|
||||
}
|
||||
}
|
||||
|
||||
export { VectorLayer, AudioTrack };
|
||||
class VideoLayer extends Widget {
|
||||
constructor(uuid, name) {
|
||||
super(0, 0);
|
||||
if (!uuid) {
|
||||
this.idx = uuidv4();
|
||||
} else {
|
||||
this.idx = uuid;
|
||||
}
|
||||
this.name = name || "Video";
|
||||
this.type = 'video';
|
||||
this.visible = true;
|
||||
this.audible = true;
|
||||
this.animationData = new AnimationData(this);
|
||||
|
||||
// Empty arrays for layer compatibility
|
||||
Object.defineProperty(this, 'shapes', {
|
||||
value: Object.freeze([]),
|
||||
writable: false,
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
Object.defineProperty(this, 'children', {
|
||||
value: Object.freeze([]),
|
||||
writable: false,
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
|
||||
// Video clips on this layer
|
||||
// { clipId, poolIndex, name, startTime, duration, offset, width, height }
|
||||
this.clips = [];
|
||||
|
||||
// Associated audio track (if video has audio)
|
||||
this.linkedAudioTrack = null; // Reference to AudioTrack
|
||||
|
||||
// Timeline display
|
||||
this.collapsed = false;
|
||||
this.curvesMode = 'segment';
|
||||
this.curvesHeight = 150;
|
||||
|
||||
pointerList[this.idx] = this;
|
||||
}
|
||||
|
||||
async addClip(poolIndex, startTime, duration, offset = 0.0, name = '', sourceDuration = null) {
|
||||
const poolInfo = await invoke('video_get_pool_info', { poolIndex });
|
||||
// poolInfo is [width, height, fps] tuple from Rust
|
||||
const [width, height, fps] = poolInfo;
|
||||
|
||||
this.clips.push({
|
||||
clipId: this.clips.length,
|
||||
poolIndex,
|
||||
name: name || `Video ${this.clips.length + 1}`,
|
||||
startTime,
|
||||
duration,
|
||||
offset,
|
||||
width,
|
||||
height,
|
||||
sourceDuration: sourceDuration || duration // Store original file duration
|
||||
});
|
||||
|
||||
console.log(`Video clip added: ${name}, ${width}x${height}, duration: ${duration}s`);
|
||||
}
|
||||
|
||||
// Pre-fetch frames for current time (call before draw)
|
||||
async updateFrame(currentTime) {
|
||||
// Prevent concurrent calls - if already updating, skip
|
||||
if (this.updateInProgress) {
|
||||
return;
|
||||
}
|
||||
this.updateInProgress = true;
|
||||
|
||||
try {
|
||||
for (let clip of this.clips) {
|
||||
// Check if clip is active at current time
|
||||
if (currentTime < clip.startTime ||
|
||||
currentTime >= clip.startTime + clip.duration) {
|
||||
clip.currentFrame = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate video timestamp from clip time
|
||||
const clipTime = currentTime - clip.startTime;
|
||||
const videoTimestamp = clip.offset + clipTime;
|
||||
|
||||
// Only fetch if timestamp changed
|
||||
if (clip.lastFetchedTimestamp === videoTimestamp && clip.currentFrame) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if already fetching this frame
|
||||
if (clip.fetchInProgress === videoTimestamp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
clip.fetchInProgress = videoTimestamp;
|
||||
clip.lastFetchedTimestamp = videoTimestamp;
|
||||
|
||||
try {
|
||||
// Request frame from Rust backend
|
||||
const t_start = performance.now();
|
||||
let frameData = await invoke('video_get_frame', {
|
||||
poolIndex: clip.poolIndex,
|
||||
timestamp: videoTimestamp
|
||||
});
|
||||
const t_after_ipc = performance.now();
|
||||
|
||||
// Handle different formats that Tauri might return
|
||||
// ByteBuf from Rust can come as Uint8Array or Array depending on serialization
|
||||
if (!(frameData instanceof Uint8Array)) {
|
||||
frameData = new Uint8Array(frameData);
|
||||
}
|
||||
|
||||
// Validate frame data size
|
||||
const expectedSize = clip.width * clip.height * 4; // RGBA = 4 bytes per pixel
|
||||
|
||||
if (frameData.length !== expectedSize) {
|
||||
throw new Error(`Invalid frame data size: got ${frameData.length}, expected ${expectedSize}`);
|
||||
}
|
||||
|
||||
// Convert to ImageData
|
||||
const t_before_conversion = performance.now();
|
||||
const imageData = new ImageData(
|
||||
new Uint8ClampedArray(frameData),
|
||||
clip.width,
|
||||
clip.height
|
||||
);
|
||||
const t_after_conversion = performance.now();
|
||||
|
||||
// Create or reuse temp canvas
|
||||
if (!clip.frameCanvas) {
|
||||
clip.frameCanvas = document.createElement('canvas');
|
||||
clip.frameCanvas.width = clip.width;
|
||||
clip.frameCanvas.height = clip.height;
|
||||
}
|
||||
|
||||
const tempCtx = clip.frameCanvas.getContext('2d');
|
||||
const t_before_putimage = performance.now();
|
||||
tempCtx.putImageData(imageData, 0, 0);
|
||||
const t_after_putimage = performance.now();
|
||||
|
||||
clip.currentFrame = clip.frameCanvas;
|
||||
|
||||
// Log detailed timing breakdown
|
||||
const total_time = t_after_putimage - t_start;
|
||||
const ipc_time = t_after_ipc - t_start;
|
||||
const conversion_time = t_after_conversion - t_before_conversion;
|
||||
const putimage_time = t_after_putimage - t_before_putimage;
|
||||
|
||||
console.log(`[JS Video Timing] ts=${videoTimestamp.toFixed(3)}s | Total: ${total_time.toFixed(1)}ms | IPC: ${ipc_time.toFixed(1)}ms (${(ipc_time/total_time*100).toFixed(0)}%) | Convert: ${conversion_time.toFixed(1)}ms | PutImage: ${putimage_time.toFixed(1)}ms | Size: ${(frameData.length/1024/1024).toFixed(2)}MB`);
|
||||
} catch (error) {
|
||||
console.error('Failed to get video frame:', error);
|
||||
clip.currentFrame = null;
|
||||
} finally {
|
||||
clip.fetchInProgress = null;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.updateInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw cached frames (synchronous)
|
||||
draw(cxt, currentTime) {
|
||||
if (!this.visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ctx = cxt.ctx || cxt;
|
||||
|
||||
// Use currentTime from context if not provided
|
||||
if (currentTime === undefined) {
|
||||
currentTime = cxt.activeObject?.currentTime || 0;
|
||||
}
|
||||
|
||||
for (let clip of this.clips) {
|
||||
// Check if clip is active at current time
|
||||
if (currentTime < clip.startTime ||
|
||||
currentTime >= clip.startTime + clip.duration) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Draw cached frame if available
|
||||
if (clip.currentFrame) {
|
||||
try {
|
||||
// Scale to fit canvas while maintaining aspect ratio
|
||||
const canvasWidth = config.fileWidth;
|
||||
const canvasHeight = config.fileHeight;
|
||||
const scale = Math.min(
|
||||
canvasWidth / clip.width,
|
||||
canvasHeight / clip.height
|
||||
);
|
||||
const scaledWidth = clip.width * scale;
|
||||
const scaledHeight = clip.height * scale;
|
||||
const x = (canvasWidth - scaledWidth) / 2;
|
||||
const y = (canvasHeight - scaledHeight) / 2;
|
||||
|
||||
ctx.drawImage(clip.currentFrame, x, y, scaledWidth, scaledHeight);
|
||||
} catch (error) {
|
||||
console.error('Failed to draw video frame:', error);
|
||||
}
|
||||
} else {
|
||||
// Draw placeholder if frame not loaded yet
|
||||
ctx.save();
|
||||
ctx.fillStyle = '#333333';
|
||||
ctx.fillRect(0, 0, config.fileWidth, config.fileHeight);
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.font = '24px sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText('Loading...', config.fileWidth / 2, config.fileHeight / 2);
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static fromJSON(json) {
|
||||
const videoLayer = new VideoLayer(json.idx, json.name);
|
||||
|
||||
if (json.animationData) {
|
||||
videoLayer.animationData = AnimationData.fromJSON(json.animationData, videoLayer);
|
||||
}
|
||||
|
||||
if (json.clips) {
|
||||
videoLayer.clips = json.clips;
|
||||
}
|
||||
|
||||
if (json.linkedAudioTrack) {
|
||||
// Will be resolved after all objects are loaded
|
||||
videoLayer.linkedAudioTrack = json.linkedAudioTrack;
|
||||
}
|
||||
|
||||
videoLayer.visible = json.visible;
|
||||
videoLayer.audible = json.audible;
|
||||
|
||||
return videoLayer;
|
||||
}
|
||||
|
||||
toJSON(randomizeUuid = false) {
|
||||
return {
|
||||
type: "VideoLayer",
|
||||
idx: randomizeUuid ? uuidv4() : this.idx,
|
||||
name: randomizeUuid ? this.name + " copy" : this.name,
|
||||
visible: this.visible,
|
||||
audible: this.audible,
|
||||
animationData: this.animationData.toJSON(),
|
||||
clips: this.clips,
|
||||
linkedAudioTrack: this.linkedAudioTrack?.idx
|
||||
};
|
||||
}
|
||||
|
||||
copy(idx) {
|
||||
const json = this.toJSON(true);
|
||||
json.idx = idx.slice(0, 8) + this.idx.slice(8);
|
||||
return VideoLayer.fromJSON(json);
|
||||
}
|
||||
|
||||
// Compatibility methods for layer interface
|
||||
bbox() {
|
||||
return {
|
||||
x: { min: 0, max: config.fileWidth },
|
||||
y: { min: 0, max: config.fileHeight }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export { VectorLayer, AudioTrack, VideoLayer };
|
||||
|
||||
Reference in New Issue
Block a user