Skip to content

Commit

Permalink
Merge pull request #320 from AgoraIO/fix-NMS-5469
Browse files Browse the repository at this point in the history
[Android]optimize pushExternalAudioFrame timestamp.
  • Loading branch information
plutoless committed Dec 20, 2022
2 parents 6f2d22c + f1e734a commit 70c0902
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class CustomAudioSource extends BaseFragment implements View.OnClickListe
public static RtcEngineEx engine;
private Switch mic, pcm;
private ChannelMediaOptions option = new ChannelMediaOptions();
private volatile int pushTimes = 0;

private AudioSeatManager audioSeatManager;
private AudioFileReader audioPushingHelper;
Expand Down Expand Up @@ -155,7 +156,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

audioPushingHelper = new AudioFileReader(requireContext(), (buffer, timestamp) -> {
if(joined && engine != null){
engine.pushExternalAudioFrame(buffer, timestamp);
Log.i(TAG, "pushExternalAudioFrame times:" + pushTimes++);
engine.pushExternalAudioFrame(buffer, 0);
}
});
} catch (Exception e) {
Expand Down Expand Up @@ -329,6 +331,7 @@ public void run() {
join.setEnabled(true);
join.setText(getString(R.string.leave));
if(audioPushingHelper != null){
pushTimes = 0;
audioPushingHelper.start();
}
audioSeatManager.upLocalSeat(uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@

public class AudioFileReader {
private static final String AUDIO_FILE = "output.raw";
public static final Integer SAMPLE_RATE = 44100;
public static final Integer SAMPLE_NUM_OF_CHANNEL = 2;
public static final Integer BITS_PER_SAMPLE = 16;
private static final Integer SAMPLES = 441;
private static final Integer BUFFER_SIZE = SAMPLES * BITS_PER_SAMPLE / 8 * SAMPLE_NUM_OF_CHANNEL;
private static final Integer PUSH_INTERVAL = SAMPLES * 1000 / SAMPLE_RATE;
public static final int SAMPLE_RATE = 44100;
public static final int SAMPLE_NUM_OF_CHANNEL = 2;
public static final int BITS_PER_SAMPLE = 16;

public static final float BYTE_PER_SAMPLE = 1.0f * BITS_PER_SAMPLE / 8 * SAMPLE_NUM_OF_CHANNEL;
public static final float DURATION_PER_SAMPLE = 1000.0f / SAMPLE_RATE; // ms
public static final float SAMPLE_COUNT_PER_MS = SAMPLE_RATE * 1.0f / 1000; // ms

private static final int BUFFER_SAMPLE_COUNT = (int) (SAMPLE_COUNT_PER_MS * 10); // 10ms sample count
private static final int BUFFER_BYTE_SIZE = (int) (BUFFER_SAMPLE_COUNT * BYTE_PER_SAMPLE); // byte
private static final long BUFFER_DURATION = (long) (BUFFER_SAMPLE_COUNT * DURATION_PER_SAMPLE); // ms

private final Context context;
private final OnAudioReadListener audioReadListener;
Expand Down Expand Up @@ -62,16 +67,21 @@ public void run() {
}
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
pushing = true;

long start_time = System.currentTimeMillis();;
int sent_audio_frames = 0;
while (pushing) {
long before = System.currentTimeMillis();
if(audioReadListener != null){
audioReadListener.onAudioRead(readBuffer(), System.currentTimeMillis());
}
++ sent_audio_frames;
long next_frame_start_time = sent_audio_frames * BUFFER_DURATION + start_time;
long now = System.currentTimeMillis();
long consuming = now - before;
if(consuming < PUSH_INTERVAL){

if(next_frame_start_time > now){
long sleep_duration = next_frame_start_time - now;
try {
Thread.sleep(PUSH_INTERVAL - consuming);
Thread.sleep(sleep_duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand All @@ -90,7 +100,7 @@ public void run() {
}

private byte[] readBuffer() {
int byteSize = BUFFER_SIZE;
int byteSize = BUFFER_BYTE_SIZE;
byte[] buffer = new byte[byteSize];
try {
if (inputStream.read(buffer) < 0) {
Expand Down

0 comments on commit 70c0902

Please sign in to comment.