From 2fdfcc94d388d062ec4ce357d9bf7c2eb19faabb Mon Sep 17 00:00:00 2001 From: ToadKing Date: Sat, 9 Nov 2013 00:16:28 -0500 Subject: [PATCH] webgl webcam test driver doesn't work on firefox, see https://bugzilla.mozilla.org/show_bug.cgi?id=917314 --- Makefile.emscripten | 2 +- emscripten/library_rwebcam.js | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 emscripten/library_rwebcam.js diff --git a/Makefile.emscripten b/Makefile.emscripten index 1092e3dfb5..60fe8748c0 100644 --- a/Makefile.emscripten +++ b/Makefile.emscripten @@ -59,7 +59,7 @@ libretro = libretro_emscripten.bc LIBS = -lm DEFINES = -DHAVE_SCREENSHOTS -DHAVE_NULLAUDIO -DHAVE_BSV_MOVIE -LDFLAGS = -L. -s TOTAL_MEMORY=$(MEMORY) --js-library emscripten/library_rwebaudio.js --js-library emscripten/library_rwebinput.js +LDFLAGS = -L. -s TOTAL_MEMORY=$(MEMORY) --js-library emscripten/library_rwebaudio.js --js-library emscripten/library_rwebinput.js --js-library emscripten/library_rwebcam.js ifeq ($(PERF_TEST), 1) DEFINES += -DPERF_TEST diff --git a/emscripten/library_rwebcam.js b/emscripten/library_rwebcam.js new file mode 100644 index 0000000000..255396d633 --- /dev/null +++ b/emscripten/library_rwebcam.js @@ -0,0 +1,70 @@ +//"use strict"; + +var LibraryRWebCam = { + $RWC: { + /* + run modes: + 0: not running + 1: waiting for user to confirm webcam + 2: running + */ + runMode: 0, + videoElement: null + }, + + RWebCamInit: function() { + RWC.runMode = 0; + + if (!navigator) return 0; + + navigator.getMedia = navigator.getUserMedia || + navigator.webkitGetUserMedia || + navigator.mozGetUserMedia || + navigator.msGetUserMedia; + + if (!navigator.getMedia) return 0; + + RWC.videoElement = document.createElement("video"); + + RWC.runMode = 1; + + navigator.getMedia({video: true, audio: false}, function(stream) { + RWC.videoElement.autoplay = true; + RWC.videoElement.src = URL.createObjectURL(stream); + RWC.runMode = 2; + }, function (err) { + console.log("webcam request failed", err); + RWC.runMode = 0; + }); + + return 1; + }, + + RWebCamTexImage2D: function(width, height) { + if (RWC.runMode != 2 || RWC.videoElement.paused) return 0; + + Module.ctx.texImage2D(Module.ctx.TEXTURE_2D, 0, Module.ctx.RGB, Module.ctx.RGB, Module.ctx.UNSIGNED_BYTE, RWC.videoElement); + + if (width) {{{ makeSetValue('width', '0', 'RWC.videoElement.videoWidth', 'i32') }}}; + if (height) {{{ makeSetValue('height', '0', 'RWC.videoElement.videoHeight', 'i32') }}}; + }, + + RWebCamTexSubImage2D: function(x, y) { + if (RWC.runMode != 2 || RWC.videoElement.paused) return 0; + + Module.ctx.texSubImage2D(Module.ctx.TEXTURE_2D, 0, x, y, Module.ctx.RGB, Module.ctx.UNSIGNED_BYTE, RWC.videoElement); + }, + + RWebCamReady: function() { + return RWC.runMode == 2 ? 1 : 0; + }, + + RWebCamFree: function() { + RWC.videoElement.pause(); + RWC.videoElement = null; + RWC.runMode = 0; + } +}; + +autoAddDeps(LibraryRWebCam, '$RWC'); +mergeInto(LibraryManager.library, LibraryRWebCam);