tav-media
Version:
Cross platform media editing framework
237 lines (236 loc) • 9.05 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { tav } from '../tav';
import { Matrix } from '../types/types';
import { MediaClip } from './tav-media-clip';
import { Rect } from '../types/tav-rect';
import { makeClipAssetPath } from '../utils/clip-asset-path';
/**
* A Media which might have a video output or a audio output.
* @extends MediaClip
* @hideconstructor
* @category Clips
*/
export class MovieClip extends MediaClip {
constructor() {
super(...arguments);
this.type = 'Movie';
this._opacity = 1;
this._matrix = new Matrix();
this._cropRect = new Rect();
}
/**
* Creates a new Movie from a asset. Returns null if the asset is not valid.
* @param asset The asset to create the Movie from.
* @param contentStartTime The start time of the content in the asset.
* @param contentDuration The duration of the content in the asset.
*/
static MakeFromAsset(asset, contentStartTime, contentDuration) {
return __awaiter(this, void 0, void 0, function* () {
return MovieClip._MakeFromAsset(asset, contentStartTime, contentDuration);
});
}
/**
* Creates a new Movie from a asset. Returns null if the asset is not valid.
* @param asset The asset to create the Movie from.
* @param ranges The time ranges wanted to be played.
* @param contentOffset The offset of the time ranges.
*/
static MakeFromAssetAndRanges(asset, ranges, contentOffset = 0) {
return __awaiter(this, void 0, void 0, function* () {
return MovieClip._MakeFromAsset(asset, ranges, contentOffset);
});
}
static _MakeFromAsset(asset, contentStartTimeOrRange, contentDurationOrOffset) {
return __awaiter(this, void 0, void 0, function* () {
if (!(asset === null || asset === void 0 ? void 0 : asset.path)) {
return null;
}
const movieClip = new MovieClip();
if (typeof contentStartTimeOrRange === 'number') {
movieClip.contentStartTime = contentStartTimeOrRange;
movieClip.contentDuration = contentDurationOrOffset;
}
else {
movieClip._contentRanges = contentStartTimeOrRange;
movieClip._contentOffset = contentDurationOrOffset;
}
const myAsset = asset;
if (asset.type === 'MovieAsset') {
myAsset.path = makeClipAssetPath(movieClip.id, asset.path);
yield myAsset.build();
}
movieClip._asset = myAsset;
return movieClip;
});
}
clone() {
const newMovie = new MovieClip();
const _a = this, { _nativeObject: _nativeClip, parent, id, nativeInvalidated } = _a, otherProperties = __rest(_a, ["_nativeObject", "parent", "id", "nativeInvalidated"]);
return Object.assign(newMovie, otherProperties);
}
createClip() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.asset || !this.asset.path)
return undefined;
if (this._nativeObject
&& !this._contentTimeInvalid) {
return this._nativeObject;
}
const nativeAsset = yield this.asset.build();
this._contentTimeInvalid = false;
if (this._contentRanges) {
return tav.Movie.MakeFrom_range(nativeAsset, this._contentRanges, this._contentOffset);
}
return tav.Movie.MakeFrom(nativeAsset, this.contentStartTime || 0, this.contentDuration || this.duration || 0);
});
}
updateClip(clip) {
const _super = Object.create(null, {
updateClip: { get: () => super.updateClip }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.updateClip.call(this, clip);
const movie = clip;
if (tav.webAssemblyQueue.exec) {
const nativeMatrix = yield tav.webAssemblyQueue.exec(this.matrix.build, this.matrix, null);
const nativeCropRect = yield tav.webAssemblyQueue.exec(this.cropRect.build, this.cropRect, null);
yield tav.webAssemblyQueue.exec(movie.setOpacity, movie, this.opacity);
yield tav.webAssemblyQueue.exec(movie.setMatrix, movie, nativeMatrix);
yield tav.webAssemblyQueue.exec(movie.setCropRect, movie, nativeCropRect);
}
else {
const nativeMatrix = yield this.matrix.build();
const nativeCropRect = yield this.cropRect.build();
movie.setOpacity(this.opacity);
movie.setMatrix(nativeMatrix);
movie.setCropRect(nativeCropRect);
}
});
}
setContentStartTime(time) {
super.setContentStartTime(time);
if (typeof time !== 'undefined') {
this._contentRanges = undefined;
this._contentOffset = undefined;
}
}
setContentDuration(time) {
super.setContentDuration(time);
if (typeof time !== 'undefined') {
this._contentRanges = undefined;
this._contentOffset = undefined;
}
}
/**
* Get the width of the movie.
*/
get width() {
var _a;
return ((_a = this.nativeClip) === null || _a === void 0 ? void 0 : _a.width()) || 0;
}
/**
* Get the height of the movie.
*/
get height() {
var _a;
return ((_a = this.nativeClip) === null || _a === void 0 ? void 0 : _a.height()) || 0;
}
/**
* Get or set the alpha value of this movie. The value of this property must be in the range 0.0
* (transparent) to 1.0 (opaque). Values outside that range are clamped to the minimum or maximum.
* The default value of this property is 1.0.
*/
get opacity() {
return this._opacity;
}
set opacity(opa) {
if (!isNaN(opa) && opa >= 0 && opa <= 1) {
this._opacity = opa;
}
if (!this.nativeClip) {
this.invalidated();
;
return;
}
const movie = this.nativeClip;
if (tav.webAssemblyQueue) {
tav.webAssemblyQueue.exec(movie.setOpacity, movie, this.opacity);
}
else {
movie.setOpacity(this.opacity);
}
}
/**
* Get or sets the transformation which specifies how this movie's video contents are positioned in
* parent Composition.
*/
get matrix() {
return this._matrix;
}
set matrix(matrix) {
this._matrix = matrix;
this.updateMatrix();
}
/**
* Get or set the rectangle which specifies how this movie's video contents are cropped in parent
* Composition.
*/
get cropRect() {
return this._cropRect;
}
set cropRect(rect) {
this._cropRect = rect;
this.updateCropRect();
}
updateMatrix() {
if (this.nativeClip) {
const nativeMatrix = this.matrix.build();
const movie = this.nativeClip;
if (tav.webAssemblyQueue) {
tav.webAssemblyQueue.exec(movie.setMatrix, movie, nativeMatrix);
}
else {
movie.setMatrix(nativeMatrix);
}
}
else {
this.invalidated();
;
}
}
updateCropRect() {
if (this.nativeClip) {
const nativeCropRect = this.cropRect.build();
const movie = this.nativeClip;
if (tav.webAssemblyQueue) {
tav.webAssemblyQueue.exec(movie.setCropRect, movie, nativeCropRect);
}
else {
movie.setCropRect(nativeCropRect);
}
}
else {
this.invalidated();
;
}
}
}