Search code examples
androidmp4android-mediacodecmuxer

MediaMuxer error "Failed to stop the muxer"


I'm encoding Camera preview data using MediaCodec with mime-type "video/avc" and passing the encoded data (video-only, no audio) to MediaMuxer. The muxer seems to run fine and creates a reasonably sized output file (i.e., gets larger the longer I record). However, when I try to stop the muxer I get the "Failed to stop the muxer" error:

10-21 10:39:40.755: E/AndroidRuntime(2166): Caused by: java.lang.IllegalStateException: Failed to stop the muxer

There are some suspicious MPEG4Writer log messages preceding the failed stop:

10-21 10:39:40.740: D/MPEG4Writer(2166): Stopping Video track
10-21 10:39:40.740: E/MPEG4Writer(2166): Missing codec specific data
10-21 10:39:40.740: W/MPEG4Writer(2166): 0-duration samples found: 122
10-21 10:39:40.740: I/MPEG4Writer(2166): Received total/0-length (123/1) buffers and encoded 123 frames. - video
10-21 10:39:40.740: D/MPEG4Writer(2166): Stopping Video track source
10-21 10:39:40.740: D/MPEG4Writer(2166): Video track stopped
10-21 10:39:40.740: D/MPEG4Writer(2166): Stopping writer thread
10-21 10:39:40.740: D/MPEG4Writer(2166): 0 chunks are written in the last batch
10-21 10:39:40.740: D/MPEG4Writer(2166): Writer thread stopped
10-21 10:39:40.740: E/MPEG4Writer(2166): writer error ended!

Any clues what is causing this? Not sure what more info you'll need.


Solution

  • E/MPEG4Writer(2166): Missing codec specific data
    

    Sounds like you didn't call MediaMuxer#addTrack() with a MediaFormat that included the CSD. See the EncodeAndMuxTest.java code for an example of how to do this.

    Looking at the MPEG4Writer implementation used by MediaMuxer, there's an isTrackMalformed() check on line 2360; it sets ERROR_MALFORMED if the CSD data isn't present, but doesn't return immediately. Nothing clears the error, so it'll do a bunch of work and then fail, which seems to match what you're seeing.