com.adrenak.univoice 4.6.0 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -16
- package/Runtime/Adrenak.UniVoice.Runtime.asmdef +2 -1
- package/Runtime/Impl/Networks/FakeNetwork.cs +6 -0
- package/Runtime/Impl/Networks/FakeNetwork.cs.meta +11 -0
- package/Runtime/Impl/Networks/FishNet/FishNetBroadcast.cs +22 -0
- package/Runtime/Impl/Networks/FishNet/FishNetBroadcast.cs.meta +2 -0
- package/Runtime/Impl/Networks/FishNet/FishNetBroadcastTags.cs +15 -0
- package/Runtime/Impl/Networks/FishNet/FishNetBroadcastTags.cs.meta +2 -0
- package/Runtime/Impl/Networks/FishNet/FishNetClient.cs +197 -0
- package/Runtime/Impl/Networks/FishNet/FishNetClient.cs.meta +2 -0
- package/Runtime/Impl/Networks/FishNet/FishNetServer.cs +236 -0
- package/Runtime/Impl/Networks/FishNet/FishNetServer.cs.meta +2 -0
- package/Runtime/Impl/Networks/FishNet.meta +3 -0
- package/Runtime/Impl/Networks/Mirror/MirrorClient.cs +5 -1
- package/Runtime/Impl/Networks/Mirror/MirrorMessage.cs +1 -1
- package/Runtime/Impl/Networks/Mirror/MirrorMessageTags.cs +1 -1
- package/Runtime/Impl/Networks/Mirror/MirrorModeObserver.cs +1 -1
- package/Runtime/Impl/Networks/Mirror/MirrorServer.cs +48 -17
- package/Runtime/Types/VoiceSettings.cs +20 -0
- package/Samples~/Basic Setup Scripts/FishNet-SinglePrefabObjects.asset +15 -0
- package/Samples~/Basic Setup Scripts/FishNet-SinglePrefabObjects.asset.meta +8 -0
- package/Samples~/Basic Setup Scripts/UniVoiceFishNetSetupSample.cs +199 -0
- package/Samples~/Basic Setup Scripts/UniVoiceFishNetSetupSample.cs.meta +11 -0
- package/Samples~/Basic Setup Scripts/UniVoiceFishNetSetupSample.unity +335 -0
- package/Samples~/Basic Setup Scripts/UniVoiceFishNetSetupSample.unity.meta +7 -0
- package/Samples~/Basic Setup Scripts/UniVoiceMirrorSetupSample.cs +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
# UniVoice
|
|
2
2
|
UniVoice is a voice chat/VoIP solution for Unity.
|
|
3
3
|
|
|
4
|
-
Some features of UniVoice:
|
|
5
|
-
- 👥 Group voice chat. Multiple peers can join a chatroom and exchange audio.
|
|
6
|
-
|
|
7
|
-
- ⚙ Peer specific settings. Don't want to listen to a peer? Mute them. Don't want someone listening to you? Deafen them.
|
|
8
|
-
|
|
4
|
+
Some features of UniVoice:
|
|
9
5
|
- 🎨 Customize your audio input, output and networking layers.
|
|
10
6
|
* 🌐 __Configurable Network__:
|
|
11
7
|
- UniVoice is networking agnostic. Implement the `IAudioClient` and `IAudioServer` interfaces using the networking plugin of your choice to have it send audio data over any networking solution.
|
|
12
8
|
- Built-in support for:
|
|
13
|
-
- Mirror networking
|
|
9
|
+
- [Mirror networking](https://mirror-networking.com/)
|
|
10
|
+
- [Fish Networking](https://fish-networking.gitbook.io/docs)
|
|
14
11
|
|
|
15
12
|
* 🎤 __Configurable Audio Input__:
|
|
16
13
|
- UniVoice is audio input agnostic. You can change the source of outgoing audio by implementing the `IAudioInput` interface.
|
|
@@ -28,6 +25,22 @@ Some features of UniVoice:
|
|
|
28
25
|
- Opus (Concentus) encoding & decoding.
|
|
29
26
|
- RNNoise based noise removal.
|
|
30
27
|
- Gaussian blurring for minor denoising.
|
|
28
|
+
|
|
29
|
+
- 👥 Easy integration with your existing networking solution
|
|
30
|
+
- Whether you're using Mirror or FishNet, UniVoice runs in the background in sync with your networking lifecycle
|
|
31
|
+
- A basic integration involves just initializing it on start.
|
|
32
|
+
- For advanced usage like teams, chatrooms, lobbies, you can use the UniVoice API to create runtime behaviour.
|
|
33
|
+
|
|
34
|
+
- ⚙ Fine control over audio data flow.
|
|
35
|
+
* Don't want to listen to a peer? Mute them. Don't want someone listening to you? Deafen them.
|
|
36
|
+
* Group players using tags and control audio flow between them. For example:
|
|
37
|
+
- "red", "blue" and "spectator" tags for two teams playing against each other.
|
|
38
|
+
- Red and Blue teams can only hear each other
|
|
39
|
+
- Spectators can hear everyone
|
|
40
|
+
- clients with "contestant", "judge" and "audience" tags for a virtual talent show.
|
|
41
|
+
- Contestant can be heard by everyone, but don't hear anyone else (for focus)
|
|
42
|
+
- Judges can talk to and hear each other for discussions. They can hear the contestant. But not the audience (for less noise)
|
|
43
|
+
- Audience can hear and talk to each other. They can hear the performer. But they cannot hear the judges.
|
|
31
44
|
|
|
32
45
|
## Installation
|
|
33
46
|
⚠️ [OpenUPM](https://openupm.com/packages/com.adrenak.univoice/?subPage=versions) may not have up to date releases. Install using NPM registry instead 👇
|
|
@@ -53,19 +66,18 @@ Then add `com.adrenak.univoice:x.y.z` to the `dependencies` in your `manifest.js
|
|
|
53
66
|
## Useful links
|
|
54
67
|
* API reference is available here: http://www.vatsalambastha.com/univoice
|
|
55
68
|
* UniVoice blog: https://blog.vatsalambastha.com/search/label/univoice
|
|
56
|
-
* Discord server: https://discord.gg/
|
|
69
|
+
* Discord server: https://discord.gg/NGvkEVbdjQ
|
|
57
70
|
|
|
58
71
|
## Integration
|
|
59
72
|
UniVoice isn't currently very drag-and-drop/low-code. The best way to integrate is to have some code perform a one time setup when your app starts and provides access to relevant objects that you can use throughout the rest of the apps runtime.
|
|
60
73
|
|
|
61
|
-
An example of this is the `UniVoiceMirrorSetupSample.cs` file that gives you access to an AudioServer that you can use in your server code and a ClientSession that you can use in your client code. For more see the "Samples" section below.
|
|
62
|
-
|
|
63
74
|
## Samples
|
|
64
75
|
This repository contains two samples:
|
|
65
|
-
* `UniVoiceMirrorSetupSample.cs` is a drag and drop component, a simple integration sample script. You can add it to your Mirror NetworkManager to get voice chat to work. No code required, it's as simple as that! It'll work as long as you have setup your project properly. For more instructions see the top of the `UniVoiceMirrorSetupSample.cs` file.
|
|
66
|
-
*
|
|
76
|
+
* `UniVoiceMirrorSetupSample.cs` is a drag and drop component, a simple integration sample script. You can add it to your Mirror NetworkManager to get voice chat to work. No code required, it's as simple as that! It'll work as long as you have setup your project properly. For more instructions see the top of the `UniVoiceMirrorSetupSample.cs` file.
|
|
77
|
+
* `UniVoiceFishNetSetypSample.cs` is also very similar. Just drag and drop and it should work!
|
|
78
|
+
* A sample scene that shows the other clients in a UI as well as allows you to mute yourself/them. This sample is Mirror based.
|
|
67
79
|
|
|
68
|
-
> UniVoice currently only supports Mirror out of the box.
|
|
80
|
+
> UniVoice currently only supports Mirror and FishNetworking out of the box. Follow the instructions in the "Activating non-packaged dependencies" section below before trying it out the samples.
|
|
69
81
|
|
|
70
82
|
## Dependencies
|
|
71
83
|
[com.adrenak.brw](https://www.github.com/adrenak/brw) for reading and writing messages for communication. See `MirrorServer.cs` and `MirrorClient.cs` where they're used.
|
|
@@ -81,19 +93,28 @@ UniVoice includes and installs the dependencies mentioned above along with itsel
|
|
|
81
93
|
* Mic audio capture input (via UniMic)
|
|
82
94
|
* AudioSource based playback output (via UniMic)
|
|
83
95
|
|
|
84
|
-
|
|
85
|
-
* Mirror network:
|
|
86
|
-
* To enable, ensure the Mirror package is in your project and add `UNIVOICE_NETWORK_MIRROR` to activate it
|
|
96
|
+
UniVoice has code that uses dependencies that you have to install and sometimes enable via compilation symbols as they are _not_ UniVoice dependencies and _don't_ get installed along with UniVoice. This is because they are either third party modules or based on native libraries (not plain C#) that can pose build issues.
|
|
87
97
|
* RNNoise Noise removal filter:
|
|
88
98
|
* To enable, ensure the [RNNoise4Unity](https://github.com/adrenak/RNNoise4Unity) package is in your project and add `UNIVOICE_FILTER_RNNOISE4UNITY` to activate it
|
|
99
|
+
* Mirror network:
|
|
100
|
+
* Just add the Mirror package to your project. UniVoice will detect it.
|
|
101
|
+
* Fish Networking:
|
|
102
|
+
* Just install FishNet package in your project. UniVoice will detect it.
|
|
89
103
|
|
|
90
104
|
## License and Support
|
|
91
105
|
This project is under the [MIT license](https://github.com/adrenak/univoice/blob/master/LICENSE).
|
|
92
106
|
|
|
93
107
|
Community contributions are welcome.
|
|
108
|
+
|
|
109
|
+
Commercial engagements with the author can be arranged, subject to schedule and availability.
|
|
110
|
+
|
|
111
|
+
## Acknowledgements and contributors
|
|
112
|
+
* [@metater](https://github.com/Metater/) for helping make improvements to audio streaming quality. [A related blog post](https://blog.vatsalambastha.com/2025/07/unimic-330-many-streamedaudiosource.html)
|
|
113
|
+
* [@FrantisekHolubec](https://github.com/FrantisekHolubec) for [FishNet support code](https://github.com/adrenak/univoice/commit/fdc3424180d8991c92b3e092b3edb50b6110c863). Here's a [related blog post](https://blog.vatsalambastha.com/2025/09/univoice-480-fishnet-support.html)
|
|
114
|
+
* [Masaryk University](https://www.muni.cz/en) for using UniVoice in their projects and providing helpful feedback
|
|
94
115
|
|
|
95
116
|
## Contact
|
|
96
|
-
The
|
|
117
|
+
The author can be reached at the following links:
|
|
97
118
|
|
|
98
119
|
[Website](http://www.vatsalambastha.com)
|
|
99
120
|
[LinkedIn](https://www.linkedin.com/in/vatsalAmbastha)
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
"GUID:f87ecb857e752164ab814a3de8eb0262",
|
|
7
7
|
"GUID:b118fd5a40c85ad4e9b38e8c4a42bbb1",
|
|
8
8
|
"GUID:4653938bfdb5cf8409322ce17219d5f7",
|
|
9
|
-
"GUID:30817c1a0e6d646d99c048fc403f5979"
|
|
9
|
+
"GUID:30817c1a0e6d646d99c048fc403f5979",
|
|
10
|
+
"GUID:7c88a4a7926ee5145ad2dfa06f454c67"
|
|
10
11
|
],
|
|
11
12
|
"includePlatforms": [],
|
|
12
13
|
"excludePlatforms": [],
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#if FISHNET
|
|
2
|
+
using System;
|
|
3
|
+
using FishNet.Broadcast;
|
|
4
|
+
|
|
5
|
+
namespace Adrenak.UniVoice.Networks
|
|
6
|
+
{
|
|
7
|
+
/// <summary>
|
|
8
|
+
/// The messages exchanged between the server and client.
|
|
9
|
+
/// To see how the Mirror implementation of UniVoice uses this struct
|
|
10
|
+
/// find the references to the <see cref="data"/> object in the project.
|
|
11
|
+
/// The gist is, it uses BRW (https://www.github.com/adrenak/brw) to
|
|
12
|
+
/// write and read data. The data always starts with a tag. All the tags
|
|
13
|
+
/// used for this UniVoice FishNet implementation are available in
|
|
14
|
+
/// <see cref="FishNetBroadcastTags"/>
|
|
15
|
+
/// </summary>
|
|
16
|
+
[Serializable]
|
|
17
|
+
public struct FishNetBroadcast : IBroadcast
|
|
18
|
+
{
|
|
19
|
+
public byte[] data;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
#endif
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#if FISHNET
|
|
2
|
+
namespace Adrenak.UniVoice.Networks
|
|
3
|
+
{
|
|
4
|
+
/// <summary>
|
|
5
|
+
/// The different types of messages we send over FishNet
|
|
6
|
+
/// to implement the <see cref="IAudioClient{T}"/> and <see cref="IAudioServer{T}"/>
|
|
7
|
+
/// interfaces for FishNet
|
|
8
|
+
/// </summary>
|
|
9
|
+
public class FishNetBroadcastTags
|
|
10
|
+
{
|
|
11
|
+
public const string AUDIO_FRAME = "AUDIO_FRAME";
|
|
12
|
+
public const string VOICE_SETTINGS = "VOICE_SETTINGS";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
#endif
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
#if FISHNET
|
|
2
|
+
using System;
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using Adrenak.BRW;
|
|
6
|
+
using FishNet;
|
|
7
|
+
using FishNet.Managing;
|
|
8
|
+
using FishNet.Transporting;
|
|
9
|
+
using UnityEngine;
|
|
10
|
+
|
|
11
|
+
namespace Adrenak.UniVoice.Networks
|
|
12
|
+
{
|
|
13
|
+
/// <summary>
|
|
14
|
+
/// This is the implementation of <see cref="IAudioClient{T}"/> interface for FishNet.
|
|
15
|
+
/// It uses the FishNet to send and receive UniVoice data to the server.
|
|
16
|
+
/// </summary>
|
|
17
|
+
public class FishNetClient : IAudioClient<int>
|
|
18
|
+
{
|
|
19
|
+
private const string TAG = "[FishNetClient]";
|
|
20
|
+
public int ID { get; private set; } = -1;
|
|
21
|
+
|
|
22
|
+
public List<int> PeerIDs { get; private set; }
|
|
23
|
+
public VoiceSettings YourVoiceSettings { get; private set; }
|
|
24
|
+
|
|
25
|
+
public event Action<int, List<int>> OnJoined;
|
|
26
|
+
public event Action OnLeft;
|
|
27
|
+
public event Action<int> OnPeerJoined;
|
|
28
|
+
public event Action<int> OnPeerLeft;
|
|
29
|
+
public event Action<int, AudioFrame> OnReceivedPeerAudioFrame;
|
|
30
|
+
|
|
31
|
+
private NetworkManager _networkManager;
|
|
32
|
+
|
|
33
|
+
public FishNetClient()
|
|
34
|
+
{
|
|
35
|
+
PeerIDs = new List<int>();
|
|
36
|
+
YourVoiceSettings = new VoiceSettings();
|
|
37
|
+
|
|
38
|
+
_networkManager = InstanceFinder.NetworkManager;
|
|
39
|
+
_networkManager.ClientManager.OnClientConnectionState += OnClientConnectionStateChanged;
|
|
40
|
+
_networkManager.ClientManager.OnAuthenticated += OnClientAuthenticated;
|
|
41
|
+
_networkManager.ClientManager.OnRemoteConnectionState += OnRemoteConnectionStateChanged;
|
|
42
|
+
_networkManager.ClientManager.RegisterBroadcast<FishNetBroadcast>(OnReceivedMessage);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public void Dispose()
|
|
46
|
+
{
|
|
47
|
+
if (_networkManager)
|
|
48
|
+
{
|
|
49
|
+
_networkManager.ClientManager.OnClientConnectionState -= OnClientConnectionStateChanged;
|
|
50
|
+
_networkManager.ClientManager.OnAuthenticated -= OnClientAuthenticated;
|
|
51
|
+
_networkManager.ClientManager.OnRemoteConnectionState -= OnRemoteConnectionStateChanged;
|
|
52
|
+
_networkManager.ClientManager.UnregisterBroadcast<FishNetBroadcast>(OnReceivedMessage);
|
|
53
|
+
}
|
|
54
|
+
PeerIDs.Clear();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private void OnRemoteConnectionStateChanged(RemoteConnectionStateArgs args)
|
|
58
|
+
{
|
|
59
|
+
// Don't process connection state changes before the client is authenticated
|
|
60
|
+
if (_networkManager.ClientManager.Connection.ClientId < 0)
|
|
61
|
+
return;
|
|
62
|
+
|
|
63
|
+
if (args.ConnectionState == RemoteConnectionState.Started)
|
|
64
|
+
{
|
|
65
|
+
var newPeerID = args.ConnectionId;
|
|
66
|
+
if (!PeerIDs.Contains(newPeerID))
|
|
67
|
+
{
|
|
68
|
+
PeerIDs.Add(newPeerID);
|
|
69
|
+
Debug.unityLogger.Log(LogType.Log, TAG,
|
|
70
|
+
$"Peer {newPeerID} joined. Peer list is now {string.Join(", ", PeerIDs)}");
|
|
71
|
+
OnPeerJoined?.Invoke(newPeerID);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (args.ConnectionState == RemoteConnectionState.Stopped)
|
|
75
|
+
{
|
|
76
|
+
var leftPeerID = args.ConnectionId;
|
|
77
|
+
if (PeerIDs.Contains(leftPeerID))
|
|
78
|
+
{
|
|
79
|
+
PeerIDs.Remove(leftPeerID);
|
|
80
|
+
var log2 = $"Peer {leftPeerID} left. ";
|
|
81
|
+
if (PeerIDs.Count == 0)
|
|
82
|
+
log2 += "There are no peers anymore.";
|
|
83
|
+
else
|
|
84
|
+
log2 += $"Peer list is now {string.Join(", ", PeerIDs)}";
|
|
85
|
+
|
|
86
|
+
Debug.unityLogger.Log(LogType.Log, TAG, log2);
|
|
87
|
+
OnPeerLeft?.Invoke(leftPeerID);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private void OnClientAuthenticated()
|
|
93
|
+
{
|
|
94
|
+
// We need to use OnClientAuthenticated to ensure the client does have ClientId set
|
|
95
|
+
ID = _networkManager.ClientManager.Connection.ClientId;
|
|
96
|
+
PeerIDs = _networkManager.ClientManager.Clients.Keys.Where(x => x != ID).ToList();
|
|
97
|
+
|
|
98
|
+
var log = $"Initialized with ID {ID}. ";
|
|
99
|
+
if (PeerIDs.Count > 0)
|
|
100
|
+
log += $"Peer list: {string.Join(", ", PeerIDs)}";
|
|
101
|
+
else
|
|
102
|
+
log += "There are currently no peers.";
|
|
103
|
+
Debug.unityLogger.Log(LogType.Log, TAG, log);
|
|
104
|
+
|
|
105
|
+
OnJoined?.Invoke(ID, PeerIDs);
|
|
106
|
+
foreach (var peerId in PeerIDs)
|
|
107
|
+
OnPeerJoined?.Invoke(peerId);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private void OnClientConnectionStateChanged(ClientConnectionStateArgs args)
|
|
111
|
+
{
|
|
112
|
+
// We check only for the stopped state here, as the started state is handled in OnClientAuthenticated
|
|
113
|
+
if (args.ConnectionState == LocalConnectionState.Stopped)
|
|
114
|
+
{
|
|
115
|
+
YourVoiceSettings = new VoiceSettings();
|
|
116
|
+
var oldPeerIds = PeerIDs.ToList();
|
|
117
|
+
PeerIDs.Clear();
|
|
118
|
+
ID = -1;
|
|
119
|
+
foreach (var peerId in oldPeerIds)
|
|
120
|
+
OnPeerLeft?.Invoke(peerId);
|
|
121
|
+
OnLeft?.Invoke();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private void OnReceivedMessage(FishNetBroadcast msg, Channel channel)
|
|
126
|
+
{
|
|
127
|
+
var reader = new BytesReader(msg.data);
|
|
128
|
+
var tag = reader.ReadString();
|
|
129
|
+
switch (tag)
|
|
130
|
+
{
|
|
131
|
+
// When the server sends audio from a peer meant for this client
|
|
132
|
+
case FishNetBroadcastTags.AUDIO_FRAME:
|
|
133
|
+
var sender = reader.ReadInt();
|
|
134
|
+
if (sender == ID || !PeerIDs.Contains(sender))
|
|
135
|
+
return;
|
|
136
|
+
var frame = new AudioFrame
|
|
137
|
+
{
|
|
138
|
+
timestamp = reader.ReadLong(),
|
|
139
|
+
frequency = reader.ReadInt(),
|
|
140
|
+
channelCount = reader.ReadInt(),
|
|
141
|
+
samples = reader.ReadByteArray()
|
|
142
|
+
};
|
|
143
|
+
OnReceivedPeerAudioFrame?.Invoke(sender, frame);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// <summary>
|
|
149
|
+
/// Sends an audio frame captured on this client to the server
|
|
150
|
+
/// </summary>
|
|
151
|
+
/// <param name="frame"></param>
|
|
152
|
+
public void SendAudioFrame(AudioFrame frame)
|
|
153
|
+
{
|
|
154
|
+
if (ID == -1)
|
|
155
|
+
return;
|
|
156
|
+
var writer = new BytesWriter();
|
|
157
|
+
writer.WriteString(FishNetBroadcastTags.AUDIO_FRAME);
|
|
158
|
+
writer.WriteInt(ID);
|
|
159
|
+
writer.WriteLong(frame.timestamp);
|
|
160
|
+
writer.WriteInt(frame.frequency);
|
|
161
|
+
writer.WriteInt(frame.channelCount);
|
|
162
|
+
writer.WriteByteArray(frame.samples);
|
|
163
|
+
|
|
164
|
+
var message = new FishNetBroadcast
|
|
165
|
+
{
|
|
166
|
+
data = writer.Bytes
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
if (_networkManager.ClientManager.Started)
|
|
170
|
+
_networkManager.ClientManager.Broadcast(message, Channel.Unreliable);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// <summary>
|
|
174
|
+
/// Updates the server with the voice settings of this client
|
|
175
|
+
/// </summary>
|
|
176
|
+
public void SubmitVoiceSettings()
|
|
177
|
+
{
|
|
178
|
+
if (ID == -1)
|
|
179
|
+
return;
|
|
180
|
+
var writer = new BytesWriter();
|
|
181
|
+
writer.WriteString(FishNetBroadcastTags.VOICE_SETTINGS);
|
|
182
|
+
writer.WriteInt(YourVoiceSettings.muteAll ? 1 : 0);
|
|
183
|
+
writer.WriteIntArray(YourVoiceSettings.mutedPeers.ToArray());
|
|
184
|
+
writer.WriteInt(YourVoiceSettings.deafenAll ? 1 : 0);
|
|
185
|
+
writer.WriteIntArray(YourVoiceSettings.deafenedPeers.ToArray());
|
|
186
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.myTags));
|
|
187
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.mutedTags));
|
|
188
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.deafenedTags));
|
|
189
|
+
|
|
190
|
+
var message = new FishNetBroadcast() {
|
|
191
|
+
data = writer.Bytes
|
|
192
|
+
};
|
|
193
|
+
_networkManager.ClientManager.Broadcast(message);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
#endif
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#if FISHNET
|
|
2
|
+
using System;
|
|
3
|
+
using System.Linq;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
using Adrenak.BRW;
|
|
7
|
+
using FishNet;
|
|
8
|
+
using FishNet.Connection;
|
|
9
|
+
using FishNet.Managing;
|
|
10
|
+
using FishNet.Transporting;
|
|
11
|
+
|
|
12
|
+
namespace Adrenak.UniVoice.Networks
|
|
13
|
+
{
|
|
14
|
+
/// <summary>
|
|
15
|
+
/// This is an implementation of the <see cref="IAudioServer{T}"/> interface for FishNet.
|
|
16
|
+
/// It uses the FishNet to send and receive UniVoice audio data to and from clients.
|
|
17
|
+
/// </summary>
|
|
18
|
+
public class FishNetServer : IAudioServer<int>
|
|
19
|
+
{
|
|
20
|
+
private const string TAG = "[FishNetServer]";
|
|
21
|
+
|
|
22
|
+
public event Action OnServerStart;
|
|
23
|
+
public event Action OnServerStop;
|
|
24
|
+
public event Action OnClientVoiceSettingsUpdated;
|
|
25
|
+
|
|
26
|
+
public List<int> ClientIDs { get; private set; }
|
|
27
|
+
public Dictionary<int, VoiceSettings> ClientVoiceSettings { get; private set; }
|
|
28
|
+
|
|
29
|
+
private NetworkManager _networkManager;
|
|
30
|
+
private List<int> _startedTransports = new();
|
|
31
|
+
|
|
32
|
+
public FishNetServer()
|
|
33
|
+
{
|
|
34
|
+
ClientIDs = new List<int>();
|
|
35
|
+
ClientVoiceSettings = new Dictionary<int, VoiceSettings>();
|
|
36
|
+
|
|
37
|
+
_networkManager = InstanceFinder.NetworkManager;
|
|
38
|
+
_networkManager.ServerManager.OnServerConnectionState += OnServerConnectionStateChanged;
|
|
39
|
+
_networkManager.ServerManager.OnRemoteConnectionState += OnServerRemoteConnectionStateChanged;
|
|
40
|
+
_networkManager.ClientManager.OnClientConnectionState += OnClientConnectionStateChanged;
|
|
41
|
+
_networkManager.ServerManager.RegisterBroadcast<FishNetBroadcast>(OnReceivedMessage, false);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public void Dispose()
|
|
45
|
+
{
|
|
46
|
+
if (_networkManager)
|
|
47
|
+
{
|
|
48
|
+
_networkManager.ServerManager.OnServerConnectionState -= OnServerConnectionStateChanged;
|
|
49
|
+
_networkManager.ServerManager.OnRemoteConnectionState -= OnServerRemoteConnectionStateChanged;
|
|
50
|
+
_networkManager.ClientManager.OnClientConnectionState -= OnClientConnectionStateChanged;
|
|
51
|
+
_networkManager.ServerManager.UnregisterBroadcast<FishNetBroadcast>(OnReceivedMessage);
|
|
52
|
+
}
|
|
53
|
+
OnServerShutdown();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private void OnServerStarted()
|
|
57
|
+
{
|
|
58
|
+
OnServerStart?.Invoke();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private void OnServerShutdown()
|
|
62
|
+
{
|
|
63
|
+
ClientIDs.Clear();
|
|
64
|
+
ClientVoiceSettings.Clear();
|
|
65
|
+
OnServerStop?.Invoke();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private void OnServerRemoteConnectionStateChanged(NetworkConnection connection, RemoteConnectionStateArgs args)
|
|
69
|
+
{
|
|
70
|
+
if (args.ConnectionState == RemoteConnectionState.Started)
|
|
71
|
+
{
|
|
72
|
+
OnServerConnected(connection.ClientId);
|
|
73
|
+
}
|
|
74
|
+
else if (args.ConnectionState == RemoteConnectionState.Stopped)
|
|
75
|
+
{
|
|
76
|
+
OnServerDisconnected(connection.ClientId);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private void OnServerConnectionStateChanged(ServerConnectionStateArgs args)
|
|
81
|
+
{
|
|
82
|
+
// Connection can change for each transport, so we need to track them
|
|
83
|
+
if (args.ConnectionState == LocalConnectionState.Started)
|
|
84
|
+
{
|
|
85
|
+
var wasStarted = _startedTransports.Count != 0;
|
|
86
|
+
_startedTransports.Add(args.TransportIndex);
|
|
87
|
+
if (!wasStarted)
|
|
88
|
+
OnServerStarted();
|
|
89
|
+
}
|
|
90
|
+
else if (args.ConnectionState == LocalConnectionState.Stopped)
|
|
91
|
+
{
|
|
92
|
+
_startedTransports.Remove(args.TransportIndex);
|
|
93
|
+
if(_startedTransports.Count == 0)
|
|
94
|
+
OnServerShutdown();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private void OnClientConnectionStateChanged(ClientConnectionStateArgs args)
|
|
99
|
+
{
|
|
100
|
+
// TODO - do we need to check if host or is this enough?
|
|
101
|
+
if (args.ConnectionState == LocalConnectionState.Started)
|
|
102
|
+
{
|
|
103
|
+
OnServerConnected(0);
|
|
104
|
+
}
|
|
105
|
+
else if (args.ConnectionState == LocalConnectionState.Stopped)
|
|
106
|
+
{
|
|
107
|
+
OnServerDisconnected(0);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private void OnReceivedMessage(NetworkConnection connection, FishNetBroadcast message, Channel channel)
|
|
112
|
+
{
|
|
113
|
+
var clientId = connection.ClientId;
|
|
114
|
+
var reader = new BytesReader(message.data);
|
|
115
|
+
var tag = reader.ReadString();
|
|
116
|
+
|
|
117
|
+
if (tag.Equals(FishNetBroadcastTags.AUDIO_FRAME))
|
|
118
|
+
{
|
|
119
|
+
// We start with all the peers except the one that's
|
|
120
|
+
// sent the audio
|
|
121
|
+
var peersToForwardAudioTo = ClientIDs
|
|
122
|
+
.Where(x => x != clientId);
|
|
123
|
+
|
|
124
|
+
// Check the voice settings of the sender and eliminate any peers the sender
|
|
125
|
+
// may have deafened
|
|
126
|
+
if (ClientVoiceSettings.TryGetValue(clientId, out var senderSettings))
|
|
127
|
+
{
|
|
128
|
+
// If the client sending the audio has deafened everyone,
|
|
129
|
+
// we simply return. Sender's audio should not be forwarded to anyone.
|
|
130
|
+
if (senderSettings.deafenAll)
|
|
131
|
+
return;
|
|
132
|
+
|
|
133
|
+
// Filter the recipient list by removing all peers that the sender has
|
|
134
|
+
// deafened using ID
|
|
135
|
+
peersToForwardAudioTo = peersToForwardAudioTo
|
|
136
|
+
.Where(x => !senderSettings.deafenedPeers.Contains(x));
|
|
137
|
+
|
|
138
|
+
// Further filter the recipient list by removing peers that the sender has
|
|
139
|
+
// deafened using tags
|
|
140
|
+
peersToForwardAudioTo = peersToForwardAudioTo.Where(peer =>
|
|
141
|
+
{
|
|
142
|
+
// Get the voice settings of the peer
|
|
143
|
+
if (ClientVoiceSettings.TryGetValue(peer, out var peerVoiceSettings))
|
|
144
|
+
{
|
|
145
|
+
// Check if sender has not deafened peer using tag
|
|
146
|
+
var hasDeafenedPeer = senderSettings.deafenedTags.Intersect(peerVoiceSettings.myTags).Any();
|
|
147
|
+
return !hasDeafenedPeer;
|
|
148
|
+
}
|
|
149
|
+
// If peer doesn't have voice settings, we can keep the peer in the list
|
|
150
|
+
return true;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// We iterate through each recipient peer that the sender wants to send audio to, checking if
|
|
155
|
+
// they have muted the sender, before forwarding the audio to them.
|
|
156
|
+
foreach (var recipient in peersToForwardAudioTo) {
|
|
157
|
+
// Get the settings of a potential recipient
|
|
158
|
+
if (ClientVoiceSettings.TryGetValue(recipient, out var recipientSettings)) {
|
|
159
|
+
// If a peer has muted everyone, don't send audio
|
|
160
|
+
if (recipientSettings.muteAll)
|
|
161
|
+
continue;
|
|
162
|
+
|
|
163
|
+
// If the peers has muted the sender using ID, skip sending audio
|
|
164
|
+
if (recipientSettings.mutedPeers.Contains(clientId))
|
|
165
|
+
continue;
|
|
166
|
+
|
|
167
|
+
// If the peer has muted the sender using tag, skip sending audio
|
|
168
|
+
if (recipientSettings.mutedTags.Intersect(senderSettings.myTags).Any())
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
SendToClient(recipient, message.data, Channel.Unreliable);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (tag.Equals(FishNetBroadcastTags.VOICE_SETTINGS)) {
|
|
175
|
+
//Debug.unityLogger.Log(LogType.Log, TAG, "FishNet server stopped");
|
|
176
|
+
// We create the VoiceSettings object by reading from the reader
|
|
177
|
+
// and update the peer voice settings map
|
|
178
|
+
var muteAll = reader.ReadInt() == 1;
|
|
179
|
+
var mutedPeers = reader.ReadIntArray().ToList();
|
|
180
|
+
var deafenAll = reader.ReadInt() == 1;
|
|
181
|
+
var deafenedPeers = reader.ReadIntArray().ToList();
|
|
182
|
+
var myTags = reader.ReadString().Split(",").ToList();
|
|
183
|
+
var mutedTags = reader.ReadString().Split(",").ToList();
|
|
184
|
+
var deafenedTags = reader.ReadString().Split(",").ToList();
|
|
185
|
+
|
|
186
|
+
var voiceSettings = new VoiceSettings {
|
|
187
|
+
muteAll = muteAll,
|
|
188
|
+
mutedPeers = mutedPeers,
|
|
189
|
+
deafenAll = deafenAll,
|
|
190
|
+
deafenedPeers = deafenedPeers,
|
|
191
|
+
myTags = myTags,
|
|
192
|
+
mutedTags = mutedTags,
|
|
193
|
+
deafenedTags = deafenedTags
|
|
194
|
+
};
|
|
195
|
+
ClientVoiceSettings[clientId] = voiceSettings;
|
|
196
|
+
OnClientVoiceSettingsUpdated?.Invoke();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private void OnServerConnected(int connId)
|
|
201
|
+
{
|
|
202
|
+
Debug.unityLogger.Log(LogType.Log, TAG, $"Client {connId} connected");
|
|
203
|
+
ClientIDs.Add(connId);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private void OnServerDisconnected(int connId)
|
|
207
|
+
{
|
|
208
|
+
ClientIDs.Remove(connId);
|
|
209
|
+
Debug.unityLogger.Log(LogType.Log, TAG, $"Client {connId} disconnected");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private void SendToClient(int clientConnId, byte[] bytes, Channel channel)
|
|
213
|
+
{
|
|
214
|
+
if (!TryGetConnectionToClient(clientConnId, out var connection))
|
|
215
|
+
return;
|
|
216
|
+
|
|
217
|
+
var message = new FishNetBroadcast {data = bytes};
|
|
218
|
+
_networkManager.ServerManager.Broadcast(connection, message, false, channel);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private bool TryGetConnectionToClient(int desiredClientID, out NetworkConnection resultConnection)
|
|
222
|
+
{
|
|
223
|
+
resultConnection = null;
|
|
224
|
+
foreach (var (clientID, conn) in _networkManager.ServerManager.Clients)
|
|
225
|
+
{
|
|
226
|
+
if (clientID == desiredClientID)
|
|
227
|
+
{
|
|
228
|
+
resultConnection = conn;
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
#endif
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#if
|
|
1
|
+
#if MIRROR
|
|
2
2
|
using System;
|
|
3
3
|
using System.Linq;
|
|
4
4
|
using System.Collections.Generic;
|
|
@@ -171,6 +171,10 @@ namespace Adrenak.UniVoice.Networks {
|
|
|
171
171
|
writer.WriteIntArray(YourVoiceSettings.mutedPeers.ToArray());
|
|
172
172
|
writer.WriteInt(YourVoiceSettings.deafenAll ? 1 : 0);
|
|
173
173
|
writer.WriteIntArray(YourVoiceSettings.deafenedPeers.ToArray());
|
|
174
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.myTags));
|
|
175
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.mutedTags));
|
|
176
|
+
writer.WriteString(string.Join(",", YourVoiceSettings.deafenedTags));
|
|
177
|
+
|
|
174
178
|
var message = new MirrorMessage {
|
|
175
179
|
data = writer.Bytes
|
|
176
180
|
};
|