export function makeAVObserver()

in src/providers/AVObserver.ts [12:55]


export function makeAVObserver(av: AudioVideoFacade, options: ObserverOptions): AudioVideoObserver {
  let shouldTryToStart = options.joinWithVideo;
  let started = false;
  let videoAvailable = false;

  const startVideoWhenReady = async () => {
    if (shouldTryToStart && !started && videoAvailable) {
      console.debug('Starting video.');
      await options.reselectVideo();
      av.startLocalVideoTile();
    }
  };

  return {
    audioVideoDidStop: (sessionStatus: MeetingSessionStatus): void => {
      if (sessionStatus.statusCode() === MeetingSessionStatusCode.AudioCallEnded) {
        options.onEnded();
      }
    },

    // This fires *before* audioVideoDidStart.
    videoAvailabilityDidChange: (availability: MeetingSessionVideoAvailability): void => {
      console.log('Video availability changed to', availability);
      videoAvailable = availability.canStartLocalVideo;
      startVideoWhenReady();
    },

    audioVideoDidStart: (): void => {
      console.log('AV did start.');
      started = true;
      startVideoWhenReady();

      options.reselectAudio().then(() => {
        if (options.joinMuted) {
          console.log('Automatically muting self on join.');
          av.realtimeMuteLocalAudio();
        } else {
          console.log('Automatically unmuting self on join.');
          av.realtimeUnmuteLocalAudio();
        }
      });
    },
  };
}