SIP.js icon indicating copy to clipboard operation
SIP.js copied to clipboard

SDH: navigator.mediaDevices.getUserMedia is blocked in background

Open thadeu opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. Chrome now blocks the use of navigator.mediaDevices.getUserMedia in the background. So when SDH gets stuck because of this.

Describe the solution you'd like Allow using custom stream in accept

Additional context I can't open PR because my problem is in version 0.14.

thadeu avatar Jan 21 '22 15:01 thadeu

Example code solution

/**
 * SessionDescriptionHandler options.
 * These options are provided to various UserAgent methods (invite() for example)
 * and passed through on calls to getDescription() and setDescription().
 */
export interface SessionDescriptionHandlerOptions {
  modifiers?: SessionDescriptionHandlerModifiers;
  constraints?: { audio: boolean, video: boolean };
  streams?: any;
}

Into Web/SessionDescriptionHandler.ts

private acquire(constraints: any, streams?: any): any {
    // Default audio & video to true
    constraints = this.checkAndDefaultConstraints(constraints);

    return new Promise(async (resolve, reject) => {
      /*
       * Make the call asynchronous, so that ICCs have a chance
       * to define callbacks to `userMediaRequest`
       */
      this.logger.log("acquiring local media");
      this.emit("userMediaRequest", constraints);

      if (constraints.audio || constraints.video) {
        try {
          if (streams) {
            this.logger.log("using custom media streams");
          } else {
            this.logger.log("using navigator.mediaDevices.getUserMedia");
            streams = await navigator.mediaDevices.getUserMedia(constraints)
          }

          this.logger.log(`MediaStream active is ${streams.active}`)
          this.observer.trackAdded();
          this.emit("userMedia", streams);
          resolve(streams);
        } catch (e: any) {
          this.emit("userMediaFailed", e);
          reject(e);
        }
      } else {
        // Local streams were explicitly excluded.
        resolve([]);
      }
    .....
  }

Simple use:

invite.accept({
  sessionDescriptionHandlerOptions: {
    streams: new MediaStream(),
    constraints: {
      audio: true,
      video: false
    }
  }
})

thadeu avatar Jan 21 '22 16:01 thadeu

Please refer to pull request #972 for resolution.

john-e-riordan avatar Sep 29 '22 16:09 john-e-riordan