webrtc-landing/pc_audio_only_test.html

131 строка
3.3 KiB
HTML
Исходник Обычный вид История

2013-01-05 00:29:50 +04:00
<html>
<head>
2013-01-05 00:30:15 +04:00
<title>Simple PeerConnection Audio Test</title>
2013-01-05 00:29:50 +04:00
</head>
<body>
2013-01-05 00:30:15 +04:00
<h1>Simple PeerConnection Audio Test</h1>
2013-01-05 00:29:50 +04:00
2013-01-05 00:30:15 +04:00
<div><audio id="pc1audio" controls></audio></div><br/>
<div><audio id="pc2audio" controls></audio></div><br/>
2013-01-05 00:29:50 +04:00
<div><button id="tehbutton" onClick="start();">Start!</button></div><br/>
<div id="log"></div>
2013-01-05 00:30:15 +04:00
<div><audio id="localaudio" height="480" controls muted="true"></audio></div><br/>
2013-01-05 00:29:50 +04:00
<script type="application/javascript">
2013-01-05 00:29:50 +04:00
function log(msg) {
let div = document.getElementById("log");
div.innerHTML = div.innerHTML + "<p>" + msg + "</p>";
}
2013-01-05 00:30:15 +04:00
let pc1audio = document.getElementById("pc1audio");
let pc2audio = document.getElementById("pc2audio");
2013-01-05 00:29:50 +04:00
let button = document.getElementById("tehbutton");
2013-01-05 00:30:15 +04:00
let localaudio = document.getElementById("localaudio");
2013-01-05 00:29:50 +04:00
let pc1;
let pc2;
let pc1_offer;
let pc2_answer;
function failed(code) {
log("Failure callback: " + code);
}
// pc1.createOffer finished, call pc1.setLocal
function step1(offer) {
pc1_offer = offer;
pc1.setLocalDescription(offer, step2, failed);
}
// pc1.setLocal finished, call pc2.setRemote
function step2() {
pc1.onicecandidate = function(obj) {
if (obj.candidate) {
log("pc1 found ICE candidate: " + JSON.stringify(obj.candidate));
pc2.addIceCandidate(obj.candidate);
} else {
log("pc1 got end-of-candidates signal");
}
}
2013-01-05 00:29:50 +04:00
pc2.setRemoteDescription(pc1_offer, step3, failed);
};
// pc2.setRemote finished, call pc2.createAnswer
function step3() {
pc2.createAnswer(step4, failed);
}
// pc2.createAnswer finished, call pc2.setLocal
function step4(answer) {
pc2_answer = answer;
pc2.setLocalDescription(answer, step5, failed);
}
// pc2.setLocal finished, call pc1.setRemote
function step5() {
pc2.onicecandidate = function(obj) {
if (obj.candidate) {
log("pc2 found ICE candidate: " + JSON.stringify(obj.candidate));
pc1.addIceCandidate(obj.candidate);
} else {
log("pc2 got end-of-candidates signal");
}
}
2013-01-05 00:29:50 +04:00
pc1.setRemoteDescription(pc2_answer, step6, failed);
}
// pc1.setRemote finished, media should be running!
function step6() {
log("HIP HIP HOORAY");
}
function start() {
button.innerHTML = "Stop!";
button.onclick = stop;
2023-02-04 00:28:16 +03:00
pc1 = new RTCPeerConnection();
pc2 = new RTCPeerConnection();
2013-01-05 00:29:50 +04:00
pc1.onaddstream = function(obj) {
log("pc1 got remote stream from pc2 " + obj.type);
2013-01-05 00:30:15 +04:00
pc2audio.mozSrcObject = obj.stream;
pc2audio.play();
2013-01-05 00:29:50 +04:00
}
pc2.onaddstream = function(obj) {
log("pc2 got remote stream from pc1 " + obj.type);
2013-01-05 00:30:15 +04:00
pc1audio.mozSrcObject = obj.stream;
pc1audio.play();
2013-01-05 00:29:50 +04:00
}
2013-01-05 00:30:15 +04:00
navigator.mozGetUserMedia({audio:true}, function(audio1) {
// Add stream obtained from gUM to <audio> to start media flow.
localaudio.mozSrcObject = audio1;
localaudio.play();
pc1.addStream(audio1);
navigator.mozGetUserMedia({audio:true, fake:true}, function(audio2) {
pc2.addStream(audio2);
// Start the signaling.
pc1.createOffer(step1, failed);
2013-01-05 00:29:50 +04:00
}, failed);
}, failed);
}
function stop() {
pc1.close();
pc2.close();
button.innerHTML = "Start!";
button.onclick = start;
}
</script>
</html>