function disposeCallFeatureViewVideo()

in packages/calling-stateful-client/src/CallFeatureStreamUtils.ts [206:279]


function disposeCallFeatureViewVideo(
  context: CallContext,
  internalContext: InternalCallContext,
  callId: string,
  stream: CallFeatureStreamState
): void {
  const streamEventType = 'disposeViewCallFeature';

  const streamType = stream.mediaStreamType;
  const callFeatureStreamId = stream && stream.id;

  const streamLogInfo = { callId, undefined, streamId: callFeatureStreamId, streamType };

  _logStreamEvent(EventNames.START_DISPOSE_STREAM, streamLogInfo);

  const featureName: CallFeatureStreamName = getStreamFeatureName(stream);

  if (streamEventType === 'disposeViewCallFeature') {
    setCallFeatureVideoRendererView(callId, featureName, context, streamType, undefined);
  }

  const renderInfo = internalContext.getCallFeatureRenderInfo(callId, featureName, stream.mediaStreamType);
  if (!renderInfo) {
    _logStreamEvent(EventNames.DISPOSE_INFO_NOT_FOUND, streamLogInfo);
    return;
  }

  // Nothing to dispose of or clean up -- we can safely exit early here.
  if (renderInfo.status === 'NotRendered') {
    _logStreamEvent(EventNames.STREAM_ALREADY_DISPOSED, streamLogInfo);
    return;
  }

  // Status is already marked as "stopping" so we can exit early here. This is because stopping only occurs
  // when the stream is being created in createView but hasn't been completed being created yet. The createView
  // method will see the "stopping" status and perform the cleanup
  if (renderInfo.status === 'Stopping') {
    _logStreamEvent(EventNames.STREAM_STOPPING, streamLogInfo);
    return;
  }

  // If the stream is in the middle of being rendered (i.e. has state "Rendering"), we need the status as
  // "stopping" without performing any cleanup. This will tell the `createView` method that it should stop
  // rendering and clean up the state once the view has finished being created.
  if (renderInfo.status === 'Rendering') {
    _logStreamEvent(EventNames.STREAM_STOPPING, streamLogInfo);
    internalContext.setCallFeatureRenderInfo(
      callId,
      featureName,
      streamType,
      renderInfo.stream,
      'Stopping',
      renderInfo.renderer
    );
    return;
  }

  if (renderInfo.renderer) {
    _logStreamEvent(EventNames.DISPOSING_RENDERER, streamLogInfo);
    renderInfo.renderer.dispose();
    // Else the state must be in the "Rendered" state, so we can dispose the renderer and clean up the state.
    internalContext.setCallFeatureRenderInfo(
      callId,
      featureName,
      streamType,
      renderInfo.stream,
      'NotRendered',
      undefined
    );
    setCallFeatureVideoRendererView(callId, featureName, context, streamType, undefined);
  } else {
    _logStreamEvent(EventNames.RENDERER_NOT_FOUND, streamLogInfo);
  }
}