Search in sources :

Example 1 with MediaCodec

use of android.media.MediaCodec in project Signal-Android by WhisperSystems.

the class AudioCodec method createMediaCodec.

private MediaCodec createMediaCodec(int bufferSize) throws IOException {
    MediaCodec mediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
    MediaFormat mediaFormat = new MediaFormat();
    mediaFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, CHANNELS);
    mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, bufferSize);
    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    try {
        mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    } catch (Exception e) {
        Log.w(TAG, e);
        mediaCodec.release();
        throw new IOException(e);
    }
    return mediaCodec;
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with MediaCodec

use of android.media.MediaCodec in project grafika by google.

the class SoftInputSurfaceActivity method prepareEncoder.

/**
     * Prepares the video encoder, muxer, and an input surface.
     */
private void prepareEncoder(File outputFile) throws IOException {
    mBufferInfo = new MediaCodec.BufferInfo();
    MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
    // Set some properties.  Failing to specify some of these can cause the MediaCodec
    // configure() call to throw an unhelpful exception.
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMES_PER_SECOND);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
    if (VERBOSE)
        Log.d(TAG, "format: " + format);
    // Create a MediaCodec encoder, and configure it with our format.  Get a Surface
    // we can use for input and wrap it with a class that handles the EGL work.
    mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
    mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mInputSurface = mEncoder.createInputSurface();
    mEncoder.start();
    // the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
    if (VERBOSE)
        Log.d(TAG, "output will go to " + outputFile);
    mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    mTrackIndex = -1;
    mMuxerStarted = false;
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) MediaMuxer(android.media.MediaMuxer)

Example 3 with MediaCodec

use of android.media.MediaCodec in project grafika by google.

the class GeneratedMovie method prepareEncoder.

/**
     * Prepares the video encoder, muxer, and an EGL input surface.
     */
protected void prepareEncoder(String mimeType, int width, int height, int bitRate, int framesPerSecond, File outputFile) throws IOException {
    mBufferInfo = new MediaCodec.BufferInfo();
    MediaFormat format = MediaFormat.createVideoFormat(mimeType, width, height);
    // Set some properties.  Failing to specify some of these can cause the MediaCodec
    // configure() call to throw an unhelpful exception.
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, framesPerSecond);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
    if (VERBOSE)
        Log.d(TAG, "format: " + format);
    // Create a MediaCodec encoder, and configure it with our format.  Get a Surface
    // we can use for input and wrap it with a class that handles the EGL work.
    mEncoder = MediaCodec.createEncoderByType(mimeType);
    mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    Log.v(TAG, "encoder is " + mEncoder.getCodecInfo().getName());
    Surface surface;
    try {
        surface = mEncoder.createInputSurface();
    } catch (IllegalStateException ise) {
        // TODO: failure message should come out of strings.xml for i18n
        if (isSoftwareCodec(mEncoder)) {
            throw new RuntimeException("Can't use input surface with software codec: " + mEncoder.getCodecInfo().getName(), ise);
        } else {
            throw new RuntimeException("Failed to create input surface", ise);
        }
    }
    mEglCore = new EglCore(null, EglCore.FLAG_RECORDABLE);
    mInputSurface = new WindowSurface(mEglCore, surface, true);
    mInputSurface.makeCurrent();
    mEncoder.start();
    // the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
    if (VERBOSE)
        Log.d(TAG, "output will go to " + outputFile);
    mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    mTrackIndex = -1;
    mMuxerStarted = false;
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) EglCore(com.android.grafika.gles.EglCore) MediaMuxer(android.media.MediaMuxer) WindowSurface(com.android.grafika.gles.WindowSurface) WindowSurface(com.android.grafika.gles.WindowSurface) Surface(android.view.Surface)

Example 4 with MediaCodec

use of android.media.MediaCodec in project grafika by google.

the class CodecOpenActivity method clickStart.

/**
     * onClick handler for "start" button.
     *
     * We create as many codecs as we can and return without releasing them.  The codecs
     * will remain in use until the next GC.
     */
public void clickStart(@SuppressWarnings("unused") View unused) {
    final String MIME_TYPE = "video/avc";
    final int WIDTH = 320;
    final int HEIGHT = 240;
    final int BIT_RATE = 1000000;
    final int FRAME_RATE = 15;
    final int IFRAME_INTERVAL = 1;
    final boolean START_CODEC = true;
    MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
    Log.d(TAG, "format: " + format);
    MediaCodec[] codecs = new MediaCodec[MAX_OPEN];
    int i;
    for (i = 0; i < MAX_OPEN; i++) {
        try {
            codecs[i] = MediaCodec.createEncoderByType(MIME_TYPE);
            codecs[i].configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
            if (START_CODEC) {
                codecs[i].createInputSurface();
                codecs[i].start();
            }
        } catch (Exception ex) {
            Log.i(TAG, "Failed on creation of codec #" + i, ex);
            break;
        }
    }
    showCountDialog(i);
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec)

Example 5 with MediaCodec

use of android.media.MediaCodec in project android_frameworks_base by DirtyUnicorns.

the class CpuVideoTrackDecoder method initMediaCodec.

@Override
protected MediaCodec initMediaCodec(MediaFormat format) {
    // Find a codec for our video that can output to one of our supported color-spaces
    MediaCodec mediaCodec = findDecoderCodec(format, new int[] { CodecCapabilities.COLOR_Format32bitARGB8888, CodecCapabilities.COLOR_FormatYUV420Planar });
    if (mediaCodec == null) {
        throw new RuntimeException("Could not find a suitable decoder for format: " + format + "!");
    }
    mediaCodec.configure(format, null, null, 0);
    return mediaCodec;
}
Also used : MediaCodec(android.media.MediaCodec)

Aggregations

MediaCodec (android.media.MediaCodec)21 IOException (java.io.IOException)10 MediaFormat (android.media.MediaFormat)7 Surface (android.view.Surface)5 TargetApi (android.annotation.TargetApi)2 MediaExtractor (android.media.MediaExtractor)2 MediaMuxer (android.media.MediaMuxer)2 SuppressLint (android.annotation.SuppressLint)1 Point (android.graphics.Point)1 BufferInfo (android.media.MediaCodec.BufferInfo)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 EglCore (com.android.grafika.gles.EglCore)1 WindowSurface (com.android.grafika.gles.WindowSurface)1 FileNotFoundException (java.io.FileNotFoundException)1 ByteBuffer (java.nio.ByteBuffer)1 CalledByNative (org.chromium.base.CalledByNative)1