Skip to content

Commit 26931a4

Browse files
committed
Revert "Merge pull request pedroSG94#1882 from pedroSG94/fix/record-audio-ts"
This reverts commit c2b77fd, reversing changes made to 87ac138.
1 parent 3da506e commit 26931a4

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

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

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

30-
import com.pedro.common.ExtensionsKt;
3130
import com.pedro.common.TimeUtils;
3231
import com.pedro.encoder.audio.G711Codec;
3332
import com.pedro.encoder.utils.CodecUtil;
@@ -37,7 +36,6 @@
3736
import java.util.concurrent.BlockingQueue;
3837
import java.util.concurrent.ExecutorService;
3938
import java.util.concurrent.Executors;
40-
import java.util.concurrent.PriorityBlockingQueue;
4139

4240
/**
4341
* Created by pedro on 18/09/19.
@@ -51,12 +49,12 @@ public abstract class BaseEncoder implements EncoderCallback {
5149
private ExecutorService executorService;
5250
protected BlockingQueue<Frame> queue = new ArrayBlockingQueue<>(80);
5351
protected MediaCodec codec;
54-
protected volatile long presentTimeUs;
52+
protected long presentTimeUs;
5553
protected volatile boolean running = false;
5654
protected boolean isBufferMode = true;
5755
protected CodecUtil.CodecType codecType = CodecUtil.CodecType.FIRST_COMPATIBLE_FOUND;
5856
private MediaCodec.Callback callback;
59-
private volatile long oldTimeStamp = 0L;
57+
private long oldTimeStamp = 0L;
6058
protected boolean shouldReset = true;
6159
protected boolean prepared = false;
6260
private Handler handler;
@@ -134,10 +132,10 @@ private void initCodec() {
134132

135133
protected void fixTimeStamp(MediaCodec.BufferInfo info) {
136134
if (oldTimeStamp > info.presentationTimeUs) {
137-
final long currentTs = TimeUtils.getCurrentTimeMicro() - presentTimeUs;
138-
info.presentationTimeUs = Math.max(currentTs, oldTimeStamp + 1);
135+
info.presentationTimeUs = oldTimeStamp;
136+
} else {
137+
oldTimeStamp = info.presentationTimeUs;
139138
}
140-
oldTimeStamp = info.presentationTimeUs;
141139
}
142140

143141
private void reloadCodec(IllegalStateException e) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ 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();
4143
protected BitrateManager bitrateManager;
42-
protected volatile long startTs = 0;
44+
protected long startTs = 0;
4345
protected RecordTracks tracks = RecordTracks.ALL;
4446

4547
public void setVideoCodec(VideoCodec videoCodec) {
@@ -98,11 +100,11 @@ protected boolean isKeyFrame(ByteBuffer videoBuffer) {
98100
}
99101

100102
//We can't reuse info because could produce stream issues
101-
protected MediaCodec.BufferInfo updateFormat(MediaCodec.BufferInfo oldInfo) {
102-
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
103+
protected void updateFormat(MediaCodec.BufferInfo newInfo, MediaCodec.BufferInfo oldInfo) {
103104
if (startTs <= 0) startTs = oldInfo.presentationTimeUs;
104-
long ts = Math.max(0, oldInfo.presentationTimeUs - startTs - pauseTime);
105-
info.set(oldInfo.offset, oldInfo.size, ts, oldInfo.flags);
106-
return info;
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);
107109
}
108110
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ 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);
111112
//we need duplicate buffer to avoid problems with the buffer
112-
write(audioBuffer.duplicate(), updateFormat(audioInfo));
113+
write(audioBuffer.duplicate(), this.audioInfo);
113114
}
114115
}
115116

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

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

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

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

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

155156
private void write(int track, ByteBuffer byteBuffer, MediaCodec.BufferInfo info) {
156157
if (track == -1) return;
157-
String trackString = track == audioTrack ? "Audio" : "Video";
158158
try {
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());
159+
mediaMuxer.writeSampleData(track, byteBuffer, info);
160+
if (bitrateManager != null) bitrateManager.calculateBitrate(info.size * 8L, ExtensionsKt.getSuspendContext());
163161
} catch (Exception e) {
164162
if (listener != null) listener.onError(e);
165163
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ 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-
write(audioTrack, audioBuffer, audioInfo);
117+
updateFormat(this.audioInfo, audioInfo);
118+
write(audioTrack, audioBuffer, this.audioInfo);
118119
}
119120
}
120121

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

0 commit comments

Comments
 (0)