Search code examples
javascriptwebrtcopentoktokbox

Unpublishing a publisher and trying to publish right after not working


I am using the tokbox javascript sdk in the browser. I am having trouble recreating a publisher. My situation is like this:

  1. I create a publisher which broadcasts audio and video
  2. At a certain point, I want to switch the audio source, to a different MediaStream source.
  3. I unpublish the previous publisher on the session and destroy it in the callback
  4. I try to create a new publisher with this new configuration.

Wheneven I do this I end up with the following error:

Session.publish, could not publish in a reasonable amount of time.

I've searched on SO and online for some answers. I have ran into people with similar problems like this or this but neither have answers to this issue.

Basically, I either need to find out how I can alter an existing publisher that is already broadcasting (in terms of audio and video source config), or I am doing something wrong with the lifecycle of the publisher when I am trying to create a new one.

Creating a second publisher while having the first seems to work okay, but first destroying one and then trying to create a new one seems to yield the issue described.

Here is the sample code:

// returns existing publisher
const existing = selectPublisher(getState()); 

if (existing) {
  session.unpublish(existing);
  existing.destroy();
  console.log('destroyed.');
}

const publisher = OT.initPublisher(
  'publisher-container',
  finalOptions,
  error => {
    console.log(error);
  }
);

session.publish(publisher, error => {
  console.log(error);
});

Initialisation of the publisher seems to work, it prints undefined. The print ends up with:

index.js:2177 OT.Publisher.onPublishingTimeout
index.js:2177 OT.exception :: title: Unable to Publish (1500) msg: ICEWorkflow
index.js:2177 1500 "Session.publish :: Could not publish in a reasonable amount of time"

I am using the opentok npm client package version 2.14.2

EDIT:

It seems that the problem is caused by the options I was passing into the publisher, the example above works fine if I just use the default options (not pass in any into the publisher).

I am using two MediaStreamTrack objects, one for audio and video, gathered using OT.getUserMedia(options). It looks like the first time it is working fine, but as soon as I unpublish and republish with the same two MediaStreamTrack objects I run into this issue.

The object I'm passing it look like this (from chrome console):

audioSource: MediaStreamTrack {kind: "audio", id: "9414787b-82b9-48c3-99bd-3208b46c2f9f", label: "Built-in Microphone", enabled: true, muted: false, …}

videoSource: MediaStreamTrack {kind: "video", id: "3f20c926-7d0c-4537-8e8d-ef4b22393a58", label: "FaceTime HD Camera", enabled: true, muted: false, …}


Solution

  • TokBox Developer Evangelist here.

    When you destroy the publisher, we call the stop method on the MediaStreamTrack which is why you're unable to use the same MediaStreamTrack options again. To prevent this from happening, call the clone method on the MediaStreamTrack and pass in the clone as the options when you initialize the publisher.