|
2 | 2 | using System.Collections;
|
3 | 3 | using LiveKit.Proto;
|
4 | 4 | using LiveKit.Internal;
|
5 |
| -using System.Threading; |
6 | 5 | using LiveKit.Internal.FFIClients.Requests;
|
7 | 6 | using UnityEngine;
|
8 | 7 |
|
@@ -43,7 +42,6 @@ public abstract class RtcAudioSource : IRtcSource
|
43 | 42 | protected AudioSourceInfo _info;
|
44 | 43 |
|
45 | 44 | // Possibly used on the AudioThread
|
46 |
| - private Thread _readAudioThread; |
47 | 45 | private AudioBuffer _captureBuffer = new AudioBuffer();
|
48 | 46 | private readonly AudioProcessingModule _apm;
|
49 | 47 | private readonly ApmReverseStream _apmReverseStream;
|
@@ -84,42 +82,31 @@ public IEnumerator PrepareAndStart()
|
84 | 82 | public void Start()
|
85 | 83 | {
|
86 | 84 | Stop();
|
87 |
| - _readAudioThread = new Thread(Update); |
88 |
| - _readAudioThread.Start(); |
89 | 85 | _apmReverseStream?.Start();
|
90 | 86 | AudioRead += OnAudioRead;
|
91 | 87 | Play();
|
92 | 88 | }
|
93 | 89 |
|
94 | 90 | public virtual void Stop()
|
95 | 91 | {
|
96 |
| - _readAudioThread?.Abort(); |
97 | 92 | _apmReverseStream?.Stop();
|
98 | 93 | AudioRead -= OnAudioRead;
|
99 | 94 | }
|
100 | 95 |
|
101 |
| - private void Update() |
| 96 | + private void OnAudioRead(float[] data, int channels, int sampleRate) |
102 | 97 | {
|
| 98 | + _captureBuffer.Write(data, (uint)channels, (uint)sampleRate); |
103 | 99 | while (true)
|
104 | 100 | {
|
105 |
| - Thread.Sleep(Constants.TASK_DELAY); |
106 | 101 | var frame = _captureBuffer.ReadDuration(AudioProcessingModule.FRAME_DURATION_MS);
|
107 |
| - if (_muted || frame == null) continue; |
| 102 | + if (_muted || frame == null) break; |
108 | 103 |
|
109 |
| - if (_apm != null) |
110 |
| - _apm.ProcessStream(frame); |
| 104 | + if (_apm != null) _apm.ProcessStream(frame); |
111 | 105 | Capture(frame);
|
112 | 106 | }
|
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. |
118 | 108 | if (_sourceType == RtcAudioSourceType.AudioSourceMicrophone)
|
119 |
| - { |
120 |
| - // Don't play the audio locally, to avoid echo. |
121 | 109 | Array.Clear(data, 0, data.Length);
|
122 |
| - } |
123 | 110 | }
|
124 | 111 |
|
125 | 112 | private void Capture(AudioFrame frame)
|
|
0 commit comments