webrtc.io.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //CLIENT
  2. // Fallbacks for vendor-specific variables until the spec is finalized.
  3. var PeerConnection = window.PeerConnection || window.webkitPeerConnection00;
  4. var URL = window.URL || window.webkitURL || window.msURL || window.oURL;
  5. var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
  6. (function() {
  7. var rtc;
  8. if ('undefined' === typeof module) {
  9. rtc = this.rtc = {};
  10. } else {
  11. rtc = module.exports = {};
  12. }
  13. // Holds a connection to the server.
  14. rtc._socket = null;
  15. // Holds callbacks for certain events.
  16. rtc._events = {};
  17. rtc.on = function(eventName, callback) {
  18. rtc._events[eventName] = rtc._events[eventName] || [];
  19. rtc._events[eventName].push(callback);
  20. };
  21. rtc.fire = function(eventName, _) {
  22. var events = rtc._events[eventName];
  23. var args = Array.prototype.slice.call(arguments, 1);
  24. if (!events) {
  25. return;
  26. }
  27. for (var i = 0, len = events.length; i < len; i++) {
  28. events[i].apply(null, args);
  29. }
  30. };
  31. // Holds the STUN server to use for PeerConnections.
  32. rtc.SERVER = "STUN stun.l.google.com:19302";
  33. // Referenc e to the lone PeerConnection instance.
  34. rtc.peerConnections = {};
  35. // Array of known peer socket ids
  36. rtc.connections = [];
  37. // Stream-related variables.
  38. rtc.streams = [];
  39. rtc.numStreams = 0;
  40. rtc.initializedStreams = 0;
  41. /**
  42. * Connects to the websocket server.
  43. */
  44. rtc.connect = function(server, room) {
  45. room = room || ""; // by default, join a room called the blank string
  46. rtc._socket = new WebSocket(server);
  47. rtc._socket.onopen = function() {
  48. rtc._socket.send(JSON.stringify({
  49. "eventName": "join_room",
  50. "room": room
  51. }), function(error){
  52. if(error){console.log(error);}
  53. });
  54. rtc._socket.onmessage = function(data) {
  55. var json = JSON.parse(data.data);
  56. rtc.fire(json.eventName, json);
  57. };
  58. rtc._socket.onerror = function(err) {
  59. console.log('onerror');
  60. console.log(err);
  61. };
  62. rtc._socket.onclose = function(data) {
  63. rtc.fire('disconnect stream', rtc._socket.id);
  64. delete rtc.peerConnections[rtc._socket.id];
  65. };
  66. rtc.on('get_peers', function(data) {
  67. rtc.connections = data.connections;
  68. // fire connections event and pass peers
  69. rtc.fire('connections', rtc.connections);
  70. });
  71. rtc.on('receive_ice_candidate', function(data) {
  72. var candidate = new IceCandidate(data.label, data.candidate);
  73. rtc.peerConnections[data.socketId].processIceMessage(candidate);
  74. rtc.fire('receive ice candidate', candidate);
  75. });
  76. rtc.on('new_peer_connected', function(data) {
  77. rtc.connections.push(data.socketId);
  78. var pc = rtc.createPeerConnection(data.socketId);
  79. for (var i = 0; i < rtc.streams.length; i++) {
  80. var stream = rtc.streams[i];
  81. pc.addStream(stream);
  82. }
  83. });
  84. rtc.on('remove_peer_connected', function(data) {
  85. rtc.fire('disconnect stream', data.socketId);
  86. delete rtc.peerConnections[data.socketId];
  87. });
  88. rtc.on('receive_offer', function(data) {
  89. rtc.receiveOffer(data.socketId, data.sdp);
  90. rtc.fire('receive offer', data);
  91. });
  92. rtc.on('receive_answer', function(data) {
  93. rtc.receiveAnswer(data.socketId, data.sdp);
  94. rtc.fire('receive answer', data);
  95. });
  96. rtc.fire('connect');
  97. };
  98. };
  99. rtc.sendOffers = function() {
  100. for (var i = 0, len = rtc.connections.length; i < len; i++) {
  101. var socketId = rtc.connections[i];
  102. rtc.sendOffer(socketId);
  103. }
  104. }
  105. rtc.onClose = function(data) {
  106. rtc.on('close_stream', function() {
  107. rtc.fire('close_stream', data);
  108. });
  109. }
  110. rtc.createPeerConnections = function() {
  111. for (var i = 0; i < rtc.connections.length; i++) {
  112. rtc.createPeerConnection(rtc.connections[i]);
  113. }
  114. };
  115. rtc.createPeerConnection = function(id) {
  116. console.log('createPeerConnection');
  117. var pc = rtc.peerConnections[id] = new PeerConnection(rtc.SERVER, function(candidate, moreToFollow) {
  118. if (candidate) {
  119. rtc._socket.send(JSON.stringify({
  120. "eventName": "send_ice_candidate",
  121. "label": candidate.label,
  122. "candidate": candidate.toSdp(),
  123. "socketId": id
  124. }), function(error){
  125. if(error){console.log(error);}
  126. });
  127. }
  128. rtc.fire('ice candidate', candidate, moreToFollow);
  129. });
  130. pc.onopen = function() {
  131. // TODO: Finalize this API
  132. rtc.fire('peer connection opened');
  133. };
  134. pc.onaddstream = function(event) {
  135. // TODO: Finalize this API
  136. rtc.fire('add remote stream', event.stream, id);
  137. };
  138. return pc;
  139. };
  140. rtc.sendOffer = function(socketId) {
  141. var pc = rtc.peerConnections[socketId];
  142. // TODO: Abstract away video: true, audio: true for offers
  143. var offer = pc.createOffer({
  144. video: true,
  145. audio: true
  146. });
  147. pc.setLocalDescription(pc.SDP_OFFER, offer);
  148. rtc._socket.send(JSON.stringify({
  149. "eventName": "send_offer",
  150. "socketId": socketId,
  151. "sdp": offer.toSdp()
  152. }), function(error){
  153. if(error){console.log(error);}
  154. });
  155. pc.startIce();
  156. };
  157. rtc.receiveOffer = function(socketId, sdp) {
  158. var pc = rtc.peerConnections[socketId];
  159. pc.setRemoteDescription(pc.SDP_OFFER, new SessionDescription(sdp));
  160. rtc.sendAnswer(socketId);
  161. };
  162. rtc.sendAnswer = function(socketId) {
  163. var pc = rtc.peerConnections[socketId];
  164. var offer = pc.remoteDescription;
  165. // TODO: Abstract away video: true, audio: true for answers
  166. var answer = pc.createAnswer(offer.toSdp(), {
  167. video: true,
  168. audio: true
  169. });
  170. pc.setLocalDescription(pc.SDP_ANSWER, answer);
  171. rtc._socket.send(JSON.stringify({
  172. "eventName": "send_answer",
  173. "socketId": socketId,
  174. "sdp": answer.toSdp()
  175. }), function(error){
  176. if(error){console.log(error);}
  177. });
  178. pc.startIce();
  179. };
  180. rtc.receiveAnswer = function(socketId, sdp) {
  181. var pc = rtc.peerConnections[socketId];
  182. pc.setRemoteDescription(pc.SDP_ANSWER, new SessionDescription(sdp));
  183. };
  184. rtc.createStream = function(opt, onSuccess, onFail) {
  185. var options;
  186. onSuccess = onSuccess ||
  187. function() {};
  188. onFail = onFail ||
  189. function() {};
  190. if(opt.audio && opt.video){
  191. options = {
  192. video: true,
  193. audio: true
  194. };
  195. }else if(opt.video){
  196. options = {
  197. video: true,
  198. audio: false
  199. };
  200. }else if(opt.audio){
  201. options = {
  202. video: false,
  203. audio: true
  204. };
  205. }else {
  206. options = {
  207. video: false,
  208. audio: false
  209. };
  210. }
  211. if (getUserMedia) {
  212. rtc.numStreams++;
  213. getUserMedia.call(navigator, options, function(stream) {
  214. rtc.streams.push(stream);
  215. rtc.initializedStreams++;
  216. onSuccess(stream);
  217. if (rtc.initializedStreams === rtc.numStreams) {
  218. rtc.fire('ready');
  219. }
  220. }, function() {
  221. alert("Could not connect stream.");
  222. onFail();
  223. });
  224. } else {
  225. alert('webRTC is not yet supported in this browser.');
  226. }
  227. }
  228. rtc.addStreams = function() {
  229. for (var i = 0; i < rtc.streams.length; i++) {
  230. var stream = rtc.streams[i];
  231. for (var connection in rtc.peerConnections) {
  232. rtc.peerConnections[connection].addStream(stream);
  233. }
  234. }
  235. };
  236. rtc.attachStream = function(stream, domId) {
  237. document.getElementById(domId).src = URL.createObjectURL(stream);
  238. };
  239. rtc.on('ready', function() {
  240. rtc.createPeerConnections();
  241. rtc.addStreams();
  242. rtc.sendOffers();
  243. });
  244. }).call(this);