Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from crosswalk-project/webrtc
Browse files Browse the repository at this point in the history
Add WebRTC sample
  • Loading branch information
townxelliot committed Jul 3, 2014
2 parents fe968bc + 37000a3 commit 5af47f2
Show file tree
Hide file tree
Showing 11 changed files with 3,033 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ extensions-android/xwalk-echo-extension-src/build
extensions-android/xwalk-echo-extension-src/lib
extensions-android/xwalk-echo-extension-src/tools
extensions-android/xwalk-echo-extension-src/xwalk-echo-extension
webrtc/server/node_modules
9 changes: 9 additions & 0 deletions webrtc/LICENSE-MIT-IP.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This software is licensed under the MIT License.

Copyright Fedor Indutny, 2012.

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.
22 changes: 22 additions & 0 deletions webrtc/LICENSE-MIT-PEERJS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Michelle Bu and Eric Zhang, http://peerjs.com

(The MIT License)

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.
64 changes: 64 additions & 0 deletions webrtc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Introduction

Basic WebRTC client (for making calls) + server (for brokering
connections), built using peerjs (http://peerjs.com/).

## Pre-requisites

nodejs, npm

## "Build"

cd server
npm install

## Running

# start the server
./run.sh

# this displays the IP address of the WebRTC brokering server;
# to keep the application simple, this is hard-coded into
# the clients

# edit client/main.js:
# you need to set the SERVER_IP at the top of the file to
# the IP address of your server;
# the clients you are installing on will need to be able
# to access that IP

# open client/index.html in a browser which supports
# WebRTC (recent Chrome or Firefox)

# or package for Crosswalk (7+)
python make_apk.py --app-root=client/ --app-local-path=index.html --name=WebRTC --package=org.crosswalkproject.samples.webrtc
# then install on an Android target

You can use two browser tabs if you like, or a mix of Crosswalk apps
on devices + browsers.

In each client, set a unique caller ID.

Now you can send messages to the other clients or make video calls to
them.

(The recipient IDs for calls or messages are whatever IDs you put into
the client instances.)

## Licenses

This application uses **peerjs** (both the client and server libraries),
released under an MIT license:

* Client: https://github.com/peers/peerjs
* Server: https://github.com/peers/peerjs-server

See LICENSE-MIT-PEERJS.txt for the full license. Note that only the
peerjs client library is distributed with this project.

The server part also uses the nodejs **ip** library, released under an
MIT license:

* ip: https://github.com/indutny/node-ip

See LICENSE-MIT-IP.txt for the full license.
57 changes: 57 additions & 0 deletions webrtc/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>WebRTC client</title>

<style>
#credentials, #dialler, #messages {
clear: both;
}
#remote-video {
max-width: 300px;
}
#local-video {
max-width: 150px;
}
</style>
</head>

<body>

<div id="credentials">
<p>
Connect as: <input type="text" id="caller-id" size="15">
<button id="connect">Connect</button>
</p>
</div>

<div id="dialler" data-active="false">
<p>
Make call to:
<input type="select" id="recipient-id">
</input>
<button id="dial">Call</button>
</p>

<hr>

<p><strong>REMOTE:</strong></p>
<video id="remote-video" autoplay></video>

<hr>

<p><strong>LOCAL:</strong></p>
<video id="local-video" autoplay></video>
</div>

<hr>

<div id="messages">
</div>

<script src="peer.js"></script>
<script src="main.js"></script>
</body>
</html>
181 changes: 181 additions & 0 deletions webrtc/client/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
document.addEventListener('DOMContentLoaded', function () {
// PeerJS server location
var SERVER_IP = '192.168.0.25';
var SERVER_PORT = 9000;

// DOM elements manipulated as user interacts with the app
var messageBox = document.querySelector('#messages');
var callerIdEntry = document.querySelector('#caller-id');
var connectBtn = document.querySelector('#connect');
var recipientIdEntry = document.querySelector('#recipient-id');
var dialBtn = document.querySelector('#dial');
var remoteVideo = document.querySelector('#remote-video');
var localVideo = document.querySelector('#local-video');

// the ID set for this client
var callerId = null;

// PeerJS object, instantiated when this client connects with its
// caller ID
var peer = null;

// the local video stream captured with getUserMedia()
var localStream = null;

// DOM utilities
var makePara = function (text) {
var p = document.createElement('p');
p.innerText = text;
return p;
};

var addMessage = function (para) {
if (messageBox.firstChild) {
messageBox.insertBefore(para, messageBox.firstChild);
}
else {
messageBox.appendChild(para);
}
};

var logError = function (text) {
var p = makePara('ERROR: ' + text);
p.style.color = 'red';
addMessage(p);
};

var logMessage = function (text) {
addMessage(makePara(text));
};

// get the local video and audio stream and show preview in the
// "LOCAL" video element
// successCb: has the signature successCb(stream); receives
// the local video stream as an argument
var getLocalStream = function (successCb) {
if (localStream && successCb) {
successCb(localStream);
}
else {
navigator.webkitGetUserMedia(
{
audio: true,
video: true
},

function (stream) {
localStream = stream;

localVideo.src = window.URL.createObjectURL(stream);

if (successCb) {
successCb(stream);
}
},

function (err) {
logError('failed to access local camera');
logError(err.message);
}
);
}
};

// set the "REMOTE" video element source
var showRemoteStream = function (stream) {
remoteVideo.src = window.URL.createObjectURL(stream);
};

// set caller ID and connect to the PeerJS server
var connect = function () {
callerId = callerIdEntry.value;

if (!callerId) {
logError('please set caller ID first');
return;
}

try {
// create connection to the ID server
peer = new Peer(callerId, {host: SERVER_IP, port: SERVER_PORT});

// hack to get around the fact that if a server connection cannot
// be established, the peer and its socket property both still have
// open === true; instead, listen to the wrapped WebSocket
// and show an error if its readyState becomes CLOSED
peer.socket._socket.onclose = function () {
logError('no connection to server');
peer = null;
};

// get local stream ready for incoming calls once the wrapped
// WebSocket is open
peer.socket._socket.onopen = function () {
getLocalStream();
};

// handle events representing incoming calls
peer.on('call', answer);
}
catch (e) {
peer = null;
logError('error while connecting to server');
}
};

// make an outgoing call
var dial = function () {
if (!peer) {
logError('please connect first');
return;
}

if (!localStream) {
logError('could not start call as there is no local camera');
return
}

var recipientId = recipientIdEntry.value;

if (!recipientId) {
logError('could not start call as no recipient ID is set');
return;
}

getLocalStream(function (stream) {
logMessage('outgoing call initiated');

var call = peer.call(recipientId, stream);

call.on('stream', showRemoteStream);

call.on('error', function (e) {
logError('error with call');
logError(e.message);
});
});
};

// answer an incoming call
var answer = function (call) {
if (!peer) {
logError('cannot answer a call without a connection');
return;
}

if (!localStream) {
logError('could not answer call as there is no localStream ready');
return;
}

logMessage('incoming call answered');

call.on('stream', showRemoteStream);

call.answer(localStream);
};

// wire up button events
connectBtn.addEventListener('click', connect);
dialBtn.addEventListener('click', dial);
});
10 changes: 10 additions & 0 deletions webrtc/client/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "WebRTC",
"description": "WebRTC client",
"version": "0.0.1",
"app": {
"launch": {
"local_path": "index.html"
}
}
}
Loading

0 comments on commit 5af47f2

Please sign in to comment.