1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.test.mediahelper; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.media.MediaExtractor; 24 import android.media.MediaFormat; 25 import android.util.Log; 26 27 import java.io.FileDescriptor; 28 import java.io.IOException; 29 import java.util.concurrent.TimeUnit; 30 31 /** Helper functions for validating media files. */ 32 public class MediaValidationHelper { 33 private static final String TAG = MediaValidationHelper.class.getSimpleName(); 34 private static final String VIDEO_MIME_TYPE = "video"; 35 private static final String AUDIO_MIME_TYPE = "audio"; 36 37 /** 38 * Validate video file. 39 * 40 * <p>This function validates that video contains at least 1 video track and 1 audio track. Also 41 * validate that the video track has expected height and width. It also checks the video track 42 * duration is bigger than the specified length. 43 * 44 * @param fileDescriptor The {@link FileDescriptor} of the video file. 45 * @param expHeight Expected video track height. 46 * @param expWidth Expected video track width. 47 * @param minDurationMillis Minimum duration in milli seconds. The video track duration should 48 * be longer than it. 49 * @throws IOException 50 */ validateVideo( FileDescriptor fileDescriptor, int expHeight, int expWidth, long minDurationMillis)51 public static void validateVideo( 52 FileDescriptor fileDescriptor, int expHeight, int expWidth, long minDurationMillis) 53 throws IOException { 54 MediaExtractor extractor = new MediaExtractor(); 55 extractor.setDataSource(fileDescriptor); 56 int numTracks = extractor.getTrackCount(); 57 assertThat(numTracks).isGreaterThan(1); 58 Log.d(TAG, String.format("Number of tracks: %d", numTracks)); 59 boolean hasVideoTrack = false; 60 boolean hasAudioTrack = false; 61 for (int i = 0; i < numTracks; i++) { 62 MediaFormat format = extractor.getTrackFormat(i); 63 String mime = format.getString(MediaFormat.KEY_MIME); 64 Log.d(TAG, String.format("Mime type: %s; format: %s", mime, format.toString())); 65 if (mime.contains(VIDEO_MIME_TYPE)) { 66 hasVideoTrack = true; 67 validateVideoTrackMediaFormat(format, expHeight, expWidth, minDurationMillis); 68 // TODO(b/164484222): Validate video track frame drop. 69 } else if (mime.contains(AUDIO_MIME_TYPE)) { 70 hasAudioTrack = true; 71 } 72 } 73 assertThat(hasVideoTrack).isTrue(); 74 assertThat(hasAudioTrack).isTrue(); 75 } 76 77 /** 78 * Validates that video data {@link MediaFormat} is equal to the expected width and height. And 79 * the duration is longer than certain value. 80 * 81 * @param format The {@link MediaFormat} of the video track. 82 * @param expHeight Expected video track height. 83 * @param expWidth Expected video track width. 84 * @param minDurationMillis Minimum duration in milli seconds. The video track duration should 85 * be longer than it. 86 */ validateVideoTrackMediaFormat( MediaFormat format, int expHeight, int expWidth, long minDurationMillis)87 private static void validateVideoTrackMediaFormat( 88 MediaFormat format, int expHeight, int expWidth, long minDurationMillis) { 89 long durationMillis = 90 TimeUnit.MICROSECONDS.toMillis(format.getLong(MediaFormat.KEY_DURATION)); 91 int width = format.getInteger(MediaFormat.KEY_WIDTH); 92 int height = format.getInteger(MediaFormat.KEY_HEIGHT); 93 Log.d( 94 TAG, 95 String.format( 96 "Duration: %d; Width: %d; Height: %d", durationMillis, width, height)); 97 assertThat(durationMillis).isGreaterThan(minDurationMillis); 98 assertThat(width).isEqualTo(expWidth); 99 assertThat(height).isEqualTo(expHeight); 100 } 101 102 /** 103 * Validates that the image file height and weight is greater than certain value. 104 * 105 * @param fileDescriptor The {@link FileDescriptor} of the image. 106 * @param minHeight Minimum height. The image height should be greater than this value. 107 * @param minWidth Minimum width. The image width should be greater than this value. 108 */ validateImage(FileDescriptor fileDescriptor, int minHeight, int minWidth)109 public static void validateImage(FileDescriptor fileDescriptor, int minHeight, int minWidth) { 110 Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); 111 int height = bitmap.getHeight(); 112 int width = bitmap.getWidth(); 113 Log.d(TAG, String.format("Height: %d; Width: %d", height, width)); 114 assertThat(height).isGreaterThan(minHeight); 115 assertThat(width).isGreaterThan(minWidth); 116 } 117 } 118