Skip to content

Commit 66f2dfb

Browse files
committed
Clamp the scaled width to at least one pixel
A malformed pixel aspect ratio could otherwise round a positive width down to zero, which is not a usable size. The platform view path additionally only scales a width that is known, so that Format.NO_VALUE is passed through unchanged instead of being turned into a scaled value.
1 parent 1bfb1c7 commit 66f2dfb

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/platformview/PlatformViewExoPlayerEventListener.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ protected void sendInitialized() {
3333
// Anamorphic content is stored with non-square pixels, so the coded width has to be
3434
// scaled by the pixel aspect ratio to obtain the display width. This is applied before
3535
// the rotation swap below, because Format reports the ratio for the unrotated frame.
36+
// Width is only scaled when it is known (Format.NO_VALUE is negative), and is clamped to at
37+
// least one pixel so that a malformed ratio cannot report a zero width.
3638
float pixelWidthHeightRatio = videoFormat.pixelWidthHeightRatio;
37-
if (pixelWidthHeightRatio > 0 && pixelWidthHeightRatio != 1f) {
38-
width = Math.round(width * pixelWidthHeightRatio);
39+
if (width > 0 && pixelWidthHeightRatio > 0 && pixelWidthHeightRatio != 1f) {
40+
width = Math.max(1, Math.round(width * pixelWidthHeightRatio));
3941
}
4042

4143
// Switch the width/height if video was taken in portrait mode and a rotation

‎packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/texture/TextureExoPlayerEventListener.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ protected void sendInitialized() {
3535
// scaled by the pixel aspect ratio to obtain the display width. ExoPlayer reports a
3636
// pixelWidthHeightRatio that already accounts for any applied rotation, so this is
3737
// correct regardless of the rotation correction computed below.
38+
// Clamped to at least one pixel so that a malformed ratio cannot report a zero width.
3839
float pixelWidthHeightRatio = videoSize.pixelWidthHeightRatio;
3940
if (pixelWidthHeightRatio > 0 && pixelWidthHeightRatio != 1f) {
40-
width = Math.round(width * pixelWidthHeightRatio);
41+
width = Math.max(1, Math.round(width * pixelWidthHeightRatio));
4142
}
4243

4344
// When the SurfaceTexture backend for Impeller is used, the preview should already

‎packages/video_player/video_player_android/android/src/test/java/io/flutter/plugins/videoplayer/PlatformViewExoPlayerEventListenerTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,31 @@ public void onPlaybackStateChangedReadyScalesThenSwaps_whenAnamorphicAndRotated9
110110
eventListener.onPlaybackStateChanged(Player.STATE_READY);
111111
verify(mockCallbacks).onInitialized(720, 405, 10L, 0);
112112
}
113+
114+
@Test
115+
public void onPlaybackStateChangedReadyReportsAtLeastOnePixel_whenPixelAspectRatioIsDegenerate() {
116+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
117+
118+
// A malformed ratio would otherwise round the width down to zero.
119+
Format format =
120+
new Format.Builder().setWidth(800).setHeight(400).setPixelWidthHeightRatio(0.0001f).build();
121+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
122+
when(mockExoPlayer.getDuration()).thenReturn(10L);
123+
124+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
125+
verify(mockCallbacks).onInitialized(1, 400, 10L, 0);
126+
}
127+
128+
@Test
129+
public void onPlaybackStateChangedReadyLeavesUnknownWidthUnscaled() {
130+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
131+
132+
// Format.NO_VALUE must not be turned into a bogus scaled width.
133+
Format format = new Format.Builder().setHeight(400).setPixelWidthHeightRatio(0.375f).build();
134+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
135+
when(mockExoPlayer.getDuration()).thenReturn(10L);
136+
137+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
138+
verify(mockCallbacks).onInitialized(Format.NO_VALUE, 400, 10L, 0);
139+
}
113140
}

‎packages/video_player/video_player_android/android/src/test/java/io/flutter/plugins/videoplayer/TextureExoPlayerEventListenerTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,17 @@ public void onPlaybackStateChangedReadyDoesNotScaleWidth_whenPixelAspectRatioIsS
168168
eventListener.onPlaybackStateChanged(Player.STATE_READY);
169169
verify(mockCallbacks).onInitialized(405, 720, 10L, rotationCorrection);
170170
}
171+
172+
@Test
173+
public void onPlaybackStateChangedReadyReportsAtLeastOnePixel_whenPixelAspectRatioIsDegenerate() {
174+
TextureExoPlayerEventListener eventListener =
175+
new TextureExoPlayerEventListener(mockExoPlayer, mockCallbacks, true);
176+
// A malformed ratio would otherwise round the width down to zero.
177+
VideoSize size = new VideoSize(800, 400, 0.0001f);
178+
when(mockExoPlayer.getVideoSize()).thenReturn(size);
179+
when(mockExoPlayer.getDuration()).thenReturn(10L);
180+
181+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
182+
verify(mockCallbacks).onInitialized(1, 400, 10L, 0);
183+
}
171184
}

0 commit comments

Comments
 (0)