private void RemoveClientBuffer(SRClient srClient) { ClientAudioProvider clientAudio = null; _clientsBufferedAudio.TryRemove(srClient.ClientGuid, out clientAudio); if (clientAudio != null) { try { _clientAudioMixer.RemoveMixerInput(clientAudio.SampleProvider); } catch (Exception ex) { Logger.Error(ex, "Error removing client input"); } } }
public void AddClientAudio(ClientAudio audio) { //sort out effects! //16bit PCM Audio //TODO: Clean - remove if we havent received audio in a while? // If we have recieved audio, create a new buffered audio and read it ClientAudioProvider client = null; if (_clientsBufferedAudio.ContainsKey(audio.ClientGuid)) { client = _clientsBufferedAudio[audio.ClientGuid]; } else { client = new ClientAudioProvider(); _clientsBufferedAudio[audio.ClientGuid] = client; _clientAudioMixer.AddMixerInput(client.SampleProvider); } client.AddClientAudioSamples(audio); }
public void StartEncoding(string guid, InputDeviceManager inputManager, IPAddress ipAddress, int port) { MMDevice speakers = null; if (_audioOutputSingleton.SelectedAudioOutput.Value == null) { speakers = WasapiOut.GetDefaultAudioEndpoint(); } else { speakers = (MMDevice)_audioOutputSingleton.SelectedAudioOutput.Value; } MMDevice micOutput = null; if (_audioOutputSingleton.SelectedMicAudioOutput.Value != null) { micOutput = (MMDevice)_audioOutputSingleton.SelectedMicAudioOutput.Value; } try { _micInputQueue.Clear(); InitMixers(); InitAudioBuffers(); //Audio manager should start / stop and cleanup based on connection successfull and disconnect //Should use listeners to synchronise all the state _waveOut = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 40, windowsN); //add final volume boost to all mixed audio _volumeSampleProvider = new VolumeSampleProviderWithPeak(_clientAudioMixer, (peak => SpeakerMax = (float)VolumeConversionHelper.ConvertFloatToDB(peak))); _volumeSampleProvider.Volume = SpeakerBoost; if (speakers.AudioClient.MixFormat.Channels == 1) { if (_volumeSampleProvider.WaveFormat.Channels == 2) { _waveOut.Init(_volumeSampleProvider.ToMono()); } else { //already mono _waveOut.Init(_volumeSampleProvider); } } else { if (_volumeSampleProvider.WaveFormat.Channels == 1) { _waveOut.Init(_volumeSampleProvider.ToStereo()); } else { //already stereo _waveOut.Init(_volumeSampleProvider); } } _waveOut.Play(); //opus _encoder = OpusEncoder.Create(MIC_SAMPLE_RATE, 1, Application.Voip); _encoder.ForwardErrorCorrection = false; //speex _speex = new Preprocessor(AudioManager.MIC_SEGMENT_FRAMES, AudioManager.MIC_SAMPLE_RATE); } catch (Exception ex) { Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message); ShowOutputError("Problem Initialising Audio Output!"); Environment.Exit(1); } if (micOutput != null) // && micOutput !=speakers { //TODO handle case when they're the same? try { _passThroughAudioProvider = new ClientAudioProvider(true); _micWaveOut = new WasapiOut(micOutput, AudioClientShareMode.Shared, true, 40, windowsN); _micWaveOutBuffer = new BufferedWaveProvider(new WaveFormat(AudioManager.OUTPUT_SAMPLE_RATE, 16, 1)); _micWaveOutBuffer.ReadFully = true; _micWaveOutBuffer.DiscardOnBufferOverflow = true; var sampleProvider = _micWaveOutBuffer.ToSampleProvider(); if (micOutput.AudioClient.MixFormat.Channels == 1) { if (sampleProvider.WaveFormat.Channels == 2) { _micWaveOut.Init(sampleProvider.ToMono()); } else { //already mono _micWaveOut.Init(sampleProvider); } } else { if (sampleProvider.WaveFormat.Channels == 1) { _micWaveOut.Init(sampleProvider.ToStereo()); } else { //already stereo _micWaveOut.Init(sampleProvider); } } _micWaveOut.Play(); } catch (Exception ex) { Logger.Error(ex, "Error starting mic audio Output - Quitting! " + ex.Message); ShowOutputError("Problem Initialising Mic Audio Output!"); Environment.Exit(1); } } if (_audioInputSingleton.MicrophoneAvailable) { try { var device = (MMDevice)_audioInputSingleton.SelectedAudioInput.Value; if (device == null) { device = WasapiCapture.GetDefaultCaptureDevice(); } device.AudioEndpointVolume.Mute = false; _wasapiCapture = new WasapiCapture(device, true); _wasapiCapture.ShareMode = AudioClientShareMode.Shared; _wasapiCapture.DataAvailable += WasapiCaptureOnDataAvailable; _wasapiCapture.RecordingStopped += WasapiCaptureOnRecordingStopped; _udpVoiceHandler = new UdpVoiceHandler(guid, ipAddress, port, this, inputManager); var voiceSenderThread = new Thread(_udpVoiceHandler.Listen); voiceSenderThread.Start(); _wasapiCapture.StartRecording(); MessageHub.Instance.Subscribe <SRClient>(RemoveClientBuffer); } catch (Exception ex) { Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message); ShowInputError("Problem initialising Audio Input!"); Environment.Exit(1); } } else { //no mic.... _udpVoiceHandler = new UdpVoiceHandler(guid, ipAddress, port, this, inputManager); MessageHub.Instance.Subscribe <SRClient>(RemoveClientBuffer); var voiceSenderThread = new Thread(_udpVoiceHandler.Listen); voiceSenderThread.Start(); } }