【问题标题】:getInpuBuffer in Android MediaCodec and MediaMuxerAndroid MediaCodec 和 MediaMuxer 中的 getInpuBuffer
【发布时间】:2017-02-08 10:54:17
【问题描述】:

以下是 Android MediaMuxer API 示例代码片段: https://developer.android.com/reference/android/media/MediaMuxer.html

MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
 // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
 // or MediaExtractor.getTrackFormat().
 MediaFormat audioFormat = new MediaFormat(...);
 MediaFormat videoFormat = new MediaFormat(...);
 int audioTrackIndex = muxer.addTrack(audioFormat);
 int videoTrackIndex = muxer.addTrack(videoFormat);
 ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
 boolean finished = false;
 BufferInfo bufferInfo = new BufferInfo();

 muxer.start();
 while(!finished) {
   // getInputBuffer() will fill the inputBuffer with one frame of encoded
   // sample from either MediaCodec or MediaExtractor, set isAudioSample to
   // true when the sample is audio data, set up all the fields of bufferInfo,
   // and return true if there are no more samples.
   finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
   if (!finished) {
     int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
     muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
   }
 };
 muxer.stop();
 muxer.release();

对于这一行:finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);我在MediaCodec.java和MediaMuxer.java中都没有找到getInputBuffer这个函数,是用户定义的函数还是API函数?

【问题讨论】:

    标签: android android-mediacodec mediamuxer


    【解决方案1】:

    在这种情况下,getInputBuffer 是一个假设的用户定义函数。它不是 API 函数。它上面的评论解释了它应该做什么。 (注意它实际上不会按照它的编写方式工作,因为 isAudioSample 变量也不能由函数以它的确切编写方式更新。)

    【讨论】:

    • 假设我想使用 MediaCodec 和 MediaMuxer API 录制视频(从表面编码)和音频,我应该明确设置 bufferInfo(视频和音频)的时间戳还是只使用默认值?也许某些平台对表面和音频使用不同的时间戳,例如启动时间和单调时间等。
    • 我希望来自 mediacodec 的时间戳应该可以正常工作,假设您获得了正确的编码器输入时间戳。
    • @mewo1234 您应该在使用 Surface 输入到 MediaCodec 时手动设置时间戳,因为您无法将时间戳与来自 Surface 的 MediaCodec 输入上的帧相关联。根据我的经验,不这样做可能会导致一些跳帧。这意味着您需要为编码器的输出缓冲区设置缓冲区信息presentationTimeUs。您不必担心音频的时间戳; MediaCodec 唯一关心的输入音频时间戳是它们严格增加。 MediaCodec 应该使用其编码的音频样本输出正确的时间戳。
    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 2013-10-05
    • 1970-01-01
    • 2021-04-26
    • 2015-08-22
    • 2021-04-15
    • 2014-07-21
    • 2016-04-25
    相关资源
    最近更新 更多