1+ /* WebRTC PubNub Controller
2+ * Author: Kevin Gleason
3+ * Date: July 15, 2015
4+ * Description: A wrapper library for the PubNub WebRTC SDK to make simple video
5+ * functions a breeze to implement.
6+ *
7+ * TODO: make getVideoElement a native non-jQuery function
8+ *
9+ */
10+
111( function ( ) {
212
313
@@ -93,6 +103,13 @@ var CONTROLLER = window.CONTROLLER = function(phone){
93103 publishCtrl ( controlChannel ( name ) , "userJoin" , phone . number ( ) ) ;
94104 }
95105
106+ CONTROLLER . leaveStream = function ( name ) {
107+ var ch = ( name ? name : phone . number ( ) ) + "-stream" ;
108+ pubnub . unsubscribe ( {
109+ channel : ch ,
110+ } ) ;
111+ }
112+
96113 CONTROLLER . send = function ( message , number ) {
97114 if ( phone . oneway ) return stream_message ( message ) ;
98115 phone . send ( message , number ) ;
@@ -119,10 +136,12 @@ var CONTROLLER = window.CONTROLLER = function(phone){
119136 } ;
120137
121138 CONTROLLER . hangup = function ( number ) {
122- if ( number ) {
139+ if ( number ) {
140+ if ( phone . oneway ) CONTROLLER . leaveStream ( number ) ;
123141 phone . hangup ( number ) ;
124142 return publishCtrl ( controlChannel ( number ) , "userLeave" , phone . number ( ) )
125143 }
144+ if ( phone . oneway ) CONTROLLER . leaveStream ( ) ;
126145 phone . hangup ( ) ;
127146 for ( var i = 0 ; i < userArray . length ; i ++ ) {
128147 var cChan = controlChannel ( userArray [ i ] . number ) ;
@@ -166,6 +185,10 @@ var CONTROLLER = window.CONTROLLER = function(phone){
166185 CONTROLLER . isOnline ( number + "-stream" , cb ) ;
167186 } ;
168187
188+ CONTROLLER . getVideoElement = function ( number ) {
189+ return $ ( '*[data-number="' + number + '"]' ) ;
190+ }
191+
169192 function manage_users ( session ) {
170193 if ( session . number == phone . number ( ) ) return ; // Do nothing if it is self.
171194 var idx = findWithAttr ( userArray , "number" , session . number ) ; // Find session by number
@@ -178,7 +201,7 @@ var CONTROLLER = window.CONTROLLER = function(phone){
178201 }
179202 }
180203 userArray = userArray . filter ( function ( s ) { return ! s . closed ; } ) ; // Clean to only open talks
181- console . log ( userArray ) ;
204+ // console.log(userArray);
182205 }
183206
184207 function add_to_stream ( number ) {
@@ -198,7 +221,7 @@ var CONTROLLER = window.CONTROLLER = function(phone){
198221 }
199222
200223 function publishCtrl ( ch , type , data ) {
201- console . log ( "Pub to " + ch ) ;
224+ // console.log("Pub to " + ch);
202225 var msg = { type : type , data : data } ;
203226 pubnub . publish ( {
204227 channel : ch ,
@@ -211,7 +234,7 @@ var CONTROLLER = window.CONTROLLER = function(phone){
211234 pubnub . subscribe ( {
212235 channel : ctrlChan ,
213236 message : receive ,
214- connect : function ( ) { console . log ( "Subscribed to " + ctrlChan ) ; }
237+ connect : function ( ) { } // console.log("Subscribed to " + ctrlChan); }
215238 } ) ;
216239 }
217240
@@ -239,7 +262,7 @@ var CONTROLLER = window.CONTROLLER = function(phone){
239262 if ( idx != - 1 ) audiotogglecb ( userArray [ idx ] , audEnabled ) ;
240263 break ;
241264 }
242- console . log ( m ) ;
265+ // console.log(m);
243266 }
244267
245268 function findWithAttr ( array , attr , value ) {
@@ -257,7 +280,7 @@ var CONTROLLER = window.CONTROLLER = function(phone){
257280} ) ( ) ;
258281
259282// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
260- // Request fresh TURN servers from XirSys
283+ // Request fresh TURN servers from XirSys - Need to explain.
261284// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
262285function get_xirsys_servers ( ) {
263286 var servers ;
0 commit comments