Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I've managed to record the preview stream with a framerate of about 30fps by setting the recordingHint to the camera paremeters.

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. To be more specific: This lag occurs about every 2-3 seconds and takes about 300-600ms.

By tracing the delay I was able to figure out the delay comes from the following line of code in the "drainEncoder" method:

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.

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!

like image 945
mAx Avatar asked Oct 14 '13 13:10

mAx


1 Answers

I suspect you're getting bitten by the MediaMuxer disk write. The best way to be sure is to run systrace during recording and see what's actually happening during the pause. (systrace docs, explanation, bigflake example -- as of right now only the latter is updated for 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.

Do these pauses happen regularly, every 5 seconds? 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.

like image 171
fadden Avatar answered Nov 09 '22 19:11

fadden