MediaMuxer有时会导致黑色视频

时间:2017-02-28 11:02:40

标签: android h.264 encoder mediamuxer muxer

我正在使用AndroidEncoder制作带有AAC音频的H264视频,我通过RTMP网络发送这些视频,并使用MediaMuxer将其本地存储在MP4文件中。

广播在RTMP上工作得很好,但是在本地保存时,生成的视频有时是好的,有时它只是带声音的黑色帧(或者只是带有一些信息块的第一个部分帧),有时它首先播放音频,然后是随机FPS速度的视频。

这是我的Muxer包装器类和MPEG4Writer SDK类显示的输出:

02-28 11:57:38.521 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Requested adding of track to class AndroidMuxer of type AUDIO
02-28 11:57:38.522 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Adding track for audio/mp4a-latm
02-28 11:57:38.522 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Track index to class AndroidMuxer of type AUDIO, track saved for future use!
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Requested adding of track to class AndroidMuxer of type VIDEO
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Adding track for video/avc
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: All tracks added, marking Local muxer as ready!
02-28 11:57:38.622 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Track index to class AndroidMuxer of type VIDEO, track saved for future use!
02-28 11:57:52.050 6420-6420/com.myapp.broadcast I/AndroidMuxer: init: Added audio track widh id 0
02-28 11:57:52.050 6420-6420/com.myapp.broadcast I/AndroidMuxer: init: Added video track widh id 1
02-28 11:57:52.050 6420-6420/com.myapp.broadcast W/AndroidMuxer: init: Muxer was successfully created! Starting to record
02-28 11:57:52.056 6420-6420/com.myapp.broadcast I/MPEG4Writer: limits: 4294967295/0 bytes/us, bit rate: -1 bps and the estimated moov size 3195 bytes
02-28 11:57:52.057 6420-6420/com.myapp.broadcast W/AndroidMuxer: start: Muxer started!
02-28 11:57:52.070 6420-6965/com.myapp.broadcast I/MPEG4Writer: setStartTimestampUs: 0
02-28 11:57:52.070 6420-6965/com.myapp.broadcast I/MPEG4Writer: Earliest track starting time: 0
02-28 11:57:52.083 6420-6966/com.myapp.broadcast I/MPEG4Writer: setStartTimestampUs: 0
02-28 11:58:02.617 6420-6475/com.myapp.broadcast W/AndroidMuxer: writeSampleData: All tracks finished! Calling Stop
02-28 11:58:02.617 6420-6475/com.myapp.broadcast W/AndroidMuxer: stop: Calling stop to the muxer
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track stopping
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track source stopping
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track source stopped
02-28 11:58:02.617 6420-6965/com.myapp.broadcast I/MPEG4Writer: Received total/0-length (452/0) buffers and encoded 452 frames. - Audio
02-28 11:58:02.617 6420-6965/com.myapp.broadcast I/MPEG4Writer: Audio track drift time: 0 us
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track stopped
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track stopping
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track source stopping
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track source stopped
02-28 11:58:02.618 6420-6966/com.myapp.broadcast I/MPEG4Writer: Received total/0-length (315/0) buffers and encoded 315 frames. - Video
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track stopped
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Duration from tracks range is [10143021, 10495420] us
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Stopping writer thread
02-28 11:58:02.620 6420-6964/com.myapp.broadcast D/MPEG4Writer: 0 chunks are written in the last batch
02-28 11:58:02.620 6420-6475/com.myapp.broadcast D/MPEG4Writer: Writer thread stopped
02-28 11:58:02.621 6420-6475/com.myapp.broadcast I/MPEG4Writer: The mp4 file will not be streamable.
02-28 11:58:02.621 6420-6475/com.myapp.broadcast W/AndroidMuxer: stop: muxer stopped!

虽然本地保存的视频不合适,但相同镜头的实时流(由相同的编码过程产生)很好,所以问题必须在多路复用器本身(或者更确切地说,在路上我处理它。)

这是我的Muxer Wrapper(受到bigflake mediacodec samples的启发):

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.util.Log;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @hide
 */
public class AndroidMuxer extends Muxer {
    private static final String TAG = "AndroidMuxer";
    private static final boolean VERBOSE = false;

    private MediaMuxer mMuxer;
    private boolean mStarted = false;
    private boolean mStopped = false;
    private boolean mReadyToStart;
    private MediaFormat mVideoTrackFormat;
    private MediaFormat mAudioTrackFormat;

    private long mStartTimeUs = 0;
    @Override
    public void reset() {
        Log.w(TAG, "reset: Reset is called on LOCAL MUXER!");

            mStarted = false;
            mReadyToStart = false;
            mNumTracks = 0;
            mNumTracksFinished = 0;
            Log.w(TAG, "reset: Reset worked on the Muxer");

    }

    /**
     * This will only initialize the fields, we only want the real muxer to be initialized
     * when we go live.
     * @param outputFile outputPath of the file
     * @param format Format of the muxer (will always be MPEG4 TBH)
     */
    public AndroidMuxer(String outputFile, FORMAT format) {
        super(outputFile, format);
        mStarted = false;
    }

