Skip to content

Commit 70c0902

Browse files
authored
Merge pull request #320 from AgoraIO/fix-NMS-5469
[Android]optimize pushExternalAudioFrame timestamp.
2 parents 6f2d22c + f1e734a commit 70c0902

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/customaudio/CustomAudioSource.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class CustomAudioSource extends BaseFragment implements View.OnClickListe
5454
public static RtcEngineEx engine;
5555
private Switch mic, pcm;
5656
private ChannelMediaOptions option = new ChannelMediaOptions();
57+
private volatile int pushTimes = 0;
5758

5859
private AudioSeatManager audioSeatManager;
5960
private AudioFileReader audioPushingHelper;
@@ -155,7 +156,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
155156

156157
audioPushingHelper = new AudioFileReader(requireContext(), (buffer, timestamp) -> {
157158
if(joined && engine != null){
158-
engine.pushExternalAudioFrame(buffer, timestamp);
159+
Log.i(TAG, "pushExternalAudioFrame times:" + pushTimes++);
160+
engine.pushExternalAudioFrame(buffer, 0);
159161
}
160162
});
161163
} catch (Exception e) {
@@ -329,6 +331,7 @@ public void run() {
329331
join.setEnabled(true);
330332
join.setText(getString(R.string.leave));
331333
if(audioPushingHelper != null){
334+
pushTimes = 0;
332335
audioPushingHelper.start();
333336
}
334337
audioSeatManager.upLocalSeat(uid);

Android/APIExample/app/src/main/java/io/agora/api/example/utils/AudioFileReader.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88

99
public class AudioFileReader {
1010
private static final String AUDIO_FILE = "output.raw";
11-
public static final Integer SAMPLE_RATE = 44100;
12-
public static final Integer SAMPLE_NUM_OF_CHANNEL = 2;
13-
public static final Integer BITS_PER_SAMPLE = 16;
14-
private static final Integer SAMPLES = 441;
15-
private static final Integer BUFFER_SIZE = SAMPLES * BITS_PER_SAMPLE / 8 * SAMPLE_NUM_OF_CHANNEL;
16-
private static final Integer PUSH_INTERVAL = SAMPLES * 1000 / SAMPLE_RATE;
11+
public static final int SAMPLE_RATE = 44100;
12+
public static final int SAMPLE_NUM_OF_CHANNEL = 2;
13+
public static final int BITS_PER_SAMPLE = 16;
14+
15+
public static final float BYTE_PER_SAMPLE = 1.0f * BITS_PER_SAMPLE / 8 * SAMPLE_NUM_OF_CHANNEL;
16+
public static final float DURATION_PER_SAMPLE = 1000.0f / SAMPLE_RATE; // ms
17+
public static final float SAMPLE_COUNT_PER_MS = SAMPLE_RATE * 1.0f / 1000; // ms
18+
19+
private static final int BUFFER_SAMPLE_COUNT = (int) (SAMPLE_COUNT_PER_MS * 10); // 10ms sample count
20+
private static final int BUFFER_BYTE_SIZE = (int) (BUFFER_SAMPLE_COUNT * BYTE_PER_SAMPLE); // byte
21+
private static final long BUFFER_DURATION = (long) (BUFFER_SAMPLE_COUNT * DURATION_PER_SAMPLE); // ms
1722

1823
private final Context context;
1924
private final OnAudioReadListener audioReadListener;
@@ -62,16 +67,21 @@ public void run() {
6267
}
6368
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
6469
pushing = true;
70+
71+
long start_time = System.currentTimeMillis();;
72+
int sent_audio_frames = 0;
6573
while (pushing) {
66-
long before = System.currentTimeMillis();
6774
if(audioReadListener != null){
6875
audioReadListener.onAudioRead(readBuffer(), System.currentTimeMillis());
6976
}
77+
++ sent_audio_frames;
78+
long next_frame_start_time = sent_audio_frames * BUFFER_DURATION + start_time;
7079
long now = System.currentTimeMillis();
71-
long consuming = now - before;
72-
if(consuming < PUSH_INTERVAL){
80+
81+
if(next_frame_start_time > now){
82+
long sleep_duration = next_frame_start_time - now;
7383
try {
74-
Thread.sleep(PUSH_INTERVAL - consuming);
84+
Thread.sleep(sleep_duration);
7585
} catch (InterruptedException e) {
7686
e.printStackTrace();
7787
}
@@ -90,7 +100,7 @@ public void run() {
90100
}
91101

92102
private byte[] readBuffer() {
93-
int byteSize = BUFFER_SIZE;
103+
int byteSize = BUFFER_BYTE_SIZE;
94104
byte[] buffer = new byte[byteSize];
95105
try {
96106
if (inputStream.read(buffer) < 0) {

0 commit comments

Comments
 (0)