Widget / WebRTC Server PeerJs

This widget to serves a video stream via WebRTC. The idea here is that the CNC machine is serving up the video. The companion widget would be the client widget to this server widget.

HTML

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://www.chilipeppr.com/js/require.js"></script>
<div id="com-chilipeppr-widget-webrtcserver" class="panel panel-default">
    <div class="panel-heading">
        <div class="btn-toolbar pull-right" role="toolbar">
            <div class="btn-group"></div>
            <div class="btn-group">
                <div class="dropdown">
                    <button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                </div>
            </div>
        </div> 
        <span class="panel-title" data-toggle="popover">WebRTC Server</span>

    </div>
    <div id="com-chilipeppr-widget-webrtc-body" class="panel-body">
        
        <div class="pure-g">
            
            <!-- Video area -->
            <div class="pure-u-2-3" id="video-container">
                <!-- <video id="their-video" autoplay></video> -->
                <video id="my-video" muted="false" autoplay ></video>
            </div>
            
            <!-- Steps -->
            <div class="pure-u-1-3">
                
                <div id="step-notloggedin" class="alert alert-warning alert-dismissible" role="alert">
                    <p>You are not logged in to ChiliPeppr, therefore you can't become a server. You must be logged in because ChiliPeppr uses your login to create the peer-to-peer call.</p>
                </div>

                <!-- Get local audio/video stream -->
                <div id="step1" class="alert alert-warning alert-dismissible" role="alert">
                    <p>Please click `allow` on the top of the screen so we can access your webcam and microphone.</p>
                    <div id="step1-error">
                        <p>Failed to access the webcam and...

CSS

#com-chilipeppr-widget-webrtc-body {
    padding:0;
}
#my-video {
    /* margin-top: 15px; */
    width:100%;
  /* width: 280px; */
    height: auto;
    background-color: #eee;
}

#their-video {
  width: 100%;
  height: 75%;
  max-height: 480px;
  background-color: #eee;
}

#video-container {
  padding: 0;
  text-align: center;
}

#step1-error, #step2, #step3 {
    display: none;
}
#step1-error {
    padding-top:10px;
}
#my-id {
  font-weight: bold;
}

.pure-g {
    position:relative;
}
.pure-u-1-3 {
    position:absolute;
    left:10px;
    top:10px;
    right:10px;
}
#step2 {
    background: rgba(255,255,255,0.4);
    padding:10px;
}

JavaScript

requirejs.config({
    paths: {
        peerJs: 'http://cdn.peerjs.com/0.3/peer'
    },
    shim: {
    }
});

// Test this element. This code is auto-removed by the chilipeppr.load()
cprequire_test(["inline:com-chilipeppr-widget-webrtc-server"], function (hello) {
    console.log("test running of " + hello.id);
    hello.init();
} /*end_test*/ );

cpdefine("inline:com-chilipeppr-widget-webrtc-server", ["chilipeppr_ready", "peerJs"], function () {
    
    return {
        id: "com-chilipeppr-widget-webrtc-server",
        url: "http://fiddle.jshell.net/chilipeppr/7P2bJ/show/light/",
        fiddleurl: "http://jsfiddle.net/chilipeppr/7P2bJ/",
        name: "Widget / WebRTC Server",
        desc: "This widget to serves a video stream via WebRTC. The idea here is that the CNC machine is serving up the video. The companion widget would be the client widget to this server widget.",
        publish: {},
        subscribe: {},
        foreignPublish: {},
        foreignSubscribe: {},
        init: function () {

            this.forkSetup();
            //this.setupWebRtc();
            this.setupPeerJs();
            
            console.log(this.name + " done loading.");
        },
        setupPeerJs: function() {
            // You can pick your own id or omit the id if you want to get a random one from the server.
            //var peer = new Peer({key: 'cpoxnt5u4z5xw29'});
            //var peer = new Peer('pick-an-id', {key: 'myapikey'}); 
            
             // Compatibility shim
            navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
            
            this.checkLogin(function(login) {
                
                if (login == null) {
                    // not logged in
                    console.log("not logged in. cannot proceed.");
                    return;
                }
                
                $('#step-notloggedin').addClass('hidden');
                
      ...