Skip to content

Commit c2b77fd

Browse files
authored
Merge pull request #1882 from pedroSG94/fix/record-audio-ts
avoid decrement audio ts
2 parents 87ac138 + 40e7814 commit c2b77fd

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

encoder/src/main/java/com/pedro/encoder/BaseEncoder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.RequiresApi;
2929

30+
import com.pedro.common.ExtensionsKt;
3031
import com.pedro.common.TimeUtils;
3132
import com.pedro.encoder.audio.G711Codec;
3233
import com.pedro.encoder.utils.CodecUtil;
@@ -36,6 +37,7 @@
3637
import java.util.concurrent.BlockingQueue;
3738
import java.util.concurrent.ExecutorService;
3839
import java.util.concurrent.Executors;
40+
import java.util.concurrent.PriorityBlockingQueue;
3941

4042
/**
4143
* Created by pedro on 18/09/19.
@@ -49,12 +51,12 @@ public abstract class BaseEncoder implements EncoderCallback {
4951
private ExecutorService executorService;
5052
protected BlockingQueue<Frame> queue = new ArrayBlockingQueue<>(80);
5153
protected MediaCodec codec;
52-
protected long presentTimeUs;
54+
protected volatile long presentTimeUs;
5355
protected volatile boolean running = false;
5456
protected boolean isBufferMode = true;
5557
protected CodecUtil.CodecType codecType = CodecUtil.CodecType.FIRST_COMPATIBLE_FOUND;
5658
private MediaCodec.Callback callback;
57-
private long oldTimeStamp = 0L;
59+
private volatile long oldTimeStamp = 0L;
5860
protected boolean shouldReset = true;
5961
protected boolean prepared = false;
6062
private Handler handler;
@@ -132,10 +134,10 @@ private void initCodec() {
132134

133135
protected void fixTimeStamp(MediaCodec.BufferInfo info) {
134136
if (oldTimeStamp > info.presentationTimeUs) {
135-
info.presentationTimeUs = oldTimeStamp;
136-
} else {
137-
oldTimeStamp = info.presentationTimeUs;
137+
final long currentTs = TimeUtils.getCurrentTimeMicro() - presentTimeUs;
138+
info.presentationTimeUs = Math.max(currentTs, oldTimeStamp + 1);
138139
}
140+
oldTimeStamp = info.presentationTimeUs;
139141
}
140142

141143
private void reloadCodec(IllegalStateException e) {

library/src/main/java/com/pedro/library/base/recording/BaseRecordController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public abstract class BaseRecordController implements RecordController {
3838
protected Listener listener;
3939
protected int videoTrack = -1;
4040
protected int audioTrack = -1;
41-
protected final MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();
42-
protected final MediaCodec.BufferInfo audioInfo = new MediaCodec.BufferInfo();
4341
protected BitrateManager bitrateManager;
44-
protected long startTs = 0;
42+
protected volatile long startTs = 0;
4543
protected RecordTracks tracks = RecordTracks.ALL;
4644

4745
public void setVideoCodec(VideoCodec videoCodec) {
@@ -100,11 +98,11 @@ protected boolean isKeyFrame(ByteBuffer videoBuffer) {
10098
}
10199

102100
//We can't reuse info because could produce stream issues
103-
protected void updateFormat(MediaCodec.BufferInfo newInfo, MediaCodec.BufferInfo oldInfo) {
101+
protected MediaCodec.BufferInfo updateFormat(MediaCodec.BufferInfo oldInfo) {
102+
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
104103
if (startTs <= 0) startTs = oldInfo.presentationTimeUs;
105-
newInfo.flags = oldInfo.flags;
106-
newInfo.offset = oldInfo.offset;
107-
newInfo.size = oldInfo.size;
108-
newInfo.presentationTimeUs = Math.max(0, oldInfo.presentationTimeUs - startTs - pauseTime);
104+
long ts = Math.max(0, oldInfo.presentationTimeUs - startTs - pauseTime);
105+
info.set(oldInfo.offset, oldInfo.size, ts, oldInfo.flags);
106+
return info;
109107
}
110108
}

library/src/main/java/com/pedro/library/util/AacMuxerRecordController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
108108
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
109109
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
110110
if (status == Status.RECORDING) {
111-
updateFormat(this.audioInfo, audioInfo);
112111
//we need duplicate buffer to avoid problems with the buffer
113-
write(audioBuffer.duplicate(), this.audioInfo);
112+
write(audioBuffer.duplicate(), updateFormat(audioInfo));
114113
}
115114
}
116115

library/src/main/java/com/pedro/library/util/AndroidMuxerRecordController.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.media.MediaFormat;
2121
import android.media.MediaMuxer;
2222
import android.os.Build;
23+
import android.util.Log;
2324

2425
import androidx.annotation.NonNull;
2526
import androidx.annotation.Nullable;
@@ -114,16 +115,14 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
114115
if (listener != null) listener.onStatusChange(status);
115116
}
116117
if (status == Status.RECORDING && tracks != RecordTracks.AUDIO) {
117-
updateFormat(this.videoInfo, videoInfo);
118-
write(videoTrack, videoBuffer, this.videoInfo);
118+
write(videoTrack, videoBuffer, videoInfo);
119119
}
120120
}
121121

122122
@Override
123123
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
124124
if (status == Status.RECORDING && tracks != RecordTracks.VIDEO) {
125-
updateFormat(this.audioInfo, audioInfo);
126-
write(audioTrack, audioBuffer, this.audioInfo);
125+
write(audioTrack, audioBuffer, audioInfo);
127126
}
128127
}
129128

@@ -155,9 +154,12 @@ private void init() {
155154

156155
private void write(int track, ByteBuffer byteBuffer, MediaCodec.BufferInfo info) {
157156
if (track == -1) return;
157+
String trackString = track == audioTrack ? "Audio" : "Video";
158158
try {
159-
mediaMuxer.writeSampleData(track, byteBuffer, info);
160-
if (bitrateManager != null) bitrateManager.calculateBitrate(info.size * 8L, ExtensionsKt.getSuspendContext());
159+
MediaCodec.BufferInfo i = updateFormat(info);
160+
Log.i(TAG, trackString + ", ts: " + i.presentationTimeUs + ", flag: " + i.flags);
161+
mediaMuxer.writeSampleData(track, byteBuffer, i);
162+
if (bitrateManager != null) bitrateManager.calculateBitrate(i.size * 8L, ExtensionsKt.getSuspendContext());
161163
} catch (Exception e) {
162164
if (listener != null) listener.onError(e);
163165
}

library/src/main/java/com/pedro/library/util/AndroidMuxerWebmRecordController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
114114
@Override
115115
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
116116
if (status == Status.RECORDING) {
117-
updateFormat(this.audioInfo, audioInfo);
118-
write(audioTrack, audioBuffer, this.audioInfo);
117+
write(audioTrack, audioBuffer, audioInfo);
119118
}
120119
}
121120

@@ -146,7 +145,7 @@ private void init() {
146145
private void write(int track, ByteBuffer byteBuffer, MediaCodec.BufferInfo info) {
147146
if (track == -1) return;
148147
try {
149-
mediaMuxer.writeSampleData(track, byteBuffer, info);
148+
mediaMuxer.writeSampleData(track, byteBuffer, updateFormat(info));
150149
if (bitrateManager != null) bitrateManager.calculateBitrate(info.size * 8L, ExtensionsKt.getSuspendContext());
151150
} catch (Exception e) {
152151
if (listener != null) listener.onError(e);

0 commit comments

Comments
 (0)