Ejemplo n.º 1
0
        public void Start([NotNull] string name, [NotNull] IAmplitudeProvider micAmplitude, [NotNull] Rooms rooms, [NotNull] RoomChannels roomChannels, [NotNull] PlayerChannels playerChannels, [NotNull] ILossEstimator loss)
        {
            if (name == null)
            {
                throw new ArgumentException("name");
            }
            if (micAmplitude == null)
            {
                throw new ArgumentException("micProvider");
            }
            if (rooms == null)
            {
                throw new ArgumentNullException("rooms");
            }
            if (roomChannels == null)
            {
                throw new ArgumentException("roomChannels");
            }
            if (playerChannels == null)
            {
                throw new ArgumentException("playerChannels");
            }
            if (loss == null)
            {
                throw new ArgumentException("playerChannels");
            }

            Local = new LocalVoicePlayerState(name, micAmplitude, rooms, roomChannels, playerChannels, loss);

            Add(Local);
        }
Ejemplo n.º 2
0
        private void Start()
        {
            //Ensure that all settings are loaded before we access them (potentially from other threads)
            DebugSettings.Preload();
            VoiceSettings.Preload();

            //Write multithreaded logs ASAP so the logging system knows which is the main thread
            Logs.WriteMultithreadedLogs();

            var net = gameObject.GetComponent <ICommsNetwork>();

            if (net == null)
            {
                throw new Exception("Cannot find a voice network component. Please attach a voice network component appropriate to your network system to the DissonanceVoiceComms' entity.");
            }

            if (PlaybackPrefab == null)
            {
                Log.Info("Loading default playback prefab");
                PlaybackPrefab = Resources.Load <GameObject>("PlaybackPrefab").GetComponent <VoicePlayback>();
            }

            net.PlayerJoined          += Net_PlayerJoined;
            net.PlayerLeft            += Net_PlayerLeft;
            net.VoicePacketReceived   += Net_VoicePacketReceived;
            net.PlayerStartedSpeaking += Net_PlayerStartedSpeaking;
            net.PlayerStoppedSpeaking += Net_PlayerStoppedSpeaking;
            net.TextPacketReceived    += _text.OnMessageReceived;

            if (string.IsNullOrEmpty(LocalPlayerName))
            {
                var guid = Guid.NewGuid().ToString();
                LocalPlayerName = guid;
            }

            //mark this component as started, locking the LocalPlayerName, PlaybackPrefab and Microphone properties from changing
            _started = true;

            MicCapture = MicrophoneCapture.Start(_micName);

            _localPlayerState = new LocalVoicePlayerState(LocalPlayerName, this);
            _players.Add(_localPlayerState);
            _playersLookup.Add(LocalPlayerName, _localPlayerState);

            Action <NetworkMode> networkModeChanged = mode =>
            {
                if (mode.IsClientEnabled())
                {
                    var encoder = new OpusEncoder(VoiceSettings.Instance.Quality, VoiceSettings.Instance.FrameSize);
                    _decoderFrameSize  = (uint)encoder.FrameSize;
                    _decoderSampleRate = encoder.SampleRate;

                    _transmissionPipeline = new EncoderPipeline(MicCapture, encoder, _net, () => _playerChannels.Count + _roomChannels.Count);

                    for (var i = 0; i < _activationListeners.Count; i++)
                    {
                        MicCapture.Subscribe(_activationListeners[i]);
                    }
                }
                else
                {
                    if (_transmissionPipeline != null)
                    {
                        _transmissionPipeline.Dispose();
                        _transmissionPipeline = null;
                    }

                    for (var i = 0; i < _activationListeners.Count; i++)
                    {
                        MicCapture.Unsubscribe(_activationListeners[i]);
                    }
                }
            };

            if (MicCapture != null)
            {
                net.ModeChanged += networkModeChanged;
            }
            else
            {
                Log.Warn("No microphone detected; local voice transmission will be disabled.");
            }

            net.Initialize(LocalPlayerName, Rooms, PlayerChannels, RoomChannels);
            _net = net;
        }