async run()

in src/task/SubscribeAndReceiveSubscribeAckTask.ts [46:162]


  async run(): Promise<void> {
    let localSdp = '';
    if (this.context.peer && this.context.peer.localDescription) {
      localSdp = new SDP(this.context.peer.localDescription.sdp).withUnifiedPlanFormat().sdp;
    }

    if (!this.context.enableSimulcast) {
      // backward compatibility
      let frameRate = 0;
      let maxEncodeBitrateKbps = 0;
      if (this.context.videoCaptureAndEncodeParameter) {
        frameRate = this.context.videoCaptureAndEncodeParameter.captureFrameRate();
        maxEncodeBitrateKbps = this.context.videoCaptureAndEncodeParameter.encodeBitrates()[0];
      }
      const param: RTCRtpEncodingParameters = {
        rid: 'hi',
        maxBitrate: maxEncodeBitrateKbps * 1000,
        maxFramerate: frameRate,
        active: true,
      };

      this.context.videoStreamIndex.integrateUplinkPolicyDecision([param]);
    }

    // See comment above `fixUpSubscriptionOrder`
    const videoSubscriptions = this.fixUpSubscriptionOrder(
      localSdp,
      this.context.videoSubscriptions
    );

    const isSendingStreams: boolean =
      this.context.videoDuplexMode === SdkStreamServiceType.TX ||
      this.context.videoDuplexMode === SdkStreamServiceType.DUPLEX;

    let compressedSDPOffer: Uint8Array | null;
    let localSdpOffer = localSdp;
    if (
      this.context.videoUplinkBandwidthPolicy.wantsVideoDependencyDescriptorRtpHeaderExtension ===
        undefined ||
      !this.context.videoUplinkBandwidthPolicy.wantsVideoDependencyDescriptorRtpHeaderExtension()
    ) {
      // See note above similar code in `SetLocalDescriptionTask`.
      localSdpOffer = new SDP(localSdpOffer).withoutDependencyDescriptorRtpHeaderExtension().sdp;
    }

    if (this.context.serverSupportsCompression) {
      // If the server supports compression, then send the compressed version of the sdp
      // and exclude the original sdp offer.
      const prevOffer = this.context.previousSdpOffer ? this.context.previousSdpOffer.sdp : '';
      compressedSDPOffer = this.textCompressor.compress(localSdpOffer, prevOffer);
      this.context.logger.info(
        `Compressed the SDP message from ${localSdpOffer.length} to ${compressedSDPOffer.length} bytes.`
      );
      localSdp = '';
    }
    this.context.previousSdpOffer = new SDP(localSdp);

    const subscribe = new SignalingClientSubscribe(
      this.context.meetingSessionConfiguration.credentials.attendeeId,
      localSdp,
      this.context.meetingSessionConfiguration.urls.audioHostURL,
      this.context.realtimeController.realtimeIsLocalAudioMuted(),
      false,
      videoSubscriptions,
      isSendingStreams,
      this.context.videoStreamIndex.localStreamDescriptions(),
      // TODO: handle check-in mode, or remove this param
      true,
      compressedSDPOffer
    );

    if (
      this.context.videoDownlinkBandwidthPolicy.getServerSideNetworkAdaption !== undefined &&
      !serverSideNetworkAdaptionIsNoneOrDefault(
        this.context.videoDownlinkBandwidthPolicy.getServerSideNetworkAdaption()
      ) &&
      this.context.videoDownlinkBandwidthPolicy.getVideoPreferences !== undefined
    ) {
      // Set initial configuration for the receive streams indicated by the rest of the subscribe
      subscribe.videoSubscriptionConfiguration = convertVideoPreferencesToSignalingClientVideoSubscriptionConfiguration(
        this.context,
        videoSubscriptions.map((streamId: number) => {
          return streamId === 0 ? 0 : this.context.videoStreamIndex.groupIdForStreamId(streamId);
        }),
        this.context.videoDownlinkBandwidthPolicy.getVideoPreferences()
      );
    }
    this.context.logger.info(`sending subscribe: ${JSON.stringify(subscribe)}`);
    this.context.signalingClient.subscribe(subscribe);

    const subscribeAckFrame = await this.receiveSubscribeAck();
    this.context.logger.info(`got subscribe ack: ${JSON.stringify(subscribeAckFrame)}`);

    let decompressedText = '';
    if (subscribeAckFrame.compressedSdpAnswer && subscribeAckFrame.compressedSdpAnswer.length) {
      decompressedText = this.textCompressor.decompress(
        subscribeAckFrame.compressedSdpAnswer,
        this.context.previousSdpAnswerAsString
      );

      if (decompressedText.length === 0) {
        this.context.sdpAnswer = '';
        this.context.previousSdpAnswerAsString = '';
        this.logAndThrow(`Error occurred while trying to decompress the SDP answer.`);
      }

      this.context.logger.info(
        `Decompressed the SDP message from ${subscribeAckFrame.compressedSdpAnswer.length} to ${decompressedText.length} bytes.`
      );
      this.context.sdpAnswer = decompressedText;
    } else {
      this.context.sdpAnswer = subscribeAckFrame.sdpAnswer;
    }
    this.context.previousSdpAnswerAsString = this.context.sdpAnswer;

    this.context.videoStreamIndex.integrateSubscribeAckFrame(subscribeAckFrame);
  }