public VoipSession(UserDataManager userData)
        {
            _userData = userData;
            _nAudioTest = new AudioHardwareSession(Codec);
            _nAudioTest.InputDeviceNumber = App.Settings.AudioInputDeviceIndex;
            _nAudioTest.TriggerInput = _userData.Settings.MicTriggerInput;
            _nAudioTest.AudioRecorded += _nAudioTest_AudioRecorded;
            _nAudioTest.AudioRecordEnded += _nAudioTest_AudioRecordEnded;
            _nAudioTest.InputDeviceNumberChanged += _nAudioTest_InputDeviceNumberChanged;
            _nAudioTest.AudioSystemTick += _nAudioTest_AudioSystemTick;

            _udpKeepAliveTimer = new DispatcherTimer();
            _udpKeepAliveTimer.Interval = TimeSpan.FromSeconds(55);
            _udpKeepAliveTimer.Tick += _udpKeepAliveTimer_Tick;

            Connect();

            _micTrigger = SignalHelpers.ReadAudioResource("MicActivate.wav", Codec.RecordFormat);
        }
 public static SerialPacketStream ReadAudioResource(string fileName, NAudio.Wave.WaveFormat format)
 {
     if (fileName.ToLower().EndsWith(".wav"))
     {
         var stream2 = App.GetResourceFileStream("MicActivate.wav");
         NAudio.Wave.WaveFileReader r = new NAudio.Wave.WaveFileReader(stream2);
         NAudio.Wave.ResamplerDmoStream s = new NAudio.Wave.ResamplerDmoStream(r, format);
         var length = (int)s.Length;
         byte[] resampled = new byte[length];
         s.Read(resampled, 0, length);
         var sps = new SerialPacketStream(resampled, format.SampleRate, fileName, 0, "self", false);
         return sps;
     }
     else if (fileName.ToLower().EndsWith(".pga"))
     {
         var stream = App.GetResourceFileStream("micTrigger.pga");
         var sps = SerialPacketStream.Read(stream);
         return sps;
     }
     else
     {
         throw new NotImplementedException();
     }
 }
 public static string AppDataPath(SerialPacketStream ps)
 {
     var directory = App.ApplicationSettingsPath;
     var path = System.IO.Path.Combine(directory, ps.Id.FilterFilename() + "__" + ps.StreamNumber + ".pga");
     return path;
 }
        private void _nAudioTest_AudioRecordEnded()
        {
            var vt = _voiceTester;
            if (vt != null)
            {
                vt.RecordEnded();
                return;
            }

            if (_speakingRoom == null)
                return;

            var message = new VoipMessageVoice
            {
                RoomName = _speakingRoom.R.AudioRoomId,
                FromUserId = _userData.User.Id,
                Audio = new byte[0],
                IsTeamOnly = _speakingRoomTeamOnly,
                MessageNumber = _messageNumber++,
                StreamNumber = _streamNumber,
            };

            _audioChatClient.Send(message);
            _speakingRoom.OnVoiceStopped(_userData.User);
            _speakingRoom = null;

            if (DebugSaveSentPackets)
            {
                _audioRec.Write(SerialPacketStream.AppDataPath(_audioRec));
                _audioRec = null;
            }
        }
        private void _nAudioTest_AudioRecorded(AudioHardwareSession source, byte[] encoded, double signalPower)
        {
            var vt = _voiceTester;
            if (vt != null)
            {
                vt.Recorded(encoded, signalPower);
                return;
            }

            var isFirst = _speakingRoom == null;
            if (isFirst)
            {
                _speakingRoom = GetSpeakingRoom();
                _messageNumber = 0;
                _streamNumber++;

                _micTrigger.Index = 0;
            }

            if (_speakingRoom == null)
                return;

            if (isFirst)
            {
                if (UserDataManager.UserData.Settings.MicTriggerSoundOffEnabled)
                    Play(_micTrigger);

                _speakingRoomTeamOnly = _speakingRoom.R.IsVoiceTeamOnly;

                if (DebugSaveSentPackets)
                    _audioRec = new SerialPacketStream
                    {
                        Id = _userData.User.Id,
                        Parts = new List<SerialPacket>(),
                        IsEncoded = true,
                        RoomName = _speakingRoom.R.AudioRoomId,
                        StreamNumber = _streamNumber,
                    };
            }

            var message = new VoipMessageVoice
            {
                RoomName = _speakingRoom.R.AudioRoomId,
                FromUserId = _userData.User.Id,
                Audio = encoded,
                IsTeamOnly = _speakingRoomTeamOnly,
                MessageNumber = _messageNumber++,
                StreamNumber = _streamNumber,
            };

            if (DebugSaveSentPackets)
                _audioRec.Parts.Add(new SerialPacket(_audioRec.Parts.Count, encoded));

            _audioChatClient.Send(message);
            if (isFirst)
                _speakingRoom.OnVoiceSent(_userData.User);
        }
 internal void Play(SerialPacketStream ps)
 {
     lock (_playStreams)
     {
         if (!_playStreams.ContainsKey(ps.Id))
             _playStreams.Add(ps.Id, ps);
     }
 }
 private void SavePacketStream()
 {
     if (_ps != null)
     {
         _ps.Write(SerialPacketStream.AppDataPath(_ps));
         _ps = null;
     }
 }
 private void SavePacket(VoipMessageVoice item)
 {
     if (_ps == null)
         _ps = new SerialPacketStream(item, true);
     var poco = new SerialPacket(item);
     _ps.Parts.Add(poco);
 }