diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..23b8d3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +build/ +npm-debug.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..da33b3c --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +The MIT License (MIT) +Copyright (c) 2016 Ricoh Company, Ltd. +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e81ed47 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Ricoh Video Streaming Sample + +Video streaming API sample app. + +## Requirements + +* Google Chrome 49 or newer. +* Web Camera accessible from your browser. +* Enable Web Camera access in your browser setting. + +You'll also need + +* Ricoh API Client Credentials (client_id & client_secret) +* Ricoh ID (user_id & password) + +If you don't have them, please register them at [THETA Developers Website](http://contest.theta360.com/). + +## Setup + +```sh +$ git clone https://github.com/ricohapi/video-streaming-js.git +$ cd video-streaming-js +$ cp samples/config_template.js samples/config.js +``` + +and put your credentials into the `config.js`. + +## Build + +```sh +$ npm install +$ gulp build +``` + +## Video Streaming + +Connect the Web Camera and execute `gulp run`, then the browser will be opened. +Select the Web Camera, put your Ricoh ID & password, and submit the login button. +Let the peer user login on his own device following the instruction above, then put the peer's User ID to the Peer-ID field and submit Connect button, then streaming connection will start between you and the peer. + +```sh +$ gulp run +``` + +## THETA View + +If the peer user is using THETA, push THETA View button, then you'll see the draggable & zoomable 360° view. diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..4a6c1f2 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,20 @@ +var gulp = require('gulp'), + webserver = require('gulp-webserver'), + webpack = require('webpack-stream'), + webpackConfig = require('./webpack.config.js'); + +gulp.task('run', function () { + gulp.src('') + .pipe(webserver({ + host: 'localhost', + port: 8034, + open: true, + fallback: 'samples/index.html' + })); +}); + +gulp.task('build', function() { + return gulp.src('') + .pipe(webpack(webpackConfig)) + .pipe(gulp.dest('')); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..601fef7 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "ricohapi-video-streaming-sample", + "version": "1.0.0", + "description": "RICOH API video streaming sample", + "main": "index.html", + "dependencies": { + "ricohapi-auth": "1.0.1" + }, + "devDependencies": { + "gulp": "^3.9.1", + "webpack-stream": "^3.1.0", + "gulp-webserver": "^0.9.1", + "babel-loader": "^6.2.4", + "babel-core": "^6.7.0", + "babel-preset-es2015": "^6.6.0" + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/ricohapi/video-streaming-js.git" + }, + "keywords": [], + "bugs": { + "url": "https://github.com/ricohapi/video-streaming-js/issues" + }, + "homepage": "https://github.com/ricohapi/video-streaming-js" +} diff --git a/samples/.gitignore b/samples/.gitignore new file mode 100644 index 0000000..1bf4259 --- /dev/null +++ b/samples/.gitignore @@ -0,0 +1 @@ +config.js diff --git a/samples/UDCStrophe.js b/samples/UDCStrophe.js new file mode 100644 index 0000000..db47bc3 --- /dev/null +++ b/samples/UDCStrophe.js @@ -0,0 +1,64 @@ +'use strict'; +/** + * Copyright (c) 2016 Ricoh Company, Ltd. All Rights Reserved. + * See LICENSE for more information + */ + +const AuthClient = require('ricohapi-auth').AuthClient; + +function _log(str) { + console.log(str); +} + +class UDCStrophe { + constructor(clientID, clientSecret) { + this._client = new AuthClient(clientID, clientSecret); + } + + _onConnect(self, status) { + if (status == Strophe.Status.CONNECTING) { + _log('Strophe is connecting.'); + } else if (status == Strophe.Status.CONNFAIL) { + _log('Strophe failed to connect.'); + self.connectReject(); + } else if (status == Strophe.Status.DISCONNECTING) { + _log('Strophe is disconnecting.'); + } else if (status == Strophe.Status.DISCONNECTED) { + _log('Strophe is disconnected.'); + } else if (status == Strophe.Status.CONNECTED) { + _log('Strophe is connected.'); + self.connection.send($pres()); + self.connectResolve(); + } else if (status == Strophe.Status.AUTHENTICATING) { + _log('Strophe is authenticating.'); + } else if (status == Strophe.Status.AUTHFAIL) { + _log('Strophe is authfail.'); + self.connectReject(); + } + } + + connect(userID, userPass) { + this._client.setResourceOwnerCreds(userID.split('+')[0], userPass.split('+')[0]); + this.id = userID; + + return new Promise((resolve, reject) => { + this._client.session(AuthClient.SCOPES.VStream) + .then(() => { + this.connection = new Strophe.Connection('https://sig.ricohapi.com/http-bind/'); + Strophe.SASLSHA1.test = () => false; + this.connectResolve = resolve; + this.connectReject = reject; + this.connection.connect(userID.replace(/@/g, '\\40') + '@sig.ricohapi.com@sig.ricohapi.com/peer', this._client.accessToken, this._onConnect.bind(this, this)); + }) + .catch(e => reject(e)); + }); + } + + disconnect() { + if (this.connection === undefined) return; + this.connection.disconnect('none'); + this.connection = undefined; + } + +}; +exports.UDCStrophe = UDCStrophe; diff --git a/samples/config_template.js b/samples/config_template.js new file mode 100644 index 0000000..1b0f4b0 --- /dev/null +++ b/samples/config_template.js @@ -0,0 +1,8 @@ +'use strict'; + +const CONFIG = { + clientId: '', + clientSecret: '', +}; + +module.exports.CONFIG = CONFIG; diff --git a/samples/index.html b/samples/index.html new file mode 100644 index 0000000..6610419 --- /dev/null +++ b/samples/index.html @@ -0,0 +1,106 @@ + + + + + + Video streaming sample + + + + +
+
+

