Exemplo n.º 1
0
    public void InitVoiceHost(Peer host, Voice.Server dpvs, Voice.Client dpvc, Form wnd)
    {
        try
        {
            server = dpvs;
            client = dpvc;

            TestAudioSetup(wnd);

            //Audio Setup was successful, open the configuration form
            mConfigForm.ShowDialog(mClientConfig, dpvs.CompressionTypes, false);
            if (mConfigForm.DialogResult == DialogResult.Cancel)
            {
                throw new Exception("Voice configuration cancelled");
            }

            mClientConfig = mConfigForm.ClientConfig;

            //create the voice session
            CreateVoiceSession(host, Voice.SessionType.Peer, mConfigForm.CompressionGuid);

            //Connect to the voice session
            ConnectToVoiceSession(host, wnd);
        }
        catch (Exception e)
        {
            MessageBox.Show("Error attempting to start Voice Server Session.  This sample will now exit.", "Exiting", MessageBoxButtons.OK, MessageBoxIcon.Information);
            throw e;
        }
    }
Exemplo n.º 2
0
    public void InitVoiceClient(Peer dpp, Voice.Client dpvc, Form wnd)
    {
        try
        {
            TestAudioSetup(wnd);

            //Audio Setup was successful, open the configuration form
            mConfigForm.ShowDialog(mClientConfig, false);

            mClientConfig = mConfigForm.ClientConfig;
            client        = dpvc;

            //connect to the voice session
            ConnectToVoiceSession(dpp, wnd);
        }
        catch (Exception e)
        {
            MessageBox.Show("Error attempting to connect to a Voice Server.  This sample will now exit.", "Exiting", MessageBoxButtons.OK, MessageBoxIcon.Information);
            throw e;
        }
    }
Exemplo n.º 3
0
        /// <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;
            }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor
        /// </summary>
        public VoiceConnect()
        {
            InitializeComponent();

            try
            {
                //set up state members
                playerList = new ArrayList();

                //create the peer
                peerObject = new Peer();

                //create the voice client and attach message handlers
                client = new Voice.Client(peerObject);
                client.HostMigrated  += new Voice.VoiceHostMigratedEventHandler(VoiceHostMigrated);
                client.PlayerCreated += new Voice.VoicePlayerCreatedEventHandler(VoicePlayerCreated);
                client.PlayerDeleted += new Voice.VoicePlayerDeletedEventHandler(VoicePlayerDeleted);
                client.SessionLost   += new Voice.SessionLostEventHandler(VoiceSessionLost);
                client.PlayerStarted += new Voice.PlayerStartedEventHandler(PlayerStarted);
                client.PlayerStopped += new Voice.PlayerStoppedEventHandler(PlayerStopped);
                client.RecordStarted += new Voice.RecordStartedEventHandler(RecordStarted);
                client.RecordStopped += new Voice.RecordStoppedEventHandler(RecordStopped);


                //session was not lobby launched -- using connection wizard
                playWizard             = new ConnectWizard(peerObject, g_guidApp, "VoiceConnect");
                playWizard.DefaultPort = DefaultPort;

                //create the voice wizard
                voiceSettings = new VoiceWizard();

                if (playWizard.StartWizard())
                {
                    if (playWizard.IsHost)
                    {
                        this.Text += " (HOST)";

                        //create the voice server
                        server = new Voice.Server(peerObject);

                        //init the voice server
                        voiceSettings.InitVoiceHost(peerObject, server, client, this);
                    }
                    else
                    {
                        //connection to another host was successful
                        voiceSettings.InitVoiceClient(peerObject, client, this);
                    }
                }
                else
                {
                    this.Dispose();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                //allow exception to fall though and exit.
                this.Close();
            }
        }
Exemplo n.º 5
0
        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);
                    }
                }
            }
        }