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 import com.google.android.exoplayer.util.MimeTypes;
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}. Since {@link
28      * com.google.android.exoplayer.TrackRenderer} uses {@link MediaFormat}, {@link
29      * android.media.MediaFormat} should be converted to be used with ExoPlayer.
30      */
createMediaFormat(android.media.MediaFormat format)31     public static MediaFormat createMediaFormat(android.media.MediaFormat format) {
32         String mimeType = format.getString(android.media.MediaFormat.KEY_MIME);
33         String language = getOptionalStringV16(format, android.media.MediaFormat.KEY_LANGUAGE);
34         int maxInputSize =
35                 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_MAX_INPUT_SIZE);
36         int width = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_WIDTH);
37         int height = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_HEIGHT);
38         int rotationDegrees = getOptionalIntegerV16(format, "rotation-degrees");
39         int channelCount =
40                 getOptionalIntegerV16(format, android.media.MediaFormat.KEY_CHANNEL_COUNT);
41         int sampleRate = getOptionalIntegerV16(format, android.media.MediaFormat.KEY_SAMPLE_RATE);
42         int encoderDelay = getOptionalIntegerV16(format, "encoder-delay");
43         int encoderPadding = getOptionalIntegerV16(format, "encoder-padding");
44         ArrayList<byte[]> initializationData = new ArrayList<>();
45         for (int i = 0; format.containsKey("csd-" + i); i++) {
46             ByteBuffer buffer = format.getByteBuffer("csd-" + i);
47             byte[] data = new byte[buffer.limit()];
48             buffer.get(data);
49             initializationData.add(data);
50             buffer.flip();
51         }
52         long durationUs =
53                 format.containsKey(android.media.MediaFormat.KEY_DURATION)
54                         ? format.getLong(android.media.MediaFormat.KEY_DURATION)
55                         : C.UNKNOWN_TIME_US;
56         int pcmEncoding =
57                 MimeTypes.AUDIO_RAW.equals(mimeType) ? C.ENCODING_PCM_16BIT : MediaFormat.NO_VALUE;
58         MediaFormat mediaFormat =
59                 new MediaFormat(
60                         null, // trackId
61                         mimeType,
62                         MediaFormat.NO_VALUE,
63                         maxInputSize,
64                         durationUs,
65                         width,
66                         height,
67                         rotationDegrees,
68                         MediaFormat.NO_VALUE,
69                         channelCount,
70                         sampleRate,
71                         language,
72                         MediaFormat.OFFSET_SAMPLE_RELATIVE,
73                         initializationData,
74                         false,
75                         MediaFormat.NO_VALUE,
76                         MediaFormat.NO_VALUE,
77                         pcmEncoding,
78                         encoderDelay,
79                         encoderPadding,
80                         null, // projectionData
81                         MediaFormat.NO_VALUE,
82                         null // colorValue
83                         );
84         mediaFormat.setFrameworkFormatV16(format);
85         return mediaFormat;
86     }
87 
88     @Nullable
getOptionalStringV16(android.media.MediaFormat format, String key)89     private static String getOptionalStringV16(android.media.MediaFormat format, String key) {
90         return format.containsKey(key) ? format.getString(key) : null;
91     }
92 
getOptionalIntegerV16(android.media.MediaFormat format, String key)93     private static int getOptionalIntegerV16(android.media.MediaFormat format, String key) {
94         return format.containsKey(key) ? format.getInteger(key) : MediaFormat.NO_VALUE;
95     }
96 }
97