1 /* 2 * Copyright (C) 2024 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.mediav2.common.cts; 18 19 import static android.media.codec.Flags.apvSupport; 20 import static android.media.MediaMuxer.OutputFormat.MUXER_OUTPUT_FIRST; 21 import static android.media.MediaMuxer.OutputFormat.MUXER_OUTPUT_LAST; 22 23 import static org.junit.Assert.fail; 24 25 import android.media.MediaCodec; 26 import android.media.MediaFormat; 27 import android.media.MediaMuxer; 28 29 import java.io.File; 30 import java.io.IOException; 31 import java.nio.ByteBuffer; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.List; 36 37 /** 38 * Wrapper class for trying and testing muxers. 39 */ 40 public class MuxerUtils { 41 private static final String LOG_TAG = MuxerUtils.class.getSimpleName(); 42 43 private static final List<String> MEDIATYPE_LIST_FOR_TYPE_MP4 = new ArrayList<>( 44 Arrays.asList(MediaFormat.MIMETYPE_VIDEO_MPEG4, MediaFormat.MIMETYPE_VIDEO_H263, 45 MediaFormat.MIMETYPE_VIDEO_AVC, MediaFormat.MIMETYPE_VIDEO_HEVC, 46 MediaFormat.MIMETYPE_AUDIO_AAC)); 47 static { 48 if (CodecTestBase.IS_AT_LEAST_U) { 49 MEDIATYPE_LIST_FOR_TYPE_MP4.add(MediaFormat.MIMETYPE_VIDEO_AV1); 50 } 51 if (CodecTestBase.IS_AT_LEAST_B && apvSupport()) { 52 MEDIATYPE_LIST_FOR_TYPE_MP4.add(MediaFormat.MIMETYPE_VIDEO_APV); 53 } 54 } 55 private static final List<String> MEDIATYPE_LIST_FOR_TYPE_WEBM = 56 Arrays.asList(MediaFormat.MIMETYPE_VIDEO_VP8, MediaFormat.MIMETYPE_VIDEO_VP9, 57 MediaFormat.MIMETYPE_AUDIO_VORBIS, MediaFormat.MIMETYPE_AUDIO_OPUS); 58 private static final List<String> MEDIATYPE_LIST_FOR_TYPE_3GP = 59 Arrays.asList(MediaFormat.MIMETYPE_VIDEO_MPEG4, MediaFormat.MIMETYPE_VIDEO_H263, 60 MediaFormat.MIMETYPE_VIDEO_AVC, MediaFormat.MIMETYPE_AUDIO_AAC, 61 MediaFormat.MIMETYPE_AUDIO_AMR_NB, MediaFormat.MIMETYPE_AUDIO_AMR_WB); 62 private static final List<String> MEDIATYPE_LIST_FOR_TYPE_OGG = 63 Collections.singletonList(MediaFormat.MIMETYPE_AUDIO_OPUS); 64 muxOutput(String filePath, int muxerFormat, MediaFormat format, ByteBuffer buffer, ArrayList<MediaCodec.BufferInfo> infos)65 public static void muxOutput(String filePath, int muxerFormat, MediaFormat format, 66 ByteBuffer buffer, ArrayList<MediaCodec.BufferInfo> infos) throws IOException { 67 MediaMuxer muxer = null; 68 try { 69 muxer = new MediaMuxer(filePath, muxerFormat); 70 int trackID = muxer.addTrack(format); 71 muxer.start(); 72 for (MediaCodec.BufferInfo info : infos) { 73 if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) { 74 muxer.writeSampleData(trackID, buffer, info); 75 } 76 } 77 muxer.stop(); 78 } finally { 79 if (muxer != null) muxer.release(); 80 } 81 } 82 isMediaTypeContainerPairValid(String mediaType, int format)83 public static boolean isMediaTypeContainerPairValid(String mediaType, int format) { 84 boolean result = false; 85 if (format == MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4) { 86 result = MEDIATYPE_LIST_FOR_TYPE_MP4.contains(mediaType) 87 || mediaType.startsWith("application/"); 88 } else if (format == MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM) { 89 result = MEDIATYPE_LIST_FOR_TYPE_WEBM.contains(mediaType); 90 } else if (format == MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP) { 91 result = MEDIATYPE_LIST_FOR_TYPE_3GP.contains(mediaType); 92 } else if (format == MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG) { 93 result = MEDIATYPE_LIST_FOR_TYPE_OGG.contains(mediaType); 94 } 95 return result; 96 } 97 getMuxerFormatForMediaType(String mediaType)98 public static int getMuxerFormatForMediaType(String mediaType) { 99 for (int muxFormat = MUXER_OUTPUT_FIRST; muxFormat <= MUXER_OUTPUT_LAST; muxFormat++) { 100 if (isMediaTypeContainerPairValid(mediaType, muxFormat)) { 101 return muxFormat; 102 } 103 } 104 fail("no configured muxer support for " + mediaType); 105 return MUXER_OUTPUT_LAST; 106 } 107 getTempFilePath(String infix)108 public static String getTempFilePath(String infix) throws IOException { 109 return File.createTempFile("tmp" + infix, ".bin").getAbsolutePath(); 110 } 111 112 }