var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var iceServers = [];
var optional = {
optional: []
};
var constraints = {
'offerToReceiveAudio': true,
'offerToReceiveVideo': true
};
var promiseCreateOffer = function() {
var peer ;
try{
peer = new PeerConnection(iceServers, optional);
}
catch(err){
console.log('error ',err);
return ;
}
peer.createOffer(constraints).then(
function(offer) {
return peer.setLocalDescription(offer);
})
.then(function() {
console.log('ok ',peer.localDescription);
},
function(){
// when promise rejected state,called
console.log('rejected ');
},function(){
//when promise progress state become rejected,called
console.log('progress ');
}
)
.catch(function(reason) {
// An error occurred
console.log('onSdpError: ', reason);
});
}
promiseCreateOffer();
when called promiseCreateOffer(),total 20% users has no any response include error event
//this my full js code
function TESTRTCPeerConnection(config) { var options = { iceServers: null, onOfferSDP: null, onOfferSDPError: null, onICE: null, onAnswerSdpSucess: null, onAnswerSdpError: null, onRemoteStreamEnded: null, onRemoteStream: null }; var peer; window.moz = !!navigator.mozGetUserMedia; var w = window; var PeerConnection = w.mozRTCPeerConnection || w.webkitRTCPeerConnection || w.RTCPeerConnection; var SessionDescription = w.mozRTCSessionDescription || w.RTCSessionDescription; var IceCandidate = w.mozRTCIceCandidate || w.RTCIceCandidate; var iceServers = []; iceServers.push({ url: 'stun:stun.l.google.com:19302', urls: 'stun:stun.l.google.com:19302' }); var self = this; var getCandidate = false; iceServers = { iceServers: iceServers }; var optional = { optional: [] }; var nowCreatSdpTime;
self.sendUT = function (msg) {
msg.liveuuid = config.liveuuid;
console.log('TestWebrtcSendUT=', msg);
//static function
};
try {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestNewPeerConnection'
});
peer = new PeerConnection(iceServers, optional);
if (!peer) {
console.error('[TESTRTCPeerConnection]peer new fail');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestNewPeerConnectionFail'
});
return;
}
} catch (err) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'CatchTestNewPeerConnectionFail',
error: err
});
return;
}
peer.onicecandidate = function (event) {
if (event.candidate) {
if (options.onICE) {
options.onICE(event.candidate);
}
if (getCandidate === false) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestOnIceCandidateEvent',
time: new Date().getTime() - nowCreatSdpTime
});
getCandidate = true;
}
console.log('[TESTRTCPeerConnection] candidate:', JSON.stringify(event.candidate));
}
};
peer.onsignalingstatechange = function (state) {
console.log('[TESTRTCPeerConnection] onsignalingstatechange', state);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestOnSignalingstatechange',
state: peer.signalingState,
time: new Date().getTime() - nowCreatSdpTime
});
};
peer.onaddstream = function (event) {
var remoteMediaStream = event.stream;
// onRemoteStreamEnded(MediaStream)
remoteMediaStream.onended = function () {
if (options.onRemoteStreamEnded) {
options.onRemoteStreamEnded(remoteMediaStream);
}
};
// onRemoteStream(MediaStream)
if (options.onRemoteStream) {
options.onRemoteStream(remoteMediaStream);
}
console.log('[TESTRTCPeerConnection] on:add:stream', remoteMediaStream);
};
var constraints = {
offerToReceiveAudio: true,
offerToReceiveVideo: true
};
self.createOffer = function () {
peer.createOffer(function (sessionDescription) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferSucess',
time: new Date().getTime() - nowCreatSdpTime
});
try {
peer.setLocalDescription(sessionDescription);
} catch (error) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'CatchTestCreateOfferSucessError',
time: new Date().getTime() - nowCreatSdpTime
});
}
console.log('[TESTRTCPeerConnection] offer-sdp', sessionDescription.sdp);
},
function (message) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferFail',
time: new Date().getTime() - nowCreatSdpTime
});
console.error('[TESTRTCPeerConnection] onSdpError:', message);
},
constraints);
};
self.promiseCreateOffer = function () {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOffer',
time: new Date().getTime() - nowCreatSdpTime
});
peer.createOffer(constraints).then(
function (offer) {
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdp sucess:', offer);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferSucess',
time: new Date().getTime() - nowCreatSdpTime
});
return peer.setLocalDescription(offer);
})
.then(
function () {
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdp: ', peer.localDescription);
},
function () {
// rejected
console.log('[TESTRTCPeerConnection] promiseCreateOffer rejected ');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferReject',
time: new Date().getTime() - nowCreatSdpTime
});
},
function () {
// progress
console.log('[TESTRTCPeerConnection] promiseCreateOffer progress ');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferProgress',
time: new Date().getTime() - nowCreatSdpTime
});
})
.catch(function (reason) {
// An error occurred, so handle the failure to connect
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdpError: ', reason);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferCatchFail',
time: new Date().getTime() - nowCreatSdpTime,
error: reason
});
});
};
self.addAnswerSDP = function (sdp) {
var answer = new SessionDescription({
type: 'answer',
sdp: sdp
});
console.log('[TESTRTCPeerConnection] adding answer-sdp', sdp);
peer.setRemoteDescription(answer, self.onAnswerSdpSuccess, self.onAnswerSdpError);
};
self.onAnswerSdpSuccess = function (msg) {
console.log('[TESTRTCPeerConnection] onSdpSuccess', msg);
if (options.onAnswerSdpSuccess) {
options.onAnswerSdpSuccess(msg);
}
};
self.onAnswerSdpError = function (error) {
console.log('[TESTRTCPeerConnection] onAnswerSdpError', error);
if (options.onAnswerSdpError) {
options.onAnswerSdpError(error);
}
};
self.addICE = function (candidate) {
peer.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
console.log('[TESTRTCPeerConnection] adding-ice', candidate.candidate);
};
nowCreatSdpTime = new Date().getTime();
console.log('[TESTRTCPeerConnection] createoffer start ', nowCreatSdpTime);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferStart'
});
if (window.moz) {
self.promiseCreateOffer();
} else {
self.createOffer();
}
console.log('[TESTRTCPeerConnection] createoffer end ', (new Date().getTime()) - nowCreatSdpTime);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferEnd',
time: (new Date().getTime()) - nowCreatSdpTime
});
} var mywebrtc = new TESTRTCPeerConnection({ liveuuid:"123456" });