-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
109 lines (92 loc) · 4.31 KB
/
test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>使用RTCPeerConnection流式传输视频</h1>
<video id="localVideo" autoplay></video>
<video id="remoteVideo" autoplay></video>
<div>
<button id="startButton">Start</button>
<button id="callButton">Call</button>
<button id="hangupButton">Hang Up</button>
</div>
<script>
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
let localStream;
let remoteStream;
let localPeerConnection;
let remotePeerConnection;
const startButton = document.getElementById('startButton');
const callButton = document.getElementById('callButton');
const hangupButton = document.getElementById('hangupButton');
startButton.addEventListener('click', function () {
navigator.mediaDevices.getUserMedia({
video: true,
})
.then(function (mediaStream) {
localVideo.srcObject = mediaStream;
localStream = mediaStream;
}).catch(function (error) {
console.log(error);
});
});
callButton.addEventListener('click', function () {
startTime = window.performance.now();
// Get local media stream tracks.
// const videoTracks = localStream.getVideoTracks();
// const audioTracks = localStream.getAudioTracks();
// Create peer connections and add behavior.
localPeerConnection = new RTCPeerConnection(null);
localPeerConnection.addEventListener('icecandidate', function (event) {
console.log(event);
const peerConnection = event.target;
const iceCandidate = event.candidate;
// if (iceCandidate) {
// const newIceCandidate = new RTCIceCandidate(iceCandidate);
// const otherPeer = getOtherPeer(peerConnection);
// otherPeer.addIceCandidate(newIceCandidate)
// .then(() => {
// handleConnectionSuccess(peerConnection);
// }).catch((error) => {
// handleConnectionFailure(peerConnection, error);
// });
// }
});
// localPeerConnection.addEventListener(
// 'iceconnectionstatechange', handleConnectionChange);
remotePeerConnection = new RTCPeerConnection(null);
// remotePeerConnection.addEventListener('icecandidate', handleConnection);
// remotePeerConnection.addEventListener(
// 'iceconnectionstatechange', handleConnectionChange);
// remotePeerConnection.addEventListener('addstream', gotRemoteMediaStream);
// // Add local stream to connection and create offer to connect.
// localPeerConnection.addStream(localStream);
// localPeerConnection.createOffer(offerOptions)
// .then(createdOffer).catch(setSessionDescriptionError);
localPeerConnection.addStream(localStream);
localPeerConnection.createOffer({
offerToReceiveVideo: 1,
})
.then(function (description) {
localPeerConnection.setLocalDescription(description)
.then(() => {
// setLocalDescriptionSuccess(localPeerConnection);
}).catch(setSessionDescriptionError);
remotePeerConnection.setRemoteDescription(description)
.then(() => {
// setRemoteDescriptionSuccess(remotePeerConnection);
}).catch(setSessionDescriptionError);
remotePeerConnection.createAnswer()
.then(createdAnswer)
.catch(setSessionDescriptionError);
}).catch(setSessionDescriptionError);
});
</script>
</body>
</html>