简体   繁体   English

Muxing相机使用MediaMuxer预览h264编码的基本流

[英]Muxing camera preview h264 encoded elementary stream with MediaMuxer

I am working on an implementation of one of the Android Test Cases regarding previewTexture recording with the new MediaCodec and MediaMuxer API's of Android 4.3. 我正在使用Android 4.3的新MediaCodec和MediaMuxer API开发一个关于previewTexture录制的Android测试用例的实现。

I've managed to record the preview stream with a framerate of about 30fps by setting the recordingHint to the camera paremeters. 通过将recordingHint设置为摄像机参数,我设法以大约30fps的帧速率记录预览流。

However, I ran into a delay/lag problem and don't really know how to fix that. 但是,我遇到了延迟/滞后问题,并不知道如何解决这个问题。 When recording the camera preview with quite standard quality settings (1280x720, bitrate of ~8.000.000) the preview and the encoded material suffers from occasional lags. 当使用相当标准的质量设置(1280x720,比特率为~8.000.000)录制相机预览时,预览和编码材料会偶尔出现滞后。 To be more specific: This lag occurs about every 2-3 seconds and takes about 300-600ms. 更具体地说:这种滞后大约每2-3秒发生一次,大约需要300-600毫秒。

By tracing the delay I was able to figure out the delay comes from the following line of code in the "drainEncoder" method: 通过跟踪延迟,我能够找出延迟来自“drainEncoder”方法中的以下代码行:

mMuxer.writeSampleData(mTrackIndex, encodedData, mBufferInfo);

This line is called in a loop if the encoder has data available for muxing. 如果编码器具有可用于多路复用的数据,则在循环中调用该行。 Currently I don't record audio so only the h264 streams is converted to a mp4 format by the MediaMuxer. 目前我不录制音频,因此MediaMuxer只将h264流转换为mp4格式。

I don't know if this has something to do with that delay, but it always occurs when the loop needs two iterations to dequeue all available data of the encoder (to be even more specific it occurs always in the first of these two iterations). 我不知道这是否与延迟有关,但它总是发生在循环需要两次迭代以使编码器的所有可用数据出列时(更具体地说,它始终在这两次迭代的第一次出现) 。 In most cases one iteration is enough to dequeue the encoder. 在大多数情况下,一次迭代足以使编码器出列。

Since there is not much information online about these new API's any help is very appreciated! 由于网上关于这些新API的信息不多,所以非常感谢任何帮助!

I suspect you're getting bitten by the MediaMuxer disk write. 我怀疑你被MediaMuxer磁盘写入了。 The best way to be sure is to run systrace during recording and see what's actually happening during the pause. 最好的方法是在录制过程中运行systrace,看看暂停期间实际发生了什么。 (systrace docs , explanation , bigflake example -- as of right now only the latter is updated for Android 4.3) (systrace docs解释bigflake示例 - 截至目前,只有后者更新为Android 4.3)

If that's the case, you may be able to mitigate the problem by running the MediaMuxer instance on a separate thread, feeding the H.264 data to it through a synchronized queue. 如果是这种情况,您可以通过在单独的线程上运行MediaMuxer实例来缓解问题,通过同步队列将H.264数据提供给它。

Do these pauses happen regularly, every 5 seconds? 这些停顿是否每5秒钟定期发生一次? The CameraToMpegTest example configures the encoder to output an I-frame every 5 seconds (with an expected frame rate of 30fps), which results in a full-sized frame being output rather than tiny deltas. CameraToMpegTest示例将编码器配置为每5秒输出一个I帧(预期帧速率为30fps),这将导致输出一个全尺寸的帧而不是微小的增量。

As @fadden points out, this is a disk write issue that occurs mostly on devices with lower writing flash speeds or if you try to write to the SD card. 正如@fadden所指出的,这是一个磁盘写入问题,主要发生在写入闪存速度较低的设备上,或者如果您尝试写入SD卡。

I have written a solution on how to buffer MediaMuxer's write in a similar question here . 我已经写了关于如何缓冲MediaMuxer的写在一个类似的问题的解决方案在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM