Janus.js: Refreshing ice credentials

Hi all,

Try as I might I cannot understand how I meant to handle soon to be expired iceCredentials for long lived streams in janus.js when using the turn rest api. The setup is simple I have janus-server one end and then am using the streaming plugin to view the stream in chrome using Janus.js

I am feeling really stupid so any help would be appreciated.

My current approach is:

  1. Are we nearing the end of the TTL for the credential
  2. If yes, ask the REST api for new turn credentials
  3. Get the PeerConnection (pc) from the plugin and call pc.setConfiguration as appropriate with new iceServers and credentials
  4. Call pc.createOffer({ iceRestart: true}) ( do I need to do this in the stream is up? )
  5. If that’s successful then send the pc.localDescription to the plugin
          cameraHandler.plugin.send({
            message: { request: 'configure', update: 'true' },
            jsep: pc.localDescription
          });
          console.log("TURN credentials updated");
          console.log(pc.localDescription);

This all seems to work… However about 5 seconds after the TURN credentials updated is logged the stream disconnects.

In chrome:://webrtc-internals I see the following when performing the above.

Everything is working beautifully at the start of the stream, but I think I must be fundamentally missing some concept about how this is meant to work in practice that’s causing these failures.

Any help would be greatly appreciated.

edit:

I am wondering if I’ve compelely misunderstood how this works, do you only need to get the credentials at the begging of the request and it doesn’t matter how long your stream stays up?

Looking at the code in janus-gateway I can’t see how it would update the turn server credentials it’s using with an in-flight stream ( good chance I am missing something there though… )

I think I was indeed being stupid, there’s no need to manually create the offer here, instead you should use the plugin createOffer method.

       const pc = cameraHandler.plugin.webrtcStuff.pc

      pc.setConfiguration({
        iceServers: [
          {
            urls: iceServers.uris,
            username: iceServers.username,
            credential: iceServers.password
          }
        ]
      })   
      
      console.log("Creating new offer")
      console.log(cameraHandler.plugin)

      cameraHandler.plugin.createOffer({ 
        iceRestart: true,
        success: (success) => {
          console.log("Success")
          console.log("updated peer connection")
        }
      })

I am still unsure how the janus-gateway does this if it ends up using TURN with rest.