-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrtc.html
96 lines (83 loc) · 2.47 KB
/
rtc.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RTC Player</title>
</head>
<style>
#dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
padding: 10px;
border-radius: 4px;
z-index: 9999;
}
.modal-footer {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.modal-footer button {
cursor: pointer;
margin-right: 10px;
}
</style>
<body>
<div style="margin-top: 10px; margin-left: 10px;">
<input style="width: 100px;" id="source" type="text" value="hls/mystream"/>
<button onclick="play()"> 播放</button>
</div>
<div style="margin-top: 10px;">
<div style="float: left">
<video id="videoview" width="310" autoplay muted controls ></video>
</div>
</div>
</body>
</html>
<script>
async function play() {
let remote_view = document.getElementById("videoview");
let source = document.getElementById("source").value;
let pc = new RTCPeerConnection(null);
// pc.addTransceiver("audio", {direction: "recvonly"});
pc.addTransceiver("video", {direction: "recvonly"});
let offer = await pc.createOffer();
await pc.setLocalDescription(offer)
var data = {
type: "offer",
sdp: offer.sdp,
}
var stream = new MediaStream();
remote_view.srcObject = stream
pc.ontrack = function (event) {
if (event.streams.length === 0) {
return
}
stream.addTrack(event.track)
// remote_view.srcObject = event.streams[0]
// remote_view.play()
}
pc.onicegatheringstatechange = function (event) {
console.log("ice state:" + pc.iceConnectionState)
}
pc.onicecandidate = function (event) {
console.log("ice candidate:" + event.candidate)
}
console.log("offer:" + offer.sdp);
let url = source + ".rtc";
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json',
}),
}).then((res) => res.json())
.then((data) => {
console.log("拉流响应:" + data["sdp"])
pc.setRemoteDescription({type: 'answer', sdp: data["sdp"]})
})
}
</script>