1 /* 2 * Copyright (C) 2017 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.exoplayer2.ext.ffmpeg; 17 18 import com.google.android.exoplayer2.util.LibraryLoader; 19 import com.google.android.exoplayer2.util.MimeTypes; 20 21 /** 22 * This class is based on com.google.android.exoplayer2.ext.ffmpeg.FfmpegLibrary from ExoPlayer2 23 * in order to support mp2 decoder. 24 * Configures and queries the underlying native library. 25 */ 26 public final class FfmpegLibrary { 27 28 private static final LibraryLoader LOADER = 29 new LibraryLoader("avutil", "avresample", "avcodec", "ffmpeg"); 30 FfmpegLibrary()31 private FfmpegLibrary() {} 32 33 /** 34 * Overrides the names of the FFmpeg native libraries. If an application wishes to call this 35 * method, it must do so before calling any other method defined by this class, and before 36 * instantiating a {@link FfmpegAudioRenderer} instance. 37 */ setLibraries(String... libraries)38 public static void setLibraries(String... libraries) { 39 LOADER.setLibraries(libraries); 40 } 41 42 /** 43 * Returns whether the underlying library is available, loading it if necessary. 44 */ isAvailable()45 public static boolean isAvailable() { 46 return LOADER.isAvailable(); 47 } 48 49 /** 50 * Returns the version of the underlying library if available, or null otherwise. 51 */ getVersion()52 public static String getVersion() { 53 return isAvailable() ? ffmpegGetVersion() : null; 54 } 55 56 /** 57 * Returns whether the underlying library supports the specified MIME type. 58 */ supportsFormat(String mimeType)59 public static boolean supportsFormat(String mimeType) { 60 if (!isAvailable()) { 61 return false; 62 } 63 String codecName = getCodecName(mimeType); 64 return codecName != null && ffmpegHasDecoder(codecName); 65 } 66 67 /** 68 * Returns the name of the FFmpeg decoder that could be used to decode {@code mimeType}. 69 */ getCodecName(String mimeType)70 /* package */ static String getCodecName(String mimeType) { 71 switch (mimeType) { 72 case MimeTypes.AUDIO_MPEG_L2: 73 return "mp2"; 74 case MimeTypes.AUDIO_AC3: 75 return "ac3"; 76 default: 77 return null; 78 } 79 } 80 ffmpegGetVersion()81 private static native String ffmpegGetVersion(); ffmpegHasDecoder(String codecName)82 private static native boolean ffmpegHasDecoder(String codecName); 83 84 } 85