-
Notifications
You must be signed in to change notification settings - Fork 4k
/
audio-only-streaming.html
87 lines (71 loc) · 2.83 KB
/
audio-only-streaming.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<blockquote style="background: #f3b7b7;border: 5px solid black;border-radius: 5px;padding: 5px 10px; margin: 10px 20px;">
This demo is <span style="border: 1px dotted red; background: yellow; padding: 2px 5px;">out-dated (published on 2013)</span>. Please check this instead: <a href="https://github.com/webrtc/samples">https://github.com/webrtc/samples</a>
</blockquote>
<script>
var offerer, answerer;
window.RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCSessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
window.RTCIceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.URL = window.webkitURL || window.URL;
window.iceServers = {
iceServers: [{
url: 'stun:23.21.150.121'
}
]
};
</script>
<script>
/* offerer */
function offererPeer(stream) {
offerer = new RTCPeerConnection(window.iceServers);
// stream is only attached by offerer
offerer.addStream(stream);
offerer.onicecandidate = function (event) {
if (!event || !event.candidate) return;
answerer.addIceCandidate(event.candidate);
};
offerer.createOffer(function (offer) {
console.debug('offer sdp', offer.sdp);
offerer.setLocalDescription(offer);
answererPeer(offer);
});
}
</script>
<script>
/* answerer */
function answererPeer(offer) {
answerer = new RTCPeerConnection(window.iceServers);
// stream that is flowing from offerer toward answerer
answerer.onaddstream = function (event) {
console.debug('remote stream', event.stream);
var audio = document.createElement('audio');
audio.src = URL.createObjectURL(event.stream);
document.body.appendChild(audio);
audio.controls = true;
audio.play();
};
answerer.onicecandidate = function (event) {
if (!event || !event.candidate) return;
offerer.addIceCandidate(event.candidate);
};
answerer.setRemoteDescription(offer);
answerer.createAnswer(function (answer) {
console.debug('answer sdp', answer.sdp);
answerer.setLocalDescription(answer);
offerer.setRemoteDescription(answer);
});
}
</script>
<script>
function getUserMedia(callback) {
navigator.getUserMedia({
audio: true,
video: false
}, callback, onerror);
function onerror(e) {
console.error(e);
}
}
getUserMedia(offererPeer);
</script>