Skip to content

Commit

Permalink
Format with google-java-format
Browse files Browse the repository at this point in the history
  • Loading branch information
microkatz committed Aug 28, 2024
1 parent 7473156 commit 0ac26c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1342,12 +1342,16 @@ protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaF
? mediaFormat.getInteger(KEY_CROP_BOTTOM) - mediaFormat.getInteger(KEY_CROP_TOP) + 1
: mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
}
boolean hasPixelAspectRatio = (Util.SDK_INT >= 30) && mediaFormat != null && mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH)
&& mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT);
pixelWidthHeightRatio = hasPixelAspectRatio ?
(float)mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH) /
mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT)
: format.pixelWidthHeightRatio;
boolean hasPixelAspectRatio =
(Util.SDK_INT >= 30)
&& mediaFormat != null
&& mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH)
&& mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT);
pixelWidthHeightRatio =
hasPixelAspectRatio
? (float) mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH)
/ mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT)
: format.pixelWidthHeightRatio;
// The decoder applies the rotation when rendering to the surface. For 90 and 270 degree
// rotations, we need to flip the width, height and pixel aspect ratio to reflect the rotation
// that was applied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,27 @@ public void render_sendsVideoSizeChangeWithCurrentFormatValues() throws Exceptio

@Config(minSdk = 30)
@Test
public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoSizeChangeWithMediaFormatValues() throws Exception {
public void
render_withMediaCodecModifyingPixelAspectRatioWidthHeight_sendsVideoSizeChangeWithMediaFormatValues()
throws Exception {
MediaCodecAdapter.Factory codecAdapterFactory =
configuration ->
new ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio(
new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration));
new ForwardingSynchronousMediaCodecAdapter(
new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration)) {
@Override
public MediaFormat getOutputFormat() {
MediaFormat mediaFormat = adapter.getOutputFormat();
if (Util.SDK_INT >= 30) {
int pixelAspectRatioHeight = 1 << 30; // Max integer power of 2.
int pixelAspectRatioWidth = (int) (0.5f * pixelAspectRatioHeight);
mediaFormat.setInteger(
MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH, pixelAspectRatioWidth);
mediaFormat.setInteger(
MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT, pixelAspectRatioHeight);
}
return mediaFormat;
}
};
MediaCodecVideoRenderer mediaCodecVideoRendererWithCustomAdapter =
new MediaCodecVideoRenderer(
ApplicationProvider.getApplicationContext(),
Expand All @@ -773,9 +789,8 @@ public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoS
}
};
mediaCodecVideoRendererWithCustomAdapter.init(/* index= */ 0, PlayerId.UNSET, Clock.DEFAULT);
surface = new Surface(new SurfaceTexture(/* texName= */ 0));
mediaCodecVideoRendererWithCustomAdapter.handleMessage(Renderer.MSG_SET_VIDEO_OUTPUT, surface);

mediaCodecVideoRendererWithCustomAdapter.handleMessage(
Renderer.MSG_SET_VIDEO_OUTPUT, new Surface(new SurfaceTexture(/* texName= */ 0)));
FakeSampleStream fakeSampleStream =
new FakeSampleStream(
new DefaultAllocator(/* trimOnReset= */ true, /* individualAllocationSize= */ 1024),
Expand All @@ -801,17 +816,16 @@ public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoS

int positionUs = 0;
do {
mediaCodecVideoRendererWithCustomAdapter.render(positionUs, SystemClock.elapsedRealtime() * 1000);
mediaCodecVideoRendererWithCustomAdapter.render(
positionUs, SystemClock.elapsedRealtime() * 1000);
positionUs += 10;
} while (!mediaCodecVideoRendererWithCustomAdapter.isEnded());
shadowOf(testMainLooper).idle();

verify(eventListener)
.onVideoSizeChanged(
new VideoSize(
VIDEO_H264.width,
VIDEO_H264.height,
VIDEO_H264.pixelWidthHeightRatio / 2));
VIDEO_H264.width, VIDEO_H264.height, VIDEO_H264.pixelWidthHeightRatio / 2));
}

@Test
Expand Down Expand Up @@ -1968,28 +1982,6 @@ public int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo) {
}
}

private static final class ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio
extends ForwardingSynchronousMediaCodecAdapter {

ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio(MediaCodecAdapter adapter) {
super(adapter);
}

@Override
public MediaFormat getOutputFormat() {
MediaFormat mediaFormat = adapter.getOutputFormat();
if (Util.SDK_INT >= 30) {
int pixelAspectRatioHeight = 1 << 30;
int pixelAspectRatioWidth = (int) (0.5f * pixelAspectRatioHeight);
mediaFormat.setInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH,
pixelAspectRatioWidth);
mediaFormat.setInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT,
pixelAspectRatioHeight);
}
return mediaFormat;
}
}

private abstract static class ForwardingSynchronousMediaCodecAdapter
implements MediaCodecAdapter {
protected final MediaCodecAdapter adapter;
Expand Down

0 comments on commit 0ac26c4

Please sign in to comment.