这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
db871de
Fixed building issue.
ashkan-saeedi-mazdeh Dec 15, 2023
561282f
Fixed a memory leak
ashkan-saeedi-mazdeh Dec 16, 2023
7320b31
Fixed a compile error on the distructor
ashkan-saeedi-mazdeh Dec 16, 2023
80d3dcd
Fixed the last error regarding the leak fix
ashkan-saeedi-mazdeh Dec 16, 2023
bb762d2
Updated dll
ashkan-saeedi-mazdeh Dec 23, 2023
c8e3906
updated protobuf messages to latest to be able to publish data.
ashkan-saeedi-mazdeh Dec 23, 2023
2cf59c3
Updated FFIClient with the correct FfiRequest class name based on upd…
ashkan-saeedi-mazdeh Dec 24, 2023
496618c
Added metas.
ashkan-saeedi-mazdeh Dec 27, 2023
830ebd1
modification trying to fix compiler errors
cdga777 Dec 27, 2023
eca1cb5
new initialize call
markgrossnickle Dec 27, 2023
6456edc
Test sending data
markgrossnickle Dec 28, 2023
511ef58
Hack for now
markgrossnickle Dec 28, 2023
72b2a83
Merge pull request #7 from markgrossnickle/fixcompilererror-cdga777
cdga777 Dec 28, 2023
892407a
to prevent null reference
cdga777 Dec 28, 2023
54bcdfa
Merge pull request #6 from decentraland/fixcompilererror-cdga777
markgrossnickle Dec 28, 2023
a5a63c4
minor cleanup
markgrossnickle Dec 28, 2023
9dc19d6
disconnect support
cdga777 Jan 2, 2024
cf33a02
Merge branch 'development' of https://github.com/decentraland/client-…
cdga777 Jan 2, 2024
c74fd99
Fix name
cdga777 Jan 2, 2024
760a13e
To handle better connection and disconnection from room
cdga777 Jan 3, 2024
298044c
change to avoid using of Coroutines, using async methods instead
cdga777 Jan 5, 2024
99289d0
async OnAudioRead
cdga777 Jan 10, 2024
ccf44b0
Cleaning logs
cdga777 Jan 11, 2024
5a9e634
Cancel tokens
cdga777 Jan 12, 2024
72cc9c6
Cancel token
cdga777 Jan 14, 2024
1042ec8
remove Task.Run, since usually SendRequest is called from other task
cdga777 Jan 14, 2024
86b851a
getting rid of use YieldInstruction
cdga777 Jan 14, 2024
204864a
adding audio filter in main thread
cdga777 Jan 15, 2024
42256a1
passing CancellationToken instead of CancellationTokenSource
cdga777 Jan 16, 2024
750eb28
added check for cancellation at beginning
cdga777 Jan 16, 2024
5d871b6
Cleaning handles
cdga777 Jan 16, 2024
1f4e49e
fix await
markgrossnickle Jan 17, 2024
709547c
remove cruft
markgrossnickle Jan 17, 2024
5520c6f
Cancellation token cleanup
markgrossnickle Jan 17, 2024
2397f23
Factory async method
markgrossnickle Jan 17, 2024
72432cb
more async
markgrossnickle Jan 17, 2024
d94c536
wip
markgrossnickle Jan 17, 2024
c2b42ca
Compile pass
markgrossnickle Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\C:\\Users\\ashka\\Source\\Repos\\client-sdk-unity",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/client-sdk-unity/v17/.wsuo
Binary file not shown.
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Runtime/Plugins/x86_64/livekit_ffi.dll
Git LFS file not shown
21 changes: 16 additions & 5 deletions Runtime/Scripts/AudioFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public class AudioFrame : IDisposable
{
private AudioFrameBufferInfo _info;

internal readonly FfiHandle Handle;
//internal readonly FfiHandle Handle;
private FfiHandle _handle;
internal FfiHandle Handle
{
get { return _handle; }
}
private bool _disposed = false;

public uint NumChannels => _info.NumChannels;
Expand All @@ -20,7 +25,7 @@ public class AudioFrame : IDisposable

internal AudioFrame(FfiHandle handle, AudioFrameBufferInfo info)
{
Handle = handle;
_handle = handle;
_info = info;
}

Expand All @@ -30,14 +35,20 @@ internal AudioFrame(int sampleRate, int numChannels, int samplesPerChannel) {
alloc.NumChannels = (uint) numChannels;
alloc.SamplesPerChannel = (uint) samplesPerChannel;

var request = new FFIRequest();
var request = new FfiRequest();
request.AllocAudioBuffer = alloc;

Init(request);
}

void Init(FfiRequest request)
{
var res = FfiClient.SendRequest(request);
var bufferInfo = res.AllocAudioBuffer.Buffer;
var bufferInfo = res.AllocAudioBuffer.Buffer.Info;

Handle = new FfiHandle((IntPtr)bufferInfo.Handle.Id);
_handle = new FfiHandle((IntPtr)res.AllocAudioBuffer.Buffer.Handle.Id);
_info = bufferInfo;

}

~AudioFrame()
Expand Down
123 changes: 86 additions & 37 deletions Runtime/Scripts/AudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using UnityEngine.Rendering;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using System.Threading.Tasks;
using System.Threading;

namespace LiveKit
{
Expand All @@ -14,74 +16,121 @@ public class RtcAudioSource
private AudioSource _audioSource;
private AudioFilter _audioFilter;

internal readonly FfiHandle Handle;
//internal readonly FfiHandle Handle;
private FfiHandle _handle;
internal FfiHandle Handle
{
get { return _handle; }
}
protected AudioSourceInfo _info;

// Used on the AudioThread
private AudioFrame _frame;
private Thread _readAudioThread;
private bool _pending = false;
private object _lock = new object();

public RtcAudioSource(AudioSource source)
{
var newAudioSource = new NewAudioSourceRequest();
newAudioSource.Type = AudioSourceType.AudioSourceNative;

var request = new FFIRequest();
var request = new FfiRequest();
request.NewAudioSource = newAudioSource;

var resp = FfiClient.SendRequest(request);
_info = resp.NewAudioSource.Source;
Handle = new FfiHandle((IntPtr)_info.Handle.Id);

_info = resp.NewAudioSource.Source.Info;
_handle = new FfiHandle((IntPtr)resp.NewAudioSource.Source.Handle.Id);
UpdateSource(source);

}

private void UpdateSource(AudioSource source)
public void Start()
{
_audioSource = source;
_audioFilter = source.gameObject.AddComponent<AudioFilter>();
//_audioFilter.hideFlags = HideFlags.HideInInspector;
_audioFilter.AudioRead += OnAudioRead;
source.Play();
Stop();
_readAudioThread = new Thread(async () => await Update());
_readAudioThread.Start();
}

public void Stop()
{
if (_readAudioThread != null) _readAudioThread.Abort();
}

private void OnAudioRead(float[] data, int channels, int sampleRate)
async private Task Update()
{
var samplesPerChannel = data.Length / channels;
if (_frame == null || _frame.NumChannels != channels
|| _frame.SampleRate != sampleRate
|| _frame.SamplesPerChannel != samplesPerChannel)
while(true)
{
_frame = new AudioFrame(sampleRate, channels, samplesPerChannel);
}

static short FloatToS16(float v) {
v *= 32768f;
v = Math.Min(v, 32767f);
v = Math.Max(v, -32768f);
return (short)(v + Math.Sign(v) * 0.5f);
await Task.Delay(Constants.TASK_DELAY);
if(_pending)
{
ReadAudio();
}
}
}

unsafe {
var frameData = new Span<short>(_frame.Data.ToPointer(), _frame.Length / sizeof(short));
for (int i = 0; i < data.Length; i++)
private void ReadAudio()
{
_pending = false;
lock (_lock)
{
var samplesPerChannel = _data.Length / _channels;
if (_frame == null || _frame.NumChannels != _channels
|| _frame.SampleRate != _sampleRate
|| _frame.SamplesPerChannel != samplesPerChannel)
{
frameData[i] = FloatToS16(data[i]);
_frame = new AudioFrame(_sampleRate, _channels, samplesPerChannel);
}
}

// Don't play the audio locally
Array.Clear(data, 0, data.Length);
try
{
// Don't play the audio locally
Array.Clear(_data, 0, _data.Length);

var pushFrame = new CaptureAudioFrameRequest();
pushFrame.SourceHandle = (ulong)Handle.DangerousGetHandle();
pushFrame.Buffer = new AudioFrameBufferInfo() { DataPtr = (ulong)_frame.Handle.DangerousGetHandle() };

var pushFrame = new CaptureAudioFrameRequest();
pushFrame.SourceHandle = new FFIHandleId { Id = (ulong)Handle.DangerousGetHandle() };
pushFrame.BufferHandle = new FFIHandleId { Id = (ulong)_frame.Handle.DangerousGetHandle() };
var request = new FfiRequest();
request.CaptureAudioFrame = pushFrame;

var request = new FFIRequest();
request.CaptureAudioFrame = pushFrame;
FfiClient.SendRequest(request);

FfiClient.SendRequest(request);
Utils.Debug($"Pushed audio frame with {_data.Length} samples");

Debug.Log($"Pushed audio frame with {data.Length} samples");
}
catch (Exception e)
{

Utils.Error("Audio Framedata error: " + e.Message);
}
}
}

private void UpdateSource(AudioSource source)
{
_audioSource = source;
_audioFilter = source.gameObject.AddComponent<AudioFilter>();
//_audioFilter.hideFlags = HideFlags.HideInInspector;
_audioFilter.AudioRead += OnAudioRead;
source.Play();
}

private float[] _data;
private int _channels;
private int _sampleRate;

private void OnAudioRead(float[] data, int channels, int sampleRate)
{
lock (_lock)
{
_data = data;
_channels = channels;
_sampleRate = sampleRate;
_pending = true;
}
}

}
}
Loading