123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- var rtc_getTool = function(onmessage,onaddstream,onclose,onopen){
-
- var PeerConnection = RTCPeerConnection;
-
- var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.mediaDevices.getUserMedia);
-
- var SessionDescription = (window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription);
- var pc = null;
- var oppositeChannel = null;
- var localStream = null;
- var createPeerConnection = function(){
-
- pc = new PeerConnection();
- pc.localChannel = pc.createDataChannel({
- ordered: false,
- maxRetransmitTime: 3000,
- });
- pc.localChannel.onerror = function (error) {
- console.log("数据传输通道建立异常:", error);
- };
- pc.localChannel.onopen = function () {
- console.log("本地数据通道建立成功");
- onopen();
- };
- pc.localChannel.onclose = function () {
- console.log("关闭数据传输通道");
- onclose();
- close();
- pc = null;
- };
- pc.localChannel.onmessage = function(event){
- onmessage(event);
- };
- pc.ondatachannel = function(event) {
-
- oppositeChannel = event.channel;
- };
- pc.onaddstream = function(event){
- onaddstream(event.stream);
- };
- pc.onicecandidate = function(event){
- if (event.candidate !== null) {
- var candidate = {"candidate":event.candidate,"type":"_candidate"};
- chat_sendMsg(8,JSON.stringify(candidate));
- }
- };
- }
-
- var sendOffer = function(){
- pc.createOffer(function(desc){
- pc.setLocalDescription(desc);
- chat_sendMsg(6,JSON.stringify({"sdp":desc,"type":"_offer"}));
- }, function (error) {
- console.log("发起信令失败:" + error);
- });
- }
-
- var sendAnswer = function(){
- pc.createAnswer(function(desc){
- pc.setLocalDescription(desc);
- chat_sendMsg(7,JSON.stringify({"sdp":desc,"type":"_answer"}));
- }, function (error) {
- console.log("响应信令失败:" + error);
- });
- }
-
- var signallingHandle = function(json){
-
- if(json.type === "_candidate" ){
- pc.addIceCandidate(new RTCIceCandidate(json.candidate));
- }else{
- pc.setRemoteDescription(new SessionDescription(json.sdp),
- function(){
-
- if(json.type === "_offer") {
- sendAnswer();
- }
- }
- );
- }
- }
-
- var send = function(msg){
- oppositeChannel.send(msg);
- }
-
-
- var openVideoAudioLocal = function(callbackLocalVideo,video,audio){
- getUserMedia.call(navigator, {
- video: video,
- audio: audio
- },function(localMediaStream) {
- callbackLocalVideo(localMediaStream);
- },function(error){
- console.log("创建本地媒体对象失败:" + error);
- });
- }
-
- var sendAddStream = function(stream){
- localStream = stream;
- pc.addStream(localStream);
- sendOffer();
- }
-
- var close = function(){
- if(pc != null){
- closeStream();
- pc.localChannel.close();
- pc.close();
- pc = null;
- oppositeChannel = null;
- chat_sendMsg(4,"");
- }
- }
-
- var closeStream = function(){
- if(localStream != null){
- if(localStream.getVideoTracks()[0]){
- localStream.getVideoTracks()[0].stop();
- }
- if(localStream.getAudioTracks()[0]){
- localStream.getAudioTracks()[0].stop();
- }
- if(localStream.getTracks()[0]){
- localStream.getTracks()[0].stop();
- }
- localStream = null;
- }
- }
- return {
- "closeStream":closeStream,
- "sendAddStream":sendAddStream,
- "openVideoAudioLocal":openVideoAudioLocal,
- "close":close,
- "signallingHandle":signallingHandle,
- "sendOffer":sendOffer,
- "sendAnswer":sendAnswer,
- "createPeerConnection":createPeerConnection,
- "send":send};
- }
|