Skip to content

Commit

Permalink
tr_webui: update stream, add shutdown function
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Feb 12, 2019
1 parent 0a1a242 commit ea10376
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
13 changes: 7 additions & 6 deletions tr_webui/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@
<input id="ang-slider" type="text"/>
</div>
</div>

<div class="row my-4">
<div class="col d-flex justify-content-center">
<video id="stream" class="p-1 bg-dark"/>
</div>
</div>

<div class="row my-4">
<div class="col">
<div class="d-flex" style="width: 210px; height: 210px;">
<div class="d-flex justify-content-center" style="width: 210px; height: 210px;">
<div id="joystick"></div>
</div>
</div>
Expand Down Expand Up @@ -79,11 +85,6 @@
</div>
<div class="col-md-10"></div>
</div>


<div class="stream-wrapper">
<video id="stream"/>
</div>

<script src="scripts/jquery-3.3.1.slim.min.js"></script>
<script src="scripts/bootstrap.min.js"></script>
Expand Down
16 changes: 9 additions & 7 deletions tr_webui/www/scripts/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ Stream.prototype.createPeerConnection = function() {
this.peerConnection.ontrack = (event) => {
console.log("[stream] remote stream added:", event.streams[0]);
let remoteVideoElement = document.getElementById('stream');
remoteVideoElement.srcObject = event.streams[0];
remoteVideoElement.play();
if(remoteVideoElement.paused) {
remoteVideoElement.srcObject = event.streams[0];
remoteVideoElement.play();
}
}

this.peerConnection.onremovestream = () => console.log('[stream] remove');
Expand All @@ -75,7 +77,7 @@ Stream.prototype.offer = function() {
}
};
this.websocket.send(JSON.stringify(command));
console.log("[stream] offer(), command=" + JSON.stringify(command));
console.debug("[stream] offer(), command=" + JSON.stringify(command));
}

Stream.prototype.open = function() {
Expand All @@ -88,7 +90,7 @@ Stream.prototype.addIceCandidates = function () {
this.iceCandidates.forEach((candidate) => {
this.peerConnection.addIceCandidate(candidate,
function() {
console.log("[stream] IceCandidate added: " + JSON.stringify(candidate));
console.debug("[stream] IceCandidate added: " + JSON.stringify(candidate));
},
function(error) {
console.error("[stream] addIceCandidate error: " + error);
Expand All @@ -103,7 +105,7 @@ Stream.prototype.message = function(event) {
var msg = JSON.parse(event.data);
var what = msg.what;
var data = msg.data;
console.log("[stream] message =" + what);
console.debug("[stream] message =" + what);

switch (what) {
case "offer":
Expand Down Expand Up @@ -158,15 +160,15 @@ Stream.prototype.message = function(event) {


Stream.prototype._onRemoteSdpSuccess = function() {
console.log('[stream] onRemoteSdpSucces()');
console.debug('[stream] onRemoteSdpSucces()');
this.remoteDesc = true;
this.peerConnection.createAnswer((sessionDescription) => {
this.peerConnection.setLocalDescription(sessionDescription);
let request = JSON.stringify({
what: "answer",
data: JSON.stringify(sessionDescription)
});
console.log('[stream] sdp success', request);
console.debug('[stream] sdp success', request);

this.websocket.send(request);

Expand Down
18 changes: 15 additions & 3 deletions tr_webui/www/scripts/webui.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var cmdVelPub;
var servoPub;
var servoAngles;
var stream;
var twistIntervalID;
var servoIntervalID;

function initROS() {

Expand Down Expand Up @@ -207,6 +209,15 @@ function publishServos() {
servoPub.publish(servoMsg);
}

function shutdown() {
clearInterval(twistIntervalID);
clearInterval(servoIntervalID);
cmdVelPub.unadvertise();
servoPub.unadvertise();
ros.close();
stream.stop();
}

window.onload = function () {

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

setInterval(() => publishTwist(), 50);
twistIntervalID = setInterval(() => publishTwist(), 50);

setInterval(() => publishServos(), 50);
servoIntervalID = setInterval(() => publishServos(), 50);

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

stream = new Stream();
window.onbeforeunload = () => stream.stop();
stream.start();

window.addEventListener("beforeunload", () => shutdown());
}

0 comments on commit ea10376

Please sign in to comment.