1 /* 2 * Copyright (C) 2016 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 package com.google.android.exoplayer; 17 18 import android.support.annotation.Nullable; 19 20 import java.nio.ByteBuffer; 21 import java.util.ArrayList; 22 23 /** {@link MediaFormat} creation helper util */ 24 public class MediaFormatUtil { 25 26 /** 27 * Creates {@link MediaFormat} from {@link android.media.MediaFormat}. 28 * Since {@link com.google.android.exoplayer.TrackRenderer} uses {@link MediaFormat}, 29 * {@link android.media.MediaFormat} should be converted to be used with ExoPlayer. 30 */ 31 public static MediaFormat createMediaFormat(android.media.MediaFormat format) { 32 // TODO: Add test for this method. 33 String mimeType = format.getString(android.media.MediaFormat.KEY_MIME); 34 String language = getOptionalStringV16(format, android.media.MediaFormat.KEY_LANGUAGE); 35 int maxInputSize = 36 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_MAX_INPUT_SIZE); 37 int width = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_WIDTH); 38 int height = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_HEIGHT); 39 int rotationDegrees = getOptionalIntegerV16(format, "rotation-degrees"); 40 int channelCount = 41 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_CHANNEL_COUNT); 42 int sampleRate = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_SAMPLE_RATE); 43 int encoderDelay = getOptionalIntegerV16(format, "encoder-delay"); 44 int encoderPadding = getOptionalIntegerV16(format, "encoder-padding"); 45 ArrayList<byte[]> initializationData = new ArrayList<>(); 46 for (int i = 0; format.containsKey("csd-" + i); i++) { 47 ByteBuffer buffer = format.getByteBuffer("csd-" + i); 48 byte[] data = new byte[buffer.limit()]; 49 buffer.get(data); 50 initializationData.add(data); 51 buffer.flip(); 52 } 53 long durationUs = format.containsKey(android.media.MediaFormat.KEY_DURATION) 54 ? format.getLong(android.media.MediaFormat.KEY_DURATION) : C.UNKNOWN_TIME_US; 55 MediaFormat mediaFormat = new MediaFormat(null, mimeType, MediaFormat.NO_VALUE, 56 maxInputSize, durationUs, width, height, rotationDegrees, MediaFormat.NO_VALUE, 57 channelCount, sampleRate, language, MediaFormat.OFFSET_SAMPLE_RELATIVE, 58 initializationData, false, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, encoderDelay, 59 encoderPadding); 60 mediaFormat.setFrameworkFormatV16(format); 61 return mediaFormat; 62 } 63 64 /** 65 * Creates {@link MediaFormat} for audio track. 66 */ 67 public static MediaFormat createAudioMediaFormat(String mimeType, long durationUs, 68 int channelCount, int sampleRate) { 69 return MediaFormat.createAudioFormat(null, mimeType, MediaFormat.NO_VALUE, 70 MediaFormat.NO_VALUE, durationUs, channelCount, sampleRate, null, ""); 71 } 72 73 /** 74 * Creates {@link MediaFormat} for closed caption track. 75 */ 76 public static MediaFormat createTextMediaFormat(String mimeType, long durationUs) { 77 return new MediaFormat(null, mimeType, 0, MediaFormat.NO_VALUE, durationUs, 78 MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, 79 MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, "", 80 MediaFormat.OFFSET_SAMPLE_RELATIVE, null, false, MediaFormat.NO_VALUE, 81 MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE); 82 } 83 84 @Nullable 85 private static final String getOptionalStringV16(android.media.MediaFormat format, String key) { 86 return format.containsKey(key) ? format.getString(key) : null; 87 } 88 89 private static final int getOptionalIntegerV16(android.media.MediaFormat format, String key) { 90 return format.containsKey(key) ? format.getInteger(key) : MediaFormat.NO_VALUE; 91 } 92 93 } 94