MediaMuxer error "Failed to stop the muxer"
Asked Answered
S

3

19

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.

Stupefaction answered 21/10, 2013 at 22:21 Comment(0)
B
21
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.

Bantam answered 22/10, 2013 at 0:15 Comment(2)
I have also encountered this issue and would like to add some clarification: In order to create a MediaFormat that includes CSD (Codec Specific Data), you must obtain said MediaFormat from a completely configured codec instance via encoder.getOutputFormat.Urea
Rakatan's comment is very useful. Don't try to build a MediaFormat by yourself.Curet
C
10

I had the same issue .While closing the Muxer it was throwing "Failed to stop" error.When I checked my saved file in an ISO viewer I couldn't find the Track in it . I solved issue by creating the track only after getting the first output from the video encoder.Here is how I add my track

 m_VideoTrackIndex = muxer.addTrack(mediaCodec.getOutputFormat());

The media format for the track is obtained from mediaCodec.getOutputFormat() which in turn will get initialized only after encoding the first frame.I changed my code to add the track after getting first encoded data (And of course only once).It is working fine .

Cly answered 24/4, 2015 at 23:21 Comment(2)
if ( encoderStatus =MediaCodec.INFO_OUTPUT_FORMAT_CHANGED ) { MediaFormat newFormat = mEncoder.getOutputFormat(); mTrackIndex = mMuxer.addTrack(newFormat); mMuxer.start();Laurustinus
I check this one and added in code. It's working on some phone. But getting an error in Samsung s6 device.Rivero
L
3

There are 2 Problems with Android 5.0.2 Devices Moto E 1) The Width & height if not supplied multiples of 16 its crashing 2) The mediaBuffer to be set after the First Frame is Encoded

Laurustinus answered 8/2, 2016 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.