|
@@ -0,0 +1,157 @@
|
|
|
+!(function () {
|
|
|
+ //v0.5.70版之后,在Android手机端推荐使用以下音频引擎
|
|
|
+ if (/(Android)/i.test(navigator.userAgent)) {
|
|
|
+ NodePlayer.activeAudioEngine(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ function LivePlayer(elementId, options) {
|
|
|
+ const { url, } = options;
|
|
|
+ const eventCbMap = {};
|
|
|
+ const el = document.getElementById(elementId);
|
|
|
+ const player = new NodePlayer();
|
|
|
+ // 开启屏幕常亮
|
|
|
+ player.setKeepScreenOn();
|
|
|
+ // 绑定dom
|
|
|
+ player.setView(elementId);
|
|
|
+ // 设置最大缓冲时长,单位毫秒
|
|
|
+ player.setBufferTime(1000);
|
|
|
+ // 设置超时时长, 单位秒;回调timeout事件
|
|
|
+ player.setTimeout(10);
|
|
|
+ // 缩放模式,0 fill,1 contain,2 cover
|
|
|
+ player.setScaleMode(1);
|
|
|
+
|
|
|
+ player.on("videoInfo", (w, h, codec) => {
|
|
|
+ emit("videoInfo", { width: w, height: h });
|
|
|
+ });
|
|
|
+
|
|
|
+ player.on("timeout", () => {
|
|
|
+ emit("timeout");
|
|
|
+ });
|
|
|
+ player.on("start", () => {
|
|
|
+ emit("start");
|
|
|
+ });
|
|
|
+ player.on("stop", () => {
|
|
|
+ emit("stop");
|
|
|
+ });
|
|
|
+ player.on("error", (error) => {
|
|
|
+ // {code:404,msg:"not found"}
|
|
|
+ emit("error", error);
|
|
|
+ });
|
|
|
+
|
|
|
+ this.setSize = function (width, height) {
|
|
|
+ player.resizeView(width, height);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.play = function () {
|
|
|
+ player.start(url);
|
|
|
+ }
|
|
|
+ this.stop = function () {
|
|
|
+ player.stop();
|
|
|
+ }
|
|
|
+ this.on = function (name, callback) {
|
|
|
+ let cbs = eventCbMap[name];
|
|
|
+ if (!cbs) {
|
|
|
+ cbs = [];
|
|
|
+ eventCbMap[name] = cbs;
|
|
|
+ }
|
|
|
+ cbs.push(callback);
|
|
|
+ }
|
|
|
+ this.off = function (name, callback) {
|
|
|
+ let cbs = eventCbMap[name];
|
|
|
+ if (!!cbs) {
|
|
|
+ let index = cbs.indexOf(callback);
|
|
|
+ if (index > -1) {
|
|
|
+ cbs.splice(index, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ function emit(name, data) {
|
|
|
+ let cbs = eventCbMap[name];
|
|
|
+ if (!!cbs) {
|
|
|
+ let len = cbs.length;
|
|
|
+ for (let i = 0; i < len; i++) {
|
|
|
+ cbs[i](data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ function LiveSubPlayer(elementId, sourceId, options) {
|
|
|
+ let isCopying = false;
|
|
|
+ const canvas = document.getElementById(elementId);
|
|
|
+ const sourceCanvas = document.getElementById(sourceId);
|
|
|
+
|
|
|
+ //TODO:这里仅摄像头1
|
|
|
+ const clipPosition = {
|
|
|
+ x: options.merge.width - options.width,
|
|
|
+ y: options.merge.height - options.height,
|
|
|
+ }
|
|
|
+
|
|
|
+ this.play = function () {
|
|
|
+ startCopying();
|
|
|
+ }
|
|
|
+ this.stop = function () {
|
|
|
+ stopCopying();
|
|
|
+ }
|
|
|
+
|
|
|
+ function startCopying() {
|
|
|
+ if (!isCopying) {
|
|
|
+ isCopying = true;
|
|
|
+
|
|
|
+ const sourceGL = sourceCanvas.getContext("webgl");
|
|
|
+ const scale = sourceGL.drawingBufferWidth / options.merge.width;
|
|
|
+
|
|
|
+ const block = {
|
|
|
+ width: parseInt(options.width * scale),
|
|
|
+ height: parseInt(options.height * scale),
|
|
|
+ clipX: parseInt(clipPosition.x * scale),
|
|
|
+ clipY: parseInt(clipPosition.y * scale),
|
|
|
+ };
|
|
|
+
|
|
|
+ canvas.width = block.width;
|
|
|
+ canvas.height = block.height;
|
|
|
+
|
|
|
+ const ctx = canvas.getContext("2d");
|
|
|
+
|
|
|
+
|
|
|
+ setInterval(() => copyVideoStream(sourceGL, ctx, block), 1000 / 20);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function stopCopying() {
|
|
|
+ if (isCopying) {
|
|
|
+ isCopying = false;
|
|
|
+ clearInterval();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function copyVideoStream(gl, ctx, block) {
|
|
|
+ const { width, height, clipX, clipY } = block;
|
|
|
+ const bufferPixels = new Uint8Array(width * height * 4);
|
|
|
+ // 添加逻辑检查当前帧是否为空
|
|
|
+ gl.readPixels(
|
|
|
+ clipX,
|
|
|
+ clipY,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ gl.RGBA,
|
|
|
+ gl.UNSIGNED_BYTE,
|
|
|
+ bufferPixels
|
|
|
+ );
|
|
|
+ const isEmpty = bufferPixels.every((val) => val === 0);
|
|
|
+ if (isEmpty) {
|
|
|
+ return; // 如果帧为空,则直接返回,不进行后续处理
|
|
|
+ }
|
|
|
+
|
|
|
+ const imageData = ctx.createImageData(width, height);
|
|
|
+
|
|
|
+ imageData.data.set(bufferPixels);
|
|
|
+ ctx.putImageData(imageData, 0, 0);
|
|
|
+ // 沿着水平方向翻转并移动到底部
|
|
|
+ ctx.setTransform(1, 0, 0, -1, 0, height);
|
|
|
+ ctx.drawImage(canvas, 0, 0); // 绘制翻转后的画面
|
|
|
+ ctx.setTransform(1, 0, 0, 1, 0, 0); // 重置变换矩阵
|
|
|
+ }
|
|
|
+ }
|
|
|
+ window.LivePlayer = LivePlayer;
|
|
|
+ window.LiveSubPlayer = LiveSubPlayer;
|
|
|
+})();
|