Skip to content

Commit

Permalink
fix fromfile loop mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Oct 20, 2023
1 parent 3c10827 commit 3777430
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class AudioDecoder extends BaseDecoder {
private boolean muted = false;

public AudioDecoder(GetMicrophoneData getMicrophoneData,
AudioDecoderInterface audioDecoderInterface) {
AudioDecoderInterface audioDecoderInterface, DecoderInterface decoderInterface) {
super(decoderInterface);
TAG = "AudioDecoder";
this.getMicrophoneData = getMicrophoneData;
this.audioDecoderInterface = audioDecoderInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public abstract class BaseDecoder {
//Avoid decode while change output surface
protected AtomicBoolean pause = new AtomicBoolean(false);
protected volatile boolean looped = false;
private final DecoderInterface decoderInterface;

public BaseDecoder(DecoderInterface decoderInterface) {
this.decoderInterface = decoderInterface;
}

public boolean initExtractor(String filePath) throws IOException {
extractor = new MediaExtractor();
Expand Down Expand Up @@ -242,6 +247,7 @@ private void decode() {
moveTo(0);
continue;
} else {
decoderInterface.onLoop();
looped = false;
}
}
Expand Down Expand Up @@ -281,8 +287,8 @@ private void decode() {
}
boolean render = decodeOutput(output);
codec.releaseOutputBuffer(outIndex, render && bufferInfo.size != 0);
double reamingTime = getDuration() - getTime();
if (reamingTime < 1 && loopMode) {
boolean finished = extractor.getSampleTime() < 0;
if (finished && loopMode) {
moveTo(0);
looped = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pedro.encoder.input.decoder

/**
* Created by pedro on 20/10/23.
*/
interface DecoderInterface {
fun onLoop()
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class VideoDecoder extends BaseDecoder {
private int height;
private int fps;

public VideoDecoder(VideoDecoderInterface videoDecoderInterface) {
public VideoDecoder(VideoDecoderInterface videoDecoderInterface, DecoderInterface decoderInterface) {
super(decoderInterface);
TAG = "VideoDecoder";
this.videoDecoderInterface = videoDecoderInterface;
}
Expand Down
22 changes: 19 additions & 3 deletions library/src/main/java/com/pedro/library/base/FromFileBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.media.MediaFormat;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -36,6 +35,7 @@
import com.pedro.encoder.input.audio.GetMicrophoneData;
import com.pedro.encoder.input.decoder.AudioDecoder;
import com.pedro.encoder.input.decoder.AudioDecoderInterface;
import com.pedro.encoder.input.decoder.DecoderInterface;
import com.pedro.encoder.input.decoder.VideoDecoder;
import com.pedro.encoder.input.decoder.VideoDecoderInterface;
import com.pedro.encoder.utils.CodecUtil;
Expand Down Expand Up @@ -117,8 +117,8 @@ private void init(VideoDecoderInterface videoDecoderInterface,
AudioDecoderInterface audioDecoderInterface) {
videoEncoder = new VideoEncoder(getVideoData);
audioEncoder = new AudioEncoder(getAacData);
videoDecoder = new VideoDecoder(videoDecoderInterface);
audioDecoder = new AudioDecoder(getMicrophoneData, audioDecoderInterface);
videoDecoder = new VideoDecoder(videoDecoderInterface, decoderInterface);
audioDecoder = new AudioDecoder(getMicrophoneData, audioDecoderInterface, decoderInterface);
recordController = new AndroidMuxerRecordController();
}

Expand Down Expand Up @@ -653,4 +653,20 @@ public void onVideoFormat(@NonNull MediaFormat mediaFormat) {
recordController.setVideoFormat(mediaFormat, !audioEnabled);
}
};
private final DecoderInterface decoderInterface = new DecoderInterface() {

private int trackFinished = 0;

@Override
public void onLoop() {
int maxTracks = 0;
if (audioEnabled) maxTracks++;
if (videoEnabled) maxTracks++;
trackFinished++;
if (trackFinished >= maxTracks) {
reSyncFile();
trackFinished = 0;
}
}
};
}

0 comments on commit 3777430

Please sign in to comment.