determineBoxFit static method

BoxFit determineBoxFit(
  1. RtcVideoRenderer renderer,
  2. MediaSourceKind source,
  3. BoxConstraints constraints,
  4. BuildContext context,
)

Calculates an optimal BoxFit mode for the provided renderer.

Implementation

static BoxFit determineBoxFit(
  RtcVideoRenderer renderer,
  MediaSourceKind source,
  BoxConstraints constraints,
  BuildContext context,
) {
  if (source == MediaSourceKind.display ||
      (renderer.width.value == 0 && renderer.height.value == 0)) {
    return BoxFit.contain;
  } else {
    bool contain = false;

    final double aspectRatio = renderer.width.value / renderer.height.value;

    if (context.isMobile) {
      // Video is horizontal.
      if (aspectRatio >= 1) {
        double width = constraints.maxWidth;
        double height =
            renderer.height.value *
            (constraints.maxWidth / renderer.width.value);
        double factor = constraints.maxHeight / height;
        contain = factor >= 3.0;
        if (factor < 1) {
          width =
              renderer.width.value *
              (constraints.maxHeight / renderer.height.value);
          height = constraints.maxHeight;
          factor = constraints.maxWidth / width;
          contain = factor >= 2.5;
        }
      }
      // Video is vertical.
      else {
        double width =
            renderer.width.value *
            (constraints.maxHeight / renderer.height.value);
        double height = constraints.maxHeight;
        double factor = constraints.maxWidth / width;
        contain = factor >= 3.9;
        if (factor < 1) {
          width = constraints.maxWidth;
          height =
              renderer.height.value *
              (constraints.maxWidth / renderer.width.value);
          factor = constraints.maxHeight / height;
          contain = factor >= 3.0;
        }
      }
    } else {
      // Video is horizontal.
      if (aspectRatio >= 1) {
        double width = constraints.maxWidth;
        double height =
            renderer.height.value *
            (constraints.maxWidth / renderer.width.value);
        double factor = constraints.maxHeight / height;
        contain = factor >= 2.41;
        if (factor < 1) {
          width =
              renderer.width.value *
              (constraints.maxHeight / renderer.height.value);
          height = constraints.maxHeight;
          factor = constraints.maxWidth / width;
          contain = factor >= 1.5;
        }
      }
      // Video is vertical.
      else {
        double width =
            renderer.width.value *
            (constraints.maxHeight / renderer.height.value);
        double height = constraints.maxHeight;
        double factor = constraints.maxWidth / width;
        contain = factor >= 2.0;
        if (factor < 1) {
          width = constraints.maxWidth;
          height =
              renderer.height.value *
              (constraints.maxWidth / renderer.width.value);
          factor = constraints.maxHeight / height;
          contain = factor >= 2.2;
        }
      }
    }

    return contain ? BoxFit.contain : BoxFit.cover;
  }
}