protected void ConnectToVoiceSession(Peer dpp, Form wnd) { try { Voice.SoundDeviceConfig soundConfig = new Voice.SoundDeviceConfig(); //Set sound config to defaults soundConfig.GuidPlaybackDevice = DSoundHelper.DefaultVoicePlaybackDevice; soundConfig.GuidCaptureDevice = DSoundHelper.DefaultVoiceCaptureDevice; soundConfig.Window = wnd; //TODO: add error message for specific failures? //Connect to voice session client.Connect(soundConfig, mClientConfig, Voice.VoiceFlags.Sync); //set state mInSession = true; //set transmit targets to all players int[] xmitTargets = new int[1]; xmitTargets[0] = (int)PlayerID.AllPlayers; client.TransmitTargets = xmitTargets; //get sound device config to check for half-duplex soundConfig = client.SoundDeviceConfig; mHalfDuplex = ((soundConfig.Flags & Voice.SoundConfigFlags.HalfDuplex) != 0); } catch (Exception e) { throw e; } }
/// <summary> /// Initializes the DirectPlayVoice Client and Server objects /// </summary> private void InitDirectPlayVoice() { // Release any existing resources if (m_VoiceClient != null) { m_VoiceClient.Dispose(); m_VoiceClient = null; } if (m_VoiceServer != null) { m_VoiceServer.Dispose(); m_VoiceServer = null; } // Initialize UI variables this.m_NumPlayersTalking = 0; // Based on the current connection, create DirectPlay voice objects. #region About DirectPlay Voice // Since the DirectPlay Voice API defines server and client interfaces, // but not a peer interface, one of the peers must act as the server // for voice communication; the logical choice is the session host, so // for this sample, the session host creates a server object in addition // to a client object. Note that in order to send and receive voice // messages you must have a connected voice client object, even if you // are acting as the voice server. #endregion switch (Connection) { case ConnectionType.Hosting: { // Create a new Voice Server m_VoiceServer = new Voice.Server(m_Peer); // Create a Session Description for the voice session Voice.SessionDescription desc = new Voice.SessionDescription(); desc.SessionType = Voice.SessionType.Peer; desc.BufferQuality = Voice.BufferQuality.Default; desc.GuidCompressionType = Voice.CompressionGuid.Default; desc.BufferAggressiveness = Voice.BufferAggressiveness.Default; // Launch a new voice session m_VoiceServer.StartSession(desc); // Fall-through to create client object goto case ConnectionType.Connected; } case ConnectionType.Connected: { // Test Direct Voice if (TestDirectVoice() == false) { return; } // Create a new Voice Client m_VoiceClient = new Voice.Client(m_Peer); // Add event handlers m_VoiceClient.PlayerStarted += new Voice.PlayerStartedEventHandler(PlayerStartedHandler); m_VoiceClient.PlayerStopped += new Voice.PlayerStoppedEventHandler(PlayerStoppedHandler); m_VoiceClient.RecordStarted += new Voice.RecordStartedEventHandler(RecordStartedHandler); m_VoiceClient.RecordStopped += new Voice.RecordStoppedEventHandler(RecordStoppedHandler); // Fill in description object for device configuration Voice.SoundDeviceConfig soundConfig = new Voice.SoundDeviceConfig(); soundConfig.Flags = Voice.SoundConfigFlags.AutoSelect; soundConfig.GuidPlaybackDevice = DSoundHelper.DefaultPlaybackDevice; soundConfig.GuidCaptureDevice = DSoundHelper.DefaultCaptureDevice; soundConfig.Window = m_Form; soundConfig.MainBufferPriority = 0; // Fill in description object for client configuration Voice.ClientConfig clientConfig = new Voice.ClientConfig(); clientConfig.Flags = Voice.ClientConfigFlags.AutoVoiceActivated | Voice.ClientConfigFlags.AutoRecordVolume; clientConfig.RecordVolume = (int)Voice.RecordVolume.Last; clientConfig.PlaybackVolume = (int)Voice.PlaybackVolume.Default; clientConfig.Threshold = Voice.Threshold.Unused; clientConfig.BufferQuality = Voice.BufferQuality.Default; clientConfig.BufferAggressiveness = Voice.BufferAggressiveness.Default; clientConfig.NotifyPeriod = 0; try { // Connect to the voice session m_VoiceClient.Connect(soundConfig, clientConfig, Voice.VoiceFlags.Sync); } catch (Exception ex) { m_Form.ShowException(ex, "Connect", true); m_Form.Dispose(); return; } // Set DirectPlay to send voice messages to all players int[] targets = { (int)Voice.PlayerId.AllPlayers }; m_VoiceClient.TransmitTargets = targets; break; } case ConnectionType.Disconnected: { return; } } }
private void onConnectComplete(object sender, ConnectCompleteEventArgs e) { timeoutTimer.Dispose(); if (status == NetworkStatus.Connecting) { if (e.Message.ResultCode == Microsoft.DirectX.DirectPlay.ResultCode.Success) { playerId = e.Message.LocalPlayerId; status = NetworkStatus.Connected; if (voiceSetupOk) { // start voice client voiceClient = new Microsoft.DirectX.DirectPlay.Voice.Client(client, new int[] { // message mask 0x0006, // RecordStarted 0x0007, // RecordStopped 0x0008, // ConnectResult 0x000A // InputLevel }); // Add event handlers voiceClient.ConnectResult += new ConnectResultEventHandler(onVoiceConnectResult); voiceClient.PlayerStarted += new PlayerStartedEventHandler(onPlayerStarted); // never raised, but necessary to allow for the calling of onRecordStarted voiceClient.PlayerStopped += new PlayerStoppedEventHandler(onPlayerStopped); // never raised, but necessary to allow for the calling of onRecordStopped voiceClient.RecordStarted += new RecordStartedEventHandler(onRecordStarted); voiceClient.RecordStopped += new RecordStoppedEventHandler(onRecordStopped); voiceClient.InputLevel += new InputLevelEventHandler(onInputLevel); SoundDeviceConfig soundConfig = new SoundDeviceConfig(); soundConfig.Flags = (audioManager.AudioProperties.DisableAutoconfiguration ? 0 : SoundConfigFlags.AutoSelect); /*SoundConfigFlags.HalfDuplex*/ if (audioManager.DirectSoundDevice != null) { soundConfig.PlaybackDevice = audioManager.DirectSoundDevice; } soundConfig.GuidPlaybackDevice = DSoundHelper.DefaultPlaybackDevice; soundConfig.GuidCaptureDevice = DSoundHelper.DefaultCaptureDevice; soundConfig.Window = voiceFocusWindow; soundConfig.MainBufferPriority = 0; updateClientConfig(); voiceStatus = VoiceStatus.Connecting; // Connect to the voice session try { voiceClient.Connect(soundConfig, clientConfig, 0 /*VoiceFlags.Sync*/); } catch (TransportNoPlayerException) { } catch (Exception exception) { voiceStatus = VoiceStatus.Disconnected; VoiceFailureCause cause = (exception is CompressionNotSupportedException ? VoiceFailureCause.CompressionNotSupported : (exception is IncompatibleVersionException ? VoiceFailureCause.IncompatibleVersion : (exception is NoVoiceSessionException ? VoiceFailureCause.NoVoiceSession : (exception is RunSetupException ? VoiceFailureCause.RunSetup : (exception is SoundInitializeFailureException ? VoiceFailureCause.SoundInitFailure : (exception is Microsoft.DirectX.DirectPlay.Voice.TimedOutException ? VoiceFailureCause.TimeOut : VoiceFailureCause.Other)))))); NetworkMessage errorMessage = new NetworkMessage(0, (byte)ReservedMessageType.VoiceConnectionFailed, new byte[1] { (byte)cause }); lock (networkMessages) { networkMessages.Enqueue(errorMessage); } return; } } else { NetworkMessage errorMessage = new NetworkMessage(0, (byte)ReservedMessageType.VoiceConnectionFailed, new byte[1] { (byte)VoiceFailureCause.RunSetup }); lock (networkMessages) { networkMessages.Enqueue(errorMessage); } } // add the new message to the message list NetworkMessage message = new NetworkMessage(0, (byte)ReservedMessageType.ConnectionEstablished, null); lock (networkMessages) { networkMessages.Enqueue(message); } } else { status = NetworkStatus.Disconnected; ConnectionFailureCause cause; switch (e.Message.ResultCode) { case Microsoft.DirectX.DirectPlay.ResultCode.HostRejectedConnection: cause = ConnectionFailureCause.HostRejectedConnection; break; case Microsoft.DirectX.DirectPlay.ResultCode.NoConnection: cause = ConnectionFailureCause.NoConnection; break; case Microsoft.DirectX.DirectPlay.ResultCode.NotHost: cause = ConnectionFailureCause.NotHost; break; case Microsoft.DirectX.DirectPlay.ResultCode.SessionFull: cause = ConnectionFailureCause.SessionFull; break; default: cause = ConnectionFailureCause.Other; break; } NetworkMessage message = new NetworkMessage(0, (byte)ReservedMessageType.ConnectionFailed, new byte[1] { (byte)cause }); lock (networkMessages) { networkMessages.Enqueue(message); } } } }