Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2014 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a709930b5d604512cce8 to your computer and use it in GitHub Desktop.
Save anonymous/a709930b5d604512cce8 to your computer and use it in GitHub Desktop.
GMP Testcase
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body onload="start()">
<div id="player1">
<video tabindex="0" id="pc1video" class="mainvideo" controls="controls" height="480" width="640"></video>
<video tabindex="0" id="localvideo1" controls="controls" height="120" width="160"></video></div>
<div id="player2">
<video tabindex="0" id="pc2video" class="mainvideo" controls="controls" height="480" width="640"></video>
<video tabindex="0" id="localvideo2" controls="controls" height="120" width="160"></video></div><br>
<script type="application/javascript;version=1.8">
let pc1video = document.getElementById("pc1video");
let pc2video = document.getElementById("pc2video");
let localvideo1 = document.getElementById("localvideo1");
let localvideo2 = document.getElementById("localvideo2");
let fake = true;
let oneway = false;
let audio_only = false;
let video_disable = false;
let audio_disable = false;
let requireh264 = true;
let prefermode0 = false;
let requireg722 = false;
let pc1;
let pc2;
let pc1_offer;
let pc2_answer;
let offer_constraints;
let answer_constraints;
function failed(code) { }
function step1(offer) {
pc1_offer = offer;
if (requireh264) {
offer.sdp = removeVP8(offer.sdp);
if (prefermode0) {
offer.sdp = preferMode0(offer.sdp);
}
pc1_offer = offer;
}
if (requireg722) {
offer.sdp = removeNonG722(offer.sdp);
pc1_offer = offer;
}
pc1.setLocalDescription(offer, step2, failed);
}
// Also remove mode 0 if it's offered. Note, we don't bother removing the fmtp lines, which makes a good test for some SDP parsing issues.
function removeVP8(sdp) {
updated_sdp = sdp.replace("a=rtpmap:120 VP8/90000\r\n","");
updated_sdp = updated_sdp.replace(/m=video ([0-9]+) RTP\/SAVPF ([0-9 ]*) 120/g, "m=video $1 RTP\/SAVPF $2");
updated_sdp = updated_sdp.replace(/m=video ([0-9]+) RTP\/SAVPF 120([0-9 ]*)/g, "m=video $1 RTP\/SAVPF$2");
updated_sdp = updated_sdp.replace("a=rtcp-fb:120 nack\r\n","");
updated_sdp = updated_sdp.replace("a=rtcp-fb:120 nack pli\r\n","");
updated_sdp = updated_sdp.replace("a=rtcp-fb:120 ccm fir\r\n","");
return updated_sdp;
}
// Remove anything that's not G.722 from the m=audio line
function removeNonG722(sdp) {
updated_sdp = sdp.replace(/m=audio ([0-9]+) RTP\/SAVPF ([0-9 ]*)/g, "m=audio $1 RTP\/SAVPF 9");
return updated_sdp;
}
function preferMode0(sdp) {
updated_sdp = updated_sdp.replace("126 97", "97 126");
return updated_sdp;
}
function step2() {
pc1.onicecandidate = function(obj) {
if (obj.candidate) {
pc2.addIceCandidate(obj.candidate);
}
}
pc2.setRemoteDescription(pc1_offer, step3, failed);
};
function step3() {
pc2.createAnswer(step4, failed, answer_constraints);
}
function step4(answer) {
pc2_answer = answer;
pc2.setLocalDescription(answer, step5, failed);
}
function step5() {
pc2.onicecandidate = function(obj) {
if (obj.candidate) {
pc1.addIceCandidate(obj.candidate);
}
}
pc1.setRemoteDescription(pc2_answer, function step6() { }, failed);
}
function start() {
pc1 = new mozRTCPeerConnection();
pc2 = new mozRTCPeerConnection();
pc1.onaddstream = function(obj) {
pc1video.mozSrcObject = obj.stream;
pc1video.play();
setTimeout(pc1video.play, 1000);
}
pc2.onaddstream = function(obj) {
pc2video.mozSrcObject = obj.stream;
pc2video.play();
setTimeout(pc2video.play, 1000);
}
var myrequest = { audio: true };
if (!(audio_only)) {
myrequest = { audio: true, video: true };
}
myrequest_reverse = JSON.parse(JSON.stringify(myrequest));
if (fake) {
myrequest_reverse.fake = true;
}
if (oneway) {
offer_constraints = { mandatory: { OfferToReceiveVideo : false, OfferToReceiveAudio: false } };
answer_constraints = { mandatory: { OfferToReceiveVideo : true, OfferToReceiveAudio: true } };
}
navigator.mozGetUserMedia(myrequest, function(video1) {
localvideo1.mozSrcObject = video1;
if (video_disable)
localvideo1.mozSrcObject.getVideoTracks()[0].enabled = false;
if (audio_disable)
localvideo1.mozSrcObject.getAudioTracks()[0].enabled = false;
localvideo1.play();
pc1.addStream(video1);
if (!oneway) {
navigator.mozGetUserMedia(myrequest_reverse, function(video2) {
localvideo2.mozSrcObject = video2;
localvideo2.play();
pc2.addStream(video2);
pc1.createOffer(step1, failed, offer_constraints);
}, failed);
} else {
pc1.createOffer(step1, failed, offer_constraints);
}
}, failed);
}
function stop() {
pc1.close();
pc2.close();
}
</script>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment