VoiceConnect
The VoiceConnect sample shows how to network with other players to start a Microsoft® DirectPlay® Voice chat session. After joining or creating a session, the players can use a microphone to talk to one other. Other players can join the session at any time.
Path
Source: (SDK root)\Samples\Multimedia\DirectPlay\VoiceConnect
Executable: (SDK root)\Samples\Multimedia\DirectPlay\Bin
User's Guide
For information about how to connect, refer to the User's Guide section of the SimplePeer sample. After connecting, the host is presented with a dialog box asking which voice codec to use. Typical voice applications
automatically select a voice codec, or present this to the user in some other
fashion. Once the chat session begins, any client can alter the playback
or capture settings by clicking Setup.
Programming Notes
The VoiceConnect sample is very similar in form to the SimplePeer sample. The VoiceConnect differs by letting clients use DirectPlay Voice to talk
to each other using a computer microphone. For
detailed programming notes see the Programming
Notes section of the SimplePeer sample.
To create the voice functionality, the application must first initialize DirectPlay voice. This sample does this by
calling CNetVoice::Init when
the main dialog is initialized. CNetVoice::Init performs the following tasks:
- Calls the VoiceSessionTestAudioSetup function to test the audio setup with DirectPlay Voice. VoiceSessionTestAudioSetup does the following:
- Creates an IDirectPlayVoiceTest object with CoCreateInstance.
- Calls IDirectPlayVoiceTest::CheckAudioSetup with the
DVFLAGS_QUERYONLY flag set. This will return DVERR_RUNSETUP
if the setup wizard needs to be run. To run the setup wizard,
call IDirectPlayVoiceTest::CheckAudioSetup again without
the DVFLAGS_QUERYONLY flag. If it returns successfully,
then the VoiceConnect sample continues, otherwise the sample ends.
- Releases the IDirectPlayVoiceTest object.
- Calls the VoiceSessionCreate function to create the audio setup with DirectPlay Voice if the player is hosting. VoiceSessionCreate does the following:
- Creates an IDirectPlayVoiceServer with CoCreateInstance.
- Calls IDirectPlayVoiceServer::Initialize to register the
voice server message handler. The
DirectPlay interface is also passed here.
- Fills out a DVSESSIONDESC structure with the session type and
the desired voice codec.
- Calls IDirectPlayVoiceServer::StartSession passing in
the DVSESSIONDESC structure.
- If the player is either hosting or joining, the application calls the VoiceSessionConnect function which uses the following steps to connect to the session.
- Create an IDirectPlayVoiceClient with CoCreateInstance.
- Call IDirectPlayVoiceClient::Initialize to register the
voice client message handler. The
DirectPlay interface is also passed here.
- Fill out a DVSOUNDDEVICECONFIG structure with the globally unique identifiers (GUIDs)
for the DirectSound playback and capture devices.
- Fill out a DVCLIENTCONFIG structure. In this sample, this structure is
filled out when with a dialog box. More complex applications
might want to do this a different way. See the VoiceConfigDlgProc function
in VoiceConnect.cpp.
- Call IDirectPlayVoiceClient::Connect passing the
DVSOUNDDEVICECONFIG and DVCLIENTCONFIG structures.
- Call IDirectPlayVoiceClient::SetTransmitTargets
to the desired default target. This sample sets the
target value to DVID_ALLPLAYERS which targets all players.
- Call IDirectPlayVoiceClient::GetSoundDeviceConfig.
Check for the DVSOUNDCONFIG_HALFDUPLEX flag in the dwflags member of the DVSOUNDDEVICECONFIG structure returned to figure out if this
client is in half-duplex mode. In half-duplex mode, the
client cannot talk, but can listen.
DirectPlayVoiceClientMessageHandler handles different DirectPlay messages such as:
- DVMSGID_CREATEVOICEPLAYER
- DVMSGID_DELETEVOICEPLAYER
- DVMSGID_HOSTMIGRATED
- DVMSGID_GAINFOCUS and DVMSGID_LOSTFOCUS
- DVMSGID_RECORDSTART
- DVMSGID_RECORDSTOP
- DVMSGID_PLAYERVOICESTART
- DVMSGID_PLAYERVOICESTOP
When Exit is clicked, the application needs to clean up DirectPlay Voice by calling CNetVoice::Free which does the following:
- Disconnects the player from the voice session. The VoiceSessionDisconnect function calls IDirectPlayVoiceClient::Disconnect and releases the IDirectPlayVoiceClient object.
- Destroys the voice session if this player is the host. The VoiceSessionDestroy function calls IDirectPlayVoiceServer::StopSession and releases the IDirectPlayVoiceServer object