Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions encoder/src/main/java/com/pedro/encoder/BaseEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.pedro.common.ExtensionsKt;
import com.pedro.common.TimeUtils;
import com.pedro.encoder.audio.G711Codec;
import com.pedro.encoder.utils.CodecUtil;
Expand All @@ -36,6 +37,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

/**
* Created by pedro on 18/09/19.
Expand All @@ -49,12 +51,12 @@ public abstract class BaseEncoder implements EncoderCallback {
private ExecutorService executorService;
protected BlockingQueue<Frame> queue = new ArrayBlockingQueue<>(80);
protected MediaCodec codec;
protected long presentTimeUs;
protected volatile long presentTimeUs;
protected volatile boolean running = false;
protected boolean isBufferMode = true;
protected CodecUtil.CodecType codecType = CodecUtil.CodecType.FIRST_COMPATIBLE_FOUND;
private MediaCodec.Callback callback;
private long oldTimeStamp = 0L;
private volatile long oldTimeStamp = 0L;
protected boolean shouldReset = true;
protected boolean prepared = false;
private Handler handler;
Expand Down Expand Up @@ -132,10 +134,10 @@ private void initCodec() {

protected void fixTimeStamp(MediaCodec.BufferInfo info) {
if (oldTimeStamp > info.presentationTimeUs) {
info.presentationTimeUs = oldTimeStamp;
} else {
oldTimeStamp = info.presentationTimeUs;
final long currentTs = TimeUtils.getCurrentTimeMicro() - presentTimeUs;
info.presentationTimeUs = Math.max(currentTs, oldTimeStamp + 1);
}
oldTimeStamp = info.presentationTimeUs;
}

private void reloadCodec(IllegalStateException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public abstract class BaseRecordController implements RecordController {
protected Listener listener;
protected int videoTrack = -1;
protected int audioTrack = -1;
protected final MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();
protected final MediaCodec.BufferInfo audioInfo = new MediaCodec.BufferInfo();
protected BitrateManager bitrateManager;
protected long startTs = 0;
protected volatile long startTs = 0;
protected RecordTracks tracks = RecordTracks.ALL;

public void setVideoCodec(VideoCodec videoCodec) {
Expand Down Expand Up @@ -100,11 +98,11 @@ protected boolean isKeyFrame(ByteBuffer videoBuffer) {
}

//We can't reuse info because could produce stream issues
protected void updateFormat(MediaCodec.BufferInfo newInfo, MediaCodec.BufferInfo oldInfo) {
protected MediaCodec.BufferInfo updateFormat(MediaCodec.BufferInfo oldInfo) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
if (startTs <= 0) startTs = oldInfo.presentationTimeUs;
newInfo.flags = oldInfo.flags;
newInfo.offset = oldInfo.offset;
newInfo.size = oldInfo.size;
newInfo.presentationTimeUs = Math.max(0, oldInfo.presentationTimeUs - startTs - pauseTime);
long ts = Math.max(0, oldInfo.presentationTimeUs - startTs - pauseTime);
info.set(oldInfo.offset, oldInfo.size, ts, oldInfo.flags);
return info;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
if (status == Status.RECORDING) {
updateFormat(this.audioInfo, audioInfo);
//we need duplicate buffer to avoid problems with the buffer
write(audioBuffer.duplicate(), this.audioInfo);
write(audioBuffer.duplicate(), updateFormat(audioInfo));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -114,16 +115,14 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
if (listener != null) listener.onStatusChange(status);
}
if (status == Status.RECORDING && tracks != RecordTracks.AUDIO) {
updateFormat(this.videoInfo, videoInfo);
write(videoTrack, videoBuffer, this.videoInfo);
write(videoTrack, videoBuffer, videoInfo);
}
}

@Override
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
if (status == Status.RECORDING && tracks != RecordTracks.VIDEO) {
updateFormat(this.audioInfo, audioInfo);
write(audioTrack, audioBuffer, this.audioInfo);
write(audioTrack, audioBuffer, audioInfo);
}
}

Expand Down Expand Up @@ -155,9 +154,12 @@ private void init() {

private void write(int track, ByteBuffer byteBuffer, MediaCodec.BufferInfo info) {
if (track == -1) return;
String trackString = track == audioTrack ? "Audio" : "Video";
try {
mediaMuxer.writeSampleData(track, byteBuffer, info);
if (bitrateManager != null) bitrateManager.calculateBitrate(info.size * 8L, ExtensionsKt.getSuspendContext());
MediaCodec.BufferInfo i = updateFormat(info);
Log.i(TAG, trackString + ", ts: " + i.presentationTimeUs + ", flag: " + i.flags);
mediaMuxer.writeSampleData(track, byteBuffer, i);
if (bitrateManager != null) bitrateManager.calculateBitrate(i.size * 8L, ExtensionsKt.getSuspendContext());
} catch (Exception e) {
if (listener != null) listener.onError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo)
@Override
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
if (status == Status.RECORDING) {
updateFormat(this.audioInfo, audioInfo);
write(audioTrack, audioBuffer, this.audioInfo);
write(audioTrack, audioBuffer, audioInfo);
}
}

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