Skip to content

Commit 764337d

Browse files
authored
fix: normalize VideoFrame rotation to 0, 90, 180 or 270 (#457)
1 parent 73aac5d commit 764337d

3 files changed

Lines changed: 80 additions & 7 deletions

File tree

‎webrtc-javafx/src/test/java/dev/onvoid/webrtc/client/javafx/VideoViewTest.java‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ void mirrors() throws Exception {
143143
assertTrue(paint(view).getPixelReader().getColor(8, 24).getBrightness() > 0.75);
144144
}
145145

146+
@Test
147+
void anyRotationMultiple() throws Exception {
148+
// A rotation outside 0..270 is shown as its equal inside it.
149+
assertSamePixels(rotated(270), rotated(-90));
150+
assertSamePixels(rotated(90), rotated(450));
151+
152+
// Sideways, a 4:3 picture in a 4:3 view leaves bars on the sides.
153+
VideoView view = sized(64, 48);
154+
view.accept(frame(64, 48, 128, 128, -90));
155+
presented(view);
156+
157+
Image painted = paint(view);
158+
assertEquals(Color.BLACK, painted.getPixelReader().getColor(2, 24));
159+
assertGray(painted.getPixelReader().getColor(32, 24));
160+
}
161+
146162
@Test
147163
void showsRemoteVideo() throws Exception {
148164
SignalingPair link = Signaling.loopback();
@@ -219,11 +235,24 @@ private static <T> T onFx(Callable<T> action) throws Exception {
219235
return result.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
220236
}
221237

238+
/** What a 64x48 view paints of a 64x48 two-tone frame with the given rotation. */
239+
private Image rotated(int rotation) throws Exception {
240+
VideoView view = sized(64, 48);
241+
view.accept(frame(64, 48, 16, 235, rotation));
242+
presented(view);
243+
244+
return paint(view);
245+
}
246+
247+
private static VideoFrame frame(int width, int height, int left, int right) {
248+
return frame(width, height, left, right, 0);
249+
}
250+
222251
/**
223252
* A frame whose left half has luma {@code left} and right half {@code right}, with
224253
* neutral chroma.
225254
*/
226-
private static VideoFrame frame(int width, int height, int left, int right) {
255+
private static VideoFrame frame(int width, int height, int left, int right, int rotation) {
227256
NativeI420Buffer buffer = NativeI420Buffer.allocate(width, height);
228257
ByteBuffer y = buffer.getDataY();
229258

@@ -236,7 +265,7 @@ private static VideoFrame frame(int width, int height, int left, int right) {
236265
fill(buffer.getDataU());
237266
fill(buffer.getDataV());
238267

239-
return new VideoFrame(buffer, System.nanoTime());
268+
return new VideoFrame(buffer, rotation, System.nanoTime());
240269
}
241270

242271
private static void fill(ByteBuffer plane) {
@@ -245,6 +274,18 @@ private static void fill(ByteBuffer plane) {
245274
}
246275
}
247276

277+
private static void assertSamePixels(Image expected, Image actual) {
278+
assertEquals((int) expected.getWidth(), (int) actual.getWidth());
279+
assertEquals((int) expected.getHeight(), (int) actual.getHeight());
280+
281+
for (int y = 0; y < (int) expected.getHeight(); y++) {
282+
for (int x = 0; x < (int) expected.getWidth(); x++) {
283+
assertEquals(expected.getPixelReader().getArgb(x, y), actual.getPixelReader().getArgb(x, y),
284+
"Pixel " + x + "," + y);
285+
}
286+
}
287+
}
288+
248289
private static void assertGray(Color color) {
249290
assertNotNull(color);
250291
assertTrue(Math.abs(color.getRed() - color.getGreen()) < 0.04 && Math.abs(color.getGreen() - color.getBlue()) < 0.04

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoFrame.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class VideoFrame implements RefCounted {
3232
/** The underlying frame buffer. */
3333
public final VideoFrameBuffer buffer;
3434

35-
/** Rotation of the frame in degrees. */
35+
/** Clockwise rotation of the frame in degrees: 0, 90, 180 or 270. */
3636
public final int rotation;
3737

3838
/** Timestamp of the frame in nanoseconds. */
@@ -55,7 +55,9 @@ public VideoFrame(VideoFrameBuffer buffer, long timestampNs) {
5555
* Creates a new VideoFrame with the specified buffer, rotation and timestamp.
5656
*
5757
* @param buffer The video frame buffer containing the actual frame data.
58-
* @param rotation The rotation of the frame in degrees (must be a multiple of 90).
58+
* @param rotation The clockwise rotation of the frame in degrees (must be a multiple of 90).
59+
* Any multiple is taken, and kept as its equal in 0, 90, 180 or 270, so that
60+
* -90 and 450 are stored as 270 and 90.
5961
* @param timestampNs The timestamp of the frame in nanoseconds.
6062
*
6163
* @throws IllegalArgumentException If buffer is null or rotation is not a multiple of 90.
@@ -69,7 +71,7 @@ public VideoFrame(VideoFrameBuffer buffer, int rotation, long timestampNs) {
6971
}
7072

7173
this.buffer = buffer;
72-
this.rotation = rotation;
74+
this.rotation = ((rotation % 360) + 360) % 360;
7375
this.timestampNs = timestampNs;
7476
}
7577

‎webrtc/src/test/java/dev/onvoid/webrtc/client/VideoViewTests.java‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package dev.onvoid.webrtc.client;
1818

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -80,6 +81,22 @@ void mirrors() {
8081
assertTrue(brightness(paint(view).getRGB(8, 24)) > 192);
8182
}
8283

84+
@Test
85+
void anyRotationMultiple() {
86+
// A rotation outside 0..270 is shown as its equal inside it.
87+
assertArrayEquals(rotated(270), rotated(-90));
88+
assertArrayEquals(rotated(90), rotated(450));
89+
90+
// Sideways, a 4:3 picture in a 4:3 view leaves bars on the sides.
91+
VideoView view = new VideoView();
92+
view.setSize(64, 48);
93+
view.accept(frame(64, 48, 128, 128, -90));
94+
95+
BufferedImage painted = paint(view);
96+
assertEquals(Color.BLACK, new Color(painted.getRGB(2, 24)));
97+
assertGray(new Color(painted.getRGB(32, 24)));
98+
}
99+
83100
@Test
84101
void showsRemoteVideo() throws Exception {
85102
Peer[] peers = pair();
@@ -112,11 +129,24 @@ void showsRemoteVideo() throws Exception {
112129
assertEquals(320, snapshot.getWidth());
113130
}
114131

132+
/** The pixels a 64x48 view paints of a 64x48 two-tone frame with the given rotation. */
133+
private static int[] rotated(int rotation) {
134+
VideoView view = new VideoView();
135+
view.setSize(64, 48);
136+
view.accept(frame(64, 48, 16, 235, rotation));
137+
138+
return paint(view).getRGB(0, 0, 64, 48, null, 0, 64);
139+
}
140+
141+
private static VideoFrame frame(int width, int height, int left, int right) {
142+
return frame(width, height, left, right, 0);
143+
}
144+
115145
/**
116146
* A frame whose left half has luma {@code left} and right half {@code right}, with
117147
* neutral chroma.
118148
*/
119-
private static VideoFrame frame(int width, int height, int left, int right) {
149+
private static VideoFrame frame(int width, int height, int left, int right, int rotation) {
120150
NativeI420Buffer buffer = NativeI420Buffer.allocate(width, height);
121151
ByteBuffer y = buffer.getDataY();
122152

@@ -129,7 +159,7 @@ private static VideoFrame frame(int width, int height, int left, int right) {
129159
fill(buffer.getDataU());
130160
fill(buffer.getDataV());
131161

132-
return new VideoFrame(buffer, System.nanoTime());
162+
return new VideoFrame(buffer, rotation, System.nanoTime());
133163
}
134164

135165
private static void fill(ByteBuffer plane) {

0 commit comments

Comments
 (0)