Java Code Examples for android.media.MediaCodec#getOutputBuffer()

The following examples show how to use android.media.MediaCodec#getOutputBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConvertAudioStreamProvider.java    From vinyl-cast with MIT License 6 votes vote down vote up
/**
 * Handle reading encoded audio from MediaCodec OutputBuffer
 * @param codec
 * @param outputBufferId
 * @param info
 * @return number bytes read
 * @throws IOException
 */
private int dequeueCodecOutputBuffer(MediaCodec codec, int outputBufferId, MediaCodec.BufferInfo info) throws IOException {
    int outBitsSize = info.size;
    int outPacketSize = outBitsSize + 7;    // 7 is ADTS header size
    ByteBuffer outBuf = codec.getOutputBuffer(outputBufferId);

    outBuf.position(info.offset);
    outBuf.limit(info.offset + outBitsSize);

    byte[] packet = new byte[outPacketSize];
    addADTStoPacket(packet, outPacketSize);
    outBuf.get(packet, 7, outBitsSize);

    convertedAudioWriteStream.write(packet, 0, outPacketSize);
    convertedAudioWriteStream.flush();

    outBuf.clear();
    codec.releaseOutputBuffer(outputBufferId, false);

    return outBitsSize;
}
 
Example 2
Source File: Mp4Processor.java    From AAVT with Apache License 2.0 5 votes vote down vote up
private ByteBuffer getOutputBuffer(MediaCodec codec, int index){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return codec.getOutputBuffer(index);
    }else{
        return codec.getOutputBuffers()[index];
    }
}
 
Example 3
Source File: CameraRecorder.java    From AAVT with Apache License 2.0 5 votes vote down vote up
private ByteBuffer getOutputBuffer(MediaCodec codec, int index){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return codec.getOutputBuffer(index);
    }else{
        return codec.getOutputBuffers()[index];
    }
}
 
Example 4
Source File: CodecUtil.java    From AAVT with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer getOutputBuffer(MediaCodec codec, int index){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return codec.getOutputBuffer(index);
    }else{
        return codec.getOutputBuffers()[index];
    }
}
 
Example 5
Source File: BaseEncoder.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@Override
public void outputAvailable(@NonNull MediaCodec mediaCodec, int outBufferIndex,
    @NonNull MediaCodec.BufferInfo bufferInfo) throws IllegalStateException {
  ByteBuffer byteBuffer;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    byteBuffer = mediaCodec.getOutputBuffer(outBufferIndex);
  } else {
    byteBuffer = mediaCodec.getOutputBuffers()[outBufferIndex];
  }
  processOutput(byteBuffer, mediaCodec, outBufferIndex, bufferInfo);
}
 
Example 6
Source File: CodecUtil.java    From EZFilter with MIT License 5 votes vote down vote up
/**
 * 防止出现 http://stackoverflow.com/q/30646885 的问题
 *
 * @param codec
 * @param index
 * @return
 */
public static ByteBuffer getOutputBuffer(MediaCodec codec, int index) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return codec.getOutputBuffer(index);
    } else {
        return codec.getOutputBuffers()[index];
    }
}
 
Example 7
Source File: VideoCapture.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private ByteBuffer getOutputBuffer(MediaCodec codec, int index) {
    return codec.getOutputBuffer(index);
}
 
