Widget / WebRTC Client PeerJs

This widget receives a video stream via WebRTC. You must have the server widget running in another window, or on a different machine.

by Mark Smit

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-webrtc-client" class="panel panel-default">
    <div class="panel-heading">
        <div class="btn-toolbar pull-right" role="toolbar">
            <div class="btn-group">
                <button type="button" class="btn btn-xs btn-default disconnect"><span class="glyphicon glyphicon-off"></span></button>
            </div>
            <div class="btn-group">
                <button type="button" class="btn btn-xs btn-default hidebody"><span class="glyphicon glyphicon-chevron-up"></span></button>
            </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">Webcam</span>
        <!-- <span style="font-size:10px">Your id: </span> -->
        <!-- <span id="my-id">...</span> -->

    </div>
    <div id="com-chilipeppr-widget-webrtc-body" class="panel-body">
        
        <div class="pure-g">
                        
            <!-- Steps -->
            <div class="pure-u-1-3-wrapper-relative">
            <div class="pure-u-1-3">
                
                <!-- Must be logged in msg -->
                <div id="step-notloggedin" class="alert alert-warning alert-dismissible hidden" role="alert">
                    <p>You are not logged in to ChiliPeppr, therefore you can't become a client to connect to your WebRTC server. You must be logged in because ChiliPeppr uses your login to create the peer-to-peer call.</p>
                </div>
                
               ...

CSS

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

#com-chilipeppr-widget-webrtc-client #their-video {
  width: 100%;
  height: 100%;
  /* max-height: 480px; */
  background-color: #eee;
}

#com-chilipeppr-widget-webrtc-client #video-container {
    padding: 0;
    text-align: center;
    margin-bottom:-5px;
}

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

#com-chilipeppr-widget-webrtc-client .pure-u-1-3-wrapper-relative{
    /* position:relative; */
}
#com-chilipeppr-widget-webrtc-client .alert {
    margin-bottom:0;
}
#com-chilipeppr-widget-webrtc-client .pure-u-1-3 {
    /* position:absolute; */
    left:10px;
    top:10px;
    right:10px;
}
#com-chilipeppr-widget-webrtc-body:after {
    content:none;
}

JavaScript

requirejs.config({
    paths: {
        peerJs: 'http://cdn.peerjs.com/0.3/peer',
        jqueryui: '//chilipeppr.com/js/jquery-ui-1.10.4/ui/jquery.ui.core',
        jqueryuiWidget: '//chilipeppr.com/js/jquery-ui-1.10.4/ui/jquery.ui.widget',
        jqueryuiMouse: '//chilipeppr.com/js/jquery-ui-1.10.4/ui/jquery.ui.mouse',
        jqueryuiResizeable: '//chilipeppr.com/js/jquery-ui-1.10.4/ui/jquery.ui.resizable',
    },
    shim: {
        jqueryuiWidget: ['jqueryui'],
        jqueryuiMouse: ['jqueryuiWidget'],
        jqueryuiResizeable: ['jqueryuiMouse' ]
    }
});

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

cpdefine("inline:com-chilipeppr-widget-webrtc-client", ["chilipeppr_ready", "peerJs", "jquerycookie", 'jqueryuiResizeable'], function () {
    
    return {
        id: "com-chilipeppr-widget-webrtc-client",
        url: "http://fiddle.jshell.net/chilipeppr/qWj4f/show/light/",
        fiddleurl: "http://jsfiddle.net/chilipeppr/qWj4f/",
        name: "Widget / WebRTC Client",
        desc: "This widget receives a video stream via WebRTC. You must have the server widget running in another window, or on a different machine.",
        publish: {},
        subscribe: {},
        foreignPublish: {},
        foreignSubscribe: {},
        init: function () {

            this.configBodyFromCookie();
            //this.setupWebRtc();
            $('.com-chilipeppr-widget-webrtc-retry').click(this.connectToServer.bind(this));
            $('.com-chilipeppr-widget-webrtc-retryhello').click(this.connAskForStreamAgain.bind(this));
            $('#com-chilipeppr-widget-webrtc-client .hidebody').click(this.toggleBody.bind(this));
            $('#com-chilipeppr-widget-webrtc-client .disconnect').click(this.toggleConnect.bind(this));
            this.setupPeerJs();
           ...