UNPKG

tav-media

Version:

Cross platform media editing framework

94 lines (93 loc) 3.31 kB
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 { Http } from '../io/http'; import { tav } from '../tav'; export class WxVideoReader { constructor(path) { this.path = path; this.type = 1; this.width = 1; this.height = 1; this.contentTime = 0; this.gotFrame = false; WxVideoReader.preload(path); this.video = WxVideoReader.videos[path]; this.width = this.video.videoWidth; this.height = this.video.videoHeight; setInterval(() => this.render(), 5); } static preload(path) { return __awaiter(this, void 0, void 0, function* () { const info = yield wx.getVideoInfo({ src: path }); const video = { videoHeight: info.height, videoWidth: info.width, duration: info.duration, decoder: wx.createVideoDecoder(), }; yield video.decoder.start({ source: Http.getUrl(path) }); this.videos[path] = video; return video; }); } seekTo(contentTime) { if (contentTime < this.contentTime) { this.video.decoder.seek(contentTime); } this.contentTime = contentTime; } render() { const frameData = this.video.decoder.getFrameData(); if (frameData) { this.lastFrameData = frameData; this.gotFrame = true; } } readSample() { let dataUint8Array; const frameData = this.lastFrameData; if (this.gotFrame) { dataUint8Array = new Uint8Array(frameData.data); } else { dataUint8Array = new Uint8Array([0, 0, 0, 0]); } const module = tav; const numBytes = dataUint8Array.byteLength * Uint8Array.BYTES_PER_ELEMENT; const dataPtr = module._malloc(numBytes); if (frameData) { this.lastSampleData = dataPtr; } const dataOnHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numBytes); dataOnHeap.set(dataUint8Array); return { bytes: dataOnHeap.byteOffset, length: dataOnHeap.length, width: frameData ? frameData.width : 1, height: frameData ? frameData.height : 1, }; } freeBuffer() { this.freeBufferNextFrame(); } freeBufferNextFrame() { if (!this.lastSampleData) { return; } const module = tav; module._free(this.lastSampleData); this.lastSampleData = null; } release() { this.freeBuffer(); } setOptions() { } } WxVideoReader.videos = {};