Example 8
Source File: MediaPlayerGrabber.java    From VIA-AI with MIT License 4 votes vote down vote up
private void frameDisplay(MediaCodec decoder) {
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

        int srcColorFormat = 0;
        int srcStride = 0;
        int outputBufferId = decoder.dequeueOutputBuffer(info, DEFAULT_TIMEOUT_US);
        if (outputBufferId >= 0) {
            if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
                sawOutputEOS = false;
            }

            boolean doRender = (info.size != 0);
            if (doRender) {
                outputFrameCount++;

                ByteBuffer b = null;
                Image image = null;

                if (Helper.isUpperThanAPI21()) {
                    image = decoder.getOutputImage(outputBufferId);
                    if(image!=null) {
//                        Log.d(TAG, "image:" + image.getWidth() + "," + image.getHeight() + "," + image.getFormat());
                    } else {
                        b = decoder.getOutputBuffer(outputBufferId);
                        MediaFormat format = decoder.getOutputFormat(outputBufferId);
                        srcColorFormat = format.getInteger(KEY_COLOR_FORMAT);
                        srcStride = format.getInteger(KEY_STRIDE);
                    }
                } else {
                    b = outputByteBuffers[outputBufferId];
                }

                if(time==-1) time = System.currentTimeMillis();
                else {
                    long diff = (System.currentTimeMillis()-time);
                    if(diff<33) {
                        //waitMs((33*1000-diff)/1000);
                        waitMs((33-diff));
                    }
                    time = System.currentTimeMillis();
                }

                if(callback!=null) {
                    if(image!=null) callback.onImageReady(image);
                    else if (b!=null) callback.onFrameReady(b,info.offset, width, height, srcColorFormat , srcStride);
                }

                decoder.releaseOutputBuffer(outputBufferId, false);
            }
        }
    }
 
Example 9
Source File: AudioRecordThread.java    From PhotoMovie with Apache License 2.0 4 votes vote down vote up
/**
 * 需要改变音频速率的情况下,需要先解码->改变速率->编码
 */
private void decodeToPCM(MediaCodec decoder, MediaExtractor extractor, MediaFormat oriAudioFormat, String outPath, Long endTimeUs) throws IOException {
    int maxBufferSize = getAudioMaxBufferSize(oriAudioFormat);
    ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

    //调整音频速率需要重解码音频帧
    decoder.configure(oriAudioFormat, null, null, 0);
    decoder.start();

    boolean decodeDone = false;
    boolean decodeInputDone = false;
    final int TIMEOUT_US = 2500;
    File pcmFile = new File(outPath);
    FileChannel writeChannel = new FileOutputStream(pcmFile).getChannel();
    ByteBuffer[] inputBuffers = null;
    ByteBuffer[] outputBuffers = null;

    try {
        while (!decodeDone) {
            if (!decodeInputDone) {
                boolean eof = false;
                int decodeInputIndex = decoder.dequeueInputBuffer(TIMEOUT_US);
                if (Build.VERSION.SDK_INT < 21 && decodeInputIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                    outputBuffers = decoder.getOutputBuffers();
                    inputBuffers = decoder.getInputBuffers();
                } else if (decodeInputIndex >= 0) {
                    long sampleTimeUs = extractor.getSampleTime();
                    if (sampleTimeUs == -1) {
                        eof = true;
                    } else if (endTimeUs != null && sampleTimeUs > endTimeUs) {
                        eof = true;
                    }

                    if (eof) {
                        decodeInputDone = true;
                        decoder.queueInputBuffer(decodeInputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    } else {
                        info.size = extractor.readSampleData(buffer, 0);
                        info.presentationTimeUs = sampleTimeUs;
                        info.flags = extractor.getSampleFlags();
                        ByteBuffer inputBuffer = null;
                        if (android.os.Build.VERSION.SDK_INT >= 21) {
                            inputBuffer = decoder.getInputBuffer(decodeInputIndex);
                        } else {
                            inputBuffer = inputBuffers[decodeInputIndex];
                        }
                        inputBuffer.put(buffer);
                        MLog.i(TAG, "audio decode queueInputBuffer " + info.presentationTimeUs / 1000);
                        decoder.queueInputBuffer(decodeInputIndex, 0, info.size, info.presentationTimeUs, info.flags);
                        extractor.advance();
                    }

                }
            }

            while (!decodeDone) {
                int outputBufferIndex = decoder.dequeueOutputBuffer(info, TIMEOUT_US);
                if (outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
                    break;
                } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                    MediaFormat newFormat = decoder.getOutputFormat();
                    MLog.i(TAG, "audio decode newFormat = " + newFormat);
                } else if (outputBufferIndex < 0) {
                    //ignore
                    MLog.e(TAG, "unexpected result from audio decoder.dequeueOutputBuffer: " + outputBufferIndex);
                } else {
                    if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
                        decodeDone = true;
                    } else {
                        ByteBuffer decodeOutputBuffer = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                            decodeOutputBuffer = decoder.getOutputBuffer(outputBufferIndex);
                        } else {
                            decodeOutputBuffer = outputBuffers[outputBufferIndex];
                        }
                        MLog.i(TAG, "audio decode saveFrame " + info.presentationTimeUs / 1000);
                        writeChannel.write(decodeOutputBuffer);
                    }
                    decoder.releaseOutputBuffer(outputBufferIndex, false);
                }
            }
        }
    } finally {
        writeChannel.close();
        extractor.release();
        decoder.stop();
        decoder.release();
    }
}
 
