desc:增加健壮性

main
xiaowusky 2 years 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,16 +105,19 @@ public class AndroidMuxer {
} }
public boolean release() { public boolean release() {
if (mMuxer != null) {
synchronized (mMuxer) { synchronized (mMuxer) {
if (++mNumReleases == mNumTracks) { if (++mNumReleases == mNumTracks) {
stopMuxer(); stopMuxer();
return true; return true;
} }
} }
}
return false; return false;
} }
public void stopMuxer() { public void stopMuxer() {
if (mMuxer != null) {
mMuxer.stop(); mMuxer.stop();
mMuxer.release(); mMuxer.release();
if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) { if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) {
@ -118,3 +125,4 @@ public class AndroidMuxer {
} }
} }
} }
}

@ -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) {
try {
MediaFormat newFormat = mMediaCodec.getOutputFormat(); MediaFormat newFormat = mMediaCodec.getOutputFormat();
mTrackIndex = androidMuxer.addTrack(newFormat); mTrackIndex = androidMuxer.addTrack(newFormat);
} catch (Exception e) {
e.printStackTrace();
}
} }
}, mHandler); }, mHandler);
mMediaCodec.start(); mMediaCodec.start();

Loading…
Cancel
Save