Skip to content

fix: tighten VideoTrackRenderer Widget #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions lib/src/widgets/video_track_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
rtc.VideoRenderer? _renderer;
// for flutter web only.
bool _rendererReadyForWeb = false;
double? _aspectRatio;
EventsListener<TrackEvent>? _listener;
// Used to compute visibility information
late GlobalKey _internalKey;
Expand Down Expand Up @@ -107,6 +108,7 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {

void disposeRenderer() {
try {
_renderer?.onResize = null;
_renderer?.srcObject = null;
_renderer?.dispose();
_renderer = null;
Expand Down Expand Up @@ -153,6 +155,13 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
// force recompute of mirror mode
setState(() {});
});
_renderer?.onResize = () {
if (mounted) {
setState(() {
_aspectRatio = _renderer?.videoValue.aspectRatio;
});
}
};
}

@override
Expand Down Expand Up @@ -254,8 +263,46 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
// FutureBuilder will cause flickering for flutter web. so using
// different rendering methods for web and native.
@override
Widget build(BuildContext context) =>
kIsWeb ? _videoViewForWeb() : _videoViewForNative();
Widget build(BuildContext context) {
final child = kIsWeb ? _videoViewForWeb() : _videoViewForNative();

if (widget.fit == rtc.RTCVideoViewObjectFit.RTCVideoViewObjectFitCover) {
return child;
}

return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if (!constraints.hasBoundedWidth && !constraints.hasBoundedHeight) {
return child;
}
if (_aspectRatio == null) {
return child;
}

bool fixHeight;
if (!constraints.hasBoundedWidth) {
fixHeight = true;
} else if (!constraints.hasBoundedHeight) {
fixHeight = false;
} else {
// both width and height are bound, figure out which to fix based on aspect ratios
final constraintsAspectRatio =
constraints.maxWidth / constraints.maxHeight;
fixHeight = constraintsAspectRatio > _aspectRatio!;
}
final double width;
final double height;
if (fixHeight) {
height = constraints.maxHeight;
width = height * _aspectRatio!;
} else {
width = constraints.maxWidth;
height = width / _aspectRatio!;
}
return SizedBox(width: width, height: height, child: child);
},
);
}

bool _shouldMirror() {
// off for screen share
Expand Down