123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- var WebRtcStreamer = (function() {
- /**
- * Interface with WebRTC-streamer API
- * @constructor
- * @param {string} videoElement - id of the video element tag
- * @param {string} srvurl - url of webrtc-streamer (default is current location)
- */
- var WebRtcStreamer = function WebRtcStreamer (videoElement, srvurl) {
- if (typeof videoElement === "string") {
- this.videoElement = document.getElementById(videoElement);
- } else {
- this.videoElement = videoElement;
- }
- this.srvurl = srvurl || location.protocol+"//"+window.location.hostname+":"+window.location.port;
- this.pc = null;
- this.mediaConstraints = { offerToReceiveAudio: true, offerToReceiveVideo: true };
- this.iceServers = null;
- this.earlyCandidates = [];
- }
- WebRtcStreamer.prototype._handleHttpErrors = function (response) {
- if (!response.ok) {
- throw Error(response.statusText);
- }
- return response;
- }
- /**
- * Connect a WebRTC Stream to videoElement
- * @param {string} videourl - id of WebRTC video stream
- * @param {string} audiourl - id of WebRTC audio stream
- * @param {string} options - options of WebRTC call
- * @param {string} stream - local stream to send
- */
- WebRtcStreamer.prototype.connect =async function(videourl, audiourl, options, localstream) {
- this.disconnect();
-
- // getIceServers is not already received
- if (!this.iceServers) {
- console.log("Get IceServers");
- try {
- // fetch(this.srvurl + "/api/getIceServers")
- // .then(this._handleHttpErrors)
- // .then((response) => { return response.json() })
- // .then((response) => this.onReceiveGetIceServers(response, videourl, audiourl, options, localstream))
- // .catch((error) => {
- // debugger
- // // new Error('WebRtcStreamer connect error')
- // return this.onError("getIceServers " + error)
- // })
- const response = await fetch(this.srvurl + "/api/getIceServers")
- const res = this._handleHttpErrors(response).json()
- this.onReceiveGetIceServers(res, videourl, audiourl, options, localstream)
- } catch (error) {
- // this.onError("getIceServers " + error)
- throw new Error('WebRtcStreamer connect error')
- }
-
- } else {
- this.onReceiveGetIceServers(this.iceServers, videourl, audiourl, options, localstream);
- }
-
- }
- /**
- * Disconnect a WebRTC Stream and clear videoElement source
- */
- WebRtcStreamer.prototype.disconnect = function() {
- if (this.videoElement?.srcObject) {
- this.videoElement.srcObject.getTracks().forEach(track => {
- track.stop()
- this.videoElement.srcObject.removeTrack(track);
- });
- }
- if (this.pc) {
- fetch(this.srvurl + "/api/hangup?peerid=" + this.pc.peerid)
- .then(this._handleHttpErrors)
- .catch( (error) => this.onError("hangup " + error ))
- try {
- this.pc.close();
- }
- catch (e) {
- console.log ("Failure close peer connection:" + e);
- }
- this.pc = null;
- }
- }
- /*
- * GetIceServers callback
- */
- WebRtcStreamer.prototype.onReceiveGetIceServers = function(iceServers, videourl, audiourl, options, stream) {
- this.iceServers = iceServers;
- this.pcConfig = iceServers || {"iceServers": [] };
- try {
- this.createPeerConnection();
- var callurl = this.srvurl + "/api/call?peerid=" + this.pc.peerid + "&url=" + encodeURIComponent(videourl);
- if (audiourl) {
- callurl += "&audiourl="+encodeURIComponent(audiourl);
- }
- if (options) {
- callurl += "&options="+encodeURIComponent(options);
- }
-
- if (stream) {
- this.pc.addStream(stream);
- }
- // clear early candidates
- this.earlyCandidates.length = 0;
-
- // create Offer
- this.pc.createOffer(this.mediaConstraints).then((sessionDescription) => {
- console.log("Create offer:" + JSON.stringify(sessionDescription));
-
- this.pc.setLocalDescription(sessionDescription)
- .then(() => {
- fetch(callurl, { method: "POST", body: JSON.stringify(sessionDescription) })
- .then(this._handleHttpErrors)
- .then( (response) => (response.json()) )
- .catch( (error) => this.onError("call " + error ))
- .then( (response) => this.onReceiveCall(response) )
- .catch( (error) => this.onError("call " + error ))
-
- }, (error) => {
- console.log ("setLocalDescription error:" + JSON.stringify(error));
- });
-
- }, (error) => {
- alert("Create offer error:" + JSON.stringify(error));
- });
- } catch (e) {
- this.disconnect();
- alert("connect error: " + e);
- }
- }
- WebRtcStreamer.prototype.getIceCandidate = function() {
- fetch(this.srvurl + "/api/getIceCandidate?peerid=" + this.pc.peerid)
- .then(this._handleHttpErrors)
- .then( (response) => (response.json()) )
- .then( (response) => this.onReceiveCandidate(response))
- .catch( (error) => this.onError("getIceCandidate " + error ))
- }
-
- /*
- * create RTCPeerConnection
- */
- WebRtcStreamer.prototype.createPeerConnection = function() {
- console.log("createPeerConnection config: " + JSON.stringify(this.pcConfig));
- this.pc = new RTCPeerConnection(this.pcConfig);
- var pc = this.pc;
- pc.peerid = Math.random();
-
- pc.onicecandidate = (evt) => this.onIceCandidate(evt);
- pc.onaddstream = (evt) => this.onAddStream(evt);
- pc.oniceconnectionstatechange = (evt) => {
- console.log("oniceconnectionstatechange state: " + pc.iceConnectionState);
- if (this.videoElement) {
- if (pc.iceConnectionState === "connected") {
- this.videoElement.style.opacity = "1.0";
- }
- else if (pc.iceConnectionState === "disconnected") {
- this.videoElement.style.opacity = "0.25";
- }
- else if ( (pc.iceConnectionState === "failed") || (pc.iceConnectionState === "closed") ) {
- this.videoElement.style.opacity = "0.5";
- } else if (pc.iceConnectionState === "new") {
- this.getIceCandidate();
- }
- }
- }
- pc.ondatachannel = function(evt) {
- console.log("remote datachannel created:"+JSON.stringify(evt));
-
- evt.channel.onopen = function () {
- console.log("remote datachannel open");
- this.send("remote channel openned");
- }
- evt.channel.onmessage = function (event) {
- console.log("remote datachannel recv:"+JSON.stringify(event.data));
- }
- }
- pc.onicegatheringstatechange = function() {
- if (pc.iceGatheringState === "complete") {
- const recvs = pc.getReceivers();
-
- recvs.forEach((recv) => {
- if (recv.track && recv.track.kind === "video") {
- console.log("codecs:" + JSON.stringify(recv.getParameters().codecs))
- }
- });
- }
- }
- try {
- var dataChannel = pc.createDataChannel("ClientDataChannel");
- dataChannel.onopen = function() {
- console.log("local datachannel open");
- this.send("local channel openned");
- }
- dataChannel.onmessage = function(evt) {
- console.log("local datachannel recv:"+JSON.stringify(evt.data));
- }
- } catch (e) {
- console.log("Cannor create datachannel error: " + e);
- }
-
- console.log("Created RTCPeerConnnection with config: " + JSON.stringify(this.pcConfig) );
- return pc;
- }
- /*
- * RTCPeerConnection IceCandidate callback
- */
- WebRtcStreamer.prototype.onIceCandidate = function (event) {
- if (event.candidate) {
- if (this.pc.currentRemoteDescription) {
- this.addIceCandidate(this.pc.peerid, event.candidate);
- } else {
- this.earlyCandidates.push(event.candidate);
- }
- }
- else {
- console.log("End of candidates.");
- }
- }
- WebRtcStreamer.prototype.addIceCandidate = function(peerid, candidate) {
- fetch(this.srvurl + "/api/addIceCandidate?peerid="+peerid, { method: "POST", body: JSON.stringify(candidate) })
- .then(this._handleHttpErrors)
- .then( (response) => (response.json()) )
- .then( (response) => {console.log("addIceCandidate ok:" + response)})
- .catch( (error) => this.onError("addIceCandidate " + error ))
- }
-
- /*
- * RTCPeerConnection AddTrack callback
- */
- WebRtcStreamer.prototype.onAddStream = function(event) {
- console.log("Remote track added:" + JSON.stringify(event));
-
- this.videoElement.srcObject = event.stream;
- var promise = this.videoElement.play();
- if (promise !== undefined) {
- promise.catch((error) => {
- console.warn("error:"+error);
- this.videoElement.setAttribute("controls", true);
- });
- }
- }
-
- /*
- * AJAX /call callback
- */
- WebRtcStreamer.prototype.onReceiveCall = function(dataJson) {
- console.log("offer: " + JSON.stringify(dataJson));
- var descr = new RTCSessionDescription(dataJson);
- this.pc.setRemoteDescription(descr).then(() => {
- console.log ("setRemoteDescription ok");
- while (this.earlyCandidates.length) {
- var candidate = this.earlyCandidates.shift();
- this.addIceCandidate(this.pc.peerid, candidate);
- }
-
- this.getIceCandidate()
- }
- , (error) => {
- console.log ("setRemoteDescription error:" + JSON.stringify(error));
- });
- }
- /*
- * AJAX /getIceCandidate callback
- */
- WebRtcStreamer.prototype.onReceiveCandidate = function(dataJson) {
- console.log("candidate: " + JSON.stringify(dataJson));
- if (dataJson) {
- for (var i=0; i<dataJson.length; i++) {
- var candidate = new RTCIceCandidate(dataJson[i]);
-
- console.log("Adding ICE candidate :" + JSON.stringify(candidate) );
- this.pc.addIceCandidate(candidate).then( () => { console.log ("addIceCandidate OK"); }
- , (error) => { console.log ("addIceCandidate error:" + JSON.stringify(error)); } );
- }
- this.pc.addIceCandidate();
- }
- }
- /*
- * AJAX callback for Error
- */
- WebRtcStreamer.prototype.onError = function(status) {
- console.log("onError:" + status);
- throw new Error('视频播放错误')
- }
- return WebRtcStreamer;
- })();
- if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
- window.WebRtcStreamer = WebRtcStreamer;
- }
- if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
- module.exports = WebRtcStreamer;
- }
|