Video streaming sample

+

ID: +
+ +

+

PASS: +
+ +

+

Choose your webcam:

+
+
+
+
S
+
e
+
a
+
r
+
c
+
h
+
i
+
n
+
g
+
.
+
+
+ + +

Please click `allow` on the top of the screen so we can access your webcam for calls.

+ + + +
+
+ + + + + + + + diff --git a/samples/main.js b/samples/main.js new file mode 100644 index 0000000..8f23337 --- /dev/null +++ b/samples/main.js @@ -0,0 +1,237 @@ +'use strict'; +/* + * Copyright (c) 2016 Ricoh Company, Ltd. All Rights Reserved. + * See LICENSE for more information + * + * main.js for browser sample + */ + +const UDCStrophe = require('./UDCStrophe').UDCStrophe; +const StrophePeer = require('./strophePeer').StrophePeer; +const Webcam = require('./webcam').Webcam; +const ThetaView = require('./thetaview').ThetaView; +const CONFIG = require('./config').CONFIG; + +const xmpp = new UDCStrophe(CONFIG.clientId, CONFIG.clientSecret); +const webrtc = new StrophePeer(); +const webcam = new Webcam(); +const thetaview = new ThetaView(); + +let _view = 'INITIAL'; +let _app = {}; +let _thetatimer = null; + +function hide(id) { + const elm = document.querySelector(id); + if (elm.style.display === '') elm.style.display = 'none'; +} + +function show(id) { + const elm = document.querySelector(id); + if (elm.style.display === 'none') elm.style.display = ''; +} + +function getSelected(id) { + const ret = Array.prototype.slice.call(document.getElementsByName(id)) + .filter(o => (o.checked))[0]; + return ret? ret.value : undefined; +} + +function camera2elm(camera) { + const radio = document.createElement('input'); + radio.setAttribute('type', 'radio'); + radio.setAttribute('name', 'radio'); + radio.setAttribute('value', camera.id); + const label = document.createElement('label'); + label.appendChild(radio); + label.appendChild(document.createTextNode(camera.label)); + return label; +} + +function nextView(view, evt) { + const SM_DEFINE = { + INITIAL: { + ok: 'CAMERA_READY', + ng: 'CAMERA_ERROR', + }, + CAMERA_READY: { + evtClickLogin: 'LOGING_IN', + }, + CAMERA_ERROR: { + // dead end + }, + LOGING_IN: { + ok: 'READY', + ng: 'CAMERA_READY', + }, + READY: { + evtClickConnect: 'CONNECTING', + evtOnCall: 'CONNECTING', + evtClickLogout: 'CAMERA_READY', + }, + CONNECTING: { + evtArrivePeer: 'CHATTING', + evtClickLogout: 'CAMERA_READY', + }, + CHATTING: { + evtClickLogout: 'CAMERA_READY', + evtThetaView: 'CHATTING', + } + }; + return SM_DEFINE[view][evt]; +} + +// DOM read write +function update(view, app) { + if (view === 'CAMERA_READY') { + if (app.cameras.length === 0) { + show('#nocamera'); + return; // dead end + } + if (app.error) { + show('#loginerr'); + app.error = false; + } + const radios = document.getElementById('cameras'); + radios.innerHTML = ''; + radios.appendChild( + app.cameras.map(camera2elm) + .reduce((fragment, elm) => fragment.appendChild(elm).parentNode, + document.createDocumentFragment())).parentNode; + ['#viewpage', '#loader', '#loader2'].forEach(hide); + ['#loginpage', '#login'].forEach(show); + } else if (view === 'CAMERA_ERROR') { + ['#loader'].forEach(hide); + ['#camerr'].forEach(show); + } else if (view === 'LOGING_IN') { + app.id = document.querySelector('#id').value; + app.pass = document.querySelector('#pass').value; + ['#loginerr'].forEach(hide); + ['#loader2'].forEach(show); + } else if (view === 'READY') { + document.querySelector('#my-video').src = app.myVideoUrl; + document.querySelector('#myid').textContent = app.xmppid; + ['#loginpage', '#loader2', '#loader3'].forEach(hide); + ['#viewpage', '#connect'].forEach(show); + } else if (view === 'CONNECTING') { + app.peerId = document.querySelector('#peer-id').value; + ['#loader3'].forEach(show); + } else if (view === 'CHATTING') { + app.peerW = document.querySelector('#peer-container').clientWidth; + app.peerH = document.querySelector('#peer-container').clientHeight; + document.querySelector('#peer-video').src = app.peerVideoUrl; + document.querySelector('#peer-video').style.width = app.peerW + 'px'; + app.peerVideoDOM = document.getElementById('peer-video'); + app.peerContainerDOM = document.getElementById('peer-container'); + if (app.thetaMode) { + hide('#peer-video'); + } else { + show('#peer-video'); + } + ['#connect','#loader3'].forEach(hide); + } +} + +function occur(evt) { + _view = nextView(_view, evt); + update(_view, _app); +} + +function waitCall(call) { + call.on('stream', s => { + _app.peerVideoUrl = URL.createObjectURL(s); + occur('evtArrivePeer'); + }); +} + +function stopThetaView() { + if (_thetatimer) return; + cancelAnimationFrame(_thetatimer); + _thetatimer = undefined; +} + +function quit() { + stopThetaView(); + if (_app.peerVideoUrl) URL.revokeObjectURL(_app.peerVideoUrl); + if (_app.myVideoUrl) URL.revokeObjectURL(_app.myVideoUrl); + webrtc.stopStream(); + webrtc.disconnect(); + xmpp.disconnect(); +} + +function animate() { + _thetatimer = requestAnimationFrame(animate); + thetaview.animate(); +} + +document.addEventListener('DOMContentLoaded', () => { + + webrtc.on('error', e => console.error(e)); + + webrtc.on('call', call => { + occur('evtOnCall'); + webrtc.answer(call); + waitCall(call); + }); + + window.onload = () => { + webcam.getUserMedia() // for force allow dialog. + .then(() => webcam.init()) + .then(cameras => { + _app.cameras = cameras; + occur('ok'); + }) + .catch(() => occur('ng')); + }; + + window.onresize = () => { + const w = document.querySelector('#peer-container').clientWidth; + const h = document.querySelector('#peer-container').clientHeight; + document.querySelector('#peer-video').style.width = w + 'px'; + document.querySelector('#peer-video').style.height = h + 'px'; + thetaview.resize(w, h); + }; + + document.querySelector('#login').addEventListener('click', () => { + occur('evtClickLogin'); + xmpp.connect(_app.id, _app.pass) + .then(() => webrtc.connect(xmpp.connection)) + .then(() => webcam.getUserMedia(getSelected('radio'))) + .then(s => { + _app.xmppid = xmpp.id; + _app.myVideoUrl = URL.createObjectURL(s); + webrtc.stopStream(); + webrtc.setStream(s); + occur('ok'); + }) + .catch(e => { + console.error(e); + _app.error = true; + occur('ng'); + }); + }); + + document.querySelector('#connect').addEventListener('click', () => { + occur('evtClickConnect'); + waitCall(webrtc.call(_app.peerId)); + }); + + + document.querySelector('#logout').addEventListener('click', () => { + occur('evtClickLogout'); + quit(); + }); + + document.querySelector('#theta').addEventListener("click", () => { + _app.thetaMode = !_app.thetaMode; + occur('evtThetaView'); + if (_app.thetaMode) { + thetaview.start(_app.peerVideoDOM, _app.peerContainerDOM, _app.peerW, _app.peerH); + animate(); + } else { + stopThetaView(); + thetaview.stop(_app.peerContainerDOM); + } + }); + +}); diff --git a/samples/strophePeer.js b/samples/strophePeer.js new file mode 100644 index 0000000..848bec0 --- /dev/null +++ b/samples/strophePeer.js @@ -0,0 +1,112 @@ +'use strict'; + +/** + * Copyright (c) 2016 Ricoh Company, Ltd. All Rights Reserved. + * See LICENSE for more information + * + * use xmpp(strophe.connection) on peerjs + * override peerjs Sock class + */ + +const EventEmitter = require('events'); + +function _log(typ, obj) { +// console.log({ type: typ, data: obj }); +} + +class StrophePeer extends EventEmitter { + + constructor() { + super(); + } + + connect(strophe, cid) { + _log('log', 'connect called'); + this.strophe = strophe; + + return new Promise((resolve, reject) => { + + // inject strophe in peerJS + Socket.prototype.close = () => {}; + Socket.prototype.start = () => {}; + Socket.prototype.send = data => { + _log('send', data) + this.strophe.send($msg({ + to: data.dst + '@sig.ricohapi.com' + }).t(JSON.stringify(data))); + } + + this.peer = new Peer(cid); + this.peer.on('open', () => { + _log('log', 'webrtc connected') + resolve(); + }); + this.peer.on('error', reject); + this.peer.on('call', call => this.emit('call', call)); + + const msgHandler = message => { + _log('onMessage', message) + try { + // change XMPP message for PeerJS + const id = Strophe.getBareJidFromJid(message.attributes['from'].value).split('@')[0]; + // to judge if peerjs message or not, and fire events for each listeners. + // + // { type: 'OPEN' | 'OFFER' | 'ANSWER' | 'CANDIDATE' + // src: jid + // ... + // } + // + if (message.firstChild.nodeType == 3 /* NODE.TEXT_NODE */ ) { + const json = JSON.parse(message.firstChild.textContent); + if (json.type) { + json.src = id; + this.peer.socket.emit('message', json); + } + } + } catch (e) { + _log(e); + } + return true; + }; + this.strophe.addHandler(msgHandler, null, 'message', null); + this.peer.socket.emit('message', { "type": "OPEN" }); + }); + } + + disconnect() { + this.peer.disconnect(); + } + + setStream(stream) { + this._localStream = stream; + }; + + call(cid) { + _log('log', 'call called'); + if (!this._localStream) { + _log('log', 'no localstream'); + return; + } + return this.peer.call(cid.replace(/@/g, '\\40'), this._localStream); + } + + answer(call) { + if (!this._localStream) { + _log('log', 'no localstream'); + return; + } + call.answer(this._localStream); + } + + stopStream() { + if (!this._localStream) { + _log('log', 'no localstream'); + return; + } + this._localStream.getVideoTracks()[0].stop(); + this._localStream = undefined; + }; + +}; + +exports.StrophePeer = StrophePeer; diff --git a/samples/styles/style.css b/samples/styles/style.css new file mode 100644 index 0000000..e2a93ff --- /dev/null +++ b/samples/styles/style.css @@ -0,0 +1,165 @@ +.wrapper { + width: 98%; + margin: 0 auto; +} + +.loginform { + width: 400px; + margin: 30px auto; + padding: 20px; + border: 1px solid #555; +} + +input[type="text"],input[type="password"] { + width: 300px; + padding: 4px; + font-size: 14px; +} + +.header { + float: left; + height: 10px; + width: 100%; +} + +.wrapleft { + float: left; + width: 100%; +} + +.left { + margin-right: 387px; +} + +#peer-container { + background-color: #111; + height: 800px; +} + +.right { + float: right; + width: 353px; + margin-top: 30px; + margin-left: -369px; + background-color: #eee; + height: auto; + padding: 8px; +} + +.footer { + float: left; + width: 100%; +} + +#camerr,#nocamera,#loginerr { + color: #F33; +} + +.li-avail { + color: white; + border-radius: 4px; + text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); + background: rgb(28, 184, 65); +} + +body { + padding: 0px; + margin: 10px; + height: 1000px; + font-family: 'sans-selif'; +} + +button { + color: white; + border-radius: 4px; + text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); + background: rgb(28, 184, 65); + border: 2px; + margin: 2px; + padding: 6px; +} + +button:active { + transform: scale(0.95); +} + +li { + list-style-position: inside; + border: 1px solid #ccc; + list-style-type: none; + border-radius: 4px; + margin: 10px; + padding: 8px; +} + +label { + display: block; +} + +/* http://codepen.io/Manoz/pen/pydxK */ +/* https://blog.codepen.io/legal/licensing/ */ +.letter-holder { + padding: 8px; + margin-bottom: 30px; +} + +.letter { + float: left; + color: #777; +} + +.load-6 .letter { + animation-name: loadingF; + animation-duration: 1.6s; + animation-iteration-count: infinite; + animation-direction: linear; +} + +.l-1 { + animation-delay: .48s; +} + +.l-2 { + animation-delay: .6s; +} + +.l-3 { + animation-delay: .72s; +} + +.l-4 { + animation-delay: .84s; +} + +.l-5 { + animation-delay: .96s; +} + +.l-6 { + animation-delay: 1.08s; +} + +.l-7 { + animation-delay: 1.2s; +} + +.l-8 { + animation-delay: 1.32s; +} + +.l-9 { + animation-delay: 1.44s; +} + +.l-10 { + animation-delay: 1.56s; +} + +@keyframes loadingF { + 0% { + opacity: 0; + } + 100% { + opacity: 1; + } +} diff --git a/samples/thetaview.js b/samples/thetaview.js new file mode 100644 index 0000000..8c1d8c6 --- /dev/null +++ b/samples/thetaview.js @@ -0,0 +1,190 @@ +/* global THREE */ +'use strict'; + +/** + * Copyright (c) 2016 Ricoh Company, Ltd. All Rights Reserved. + * See LICENSE for more information + */ + +class ThetaView { + + _cyln2world(a, e) { + return (new THREE.Vector3( + Math.cos(e) * Math.cos(a), + Math.cos(e) * Math.sin(a), + Math.sin(e))); + } + + _world2fish(x, y, z) { + let nz = z; + if (z < -1.0) nz = -1.0; + else if (z > 1.0) nz = 1.0; + return (new THREE.Vector2( + Math.atan2(y, x), + Math.acos(nz) / Math.PI)); // 0.0 to 1.0 + } + + _calcTexUv(i, j, lens) { + const world = this._cyln2world( + ((i + 90) / 180.0 - 1.0) * Math.PI, // rotate 90 deg for polygon + (0.5 - j / 180.0) * Math.PI); + const ar = this._world2fish( + Math.sin(-0.5 * Math.PI) * world.z + Math.cos(-0.5 * Math.PI) * world.x, + world.y, + Math.cos(-0.5 * Math.PI) * world.z - Math.sin(-0.5 * Math.PI) * world.x); + + const fishRad = 0.883; + const fishRad2 = fishRad * 0.88888888888888; + const fishCenter = 1.0 - 0.44444444444444; + const ret = (lens === 0) ? + (new THREE.Vector2( + fishRad * ar.y * Math.cos(ar.x) * 0.5 + 0.25, + fishRad2 * ar.y * Math.sin(ar.x) + fishCenter)) : + (new THREE.Vector2( + fishRad * (1.0 - ar.y) * Math.cos(-1.0 * ar.x + Math.PI) * 0.5 + 0.75, + fishRad2 * (1.0 - ar.y) * Math.sin(-1.0 * ar.x + Math.PI) + fishCenter)); + return ret; + } + + _createGeometry() { + const geometry = new THREE.Geometry(); + + const uvs = []; + for (let j = 0; j <= 180; j += 5) { + for (let i = 0; i <= 360; i += 5) { + geometry.vertices.push(new THREE.Vector3( + Math.sin(Math.PI * j / 180.0) * Math.sin(Math.PI * i / 180.0) * 500.0, + Math.cos(Math.PI * j / 180.0) * 500.0, + Math.sin(Math.PI * j / 180.0) * Math.cos(Math.PI * i / 180.0) * 500.0)); + } + /* devide texture */ + for (let k = 0; k <= 180; k += 5) { + uvs.push(this._calcTexUv(k, j, 0)); + } + for (let l = 180; l <= 360; l += 5) { + uvs.push(this._calcTexUv(l, j, 1)); + } + } + + for (let m = 0; m < 36; m++) { + for (let n = 0; n < 72; n++) { + const v = m * 73 + n; + geometry.faces.push( + new THREE.Face3(v + 0, v + 1, v + 73, null, null, 0), + new THREE.Face3(v + 1, v + 74, v + 73, null, null, 0)); + const t = (n < 36) ? m * 74 + n : m * 74 + n + 1; + + geometry.faceVertexUvs[0].push( + [uvs[t + 0], uvs[t + 1], uvs[t + 74]], [uvs[t + 1], uvs[t + 75], uvs[t + 74]]); + } + } + geometry.scale(-1, 1, 1); // rotate left-right + return geometry; + } + + _createMaterial(video) { // video:DOM + const texture = new THREE.VideoTexture(video); + texture.minFilter = THREE.LinearFilter; + texture.magFilter = THREE.LinearFilter; + return new THREE.MeshBasicMaterial({ + map: texture, + side: THREE.BackSide, + }); + } + + constructor() { + this._isUserInteracting = false; + this._lon = 0; + this._lat = 0; + this._onPointerDownPointerX = 0; + this._onPointerDownPointerY = 0; + this._onPointerDownLon = 0; + this._onPointerDownLat = 0; + this._camera = null; + this._scene = null; + this._renderer = null; + }; + + start(videoDOM, containerDOM, w, h) { + const self = this; + // create Camera + this._camera = new THREE.PerspectiveCamera(75, w / h, 1, 1100); + this._camera.target = new THREE.Vector3(0, 0, 0); + + // create Scene + this._scene = new THREE.Scene(); + this._scene.add(new THREE.Mesh(this._createGeometry(), this._createMaterial(videoDOM))); + + // create Renderer + this._renderer = new THREE.WebGLRenderer(); + this._renderer.setPixelRatio(window.devicePixelRatio); + this._renderer.setSize(w, h); + + function onWheel(e) { + e.preventDefault(); + let diff = 0; + if (event.wheelDeltaY) diff = event.wheelDeltaY * -0.05; // webkit + else if (event.wheelDelta) diff = event.wheelDelta * -0.05; // Opera / Explorer 9 + else if (event.detail) diff = event.detail * 1.0; // Firefox + self._camera.fov += diff; + self._camera.updateProjectionMatrix(); + } + + function onMouseUp(e) { + e.preventDefault(); + self._isUserInteracting = false; + } + + function onMouseDown(e) { + e.preventDefault(); + self._isUserInteracting = true; + self._onPointerDownPointerX = e.clientX; + self._onPointerDownPointerY = e.clientY; + self._onPointerDownLon = self._lon; + self._onPointerDownLat = self._lat; + } + + function onMouseMove(e) { + if (self._isUserInteracting !== true) return; + self._lon = (self._onPointerDownPointerX - e.clientX) * 0.1 + self._onPointerDownLon; + self._lat = (e.clientY - self._onPointerDownPointerY) * 0.1 + self._onPointerDownLat; + } + + this._renderer.domElement.addEventListener('mousewheel', onWheel, false); + this._renderer.domElement.addEventListener('MozMousePixelScroll', onWheel, false); + this._renderer.domElement.addEventListener('mouseup', onMouseUp, false); + this._renderer.domElement.addEventListener('mousedown', onMouseDown, false); + this._renderer.domElement.addEventListener('mousemove', onMouseMove, false); + + containerDOM.appendChild(this._renderer.domElement); + }; + + stop(containerDOM) { + const child = containerDOM.lastChild; + if (child) containerDOM.removeChild(child); + } + + animate() { + let phi = 0; + let theta = 0; + if (this._camera === null) return; + this._lat = Math.max(-85, Math.min(85, this._lat)); + phi = THREE.Math.degToRad(90 - this._lat); + theta = THREE.Math.degToRad(this._lon); + this._camera.target.x = 500 * Math.sin(phi) * Math.cos(theta); + this._camera.target.y = 500 * Math.cos(phi); + this._camera.target.z = 500 * Math.sin(phi) * Math.sin(theta); + this._camera.lookAt(this._camera.target); + this._renderer.render(this._scene, this._camera); + }; + + resize(w, h) { + if (this._camera === null) return; + this._camera.aspect = w / h; + this._camera.updateProjectionMatrix(); + this._renderer.setSize(w, h); + }; + +}; + +exports.ThetaView = ThetaView; diff --git a/samples/webcam.js b/samples/webcam.js new file mode 100644 index 0000000..c4b5925 --- /dev/null +++ b/samples/webcam.js @@ -0,0 +1,59 @@ +'use strict'; +/** + * Copyright (c) 2016 Ricoh Company, Ltd. All Rights Reserved. + * See LICENSE for more information + */ + +class Webcam { + constructor() { + navigator.getUserMedia = navigator.getUserMedia || + navigator.webkitGetUserMedia || navigator.mozGetUserMedia; + }; + + _enumerateDevice() { + return new Promise((resolve, reject) => { + navigator.mediaDevices.enumerateDevices() + .then(devices => { + const valid = devices.filter(o => (o.kind === 'videoinput') && (o.label !== '')); + resolve(valid.map(o => ({ id: o.deviceId, label: o.label }))); + }) + .catch(e => reject(e)); + }); + } + + _getSources() { + return new Promise((resolve, reject) => { + if (typeof MediaStreamTrack.getSources === 'undefined') { + console.log('MediaStreamTrack.getSources not supported'); + return reject; + } + MediaStreamTrack.getSources(data => { + const valid = data.filter(o => (o.kind === 'video') && (o.label !== '')); + return resolve(valid.map(o => ({ id: o.id, label: o.label }))); + }); + }); + } + + init() { + return (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) ? + this._getSources() : this._enumerateDevice(); + }; + + getUserMedia(src) { + let option = {}; + if (src) { + option = { + video: { mandatory: { sourceId: src } }, + audio: false, + }; + } else { + option = { video: true, audio: false }; + } + return new Promise((resolve, reject) => { + navigator.getUserMedia(option, s => resolve(s), e => reject(e)); + }); + }; + +}; + +exports.Webcam = Webcam; diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..f45972e --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,19 @@ +module.exports = { + entry: './samples/main.js', + output: { + filename: './build/bundle.js' + }, + module: { + loaders: [{ + test: /\.js$/, + loader: 'babel', + query: { + presets: ['es2015'], + compact: false + } + }] + }, + resolve: { + extensions: ['', '.js'] + } +};