@@ -98,6 +98,66 @@ void staleSinkDropsFrames() {
9898 }
9999 }
100100
101+ @ Test
102+ void acceptsCustomBufferLayouts () {
103+ VideoDesktopSource source = new VideoDesktopSource ();
104+ TestProcessor processor = new TestProcessor ();
105+
106+ source .setVideoProcessor (processor );
107+
108+ try {
109+ // Tightly packed, odd-sized, and padded rows whose last row ends at the width.
110+ processor .sink .onVideoFrame (new VideoFrame (new LaidOutI420Buffer (64 , 48 ), 0 ));
111+ processor .sink .onVideoFrame (new VideoFrame (new LaidOutI420Buffer (5 , 3 ), 0 ));
112+ processor .sink .onVideoFrame (new VideoFrame (new LaidOutI420Buffer (64 , 48 ,
113+ 80 , 80 * 47 + 64 , 40 , 40 * 23 + 32 , 48 , 48 * 23 + 32 ), 0 ));
114+ }
115+ finally {
116+ source .dispose ();
117+ }
118+ }
119+
120+ @ Test
121+ void rejectsCustomBufferOverreads () {
122+ VideoDesktopSource source = new VideoDesktopSource ();
123+ TestProcessor processor = new TestProcessor ();
124+
125+ source .setVideoProcessor (processor );
126+
127+ int w = 64 ;
128+ int h = 48 ;
129+ int cw = 32 ;
130+ int ch = 24 ;
131+
132+ // Each would make the copy read past the end of a plane.
133+ LaidOutI420Buffer [] buffers = {
134+ new LaidOutI420Buffer (w , h , w - 1 , w * h , cw , cw * ch , cw , cw * ch ),
135+ new LaidOutI420Buffer (w , h , -w , w * h , cw , cw * ch , cw , cw * ch ),
136+ new LaidOutI420Buffer (w , h , w , w * h , cw - 1 , cw * ch , cw , cw * ch ),
137+ new LaidOutI420Buffer (w , h , w , w * h , cw , cw * ch , 0 , cw * ch ),
138+ new LaidOutI420Buffer (w , h , w , w * h - 1 , cw , cw * ch , cw , cw * ch ),
139+ new LaidOutI420Buffer (w , h , w * 2 , w * h , cw , cw * ch , cw , cw * ch ),
140+ new LaidOutI420Buffer (w , h , w , w * h , cw , cw * ch - 1 , cw , cw * ch ),
141+ new LaidOutI420Buffer (w , h , w , w * h , cw , cw * ch , cw , cw * (ch - 1 )),
142+ new LaidOutI420Buffer (w , h , w , w * h , Integer .MAX_VALUE , cw * ch , cw , cw * ch ),
143+ // Larger than WebRTC can allocate, with planes that claim to fit.
144+ new LaidOutI420Buffer (46341 , 46341 , 46341 , 16 , 23171 , 16 , 23171 , 16 ),
145+ new LaidOutI420Buffer (Integer .MAX_VALUE , 1 , Integer .MAX_VALUE , 16 , Integer .MAX_VALUE , 16 , Integer .MAX_VALUE , 16 ),
146+ };
147+
148+ try {
149+ for (LaidOutI420Buffer buffer : buffers ) {
150+ VideoFrame frame = new VideoFrame (buffer , 0 );
151+
152+ assertThrows (RuntimeException .class , () -> processor .sink .onVideoFrame (frame ),
153+ () -> buffer .toString ());
154+ }
155+ }
156+ finally {
157+ source .dispose ();
158+ }
159+ }
160+
101161 @ Test
102162 void processedDesktopFramesReachTrack () throws Exception {
103163 DesktopSource screen = firstScreen ();
@@ -250,6 +310,105 @@ private static void fill(ByteBuffer buffer, byte value) {
250310
251311
252312
313+ /**
314+ * An I420 buffer in Java with the strides and plane sizes it is given.
315+ */
316+ private static class LaidOutI420Buffer implements I420Buffer {
317+
318+ private final int width ;
319+ private final int height ;
320+ private final int strideY ;
321+ private final int strideU ;
322+ private final int strideV ;
323+ private final ByteBuffer dataY ;
324+ private final ByteBuffer dataU ;
325+ private final ByteBuffer dataV ;
326+
327+
328+ LaidOutI420Buffer (int width , int height ) {
329+ this (width , height , width , width * height , (width + 1 ) / 2 ,
330+ ((width + 1 ) / 2 ) * ((height + 1 ) / 2 ), (width + 1 ) / 2 ,
331+ ((width + 1 ) / 2 ) * ((height + 1 ) / 2 ));
332+ }
333+
334+ LaidOutI420Buffer (int width , int height , int strideY , int sizeY , int strideU , int sizeU ,
335+ int strideV , int sizeV ) {
336+ this .width = width ;
337+ this .height = height ;
338+ this .strideY = strideY ;
339+ this .strideU = strideU ;
340+ this .strideV = strideV ;
341+ this .dataY = ByteBuffer .allocateDirect (sizeY );
342+ this .dataU = ByteBuffer .allocateDirect (sizeU );
343+ this .dataV = ByteBuffer .allocateDirect (sizeV );
344+ }
345+
346+ @ Override
347+ public ByteBuffer getDataY () {
348+ return dataY .slice ();
349+ }
350+
351+ @ Override
352+ public ByteBuffer getDataU () {
353+ return dataU .slice ();
354+ }
355+
356+ @ Override
357+ public ByteBuffer getDataV () {
358+ return dataV .slice ();
359+ }
360+
361+ @ Override
362+ public int getStrideY () {
363+ return strideY ;
364+ }
365+
366+ @ Override
367+ public int getStrideU () {
368+ return strideU ;
369+ }
370+
371+ @ Override
372+ public int getStrideV () {
373+ return strideV ;
374+ }
375+
376+ @ Override
377+ public int getWidth () {
378+ return width ;
379+ }
380+
381+ @ Override
382+ public int getHeight () {
383+ return height ;
384+ }
385+
386+ @ Override
387+ public I420Buffer toI420 () {
388+ return this ;
389+ }
390+
391+ @ Override
392+ public VideoFrameBuffer cropAndScale (int cropX , int cropY , int cropWidth ,
393+ int cropHeight , int scaleWidth , int scaleHeight ) {
394+ throw new UnsupportedOperationException ();
395+ }
396+
397+ @ Override
398+ public void retain () {
399+ }
400+
401+ @ Override
402+ public void release () {
403+ }
404+
405+ @ Override
406+ public String toString () {
407+ return String .format ("%dx%d, Y %d/%d, U %d/%d, V %d/%d" , width , height ,
408+ strideY , dataY .capacity (), strideU , dataU .capacity (), strideV , dataV .capacity ());
409+ }
410+ }
411+
253412 private static class TestProcessor implements VideoProcessor {
254413
255414 VideoTrackSink sink ;
0 commit comments