Example 10
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 4 votes vote down vote up
/**
 * 需要改变音频速率的情况下,需要先解码->改变速率->编码
 */
public static void decodeToPCM(VideoProcessor.MediaSource audioSource, String outPath, Integer startTimeUs, Integer endTimeUs) throws IOException {
    MediaExtractor extractor = new MediaExtractor();
    audioSource.setDataSource(extractor);
    int audioTrack = VideoUtil.selectTrack(extractor, true);
    extractor.selectTrack(audioTrack);
    if (startTimeUs == null) {
        startTimeUs = 0;
    }
    extractor.seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
    MediaFormat oriAudioFormat = extractor.getTrackFormat(audioTrack);
    int maxBufferSize;
    if (oriAudioFormat.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
        maxBufferSize = oriAudioFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
    } else {
        maxBufferSize = 100 * 1000;
    }
    ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

    //调整音频速率需要重解码音频帧
    MediaCodec decoder = MediaCodec.createDecoderByType(oriAudioFormat.getString(MediaFormat.KEY_MIME));
    decoder.configure(oriAudioFormat, null, null, 0);
    decoder.start();

    boolean decodeDone = false;
    boolean decodeInputDone = false;
    final int TIMEOUT_US = 2500;
    File pcmFile = new File(outPath);
    FileChannel writeChannel = new FileOutputStream(pcmFile).getChannel();
    try {
        while (!decodeDone) {
            if (!decodeInputDone) {
                boolean eof = false;
                int decodeInputIndex = decoder.dequeueInputBuffer(TIMEOUT_US);
                if (decodeInputIndex >= 0) {
                    long sampleTimeUs = extractor.getSampleTime();
                    if (sampleTimeUs == -1) {
                        eof = true;
                    } else if (sampleTimeUs < startTimeUs) {
                        extractor.advance();
                        continue;
                    } else if (endTimeUs != null && sampleTimeUs > endTimeUs) {
                        eof = true;
                    }

                    if (eof) {
                        decodeInputDone = true;
                        decoder.queueInputBuffer(decodeInputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    } else {
                        info.size = extractor.readSampleData(buffer, 0);
                        info.presentationTimeUs = sampleTimeUs;
                        info.flags = extractor.getSampleFlags();
                        ByteBuffer inputBuffer = decoder.getInputBuffer(decodeInputIndex);
                        inputBuffer.put(buffer);
                        CL.it(TAG, "audio decode queueInputBuffer " + info.presentationTimeUs / 1000);
                        decoder.queueInputBuffer(decodeInputIndex, 0, info.size, info.presentationTimeUs, info.flags);
                        extractor.advance();
                    }

                }
            }

            while (!decodeDone) {
                int outputBufferIndex = decoder.dequeueOutputBuffer(info, TIMEOUT_US);
                if (outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
                    break;
                } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                    MediaFormat newFormat = decoder.getOutputFormat();
                    CL.it(TAG, "audio decode newFormat = " + newFormat);
                } else if (outputBufferIndex < 0) {
                    //ignore
                    CL.et(TAG, "unexpected result from audio decoder.dequeueOutputBuffer: " + outputBufferIndex);
                } else {
                    if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
                        decodeDone = true;
                    } else {
                        ByteBuffer decodeOutputBuffer = decoder.getOutputBuffer(outputBufferIndex);
                        CL.it(TAG, "audio decode saveFrame " + info.presentationTimeUs / 1000);
                        writeChannel.write(decodeOutputBuffer);
                    }
                    decoder.releaseOutputBuffer(outputBufferIndex, false);
                }
            }
        }
    } finally {
        writeChannel.close();
        extractor.release();
        decoder.stop();
        decoder.release();
    }
}