desc:增加健壮性

main
xiaowusky 1 year ago
parent 9a5616ee69
commit 0c23f5bfa0

@ -62,6 +62,10 @@ public class AndroidMuxer {
throw new IllegalStateException(); throw new IllegalStateException();
} }
if (mMuxer == null) {
throw new IllegalStateException("mMuxer is null");
}
synchronized (mMuxer) { synchronized (mMuxer) {
int track = mMuxer.addTrack(trackFormat); int track = mMuxer.addTrack(trackFormat);
mVideoFormat = trackFormat; mVideoFormat = trackFormat;
@ -79,7 +83,7 @@ public class AndroidMuxer {
} }
public void writeSampleData(int trackIndex, ByteBuffer encodedData, MediaCodec.BufferInfo bufferInfo) { public void writeSampleData(int trackIndex, ByteBuffer encodedData, MediaCodec.BufferInfo bufferInfo) {
if (mNumReleases != 0) { if (mNumReleases != 0 || mMuxer == null) {
return; return;
} }
synchronized (mMuxer) { synchronized (mMuxer) {
@ -101,20 +105,24 @@ public class AndroidMuxer {
} }
public boolean release() { public boolean release() {
synchronized (mMuxer) { if (mMuxer != null) {
if (++mNumReleases == mNumTracks) { synchronized (mMuxer) {
stopMuxer(); if (++mNumReleases == mNumTracks) {
return true; stopMuxer();
return true;
}
} }
} }
return false; return false;
} }
public void stopMuxer() { public void stopMuxer() {
mMuxer.stop(); if (mMuxer != null) {
mMuxer.release(); mMuxer.stop();
if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) { mMuxer.release();
insertToDB(mStartRecordTime, mCurrentPath); if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) {
insertToDB(mStartRecordTime, mCurrentPath);
}
} }
} }
} }

@ -287,8 +287,12 @@ public class MediaCodecManager {
@Override @Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) { public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
MediaFormat newFormat = mMediaCodec.getOutputFormat(); try {
mTrackIndex = androidMuxer.addTrack(newFormat); MediaFormat newFormat = mMediaCodec.getOutputFormat();
mTrackIndex = androidMuxer.addTrack(newFormat);
} catch (Exception e) {
e.printStackTrace();
}
} }
}, mHandler); }, mHandler);
mMediaCodec.start(); mMediaCodec.start();

Loading…
Cancel
Save