    @Override
    public void init() {
        if(!mReadyToStart) {
            return;
        }

        if(mStarted){
            Log.i(TAG, "init: Muxer already created! Skipping!");
            return;
        }


        try {
            switch (mFormat) {
                case MPEG4:
                    mMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
                    int audioTrack = mMuxer.addTrack(mAudioTrackFormat);
                    Log.i(TAG, "init: Added audio track widh id " + audioTrack);
                    mAudioTrackIndex = audioTrack;

                    int videoTrack = mMuxer.addTrack(mVideoTrackFormat);
                    Log.i(TAG, "init: Added video track widh id " + videoTrack);
                    mVideoTrackIndex = videoTrack;
                    Log.w(TAG, "init: Muxer was successfully created! Starting to record");
                    start();
                    break;
                default:
                    throw new IllegalArgumentException("Unrecognized format!");
            }
        } catch (IOException e) {
            throw new RuntimeException("MediaMuxer creation failed", e);
        }
    }

    public static AndroidMuxer create(String outputFile, FORMAT format) {
        return new AndroidMuxer(outputFile, format);
    }

    @Override
    public int addTrack(MediaFormat trackFormat, AndroidEncoder.TrackType trackType) {
        Log.w(TAG, "addTrack: Requested adding of track to class " + getClass().getSimpleName() + " of type " + trackType);
        //Super method just keeps track of amount of tracks added
        super.addTrack(trackFormat, trackType);
        Log.w(TAG, "addTrack: Adding track for " + trackFormat.getString(MediaFormat.KEY_MIME));
        if (mStarted)
            throw new RuntimeException("format changed twice");

        //What we do is store the media info for later use once the muxer is created.
        //This is done to prevent file creation until the event goes live.

        if(trackType == AndroidEncoder.TrackType.VIDEO){
            mVideoTrackFormat = trackFormat;
        }
        else if(trackType == AndroidEncoder.TrackType.AUDIO){
            mAudioTrackFormat = trackFormat;
        }



        if (allTracksAdded()) {
            Log.w(TAG, "addTrack: All tracks added, marking Local muxer as ready!");
            markReadyToStart();
        }
        Log.w(TAG, "addTrack: Track index to class " + getClass().getSimpleName() + " of type " + trackType + ", track saved for future use!");
        //Return dummy
        return 1;
    }


    /**
     *
     * Marking the Muxer as ready to be started but not yet!
     *
     */
    private void markReadyToStart() {
        mReadyToStart = true;
    }



    private void start() {
        if(mStarted){
            Log.i(TAG, "start: Skipped start due to muxer already started");
        }
        mStartTimeUs = 0;
        mMuxer.start();
        Log.w(TAG, "start: Muxer started!");
        mStarted = true;
    }

    protected void stop() {
        if(mStarted) {
            Log.w(TAG, "stop: Calling stop to the muxer");
            mMuxer.stop();
            Log.w(TAG, "stop: muxer stopped!");
            mStarted = false;
            mStopped = true;
            mReadyToStart = false;
        }
    }

    @Override
    public void release() {
        super.release();
        if(mStopped) {
            mMuxer.release();
            mStopped = false;
            mStarted = false;
        }
    }

    @Override
    public boolean isStarted() {
        return mStarted;
    }

    @Override
    public void writeSampleData(MediaCodec encoder, int trackIndex, int bufferIndex, ByteBuffer encodedData, MediaCodec.BufferInfo bufferInfo) {
        if(!mStarted) {

            return;
        }

        super.writeSampleData(encoder, trackIndex, bufferIndex, encodedData, bufferInfo);
        if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
            // MediaMuxer gets the codec config info via the addTrack command
            if (VERBOSE) Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
            return;
        }

        if (bufferInfo.size == 0) {
            if (VERBOSE) Log.d(TAG, "ignoring zero size buffer");
            return;
        }

        if (!mStarted) {
            Log.w(TAG, "writeSampleData called before muxer started. Ignoring packet. Track index: " + trackIndex + " tracks added: " + mNumTracks);
//            encoder.releaseOutputBuffer(bufferIndex, false);
            return;
        }

//        bufferInfo.presentationTimeUs = getNextRelativePts(bufferInfo.presentationTimeUs, trackIndex);


       if(mStartTimeUs == 0) {
              mStartTimeUs = bufferInfo.presentationTimeUs;
             }
        bufferInfo.presentationTimeUs -= mStartTimeUs;
        if(bufferInfo.presentationTimeUs < 0) {
             bufferInfo.presentationTimeUs = 0;
        }
        bufferInfo.presentationTimeUs = getSafePts(bufferInfo.presentationTimeUs, trackIndex);


        mMuxer.writeSampleData(trackIndex, encodedData, bufferInfo);

//        encoder.releaseOutputBuffer(bufferIndex, false);

        if (allTracksFinished()) {
            Log.w(TAG, "writeSampleData: All tracks finished! Calling Stop");
            stop();
        }
    }

    @Override
    public void forceStop() {
        Log.w(TAG, "forceStop: ForceStop called!");
        stop();
    }
}

请注意,RTMP复用器和本地复用器可能无法同时启动,这就是我存储媒体信息的原因,然后仅在用户请求启动复用器时才使用它。

1 个答案:

答案 0 :(得分:1)

如果本地多路复用器尚未准备就绪并且您跳过写入数据包,则可能会跳过编写关键帧。在您到达下一个关键帧之前,以后不是关键帧的视频帧无法解码(如果文件以非关键帧开头,谁知道视频播放器是否会混淆?)。如果关键帧之间的间隔时间很长,则可能无法在合理的时间内获得关键帧。 (虽然,如果你也通过RTMP流式传输,你可能会经常使用关键帧。)

我的建议是确保在本地复用器准备就绪之前存储所有生成的数据包,并在准备就绪后将它们写入本地复用器,或者继续跳过数据包直到到达下一个关键帧。