Skip to content

Commit 5f71c75

Browse files
committed
Process & capture on audio thread
1 parent 87f723a commit 5f71c75

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

Runtime/Scripts/ApmReverseStream.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Threading;
21
using LiveKit.Internal;
32
using UnityEngine;
43

@@ -14,7 +13,6 @@ internal class ApmReverseStream
1413
{
1514
private readonly AudioBuffer _captureBuffer = new AudioBuffer();
1615
private readonly AudioProcessingModule _apm; // APM is thread safe
17-
private Thread _thread;
1816
private AudioFilter _audioFilter;
1917

2018
internal ApmReverseStream(AudioProcessingModule apm)
@@ -32,32 +30,24 @@ internal void Start()
3230
}
3331
_audioFilter = audioListener.gameObject.AddComponent<AudioFilter>();
3432
_audioFilter.AudioRead += OnAudioRead;
35-
36-
_thread = new Thread(ProcessReverseStream);
37-
_thread.Start();
3833
}
3934

4035
internal void Stop()
4136
{
42-
_thread?.Abort();
4337
if (_audioFilter != null)
4438
Object.Destroy(_audioFilter);
4539
}
4640

47-
private void ProcessReverseStream()
41+
private void OnAudioRead(float[] data, int channels, int sampleRate)
4842
{
43+
_captureBuffer.Write(data, (uint)channels, (uint)sampleRate);
4944
while (true)
5045
{
51-
Thread.Sleep(Constants.TASK_DELAY);
5246
using var frame = _captureBuffer.ReadDuration(AudioProcessingModule.FRAME_DURATION_MS);
53-
if (frame == null) continue;
47+
if (frame == null) break;
48+
5449
_apm.ProcessReverseStream(frame);
5550
}
5651
}
57-
58-
private void OnAudioRead(float[] data, int channels, int sampleRate)
59-
{
60-
_captureBuffer.Write(data, (uint)channels, (uint)sampleRate);
61-
}
6252
}
6353
}

Runtime/Scripts/RtcAudioSource.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections;
33
using LiveKit.Proto;
44
using LiveKit.Internal;
5-
using System.Threading;
65
using LiveKit.Internal.FFIClients.Requests;
76
using UnityEngine;
87

@@ -43,7 +42,6 @@ public abstract class RtcAudioSource : IRtcSource
4342
protected AudioSourceInfo _info;
4443

4544
// Possibly used on the AudioThread
46-
private Thread _readAudioThread;
4745
private AudioBuffer _captureBuffer = new AudioBuffer();
4846
private readonly AudioProcessingModule _apm;
4947
private readonly ApmReverseStream _apmReverseStream;
@@ -84,42 +82,31 @@ public IEnumerator PrepareAndStart()
8482
public void Start()
8583
{
8684
Stop();
87-
_readAudioThread = new Thread(Update);
88-
_readAudioThread.Start();
8985
_apmReverseStream?.Start();
9086
AudioRead += OnAudioRead;
9187
Play();
9288
}
9389

9490
public virtual void Stop()
9591
{
96-
_readAudioThread?.Abort();
9792
_apmReverseStream?.Stop();
9893
AudioRead -= OnAudioRead;
9994
}
10095

101-
private void Update()
96+
private void OnAudioRead(float[] data, int channels, int sampleRate)
10297
{
98+
_captureBuffer.Write(data, (uint)channels, (uint)sampleRate);
10399
while (true)
104100
{
105-
Thread.Sleep(Constants.TASK_DELAY);
106101
var frame = _captureBuffer.ReadDuration(AudioProcessingModule.FRAME_DURATION_MS);
107-
if (_muted || frame == null) continue;
102+
if (_muted || frame == null) break;
108103

109-
if (_apm != null)
110-
_apm.ProcessStream(frame);
104+
if (_apm != null) _apm.ProcessStream(frame);
111105
Capture(frame);
112106
}
113-
}
114-
115-
private void OnAudioRead(float[] data, int channels, int sampleRate)
116-
{
117-
_captureBuffer.Write(data, (uint)channels, (uint)sampleRate);
107+
// Don't play the audio locally, to avoid echo.
118108
if (_sourceType == RtcAudioSourceType.AudioSourceMicrophone)
119-
{
120-
// Don't play the audio locally, to avoid echo.
121109
Array.Clear(data, 0, data.Length);
122-
}
123110
}
124111

125112
private void Capture(AudioFrame frame)

0 commit comments

Comments
 (0)