| | |
| | | import { PCMPlayer } from './pcm-player' |
| | | import { WebGLPlayer } from './webgl' |
| | | import { PCMPlayer } from "./pcm-player"; |
| | | import { WebGLPlayer } from "./webgl"; |
| | | |
| | | export function Player() { |
| | | this.hPlayer = 0 |
| | | this.PlayOrPause = 0 |
| | | this.MediaName = null |
| | | this.isStream = false |
| | | this.canvas = null |
| | | this.timeLabel = null |
| | | this.timeTrack = null |
| | | this.CurPos = 0 |
| | | this.displayDuration = '00:00:00' |
| | | this.hPlayer = 0; |
| | | this.PlayOrPause = 0; |
| | | this.MediaName = null; |
| | | this.isStream = false; |
| | | this.canvas = null; |
| | | this.timeLabel = null; |
| | | this.timeTrack = null; |
| | | this.CurPos = 0; |
| | | this.displayDuration = "00:00:00"; |
| | | |
| | | this.pcmPlayer = null |
| | | this.webglPlayer = null |
| | | this.pcmPlayer = null; |
| | | this.webglPlayer = null; |
| | | |
| | | this.trackTimer = null |
| | | this.loop = false |
| | | this.preload = true |
| | | this.preloadFlag = 0 |
| | | this.trackTimer = null; |
| | | this.loop = false; |
| | | this.preload = true; |
| | | this.preloadFlag = 0; |
| | | this.statusCallback = (stat) => { |
| | | console.log(stat) |
| | | } |
| | | console.log(stat); |
| | | }; |
| | | } |
| | | |
| | | Player.prototype.play = function( |
| | |
| | | timeLabel, |
| | | payload |
| | | ) { |
| | | if (this.hPlayer != 0) return |
| | | if (this.hPlayer != 0) return; |
| | | |
| | | this.MediaName = url |
| | | this.MediaName = url; |
| | | |
| | | this.isStream = isStream |
| | | this.isStream = isStream; |
| | | |
| | | this.canvas = canvas //hWnd |
| | | this.canvas = canvas; //hWnd |
| | | |
| | | this.timeTrack = timeTrack |
| | | this.timeLabel = timeLabel |
| | | this.timeTrack = timeTrack; |
| | | this.timeLabel = timeLabel; |
| | | |
| | | var This = this |
| | | var This = this; |
| | | this.timeTrack.oninput = function() { |
| | | This.seek(This.timeTrack.value) |
| | | } |
| | | This.seek(This.timeTrack.value); |
| | | }; |
| | | |
| | | this.TurboWorker = new Worker('/libs/wasmPlayer/turbo.js') |
| | | this.hPlayer = 1 |
| | | this.PlayOrPause = 1 |
| | | this.TurboWorker = new Worker("/libs/wasmPlayer/turbo.js"); |
| | | this.hPlayer = 1; |
| | | this.PlayOrPause = 1; |
| | | |
| | | this.TurboWorker.onmessage = function(evt) { |
| | | switch (evt.data.command) { |
| | | case 'initialized': { |
| | | case "initialized": { |
| | | This.TurboWorker.postMessage({ |
| | | command: 'play', |
| | | command: "play", |
| | | media_name: url, |
| | | other: isStream, |
| | | payload: payload, |
| | | }) |
| | | break |
| | | }); |
| | | break; |
| | | } |
| | | case 'create_video': { |
| | | This.VideoParam = evt.data.param //{duration:xxx,width:xxx,height:xxx} |
| | | This.webglPlayer = new WebGLPlayer(This.canvas) |
| | | case "create_video": { |
| | | This.VideoParam = evt.data.param; //{duration:xxx,width:xxx,height:xxx} |
| | | This.webglPlayer = new WebGLPlayer(This.canvas); |
| | | if (This.timeTrack) { |
| | | This.timeTrack.min = 0 |
| | | This.timeTrack.max = This.VideoParam.duration |
| | | This.timeTrack.value = 0 |
| | | This.timeTrack.min = 0; |
| | | This.timeTrack.max = This.VideoParam.duration; |
| | | This.timeTrack.value = 0; |
| | | This.displayDuration = This.formatTime( |
| | | This.VideoParam.duration / 1000 |
| | | ) |
| | | ); |
| | | } |
| | | This.startTrackTimer() |
| | | This.startTrackTimer(); |
| | | |
| | | This.statusCallback(0) |
| | | break |
| | | This.statusCallback(0); |
| | | break; |
| | | } |
| | | case 'create_audio': { |
| | | This.AudioParam = evt.data.param //{channel:Channel,sample_rate:SampleRate} |
| | | case "create_audio": { |
| | | This.AudioParam = evt.data.param; //{channel:Channel,sample_rate:SampleRate} |
| | | This.pcmPlayer = new PCMPlayer({ |
| | | encoding: '16bitInt', |
| | | encoding: "16bitInt", |
| | | channels: This.AudioParam.channel, |
| | | sampleRate: This.AudioParam.sample_rate, |
| | | flushingTime: 5000, |
| | | }) |
| | | }); |
| | | |
| | | This.statusCallback(1) |
| | | break |
| | | This.statusCallback(1); |
| | | break; |
| | | } |
| | | case 'deliver_video': { |
| | | case "deliver_video": { |
| | | if (!This.isStream && This.preload && This.preloadFlag == 0) { |
| | | This.preloadFlag = 1 |
| | | This.pause() |
| | | This.statusCallback(0) |
| | | This.preloadFlag = 1; |
| | | This.pause(); |
| | | This.statusCallback(0); |
| | | } |
| | | |
| | | //sample:{time_stamp:xxx,data:xxx} |
| | | This.CurPos = evt.data.sample.time_stamp |
| | | This.CurPos = evt.data.sample.time_stamp; |
| | | This.webglPlayer.renderFrame( |
| | | evt.data.sample.buf, |
| | | This.VideoParam.width, |
| | | This.VideoParam.height |
| | | ) |
| | | break |
| | | ); |
| | | break; |
| | | } |
| | | case 'deliver_audio': { |
| | | case "deliver_audio": { |
| | | //sample:{time_stamp:xxx,data:xxx} |
| | | This.pcmPlayer.play(evt.data.sample.buf) |
| | | break |
| | | This.pcmPlayer.play(evt.data.sample.buf); |
| | | break; |
| | | } |
| | | case 'play_failed': { |
| | | This.statusCallback(-1) |
| | | This.stop() |
| | | break |
| | | case "play_failed": { |
| | | This.statusCallback(-1); |
| | | This.stop(); |
| | | break; |
| | | } |
| | | case 'play_end': { |
| | | case "play_end": { |
| | | if (This.loop) { |
| | | This.seek(0) |
| | | This.seek(0); |
| | | } |
| | | break |
| | | break; |
| | | } |
| | | |
| | | default: |
| | | return |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | }; |
| | | |
| | | Player.prototype.pause = function() { |
| | | if (this.hPlayer == 0 || this.PlayOrPause == 0) return |
| | | this.PlayOrPause = 0 |
| | | this.TurboWorker.postMessage({ command: 'pause' }) |
| | | } |
| | | if (this.hPlayer == 0 || this.PlayOrPause == 0) return; |
| | | this.PlayOrPause = 0; |
| | | this.TurboWorker.postMessage({ command: "pause" }); |
| | | }; |
| | | |
| | | Player.prototype.resume = function() { |
| | | if (this.hPlayer == 0 || this.PlayOrPause == 1) return |
| | | this.PlayOrPause = 1 |
| | | this.TurboWorker.postMessage({ command: 'resume' }) |
| | | } |
| | | if (this.hPlayer == 0 || this.PlayOrPause == 1) return; |
| | | this.PlayOrPause = 1; |
| | | this.TurboWorker.postMessage({ command: "resume" }); |
| | | }; |
| | | |
| | | Player.prototype.stop = function() { |
| | | if (this.hPlayer == 0) return |
| | | this.hPlayer = 0 |
| | | this.TurboWorker.postMessage({ command: 'stop' }) |
| | | this.stopTrackTimer() |
| | | if (this.hPlayer == 0) return; |
| | | this.hPlayer = 0; |
| | | this.TurboWorker.postMessage({ command: "stop" }); |
| | | this.stopTrackTimer(); |
| | | if (this.pcmPlayer) { |
| | | this.pcmPlayer.destroy() |
| | | this.pcmPlayer = null |
| | | this.pcmPlayer.destroy(); |
| | | this.pcmPlayer = null; |
| | | } |
| | | //webglPlayer? |
| | | } |
| | | }; |
| | | |
| | | Player.prototype.seek = function(ms) { |
| | | if (this.hPlayer == 0) return |
| | | this.TurboWorker.postMessage({ command: 'seek', pos: ms }) |
| | | if (this.hPlayer == 0) return; |
| | | this.TurboWorker.postMessage({ command: "seek", pos: ms }); |
| | | |
| | | if (this.pcmPlayer != null) { |
| | | this.pcmPlayer.destroy() |
| | | this.pcmPlayer.destroy(); |
| | | this.pcmPlayer = new PCMPlayer({ |
| | | encoding: '16bitInt', |
| | | encoding: "16bitInt", |
| | | channels: this.AudioParam.channel, |
| | | sampleRate: this.AudioParam.sample_rate, |
| | | flushingTime: 5000, |
| | | }) |
| | | }); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | Player.prototype.fullscreen = function() { |
| | | if (this.webglPlayer) this.webglPlayer.fullscreen() |
| | | } |
| | | if (this.webglPlayer) this.webglPlayer.fullscreen(); |
| | | }; |
| | | |
| | | Player.prototype.exitfullscreen = function() { |
| | | if (this.webglPlayer) this.webglPlayer.exitfullscreen() |
| | | } |
| | | |
| | | if (this.webglPlayer) this.webglPlayer.exitfullscreen(); |
| | | }; |
| | | |
| | | Player.prototype.startTrackTimer = function() { |
| | | var This = this |
| | | var This = this; |
| | | this.trackTimer = setInterval(function() { |
| | | if (This.timeTrack) This.timeTrack.value = This.CurPos |
| | | if (This.timeTrack) This.timeTrack.value = This.CurPos; |
| | | |
| | | if (This.timeLabel) |
| | | This.timeLabel.innerHTML = |
| | | This.formatTime(This.CurPos / 1000) + '/' + This.displayDuration |
| | | }, 500) |
| | | } |
| | | This.formatTime(This.CurPos / 1000) + "/" + This.displayDuration; |
| | | }, 500); |
| | | }; |
| | | |
| | | Player.prototype.stopTrackTimer = function() { |
| | | if (this.trackTimer != null) { |
| | | clearInterval(this.trackTimer) |
| | | this.trackTimer = null |
| | | clearInterval(this.trackTimer); |
| | | this.trackTimer = null; |
| | | } |
| | | } |
| | | }; |
| | | |
| | | Player.prototype.formatTime = function(s) { |
| | | var h = |
| | | Math.floor(s / 3600) < 10 |
| | | ? '0' + Math.floor(s / 3600) |
| | | : Math.floor(s / 3600) |
| | | ? "0" + Math.floor(s / 3600) |
| | | : Math.floor(s / 3600); |
| | | var m = |
| | | Math.floor((s / 60) % 60) < 10 |
| | | ? '0' + Math.floor((s / 60) % 60) |
| | | : Math.floor((s / 60) % 60) |
| | | ? "0" + Math.floor((s / 60) % 60) |
| | | : Math.floor((s / 60) % 60); |
| | | var s = |
| | | Math.floor(s % 60) < 10 ? '0' + Math.floor(s % 60) : Math.floor(s % 60) |
| | | Math.floor(s % 60) < 10 ? "0" + Math.floor(s % 60) : Math.floor(s % 60); |
| | | |
| | | return h + ':' + m + ':' + s |
| | | } |
| | | return h + ":" + m + ":" + s; |
| | | }; |