-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.html
183 lines (150 loc) · 4.69 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
button {
width: 80px;
margin-right: 2px;
}
textarea {
font-family: monospace;
min-height: 200px;
}
.form-control[readonly] {
background-color: #ffffff;
}
.no-left-padding {
padding-left: 0 !important;
margin: 0 !important;
}
.no-right-padding {
padding-right: 0 !important;
margin: 0 !important;
}
</style>
<script>
function createSession() {
stopSession()
printLog('Creating session...')
pc = new RTCPeerConnection({
'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]
})
pc.ontrack = function(event) {
printLog('Accepting new track')
var el = document.createElement(event.track.kind)
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
document.getElementById('tracks').appendChild(el)
}
pc.oniceconnectionstatechange = function(event) {
printLog('ICE connection state changed to '+pc.iceConnectionState)
}
pc.addTransceiver('audio', {'direction': 'sendrecv'})
mediaOpts = {
audio: true,
video: false,
}
navigator.mediaDevices.getUserMedia(mediaOpts).
then(addMic).
catch(skipMic)
}
function addMic(stream) {
printLog('Adding microphone to session...')
let track = stream.getTracks()[0]
pc.addTrack(track, stream)
createOffer()
}
function skipMic(err) {
printLog('Skipping microphone configuration: '+err)
createOffer()
}
function createOffer() {
let offerOpts = {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': false,
},
}
pc.createOffer(offerOpts).
then(setLocalDescription).
catch(printLog)
}
function setLocalDescription(offer) {
pc.setLocalDescription(offer).then(setOffer)
}
function setOffer(offer) {
document.getElementById('offer').value = pc.localDescription.sdp
}
function startSession() {
let answer = document.getElementById('answer').value
if (answer === '') {
return printLog('Error: SDP answer is not set')
}
printLog('Starting session...')
let desc = new RTCSessionDescription({
'type': 'answer',
'sdp': answer,
})
pc.setRemoteDescription(desc).catch(printLog)
}
function stopSession() {
if (typeof pc === 'undefined') {
return
}
printLog('Stopping session...')
pc.close()
pc = undefined
document.getElementById('offer').value = ''
document.getElementById('answer').value = ''
document.getElementById('tracks').innerHTML = ''
}
function printLog(msg) {
log = document.getElementById('log')
log.value += msg + '\n'
log.scrollTop = log.scrollHeight
}
</script>
</head>
<body onload="window.createSession()">
<div class="container">
<div class="row">
<h1>WebRTC demo</h1>
</div>
<div class="row">
<div class="col-sm-6 no-left-padding">
<h2>Offer</h2>
<p><i>Copy this to webrtc-cli stdin and press ^D</i></p>
<textarea class="form-control" id="offer" readonly="true"></textarea>
</div>
<div class="col-sm-6 no-right-padding">
<h2>Answer</h2>
<p><i>Paste webrtc-cli stdout here and press "Start"</i></p>
<textarea class="form-control" id="answer"></textarea>
</div>
</div>
<div class="row">
<h2>Session</h2>
<button class="btn btn-primary" onclick="window.startSession()">
Start
</button>
<button class="btn btn-primary" onclick="window.stopSession()">
Stop
</button>
<button class="btn btn-primary" onclick="window.createSession()">
Refresh
</button>
</div>
<div class="row">
<h2>Tracks</h2>
<div id="tracks"></div>
</div>
<div class="row">
<h2>Logs</h2>
<textarea class="form-control" id="log" readonly="true"></textarea>
</div>
</div>
</body>
</html>