doUpdateMirroring_(
      route, sourceUrn, mirrorSettings, opt_presentationId,
      opt_streamStartedCallback) {
    if (!this.currentSession) {
      return Promise.reject(new mr.mirror.Error(
          'No session to update streams on',
          mr.MirrorAnalytics.CapturingFailure.TAB_FAIL));
    }
    if (!this.currentSession.supportsUpdateStream()) {
      return Promise.reject(new mr.mirror.Error(
          'Session does not support updating stream',
          mr.MirrorAnalytics.CapturingFailure.TAB_FAIL));
    }

    let streamSwapped = false;
    return new Promise((resolve, reject) => {
      const captureParams = mr.mirror.Service.createCaptureParameters_(
          sourceUrn, mirrorSettings, opt_presentationId);
      new mr.mirror.MirrorMediaStream(captureParams)
          .start()
          .then(stream => {
            if (this.currentMediaStream_) {
              this.currentMediaStream_.setOnStreamEnded(null);
              this.currentMediaStream_.stop();
              this.recordStreamEnded();
            }
            this.currentMediaStream_ = stream;
            this.currentMediaStream_.setOnStreamEnded(this.cleanup_.bind(this));
            streamSwapped = true;

            if (opt_streamStartedCallback) {
              return opt_streamStartedCallback(route).promise;
            }
            return route;
          })
          .then(_ => {
            if (!this.currentSession) {
              throw new mr.mirror.Error('Session ended while updating stream');
            }
            if (!this.currentMediaStream_) {
              throw new mr.mirror.Error(
                  'Media stream ended before session could be updated.');
            }
            return this.currentSession.updateStream(
                /** @type {!MediaStream} */ (
                    this.currentMediaStream_.getMediaStream()));
          })
          .then(this.postProcessMirroring_.bind(
              this, route, sourceUrn, mirrorSettings))
          .then(() => resolve(route))
          .catch(err => {
            this.onStartError_(/** @type {!Error} */ (err));
            if (streamSwapped) {
              return this.cleanup_().then(() => {
                reject(err);
              });
            } else {
              reject(err);
            }
          });
    });
  }
/**
   * A helper method for updateMirroring.
   * @param {!mr.Route} route
   * @param {string} sourceUrn
   * @param {!mr.mirror.Settings} mirrorSettings
   * @param {string=} opt_presentationId
   * @param {(function(!mr.Route): !mr.CancellablePromise<!mr.Route>)=}
   *     opt_streamStartedCallback Callback to invoke after stream capture
   *     succeeded and before the mirror session is created. The callback may
   *     update the route.
   * @return {!Promise<!mr.Route>} A promise fulfilled
   *    when mirroring has started successfully.
   * @private
   */
A helper method for updateMirroring.
A helper method for updateMirroring .
doUpdateMirroring_(
      route, sourceUrn, mirrorSettings, opt_presentationId,
      opt_streamStartedCallback) {
    if (!this.currentSession) {
      return Promise.reject(new mr.mirror.Error(
          'No session to update streams on',
          mr.MirrorAnalytics.CapturingFailure.TAB_FAIL));
    }
    if (!this.currentSession.supportsUpdateStream()) {
      return Promise.reject(new mr.mirror.Error(
          'Session does not support updating stream',
          mr.MirrorAnalytics.CapturingFailure.TAB_FAIL));
    }
    let streamSwapped = false;
    return new Promise((resolve, reject) => {
      const captureParams = mr.mirror.Service.createCaptureParameters_(
          sourceUrn, mirrorSettings, opt_presentationId);
      new mr.mirror.MirrorMediaStream(captureParams)
          .start()
          .then(stream => {
            if (this.currentMediaStream_) {
              this.currentMediaStream_.setOnStreamEnded(null);
              this.currentMediaStream_.stop();
              this.recordStreamEnded();
            }
            this.currentMediaStream_ = stream;
            this.currentMediaStream_.setOnStreamEnded(this.cleanup_.bind(this));
            streamSwapped = true;
            if (opt_streamStartedCallback) {
              return opt_streamStartedCallback(route).promise;
            }
            return route;
          })
          .then(_ => {
            if (!this.currentSession) {
              throw new mr.mirror.Error('Session ended while updating stream');
            }
            if (!this.currentMediaStream_) {
              throw new mr.mirror.Error(
                  'Media stream ended before session could be updated.');
            }
            return this.currentSession.updateStream(
                 (
                    this.currentMediaStream_.getMediaStream()));
          })
          .then(this.postProcessMirroring_.bind(
              this, route, sourceUrn, mirrorSettings))
          .then(() => resolve(route))
          .catch(err => {
            this.onStartError_( (err));
            if (streamSwapped) {
              return this.cleanup_().then(() => {
                reject(err);
              });
            } else {
              reject(err);
            }
          });
    });
  }
doUpdateMirroring_ ( route , sourceUrn , mirrorSettings , opt_presentationId , opt_streamStartedCallback ) { if ( ! this . currentSession ) { return Promise . reject ( new mr . mirror . Error ( 'No session to update streams on' , mr . MirrorAnalytics . CapturingFailure . TAB_FAIL ) ) ; } if ( ! this . currentSession . supportsUpdateStream ( ) ) { return Promise . reject ( new mr . mirror . Error ( 'Session does not support updating stream' , mr . MirrorAnalytics . CapturingFailure . TAB_FAIL ) ) ; } let streamSwapped = false ; return new Promise ( ( resolve , reject ) => { const captureParams = mr . mirror . Service . createCaptureParameters_ ( sourceUrn , mirrorSettings , opt_presentationId ) ; new mr . mirror . MirrorMediaStream ( captureParams ) . start ( ) . then ( stream => { if ( this . currentMediaStream_ ) { this . currentMediaStream_ . setOnStreamEnded ( null ) ; this . currentMediaStream_ . stop ( ) ; this . recordStreamEnded ( ) ; } this . currentMediaStream_ = stream ; this . currentMediaStream_ . setOnStreamEnded ( this . cleanup_ . bind ( this ) ) ; streamSwapped = true ; if ( opt_streamStartedCallback ) { return opt_streamStartedCallback ( route ) . promise ; } return route ; } ) . then ( _ => { if ( ! this . currentSession ) { throw new mr . mirror . Error ( 'Session ended while updating stream' ) ; } if ( ! this . currentMediaStream_ ) { throw new mr . mirror . Error ( 'Media stream ended before session could be updated.' ) ; } return this . currentSession . updateStream ( ( this . currentMediaStream_ . getMediaStream ( ) ) ) ; } ) . then ( this . postProcessMirroring_ . bind ( this , route , sourceUrn , mirrorSettings ) ) . then ( ( ) => resolve ( route ) ) . catch ( err => { this . onStartError_ ( ( err ) ) ; if ( streamSwapped ) { return this . cleanup_ ( ) . then ( ( ) => { reject ( err ) ; } ) ; } else { reject ( err ) ; } } ) ; } ) ; }
A helper method for updateMirroring.
A helper method for updateMirroring .
/** @type {!MediaStream} */ /** @type {!Error} */