-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafikart.html
130 lines (101 loc) · 3.81 KB
/
grafikart.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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>Web RTC</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
</style>
</head>
<body>
<p>References :</p>
<ul>
<li>https://github.com/feross/simple-peer</li>
<li>https://www.grafikart.fr/tutoriels/webrtc-864</li>
</ul>
<div class="container">
<div class="row">
<div class="col-sm-6">
IN
<video id="vid-in" controls width="100%" height="300px"></video>
<textarea id='offer' class="form-control"></textarea>
<p>
<button id="start" class="btn btn-primary">Start</button>
</p>
</div>
<div class="col-sm-6">
OUT
<video id="vid-out" controls width="100%" height="300px"></video>
<form id="incoming" >
<div class="form-group">
<textarea class="form-control"></textarea>
</div>
<p>
<a id="receive" href="#" class="btn btn-default" >Recevoir</a>
</p>
<p>
<button type="submit" style="display: none">Enregistrer l'offre</button>
</p>
</form>
</div>
</div>
</div>
</body>
<script src="simplepeer.min.js"></script>
<script>
function bindEvents(p) {
p.on('error', function (error) {
console.log(error)
})
p.on('signal', function (data) {
document.querySelector('#offer').textContent = JSON.stringify(data)
})
p.on('stream', function (stream) {
let $video = document.getElementById('vid-in');
$video.srcObject = stream;
$video.onloadedmetadata = function (e) {
$video.play();
}
})
document.querySelector('#incoming button').style.display = 'block'
document.querySelector('#incoming').addEventListener('submit', friendData(p))
console.log("Events bound, form activated")
}
function start(isInitiator) {
return function () {
function couldStartVideo(mediaStream) {
let p = new SimplePeer({
initiator: isInitiator,
stream: mediaStream,
trickle: false,
// config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }] }
})
bindEvents(p)
let $video = document.getElementById('vid-out');
$video.srcObject = mediaStream;
$video.onloadedmetadata = function (e) {
$video.play();
}
}
function failStartVideo(err) {
console.log(err.name + ": " + err.message);
}
let constraints = {audio: false, video: true};
navigator.mediaDevices.getUserMedia(constraints).then(couldStartVideo).catch(failStartVideo)
console.log("Started self-video on out ")
}
}
function friendData(p) {
return function (e) {
e.preventDefault()
let friendDataX = JSON.parse(e.target.querySelector('textarea').value)
p.signal(friendDataX)
}
}
document.querySelector('#start').addEventListener('click', start(true))
document.querySelector('#receive').addEventListener('click', start(false))
</script>
</html>