Revise TransformationRequest MIME type validation.

PiperOrigin-RevId: 422333929
This commit is contained in:
hschlueter 2022-01-17 12:23:05 +00:00 committed by Ian Baker
parent 86fdbd6f6b
commit a62a189b41
3 changed files with 59 additions and 11 deletions

View File

@ -16,6 +16,8 @@
package androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkArgument;
import android.graphics.Matrix;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
@ -130,6 +132,7 @@ public final class TransformationRequest {
*
* @param outputHeight The output height in pixels.
* @return This builder.
* @throws IllegalArgumentException If the {@code outputHeight} is not supported.
*/
public Builder setResolution(int outputHeight) {
// TODO(b/209781577): Define outputHeight in the javadoc as height can be ambiguous for videos
@ -137,9 +140,9 @@ public final class TransformationRequest {
// TODO(b/201293185): Restructure to input a Presentation class.
// TODO(b/201293185): Check encoder codec capabilities in order to allow arbitrary
// resolutions and reasonable fallbacks.
if (outputHeight != C.LENGTH_UNSET && !SUPPORTED_OUTPUT_HEIGHTS.contains(outputHeight)) {
throw new IllegalArgumentException("Unsupported outputHeight: " + outputHeight);
}
checkArgument(
outputHeight == C.LENGTH_UNSET || SUPPORTED_OUTPUT_HEIGHTS.contains(outputHeight),
"Unsupported outputHeight: " + outputHeight);
this.outputHeight = outputHeight;
return this;
}
@ -157,10 +160,13 @@ public final class TransformationRequest {
*
* @param videoMimeType The MIME type of the video samples in the output.
* @return This builder.
* @throws IllegalArgumentException If the {@code videoMimeType} is non-null but not a video
* {@link MimeTypes MIME type}.
*/
public Builder setVideoMimeType(@Nullable String videoMimeType) {
// TODO(b/209469847): Validate videoMimeType here once deprecated
// Transformer.Builder#setOuputMimeType(String) has been removed.
checkArgument(
videoMimeType == null || MimeTypes.isVideo(videoMimeType),
"Not a video MIME type: " + videoMimeType);
this.videoMimeType = videoMimeType;
return this;
}
@ -177,10 +183,13 @@ public final class TransformationRequest {
*
* @param audioMimeType The MIME type of the audio samples in the output.
* @return This builder.
* @throws IllegalArgumentException If the {@code audioMimeType} is non-null but not an audio
* {@link MimeTypes MIME type}.
*/
public Builder setAudioMimeType(@Nullable String audioMimeType) {
// TODO(b/209469847): Validate audioMimeType here once deprecated
// Transformer.Builder#setOuputMimeType(String) has been removed.
checkArgument(
audioMimeType == null || MimeTypes.isAudio(audioMimeType),
"Not an audio MIME type: " + audioMimeType);
this.audioMimeType = audioMimeType;
return this;
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.transformer;
import static org.junit.Assert.assertThrows;
import androidx.media3.common.MimeTypes;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Unit test for {@link TransformationRequest.Builder}. */
@RunWith(AndroidJUnit4.class)
public class TransformationRequestBuilderTest {
@Test
public void setAudioMimeType_withVideoMimeType_throws() {
assertThrows(
IllegalArgumentException.class,
() -> new TransformationRequest.Builder().setAudioMimeType(MimeTypes.VIDEO_H264));
}
@Test
public void setVideoMimeType_withAudioMimeType_throws() {
assertThrows(
IllegalArgumentException.class,
() -> new TransformationRequest.Builder().setVideoMimeType(MimeTypes.AUDIO_AAC));
}
}

View File

@ -52,8 +52,6 @@ public class TransformerBuilderTest {
() -> new Transformer.Builder(context).setRemoveAudio(true).setRemoveVideo(true).build());
}
// TODO(b/209469847): Move this test to TransformationRequestBuilderTest once deprecated
// Transformer.Builder#setOuputMimeType(String) has been removed.
@Test
public void build_withUnsupportedAudioMimeType_throws() {
Context context = ApplicationProvider.getApplicationContext();
@ -68,8 +66,6 @@ public class TransformerBuilderTest {
.build());
}
// TODO(b/209469847): Move this test to TransformationRequestBuilderTest once deprecated
// Transformer.Builder#setOuputMimeType(String) has been removed.
@Test
public void build_withUnsupportedVideoMimeType_throws() {
Context context = ApplicationProvider.getApplicationContext();