UNPKG

tav-media

Version:

Cross platform media editing framework

128 lines (127 loc) 5.81 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; 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()); }); }; import { tav } from '../tav'; import { allowCallNativeAnytime } from '../types/tav-object'; import { Dispatcher } from '../utils/dispatcher'; /** * A video reader is used to obtain the video frames of a media. * @category Engine */ export class TAVVideoReader { constructor(ref) { this.ref = ref; } /** * Make a video reader from a media clip. * @param root The root media clip. * @param frameRate The frame rate of the video reader. * @param isRecycle * @returns New video reader. */ static MakeFrom(root, frameRate, isRecycle = false) { return __awaiter(this, void 0, void 0, function* () { const rootNative = yield root.build(); if (!rootNative) return null; const native = yield tav.webAssemblyQueue.exec(tav.TAVVideoReader.MakeFrom, tav.TAVVideoReader, rootNative, frameRate, isRecycle); if (!native) return null; return new TAVVideoReader(native); }); } /** * Set the surface to render the video frames. * @param surface The surface to render the video. */ setSurface(surface) { return __awaiter(this, void 0, void 0, function* () { this.surface = surface; const nativeSurface = yield surface.build(); if (this.ref && nativeSurface) { yield tav.webAssemblyQueue.exec(this.ref.setSurface, this.ref, nativeSurface); } }); } /** * Sets the current frame to the specified time. * @param targetTime The target time to seek to. * @returns seeked or not. */ seekTo(targetTime) { return __awaiter(this, void 0, void 0, function* () { return TAVVideoReader.dispatcher.queue(() => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; if (tav.webAssemblyQueue) { (_b = (_a = this.surface).prepare) === null || _b === void 0 ? void 0 : _b.call(_a); const previousHandle = tav.GL.currentContext.handle; tav.GL.makeContextCurrent(this.surface.webGLContextId); const result = yield tav.webAssemblyQueue.exec((_c = this.ref) === null || _c === void 0 ? void 0 : _c.seekTo, this.ref, targetTime); tav.GL.makeContextCurrent(previousHandle); return result; } return yield ((_d = this.ref) === null || _d === void 0 ? void 0 : _d.seekTo(targetTime)); }), 'seekTo'); }); } /** * Reads the next video frame for the output. */ readNextFrame() { return __awaiter(this, void 0, void 0, function* () { yield TAVVideoReader.dispatcher.queue(() => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; if (tav.webAssemblyQueue) { (_b = (_a = this.surface).prepare) === null || _b === void 0 ? void 0 : _b.call(_a); const previousHandle = tav.GL.currentContext.handle; tav.GL.makeContextCurrent(this.surface.webGLContextId); yield tav.webAssemblyQueue.exec((_c = this.ref) === null || _c === void 0 ? void 0 : _c.readNextFrame, this.ref); tav.GL.makeContextCurrent(previousHandle); (_e = (_d = this.surface).present) === null || _e === void 0 ? void 0 : _e.call(_d); } else { return yield ((_f = this.ref) === null || _f === void 0 ? void 0 : _f.readNextFrame()); } }), 'readNextFrame'); }); } /** * Returns the current scale mode. */ scaleMode() { var _a; return (_a = this.ref) === null || _a === void 0 ? void 0 : _a.scaleMode(); } /** * Specifies the rule of how to scale the content to fit the surface size. The matrix * changes when this method is called. * @param mode The scale mode. */ setScaleMode(mode) { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.setScaleMode(mode); } release() { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.release(); } } TAVVideoReader.dispatcher = new Dispatcher(); __decorate([ allowCallNativeAnytime ], TAVVideoReader.prototype, "setScaleMode", null); __decorate([ allowCallNativeAnytime ], TAVVideoReader.prototype, "release", null);