|
| 1 | +var LibraryFixWebCamWebGL = { |
| 2 | + $webcamBufferToTextureTable: {}, |
| 3 | + $webcamLatestTextureId: 0, |
| 4 | + $disableNextSubImage: false, |
| 5 | + |
| 6 | + glTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, type, pixels) { |
| 7 | + if (disableNextSubImage) { |
| 8 | + disableNextSubImage = false; |
| 9 | + return; |
| 10 | + } |
| 11 | + if (GL.currentContext.supportsWebGL2EntryPoints) { |
| 12 | + if (GLctx.currentPixelUnpackBufferBinding) { |
| 13 | + GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels) |
| 14 | + } else if (pixels != 0) { |
| 15 | + GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type)) |
| 16 | + } else { |
| 17 | + GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, null) |
| 18 | + } |
| 19 | + return |
| 20 | + } |
| 21 | + var pixelData = null; |
| 22 | + if (pixels) |
| 23 | + pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, 0); |
| 24 | + GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixelData) |
| 25 | + }, |
| 26 | + |
| 27 | + JS_WebCamVideo_SetLatestTextureId: function(textureId) { |
| 28 | + webcamLatestTextureId = textureId; |
| 29 | + }, |
| 30 | + |
| 31 | + JS_WebCamVideo_RemoveWhereTextureId: function(textureId) { |
| 32 | + Object.entries(webcamBufferToTextureTable).forEach(function (pair) { |
| 33 | + if (pair[1] == textureId) { |
| 34 | + delete webcamBufferToTextureTable[pair[0]]; |
| 35 | + } |
| 36 | + }); |
| 37 | + }, |
| 38 | + |
| 39 | + JS_WebCamVideo_GrabFrame: function(deviceId, buffer, destWidth, destHeight) { |
| 40 | + var videoElement; |
| 41 | + if (typeof activeWebCams !== "undefined") { |
| 42 | + var webcamDevice = activeWebCams[deviceId]; |
| 43 | + if (!webcamDevice) |
| 44 | + return; |
| 45 | + var timeNow = performance.now(); |
| 46 | + if (timeNow < webcamDevice.nextFrameAvailableTime) { |
| 47 | + return; |
| 48 | + } |
| 49 | + webcamDevice.nextFrameAvailableTime += webcamDevice.frameLengthInMsecs; |
| 50 | + if (webcamDevice.nextFrameAvailableTime < timeNow) { |
| 51 | + webcamDevice.nextFrameAvailableTime = timeNow + webcamDevice.frameLengthInMsecs |
| 52 | + } |
| 53 | + videoElement = webcamDevice.video; |
| 54 | + } |
| 55 | + if (typeof MediaDevices !== "undefined") { |
| 56 | + if (!MediaDevices[deviceId].video) { |
| 57 | + console.error("WebCam not initialized."); |
| 58 | + return; |
| 59 | + } |
| 60 | + videoElement = MediaDevices[deviceId].video; |
| 61 | + } |
| 62 | + if (!webcamBufferToTextureTable[buffer]) { |
| 63 | + if (!webcamLatestTextureId) { |
| 64 | + return; |
| 65 | + } |
| 66 | + webcamBufferToTextureTable[buffer] = webcamLatestTextureId; |
| 67 | + GLctx.deleteTexture(GL.textures[webcamBufferToTextureTable[buffer]]); |
| 68 | + var t = GLctx.createTexture(); |
| 69 | + t.name = webcamBufferToTextureTable[buffer]; |
| 70 | + GL.textures[webcamBufferToTextureTable[buffer]] = t; |
| 71 | + GLctx.bindTexture(GLctx.TEXTURE_2D, t); |
| 72 | + GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_S, GLctx.CLAMP_TO_EDGE); |
| 73 | + GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_T, GLctx.CLAMP_TO_EDGE); |
| 74 | + GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MIN_FILTER, GLctx.LINEAR); |
| 75 | + webcamLatestTextureId = 0 |
| 76 | + } else { |
| 77 | + GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[webcamBufferToTextureTable[buffer]]) |
| 78 | + } |
| 79 | + GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true); |
| 80 | + GLctx.texImage2D(GLctx.TEXTURE_2D, 0, GLctx.RGBA, GLctx.RGBA, GLctx.UNSIGNED_BYTE, videoElement); |
| 81 | + GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false); |
| 82 | + disableNextSubImage = true; |
| 83 | + return 1; |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +autoAddDeps(LibraryFixWebCamWebGL, '$webcamBufferToTextureTable'); |
| 88 | +autoAddDeps(LibraryFixWebCamWebGL, '$webcamLatestTextureId'); |
| 89 | +autoAddDeps(LibraryFixWebCamWebGL, '$disableNextSubImage'); |
| 90 | +mergeInto(LibraryManager.library, LibraryFixWebCamWebGL); |
0 commit comments