1+ var videos = [ ] ;
2+ var rooms = [ 1 , 2 , 3 , 4 , 5 ] ;
3+ var PeerConnection = window . PeerConnection || window . webkitPeerConnection00 || window . webkitRTCPeerConnection ;
4+
5+ function getNumPerRow ( ) {
6+ var len = videos . length ;
7+ var biggest ;
8+
9+ // Ensure length is even for better division.
10+ if ( len % 2 === 1 ) {
11+ len ++ ;
12+ }
13+
14+ biggest = Math . ceil ( Math . sqrt ( len ) ) ;
15+ while ( len % biggest !== 0 ) {
16+ biggest ++ ;
17+ }
18+ return biggest ;
19+ }
20+
21+ function subdivideVideos ( ) {
22+ var perRow = getNumPerRow ( ) ;
23+ var numInRow = 0 ;
24+ for ( var i = 0 , len = videos . length ; i < len ; i ++ ) {
25+ var video = videos [ i ] ;
26+ setWH ( video , i ) ;
27+ numInRow = ( numInRow + 1 ) % perRow ;
28+ }
29+ }
30+
31+ function setWH ( video , i ) {
32+ var perRow = getNumPerRow ( ) ;
33+ var perColumn = Math . ceil ( videos . length / perRow ) ;
34+ var width = Math . floor ( ( window . innerWidth ) / perRow ) ;
35+ var height = Math . floor ( ( window . innerHeight - 190 ) / perColumn ) ;
36+ video . width = width ;
37+ video . height = height ;
38+ video . style . position = "absolute" ;
39+ video . style . left = ( i % perRow ) * width + "px" ;
40+ video . style . top = Math . floor ( i / perRow ) * height + "px" ;
41+ }
42+
43+ function cloneVideo ( domId , socketId ) {
44+ var video = document . getElementById ( domId ) ;
45+ var clone = video . cloneNode ( false ) ;
46+ clone . id = "remote" + socketId ;
47+ document . getElementById ( 'videos' ) . appendChild ( clone ) ;
48+ videos . push ( clone ) ;
49+ return clone ;
50+ }
51+
52+ function removeVideo ( socketId ) {
53+ var video = document . getElementById ( 'remote' + socketId ) ;
54+ if ( video ) {
55+ videos . splice ( videos . indexOf ( video ) , 1 ) ;
56+ video . parentNode . removeChild ( video ) ;
57+ }
58+ }
59+
60+ function addToChat ( msg , color ) {
61+ var messages = document . getElementById ( 'messages' ) ;
62+ msg = sanitize ( msg ) ;
63+ if ( color ) {
64+ msg = '<span style="color: ' + color + '; padding-left: 15px">' + msg + '</span>' ;
65+ } else {
66+ msg = '<strong style="padding-left: 15px">' + msg + '</strong>' ;
67+ }
68+ messages . innerHTML = messages . innerHTML + msg + '<br>' ;
69+ messages . scrollTop = 10000 ;
70+ }
71+
72+ function sanitize ( msg ) {
73+ return msg . replace ( / < / g, '<' ) ;
74+ }
75+
76+ function initFullScreen ( ) {
77+ var button = document . getElementById ( "fullscreen" ) ;
78+ button . addEventListener ( 'click' , function ( event ) {
79+ var elem = document . getElementById ( "videos" ) ;
80+ //show full screen
81+ elem . webkitRequestFullScreen ( ) ;
82+ } ) ;
83+ }
84+
85+ function initNewRoom ( ) {
86+ var button = document . getElementById ( "newRoom" ) ;
87+
88+ button . addEventListener ( 'click' , function ( event ) {
89+
90+ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz" ;
91+ var string_length = 8 ;
92+ var randomstring = '' ;
93+ for ( var i = 0 ; i < string_length ; i ++ ) {
94+ var rnum = Math . floor ( Math . random ( ) * chars . length ) ;
95+ randomstring += chars . substring ( rnum , rnum + 1 ) ;
96+ }
97+
98+ window . location . hash = randomstring ;
99+ location . reload ( ) ;
100+ } )
101+ }
102+
103+
104+ var websocketChat = {
105+ send : function ( message ) {
106+ rtc . _socket . send ( message ) ;
107+ } ,
108+ recv : function ( message ) {
109+ return message ;
110+ } ,
111+ event : 'receive_chat_msg'
112+ } ;
113+
114+ var dataChannelChat = {
115+ send : function ( message ) {
116+ for ( var connection in rtc . dataChannels ) {
117+ var channel = rtc . dataChannels [ connection ] ;
118+ channel . send ( message ) ;
119+ }
120+ } ,
121+ recv : function ( channel , message ) {
122+ return JSON . parse ( message ) . data ;
123+ } ,
124+ event : 'data stream data'
125+ } ;
126+
127+ function initChat ( ) {
128+ var chat ;
129+
130+ if ( rtc . dataChannelSupport ) {
131+ console . log ( 'initializing data channel chat' ) ;
132+ chat = dataChannelChat ;
133+ } else {
134+ console . log ( 'initializing websocket chat' ) ;
135+ chat = websocketChat ;
136+ }
137+
138+ var input = document . getElementById ( "chatinput" ) ;
139+ var room = window . location . hash . slice ( 1 ) ;
140+ var color = "#" + ( ( 1 << 24 ) * Math . random ( ) | 0 ) . toString ( 16 ) ;
141+
142+ input . addEventListener ( 'keydown' , function ( event ) {
143+ var key = event . which || event . keyCode ;
144+ if ( key === 13 ) {
145+ chat . send ( JSON . stringify ( {
146+ "eventName" : "chat_msg" ,
147+ "data" : {
148+ "messages" : input . value ,
149+ "room" : room ,
150+ "color" : color
151+ }
152+ } ) ) ;
153+ addToChat ( input . value ) ;
154+ input . value = "" ;
155+ }
156+ } , false ) ;
157+ rtc . on ( chat . event , function ( ) {
158+ data = chat . recv . apply ( this , arguments ) ;
159+ console . log ( data . color ) ;
160+ addToChat ( data . messages , data . color . toString ( 16 ) ) ;
161+ } ) ;
162+ }
163+
164+
165+ function init ( ) {
166+ if ( PeerConnection ) {
167+ rtc . createStream ( {
168+ "video" : true ,
169+ "audio" : true
170+ } , function ( stream ) {
171+ document . getElementById ( 'you' ) . src = URL . createObjectURL ( stream ) ;
172+ videos . push ( document . getElementById ( 'you' ) ) ;
173+ //rtc.attachStream(stream, 'you');
174+ subdivideVideos ( ) ;
175+ } ) ;
176+ } else {
177+ alert ( 'Your browser is not supported or you have to turn on flags. In chrome you go to chrome://flags and turn on Enable PeerConnection remember to restart chrome' ) ;
178+ }
179+
180+
181+ var room = window . location . hash . slice ( 1 ) ;
182+
183+ rtc . connect ( "ws:" + window . location . href . substring ( window . location . protocol . length ) . split ( '#' ) [ 0 ] , room ) ;
184+
185+ rtc . on ( 'add remote stream' , function ( stream , socketId ) {
186+ console . log ( "ADDING REMOTE STREAM..." ) ;
187+ var clone = cloneVideo ( 'you' , socketId ) ;
188+ document . getElementById ( clone . id ) . setAttribute ( "class" , "" ) ;
189+ rtc . attachStream ( stream , clone . id ) ;
190+ subdivideVideos ( ) ;
191+ } ) ;
192+ rtc . on ( 'disconnect stream' , function ( data ) {
193+ console . log ( 'remove ' + data ) ;
194+ removeVideo ( data ) ;
195+ } ) ;
196+ initFullScreen ( ) ;
197+ initNewRoom ( ) ;
198+ initChat ( ) ;
199+ }
200+
201+ window . onresize = function ( event ) {
202+ subdivideVideos ( ) ;
203+ } ;
0 commit comments