private fixUpSubscriptionOrder()

in src/task/SubscribeAndReceiveSubscribeAckTask.ts [174:215]


  private fixUpSubscriptionOrder(sdp: string, videoSubscriptions: number[]): number[] {
    if (this.context.transceiverController.getMidForStreamId === undefined) {
      return videoSubscriptions;
    }

    const midsToStreamIds = new Map<string, number>();
    for (const streamId of videoSubscriptions) {
      // The local description will have been set by the time this task is running, so all
      // of the transceivers should have `mid` set by now (see comment above `getMidForStreamId`)
      const mid = this.context.transceiverController.getMidForStreamId(streamId);
      if (mid === undefined) {
        if (streamId !== 0) {
          // Send section or inactive section
          this.logger.warn(`Could not find MID for stream ID: ${streamId}`);
        }
        continue;
      }
      midsToStreamIds.set(mid, streamId);
    }

    const sections = new SDP(sdp).mediaSections();
    const newSubscriptions: number[] = [];
    for (const section of sections) {
      if (section.mediaType !== 'video') {
        continue;
      }

      if (section.direction === 'recvonly') {
        const streamId = midsToStreamIds.get(section.mid);
        if (streamId === undefined) {
          this.logger.warn(`Could not find stream ID for MID: ${section.mid}`);
          continue;
        }
        newSubscriptions.push(streamId);
      } else {
        newSubscriptions.push(0);
      }
    }
    this.logger.info(
      `Fixed up ${JSON.stringify(videoSubscriptions)} to ${JSON.stringify(
        newSubscriptions
      )} (may be same))}`