Skip to content

Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.

Swift
5.8 KB
Raw Blame
import{transcode}from"./transcode/transcode.js";export class LibMedia{constructor(){}async init(){}async close(){}createMediaPlayer(){return new MediaPlayer}}function unlockMediaElement(e){const t=document.body,i=["touchstart","touchend","mousedown","keydown"];function n(){console.log("muted: unmuting"),e.muted=!1,i.forEach(e=>t.removeEventListener(e,n))}i.forEach(e=>t.addEventListener(e,n,!1))}function setMediaBlob(e,t,i){e.src&&e.src.startsWith("blob:")&&URL.revokeObjectURL(e.src);const n=URL.createObjectURL(t);console.log("objurl: creatin",i,n);const s=t=>{console.log("objurl: removin",i,n),URL.revokeObjectURL(n),e.removeEventListener("loadeddata",s),e.removeEventListener("error",s)};e.addEventListener("loadeddata",s),e.addEventListener("error",s),e.src=n}const mp3Types=["audio/mp3","audio/mpeg3","audio/x-mp3","audio/x-mpeg-3","audio/x-mpeg3"];export class MediaPlayer extends EventTarget{static snapshotCtx=null;static _finalizer=new FinalizationRegistry(e=>{console.warn("closing mediaplayer via finalizer"),URL.revokeObjectURL(e)});constructor(){super(),this.mediaElement=document.createElement("video"),this.mediaElement.controls=!0,this.mediaElement.style.display="none",this.mediaElement.addEventListener("playing",()=>{this.maybeSetupFrameDisplay()}),this.mediaElement.addEventListener("waiting",()=>{this.stopFrameDisplay()}),this.mediaElement.addEventListener("pause",()=>{this.stopFrameDisplay()}),this.mediaElement.addEventListener("ended",()=>{this.stopFrameDisplay(),this.dispatchEvent(new Event("end-of-media"))}),this.videoCtx=null,this.videoX=0,this.videoY=0,this.videoW=0,this.videoH=0,this.videoFullscreen=!1,this.videoAnimationFrame=null,this.onVideoFramePainter=null}async load(e,t=null){let i=null;e.byteLength;if(mp3Types.includes(t))i=new Blob([e],{type:"audio/mp3"});else{const t=await transcode(e);t&&(i=new Blob([t],{type:"video/mp4"}))}if(i){const e=this.mediaElement,t=new Promise(t=>{const i=n=>{e.removeEventListener("loadedmetadata",i),e.removeEventListener("error",i),t("loadedmetadata"===n.type)};e.addEventListener("loadedmetadata",i),e.addEventListener("error",i)});return this.objectUrl=URL.createObjectURL(i),this.constructor._finalizer.register(this,this.objectUrl,this),this.mediaElement.src=this.objectUrl,await t}return!1}maybeSetupFrameDisplay(){!this.mediaElement.paused&&this.mediaElement.readyState>2&&this.videoCtx&&this._drawVideoFrame()}stopFrameDisplay(){this.videoAnimationFrame&&(cancelAnimationFrame(this.videoAnimationFrame),this.videoAnimationFrame=null)}_drawVideoFrame(){if(this.videoCtx){try{const e=this.mediaElement.videoWidth,t=this.mediaElement.videoHeight;if(this.videoFullscreen){const i=this.videoCtx.canvas.width,n=this.videoCtx.canvas.height,s=e/t;let a,o,r,d;s>i/n?(a=i,o=i/s,r=0,d=(n-o)/2):(o=n,a=n*s,r=(i-a)/2,d=0),this.videoCtx.drawImage(this.mediaElement,r,d,a,o)}else this.videoCtx.drawImage(this.mediaElement,this.videoX,this.videoY,this.videoW||e,this.videoH||t);this.onVideoFramePainter()}catch(e){console.error("Error drawing video frame",e)}this.videoAnimationFrame=requestAnimationFrame(()=>this._drawVideoFrame())}}async play(){try{await this.mediaElement.play()}catch(e){if("NotAllowedError"==e.name){console.log("playing muted"),this.mediaElement.muted=!0,unlockMediaElement(this.mediaElement);try{await this.mediaElement.play()}catch(e){}}}}pause(){this.mediaElement.pause()}stop(){this.pause(),this.mediaElement.currentTime=0}get volume(){return this.mediaElement.volume}set volume(e){this.mediaElement.volume=e}get duration(){return this.mediaElement.duration}get position(){return this.mediaElement.currentTime}get videoWidth(){return this.mediaElement.videoWidth}get videoHeight(){return this.mediaElement.videoHeight}seek(e){this.mediaElement.currentTime=e}configureVideo(e,t,i=0,n=0,s=0,a=0,o=!1){e?(this.videoCtx=e,this.videoX=i,this.videoY=n,this.videoFullscreen=o,this.videoW=s,this.videoH=a,this.onVideoFramePainter=t,this.maybeSetupFrameDisplay()):(this.videoCtx=null,this.stopFrameDisplay())}setLooping(e){this.mediaElement.loop=!!e}close(){this.objectUrl&&URL.revokeObjectURL(this.objectUrl),this.constructor._finalizer.unregister(this)}async getSnapshot(e){if(!this.mediaElement.videoWidth||!this.mediaElement.videoHeight)return null;if(!this.constructor.snapshotCtx){const e=document.createElement("canvas");e.width=this.mediaElement.videoWidth,e.height=this.mediaElement.videoHeight,this.constructor.snapshotCtx=e.getContext("2d")}const t=this.constructor.snapshotCtx.canvas;return this.constructor.snapshotCtx.drawImage(this.mediaElement,0,0,t.width,t.height),await new Promise(i=>{t.toBlob(async e=>{i(e?await e.arrayBuffer():null)},e,.9)})}}export class FFPlayer extends EventTarget{static _unregister=([e,t,i])=>{e.send({cmd:"close"}),t.disconnect(),i.disconnect()};static _finalizer=new FinalizationRegistry(e=>{console.warn("closing ffplayer via finalizer"),this._unregister(e)});constructor(e,t){super(),this.gainNode=e.createGain(),this.gainNode.gain.value=1,this.gainNode.connect(t),this.node=new AudioWorkletNode(e,"ff-player",{outputChannelCount:[2]}),this.node.connect(this.gainNode),this.client=new CmdClient(this.node.port);const i=new WeakRef(this);this.node.port.onmessage=e=>{e.data?.replyFor||"end-of-media"===e.data&&(console.log("dispatchin eom"),i.deref()?.dispatchEvent(new Event("end-of-media")))},this.duration=0,this.loaded=!1,FFPlayer._finalizer.register(this,[this.client,this.node,this.gainNode],this)}send(e,t=[]){return this.client.send(e,t)}async load(e,t=null){const i=await this.send({cmd:"load",buffer:e,contentType:t});this.duration=i}play(){this.send({cmd:"play"})}loop(e){this.send({cmd:"loop",times:e})}stop(){this.send({cmd:"stop"})}getPosition(){return this.send({cmd:"getPosition"})}seek(e){return this.send({cmd:"seek",pos:e})}close(){FFPlayer._unregister([this.client,this.node,this.gainNode]),FFPlayer._finalizer.unregister(this)}get volume(){return this.gainNode.gain.value}set volume(e){this.gainNode.gain.value=e}}