Skip to content

Commit ea10376

Browse files
committed
tr_webui: update stream, add shutdown function
1 parent 0a1a242 commit ea10376

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

tr_webui/www/index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@
3434
<input id="ang-slider" type="text"/>
3535
</div>
3636
</div>
37+
38+
<div class="row my-4">
39+
<div class="col d-flex justify-content-center">
40+
<video id="stream" class="p-1 bg-dark"/>
41+
</div>
42+
</div>
3743

3844
<div class="row my-4">
3945
<div class="col">
40-
<div class="d-flex" style="width: 210px; height: 210px;">
46+
<div class="d-flex justify-content-center" style="width: 210px; height: 210px;">
4147
<div id="joystick"></div>
4248
</div>
4349
</div>
@@ -79,11 +85,6 @@
7985
</div>
8086
<div class="col-md-10"></div>
8187
</div>
82-
83-
84-
<div class="stream-wrapper">
85-
<video id="stream"/>
86-
</div>
8788

8889
<script src="scripts/jquery-3.3.1.slim.min.js"></script>
8990
<script src="scripts/bootstrap.min.js"></script>

tr_webui/www/scripts/stream.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ Stream.prototype.createPeerConnection = function() {
5454
this.peerConnection.ontrack = (event) => {
5555
console.log("[stream] remote stream added:", event.streams[0]);
5656
let remoteVideoElement = document.getElementById('stream');
57-
remoteVideoElement.srcObject = event.streams[0];
58-
remoteVideoElement.play();
57+
if(remoteVideoElement.paused) {
58+
remoteVideoElement.srcObject = event.streams[0];
59+
remoteVideoElement.play();
60+
}
5961
}
6062

6163
this.peerConnection.onremovestream = () => console.log('[stream] remove');
@@ -75,7 +77,7 @@ Stream.prototype.offer = function() {
7577
}
7678
};
7779
this.websocket.send(JSON.stringify(command));
78-
console.log("[stream] offer(), command=" + JSON.stringify(command));
80+
console.debug("[stream] offer(), command=" + JSON.stringify(command));
7981
}
8082

8183
Stream.prototype.open = function() {
@@ -88,7 +90,7 @@ Stream.prototype.addIceCandidates = function () {
8890
this.iceCandidates.forEach((candidate) => {
8991
this.peerConnection.addIceCandidate(candidate,
9092
function() {
91-
console.log("[stream] IceCandidate added: " + JSON.stringify(candidate));
93+
console.debug("[stream] IceCandidate added: " + JSON.stringify(candidate));
9294
},
9395
function(error) {
9496
console.error("[stream] addIceCandidate error: " + error);
@@ -103,7 +105,7 @@ Stream.prototype.message = function(event) {
103105
var msg = JSON.parse(event.data);
104106
var what = msg.what;
105107
var data = msg.data;
106-
console.log("[stream] message =" + what);
108+
console.debug("[stream] message =" + what);
107109

108110
switch (what) {
109111
case "offer":
@@ -158,15 +160,15 @@ Stream.prototype.message = function(event) {
158160

159161

160162
Stream.prototype._onRemoteSdpSuccess = function() {
161-
console.log('[stream] onRemoteSdpSucces()');
163+
console.debug('[stream] onRemoteSdpSucces()');
162164
this.remoteDesc = true;
163165
this.peerConnection.createAnswer((sessionDescription) => {
164166
this.peerConnection.setLocalDescription(sessionDescription);
165167
let request = JSON.stringify({
166168
what: "answer",
167169
data: JSON.stringify(sessionDescription)
168170
});
169-
console.log('[stream] sdp success', request);
171+
console.debug('[stream] sdp success', request);
170172

171173
this.websocket.send(request);
172174

tr_webui/www/scripts/webui.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var cmdVelPub;
99
var servoPub;
1010
var servoAngles;
1111
var stream;
12+
var twistIntervalID;
13+
var servoIntervalID;
1214

1315
function initROS() {
1416

@@ -207,6 +209,15 @@ function publishServos() {
207209
servoPub.publish(servoMsg);
208210
}
209211

212+
function shutdown() {
213+
clearInterval(twistIntervalID);
214+
clearInterval(servoIntervalID);
215+
cmdVelPub.unadvertise();
216+
servoPub.unadvertise();
217+
ros.close();
218+
stream.stop();
219+
}
220+
210221
window.onload = function () {
211222

212223
initROS();
@@ -218,9 +229,9 @@ window.onload = function () {
218229
$('#firmware-ver').text(result.firmware_ver);
219230
});
220231

221-
setInterval(() => publishTwist(), 50);
232+
twistIntervalID = setInterval(() => publishTwist(), 50);
222233

223-
setInterval(() => publishServos(), 50);
234+
servoIntervalID = setInterval(() => publishServos(), 50);
224235

225236
setInterval(function() {
226237
batteryClient.callService(new ROSLIB.ServiceRequest(), function(result) {
@@ -229,6 +240,7 @@ window.onload = function () {
229240
}, 1000);
230241

231242
stream = new Stream();
232-
window.onbeforeunload = () => stream.stop();
233243
stream.start();
244+
245+
window.addEventListener("beforeunload", () => shutdown());
234246
}

0 commit comments

Comments
 (0)