Search in sources :

Example 41 with MediaFormat

use of android.media.MediaFormat in project libstreaming by fyhertz.

the class EncoderDebugger method configureEncoder.

/**
 * Instantiates and starts the encoder.
 * @throws IOException The encoder cannot be configured
 */
private void configureEncoder() throws IOException {
    mEncoder = MediaCodec.createByCodecName(mEncoderName);
    MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BITRATE);
    mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMERATE);
    mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, mEncoderColorFormat);
    mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
    mEncoder.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mEncoder.start();
}
Also used : MediaFormat(android.media.MediaFormat)

Example 42 with MediaFormat

use of android.media.MediaFormat in project speechutils by Kaljurand.

the class MediaFormatFactory method createMediaFormat.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static MediaFormat createMediaFormat(String mime, int sampleRate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        MediaFormat format = new MediaFormat();
        // TODO: this causes a crash in MediaCodec.configure
        // format.setString(MediaFormat.KEY_FRAME_RATE, null);
        format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
        format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
        format.setString(MediaFormat.KEY_MIME, mime);
        if ("audio/mp4a-latm".equals(mime)) {
            // TODO: or 39?
            format.setInteger(MediaFormat.KEY_AAC_PROFILE, 2);
            format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
        } else if ("audio/flac".equals(mime)) {
            // format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_FLAC); // API=21
            format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
        // TODO: use another bit rate, does not seem to have effect always
        // format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
        // TODO: experiment with 0 (fastest/least) to 8 (slowest/most)
        // format.setInteger(MediaFormat.KEY_FLAC_COMPRESSION_LEVEL, 0);
        } else if ("audio/opus".equals(mime)) {
            // format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_OPUS); // API=21
            format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
        } else {
            // TODO: assuming: "audio/amr-wb"
            format.setInteger(MediaFormat.KEY_BIT_RATE, 23050);
        }
        return format;
    }
    return null;
}
Also used : MediaFormat(android.media.MediaFormat) TargetApi(android.annotation.TargetApi)

Example 43 with MediaFormat

use of android.media.MediaFormat in project speechutils by Kaljurand.

the class EncodedAudioRecorder method recorderLoop.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@RequiresPermission(RECORD_AUDIO)
@Override
protected void recorderLoop(AudioRecord speechRecord) {
    mNumBytesSubmitted = 0;
    mNumBytesDequeued = 0;
    MediaFormat format = MediaFormatFactory.createMediaFormat(MIME, getSampleRate());
    MediaCodec codec = getCodec(format);
    if (codec == null) {
        handleError("no codec found");
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Log.i("Using codec: " + codec.getCanonicalName());
        }
        int status = recorderEncoderLoop(codec, speechRecord);
        if (Log.DEBUG) {
            AudioUtils.showMetrics(format, mNumBytesSubmitted, mNumBytesDequeued);
        }
        if (status < 0) {
            handleError("encoder error");
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) RequiresPermission(androidx.annotation.RequiresPermission) TargetApi(android.annotation.TargetApi)

Example 44 with MediaFormat

use of android.media.MediaFormat in project robolectric by robolectric.

the class MediaCodecInfoBuilderTest method createMediaFormat.

/**
 * Create a sample {@link MediaFormat}.
 *
 * @param mime one of MIMETYPE_* from {@link MediaFormat}.
 * @param features an array of CodecCapabilities.FEATURE_ features to be enabled.
 */
private static MediaFormat createMediaFormat(String mime, String[] features) {
    MediaFormat mediaFormat = new MediaFormat();
    mediaFormat.setString(MediaFormat.KEY_MIME, mime);
    for (String feature : features) {
        mediaFormat.setFeatureEnabled(feature, true);
    }
    return mediaFormat;
}
Also used : MediaFormat(android.media.MediaFormat)

Example 45 with MediaFormat

use of android.media.MediaFormat in project robolectric by robolectric.

the class ShadowMediaCodecTest method getBasicAacFormat.

private static MediaFormat getBasicAacFormat() {
    MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 96000);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 16000);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, CodecProfileLevel.AACObjectLC);
    return format;
}
Also used : MediaFormat(android.media.MediaFormat)

Aggregations

MediaFormat (android.media.MediaFormat)130 IOException (java.io.IOException)29 MediaCodec (android.media.MediaCodec)24 ByteBuffer (java.nio.ByteBuffer)22 MediaExtractor (android.media.MediaExtractor)18 SuppressLint (android.annotation.SuppressLint)16 Handler (android.os.Handler)15 Test (org.junit.Test)14 TargetApi (android.annotation.TargetApi)13 InputStream (java.io.InputStream)12 Context (android.content.Context)10 HandlerThread (android.os.HandlerThread)10 Message (android.os.Message)10 Pair (android.util.Pair)10 Format (com.google.android.exoplayer2.Format)10 Runnable (java.lang.Runnable)10 Nullable (androidx.annotation.Nullable)9 File (java.io.File)9 BufferInfo (android.media.MediaCodec.BufferInfo)8 Surface (android.view.Surface)8