Skip to content

Commit

Permalink
Revise TransformationRequest MIME type validation.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 422333929
  • Loading branch information
hmsch authored and icbaker committed Jan 25, 2022
1 parent 86fdbd6 commit a62a189
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,16 +132,17 @@ public Builder setFlattenForSlowMotion(boolean flattenForSlowMotion) {
*
* @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
// where rotationDegrees is set in the Format.
// 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;
}
Expand All @@ -157,10 +160,13 @@ public Builder setResolution(int outputHeight) {
*
* @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;
}
Expand All @@ -177,10 +183,13 @@ public Builder setVideoMimeType(@Nullable String videoMimeType) {
*
* @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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public void build_removeAudioAndVideo_throws() {
() -> 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();
Expand All @@ -68,8 +66,6 @@ public void build_withUnsupportedAudioMimeType_throws() {
.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();
Expand Down

0 comments on commit a62a189

Please sign in to comment.