-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
235 lines (217 loc) · 9.07 KB
/
index.html
File metadata and controls
235 lines (217 loc) · 9.07 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RtpTransport echo</title>
<script type="module">
import {WHIPClient} from "./whip.js"
let cam;
const pcConfig = {
"iceServers": [
{"urls": "stun:stun4.l.google.com:19302"}
],
"bundlePolicy": "max-bundle", "iceCandidatePoolSize": 2
};
function sortByMimeTypes(codecs, preferredOrder) {
return codecs.sort((a, b) => {
const indexA = preferredOrder.indexOf(a.mimeType);
const indexB = preferredOrder.indexOf(b.mimeType);
const orderA = indexA >= 0 ? indexA : Number.MAX_VALUE;
const orderB = indexB >= 0 ? indexB : Number.MAX_VALUE;
return orderA - orderB;
});
}
function setCodecs(peerConnection){
// Get supported codecs the sort using preferred codecs
const supportedCodecs = RTCRtpReceiver.getCapabilities("video").codecs;
const preferredCodecs = ["video/H264", "video/VP8", "video/VP9"];
const sortedCodecs = sortByMimeTypes(supportedCodecs, preferredCodecs);
// Get transceiver for connection and set the preferences
const [transceiver] = peerConnection.getTransceivers();
transceiver.setCodecPreferences(sortedCodecs); // <---
}
var whipSettings;
function loadSettings(){
whipSettings = (localStorage.whipSettings) ?JSON.parse(localStorage.whipSettings):null;
if (whipSettings) {
document.getElementById("whipURL").value = whipSettings.whipURL;
document.getElementById("whipToken").value = whipSettings.token;
document.getElementById("viewURL").value = whipSettings.viewURL;
}
}
function saveSettings(){
var whipURL = document.getElementById("whipURL").value;
var token = document.getElementById("whipToken").value;
var viewURL = document.getElementById("viewURL").value;
whipSettings = { whipURL: whipURL , token:token , viewURL:viewURL };
localStorage.whipSettings = JSON.stringify(whipSettings);
}
async function loaded() {
console.log("loading");
const castb = document.getElementById("cast");
castb.addEventListener("click", castMe)
loadSettings();
cam = await navigator.mediaDevices.getUserMedia({video: {width: 640, height: 480}});
document.getElementById("source").srcObject = cam;
const sourcepc = new RTCPeerConnection(pcConfig);
sourcepc.addTrack(cam.getVideoTracks()[0]);
const offer = await sourcepc.createOffer();
await sourcepc.setLocalDescription(offer);
const sinkpc = new RTCPeerConnection(pcConfig);
sinkpc.ontrack = (e) => {
if (e.track.kind == 'video') {
console.log("got new video stream");
//toDupe = e.track;
document.getElementById("sink").srcObject = new MediaStream([e.track]);
const es = e.receiver.createEncodedStreams();
recvFrames(es);
}
}
sourcepc.onicecandidate = (e) => {
sinkpc.addIceCandidate(e.candidate)
}
sinkpc.onicecandidate = (e) => {
sourcepc.addIceCandidate(e.candidate)
}
await sinkpc.setRemoteDescription(offer);
setCodecs(sinkpc);
const answer = await sinkpc.createAnswer()
await sinkpc.setLocalDescription(answer);
await sourcepc.setRemoteDescription(answer);
//await doCopy();
}
var copywriter = null;
var whipOptions = {};
function setWhipOptions(t) {
console.log("prune codecs to just ours.")
let paras = t.sender.getParameters();
let codecs = paras.codecs;
codecs.forEach((codec)=> {
console.log(codec.mimeType);
})
var wcodec = codecs.find((codec) => codec.mimeType.toUpperCase().includes("H264"));
whipOptions.payloadType = wcodec.payloadType;
}
async function recvFrames(es){
const reader = es.readable.getReader();
const writer = es.writable.getWriter();
while (true) {
const {value: frame, done} = await reader.read();
if (done) return;
var dframe = new RTCEncodedVideoFrame(frame,whipOptions);
await writer.write(frame);
//console.log("frame");
if (copywriter){
copywriter.write(dframe);
//console.log("whipframe");
}
}
}
var streaming = false;
var whip = null;
async function castMe() {
saveSettings();
if (whipSettings) {
var whipURL = whipSettings.whipURL;
var viewURL = whipSettings.viewURL;
var whiptoken = whipSettings.token;
var castb = document.getElementById("cast");
if (!streaming) {
console.log("starting publish stream ");
//Get mic+cam
const pc = new RTCPeerConnection(pcConfig);
let t = pc.addTransceiver("video", {
'direction': 'sendonly'
});
console.log("bypass reencode step");
let enc = t.sender.createEncodedStreams()
copywriter = enc.writable.getWriter();
//Send all tracks
//Create whip client
whip = new WHIPClient();
//Start publishing
whip.publish(pc, whipURL, whiptoken)
.then(() => {
streaming = true;
console.log("Broadcast has begun.");
castb.innerText = "Stop Casting";
var icopy = document.getElementById("copy");
icopy.src = viewURL;
})
.catch((e) => {
console.error("Failed to begin broadcast: ", e);
});
} else {
console.log("stop publishing stream ");
whip.stop();
streaming = false;
castb.innerText = "Restart Casting";
}
}
}
async function doCopy(){
const sourcepc2 = new RTCPeerConnection(pcConfig);
let t = sourcepc2.addTransceiver("video", {
'direction': 'sendonly'
});
console.log("bypass reencode step");
copywriter = t.sender.createEncodedStreams().writable.getWriter();
//sourcepc2.addTrack(toDupe);
const offer2 = await sourcepc2.createOffer();
await sourcepc2.setLocalDescription(offer2);
const sinkpc2 = new RTCPeerConnection(pcConfig);
sinkpc2.ontrack = (e)=> {
if (e.track.kind == 'video') {
console.log("got copy video stream");
document.getElementById("copy").srcObject = new MediaStream([e.track]);
}
}
sourcepc2.onicecandidate = (e) => {
sinkpc2.addIceCandidate(e.candidate)
}
sinkpc2.onicecandidate = (e) => {
sourcepc2.addIceCandidate(e.candidate)
}
await sinkpc2.setRemoteDescription(offer2);
setCodecs(sinkpc2);
const answer2 = await sinkpc2.createAnswer()
await sinkpc2.setLocalDescription(answer2);
await sourcepc2.setRemoteDescription(answer2);
}
console.log("started");
document.addEventListener('DOMContentLoaded', loaded);
</script>
</head>
<body>
<h1>RtpTransport echo</h1>
<p>
This test takes video from a camera
sends it over a peer connection to a peer.
The peer forwards copies of the video (without re-encoding it) to a second peer.
</p>
<div>
<div class="form-group">
<label for="whipURL">WHIP URL</label>
<input type="url" class="form-control" id="whipURL">
</div>
<div class="form-group">
<label for="whipToken">WHIP Token </label>
<input type="text" class="form-control" id="whipToken" >
</div>
<div class="form-group">
<label for="viewURL">View URL</label>
<input type="url" class="form-control" id="viewURL">
</div>
</form>
<button id="cast" type="button">cast</button>
</div>
<table>
<tr><th>Source</th><th>Local peer</th><th>Copy</th></tr>
<tr>
<td><video id="source" autoplay="true"></video></td>
<td><video id="sink" autoplay="true"></video></td>
<td><iframe width="640" height="480" id="copy"/></td>
</tr>
</table>
</body>
</html>