Search in sources :

Example 1 with MediaCodecBufferCompatWrapper

use of net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper in project Pix-Art-Messenger by kriztan.

the class AudioTrackTranscoder method setup.

@Override
public void setup() {
    mExtractor.selectTrack(mTrackIndex);
    try {
        mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mEncoder.start();
    mEncoderStarted = true;
    mEncoderBuffers = new MediaCodecBufferCompatWrapper(mEncoder);
    final MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
    try {
        mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    mDecoder.configure(inputFormat, null, null, 0);
    mDecoder.start();
    mDecoderStarted = true;
    mDecoderBuffers = new MediaCodecBufferCompatWrapper(mDecoder);
    mAudioChannel = new AudioChannel(mDecoder, mEncoder, mOutputFormat);
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodecBufferCompatWrapper(net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper) IOException(java.io.IOException)

Example 2 with MediaCodecBufferCompatWrapper

use of net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper in project Pix-Art-Messenger by kriztan.

the class AudioTrackTranscoder method drainEncoder.

private int drainEncoder(long timeoutUs) {
    if (mIsEncoderEOS)
        return DRAIN_STATE_NONE;
    int result = mEncoder.dequeueOutputBuffer(mBufferInfo, timeoutUs);
    switch(result) {
        case MediaCodec.INFO_TRY_AGAIN_LATER:
            return DRAIN_STATE_NONE;
        case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
            if (mActualOutputFormat != null) {
                throw new RuntimeException("Audio output format changed twice.");
            }
            mActualOutputFormat = mEncoder.getOutputFormat();
            mMuxer.setOutputFormat(SAMPLE_TYPE, mActualOutputFormat);
            return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
        case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
            mEncoderBuffers = new MediaCodecBufferCompatWrapper(mEncoder);
            return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
    }
    if (mActualOutputFormat == null) {
        throw new RuntimeException("Could not determine actual output format.");
    }
    if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
        mIsEncoderEOS = true;
        mBufferInfo.set(0, 0, 0, mBufferInfo.flags);
    }
    if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
        // SPS or PPS, which should be passed by MediaFormat.
        mEncoder.releaseOutputBuffer(result, false);
        return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
    }
    mMuxer.writeSampleData(SAMPLE_TYPE, mEncoderBuffers.getOutputBuffer(result), mBufferInfo);
    mWrittenPresentationTimeUs = mBufferInfo.presentationTimeUs;
    mEncoder.releaseOutputBuffer(result, false);
    return DRAIN_STATE_CONSUMED;
}
Also used : MediaCodecBufferCompatWrapper(net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper)

Aggregations

MediaCodecBufferCompatWrapper (net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper)2 MediaFormat (android.media.MediaFormat)1 IOException (java.io.IOException)1