Example #1
0
    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;
        }
    }
Example #2
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;
            }
            }
        }