• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.android.exoplayer.util.MimeTypes;
21 
22 import java.nio.ByteBuffer;
23 import java.util.ArrayList;
24 
25 /** {@link MediaFormat} creation helper util */
26 public class MediaFormatUtil {
27 
28     /**
29      * Creates {@link MediaFormat} from {@link android.media.MediaFormat}.
30      * Since {@link com.google.android.exoplayer.TrackRenderer} uses {@link MediaFormat},
31      * {@link android.media.MediaFormat} should be converted to be used with ExoPlayer.
32      */
createMediaFormat(android.media.MediaFormat format)33     public static MediaFormat createMediaFormat(android.media.MediaFormat format) {
34         String mimeType = format.getString(android.media.MediaFormat.KEY_MIME);
35         String language = getOptionalStringV16(format, android.media.MediaFormat.KEY_LANGUAGE);
36         int maxInputSize =
37                 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_MAX_INPUT_SIZE);
38         int width = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_WIDTH);
39         int height = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_HEIGHT);
40         int rotationDegrees = getOptionalIntegerV16(format, "rotation-degrees");
41         int channelCount =
42                 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_CHANNEL_COUNT);
43         int sampleRate = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_SAMPLE_RATE);
44         int encoderDelay = getOptionalIntegerV16(format, "encoder-delay");
45         int encoderPadding = getOptionalIntegerV16(format, "encoder-padding");
46         ArrayList<byte[]> initializationData = new ArrayList<>();
47         for (int i = 0; format.containsKey("csd-" + i); i++) {
48             ByteBuffer buffer = format.getByteBuffer("csd-" + i);
49             byte[] data = new byte[buffer.limit()];
50             buffer.get(data);
51             initializationData.add(data);
52             buffer.flip();
53         }
54         long durationUs = format.containsKey(android.media.MediaFormat.KEY_DURATION)
55                 ? format.getLong(android.media.MediaFormat.KEY_DURATION) : C.UNKNOWN_TIME_US;
56         int pcmEncoding = MimeTypes.AUDIO_RAW.equals(mimeType) ? C.ENCODING_PCM_16BIT
57                 : MediaFormat.NO_VALUE;
58         MediaFormat mediaFormat = new MediaFormat(null, mimeType, MediaFormat.NO_VALUE,
59                 maxInputSize, durationUs, width, height, rotationDegrees, MediaFormat.NO_VALUE,
60                 channelCount, sampleRate, language, MediaFormat.OFFSET_SAMPLE_RELATIVE,
61                 initializationData, false, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, pcmEncoding,
62                 encoderDelay, encoderPadding, null, MediaFormat.NO_VALUE);
63         mediaFormat.setFrameworkFormatV16(format);
64         return mediaFormat;
65     }
66 
67     @Nullable
getOptionalStringV16(android.media.MediaFormat format, String key)68     private static String getOptionalStringV16(android.media.MediaFormat format, String key) {
69         return format.containsKey(key) ? format.getString(key) : null;
70     }
71 
getOptionalIntegerV16(android.media.MediaFormat format, String key)72     private static int getOptionalIntegerV16(android.media.MediaFormat format, String key) {
73         return format.containsKey(key) ? format.getInteger(key) : MediaFormat.NO_VALUE;
74     }
75 
76 }
77