0

In my project I have WebRTC live stream which is drawn to canvas frame by frame like this:

//GET THE VIDEO STREAM
navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" }, audio: false })
    .then(function(stream) {
        video.srcObject = stream;
        setTimeout(RunningVideo, 0);
    })
    .catch(function(error) {
        console.error("Oops. Something is broken.", error);
    });

//DRAW TO CANVAS
async function RunningVideo() {

    context2d.drawImage(video, 0, 0, canvas2d.width, canvas2d.height);
    src1.data.set(context2d.getImageData(0, 0, canvas2d.width, canvas2d.height).data);

    cv.imshow("outputCanvas2d", src1);

    let begin = Date.now()
    let delay = 1000/FPS - (Date.now() - begin);
    setTimeout(RunningVideo, delay); 

    var GoodMatch = await Myorb_split()
    //further code
}

After some condition A is fullfilled I want to run an AR session as it is described here. However, for the time the AR session is running, I need to pause the WebRTC live stream. As far as I understand, to do that I need to pause the setTimeout() method itself. Please, correct me if I am wrong.

I would be very grateful if anyone could help me to understand how to pause the setTimeout(). Thank you.

Sapfirra
  • 7
  • 2
  • 3
    Either store the result of the call (the timer id) in a global variable and `clearTimeout(id)`, or have a global boolean flag and check `if (enabled)` before the `setTimeout(RunningVideo, delay)`. – Bergi Dec 05 '19 at 09:26
  • Does this answer your question? [javascript: pause setTimeout();](https://stackoverflow.com/questions/3969475/javascript-pause-settimeout) – er-han Dec 05 '19 at 09:28
  • Thank you. Now I understand it more or less. However, I also try to add an `addeventListener` 'click', so that when condition A is fulfilled and there is a 'click' event it would pause the `SetTimeOut()`. Do you have any ideas how to do that? – Sapfirra Dec 09 '19 at 14:59

0 Answers0