Search in sources :

Example 86 with MediaCodecInfo

use of android.media.MediaCodecInfo in project AndroidUSBCamera by jiangdongguo.

the class MediaSurfaceEncoder method selectVideoCodec.

/**
 * select the first codec that match a specific MIME type
 * @param mimeType
 * @return null if no codec matched
 */
protected static final MediaCodecInfo selectVideoCodec(final String mimeType) {
    if (DEBUG)
        Log.v(TAG, "selectVideoCodec:");
    // get the list of available codecs
    final int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        if (!codecInfo.isEncoder()) {
            // skipp decoder
            continue;
        }
        // select first codec that match a specific MIME type and color format
        final String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++) {
            if (types[j].equalsIgnoreCase(mimeType)) {
                if (DEBUG)
                    Log.i(TAG, "codec:" + codecInfo.getName() + ",MIME=" + types[j]);
                final int format = selectColorFormat(codecInfo, mimeType);
                if (format > 0) {
                    return codecInfo;
                }
            }
        }
    }
    return null;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo)

Example 87 with MediaCodecInfo

use of android.media.MediaCodecInfo in project AndroidUSBCamera by jiangdongguo.

the class MediaVideoBufferEncoder method prepare.

@Override
protected void prepare() throws IOException {
    if (DEBUG)
        Log.i(TAG, "prepare: ");
    mTrackIndex = -1;
    mMuxerStarted = mIsEOS = false;
    final MediaCodecInfo videoCodecInfo = selectVideoCodec(MIME_TYPE);
    if (videoCodecInfo == null) {
        Log.e(TAG, "Unable to find an appropriate codec for " + MIME_TYPE);
        return;
    }
    if (DEBUG)
        Log.i(TAG, "selected codec: " + videoCodecInfo.getName());
    final MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, mColorFormat);
    format.setInteger(MediaFormat.KEY_BIT_RATE, calcBitRate());
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
    if (DEBUG)
        Log.i(TAG, "format: " + format);
    mMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mMediaCodec.start();
    Bundle params = new Bundle();
    params.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mMediaCodec.setParameters(params);
    }
    if (DEBUG)
        Log.i(TAG, "prepare finishing");
    if (mListener != null) {
        try {
            mListener.onPrepared(this);
        } catch (final Exception e) {
            Log.e(TAG, "prepare:", e);
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) Bundle(android.os.Bundle) MediaCodecInfo(android.media.MediaCodecInfo) IOException(java.io.IOException)

Example 88 with MediaCodecInfo

use of android.media.MediaCodecInfo in project AndroidUSBCamera by jiangdongguo.

the class AACEncodeConsumer method selectSupportCodec.

/**
 * 遍历所有编解码器,返回第一个与指定MIME类型匹配的编码器
 *  判断是否有支持指定mime类型的编码器
 */
private MediaCodecInfo selectSupportCodec(String mimeType) {
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        // 判断是否为编码器,否则直接进入下一次循环
        if (!codecInfo.isEncoder()) {
            continue;
        }
        // 如果是编码器,判断是否支持Mime类型
        String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++) {
            if (types[j].equalsIgnoreCase(mimeType)) {
                return codecInfo;
            }
        }
    }
    return null;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo)

Example 89 with MediaCodecInfo

use of android.media.MediaCodecInfo in project AndroidUSBCamera by jiangdongguo.

the class AACEncodeConsumer method initMediaCodec.

private void initMediaCodec() {
    if (DEBUG)
        Log.d(TAG, "AACEncodeConsumer-->开始编码音频");
    MediaCodecInfo mCodecInfo = selectSupportCodec(MIME_TYPE);
    if (mCodecInfo == null) {
        Log.e(TAG, "编码器不支持" + MIME_TYPE + "类型");
        return;
    }
    try {
        mAudioEncoder = MediaCodec.createByCodecName(mCodecInfo.getName());
    } catch (IOException e) {
        Log.e(TAG, "创建编码器失败" + e.getMessage());
        e.printStackTrace();
    }
    MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, BUFFER_SIZE);
    mAudioEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mAudioEncoder.start();
    isEncoderStart = true;
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodecInfo(android.media.MediaCodecInfo) IOException(java.io.IOException)

Example 90 with MediaCodecInfo

use of android.media.MediaCodecInfo in project AndroidUSBCamera by jiangdongguo.

the class H264EncodeConsumer method selectVideoCodec.

/**
 * select the first codec that match a specific MIME type
 *
 * @param mimeType
 * @return null if no codec matched
 */
@SuppressWarnings("deprecation")
protected final MediaCodecInfo selectVideoCodec(final String mimeType) {
    // get the list of available codecs
    final int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        if (!codecInfo.isEncoder()) {
            // skipp decoder
            continue;
        }
        // select first codec that match a specific MIME type and color format
        final String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++) {
            if (types[j].equalsIgnoreCase(mimeType)) {
                final int format = selectColorFormat(codecInfo, mimeType);
                if (format > 0) {
                    mColorFormat = format;
                    return codecInfo;
                }
            }
        }
    }
    return null;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) SuppressLint(android.annotation.SuppressLint)

Aggregations

MediaCodecInfo (android.media.MediaCodecInfo)108 ArrayList (java.util.ArrayList)33 MediaCodecList (android.media.MediaCodecList)22 SuppressLint (android.annotation.SuppressLint)20 MediaFormat (android.media.MediaFormat)17 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)15 IOException (java.io.IOException)14 TargetApi (android.annotation.TargetApi)10 Point (android.graphics.Point)9 VideoCapabilities (android.media.MediaCodecInfo.VideoCapabilities)6 Size (android.util.Size)6 Nullable (androidx.annotation.Nullable)6 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 Config (org.robolectric.annotation.Config)5 SparseIntArray (android.util.SparseIntArray)4 NonNull (androidx.annotation.NonNull)4 TreeMap (java.util.TreeMap)4 MediaCodec (android.media.MediaCodec)3 Bundle (android.os.Bundle)3