Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7888 lines
238 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dvclientengine.cpp
  6. * Content: Implementation of class for DirectXVoice Client
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/19/99 rodtoll Created
  12. * 07/21/99 rodtoll Added settings confirm message to protocol
  13. * Added storing user flags
  14. * 07/22/99 rodtoll Updated to use new player class
  15. * Improved concurrency protection
  16. * Added client/server support
  17. * 07/23/99 rodtoll Added multicast support
  18. * Modified notify loop, now checks for multicast timeouts
  19. * Modified playback loop to kill timed-out users
  20. * Minor fixes to client/server support
  21. * Other bugfixes.
  22. * Removed valid target check in multicast and client/server
  23. * 07/26/99 rodtoll Updated to support IDirectXVoiceNotify interface
  24. * 07/29/99 pnewson Updated call to CInputQueue2 constructor
  25. * 07/29/99 rodtoll Removed warnings, updated for new queue, added better
  26. * parameter checking.
  27. * 07/30/99 rodtoll Updated InitializeSound to use the parameters passed
  28. * in through the sounddeviceconfig.
  29. * 08/02/99 rodtoll Fixed bug in record volume
  30. * 08/03/99 pnewson Cleanup of Frame and Queue classes
  31. * 08/03/99 rodtoll Fixed calls into the transport
  32. * 08/04/99 rodtoll Fixed up Get/SetClientConfig
  33. * Added pointer to SoundConfig for second param of connect result
  34. * Half duplex clients won't get record level notifications anymore
  35. * Added connect result when error on connect
  36. * Modified so that soundeviceconfig gets ptr to dsound/dsc devices
  37. * Fixed bug w/sensitivity setting
  38. * 08/05/99 rodtoll Fixed locking, was causing deadlock w/DirectPlay
  39. * 08/10/99 rodtoll Initial support for host migration
  40. * rodtoll No longer creates a queue for ourselves
  41. * 08/18/99 rodtoll Fixed bug w/multicast. Added support for SPEECHBOUNCE
  42. * message type.
  43. * 08/25/99 rodtoll General Cleanup/Modifications to support new
  44. * compression sub-system.
  45. * 08/26/99 rodtoll Added copy of flags when doing SetClientConfig
  46. * rodtoll Fixed playback thread to properly handle playback mute
  47. * 08/27/99 rodtoll Fixed record start/stop notifications target info
  48. * rodtoll Added playervoicestart and playervoice stop messages
  49. * 08/30/99 rodtoll Fixed disconnect when sound init fails
  50. * Fixed disconnect when compression type unsupported
  51. * Fixed notification intervals
  52. * Added timeouts to connect and disconnect processes
  53. * Fixed bug in disconnect code
  54. * Added re-transmission of connect request
  55. * Fixed host migration notifications
  56. * Fixed return code on GetCompressionTypes call.
  57. * Updated to use new format specifications on playback
  58. * 08/31/99 rodtoll Logic re-write to fix shutdown problems.
  59. * - Notify thread now starts as soon as Initialize called
  60. * and stops when object destroyed
  61. * - Disconnect based on Disconnectconfirm or loss of
  62. * connection signals event, notify thread then handles
  63. * - Updated playback format to use 8Khz for playback
  64. * 09/01/99 rodtoll Added check for valid pointers in func calls
  65. * 09/02/99 rodtoll Added checks to handle case no local player created
  66. * Re-activated and fixed old auto record volume code
  67. * 09/03/99 rodtoll Re-work of playback core to support mixing to multiple buffers
  68. * for 3d support.
  69. * Re-worked playback to use position instead of notifications
  70. * allows for simpler handling of high CPU and 3d support
  71. * Implemented CreateUserBuffer/DeleteUserBuffer
  72. * 09/04/99 rodtoll Added code to delete 3d buffers for users/groups when they
  73. * are destroyed.
  74. * 09/07/99 rodtoll Added support for server controlled target message
  75. * rodtoll Added support for set/get user buffer for notarget
  76. * rodtoll Fixed InitHalfDuplex call -- was defaulting to default device always
  77. * 09/07/99 rodtoll Placed some guards to fix crash when Releasing when not initialized
  78. * 09/07/99 rodtoll Updated to allow buffer management on buffer for remaining users (main buffer)
  79. * rodtoll Updated return codes to use new errors (3d related)
  80. * 09/08/99 rodtoll Fixed playback level checking.
  81. * 09/10/99 rodtoll Implemented the DVCLIENTCONFIG_MUTEGLOBAL flag
  82. * rodtoll Additional parameter validations
  83. * 09/13/99 rodtoll Added preliminary support for new DVSOUNDDEVICECONFIG structure
  84. * 09/14/99 rodtoll Added new DVMSGID_PLAYEROUTPUTLEVEL message
  85. * rodtoll Added new SetNotifyMask function
  86. * rodtoll Updated INiitalize parameters to take notification masks.
  87. * rodtoll Implemented notification masks. (Allows user to choose which notifications to receive)
  88. * rodtoll Added CheckShouldSendMessage
  89. * rodtoll Added SendPlayerLevels (DVMSGID_PLAYEROUTPUTLEVEL messages handler)
  90. * 09/15/99 rodtoll Added DVMSGID_SETTARGET message when target is set by remote server
  91. * 09/16/99 rodtoll Updated Disconnect to alllow async and sync abortions of connects
  92. * 09/17/99 rodtoll Fixed bug in recordthread in error handling
  93. * 09/18/99 rodtoll Added HandleThreadError to be called when an internal thread dies.
  94. * 09/20/99 rodtoll Updated to return SESSIONLOST message instead of DISCONNECT when session is lost
  95. * rodtoll Functions will return DVERR_SESSIONLOST if called after session is lost.
  96. * rodtoll Improved error checking in playback thread
  97. * rodtoll Added more checks for memory alloc failures
  98. * rodtoll Small bugfix in playeroutputlevel messages
  99. * rodtoll Stricter checks on valid notify arrays
  100. * rodtoll Added proper error checking to SetNotifyMask
  101. * 09/27/99 rodtoll Fixed playback volume control
  102. * rodtoll Fixed bug w/echo servers w/clients running on same dvid as host
  103. * 09/28/99 rodtoll Fixed playervoicestart/stop notifications, now send source in the dwFrom param.
  104. * rodtoll Double notifications of local client when host migrates fixed.
  105. * rodtoll Added queue for notifications, notifications are added to the queue and
  106. * then signalled by the notify thread. (Prevents problems caused by notify
  107. * handlers taking a long time to return).
  108. * rodtoll Added voice suppression
  109. * 09/29/99 pnewson Major AGC overhaul
  110. * 10/04/99 rodtoll Added usage of the DVPROTOCOL_VERSION_XXX macros
  111. * rodtoll Added comments
  112. * rodtoll Fixed crash which occurs if object released before initialized
  113. * 10/05/99 rodtoll Added guards to DoDisconnect. If recording locked up on shutdown, then
  114. * DoDisconnect would be called twice --> Crash! Fixed.
  115. * rodtoll Reversed order of recording/playback shutdown. Shutting down playback
  116. * before recording caused recording lockup on ESS cards.
  117. * rodtoll Additional documentation
  118. * 10/07/99 rodtoll Updated to work in Unicode
  119. * rodtoll Modified notifications so connectresult should always be first
  120. * Removed release of write locks so that connect result would be queued first.
  121. * 10/15/99 pnewson Added config check in Connect call
  122. * 10/18/99 rodtoll Fix: Calling initialize twice doesn't fail.
  123. * 10/19/99 rodtoll Fix: Bug #113904 Shutdown Issues
  124. * - Added handler for SESSIONLOST messages. Fixes shutdown lockup.
  125. * - Changed disconnectAck event to manual reset so multiple threads can wait
  126. * on it. Neccessary to ensure disconnect is completed before release is done
  127. * - Changed behaviour of disconnect so that if you specify SYNC and disconnect
  128. * is in progress, you wait for complete. Required to support disconnect in releae
  129. * 10/25/99 rodtoll Fix: Bug #114684 - GetCaps causes lockup on shutdown
  130. * rodtoll Fix: Bug #114223 - Debug messages being printed at error level when inappropriate
  131. * 10/27/99 pnewson Fix: Bug #113935 - Saved AGC values should be device specific
  132. * Fix: Bug #113936 - Wizard should reset the AGC level before loopback test
  133. * Note: this fix adds the DVCLIENTCONFIG_AUTOVOLUMERESET flag
  134. * 10/28/99 pnewson Bug #114176 updated DVSOUNDDEVICECONFIG struct
  135. * 10/29/99 rodtoll Bug #113726 - Integrate Voxware Codecs, updating to use new
  136. * pluggable codec architecture. In order to support new architecture
  137. * all codecs creates were moved to threads where CoInitialize has been called.
  138. * rodtoll Fixed memory leak in multicast mode caused by new architecture
  139. * 11/04/99 pnewson Bug #114297 - Added HWND to SupervisorCheckAudioSetup call
  140. * 11/12/99 rodtoll Updated to use new playback and record classes and remove
  141. * old playback/record system (Includes waveIN/waveOut support)
  142. * rodtoll Updated to support new recording thread
  143. * rodtoll Added new echo suppression code
  144. * 11/16/99 rodtoll Recording thread now loops everytime it wakes up until it
  145. * has compressed and transmitted all the data it can before
  146. * going back to sleep.
  147. * 11/17/99 rodtoll Fix: Bug #115538 - dwSize members of > sizeof struct were accepted
  148. * rodtoll Fix: Bug #115827 - Calling SetNotifyMask w/no callback should fail
  149. * rodtoll Fix: Bug #117442 - Calling Disconnect with invalid flags doesn't return DVERR_INVALIDFLAGS
  150. * rodtoll Fix: Bug #117447 - GetTransmitTarget has problems
  151. * rodtoll Fix: Bug #117177 - Calling Connect w/o voice session never returns
  152. * 11/18/99 rodtoll Updated to control echo cancellation switching code by define.
  153. * 11/22/99 rodtoll Fixed problem caused by switching on echo cancellation while talking
  154. * 11/22/99 rodtoll Fixed Initialize() would fail incorrectly
  155. * 11/23/99 rodtoll Updated Initialize/SetNotifyMask so error checking behaviour is consistant
  156. * 11/24/99 rodtoll Adjusted Set/GetTransmit Target so locks are released before calling into dplay
  157. * 11/30/99 pnewson Reworked default device mapping code
  158. * Adjusted some timing issues to make single stepping connect possible
  159. * 12/01/99 rodtoll Bug #121815 - Recording/playback may contain static. Updated to call functions
  160. * to set conversion quality setting to high.
  161. * rodtoll Bug #115783 - Always adjusts volume for default device. Fixed for Win2k, Win9x w/DX7
  162. * Systems w/DX5 or none will use waveIN/waveOUT and will default to default device.
  163. * 12/02/99 rodtoll Bug #115783 - Will now use waveIN/waveOut object corresponding to specified GUID
  164. * on DX3 systems.
  165. * 12/06/99 rodtoll Bumped playback/record threads to time critical priority
  166. * 12/16/99 rodtoll Bug #117405 - 3D Sound APIs misleading - 3d sound apis renamed
  167. * The Delete3DSoundBuffer was re-worked to match the create
  168. * rodtoll Bug #122629 - Host migration broken in unusual configurations
  169. * Implemented new host migration scheme.
  170. * rodtoll Bug #121054 - DirectX 7.1 changes must be incorporated
  171. * rodtoll Implemented new DVPROTOCOLMSG_PLAYERLIST message to handle player table message.
  172. * rodtoll As part of new host migration, implemented proper handling of connection
  173. * rejected message (was broken, exposed by new host migration).
  174. * rodtoll Updated Disconnect to handle inability to contact server properly which
  175. * was resulting in an error message (when it should disconnect anyhow).
  176. * rodtoll Removed voice suppression
  177. * 01/10/00 pnewson AGC and VA tuning
  178. * 01/14/2000 rodtoll Updated for new speech packet types / packet handling
  179. * rodtoll Updated for new Get/SetTransmitTargets functions
  180. * rodtoll Added support for multiple targets
  181. * rodtoll Added use of fixed pool manager to manage memory for
  182. * notifications.
  183. * rodtoll Updated notifications to support messages with memory.
  184. * rodtoll Updated message handler calls to use new format
  185. * rodtoll Updated all notifications to use new message structures
  186. * rodtoll Updated Connect/Disconnect so that when DVFLAGS_SYNCH is
  187. * specified no completion messages will be sent.
  188. * rodtoll Added new API call GetSoundDeviceConfig
  189. * 01/21/2000 pnewson Changes in support of revised wizard UI
  190. * Allow concurrent AGC and user controlled volume
  191. * 01/24/2000 rodtoll Bug #129427: Calling Destroy3DSoundBuffer for player who has
  192. * already disconnected resulted in an incorrect DVERR_NOTBUFFERED
  193. * error code.
  194. * 01/27/2000 rodtoll Bug #129934 - Update Create3DSoundBuffer to take DSBUFFERDESC
  195. * 01/28/2000 rodtoll Bug #130465: Record Mute/Unmute must call YieldFocus() / ClaimFocus()
  196. * 02/01/2000 rodtoll Disable capture focus features - Bug #129457
  197. * 02/08/2000 rodtoll Bug #131496 - Selecting DVTHRESHOLD_DEFAULT results in voice
  198. * never being detected
  199. * 02/15/2000 rodtoll Fixed Connect so mapping GUIDs doesn't stomp user structure
  200. * 02/17/2000 rodtoll Bug #133691 - Choppy audio - queue was not adapting
  201. * Added instrumentation
  202. * 03/28/2000 rodtoll Re-wrote nametable handling and locking -- more scalable
  203. * rodtoll Fixed pool for players
  204. * rodtoll Bilink of "active players" and "players to notify" allows for greater
  205. * concurrency (playback and notify threads don't need to lock entire
  206. * nametable while running.
  207. * 03/29/2000 rodtoll Bug #30957 - Made conversion quality slider setting optional -- new flag -- DVSOUNDCONFIG_SETCONVERSIONQUALITY
  208. * rodtoll Incorporated experimental playback handling w/lower priority and more frequent wakeup
  209. * rodtoll Instead of calling ConfirmValidEntity now checks nametable
  210. * 04/07/2000 rodtoll Bug #32179 - Prevent registration of > 1 interface
  211. * rodtoll Updated to use no copy sends, so handles pooling frames to be sent, proper
  212. * pulling of frames from pools and returns.
  213. * 04/19/2000 rodtoll Re-enabled capture focus behaviour / found not working on WDM, re-disabled.
  214. * rodtoll Bug #31106 - Handle sound devices w/no recording volume
  215. * Set DVSOUNDCONFIG_NORECVOLAVAILABLE flag on DVSOUNDDEVICECONFIG and do not
  216. * perform any volume sets when this flag is present
  217. * 04/20/2000 rodtoll Bug #31478 - Lockup in shutdown on client who has become new host -- ref count issue
  218. * 04/24/2000 rodtoll Bug #33228 - Compile error reported by davidkl
  219. * 04/27/2000 rodtoll Fix for host migration crash turned out to be sample bug, restoring.
  220. * rodtoll Fix for crash on Connect failed
  221. * 05/11/2000 rodtoll Bug #34852 Voice connection crashes after being in a session for a couple of minutes
  222. * 05/17/2000 rodtoll Bug #35110 Simultaneous playback of 2 voices results in distorted playback
  223. * 05/30/2000 rodtoll Bug #35476 Access violation in DPVOICE.DLL on host after client left
  224. * Host migration code was being fired because of DirectPlay8's nametable unwinding funcs.
  225. * 05/31/2000 rodtoll Bug #35860 - Fix VC6 compile errors for instrumented builds
  226. * rodtoll Bug #35794 - Setting targets to none results in leak.
  227. * 06/02/2000 rodtoll Moved host migration so it is keyed off voice message and transport messages.
  228. * More reliable this way.
  229. * 06/21/2000 rodtoll Bug #35767 - Implement ability for Dsound effects processing if dpvoice buffers
  230. * Updated Connect and Create3DSoundBuffer to take buffers instead of descriptions
  231. * rodtoll Bug #36820 - Host migrates to wrong client when server is shut down before host's client disconnects
  232. * Caused because client attempts to register new server when there is one already
  233. * rodtoll Bug #37045 - Race conditions prevent acknowledgement of new host
  234. * Added send when new host is elected of settingsconfirm message
  235. * 06/27/2000 rodtoll Fixed window where outstanding sends being returned after we have deregistered
  236. * Voice now waits for all outstanding voice sends to complete before shutting down
  237. * rodtoll Added COM abstraction
  238. * 06/28/2000 rodtoll Prefix Bug #38022
  239. * 07/01/2000 rodtoll Bug #38280 - DVMSGID_DELETEVOICEPLAYER messages are being sent in non-peer to peer sessions
  240. * Nametable will now only unravel with messages if session is peer to peer.
  241. * rodtoll Bug #38316 - HOST MIGRATION - Player fails to get HOST_MIGRATED message
  242. * 07/09/2000 rodtoll Added signature bytes
  243. * 07/12/2000 rodtoll Bug #39117 - Access violation while running VoicePosition. Several issues:
  244. * - Allow Destroy3DBuffer during disconnect
  245. * - Move nametable cleanup to before freesoundbufferlist
  246. * - Fixed code so always remove from list on DeleteSoundTarget
  247. * - Removed unneeded logic
  248. * 07/12/2000 rodtoll Bug #31468 - Add diagnostic spew to logfile to show what is failing the HW Wizard
  249. * rodtoll Bug #32841 - Renable support for capture focus
  250. * 07/22/2000 rodtoll Bug #40284 - Initialize() and SetNotifyMask() should return invalidparam instead of invalidpointer
  251. * rodtoll Bug #40296, 38858 - Crashes due to shutdown race condition
  252. * Now ensures that all threads from transport have left and that
  253. * all notificatinos have been processed before shutdown is complete.
  254. * rodtoll Bug #39586 - Trap 14 in DPVVOX.DLL during session of voicegroup, adding guards for overwrites
  255. * 07/26/2000 rodtoll Bug #40676 - Forwarding server is broken
  256. * 07/31/2000 rodtoll Bug #40470 - SetClientConfig() with invalid flags returns INVALIDPARAM
  257. * 08/03/2000 rodtoll Bug #41457 - DPVOICE: need way to discover which specific dsound call failed when returning DVERR_SOUNDINITFAILURE
  258. * 08/08/2000 rodtoll Was missing a DNLeaveCriticalSection
  259. * 08/09/2000 rodtoll Bug #41936 - No voice session message instead of compression not supported
  260. * 08/21/2000 rodtoll Bug #41475 - DPVOICE: Lockup during shutdown when deleteplayer messages received
  261. * 08/22/2000 rodtoll Bug #43095 - DPVOICE: DVMSGID_GAINFOCUS and DVMSGID_LOSTFOCUS are not passing NULL message parameters
  262. * 08/28/2000 masonb Voice Merge: DNet FPOOLs use DNCRITICAL_SECTION, modified m_pBufferDescPool usage
  263. * 08/29/2000 rodtoll Bug #43668 - DPVOICE: Asserts when exiting DPVOICE session
  264. * 08/31/2000 rodtoll Bug #43804 - DVOICE: dwSensitivity structure member is confusing - should be dwThreshold
  265. * 08/31/2000 rodtoll Whistler Bug #171841 - Prefix Bug
  266. * 09/01/2000 masonb Modified PlaybackThread, RecordThread, and NotifyThread to call _endthread to clean up thread handles
  267. * 09/14/2000 rodtoll Bug #45001 - DVOICE: AV if client has targetted > 10 players
  268. * 09/26/2000 rodtoll Bug #45541 - DPVOICE: Client gets DVERR_TIMEOUT message when disconnecting
  269. * 09/28/2000 rodtoll Fix Again: Bug #45541 - DPVOICE: Client gets DVERR_TIMEOUT message when disconnecting (Server always confirms disconnect)
  270. * 10/05/2000 rodtoll Bug #46541 - DPVOICE: A/V linking to dpvoice.lib could cause application to fail init and crash
  271. * 11/16/2000 rodtoll Bug #40587 - DPVOICE: Mixing server needs to use multi-processors
  272. * 01/22/2001 rodtoll WINBUG #288437 - IA64 Pointer misalignment due to wire packets
  273. * 01/25/2001 rodtoll WINBUG #293197 - DPVOICE: Stress applications cannot tell the difference between out of memory/invalid device/other errors
  274. * 01/26/2001 rodtoll WINBUG #293197 - DPVOICE: [STRESS} Stress applications cannot tell difference between out of memory and internal errors.
  275. * Remap DSERR_OUTOFMEMORY to DVERR_OUTOFMEMORY instead of DVERR_SOUNDINITFAILURE.
  276. * Remap DSERR_ALLOCATED to DVERR_PLAYBACKSYSTEMERROR instead of DVERR_SOUNDINITFAILURE.
  277. *
  278. * 04/04/2001 rodtoll WINBUG #343428 - DPVOICE: Voice wizard's playback is very choppy.
  279. * rodtoll WINBUG #356124 - STRESS: DPVLPY7 broke when Initialize() failed due to being out of memory.
  280. * 04/02/2001 simonpow Bug #354859 Fixes for problems spotted by PREfast (hresult casting to bool and local variable hiding)
  281. * 04/06/2001 kareemc Added Voice Defense
  282. * 04/12/2001 kareemc WINBUG #360971 - Wizard Memory Leaks
  283. * 04/09/2001 rodtoll WINBUG #363804 DPVOICE: Race condition in shutdown causes disconnect to timeout if session is being shutdown concurrently.
  284. * 04/11/2001 rodtoll WINBUG #221494 DPVOICE: Capped the # of queued recording events to prevent multiple wakeups resulting in false lockup
  285. * detection
  286. * 04/12/2001 simonpow WINBUG #322454 Removed early unlock in RemovePlayer method and added
  287. * extra return code checking in DoConnectResponse
  288. * 04/21/2001 rodtoll MANBUG #50058 DPVOICE: VoicePosition: No sound for couple of seconds when position bars are moved
  289. * - Added StartMix() call when secondary buffers are created
  290. * - Added extra bit of debug spew, removed dead code
  291. * 04/21/2001 rodtoll (for Simonpow)
  292. * WINBUG #322454 DPVOICE: [STRESS] Connect() appears to be suceeding (returns S_OK) but connection is not established.
  293. *
  294. ***************************************************************************/
  295. #include "dxvoicepch.h"
  296. extern HRESULT DVS_Create(LPDIRECTVOICESERVEROBJECT *piDVS);
  297. // Use high priority for playback / record threads
  298. #define __CORE_THREAD_PRIORITY_HIGH
  299. // Disables the sound system
  300. //#define __DISABLE_SOUND
  301. // Forces full duplex mode
  302. //#define __FORCEFULLDUPLEX
  303. // # of ms of inactivity before a multicast user is considered to have
  304. // timed-out.
  305. #define DV_MULTICAST_USERTIMEOUT_PERIOD 300000
  306. // # of ms of inactivity before an incoming audio stream is considered to
  307. // have stopped. Used to determine when to send PLAYERVOICESTOP
  308. // message.
  309. #define PLAYBACK_RECEIVESTOP_TIMEOUT 500
  310. // # of ms the notify thread sleeps without notification to wakeup
  311. // and perform house cleaning.
  312. #define DV_CLIENT_NOTIFYWAKEUP_TIMEOUT 100
  313. // # of ms before a connect request is considered to have been lost
  314. #define DV_CLIENT_CONNECT_RETRY_TIMEOUT 1250
  315. // # of ms before we should timeout a connect request completely
  316. #define DV_CLIENT_CONNECT_TIMEOUT 30000
  317. // Maximum count notification semaphores can have
  318. #define DVCLIENT_NOTIFY_MAXSEMCOUNT 0x0FFFFFFF
  319. //// TODO: Needs tuning.
  320. // # of ms to wait for disconnect reply from server before timing out.
  321. #define DV_CLIENT_DISCONNECT_TIMEOUT 10000
  322. #define DV_CLIENT_SRCQUALITY_INVALID ((DIRECTSOUNDMIXER_SRCQUALITY) 0xFFFFFFFF)
  323. #define CLIENT_POOLS_NUM 3
  324. #define CLIENT_POOLS_SIZE_MESSAGE (sizeof(DVPROTOCOLMSG_FULLMESSAGE))
  325. #define CLIENT_POOLS_SIZE_PLAYERLIST DVPROTOCOL_PLAYERLIST_MAXSIZE
  326. // DV_CLIENT_EMULATED_LEAD_ADJUST
  327. //
  328. // # of buffer's worth of lead ahead of read pointer allowable to write
  329. // in playback buffer when buffer is emulated (additional, not total)
  330. #define DV_CLIENT_EMULATED_LEAD_ADJUST 2
  331. // DV_CLIENT_BASE_LEAD_MAX
  332. //
  333. // Maximum # of buffers lead to allow
  334. #define DV_CLIENT_BASE_LEAD_MAX 2
  335. // MixingWakeupProc
  336. //
  337. // This function is called by the windows timer used by this
  338. // class each time the timer goes off. The function signals
  339. // a semaphore provided by the creator of the timer.
  340. //
  341. // Parameters:
  342. // DWORD param - A recast pointer to a HANDLE
  343. #undef DPF_MODNAME
  344. #define DPF_MODNAME "CDirectVoiceClientEngine::MixingWakeupProc"
  345. BOOL CDirectVoiceClientEngine::MixingWakeupProc( DWORD_PTR param )
  346. {
  347. TimerHandlerParam *pParam = (TimerHandlerParam *) param;
  348. SetEvent( pParam->hPlaybackTimerEvent );
  349. SetEvent( pParam->hRecordTimerEvent );
  350. DNEnterCriticalSection( &pParam->csPlayCount );
  351. pParam->lPlaybackCount++;
  352. DNLeaveCriticalSection( &pParam->csPlayCount );
  353. return TRUE;
  354. }
  355. #undef DPF_MODNAME
  356. #define DPF_MODNAME "CDirectVoiceClientEngine::CDirectVoiceClientEngine"
  357. //
  358. // Constructor
  359. //
  360. // Initializes object to uninitialized state. Must call Initialize succesfully before
  361. // the object can be used (except GetCompressionTypes which can be called at any time).
  362. //
  363. CDirectVoiceClientEngine::CDirectVoiceClientEngine( DIRECTVOICECLIENTOBJECT *lpObject
  364. ): m_dwSignature(VSIG_CLIENTENGINE),
  365. m_lpFramePool(NULL),
  366. m_lpObject(lpObject),
  367. m_lpSessionTransport(NULL),
  368. m_lpUserContext(NULL),
  369. m_dvidServer(0),
  370. m_pTimer(NULL),
  371. m_bLastPeak(0),
  372. m_bLastTransmitted(FALSE),
  373. m_bMsgNum(0),
  374. m_bSeqNum(0),
  375. m_dwActiveCount(0),
  376. m_dwLastConnectSent(0),
  377. m_audioPlaybackBuffer(NULL),
  378. m_audioRecordDevice(NULL),
  379. m_audioPlaybackDevice(NULL),
  380. m_audioRecordBuffer(NULL),
  381. m_hRecordDone(NULL),
  382. m_hRecordTerminate(NULL),
  383. m_hPlaybackDone(NULL),
  384. m_hPlaybackTerminate(NULL),
  385. m_hConnectAck(NULL),
  386. m_dwCurrentState(DVCSTATE_NOTINITIALIZED),
  387. m_hrConnectResult(DVERR_GENERIC),
  388. m_hrOriginalConnectResult(DVERR_GENERIC),
  389. m_hrDisconnectResult(DV_OK),
  390. m_hDisconnectAck(NULL),
  391. m_hNotifyDone( NULL ),
  392. m_hNotifyTerminate( NULL ),
  393. m_hNotifyChange( NULL ),
  394. m_bLastPlaybackPeak( 0 ),
  395. m_lpdvServerMigrated(NULL),
  396. m_hNotifyDisconnect(NULL),
  397. m_hNotifyConnect(NULL),
  398. m_pFramePool(NULL),
  399. m_lpstGeneralBuffer(NULL),
  400. m_lpstBufferList(NULL),
  401. m_lpdwMessageElements(NULL),
  402. m_dwNumMessageElements(0),
  403. m_fSessionLost(FALSE),
  404. m_fLocalPlayerNotify(FALSE),
  405. m_lpNotifyList(NULL),
  406. m_hNewNotifyElement(NULL),
  407. m_dwHostOrderID(DVPROTOCOL_HOSTORDER_INVALID),
  408. m_pdvidTargets(NULL),
  409. m_dwNumTargets(0),
  410. m_pfpNotifications(NULL),
  411. m_dwTargetVersion(0),
  412. m_fConnectAsync(false),
  413. m_fDisconnectAsync(false),
  414. m_dwOriginalRecordQuality(DV_CLIENT_SRCQUALITY_INVALID),
  415. m_dwOriginalPlayQuality(DV_CLIENT_SRCQUALITY_INVALID),
  416. m_dwPlayActiveCount(0),
  417. m_fLocalPlayerAvailable(FALSE),
  418. m_fNotifyQueueEnabled(FALSE),
  419. m_hRecordThreadHandle(NULL),
  420. m_hPlaybackThreadHandle(NULL),
  421. m_dwMigrateHostOrderID(DVPROTOCOL_HOSTORDER_INVALID),
  422. m_pStatsBlob(NULL),
  423. m_fCritSecInited(FALSE)
  424. {
  425. m_dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
  426. m_dvSoundDeviceConfig.lpdsPlaybackDevice = NULL;
  427. memset( &m_dvCaps, 0x00, sizeof( DVCAPS ) );
  428. m_dvCaps.dwSize = sizeof( DVCAPS );
  429. m_dvCaps.dwFlags = 0;
  430. ClientStats_Reset();
  431. ZeroMemory( &m_perfInfo, sizeof( PERF_APPLICATION ) );
  432. ZeroMemory( &m_perfAppInfo, sizeof( PERF_APPLICATION_INFO ) );
  433. }
  434. #undef DPF_MODNAME
  435. #define DPF_MODNAME "CDirectVoiceClientEngine::InitClass"
  436. BOOL CDirectVoiceClientEngine::InitClass( )
  437. {
  438. if (!DNInitializeCriticalSection( &m_csNotifyQueueLock ))
  439. {
  440. return FALSE;
  441. }
  442. if (!DNInitializeCriticalSection( &m_lockPlaybackMode ))
  443. {
  444. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  445. return FALSE;
  446. }
  447. if (!DNInitializeCriticalSection( &m_csTargetLock ))
  448. {
  449. DNDeleteCriticalSection( &m_lockPlaybackMode );
  450. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  451. return FALSE;
  452. }
  453. if (!DNInitializeCriticalSection( &m_csCleanupProtect ))
  454. {
  455. DNDeleteCriticalSection( &m_csTargetLock );
  456. DNDeleteCriticalSection( &m_lockPlaybackMode );
  457. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  458. return FALSE;
  459. }
  460. if (!DNInitializeCriticalSection( &m_csBufferLock ))
  461. {
  462. DNDeleteCriticalSection( &m_csCleanupProtect );
  463. DNDeleteCriticalSection( &m_csTargetLock );
  464. DNDeleteCriticalSection( &m_lockPlaybackMode );
  465. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  466. return FALSE;
  467. }
  468. if (!DNInitializeCriticalSection( &m_csPlayAddList ))
  469. {
  470. DNDeleteCriticalSection( &m_csBufferLock );
  471. DNDeleteCriticalSection( &m_csCleanupProtect );
  472. DNDeleteCriticalSection( &m_csTargetLock );
  473. DNDeleteCriticalSection( &m_lockPlaybackMode );
  474. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  475. return FALSE;
  476. }
  477. if (!DNInitializeCriticalSection( &m_csNotifyAddList ))
  478. {
  479. DNDeleteCriticalSection( &m_csPlayAddList );
  480. DNDeleteCriticalSection( &m_csBufferLock );
  481. DNDeleteCriticalSection( &m_csCleanupProtect );
  482. DNDeleteCriticalSection( &m_csTargetLock );
  483. DNDeleteCriticalSection( &m_lockPlaybackMode );
  484. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  485. return FALSE;
  486. }
  487. if (!DNInitializeCriticalSection( &m_csTransmitBufferLock ))
  488. {
  489. DNDeleteCriticalSection( &m_csNotifyAddList );
  490. DNDeleteCriticalSection( &m_csPlayAddList );
  491. DNDeleteCriticalSection( &m_csBufferLock );
  492. DNDeleteCriticalSection( &m_csCleanupProtect );
  493. DNDeleteCriticalSection( &m_csTargetLock );
  494. DNDeleteCriticalSection( &m_lockPlaybackMode );
  495. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  496. return FALSE;
  497. }
  498. if (!DNInitializeCriticalSection( &m_thTimerInfo.csPlayCount ))
  499. {
  500. DNDeleteCriticalSection( &m_csTransmitBufferLock );
  501. DNDeleteCriticalSection( &m_csNotifyAddList );
  502. DNDeleteCriticalSection( &m_csPlayAddList );
  503. DNDeleteCriticalSection( &m_csBufferLock );
  504. DNDeleteCriticalSection( &m_csCleanupProtect );
  505. DNDeleteCriticalSection( &m_csTargetLock );
  506. DNDeleteCriticalSection( &m_lockPlaybackMode );
  507. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  508. return FALSE;
  509. }
  510. if (!DNInitializeCriticalSection( &m_csClassLock ))
  511. {
  512. DNDeleteCriticalSection( &m_thTimerInfo.csPlayCount );
  513. DNDeleteCriticalSection( &m_csTransmitBufferLock );
  514. DNDeleteCriticalSection( &m_csNotifyAddList );
  515. DNDeleteCriticalSection( &m_csPlayAddList );
  516. DNDeleteCriticalSection( &m_csBufferLock );
  517. DNDeleteCriticalSection( &m_csCleanupProtect );
  518. DNDeleteCriticalSection( &m_csTargetLock );
  519. DNDeleteCriticalSection( &m_lockPlaybackMode );
  520. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  521. return FALSE;
  522. }
  523. if (!DNInitializeCriticalSection( &m_csNotifyLock ))
  524. {
  525. DNDeleteCriticalSection( &m_csClassLock );
  526. DNDeleteCriticalSection( &m_thTimerInfo.csPlayCount );
  527. DNDeleteCriticalSection( &m_csTransmitBufferLock );
  528. DNDeleteCriticalSection( &m_csNotifyAddList );
  529. DNDeleteCriticalSection( &m_csPlayAddList );
  530. DNDeleteCriticalSection( &m_csBufferLock );
  531. DNDeleteCriticalSection( &m_csCleanupProtect );
  532. DNDeleteCriticalSection( &m_csTargetLock );
  533. DNDeleteCriticalSection( &m_lockPlaybackMode );
  534. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  535. return FALSE;
  536. }
  537. m_fCritSecInited = TRUE;
  538. return TRUE;
  539. }
  540. #undef DPF_MODNAME
  541. #define DPF_MODNAME "CDirectVoiceClientEngine::~CDirectVoiceClientEngine"
  542. // Destructor
  543. //
  544. // This function requires a write lock to complete.
  545. //
  546. // If the object is connected to a session, it will be disconnected
  547. // by this function.
  548. //
  549. // Releases the resources associated with the object and stops the
  550. // notifythread.
  551. //
  552. // Locks Required:
  553. // - Class Write Lock
  554. //
  555. // Called By:
  556. // DVC_Release when reference count reaches 0.
  557. //
  558. CDirectVoiceClientEngine::~CDirectVoiceClientEngine()
  559. {
  560. CDVCSLock guardLock(&m_csClassLock);
  561. guardLock.Lock();
  562. if( m_dwCurrentState != DVCSTATE_IDLE &&
  563. m_dwCurrentState != DVCSTATE_NOTINITIALIZED )
  564. {
  565. Cleanup();
  566. }
  567. if( m_hNotifyDone != NULL )
  568. {
  569. SetEvent( m_hNotifyTerminate );
  570. WaitForSingleObject( m_hNotifyDone, INFINITE );
  571. CloseHandle( m_hNotifyDone );
  572. CloseHandle( m_hNotifyTerminate );
  573. CloseHandle( m_hNotifyChange );
  574. CloseHandle( m_hNotifyDisconnect );
  575. CloseHandle( m_hNotifyConnect );
  576. m_hNotifyDone = NULL;
  577. m_hNotifyTerminate = NULL;
  578. m_hNotifyChange = NULL;
  579. m_hNotifyDisconnect = NULL;
  580. m_hNotifyConnect = NULL;
  581. }
  582. if( m_hConnectAck != NULL )
  583. CloseHandle( m_hConnectAck );
  584. if( m_hDisconnectAck != NULL )
  585. CloseHandle( m_hDisconnectAck );
  586. if( m_lpdvServerMigrated != NULL )
  587. {
  588. m_lpdvServerMigrated->Release();
  589. m_lpdvServerMigrated = NULL;
  590. }
  591. if( m_lpdwMessageElements != NULL )
  592. delete [] m_lpdwMessageElements;
  593. guardLock.Unlock();
  594. NotifyQueue_Free();
  595. if (m_fCritSecInited)
  596. {
  597. DNDeleteCriticalSection( &m_lockPlaybackMode );
  598. DNDeleteCriticalSection( &m_csTargetLock );
  599. DNDeleteCriticalSection( &m_csCleanupProtect );
  600. DNDeleteCriticalSection( &m_csBufferLock );
  601. DNDeleteCriticalSection( &m_thTimerInfo.csPlayCount );
  602. DNDeleteCriticalSection( &m_csPlayAddList );
  603. DNDeleteCriticalSection( &m_csNotifyAddList );
  604. DNDeleteCriticalSection( &m_csTransmitBufferLock );
  605. DNDeleteCriticalSection( &m_csClassLock );
  606. DNDeleteCriticalSection( &m_csNotifyQueueLock );
  607. DNDeleteCriticalSection( &m_csNotifyLock );
  608. }
  609. if( m_pdvidTargets != NULL )
  610. {
  611. delete [] m_pdvidTargets;
  612. }
  613. m_dwSignature = VSIG_CLIENTENGINE_FREE;
  614. }
  615. // InternalSetNotifyMask
  616. //
  617. // Sets the list of valid notifiers for this object.
  618. //
  619. // Locks Needed:
  620. // - ReadLock to check status and then releases it.
  621. // - m_csNotifyLock to update notification list
  622. //
  623. // Called By:
  624. // DVC_SetNotifyMask
  625. //
  626. #undef DPF_MODNAME
  627. #define DPF_MODNAME "CDirectVoiceClientEngine::InternalSetNotifyMask"
  628. HRESULT CDirectVoiceClientEngine::InternalSetNotifyMask( LPDWORD lpdwMessages, DWORD dwNumElements )
  629. {
  630. BFCSingleLock slLock( &m_csNotifyLock );
  631. slLock.Lock();
  632. // Delete previous elements
  633. if( m_lpdwMessageElements != NULL )
  634. {
  635. delete [] m_lpdwMessageElements;
  636. }
  637. m_dwNumMessageElements = dwNumElements;
  638. // Make copies of the message elements into our own message array.
  639. if( dwNumElements > 0 )
  640. {
  641. m_lpdwMessageElements = new DWORD[dwNumElements];
  642. if( m_lpdwMessageElements == NULL )
  643. {
  644. DPFX(DPFPREP, DVF_ERRORLEVEL, "Initialize: Error allocating memory" );
  645. return DVERR_OUTOFMEMORY;
  646. }
  647. memcpy( m_lpdwMessageElements, lpdwMessages, sizeof(DWORD)*dwNumElements );
  648. }
  649. else
  650. {
  651. m_lpdwMessageElements = NULL;
  652. }
  653. return DV_OK;
  654. }
  655. // SetNotifyMask
  656. //
  657. // Sets the list of valid notifiers for this object.
  658. //
  659. // Locks Needed:
  660. // - ReadLock to check status and then releases it.
  661. // - m_csNotifyLock to update notification list
  662. //
  663. // Called By:
  664. // DVC_SetNotifyMask
  665. //
  666. #undef DPF_MODNAME
  667. #define DPF_MODNAME "CDirectVoiceClientEngine::SetNotifyMask"
  668. HRESULT CDirectVoiceClientEngine::SetNotifyMask( LPDWORD lpdwMessages, DWORD dwNumElements )
  669. {
  670. HRESULT hr;
  671. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  672. hr = DV_ValidMessageArray( lpdwMessages, dwNumElements, FALSE );
  673. if( FAILED( hr ) )
  674. {
  675. DPFX(DPFPREP, DVF_ERRORLEVEL, "ValidMessageArray Failed 0x%x", hr );
  676. return hr;
  677. }
  678. DPFX(DPFPREP, DVF_APIPARAM, "Message IDs=%d", dwNumElements );
  679. if( lpdwMessages != NULL )
  680. {
  681. for( DWORD dwIndex = 0; dwIndex < dwNumElements; dwIndex++ )
  682. {
  683. DPFX(DPFPREP, DVF_APIPARAM, "MessageIDs[%d] = %d", dwIndex, lpdwMessages[dwIndex] );
  684. }
  685. }
  686. CDVCSLock guardLock(&m_csClassLock);
  687. guardLock.Lock();
  688. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  689. {
  690. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not initialized" );
  691. return DVERR_NOTINITIALIZED;
  692. }
  693. guardLock.Unlock();
  694. BFCSingleLock slLock( &m_csNotifyLock );
  695. slLock.Lock();
  696. if( m_lpMessageHandler == NULL )
  697. {
  698. DPFX(DPFPREP, DVF_ERRORLEVEL, "Cannot specify message mask there is no callback function" );
  699. return DVERR_NOCALLBACK;
  700. }
  701. hr = InternalSetNotifyMask( lpdwMessages, dwNumElements );
  702. DPFX(DPFPREP, DVF_APIPARAM, "Returning hr=0x%x", hr );
  703. return DV_OK;
  704. }
  705. // Initialize
  706. //
  707. // Initializes this object into a state where it can be used to Connect to a session. Sets the
  708. // notification function, notification mask and the transport object that will be used.
  709. //
  710. // Starts the notification thread.
  711. //
  712. // Locks Required:
  713. // - Class Write Lock
  714. //
  715. // Called By:
  716. // DV_Initialize
  717. //
  718. #undef DPF_MODNAME
  719. #define DPF_MODNAME "CDirectVoiceClientEngine::Initialize"
  720. HRESULT CDirectVoiceClientEngine::Initialize( CDirectVoiceTransport *lpTransport, LPDVMESSAGEHANDLER lpdvHandler, LPVOID lpUserContext, LPDWORD lpdwMessages, DWORD dwNumElements )
  721. {
  722. HRESULT hr;
  723. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  724. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  725. DPFX(DPFPREP, DVF_APIPARAM, "Param: lpTransport = 0x%p lpdvHandler = 0x%p lpUserContext = 0x%p dwNumElements = %d", lpTransport, lpdvHandler, lpUserContext, dwNumElements );
  726. if( lpTransport == NULL )
  727. {
  728. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid transport" );
  729. return DVERR_INVALIDPOINTER;
  730. }
  731. hr = DV_ValidMessageArray( lpdwMessages, dwNumElements, FALSE );
  732. if( FAILED( hr ) )
  733. {
  734. DPFX(DPFPREP, DVF_ERRORLEVEL, "ValidMessageArray Failed hr = 0x%x", hr );
  735. return hr;
  736. }
  737. DPFX(DPFPREP, DVF_APIPARAM, "Message IDs=%d", dwNumElements );
  738. if( lpdwMessages != NULL )
  739. {
  740. for( DWORD dwIndex = 0; dwIndex < dwNumElements; dwIndex++ )
  741. {
  742. DPFX(DPFPREP, DVF_APIPARAM, "MessageIDs[%d] = %d", dwIndex, lpdwMessages[dwIndex] );
  743. }
  744. }
  745. HANDLE hThread;
  746. // Wait for a write lock on the object
  747. CDVCSLock guardLock(&m_csClassLock);
  748. guardLock.Lock();
  749. if( m_dwCurrentState != DVCSTATE_NOTINITIALIZED )
  750. {
  751. DPFX(DPFPREP, DVF_ERRORLEVEL, "Already Initialized" );
  752. return DVERR_INITIALIZED;
  753. }
  754. if( lpdvHandler == NULL && lpdwMessages != NULL )
  755. {
  756. DPFX(DPFPREP, DVF_ERRORLEVEL, "Cannot specify message mask there is no callback function" );
  757. return DVERR_NOCALLBACK;
  758. }
  759. m_dwLastConnectSent = 0;
  760. m_dwSynchBegin = 0;
  761. SetCurrentState( DVCSTATE_IDLE );
  762. BFCSingleLock slLock( &m_csNotifyLock );
  763. slLock.Lock();
  764. m_lpMessageHandler = lpdvHandler;
  765. hr = InternalSetNotifyMask( lpdwMessages, dwNumElements );
  766. if( FAILED( hr ) )
  767. {
  768. SetCurrentState( DVCSTATE_NOTINITIALIZED );
  769. DPFX(DPFPREP, DVF_ERRORLEVEL, "SetNotifyMask Failed hr=0x%x", hr );
  770. return hr;
  771. }
  772. m_lpSessionTransport = lpTransport;
  773. m_lpUserContext = lpUserContext;
  774. m_dvidLocal = 0;
  775. m_dwActiveCount = 0;
  776. m_thTimerInfo.hPlaybackTimerEvent = NULL;
  777. m_thTimerInfo.lPlaybackCount = 0;
  778. m_thTimerInfo.hRecordTimerEvent = NULL;
  779. m_hConnectAck = CreateEvent( NULL, FALSE, FALSE, NULL );
  780. m_hDisconnectAck = CreateEvent( NULL, TRUE, FALSE, NULL );
  781. m_hNotifyDone = CreateEvent( NULL, FALSE, FALSE, NULL );
  782. m_hNotifyTerminate = CreateEvent( NULL, FALSE, FALSE, NULL );
  783. m_hNotifyChange = CreateEvent( NULL, FALSE, FALSE, NULL );
  784. m_hNotifyDisconnect = CreateEvent( NULL, FALSE, FALSE, NULL );
  785. m_hNotifyConnect = CreateEvent( NULL, FALSE, FALSE, NULL );
  786. if( m_hConnectAck == NULL ||
  787. m_hDisconnectAck == NULL ||
  788. m_hNotifyTerminate == NULL ||
  789. m_hNotifyChange == NULL ||
  790. m_hNotifyDisconnect == NULL ||
  791. m_hNotifyConnect == NULL ||
  792. m_hNotifyDone==NULL)
  793. {
  794. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create required events" );
  795. goto ERROR_EXIT_INIT;
  796. }
  797. if (FAILED(NotifyQueue_Init()))
  798. {
  799. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to init notify queue" );
  800. goto ERROR_EXIT_INIT;
  801. }
  802. hThread = (HANDLE) _beginthread( NotifyThread, 0, this );
  803. if( hThread == NULL )
  804. {
  805. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create watcher thread" );
  806. goto ERROR_EXIT_INIT;
  807. }
  808. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  809. DPFX(DPFPREP, DVF_INFOLEVEL, "Notify Thread Started: 0x%p", hThread );
  810. guardLock.Unlock();
  811. DPFX(DPFPREP, DVF_APIPARAM, "Returning DV_OK" );
  812. return DV_OK;
  813. ERROR_EXIT_INIT:
  814. if( m_hConnectAck != NULL )
  815. {
  816. CloseHandle( m_hConnectAck );
  817. m_hConnectAck = NULL;
  818. }
  819. if( m_hDisconnectAck != NULL )
  820. {
  821. CloseHandle( m_hDisconnectAck );
  822. m_hDisconnectAck = NULL;
  823. }
  824. if( m_hNotifyTerminate != NULL )
  825. {
  826. CloseHandle( m_hNotifyTerminate );
  827. m_hNotifyTerminate = NULL;
  828. }
  829. if( m_hNotifyChange != NULL )
  830. {
  831. CloseHandle( m_hNotifyChange );
  832. m_hNotifyChange = NULL;
  833. }
  834. if( m_hNotifyDisconnect != NULL )
  835. {
  836. CloseHandle( m_hNotifyDisconnect );
  837. m_hNotifyDisconnect = NULL;
  838. }
  839. if( m_hNotifyConnect != NULL )
  840. {
  841. CloseHandle( m_hNotifyConnect );
  842. m_hNotifyConnect = NULL;
  843. }
  844. DPFX(DPFPREP, DVF_ERRORLEVEL, "Returning DVERR_GENERIC" );
  845. return DVERR_GENERIC;
  846. }
  847. #undef DPF_MODNAME
  848. #define DPF_MODNAME "CDirectVoiceClientEngine::Connect"
  849. // Connect
  850. //
  851. // Implements the IDirectXVoiceClient::Connect function.
  852. //
  853. // Locks Required:
  854. // - Write Lock
  855. //
  856. // Called By:
  857. // DVC_Connect
  858. //
  859. HRESULT CDirectVoiceClientEngine::Connect( LPDVSOUNDDEVICECONFIG lpSoundDeviceConfig, LPDVCLIENTCONFIG lpClientConfig, DWORD dwFlags )
  860. {
  861. HRESULT hr;
  862. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  863. hr = DV_ValidClientConfig( lpClientConfig );
  864. if( FAILED( hr ) )
  865. {
  866. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid Client Config hr=0x%x", hr );
  867. return hr;
  868. }
  869. hr = DV_ValidSoundDeviceConfig( lpSoundDeviceConfig, s_lpwfxPlaybackFormat );
  870. if( FAILED( hr ) )
  871. {
  872. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid Sound Device Config hr=0x%x", hr );
  873. return hr;
  874. }
  875. if( dwFlags & ~(DVFLAGS_SYNC|DVFLAGS_NOQUERY) )
  876. {
  877. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid flags specified" );
  878. return DVERR_INVALIDFLAGS;
  879. }
  880. DV_DUMP_SDC( lpSoundDeviceConfig );
  881. DV_DUMP_CC( lpClientConfig );
  882. CDVCSLock guardLock(&m_csClassLock);
  883. guardLock.Lock();
  884. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  885. {
  886. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initialized" );
  887. return DVERR_NOTINITIALIZED;
  888. }
  889. if( m_dwCurrentState == DVCSTATE_CONNECTING ||
  890. m_dwCurrentState == DVCSTATE_DISCONNECTING )
  891. {
  892. DPFX(DPFPREP, DVF_ERRORLEVEL, "Already connecting or disconnecting" );
  893. return DVERR_ALREADYPENDING;
  894. }
  895. if( m_dwCurrentState == DVCSTATE_CONNECTED )
  896. {
  897. DPFX(DPFPREP, DVF_ERRORLEVEL, "Already connected" );
  898. return DVERR_CONNECTED;
  899. }
  900. // Copy over the parameters
  901. memcpy( &m_dvSoundDeviceConfig, lpSoundDeviceConfig, sizeof( DVSOUNDDEVICECONFIG ) );
  902. // map the devices
  903. GUID guidTemp;
  904. hr = DV_MapCaptureDevice(&(m_dvSoundDeviceConfig.guidCaptureDevice), &guidTemp);
  905. if (FAILED(hr))
  906. {
  907. DPFX(DPFPREP, DVF_ERRORLEVEL, "DV_MapCaptureDevice failed, code: %i", hr);
  908. return hr;
  909. }
  910. m_dvSoundDeviceConfig.guidCaptureDevice = guidTemp;
  911. hr = DV_MapPlaybackDevice(&(m_dvSoundDeviceConfig.guidPlaybackDevice), &guidTemp);
  912. if (FAILED(hr))
  913. {
  914. DPFX(DPFPREP, DVF_ERRORLEVEL, "DV_MapPlaybackDevice failed, code: %i", hr);
  915. return hr;
  916. }
  917. m_dvSoundDeviceConfig.guidPlaybackDevice = guidTemp;
  918. // Check to ensure setup has been run on these devices
  919. // but only if the NOQUERY flag has not been set.
  920. if (!(dwFlags & DVFLAGS_NOQUERY))
  921. {
  922. hr = SupervisorCheckAudioSetup(
  923. &(m_dvSoundDeviceConfig.guidPlaybackDevice),
  924. &(m_dvSoundDeviceConfig.guidCaptureDevice),
  925. NULL,
  926. DVFLAGS_QUERYONLY);
  927. switch (hr)
  928. {
  929. case DV_FULLDUPLEX:
  930. // great - carry on.
  931. DPFX(DPFPREP, DVF_INFOLEVEL, "Devices have been tested - full duplex ok");
  932. break;
  933. case DV_HALFDUPLEX:
  934. // force on the half duplex flag.
  935. DPFX(DPFPREP, DVF_INFOLEVEL, "Devices have been tested - half duplex only");
  936. m_dvSoundDeviceConfig.dwFlags |= DVSOUNDCONFIG_HALFDUPLEX;
  937. break;
  938. case DVERR_SOUNDINITFAILURE:
  939. // The devices were tested, and failed miserably.
  940. DPFX(DPFPREP, DVF_INFOLEVEL, "Devices have been tested - total failure");
  941. return DVERR_SOUNDINITFAILURE;
  942. break;
  943. case DVERR_RUNSETUP:
  944. // return the run setup code
  945. DPFX(DPFPREP, DVF_ERRORLEVEL, "Devices have not been tested");
  946. return DVERR_RUNSETUP;
  947. default:
  948. // unexpected return code - this is a real error - propogate it back up
  949. DPFX(DPFPREP, DVF_ERRORLEVEL, "SupervisorCheckAudioSetup failed, code: %i", hr);
  950. return hr;
  951. }
  952. }
  953. // RESET Session flags that need to be reset on every connect
  954. m_fSessionLost = FALSE;
  955. m_hrDisconnectResult = DV_OK;
  956. m_fLocalPlayerNotify = FALSE;
  957. m_fLocalPlayerAvailable = FALSE;
  958. m_dwHostOrderID = DVPROTOCOL_HOSTORDER_INVALID;
  959. m_hPlaybackThreadHandle = NULL;
  960. m_hRecordThreadHandle = NULL;
  961. m_thTimerInfo.hPlaybackTimerEvent = NULL;
  962. m_thTimerInfo.lPlaybackCount = 0;
  963. m_thTimerInfo.hRecordTimerEvent = NULL;
  964. m_lpdvfCompressionInfo = NULL;
  965. m_hrConnectResult = DVERR_GENERIC;
  966. m_hrOriginalConnectResult = DVERR_GENERIC;
  967. ClientStats_Reset();
  968. // Add a reference to incoming objects
  969. if( m_dvSoundDeviceConfig.lpdsPlaybackDevice != NULL )
  970. {
  971. m_dvSoundDeviceConfig.lpdsPlaybackDevice->AddRef();
  972. }
  973. if( m_dvSoundDeviceConfig.lpdsMainBuffer != NULL )
  974. {
  975. m_dvSoundDeviceConfig.lpdsMainBuffer->AddRef();
  976. }
  977. // Add a reference to incoming objects
  978. if( m_dvSoundDeviceConfig.lpdsCaptureDevice != NULL )
  979. {
  980. m_dvSoundDeviceConfig.lpdsCaptureDevice->AddRef();
  981. }
  982. DNEnterCriticalSection( &m_lockPlaybackMode );
  983. m_dwActiveCount = 0;
  984. m_dwEchoState = DVCECHOSTATE_IDLE;
  985. DNLeaveCriticalSection( &m_lockPlaybackMode );
  986. // Need to reset disconnect event manually
  987. ResetEvent( m_hDisconnectAck );
  988. // This was here to disable capture
  989. //
  990. // m_dvSoundDeviceConfig.dwFlags |= DVSOUNDCONFIG_NOFOCUS;
  991. m_dvSoundDeviceConfig.dwMainBufferFlags |= DSBPLAY_LOOPING;
  992. memcpy( &m_dvClientConfig, lpClientConfig, sizeof( DVCLIENTCONFIG ) );
  993. // Check for duplicate objects in the process, re-use existing object if there is one.
  994. // Also does some sanity checks.
  995. hr = CheckForDuplicateObjects();
  996. if( FAILED(hr) )
  997. {
  998. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error checking dsound(cap) objects hr=0x%x", hr );
  999. goto CONNECT_ERROR;
  1000. }
  1001. #ifdef __FORCEFULLDUPLEX
  1002. m_dvSoundDeviceConfig.dwFlags |= DVSOUNDCONFIG_HALFDUPLEX;
  1003. #endif
  1004. if( m_dvClientConfig.dwBufferAggressiveness == DVBUFFERAGGRESSIVENESS_DEFAULT )
  1005. {
  1006. m_dvClientConfig.dwBufferAggressiveness = s_dwDefaultBufferAggressiveness;
  1007. }
  1008. if( m_dvClientConfig.dwBufferQuality == DVBUFFERQUALITY_DEFAULT )
  1009. {
  1010. m_dvClientConfig.dwBufferQuality = s_dwDefaultBufferQuality;
  1011. }
  1012. if( m_dvClientConfig.dwThreshold == DVTHRESHOLD_DEFAULT )
  1013. {
  1014. m_dvClientConfig.dwThreshold = s_dwDefaultSensitivity;
  1015. }
  1016. m_dwMigrateHostOrderID = DVPROTOCOL_HOSTORDER_INVALID;
  1017. m_dwLastConnectSent = 0;
  1018. m_dwSynchBegin = 0;
  1019. SetCurrentState( DVCSTATE_CONNECTING );
  1020. // Initialize the name table
  1021. m_voiceNameTable.Initialize();
  1022. // Initialize bilinks -- if we fail on our connect things won't go south.
  1023. m_dwPlayActiveCount = 0;
  1024. InitBilink( &m_blPlayActivePlayers, NULL );
  1025. InitBilink( &m_blPlayAddPlayers, NULL );
  1026. InitBilink( &m_blNotifyActivePlayers, NULL );
  1027. InitBilink( &m_blNotifyAddPlayers, NULL );
  1028. ZeroMemory( &m_perfInfo, sizeof( PERF_APPLICATION_INFO ) );
  1029. // Setup for performance entry
  1030. // TODO: Get the GUID of the instance for the session
  1031. m_perfInfo.dwFlags = PERF_APPLICATION_VALID | PERF_APPLICATION_VOICE;
  1032. CoCreateGuid( &m_perfInfo.guidApplicationInstance );
  1033. CoCreateGuid( &m_perfInfo.guidInternalInstance );
  1034. m_perfInfo.guidIID = IID_IDirectPlayVoiceClient;
  1035. m_perfInfo.dwProcessID = GetCurrentProcessId();
  1036. m_perfInfo.dwMemoryBlockSize = sizeof( ClientStatistics ) + sizeof( DVSESSIONDESC );
  1037. // Initilalize the new performance library
  1038. hr = PERF_AddEntry( &m_perfInfo, &m_perfAppInfo );
  1039. // This is an error, but not fatal
  1040. if( FAILED ( hr ) )
  1041. {
  1042. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create performance tracking object 0x%x", hr );
  1043. }
  1044. if( m_perfAppInfo.pbMemoryBlock )
  1045. {
  1046. m_pStatsBlob = (ClientStatistics *) m_perfAppInfo.pbMemoryBlock;
  1047. }
  1048. else
  1049. {
  1050. m_pStatsBlob = &m_stats;
  1051. }
  1052. m_fpPlayers.Initialize();
  1053. hr = SetupInitialBuffers();
  1054. if( FAILED( hr ) )
  1055. {
  1056. DPFX(DPFPREP, DVF_ERRORLEVEL, "SetupBuffersInitial Failed 0x%x", hr );
  1057. goto CONNECT_ERROR;
  1058. }
  1059. hr = m_lpSessionTransport->EnableReceiveHook( m_lpObject, DVTRANSPORT_OBJECTTYPE_CLIENT );
  1060. if( FAILED( hr ) )
  1061. {
  1062. DPFX(DPFPREP, DVF_ERRORLEVEL, "EnableReceiveHook Failed 0x%x", hr );
  1063. goto CONNECT_ERROR;
  1064. }
  1065. m_fConnectAsync = !(dwFlags & DVFLAGS_SYNC);
  1066. m_dvidServer = m_lpSessionTransport->GetServerID();
  1067. m_dvidLocal = m_lpSessionTransport->GetLocalID();
  1068. // Send connect request to the server
  1069. guardLock.Unlock();
  1070. m_dwLastConnectSent = GetTickCount();
  1071. m_dwSynchBegin = m_dwLastConnectSent;
  1072. hr = Send_ConnectRequest();
  1073. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::Connect() - Sending Request to server" );
  1074. if( FAILED( hr ) )
  1075. {
  1076. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error on send 0x%x", hr );
  1077. goto CONNECT_ERROR;
  1078. }
  1079. // If the user wants us to wait for the response, wait here.
  1080. if( dwFlags & DVFLAGS_SYNC )
  1081. {
  1082. DPFX(DPFPREP, DVF_INFOLEVEL, "Sync flag, waiting for completion." );
  1083. // Wait until we're called with the appropriate message
  1084. WaitForSingleObject( m_hConnectAck, INFINITE );
  1085. DPFX(DPFPREP, DVF_INFOLEVEL, "Server response received" );
  1086. return GetConnectResult();
  1087. }
  1088. return DVERR_PENDING;
  1089. CONNECT_ERROR:
  1090. // Release any objects we are holding.. if we have created a sound device
  1091. // reference. I.e. we've linked to an inproc sound object
  1092. if( lpSoundDeviceConfig->lpdsPlaybackDevice == NULL &&
  1093. m_dvSoundDeviceConfig.lpdsPlaybackDevice != NULL )
  1094. {
  1095. m_dvSoundDeviceConfig.lpdsPlaybackDevice->Release();
  1096. m_dvSoundDeviceConfig.lpdsPlaybackDevice = NULL;
  1097. }
  1098. if( m_dvSoundDeviceConfig.lpdsMainBuffer != NULL )
  1099. {
  1100. m_dvSoundDeviceConfig.lpdsMainBuffer->Release();
  1101. m_dvSoundDeviceConfig.lpdsMainBuffer = NULL;
  1102. }
  1103. // Release any objects we are holding
  1104. // i.e. we've linked to an inproc sound object
  1105. if( lpSoundDeviceConfig->lpdsCaptureDevice == NULL &&
  1106. m_dvSoundDeviceConfig.lpdsCaptureDevice != NULL )
  1107. {
  1108. m_dvSoundDeviceConfig.lpdsCaptureDevice->Release();
  1109. m_dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
  1110. }
  1111. SetCurrentState( DVCSTATE_IDLE );
  1112. FreeBuffers();
  1113. m_voiceNameTable.DeInitialize((m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER),m_lpUserContext,m_lpMessageHandler);
  1114. m_fpPlayers.Deinitialize();
  1115. PERF_RemoveEntry( m_perfInfo.guidInternalInstance, &m_perfAppInfo );
  1116. return hr;
  1117. }
  1118. #undef DPF_MODNAME
  1119. #define DPF_MODNAME "CDirectVoiceClientEngine::Send_SessionLost"
  1120. HRESULT CDirectVoiceClientEngine::Send_SessionLost()
  1121. {
  1122. PDVPROTOCOLMSG_SESSIONLOST pSessionLost;
  1123. PDVTRANSPORT_BUFFERDESC pBufferDesc;
  1124. LPVOID pvSendContext;
  1125. HRESULT hr;
  1126. pBufferDesc = GetTransmitBuffer( sizeof( DVPROTOCOLMSG_SESSIONLOST ), &pvSendContext );
  1127. if( pBufferDesc == NULL )
  1128. {
  1129. return DVERR_OUTOFMEMORY;
  1130. }
  1131. pSessionLost = (PDVPROTOCOLMSG_SESSIONLOST) pBufferDesc->pBufferData;
  1132. // Send connection request to the server
  1133. pSessionLost->dwType = DVMSGID_SESSIONLOST;
  1134. pSessionLost->hresReason = DVERR_SESSIONLOST;
  1135. // Fixed so that it gets sent
  1136. hr = m_lpSessionTransport->SendToAll( pBufferDesc, pvSendContext, DVTRANSPORT_SEND_GUARANTEED );
  1137. if( hr != DVERR_PENDING && hr != DV_OK )
  1138. {
  1139. DPFX(DPFPREP, 0, "Error sending connect request hr=0x%x", hr );
  1140. ReturnTransmitBuffer( pvSendContext );
  1141. }
  1142. // Pending = OK = expected
  1143. else
  1144. {
  1145. hr = DV_OK;
  1146. }
  1147. return hr;
  1148. }
  1149. #undef DPF_MODNAME
  1150. #define DPF_MODNAME "CDirectVoiceClientEngine::Send_ConnectRequest"
  1151. HRESULT CDirectVoiceClientEngine::Send_ConnectRequest()
  1152. {
  1153. PDVPROTOCOLMSG_CONNECTREQUEST pConnectRequest;
  1154. PDVTRANSPORT_BUFFERDESC pBufferDesc;
  1155. LPVOID pvSendContext;
  1156. HRESULT hr;
  1157. pBufferDesc = GetTransmitBuffer( sizeof( DVPROTOCOLMSG_CONNECTREQUEST ), &pvSendContext );
  1158. if( pBufferDesc == NULL )
  1159. {
  1160. return DVERR_OUTOFMEMORY;
  1161. }
  1162. pConnectRequest = (PDVPROTOCOLMSG_CONNECTREQUEST) pBufferDesc->pBufferData;
  1163. // Send connection request to the server
  1164. pConnectRequest->dwType = DVMSGID_CONNECTREQUEST;
  1165. pConnectRequest->ucVersionMajor = DVPROTOCOL_VERSION_MAJOR;
  1166. pConnectRequest->ucVersionMinor = DVPROTOCOL_VERSION_MINOR;
  1167. pConnectRequest->dwVersionBuild = DVPROTOCOL_VERSION_BUILD;
  1168. hr = m_lpSessionTransport->SendToServer( pBufferDesc, pvSendContext, DVTRANSPORT_SEND_GUARANTEED );
  1169. if( hr != DVERR_PENDING && hr != DV_OK )
  1170. {
  1171. DPFX(DPFPREP, 0, "Error sending connect request hr=0x%x", hr );
  1172. }
  1173. // Pending = OK = expected
  1174. else
  1175. {
  1176. hr = DV_OK;
  1177. }
  1178. return hr;
  1179. }
  1180. #undef DPF_MODNAME
  1181. #define DPF_MODNAME "CDirectVoiceClientEngine::Send_DisconnectRequest"
  1182. HRESULT CDirectVoiceClientEngine::Send_DisconnectRequest()
  1183. {
  1184. PDVPROTOCOLMSG_GENERIC pDisconnectRequest;
  1185. PDVTRANSPORT_BUFFERDESC pBufferDesc;
  1186. LPVOID pvSendContext;
  1187. HRESULT hr;
  1188. pBufferDesc = GetTransmitBuffer( sizeof( DVPROTOCOLMSG_GENERIC ), &pvSendContext );
  1189. if( pBufferDesc == NULL )
  1190. {
  1191. return DVERR_OUTOFMEMORY;
  1192. }
  1193. pDisconnectRequest = (PDVPROTOCOLMSG_GENERIC) pBufferDesc->pBufferData;
  1194. // Send connection request to the server
  1195. pDisconnectRequest->dwType = DVMSGID_DISCONNECT;
  1196. hr = m_lpSessionTransport->SendToServer( pBufferDesc, pvSendContext, DVTRANSPORT_SEND_GUARANTEED );
  1197. if( hr != DVERR_PENDING && hr != DV_OK )
  1198. {
  1199. DPFX(DPFPREP, 0, "Error sending connect request hr=0x%x", hr );
  1200. }
  1201. // Pending = OK = expected
  1202. else
  1203. {
  1204. hr = DV_OK;
  1205. }
  1206. return hr;
  1207. }
  1208. #undef DPF_MODNAME
  1209. #define DPF_MODNAME "CDirectVoiceClientEngine::Send_SettingsConfirm"
  1210. HRESULT CDirectVoiceClientEngine::Send_SettingsConfirm()
  1211. {
  1212. PDVPROTOCOLMSG_SETTINGSCONFIRM pSettingsConfirm;
  1213. PDVTRANSPORT_BUFFERDESC pBufferDesc;
  1214. LPVOID pvSendContext;
  1215. HRESULT hr;
  1216. pBufferDesc = GetTransmitBuffer( sizeof( DVPROTOCOLMSG_SETTINGSCONFIRM ), &pvSendContext );
  1217. if( pBufferDesc == NULL )
  1218. {
  1219. return DVERR_OUTOFMEMORY;
  1220. }
  1221. pSettingsConfirm = (PDVPROTOCOLMSG_SETTINGSCONFIRM) pBufferDesc->pBufferData;
  1222. // Send connection request to the server
  1223. pSettingsConfirm->dwType = DVMSGID_SETTINGSCONFIRM;
  1224. pSettingsConfirm->dwHostOrderID = m_dwHostOrderID;
  1225. pSettingsConfirm->dwFlags = 0;
  1226. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX )
  1227. {
  1228. pSettingsConfirm->dwFlags |= DVPLAYERCAPS_HALFDUPLEX;
  1229. }
  1230. hr = m_lpSessionTransport->SendToServer( pBufferDesc, pvSendContext, DVTRANSPORT_SEND_GUARANTEED );
  1231. if( hr != DVERR_PENDING && hr != DV_OK )
  1232. {
  1233. DPFX(DPFPREP, 0, "Error sending connect request hr=0x%x", hr );
  1234. }
  1235. // Pending = OK = expected
  1236. else
  1237. {
  1238. hr = DV_OK;
  1239. }
  1240. return hr;
  1241. }
  1242. #undef DPF_MODNAME
  1243. #define DPF_MODNAME "CDirectVoiceClientEngine::Disconnect"
  1244. // Disconnect
  1245. //
  1246. // Implements the IDirectXVoiceClient::Disconnect function
  1247. //
  1248. // Locks Required:
  1249. // - Global Lock
  1250. //
  1251. // Called By:
  1252. // DVC_Disconnect
  1253. //
  1254. HRESULT CDirectVoiceClientEngine::Disconnect( DWORD dwFlags )
  1255. {
  1256. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  1257. DPFX(DPFPREP, DVF_APIPARAM, "dwFlags = 0x%x", dwFlags );
  1258. HRESULT hr;
  1259. if( dwFlags & ~(DVFLAGS_SYNC) )
  1260. {
  1261. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid flags specified" );
  1262. return DVERR_INVALIDFLAGS;
  1263. }
  1264. CDVCSLock guardLock(&m_csClassLock);
  1265. guardLock.Lock();
  1266. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1267. {
  1268. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not initialized" );
  1269. return DVERR_NOTINITIALIZED;
  1270. }
  1271. if( m_dwCurrentState == DVCSTATE_CONNECTING )
  1272. {
  1273. m_fDisconnectAsync = !(dwFlags & DVFLAGS_SYNC);
  1274. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "DVCE::Disconnect() Abort connection" );
  1275. // Handle Connect
  1276. SetConnectResult( DVERR_CONNECTABORTED );
  1277. SendConnectResult();
  1278. SetEvent( m_hConnectAck );
  1279. DoSignalDisconnect( DVERR_CONNECTABORTED );
  1280. guardLock.Unlock();
  1281. if( dwFlags & DVFLAGS_SYNC )
  1282. {
  1283. goto DISCONNECT_WAIT;
  1284. }
  1285. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Returning DVERR_CONNECTABORTING" );
  1286. return DVERR_CONNECTABORTING;
  1287. }
  1288. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "State Good.." );
  1289. if( m_dwCurrentState == DVCSTATE_DISCONNECTING )
  1290. {
  1291. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Already disconnecting. Waiting on current event" );
  1292. guardLock.Unlock();
  1293. if( dwFlags & DVFLAGS_SYNC )
  1294. {
  1295. goto DISCONNECT_WAIT;
  1296. }
  1297. return DVERR_ALREADYPENDING;
  1298. }
  1299. else if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1300. {
  1301. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Not Connected" );
  1302. DPFX(DPFPREP, DVF_APIPARAM, "Returning DVERR_NOTCONNECTED" );
  1303. return DVERR_NOTCONNECTED;
  1304. }
  1305. else
  1306. {
  1307. m_fDisconnectAsync = !(dwFlags & DVFLAGS_SYNC);
  1308. m_dwSynchBegin = GetTickCount();
  1309. // Set current state to disconnecting before we release the lock
  1310. SetCurrentState( DVCSTATE_DISCONNECTING );
  1311. guardLock.Unlock();
  1312. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnect request about to be sent" );
  1313. hr = Send_DisconnectRequest();
  1314. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnect request transmitted hr=0x%x", hr );
  1315. guardLock.Lock();
  1316. if( FAILED( hr ) )
  1317. {
  1318. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "DVCE::Disconnect - Error on send 0x%x", hr );
  1319. // Inform notify thread to disconnect, since send failed there won't be a confirm
  1320. SetEvent( m_hNotifyDisconnect );
  1321. hr = DV_OK;
  1322. }
  1323. else
  1324. {
  1325. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnect sent" );
  1326. }
  1327. }
  1328. guardLock.Unlock();
  1329. if( dwFlags & DVFLAGS_SYNC )
  1330. {
  1331. goto DISCONNECT_WAIT;
  1332. }
  1333. DPFX(DPFPREP, DVF_INFOLEVEL, "Returning DVERR_PENDING" );
  1334. return DVERR_PENDING;
  1335. // You should have dropped the Write Loc+k by now
  1336. DISCONNECT_WAIT:
  1337. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Sync flag, waiting for completion." );
  1338. WaitForSingleObject( m_hDisconnectAck, INFINITE );
  1339. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Server response received" );
  1340. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnect Result = 0x%x", m_hrDisconnectResult );
  1341. return m_hrDisconnectResult;
  1342. }
  1343. #undef DPF_MODNAME
  1344. #define DPF_MODNAME "CDirectVoiceClientEngine::GetSessionDesc"
  1345. // GetSessionDesc
  1346. //
  1347. // Retrieves the current session description.
  1348. //
  1349. // Called By:
  1350. // DVC_GetSessionDesc
  1351. //
  1352. // Locks Required:
  1353. // - Global Read Lock
  1354. //
  1355. HRESULT CDirectVoiceClientEngine::GetSessionDesc( PDVSESSIONDESC lpSessionDesc )
  1356. {
  1357. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  1358. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1359. DPFX(DPFPREP, DVF_APIPARAM, "lpSessionDescBuffer = 0x%p", lpSessionDesc );
  1360. if( lpSessionDesc == NULL || !DNVALID_WRITEPTR(lpSessionDesc,sizeof( DVSESSIONDESC )) )
  1361. {
  1362. DPFX(DPFPREP, DVF_ERRORLEVEL, "Session desc pointer bad" );
  1363. return DVERR_INVALIDPOINTER;
  1364. }
  1365. if( lpSessionDesc->dwSize != sizeof( DVSESSIONDESC ) )
  1366. {
  1367. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid size on session desc" );
  1368. return DVERR_INVALIDPARAM;
  1369. }
  1370. CDVCSLock guardLock(&m_csClassLock);
  1371. guardLock.Lock();
  1372. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1373. {
  1374. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initalized" );
  1375. return DVERR_NOTINITIALIZED;
  1376. }
  1377. if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1378. {
  1379. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not connected" );
  1380. return DVERR_NOTCONNECTED;
  1381. }
  1382. memcpy( lpSessionDesc, &m_dvSessionDesc, sizeof( DVSESSIONDESC ) );
  1383. DV_DUMP_SD( (LPDVSESSIONDESC) lpSessionDesc );
  1384. DPFX(DPFPREP, DVF_APIPARAM, "Returning DV_OK" );
  1385. return DV_OK;
  1386. }
  1387. #undef DPF_MODNAME
  1388. #define DPF_MODNAME "CDirectVoiceClientEngine::GetSoundDeviceConfig"
  1389. // GetSoundDeviceConfig
  1390. //
  1391. // Retrieves the current client configuration.
  1392. //
  1393. // Called By:
  1394. // DVC_GetSoundDeviceConfig
  1395. //
  1396. // Locks Required:
  1397. // - Global Read Lock
  1398. HRESULT CDirectVoiceClientEngine::GetSoundDeviceConfig( PDVSOUNDDEVICECONFIG pSoundDeviceConfig, PDWORD pdwBufferSize )
  1399. {
  1400. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  1401. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1402. DPFX(DPFPREP, DVF_APIPARAM, "pSoundDeviceConfig = 0x%p", pSoundDeviceConfig );
  1403. DWORD dwRequiredSize = sizeof(DVSOUNDDEVICECONFIG);
  1404. if( pdwBufferSize == NULL ||
  1405. !DNVALID_WRITEPTR(pdwBufferSize,sizeof(DWORD)) )
  1406. {
  1407. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  1408. return DVERR_INVALIDPOINTER;
  1409. }
  1410. if( pSoundDeviceConfig != NULL &&
  1411. !DNVALID_WRITEPTR(pSoundDeviceConfig,*pdwBufferSize ) )
  1412. {
  1413. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  1414. return DVERR_INVALIDPOINTER;
  1415. }
  1416. if( pSoundDeviceConfig != NULL && pSoundDeviceConfig->dwSize != sizeof( DVSOUNDDEVICECONFIG ) )
  1417. {
  1418. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid Size on clientconfig" );
  1419. return DVERR_INVALIDPARAM;
  1420. }
  1421. CDVCSLock guardLock(&m_csClassLock);
  1422. guardLock.Lock();
  1423. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1424. {
  1425. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initialized" );
  1426. return DVERR_NOTINITIALIZED;
  1427. }
  1428. if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1429. {
  1430. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not Connected" );
  1431. return DVERR_NOTCONNECTED;
  1432. }
  1433. if( *pdwBufferSize < dwRequiredSize || pSoundDeviceConfig == NULL )
  1434. {
  1435. DPFX(DPFPREP, DVF_ERRORLEVEL, "Buffer too small!" );
  1436. *pdwBufferSize = dwRequiredSize;
  1437. return DVERR_BUFFERTOOSMALL;
  1438. }
  1439. memcpy( pSoundDeviceConfig, &m_dvSoundDeviceConfig, sizeof( DVSOUNDDEVICECONFIG ) );
  1440. DV_DUMP_SDC( pSoundDeviceConfig );
  1441. // # of bytes written
  1442. *pdwBufferSize = dwRequiredSize;
  1443. DPFX(DPFPREP, DVF_ENTRYLEVEL, "End" );
  1444. DPFX(DPFPREP, DVF_APIPARAM, "Returning DV_OK" );
  1445. return DV_OK;
  1446. }
  1447. #undef DPF_MODNAME
  1448. #define DPF_MODNAME "CDirectVoiceClientEngine::GetClientConfig"
  1449. // GetClientConfig
  1450. //
  1451. // Retrieves the current client configuration.
  1452. //
  1453. // Called By:
  1454. // DVC_GetClientConfig
  1455. //
  1456. // Locks Required:
  1457. // - Global Read Lock
  1458. HRESULT CDirectVoiceClientEngine::GetClientConfig( LPDVCLIENTCONFIG lpClientConfig )
  1459. {
  1460. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  1461. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1462. DPFX(DPFPREP, DVF_APIPARAM, "lpClientConfig = 0x%p", lpClientConfig );
  1463. if( lpClientConfig == NULL ||
  1464. !DNVALID_WRITEPTR(lpClientConfig,sizeof(DVCLIENTCONFIG) ) )
  1465. {
  1466. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  1467. return E_POINTER;
  1468. }
  1469. if( lpClientConfig->dwSize != sizeof( DVCLIENTCONFIG ) )
  1470. {
  1471. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid Size on clientconfig" );
  1472. return DVERR_INVALIDPARAM;
  1473. }
  1474. CDVCSLock guardLock(&m_csClassLock);
  1475. guardLock.Lock();
  1476. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1477. {
  1478. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initialized" );
  1479. return DVERR_NOTINITIALIZED;
  1480. }
  1481. if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1482. {
  1483. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not Connected" );
  1484. return DVERR_NOTCONNECTED;
  1485. }
  1486. memcpy( lpClientConfig, &m_dvClientConfig, sizeof( DVCLIENTCONFIG ) );
  1487. if( lpClientConfig->dwFlags & DVCLIENTCONFIG_AUTOVOICEACTIVATED )
  1488. {
  1489. lpClientConfig->dwThreshold = DVTHRESHOLD_UNUSED;
  1490. }
  1491. DV_DUMP_CC( lpClientConfig );
  1492. DPFX(DPFPREP, DVF_ENTRYLEVEL, "End" );
  1493. DPFX(DPFPREP, DVF_APIPARAM, "Returning DV_OK" );
  1494. return DV_OK;
  1495. }
  1496. #undef DPF_MODNAME
  1497. #define DPF_MODNAME "CDirectVoiceClientEngine::SetClientConfig"
  1498. // SetClientConfig
  1499. //
  1500. // Sets the current client configuration.
  1501. //
  1502. // Called By:
  1503. // DVC_SetClientConfig
  1504. //
  1505. // Locks Required:
  1506. // - Global Write Lock
  1507. //
  1508. HRESULT CDirectVoiceClientEngine::SetClientConfig( LPDVCLIENTCONFIG lpClientConfig )
  1509. {
  1510. HRESULT hr;
  1511. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Enter" );
  1512. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1513. DPFX(DPFPREP, DVF_APIPARAM, "lpClientConfig=0x%p", lpClientConfig );
  1514. if( lpClientConfig == NULL ||
  1515. !DNVALID_READPTR(lpClientConfig,sizeof(DVCLIENTCONFIG)))
  1516. {
  1517. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  1518. return E_POINTER;
  1519. }
  1520. DV_DUMP_CC( lpClientConfig );
  1521. if( lpClientConfig->dwSize != sizeof( DVCLIENTCONFIG ) )
  1522. {
  1523. DPFX(DPFPREP, DVF_ERRORLEVEL, "DVCE::SetClientConfig() Error parameters" );
  1524. return DVERR_INVALIDPARAM;
  1525. }
  1526. hr = DV_ValidClientConfig( lpClientConfig );
  1527. if( FAILED( hr ) )
  1528. {
  1529. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error validating Clientconfig hr=0x%x", hr );
  1530. return hr;
  1531. }
  1532. CDVCSLock guardLock(&m_csClassLock);
  1533. guardLock.Lock();
  1534. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1535. {
  1536. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not initialized" );
  1537. return DVERR_NOTINITIALIZED;
  1538. }
  1539. if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1540. {
  1541. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not connected" );
  1542. return DVERR_NOTCONNECTED;
  1543. }
  1544. BOOL bNotifyChange = FALSE,
  1545. bPlaybackChange = FALSE,
  1546. bRecordChange = FALSE,
  1547. bSensitivityChange = FALSE;
  1548. // If we're not half duplex, take care of the volume settings
  1549. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX ) &&
  1550. !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_NORECVOLAVAILABLE) )
  1551. {
  1552. if( lpClientConfig->lRecordVolume != DVRECORDVOLUME_LAST)
  1553. {
  1554. m_dvClientConfig.lRecordVolume = lpClientConfig->lRecordVolume;
  1555. m_audioRecordBuffer->SetVolume( m_dvClientConfig.lRecordVolume );
  1556. }
  1557. }
  1558. if( m_dvClientConfig.lPlaybackVolume != lpClientConfig->lPlaybackVolume )
  1559. {
  1560. m_dvClientConfig.lPlaybackVolume = lpClientConfig->lPlaybackVolume;
  1561. SetPlaybackVolume( m_dvClientConfig.lPlaybackVolume );
  1562. }
  1563. if( m_dvClientConfig.dwNotifyPeriod != lpClientConfig->dwNotifyPeriod )
  1564. {
  1565. m_dvClientConfig.dwNotifyPeriod = lpClientConfig->dwNotifyPeriod;
  1566. SetEvent( m_hNotifyChange );
  1567. }
  1568. if( !(lpClientConfig->dwFlags & DVCLIENTCONFIG_MANUALVOICEACTIVATED ) )
  1569. {
  1570. m_dvClientConfig.dwThreshold = DVTHRESHOLD_UNUSED;
  1571. }
  1572. else if( m_dvClientConfig.dwThreshold != lpClientConfig->dwThreshold )
  1573. {
  1574. if( lpClientConfig->dwThreshold == DVTHRESHOLD_DEFAULT )
  1575. {
  1576. m_dvClientConfig.dwThreshold = s_dwDefaultSensitivity;
  1577. }
  1578. else
  1579. {
  1580. m_dvClientConfig.dwThreshold = lpClientConfig->dwThreshold;
  1581. }
  1582. }
  1583. if( (lpClientConfig->dwFlags & DVCLIENTCONFIG_ECHOSUPPRESSION) !=
  1584. (m_dvClientConfig.dwFlags & DVCLIENTCONFIG_ECHOSUPPRESSION) )
  1585. {
  1586. DNEnterCriticalSection( &m_lockPlaybackMode );
  1587. m_dwEchoState = DVCECHOSTATE_IDLE;
  1588. DNLeaveCriticalSection( &m_lockPlaybackMode );
  1589. }
  1590. if( m_dvClientConfig.dwBufferAggressiveness == DVBUFFERAGGRESSIVENESS_DEFAULT )
  1591. {
  1592. m_dvClientConfig.dwBufferAggressiveness = s_dwDefaultBufferAggressiveness;
  1593. }
  1594. if( m_dvClientConfig.dwBufferQuality == DVBUFFERQUALITY_DEFAULT )
  1595. {
  1596. m_dvClientConfig.dwBufferQuality = s_dwDefaultBufferQuality;
  1597. }
  1598. // If we haven't specified NOFOCUS and we're not half duplex
  1599. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX) &&
  1600. !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_NOFOCUS) )
  1601. {
  1602. // If the settings have changed
  1603. if( (m_dvClientConfig.dwFlags & DVCLIENTCONFIG_RECORDMUTE) !=
  1604. (lpClientConfig->dwFlags & DVCLIENTCONFIG_RECORDMUTE) )
  1605. {
  1606. if( lpClientConfig->dwFlags & DVCLIENTCONFIG_RECORDMUTE )
  1607. {
  1608. DPFX(DPFPREP, DVF_INFOLEVEL, "Record Muted: Yielding focus" );
  1609. hr = m_audioRecordBuffer->YieldFocus();
  1610. }
  1611. else
  1612. {
  1613. DPFX(DPFPREP, DVF_INFOLEVEL, "Record Un-Muted: Attempting to reclaim focus" );
  1614. hr = m_audioRecordBuffer->ClaimFocus();
  1615. }
  1616. if( FAILED( hr ) )
  1617. {
  1618. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Focus set failed hr=0x%x", hr );
  1619. }
  1620. }
  1621. }
  1622. m_dvClientConfig.dwFlags = lpClientConfig->dwFlags;
  1623. m_dvClientConfig.dwNotifyPeriod = lpClientConfig->dwNotifyPeriod;
  1624. guardLock.Unlock();
  1625. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Returning DV_OK" );
  1626. return DV_OK;
  1627. }
  1628. #undef DPF_MODNAME
  1629. #define DPF_MODNAME "CDirectVoiceClientEngine::GetCaps"
  1630. //
  1631. // GetCaps
  1632. //
  1633. // This function retrieves the caps structure for this DirectPlayVoiceClient
  1634. // object.
  1635. //
  1636. // Called By:
  1637. // - DVC_GetCaps
  1638. //
  1639. // Locks Required:
  1640. // - Global Read Lock
  1641. //
  1642. HRESULT CDirectVoiceClientEngine::GetCaps( LPDVCAPS lpCaps )
  1643. {
  1644. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::GetCaps() Begin" );
  1645. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1646. DPFX(DPFPREP, DVF_APIPARAM, "Params: lpCaps: 0x%p", lpCaps );
  1647. CDVCSLock guardLock(&m_csClassLock);
  1648. if( lpCaps == NULL ||
  1649. !DNVALID_WRITEPTR(lpCaps,sizeof(DVCAPS)))
  1650. {
  1651. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  1652. return DVERR_INVALIDPOINTER;
  1653. }
  1654. if( lpCaps->dwSize != sizeof( DVCAPS ) )
  1655. {
  1656. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error parameters" );
  1657. return DVERR_INVALIDPARAM;
  1658. }
  1659. guardLock.Lock();
  1660. memcpy( lpCaps, &m_dvCaps, sizeof( DVCAPS ) );
  1661. guardLock.Unlock();
  1662. DV_DUMP_CAPS( lpCaps );
  1663. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  1664. return DV_OK;
  1665. }
  1666. #undef DPF_MODNAME
  1667. #define DPF_MODNAME "CDirectVoiceClientEngine::GetCompressionTypes"
  1668. //
  1669. // GetCompressionTypes
  1670. //
  1671. // Retrieves configured compression types for this object.
  1672. //
  1673. // Called By:
  1674. // - DVC_GetCompressionTypes
  1675. //
  1676. // Locks Required:
  1677. // - Global Read Lock
  1678. //
  1679. HRESULT CDirectVoiceClientEngine::GetCompressionTypes( LPVOID lpBuffer, LPDWORD lpdwSize, LPDWORD lpdwNumElements, DWORD dwFlags )
  1680. {
  1681. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Begin" );
  1682. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1683. DPFX(DPFPREP, DVF_APIPARAM, "Params: lpBuffer = 0x%p lpdwSize = 0x%p lpdwNumElements = 0x%p, dwFlags = 0x%x",
  1684. lpBuffer, lpdwSize, lpdwNumElements, dwFlags );
  1685. CDVCSLock guardLock(&m_csClassLock);
  1686. guardLock.Lock();
  1687. HRESULT hres = DVCDB_CopyCompressionArrayToBuffer( lpBuffer, lpdwSize, lpdwNumElements, dwFlags );
  1688. guardLock.Unlock();
  1689. if( hres == DV_OK )
  1690. {
  1691. DV_DUMP_CI( (LPDVCOMPRESSIONINFO) lpBuffer, *lpdwNumElements );
  1692. }
  1693. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  1694. return hres;
  1695. }
  1696. #undef DPF_MODNAME
  1697. #define DPF_MODNAME "CDirectVoiceClientEngine::CheckForAndRemoveTarget"
  1698. //
  1699. // CheckForAndRemoveTarget
  1700. //
  1701. // Checks the current target list for the specified ID and removes it from
  1702. // the target list if it is in the target list.
  1703. //
  1704. HRESULT CDirectVoiceClientEngine::CheckForAndRemoveTarget( DVID dvidID )
  1705. {
  1706. HRESULT hr = DV_OK;
  1707. DNEnterCriticalSection( &m_csTargetLock );
  1708. // Search the list of targets
  1709. for( DWORD dwIndex = 0; dwIndex < m_dwNumTargets; dwIndex++ )
  1710. {
  1711. if( m_pdvidTargets[dwIndex] == dvidID )
  1712. {
  1713. if( m_dwNumTargets == 1 )
  1714. {
  1715. hr = InternalSetTransmitTarget( NULL, 0 );
  1716. }
  1717. // We'll re-use the current target array
  1718. else
  1719. {
  1720. // Collapse the list by either ommiting the last element (if the
  1721. // one we want to remove is last, or by moving last element into
  1722. // the place in the list we're removing.
  1723. if( dwIndex+1 != m_dwNumTargets )
  1724. {
  1725. m_pdvidTargets[dwIndex] = m_pdvidTargets[m_dwNumTargets-1];
  1726. }
  1727. hr = InternalSetTransmitTarget( m_pdvidTargets, m_dwNumTargets-1 );
  1728. }
  1729. break;
  1730. }
  1731. }
  1732. DNLeaveCriticalSection( &m_csTargetLock );
  1733. return DV_OK;
  1734. }
  1735. #undef DPF_MODNAME
  1736. #define DPF_MODNAME "CDirectVoiceClientEngine::InternalSetTransmitTarget"
  1737. //
  1738. // InternalSetTransmitTarget
  1739. //
  1740. // Does the work of setting the target. (Assumes values have been validated).
  1741. //
  1742. // This function is safe to pass a pointer to the current target array. It works
  1743. // on a temporary.
  1744. //
  1745. HRESULT CDirectVoiceClientEngine::InternalSetTransmitTarget( PDVID pdvidTargets, DWORD dwNumTargets )
  1746. {
  1747. DWORD dwRequiredSize;
  1748. PBYTE pbDataBuffer;
  1749. PDVMSG_SETTARGETS pdvSetTarget;
  1750. DNEnterCriticalSection( &m_csTargetLock );
  1751. // No targets? set list to NULL
  1752. if( dwNumTargets == 0 )
  1753. {
  1754. // Close memory leak
  1755. //
  1756. // Hawk Bug #
  1757. //
  1758. if( m_pdvidTargets != NULL )
  1759. {
  1760. delete [] m_pdvidTargets;
  1761. }
  1762. m_pdvidTargets = NULL;
  1763. }
  1764. // Otherwise allocate new list and copy
  1765. else
  1766. {
  1767. PDVID pTmpTargetList;
  1768. pTmpTargetList = new DVID[dwNumTargets];
  1769. if( pTmpTargetList == NULL )
  1770. {
  1771. delete [] m_pdvidTargets;
  1772. m_dwNumTargets = 0;
  1773. DPFX(DPFPREP, DVF_ERRORLEVEL, "Memory alloc failure" );
  1774. DNLeaveCriticalSection( &m_csTargetLock );
  1775. return DVERR_OUTOFMEMORY;
  1776. }
  1777. memcpy( pTmpTargetList, pdvidTargets, dwNumTargets*sizeof(DVID) );
  1778. // Kill off old target list
  1779. if( m_pdvidTargets != NULL )
  1780. delete [] m_pdvidTargets;
  1781. m_pdvidTargets = pTmpTargetList;
  1782. }
  1783. m_dwNumTargets = dwNumTargets;
  1784. m_dwTargetVersion++;
  1785. dwRequiredSize = m_dwNumTargets * sizeof( DVID );
  1786. dwRequiredSize += sizeof( DVMSG_SETTARGETS );
  1787. pbDataBuffer = new BYTE[dwRequiredSize];
  1788. if( pbDataBuffer == NULL )
  1789. {
  1790. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error allocating memory!" );
  1791. DNLeaveCriticalSection( &m_csTargetLock );
  1792. return TRUE;
  1793. }
  1794. pdvSetTarget = (PDVMSG_SETTARGETS) pbDataBuffer;
  1795. pdvSetTarget->pdvidTargets = (PDVID) (pbDataBuffer+sizeof(DVMSG_SETTARGETS));
  1796. pdvSetTarget->dwNumTargets = m_dwNumTargets;
  1797. pdvSetTarget->dwSize = sizeof( DVMSG_SETTARGETS );
  1798. memcpy( pdvSetTarget->pdvidTargets, m_pdvidTargets, sizeof(DVID)*m_dwNumTargets );
  1799. NotifyQueue_Add( DVMSGID_SETTARGETS, pdvSetTarget, dwRequiredSize );
  1800. delete [] pbDataBuffer;
  1801. DNLeaveCriticalSection( &m_csTargetLock );
  1802. return DV_OK;
  1803. }
  1804. #undef DPF_MODNAME
  1805. #define DPF_MODNAME "CDirectVoiceClientEngine::SetTransmitTarget"
  1806. //
  1807. // SetTransmitTarget
  1808. //
  1809. // Sets the current transmit target.
  1810. //
  1811. // Called by:
  1812. // - DVC_SetTransmitTarget
  1813. //
  1814. // Locks Required:
  1815. // - Global Write Lock
  1816. //
  1817. HRESULT CDirectVoiceClientEngine::SetTransmitTarget( PDVID pdvidTargets, DWORD dwNumTargets, DWORD dwFlags )
  1818. {
  1819. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Begin" );
  1820. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1821. DPFX(DPFPREP, DVF_APIPARAM, "Params: pdvidTargets = 0x%p dwNumTargets = %d dwFlags = 0x%x", pdvidTargets, dwNumTargets, dwFlags );
  1822. if( dwFlags != 0 )
  1823. {
  1824. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid flags" );
  1825. return DVERR_INVALIDFLAGS;
  1826. }
  1827. HRESULT hr;
  1828. // Check that the target list is valid
  1829. hr = DV_ValidTargetList( pdvidTargets, dwNumTargets );
  1830. if( FAILED( hr ) )
  1831. {
  1832. DPFX(DPFPREP, DVF_ERRORLEVEL, "Target list is not valid" );
  1833. return hr;
  1834. }
  1835. DWORD dwIndex;
  1836. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1837. {
  1838. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initialized" );
  1839. return DVERR_NOTINITIALIZED;
  1840. }
  1841. if( m_dwCurrentState != DVCSTATE_CONNECTED &&
  1842. m_dwCurrentState != DVCSTATE_CONNECTING )
  1843. {
  1844. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not connected" );
  1845. return DVERR_NOTCONNECTED;
  1846. }
  1847. else
  1848. {
  1849. if( m_dvSessionDesc.dwFlags & DVSESSION_SERVERCONTROLTARGET )
  1850. {
  1851. DPFX(DPFPREP, DVF_ERRORLEVEL, "Denied. Server controlled target" );
  1852. return DVERR_NOTALLOWED;
  1853. }
  1854. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER )
  1855. {
  1856. // Loop through target list, confirm they are valid entries
  1857. for( dwIndex = 0; dwIndex < dwNumTargets; dwIndex++ )
  1858. {
  1859. if( !m_voiceNameTable.IsEntry(pdvidTargets[dwIndex]) )
  1860. {
  1861. if( !m_lpSessionTransport->ConfirmValidGroup( pdvidTargets[dwIndex] ) )
  1862. {
  1863. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid target" );
  1864. return DVERR_INVALIDTARGET;
  1865. }
  1866. }
  1867. }
  1868. }
  1869. }
  1870. hr = InternalSetTransmitTarget( pdvidTargets, dwNumTargets );
  1871. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  1872. return hr;
  1873. }
  1874. #undef DPF_MODNAME
  1875. #define DPF_MODNAME "CDirectVoiceClientEngine::GetTransmitTarget"
  1876. //
  1877. // GetTransmitTarget
  1878. //
  1879. // Retrieves the current transmission target.
  1880. //
  1881. // Called By:
  1882. // - DVC_GetTransmitTarget
  1883. //
  1884. // Locks Required:
  1885. // - Read Lock Required
  1886. //
  1887. HRESULT CDirectVoiceClientEngine::GetTransmitTarget( LPDVID lpdvidTargets, PDWORD pdwNumElements, DWORD dwFlags )
  1888. {
  1889. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Begin" );
  1890. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  1891. DPFX(DPFPREP, DVF_APIPARAM, "Params: lpdvidTargets = 0x%p pdwNumElements = 0x%x dwFlags = 0x%x", lpdvidTargets, pdwNumElements, dwFlags );
  1892. if( pdwNumElements == NULL ||
  1893. !DNVALID_WRITEPTR( pdwNumElements, sizeof( DWORD ) ) )
  1894. {
  1895. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer to num of elements" );
  1896. return DVERR_INVALIDPOINTER;
  1897. }
  1898. if( pdwNumElements != NULL &&
  1899. *pdwNumElements > 0 &&
  1900. !DNVALID_WRITEPTR( lpdvidTargets, (*pdwNumElements) * sizeof( DVID) ) )
  1901. {
  1902. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid target list buffer specified" );
  1903. return DVERR_INVALIDPOINTER;
  1904. }
  1905. if( pdwNumElements == NULL )
  1906. {
  1907. DPFX(DPFPREP, DVF_ERRORLEVEL, "Must specify a ptr for # of elements" );
  1908. return DVERR_INVALIDPARAM;
  1909. }
  1910. if( dwFlags != 0 )
  1911. {
  1912. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid flags" );
  1913. return DVERR_INVALIDFLAGS;
  1914. }
  1915. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  1916. {
  1917. DPFX(DPFPREP, DVF_ERRORLEVEL, "Object not initialized" );
  1918. return DVERR_NOTINITIALIZED;
  1919. }
  1920. else if( m_dwCurrentState != DVCSTATE_CONNECTED )
  1921. {
  1922. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not Connected" );
  1923. DPFX(DPFPREP, DVF_APIPARAM, "Returning DVERR_NOTCONNECTED" );
  1924. return DVERR_NOTCONNECTED;
  1925. }
  1926. HRESULT hr = DV_OK;
  1927. DNEnterCriticalSection( &m_csTargetLock );
  1928. if( *pdwNumElements < m_dwNumTargets )
  1929. {
  1930. hr = DVERR_BUFFERTOOSMALL;
  1931. }
  1932. else
  1933. {
  1934. memcpy( lpdvidTargets, m_pdvidTargets,m_dwNumTargets*sizeof(DVID) );
  1935. }
  1936. *pdwNumElements = m_dwNumTargets;
  1937. DNLeaveCriticalSection( &m_csTargetLock );
  1938. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Success" );
  1939. return hr;
  1940. }
  1941. #undef DPF_MODNAME
  1942. #define DPF_MODNAME "CDirectVoiceClientEngine::InitSoundTargetList"
  1943. //
  1944. // InitSoundTargetList
  1945. //
  1946. // Initializes the sound target list.
  1947. //
  1948. // Called By:
  1949. // - InitializeSoundSystem
  1950. //
  1951. // Locks Required:
  1952. // - Buffer Lock
  1953. //
  1954. HRESULT CDirectVoiceClientEngine::InitSoundTargetList()
  1955. {
  1956. DNEnterCriticalSection( &m_csBufferLock );
  1957. m_lpstBufferList = NULL;
  1958. DNLeaveCriticalSection( &m_csBufferLock );
  1959. return DV_OK;
  1960. }
  1961. #undef DPF_MODNAME
  1962. #define DPF_MODNAME "CDirectVoiceClientEngine::FreeSoundTargetList"
  1963. //
  1964. // FreeSoundTargetList
  1965. //
  1966. // Releases the sound target list.
  1967. //
  1968. // Also cleans up buffers not released by the user. This must be called before the playback system
  1969. // is shutdown.
  1970. //
  1971. // Called By:
  1972. // - DeInitializeSoundSystem
  1973. //
  1974. // Locks Required:
  1975. // - Buffer Lock
  1976. //
  1977. HRESULT CDirectVoiceClientEngine::FreeSoundTargetList()
  1978. {
  1979. CSoundTarget *lpctFinder, *lpctLast;
  1980. LONG lRefCount;
  1981. DVID dvid;
  1982. DNEnterCriticalSection( &m_csBufferLock );
  1983. lpctLast = NULL;
  1984. lpctFinder = m_lpstBufferList;
  1985. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: DESTROYING OPEN TARGET OBJECTS" );
  1986. CHECKLISTINTEGRITY();
  1987. // If we enter this loop we're in a questionable state.
  1988. //
  1989. // The user hasn't called Delete3DSoundBuffer on one or more buffers
  1990. //
  1991. // We're going to cleanup, if they attempt to access the pointers after this point
  1992. // the app will access violate.
  1993. //
  1994. while( lpctFinder != NULL )
  1995. {
  1996. lpctLast = lpctFinder;
  1997. lpctFinder = lpctFinder->m_lpstNext;
  1998. lRefCount = lpctLast->GetRefCount();
  1999. DNASSERT( lRefCount == 2 );
  2000. CHECKLISTINTEGRITY();
  2001. DPFX(DPFPREP, DVF_ERRORLEVEL, "=========================================================================" );
  2002. DPFX(DPFPREP, DVF_ERRORLEVEL, "3D SoundBuffer for ID 0x%x was not released, cleaning it up", lpctLast->GetTarget() );
  2003. DPFX(DPFPREP, DVF_ERRORLEVEL, "This is an ERROR. You must Delete3DSoundBuffer before closing the interface." );
  2004. DPFX(DPFPREP, DVF_ERRORLEVEL, "DirectPlayVoice has freed the resources, so if you access them you will crash." );
  2005. DPFX(DPFPREP, DVF_ERRORLEVEL, "=========================================================================" );
  2006. lpctLast->Release();
  2007. DeleteSoundTarget( lpctLast->GetTarget() );
  2008. CHECKLISTINTEGRITY();
  2009. }
  2010. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: DESTROYING GENERAL BUFFER" );
  2011. if( m_lpstGeneralBuffer != NULL )
  2012. {
  2013. // Release the core's reference to the buffer
  2014. lRefCount = m_lpstGeneralBuffer->Release();
  2015. // User must not have freed the buffer
  2016. if( lRefCount > 0 )
  2017. {
  2018. DNASSERT( lRefCount == 1 );
  2019. DPFX(DPFPREP, DVF_ERRORLEVEL, "=========================================================================" );
  2020. DPFX(DPFPREP, DVF_ERRORLEVEL, "Main 3D SoundBuffer was not released, cleaning it up" );
  2021. DPFX(DPFPREP, DVF_ERRORLEVEL, "This is an ERROR. You must Delete3DSoundBuffer before closing the interface." );
  2022. DPFX(DPFPREP, DVF_ERRORLEVEL, "DirectPlayVoice has freed the resources, so if you access them you will crash." );
  2023. DPFX(DPFPREP, DVF_ERRORLEVEL, "=========================================================================" );
  2024. m_lpstGeneralBuffer->Release();
  2025. m_lpstGeneralBuffer = NULL;
  2026. }
  2027. else
  2028. {
  2029. m_lpstGeneralBuffer = NULL;
  2030. }
  2031. // Releasing the buffer above released this object as well
  2032. m_audioPlaybackBuffer = NULL;
  2033. }
  2034. DNLeaveCriticalSection( &m_csBufferLock );
  2035. return DV_OK;
  2036. }
  2037. #undef DPF_MODNAME
  2038. #define DPF_MODNAME "CDirectVoiceClientEngine::AddSoundTarget"
  2039. //
  2040. // AddSoundTarget
  2041. //
  2042. // Adds a new target to the sound target list
  2043. //
  2044. // Called By:
  2045. // - CreateUserBuffer
  2046. //
  2047. // Locks Required:
  2048. // - Buffer Lock
  2049. //
  2050. HRESULT CDirectVoiceClientEngine::AddSoundTarget( CSoundTarget *lpcsTarget )
  2051. {
  2052. DNEnterCriticalSection( &m_csBufferLock );
  2053. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: [0x%x] ADDSOUNDTARGET", lpcsTarget->GetTarget() );
  2054. CHECKLISTINTEGRITY();
  2055. lpcsTarget->m_lpstNext = m_lpstBufferList;
  2056. m_lpstBufferList = lpcsTarget;
  2057. CHECKLISTINTEGRITY();
  2058. DNLeaveCriticalSection( &m_csBufferLock );
  2059. return DV_OK;
  2060. }
  2061. #undef DPF_MODNAME
  2062. #define DPF_MODNAME "CDirectVoiceClientEngine::CheckListIntegrity"
  2063. void CDirectVoiceClientEngine::CheckListIntegrity()
  2064. {
  2065. CSoundTarget *lpctFinder;
  2066. DNEnterCriticalSection( &m_csBufferLock );
  2067. lpctFinder = m_lpstBufferList;
  2068. while( lpctFinder != NULL )
  2069. {
  2070. if( lpctFinder != NULL )
  2071. {
  2072. DNASSERT( lpctFinder->m_dwSignature == VSIG_SOUNDTARGET );
  2073. DNASSERT( lpctFinder->GetRefCount() > 0 );
  2074. DNASSERT( lpctFinder->GetRefCount() <= 3 );
  2075. DNASSERT( lpctFinder->GetBuffer() != NULL );
  2076. }
  2077. lpctFinder = lpctFinder->m_lpstNext;
  2078. }
  2079. DNLeaveCriticalSection( &m_csBufferLock );
  2080. }
  2081. #undef DPF_MODNAME
  2082. #define DPF_MODNAME "CDirectVoiceClientEngine::DeleteSoundTarget"
  2083. //
  2084. // DeleteSoundTarget
  2085. //
  2086. // Removes the specified ID's entry from the sound target list
  2087. //
  2088. // Called By:
  2089. // - DeleteSoundTarget
  2090. //
  2091. // Locks Required:
  2092. // - Buffer lock
  2093. //
  2094. HRESULT CDirectVoiceClientEngine::DeleteSoundTarget( DVID dvidID )
  2095. {
  2096. CSoundTarget *lpctFinder, *lpctLast, *lpctNext;
  2097. LONG lRefCount;
  2098. DNEnterCriticalSection( &m_csBufferLock );
  2099. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: [0x%x] DELETESOUNDTARGET ", dvidID );
  2100. lpctLast = NULL;
  2101. lpctFinder = m_lpstBufferList;
  2102. while( lpctFinder != NULL )
  2103. {
  2104. CHECKLISTINTEGRITY();
  2105. if( lpctFinder->GetTarget() == dvidID )
  2106. {
  2107. // Store next pointer
  2108. lpctNext = lpctFinder->m_lpstNext;
  2109. // Release the reference the core has
  2110. // If this is the last reference, it destroys the object
  2111. //
  2112. // If user is holding reference this won't destroy
  2113. // the object, the cleanup will.
  2114. //
  2115. lRefCount = lpctFinder->Release();
  2116. // Only remove from list if reference count is 0.
  2117. //
  2118. // Otherwise you end up with buffer replaying old
  2119. // audio
  2120. //
  2121. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: [0x%x] REMOVING FROM LIST ", dvidID );
  2122. /* DNASSERT( lRefCount == 0 );
  2123. if( lRefCount == 0 )
  2124. { */
  2125. if( lpctLast == NULL )
  2126. {
  2127. m_lpstBufferList = lpctNext;
  2128. }
  2129. else
  2130. {
  2131. lpctLast->m_lpstNext = lpctNext;
  2132. }
  2133. // }
  2134. CHECKLISTINTEGRITY();
  2135. DNLeaveCriticalSection( &m_csBufferLock );
  2136. return DV_OK;
  2137. }
  2138. lpctLast = lpctFinder;
  2139. lpctFinder = lpctFinder->m_lpstNext;
  2140. }
  2141. DNLeaveCriticalSection( &m_csBufferLock );
  2142. return DVERR_INVALIDPLAYER;
  2143. }
  2144. #undef DPF_MODNAME
  2145. #define DPF_MODNAME "CDirectVoiceClientEngine::FindSoundTarget"
  2146. //
  2147. // FindSoundTarget
  2148. //
  2149. // Look for sound target buffer for the specified user.
  2150. //
  2151. // If it exists, return it in lpcsTarget
  2152. //
  2153. // Called By:
  2154. // - CreateUserBuffer
  2155. //
  2156. // Locks Required:
  2157. // - Buffer Lock
  2158. //
  2159. HRESULT CDirectVoiceClientEngine::FindSoundTarget( DVID dvidID, CSoundTarget **lpcsTarget )
  2160. {
  2161. DNEnterCriticalSection( &m_csBufferLock );
  2162. CHECKLISTINTEGRITY();
  2163. *lpcsTarget = NULL;
  2164. CSoundTarget *lpctFinder;
  2165. lpctFinder = m_lpstBufferList;
  2166. while( lpctFinder != NULL )
  2167. {
  2168. if( lpctFinder->GetTarget() == dvidID )
  2169. {
  2170. *lpcsTarget = lpctFinder;
  2171. lpctFinder->AddRef();
  2172. DNLeaveCriticalSection( &m_csBufferLock );
  2173. return DV_OK;
  2174. }
  2175. lpctFinder = lpctFinder->m_lpstNext;
  2176. }
  2177. CHECKLISTINTEGRITY();
  2178. DNLeaveCriticalSection( &m_csBufferLock );
  2179. return DVERR_INVALIDPLAYER;
  2180. }
  2181. #undef DPF_MODNAME
  2182. #define DPF_MODNAME "CDirectVoiceClientEngine::SetupPlaybackBufferDesc"
  2183. void CDirectVoiceClientEngine::SetupPlaybackBufferDesc( LPDSBUFFERDESC lpdsBufferDesc, LPDSBUFFERDESC lpdsBufferSource )
  2184. {
  2185. DV_SetupBufferDesc( lpdsBufferDesc, lpdsBufferSource, s_lpwfxPlaybackFormat, m_dwUnCompressedFrameSize*m_dwNumPerBuffer );
  2186. }
  2187. #undef DPF_MODNAME
  2188. #define DPF_MODNAME "CDirectVoiceClientEngine::Create3DSoundBuffer"
  2189. //
  2190. // Create3DSoundBuffer
  2191. //
  2192. // Creates a mixing buffer (a sound target) for the specified user ID.
  2193. //
  2194. // Called By:
  2195. // - DVC_CreateUserBuffer
  2196. //
  2197. // Locks Required:
  2198. // - Global Read Lock
  2199. //
  2200. HRESULT CDirectVoiceClientEngine::Create3DSoundBuffer( DVID dvidID, LPDIRECTSOUNDBUFFER lpdsBuffer, DWORD dwPriority, DWORD dwFlags, LPDIRECTSOUND3DBUFFER *lpBuffer )
  2201. {
  2202. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Begin" );
  2203. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  2204. DPFX(DPFPREP, DVF_APIPARAM, "Params: dvidID = 0x%x lpdsBuffer = 0x%p dwPriority = 0x%x dwFlags = 0x%x lpBuffer = 0x%p", dvidID, lpdsBuffer, dwPriority, dwFlags, lpBuffer );
  2205. HRESULT hr;
  2206. if( lpBuffer == NULL ||
  2207. !DNVALID_WRITEPTR( lpBuffer, sizeof( LPDIRECTSOUND3DBUFFER ) ) )
  2208. {
  2209. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  2210. return DVERR_INVALIDPOINTER;
  2211. }
  2212. if( dvidID == DVID_REMAINING )
  2213. {
  2214. if( lpdsBuffer != NULL )
  2215. {
  2216. DPFX(DPFPREP, DVF_ERRORLEVEL, "Cannot specify a buffer for the DVID_REMAINING buffer" );
  2217. DPFX(DPFPREP, DVF_ERRORLEVEL, "You can set these values from the SoundDeviceConfig structure" );
  2218. return DVERR_INVALIDPARAM;
  2219. }
  2220. if( dwFlags != 0 || dwPriority != 0 )
  2221. {
  2222. DPFX(DPFPREP, DVF_ERRORLEVEL, "Cannot specify non-zero flags for voice management for DVID_REMAINING buffer" );
  2223. DPFX(DPFPREP, DVF_ERRORLEVEL, "You can set these values from the SoundDeviceConfig structure" );
  2224. return DVERR_INVALIDFLAGS;
  2225. }
  2226. }
  2227. else
  2228. {
  2229. hr = DV_ValidBufferSettings( lpdsBuffer, dwPriority, dwFlags, s_lpwfxPlaybackFormat );
  2230. if( FAILED( hr ) )
  2231. {
  2232. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid settings for buffer description hr=0x%x", hr );
  2233. return hr;
  2234. }
  2235. dwFlags |= DSBPLAY_LOOPING;
  2236. }
  2237. CDVCSLock guardLock(&m_csClassLock);
  2238. guardLock.Lock();
  2239. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  2240. {
  2241. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not initialized" );
  2242. return DVERR_NOTINITIALIZED;
  2243. }
  2244. else if( m_dwCurrentState != DVCSTATE_CONNECTED )
  2245. {
  2246. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not connected" );
  2247. return DVERR_NOTCONNECTED;
  2248. }
  2249. if( this->m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_MIXING &&
  2250. dvidID != DVID_REMAINING )
  2251. {
  2252. DPFX(DPFPREP, DVF_ERRORLEVEL, "Only DVID_REMAINING can be spatialized in mixing sessions" );
  2253. return DVERR_NOTALLOWED;
  2254. }
  2255. if( dvidID == m_dvidLocal )
  2256. {
  2257. DPFX(DPFPREP, DVF_ERRORLEVEL, "Cannot create buffer for local player!" );
  2258. return DVERR_INVALIDPLAYER;
  2259. }
  2260. if( dvidID != DVID_ALLPLAYERS &&
  2261. dvidID != DVID_REMAINING &&
  2262. !m_voiceNameTable.IsEntry(dvidID) )
  2263. {
  2264. guardLock.Unlock();
  2265. if( !m_lpSessionTransport->ConfirmValidGroup( dvidID ) )
  2266. {
  2267. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid player/group ID" );
  2268. return DVERR_INVALIDPLAYER;
  2269. }
  2270. guardLock.Lock();
  2271. }
  2272. DWORD dwMode;
  2273. // Handle request for 3d buffer on the main buffer
  2274. if( dvidID == DVID_REMAINING )
  2275. {
  2276. LPDIRECTSOUND3DBUFFER lpds3dTmp;
  2277. lpds3dTmp = m_lpstGeneralBuffer->Get3DBuffer();
  2278. if( lpds3dTmp == NULL )
  2279. {
  2280. DPFX(DPFPREP, DVF_ERRORLEVEL, "No 3d support" );
  2281. return DVERR_NO3DSOUND;
  2282. }
  2283. hr = lpds3dTmp->GetMode( &dwMode );
  2284. if( FAILED( hr ) )
  2285. {
  2286. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to get 3d buffer mode hr=0x%x", hr );
  2287. return DVERR_GENERIC;
  2288. }
  2289. if( dwMode != DS3DMODE_DISABLE )
  2290. {
  2291. DPFX(DPFPREP, DVF_ERRORLEVEL, "Already have a buffer for specified user" );
  2292. return DVERR_ALREADYBUFFERED;
  2293. }
  2294. // Check return code
  2295. hr = lpds3dTmp->SetMode( DS3DMODE_NORMAL, DS3D_IMMEDIATE );
  2296. if( hr != DV_OK )
  2297. {
  2298. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to set the mode to activate 3d buffer hr=0x%x", hr );
  2299. return hr;
  2300. }
  2301. // Add a reference for the user (core already has one)
  2302. m_lpstGeneralBuffer->AddRef();
  2303. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  2304. *lpBuffer = lpds3dTmp;
  2305. return DV_OK;
  2306. }
  2307. LONG lResult;
  2308. CSoundTarget *lpstTarget = NULL;
  2309. // Check for existing buffer.. if it already exists return it
  2310. // (Note: Adds a reference to the buffer)
  2311. hr = FindSoundTarget( dvidID, &lpstTarget );
  2312. if( hr == DV_OK )
  2313. {
  2314. if( lpstTarget != NULL )
  2315. {
  2316. lResult = lpstTarget->Release();
  2317. DNASSERT( lResult != 0 );
  2318. }
  2319. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: Find of buffer failed. hr=0x%x", hr );
  2320. return DVERR_ALREADYBUFFERED;
  2321. }
  2322. if( lpstTarget != NULL )
  2323. {
  2324. lResult = lpstTarget->Release();
  2325. DNASSERT( lResult != 0 );
  2326. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: Buffer already available" );
  2327. return DVERR_ALREADYBUFFERED;
  2328. }
  2329. // If the user has given us a buffer
  2330. if( lpdsBuffer )
  2331. {
  2332. DPFX(DPFPREP, DVF_INFOLEVEL, "Creating buffer using user buffer" );
  2333. lpstTarget = new CSoundTarget( dvidID, m_audioPlaybackDevice, lpdsBuffer, (s_lpwfxPlaybackFormat->wBitsPerSample == 8), dwPriority, dwFlags, m_dwUnCompressedFrameSize );
  2334. }
  2335. else
  2336. {
  2337. DSBUFFERDESC dsBufferDesc;
  2338. DPFX(DPFPREP, DVF_INFOLEVEL, "Creating buffer using user buffer" );
  2339. // Fill in appropriate values for the buffer description
  2340. SetupPlaybackBufferDesc( &dsBufferDesc, NULL );
  2341. // Buffer and sound target ref count = 1
  2342. lpstTarget = new CSoundTarget( dvidID, m_audioPlaybackDevice, &dsBufferDesc, dwPriority, dwFlags, m_dwUnCompressedFrameSize );
  2343. }
  2344. if( lpstTarget == NULL )
  2345. {
  2346. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: Failed allocating sound target" );
  2347. return DVERR_OUTOFMEMORY;
  2348. }
  2349. hr = lpstTarget->GetInitResult();
  2350. if( FAILED( hr ) )
  2351. {
  2352. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: Init of buffer failed. hr=0x%x", hr );
  2353. lpstTarget->Release();
  2354. return hr;
  2355. }
  2356. hr = lpstTarget->StartMix();
  2357. if( FAILED( hr ) )
  2358. {
  2359. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to start the mix on secondary buffer hr=0x%x.", hr );
  2360. lpstTarget->Release();
  2361. return hr;
  2362. }
  2363. // Buffer and sound target ref count = 2
  2364. lpstTarget->AddRef();
  2365. hr = AddSoundTarget( lpstTarget );
  2366. if( FAILED( hr ) )
  2367. {
  2368. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: AddTarget failed. hr=0x%x", hr );
  2369. // Destroy reference from above
  2370. lResult = lpstTarget->Release();
  2371. DNASSERT( lResult != 0 );
  2372. // Destroy base reference
  2373. lResult = lpstTarget->Release();
  2374. DNASSERT( lResult == 0 );
  2375. return hr;
  2376. }
  2377. *lpBuffer = lpstTarget->Get3DBuffer();
  2378. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  2379. return DV_OK;
  2380. }
  2381. #undef DPF_MODNAME
  2382. #define DPF_MODNAME "CDirectVoiceClientEngine::Delete3DSoundBuffer"
  2383. //
  2384. // Delete3DSoundBuffer
  2385. //
  2386. // Removes the specified ID from the mixer buffer list. Further speech from
  2387. // the specified player will be played in the remaining buffer.
  2388. //
  2389. // Called By:
  2390. // - DVC_DeleteUserBuffer
  2391. //
  2392. // Locks Required:
  2393. // - Global Write Lock
  2394. //
  2395. HRESULT CDirectVoiceClientEngine::Delete3DSoundBuffer( DVID dvidID, LPDIRECTSOUND3DBUFFER *lplpBuffer )
  2396. {
  2397. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Begin" );
  2398. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  2399. DPFX(DPFPREP, DVF_APIPARAM, "Params: dvidID = 0x%x lpBuffer = 0x%p", dvidID, lplpBuffer );
  2400. if( lplpBuffer == NULL ||
  2401. !DNVALID_WRITEPTR( lplpBuffer, sizeof( LPDIRECTSOUND3DBUFFER ) ) )
  2402. {
  2403. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid pointer" );
  2404. return E_POINTER;
  2405. }
  2406. CDVCSLock guardLock(&m_csClassLock);
  2407. guardLock.Lock();
  2408. if( m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  2409. {
  2410. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not initialized" );
  2411. return DVERR_NOTINITIALIZED;
  2412. }
  2413. else if( m_dwCurrentState != DVCSTATE_CONNECTED &&
  2414. m_dwCurrentState != DVCSTATE_DISCONNECTING )
  2415. {
  2416. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not connected" );
  2417. return DVERR_NOTCONNECTED;
  2418. }
  2419. HRESULT hr;
  2420. DWORD dwMode;
  2421. LONG lResult;
  2422. // Handle request to disable 3D on the main buffer
  2423. if( dvidID == DVID_REMAINING )
  2424. {
  2425. LPDIRECTSOUND3DBUFFER lpTmpBuffer;
  2426. lpTmpBuffer = m_lpstGeneralBuffer->Get3DBuffer();
  2427. if( lpTmpBuffer == NULL )
  2428. {
  2429. DPFX(DPFPREP, DVF_ERRORLEVEL, "No 3d buffer supported" );
  2430. return DVERR_NOTBUFFERED;
  2431. }
  2432. if( lpTmpBuffer != *lplpBuffer )
  2433. {
  2434. DPFX(DPFPREP, DVF_ERRORLEVEL, "Buffer passed in does not belong to specified id" );
  2435. return DVERR_INVALIDPARAM;
  2436. }
  2437. hr = lpTmpBuffer->GetMode( &dwMode );
  2438. if( FAILED( hr ) )
  2439. {
  2440. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to get current mode hr=0x%x", hr );
  2441. return DVERR_GENERIC;
  2442. }
  2443. if( dwMode == DS3DMODE_DISABLE )
  2444. {
  2445. DPFX(DPFPREP, DVF_ERRORLEVEL, "Not buffered" );
  2446. return DVERR_NOTBUFFERED;
  2447. }
  2448. // Check return code
  2449. // Add reference
  2450. hr = lpTmpBuffer->SetMode( DS3DMODE_DISABLE, DS3D_IMMEDIATE );
  2451. if( hr != DV_OK )
  2452. {
  2453. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to set the mode to activate 3d buffer hr=0x%x", hr );
  2454. return DVERR_GENERIC;
  2455. }
  2456. hr = lpTmpBuffer->SetPosition( 0.0, 0.0, 0.0, DS3D_IMMEDIATE );
  2457. // Not a Failure condition.
  2458. if( hr != DV_OK )
  2459. {
  2460. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to set the position of the 3d buffer hr=0x%x", hr );
  2461. }
  2462. // Remove reference the user has
  2463. lResult = m_lpstGeneralBuffer->Release();
  2464. *lplpBuffer = NULL;
  2465. return DV_OK;
  2466. }
  2467. CSoundTarget *lpstTarget;
  2468. hr = FindSoundTarget( dvidID, &lpstTarget );
  2469. if( FAILED( hr ) )
  2470. {
  2471. DPFX(DPFPREP, DVF_ERRORLEVEL, "CreateUserBuffer: Find of buffer failed. hr=0x%x", hr );
  2472. return DVERR_NOTBUFFERED;
  2473. }
  2474. if( lpstTarget == NULL )
  2475. {
  2476. DPFX(DPFPREP, DVF_ERRORLEVEL, "DeleteUserBuffer: Unable to retrieve user record" );
  2477. return DVERR_NOTBUFFERED;
  2478. }
  2479. if( lpstTarget->Get3DBuffer() != *lplpBuffer )
  2480. {
  2481. DPFX(DPFPREP, DVF_ERRORLEVEL, "Buffer passed in does not belong to specified id" );
  2482. // Get rid of the reference this func has
  2483. lResult = lpstTarget->Release();
  2484. DNASSERT( lResult != 0 );
  2485. return DVERR_INVALIDPARAM;
  2486. }
  2487. DPFX(DPFPREP, DVF_SOUNDTARGET_DEBUG_LEVEL, "SOUNDTARGET: [0x%x] DESTROY3DBUFFER ", dvidID );
  2488. // Get rid of the reference the FindSoundTarget has
  2489. lResult = lpstTarget->Release();
  2490. DNASSERT( lResult != 0 );
  2491. // Get rid of the reference the user has
  2492. lResult = lpstTarget->Release();
  2493. DNASSERT( lResult != 0 );
  2494. // Destroy the last reference (unless there is one outstanding)
  2495. DeleteSoundTarget( dvidID );
  2496. *lplpBuffer = NULL;
  2497. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Done" );
  2498. return DV_OK;
  2499. }
  2500. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  2501. //
  2502. // DIRECTPLAY/NET --> DirectXVoiceClient Interface
  2503. //
  2504. #undef DPF_MODNAME
  2505. #define DPF_MODNAME "CDirectVoiceClientEngine::ReceiveSpeechMessage"
  2506. // ReceiveSpeechMessage
  2507. //
  2508. // Called by DirectPlay/DirectNet when a DirectXVoice message is received
  2509. //
  2510. // Called By:
  2511. // - DV_ReceiveSpeechMessage
  2512. //
  2513. // Locks Required:
  2514. // - None
  2515. //
  2516. BOOL CDirectVoiceClientEngine::ReceiveSpeechMessage( DVID dvidSource, LPVOID lpMessage, DWORD dwSize )
  2517. {
  2518. BOOL fResult;
  2519. PDVPROTOCOLMSG_FULLMESSAGE lpdvFullMessage;
  2520. // if we dont' have at least one byte then we are going to bail
  2521. if ( dwSize < 1 )
  2522. {
  2523. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::ReceiveSpeechMessage() Ignoring zero-byte sized message from=0x%x",
  2524. dvidSource );
  2525. return FALSE;
  2526. }
  2527. lpdvFullMessage = (PDVPROTOCOLMSG_FULLMESSAGE) lpMessage;
  2528. if( !ValidatePacketType( lpdvFullMessage ) )
  2529. {
  2530. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::ReceiveSpeechMessage() Ignoring message with invalid packet type, type=0x%x, from=0x%x",
  2531. lpdvFullMessage->dvGeneric.dwType, dvidSource );
  2532. return FALSE;
  2533. }
  2534. switch( lpdvFullMessage->dvGeneric.dwType )
  2535. {
  2536. case DVMSGID_HOSTMIGRATELEAVE:
  2537. fResult = HandleHostMigrateLeave( dvidSource, static_cast<PDVPROTOCOLMSG_HOSTMIGRATELEAVE>(lpMessage), dwSize );
  2538. break;
  2539. case DVMSGID_HOSTMIGRATED:
  2540. fResult = HandleHostMigrated( dvidSource, static_cast<PDVPROTOCOLMSG_HOSTMIGRATED>(lpMessage),dwSize );
  2541. break;
  2542. case DVMSGID_CONNECTREFUSE:
  2543. fResult = HandleConnectRefuse( dvidSource, static_cast<PDVPROTOCOLMSG_CONNECTREFUSE>(lpMessage), dwSize );
  2544. break;
  2545. case DVMSGID_CONNECTACCEPT:
  2546. fResult = HandleConnectAccept( dvidSource, static_cast<PDVPROTOCOLMSG_CONNECTACCEPT>(lpMessage), dwSize );
  2547. break;
  2548. case DVMSGID_CREATEVOICEPLAYER:
  2549. fResult = HandleCreateVoicePlayer( dvidSource, static_cast<PDVPROTOCOLMSG_PLAYERJOIN>(lpMessage), dwSize );
  2550. break;
  2551. case DVMSGID_DELETEVOICEPLAYER:
  2552. fResult = HandleDeleteVoicePlayer( dvidSource, static_cast<PDVPROTOCOLMSG_PLAYERQUIT>(lpMessage), dwSize );
  2553. break;
  2554. case DVMSGID_SPEECH:
  2555. fResult = HandleSpeech( dvidSource, static_cast<PDVPROTOCOLMSG_SPEECHHEADER>(lpMessage), dwSize );
  2556. break;
  2557. case DVMSGID_SPEECHBOUNCE:
  2558. fResult = HandleSpeechBounce( dvidSource, static_cast<PDVPROTOCOLMSG_SPEECHHEADER>(lpMessage), dwSize );
  2559. break;
  2560. case DVMSGID_SPEECHWITHFROM:
  2561. fResult = HandleSpeechWithFrom( dvidSource, static_cast<PDVPROTOCOLMSG_SPEECHWITHFROM>(lpMessage), dwSize );
  2562. break;
  2563. case DVMSGID_DISCONNECTCONFIRM:
  2564. fResult = HandleDisconnectConfirm( dvidSource, static_cast<PDVPROTOCOLMSG_DISCONNECT>(lpMessage), dwSize);
  2565. break;
  2566. case DVMSGID_SETTARGETS:
  2567. fResult = HandleSetTarget( dvidSource, static_cast<PDVPROTOCOLMSG_SETTARGET>(lpMessage), dwSize );
  2568. break;
  2569. case DVMSGID_SESSIONLOST:
  2570. fResult = HandleSessionLost( dvidSource, static_cast<PDVPROTOCOLMSG_SESSIONLOST>(lpMessage), dwSize );
  2571. break;
  2572. case DVMSGID_PLAYERLIST:
  2573. fResult = HandlePlayerList( dvidSource, static_cast<PDVPROTOCOLMSG_PLAYERLIST>(lpMessage), dwSize );
  2574. break;
  2575. default:
  2576. DPFX(DPFPREP, DVF_WARNINGLEVEL, "DVCE::ReceiveSpeechMessage() Ignoring non-speech message id=0x%x from=0x%x",
  2577. lpdvFullMessage->dvGeneric.dwType, dvidSource );
  2578. return FALSE;
  2579. }
  2580. return fResult;
  2581. }
  2582. #undef DPF_MODNAME
  2583. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleSetTarget"
  2584. // HandleSetTarget
  2585. //
  2586. // Handles server settarget messages. Sets the local target.
  2587. //
  2588. BOOL CDirectVoiceClientEngine::HandleSetTarget( DVID dvidSource, PDVPROTOCOLMSG_SETTARGET lpdvSetTarget, DWORD dwSize )
  2589. {
  2590. HRESULT hr;
  2591. // check structure size first so that we don't crash by accessing bad data
  2592. if ( dwSize < sizeof( DVPROTOCOLMSG_SETTARGET ) || ( dwSize != (sizeof( DVPROTOCOLMSG_SETTARGET ) + ( lpdvSetTarget->dwNumTargets * sizeof(DVID) ) ) ) )
  2593. {
  2594. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSetTarget() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  2595. dwSize, dvidSource );
  2596. return FALSE;
  2597. }
  2598. if( lpdvSetTarget->dwNumTargets > DV_MAX_TARGETS )
  2599. {
  2600. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSetTarget() Ignoring message with too many targets, targets=0x%x, from=0x%x",
  2601. lpdvSetTarget->dwNumTargets, dvidSource );
  2602. return FALSE;
  2603. }
  2604. hr = InternalSetTransmitTarget( (DWORD *) &lpdvSetTarget[1], lpdvSetTarget->dwNumTargets );
  2605. DNASSERT( hr == DV_OK );
  2606. return TRUE;
  2607. }
  2608. #undef DPF_MODNAME
  2609. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleHostMigrateLeave"
  2610. BOOL CDirectVoiceClientEngine::HandleHostMigrateLeave( DVID dvidSource, PDVPROTOCOLMSG_HOSTMIGRATELEAVE lpdvHostMigrateLeave, DWORD dwSize )
  2611. {
  2612. // Call RemovePlayer with the ID of the person who sent this,
  2613. // which should be the host.
  2614. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Triggered by DVMSGID_HOSTMIGRATELEAVE" );
  2615. MigrateHost_RunElection();
  2616. return TRUE;
  2617. }
  2618. #undef DPF_MODNAME
  2619. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleHostMigrated"
  2620. BOOL CDirectVoiceClientEngine::HandleHostMigrated( DVID dvidSource, PDVPROTOCOLMSG_HOSTMIGRATED lpdvHostMigrated, DWORD dwSize )
  2621. {
  2622. HRESULT hr;
  2623. CDVCSLock guardLock(&m_csClassLock);
  2624. guardLock.Lock();
  2625. // We're not yet connected, so we can't send our settings to the server yet.
  2626. // However, because of the write lock we know that when the connection
  2627. // completes we'll have the right host. (Transparently to the client).
  2628. //
  2629. // We need to proceed if we're disconnecting because we need to send a new disconnect confirm
  2630. //
  2631. if( m_dwCurrentState != DVCSTATE_CONNECTED &&
  2632. m_dwCurrentState != DVCSTATE_DISCONNECTING )
  2633. {
  2634. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Could not respond to new host yet, not yet initialized" );
  2635. return TRUE;
  2636. }
  2637. if( dvidSource != m_dvidServer )
  2638. {
  2639. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Ignoring host migration from 0x%x -- 0x%x is server", dvidSource, m_dvidServer );
  2640. return TRUE;
  2641. }
  2642. guardLock.Unlock();
  2643. if( m_dwCurrentState == DVCSTATE_DISCONNECTING )
  2644. {
  2645. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Sending NEW host disconnect request" );
  2646. hr = Send_DisconnectRequest();
  2647. if( FAILED( hr ) )
  2648. {
  2649. DPFX(DPFPREP, DVF_ERRORLEVEL, "Internal send failed hr=0x%x", hr );
  2650. }
  2651. else
  2652. {
  2653. hr=DV_OK;
  2654. }
  2655. }
  2656. else
  2657. {
  2658. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Player 0x%x received host migrated message!", m_dvidLocal );
  2659. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Sending player confirm message" );
  2660. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: According to message Player 0x%x is new host", dvidSource );
  2661. hr = Send_SettingsConfirm();
  2662. if( FAILED( hr ) )
  2663. {
  2664. DPFX(DPFPREP, DVF_ERRORLEVEL, "Internal send failed hr=0x%x", hr );
  2665. }
  2666. else
  2667. {
  2668. hr=DV_OK;
  2669. }
  2670. }
  2671. return TRUE;
  2672. }
  2673. #undef DPF_MODNAME
  2674. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleConnectRefuse"
  2675. // HandleConnectRefuse
  2676. //
  2677. // Handles connection refusals
  2678. //
  2679. BOOL CDirectVoiceClientEngine::HandleConnectRefuse( DVID dvidSource, PDVPROTOCOLMSG_CONNECTREFUSE lpdvConnectRefuse, DWORD dwSize )
  2680. {
  2681. if ( dwSize != sizeof( DVPROTOCOLMSG_CONNECTREFUSE ) )
  2682. {
  2683. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleConnectRefuse() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  2684. dwSize, dvidSource );
  2685. return FALSE;
  2686. }
  2687. // This prevents someone from sending a connect refuse to bump off a valid player
  2688. if( GetCurrentState() != DVCSTATE_CONNECTING )
  2689. {
  2690. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleConnectRefuse() Ignoring connect refuse AFTER connection from=0x%x", dvidSource );
  2691. return FALSE;
  2692. }
  2693. // Do some brain dead error checking. Should never happen but print
  2694. // some debug spew just in case
  2695. if (SUCCEEDED(lpdvConnectRefuse->hresResult))
  2696. {
  2697. DPFX(DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "CDirectVoiceClientEngine::HandleConnectRefuse but reason given is success!" );
  2698. }
  2699. SetConnectResult( lpdvConnectRefuse->hresResult );
  2700. SetEvent( m_hNotifyConnect );
  2701. return TRUE;
  2702. }
  2703. #undef DPF_MODNAME
  2704. #define DPF_MODNAME "CDirectVoiceClientEngine::DeInitializeClientServer"
  2705. // DeInitializeClientServer
  2706. //
  2707. // This function is responsible for pre-allocating the information
  2708. // for receiving client/server voice.
  2709. //
  2710. void CDirectVoiceClientEngine::DeInitializeClientServer()
  2711. {
  2712. DVPROTOCOLMSG_PLAYERQUIT dvPlayerQuit;
  2713. dvPlayerQuit.dwType = DVMSGID_DELETEVOICEPLAYER;
  2714. dvPlayerQuit.dvidID = m_dvidServer;
  2715. HandleDeleteVoicePlayer( 0, &dvPlayerQuit, sizeof( DVPROTOCOLMSG_PLAYERQUIT ) );
  2716. }
  2717. #undef DPF_MODNAME
  2718. #define DPF_MODNAME "CDirectVoiceClientEngine::InitializeClientServer"
  2719. // InitializeClientServer
  2720. //
  2721. // This function is responsible for pre-allocating the information
  2722. // for receiving client/server voice
  2723. //
  2724. HRESULT CDirectVoiceClientEngine::InitializeClientServer()
  2725. {
  2726. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::InitializeClientServer() - Initializing Client/Server Queues" );
  2727. HRESULT hr;
  2728. CVoicePlayer *pNewPlayer;
  2729. QUEUE_PARAMS queueParams;
  2730. pNewPlayer = m_fpPlayers.Get();
  2731. if( pNewPlayer == NULL )
  2732. {
  2733. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to allocate new player for client/server" );
  2734. return DVERR_OUTOFMEMORY;
  2735. }
  2736. hr = pNewPlayer->Initialize( m_dvidServer, 0, 0, NULL, &m_fpPlayers );
  2737. if( FAILED( hr ) )
  2738. {
  2739. pNewPlayer->Release();
  2740. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to initialize server player record" );
  2741. return hr;
  2742. }
  2743. queueParams.wFrameSize = m_dwCompressedFrameSize;
  2744. queueParams.bInnerQueueSize = m_lpdvfCompressionInfo->wInnerQueueSize;
  2745. queueParams.bMaxHighWaterMark = m_lpdvfCompressionInfo->wMaxHighWaterMark,
  2746. queueParams.iQuality = m_dvClientConfig.dwBufferQuality;
  2747. queueParams.iHops = 2;
  2748. queueParams.iAggr = m_dvClientConfig.dwBufferAggressiveness;
  2749. queueParams.bInitHighWaterMark = 2;
  2750. queueParams.wQueueId = -1;
  2751. queueParams.wMSPerFrame = m_lpdvfCompressionInfo->dwTimeout,
  2752. queueParams.pFramePool = m_pFramePool;
  2753. hr = pNewPlayer->CreateQueue( &queueParams );
  2754. if( FAILED( hr ) )
  2755. {
  2756. pNewPlayer->Release();
  2757. DPFX(DPFPREP, DVF_ERRORLEVEL, "Could not create queue hr=0x%x", hr );
  2758. return hr;
  2759. }
  2760. hr = m_voiceNameTable.AddEntry( m_dvidServer, pNewPlayer );
  2761. if( FAILED( hr ) )
  2762. {
  2763. // Main ref
  2764. pNewPlayer->Release();
  2765. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to add entry to nametable hr=0x%x", hr );
  2766. return TRUE;
  2767. }
  2768. // Add a reference for the player to the "Playback Add List"
  2769. DNEnterCriticalSection( &m_csPlayAddList );
  2770. pNewPlayer->AddRef();
  2771. pNewPlayer->AddToPlayList( &m_blPlayAddPlayers );
  2772. DNLeaveCriticalSection( &m_csPlayAddList );
  2773. // Add a reference for the player to the "Notify Add List"
  2774. DNEnterCriticalSection( &m_csNotifyAddList );
  2775. pNewPlayer->AddRef();
  2776. pNewPlayer->AddToNotifyList( &m_blNotifyAddPlayers );
  2777. DNLeaveCriticalSection( &m_csNotifyAddList );
  2778. pNewPlayer->SetAvailable( TRUE );
  2779. m_fLocalPlayerAvailable = TRUE;
  2780. // Release our personal reference
  2781. pNewPlayer->Release();
  2782. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::InitializeClientServer() - Done Initializing Client/Server Queues" );
  2783. return DP_OK;
  2784. }
  2785. #undef DPF_MODNAME
  2786. #define DPF_MODNAME "CDirectVoiceClientEngine::DoConnectResponse"
  2787. void CDirectVoiceClientEngine::DoConnectResponse()
  2788. {
  2789. HRESULT hr = DV_OK;
  2790. CDVCSLock guardLock(&m_csClassLock);
  2791. guardLock.Lock();
  2792. if( m_dwCurrentState != DVCSTATE_CONNECTING )
  2793. {
  2794. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Aborting Connection & server response arrived" );
  2795. return;
  2796. }
  2797. // Work from the default assumption that something will screw up
  2798. SetConnectResult( DVERR_GENERIC );
  2799. ClientStats_Begin();
  2800. #ifndef __DISABLE_SOUND
  2801. // Handle sound initialization
  2802. hr = InitializeSoundSystem();
  2803. if( FAILED(hr) )
  2804. {
  2805. SetConnectResult(hr);
  2806. DPFX(DPFPREP, DVF_ERRORLEVEL, "Sound Initialization Failed hr=0x%x", hr );
  2807. goto EXIT_ERROR;
  2808. }
  2809. #endif
  2810. hr = SetupSpeechBuffer();
  2811. if( FAILED( hr ) )
  2812. {
  2813. SetConnectResult(hr);
  2814. DPFX(DPFPREP, DVF_ERRORLEVEL, "Could not init speech buffers hr=0x%x", hr );
  2815. goto EXIT_ERROR;
  2816. }
  2817. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::HandleConnectAccept() - Sound Initialized" );
  2818. // If we're running in client/server we need to pre-create a single buffer
  2819. // and compressor
  2820. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_MIXING ||
  2821. m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_ECHO )
  2822. {
  2823. hr = InitializeClientServer();
  2824. if( FAILED( hr ) )
  2825. {
  2826. SetConnectResult(hr);
  2827. DPFX(DPFPREP, DVF_ERRORLEVEL, "Could not init client/server hr=0x%x", hr );
  2828. goto EXIT_ERROR;
  2829. }
  2830. }
  2831. // We need to make player available in client/server because there will be no indication
  2832. if( m_dvSessionDesc.dwSessionType != DVSESSIONTYPE_PEER )
  2833. {
  2834. m_fLocalPlayerAvailable = TRUE;
  2835. }
  2836. #ifndef __DISABLE_SOUND
  2837. // Start playback thread
  2838. // Create Thread events
  2839. m_hPlaybackTerminate = CreateEvent( NULL, FALSE, FALSE, NULL );
  2840. m_hPlaybackDone = CreateEvent( NULL, FALSE, FALSE, NULL );
  2841. // Create Semaphores for playback and record
  2842. m_thTimerInfo.hPlaybackTimerEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  2843. m_thTimerInfo.lPlaybackCount = 0;
  2844. m_thTimerInfo.hRecordTimerEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  2845. if( m_hPlaybackTerminate == NULL || m_hPlaybackDone == NULL ||
  2846. m_thTimerInfo.hPlaybackTimerEvent == NULL || m_thTimerInfo.hRecordTimerEvent == NULL )
  2847. {
  2848. DPFX(DPFPREP, DVF_ERRORLEVEL, "Windows error, event create failure." );
  2849. SetConnectResult( DVERR_GENERIC );
  2850. goto EXIT_ERROR;
  2851. }
  2852. // Create Multimedia timer
  2853. m_pTimer = new Timer;
  2854. if( m_pTimer == NULL )
  2855. {
  2856. SetConnectResult( DVERR_OUTOFMEMORY );
  2857. goto EXIT_ERROR;
  2858. }
  2859. if( !m_pTimer->Create( m_lpdvfCompressionInfo->dwTimeout / DV_CLIENT_WAKEUP_MULTIPLER, 0, (DWORD_PTR) &m_thTimerInfo, MixingWakeupProc ) )
  2860. {
  2861. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create timer." );
  2862. SetConnectResult( DVERR_GENERIC );
  2863. goto EXIT_ERROR;
  2864. }
  2865. m_hPlaybackThreadHandle = (HANDLE) _beginthread( PlaybackThread, 0, this );
  2866. #ifdef __CORE_THREAD_PRIORITY_HIGH
  2867. SetThreadPriority( m_hPlaybackThreadHandle, THREAD_PRIORITY_TIME_CRITICAL );
  2868. #endif
  2869. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  2870. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::HandleConnectAccept() - Playback Thread Started: 0x%p", m_hPlaybackThreadHandle );
  2871. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX) )
  2872. {
  2873. m_hRecordTerminate = CreateEvent( NULL, FALSE, FALSE, NULL );
  2874. m_hRecordDone = CreateEvent( NULL, FALSE, FALSE, NULL );
  2875. if (m_hRecordTerminate==NULL || m_hRecordDone==NULL)
  2876. {
  2877. DPFX(DPFPREP, DVF_ERRORLEVEL, "Windows error, event create failure. hr=0x%x", GetLastError() );
  2878. SetConnectResult( DVERR_GENERIC );
  2879. goto EXIT_ERROR;
  2880. }
  2881. // Start Record Thread
  2882. m_hRecordThreadHandle = (HANDLE) _beginthread( RecordThread, 0, this );
  2883. #ifdef __CORE_THREAD_PRIORITY_HIGH
  2884. SetThreadPriority( m_hRecordThreadHandle, THREAD_PRIORITY_TIME_CRITICAL );
  2885. #endif
  2886. // 7/31/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers, addresses, and handles.
  2887. DPFX(DPFPREP, DVF_INFOLEVEL, "DVCE::HandleConnectAccept() - Record Thread Started: 0x%p", m_hRecordThreadHandle );
  2888. }
  2889. else
  2890. {
  2891. m_hRecordTerminate = NULL;
  2892. m_hRecordDone = NULL;
  2893. }
  2894. #endif
  2895. SetCurrentState( DVCSTATE_CONNECTED );
  2896. SetConnectResult(DV_OK);
  2897. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::HandleConnectAccept() Success" );
  2898. SendConnectResult();
  2899. ///////
  2900. guardLock.Unlock();
  2901. hr = Send_SettingsConfirm();
  2902. if( FAILED( hr ) )
  2903. {
  2904. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to send connect confirmation hr=0x%x", hr );
  2905. DPFX(DPFPREP, DVF_ERRORLEVEL, "Other threads will cleanup because connection must be gone" );
  2906. }
  2907. SetEvent( m_hConnectAck );
  2908. return;
  2909. EXIT_ERROR:
  2910. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_MIXING ||
  2911. m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_ECHO )
  2912. {
  2913. DeInitializeClientServer();
  2914. }
  2915. DPFX(DPFPREP, DVF_ERRORLEVEL, "HandleConnectAccept Failed hr=0x%x", GetConnectResult() );
  2916. DV_DUMP_GUID( m_dvSessionDesc.guidCT );
  2917. guardLock.Unlock();
  2918. Cleanup();
  2919. SendConnectResult();
  2920. SetEvent( m_hConnectAck );
  2921. return;
  2922. }
  2923. #undef DPF_MODNAME
  2924. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleConnectAccept"
  2925. // HandleConnectAccepts
  2926. //
  2927. // Handles connect accepts. Sets connected flag, finishes initialization, informs the
  2928. // connect function to proceed (if it's waiting).
  2929. //
  2930. BOOL CDirectVoiceClientEngine::HandleConnectAccept( DVID dvidSource, PDVPROTOCOLMSG_CONNECTACCEPT lpdvConnectAccept, DWORD dwSize )
  2931. {
  2932. char tmpString[100];
  2933. if ( dwSize != sizeof( DVPROTOCOLMSG_CONNECTACCEPT ) )
  2934. {
  2935. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleConnectAccept() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  2936. dwSize, dvidSource );
  2937. return FALSE;
  2938. }
  2939. if( !ValidateSessionType( lpdvConnectAccept->dwSessionType ) )
  2940. {
  2941. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleConnectAccept() Ignoring message with invalid session type, type=0x%x, from=0x%x",
  2942. lpdvConnectAccept->dwSessionType, dvidSource );
  2943. return FALSE;
  2944. }
  2945. if( !ValidateSessionFlags( lpdvConnectAccept->dwSessionFlags ) )
  2946. {
  2947. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleConnectAccept() Ignoring message with invalid session flags, flags=0x%x, from=0x%x",
  2948. lpdvConnectAccept->dwSessionFlags, dvidSource );
  2949. return FALSE;
  2950. }
  2951. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::HandleConnectAccept() Entry" );
  2952. CDVCSLock guardLock(&m_csClassLock);
  2953. guardLock.Lock();
  2954. // We're already connected, server is responding to the earlier request
  2955. if( m_dwCurrentState != DVCSTATE_CONNECTING )
  2956. {
  2957. return TRUE;
  2958. }
  2959. m_hPlaybackTerminate = NULL;
  2960. m_hPlaybackDone = NULL;
  2961. m_thTimerInfo.hPlaybackTimerEvent = NULL;
  2962. m_thTimerInfo.hRecordTimerEvent = NULL;
  2963. m_pTimer = NULL;
  2964. // Inform transport layer who the server is. (So it no longer thinks it's DPID_ALL).
  2965. m_lpSessionTransport->MigrateHost( dvidSource );
  2966. m_dvidServer = m_lpSessionTransport->GetServerID();
  2967. DVPROTOCOLMSG_FULLMESSAGE dvMessage;
  2968. DPFX(DPFPREP, DVF_INFOLEVEL, "Connect Accept Received" );
  2969. m_dvSessionDesc.dwSize = sizeof( DVSESSIONDESC );
  2970. m_dvSessionDesc.dwBufferAggressiveness = 0;
  2971. m_dvSessionDesc.dwBufferQuality = 0;
  2972. m_dvSessionDesc.dwFlags = lpdvConnectAccept->dwSessionFlags;
  2973. m_dvSessionDesc.guidCT = lpdvConnectAccept->guidCT;
  2974. m_dvSessionDesc.dwSessionType = lpdvConnectAccept->dwSessionType;
  2975. HRESULT hr = DVCDB_GetCompressionInfo( lpdvConnectAccept->guidCT, &m_lpdvfCompressionInfo );
  2976. if( FAILED( hr ) )
  2977. {
  2978. SetConnectResult( DVERR_COMPRESSIONNOTSUPPORTED );
  2979. DPFX(DPFPREP, DVF_ERRORLEVEL, "Invalid Compression Type" );
  2980. SetEvent( m_hNotifyConnect );
  2981. return TRUE;
  2982. }
  2983. SetConnectResult( DV_OK );
  2984. DV_DUMP_CIF( m_lpdvfCompressionInfo, 1 );
  2985. SetEvent( m_hNotifyConnect );
  2986. return TRUE;
  2987. }
  2988. #undef DPF_MODNAME
  2989. #define DPF_MODNAME "CDirectVoiceClientEngine::HandlePlayerList"
  2990. BOOL CDirectVoiceClientEngine::HandlePlayerList( DVID dvidSource, PDVPROTOCOLMSG_PLAYERLIST lpdvPlayerList, DWORD dwSize )
  2991. {
  2992. DVPROTOCOLMSG_PLAYERJOIN dvMsgPlayerJoin; // Used to fake out HandleCreateVoicePlayer
  2993. DWORD dwIndex;
  2994. // check structure size first so that we don't crash by accessing bad data
  2995. if ( dwSize < sizeof( DVPROTOCOLMSG_PLAYERLIST ) || ( dwSize != (sizeof( DVPROTOCOLMSG_PLAYERLIST ) + ( lpdvPlayerList->dwNumEntries * sizeof(DVPROTOCOLMSG_PLAYERLIST_ENTRY ) ) ) ) )
  2996. {
  2997. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandlePlayerList() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  2998. dwSize, dvidSource );
  2999. return FALSE;
  3000. }
  3001. DVPROTOCOLMSG_PLAYERLIST_ENTRY *pdvPlayerList = (DVPROTOCOLMSG_PLAYERLIST_ENTRY *) &lpdvPlayerList[1];
  3002. // Get our host order ID
  3003. m_dwHostOrderID = lpdvPlayerList->dwHostOrderID;
  3004. DPFX(DPFPREP, DVF_INFOLEVEL, "Received player list. Unpacking %d entries", lpdvPlayerList->dwNumEntries );
  3005. for( dwIndex = 0; dwIndex < lpdvPlayerList->dwNumEntries; dwIndex++ )
  3006. {
  3007. dvMsgPlayerJoin.dwType = DVMSGID_CREATEVOICEPLAYER;
  3008. dvMsgPlayerJoin.dvidID = pdvPlayerList[dwIndex].dvidID;
  3009. dvMsgPlayerJoin.dwFlags = pdvPlayerList[dwIndex].dwPlayerFlags;
  3010. dvMsgPlayerJoin.dwHostOrderID = pdvPlayerList[dwIndex].dwHostOrderID;
  3011. if( !HandleCreateVoicePlayer( dvidSource, &dvMsgPlayerJoin, sizeof( DVPROTOCOLMSG_PLAYERJOIN ) ) )
  3012. {
  3013. DPFX(DPFPREP, DVF_ERRORLEVEL, "Handle voice player failed during unpack" );
  3014. return FALSE;
  3015. }
  3016. }
  3017. return TRUE;
  3018. }
  3019. #undef DPF_MODNAME
  3020. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyComplete_SyncWait"
  3021. //
  3022. // NotifyComplete_SyncWait
  3023. //
  3024. // This is a completion function for notifications which need to be performed synchronously.
  3025. //
  3026. void CDirectVoiceClientEngine::NotifyComplete_SyncWait( PVOID pvContext, CNotifyElement *pElement )
  3027. {
  3028. HANDLE *pTmpHandle = (HANDLE *) pvContext;
  3029. DNASSERT( pTmpHandle != NULL );
  3030. if( pTmpHandle != NULL && *pTmpHandle != NULL )
  3031. {
  3032. SetEvent( *pTmpHandle );
  3033. }
  3034. }
  3035. #undef DPF_MODNAME
  3036. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyComplete_RemotePlayer"
  3037. //
  3038. // NotifyComplete_RemotePlayer
  3039. //
  3040. // This is a completion function for when notification of a new remote player has been processed
  3041. //
  3042. void CDirectVoiceClientEngine::NotifyComplete_RemotePlayer( PVOID pvContext, CNotifyElement *pElement )
  3043. {
  3044. CVoicePlayer *pPlayer = (CVoicePlayer *) pvContext;
  3045. PDVMSG_CREATEVOICEPLAYER pCreatePlayer = NULL;
  3046. if( pElement->m_etElementType == NOTIFY_DYNAMIC )
  3047. {
  3048. pCreatePlayer = (PDVMSG_CREATEVOICEPLAYER) pElement->m_element.dynamic.m_lpData;
  3049. }
  3050. else
  3051. {
  3052. pCreatePlayer = (PDVMSG_CREATEVOICEPLAYER) pElement->m_element.fixed.m_bFixedHolder;
  3053. }
  3054. DNASSERT( pPlayer != NULL );
  3055. DNASSERT( pCreatePlayer->dwSize == sizeof( DVMSG_CREATEVOICEPLAYER ) );
  3056. pPlayer->SetContext( pCreatePlayer->pvPlayerContext );
  3057. pPlayer->SetAvailable( TRUE );
  3058. pPlayer->Release();
  3059. }
  3060. #undef DPF_MODNAME
  3061. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyComplete_LocalPlayer"
  3062. //
  3063. // NotifyComplete_LocalPlayer
  3064. //
  3065. // This is a completion function for when notification of the local player has been processed
  3066. //
  3067. void CDirectVoiceClientEngine::NotifyComplete_LocalPlayer( PVOID pvContext, CNotifyElement *pElement )
  3068. {
  3069. CDirectVoiceClientEngine *pvEngine = (CDirectVoiceClientEngine *) pvContext;
  3070. PDVMSG_CREATEVOICEPLAYER pCreatePlayer = NULL;
  3071. if( pElement->m_etElementType == NOTIFY_DYNAMIC )
  3072. {
  3073. pCreatePlayer = (PDVMSG_CREATEVOICEPLAYER) pElement->m_element.dynamic.m_lpData;
  3074. }
  3075. else
  3076. {
  3077. pCreatePlayer = (PDVMSG_CREATEVOICEPLAYER) pElement->m_element.fixed.m_bFixedHolder;
  3078. }
  3079. DNASSERT( pCreatePlayer->dwSize == sizeof( DVMSG_CREATEVOICEPLAYER ) );
  3080. pvEngine->m_pvLocalPlayerContext = pCreatePlayer->pvPlayerContext;
  3081. pvEngine->m_fLocalPlayerAvailable = TRUE;
  3082. }
  3083. #undef DPF_MODNAME
  3084. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleCreateVoicePlayer"
  3085. //
  3086. // HandleCreateVoicePlayer
  3087. //
  3088. // Performs initialization required to create the specified user's record.
  3089. //
  3090. // Players in the system will normall have a reference count of 3:
  3091. // - 1 for playback thread
  3092. // - 1 for notify thread
  3093. // - 1 for nametable
  3094. //
  3095. // When a player is added they are added to the nametable as well as the
  3096. // pending lists for notify thread and playback thread.
  3097. //
  3098. // Both of these threads wakeup and:
  3099. // - Add any players on the "add list"
  3100. // - Remove any players who are marked disconnecting
  3101. //
  3102. BOOL CDirectVoiceClientEngine::HandleCreateVoicePlayer( DVID dvidSource, PDVPROTOCOLMSG_PLAYERJOIN lpdvCreatePlayer, DWORD dwSize )
  3103. {
  3104. if ( dwSize != sizeof( DVPROTOCOLMSG_PLAYERJOIN ) )
  3105. {
  3106. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleCreateVoicePlayer() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3107. dwSize, dvidSource );
  3108. return FALSE;
  3109. }
  3110. if( !ValidatePlayerFlags( lpdvCreatePlayer->dwFlags ) )
  3111. {
  3112. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleCreateVoicePlayer() Ignoring message with invalid player flags, flags=0x%x, from=0x%x",
  3113. lpdvCreatePlayer->dwFlags, dvidSource );
  3114. return FALSE;
  3115. }
  3116. if( !ValidatePlayerDVID( lpdvCreatePlayer->dvidID ) )
  3117. {
  3118. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleCreateVoicePlayer() Ignoring message with invalid player dvid, flags=0x%x, from=0x%x",
  3119. lpdvCreatePlayer->dvidID, dvidSource );
  3120. return FALSE;
  3121. }
  3122. if( m_dwCurrentState != DVCSTATE_CONNECTED )
  3123. return TRUE;
  3124. CVoicePlayer *newPlayer;
  3125. HRESULT hr;
  3126. QUEUE_PARAMS queueParams;
  3127. hr = m_voiceNameTable.GetEntry( lpdvCreatePlayer->dvidID, &newPlayer, TRUE );
  3128. // Ignore duplicate players
  3129. if( hr == DV_OK )
  3130. {
  3131. newPlayer->Release();
  3132. return TRUE;
  3133. }
  3134. DPFX(DPFPREP, DVF_CONNECT_PROCEDURE_DEBUG_LEVEL, "Received Create for player ID 0x%x",lpdvCreatePlayer->dvidID );
  3135. // Do not both creating a queue or a player entry for ourselves
  3136. // Not needed
  3137. if( lpdvCreatePlayer->dvidID != m_lpSessionTransport->GetLocalID() )
  3138. {
  3139. DPFX(DPFPREP, DVF_CONNECT_PROCEDURE_DEBUG_LEVEL, "Creating player record" );
  3140. newPlayer = m_fpPlayers.Get();
  3141. if( newPlayer == NULL )
  3142. {
  3143. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to allocate new player record. Alloc failure" );
  3144. return TRUE;
  3145. }
  3146. hr = newPlayer->Initialize( lpdvCreatePlayer->dvidID, lpdvCreatePlayer->dwHostOrderID, lpdvCreatePlayer->dwFlags, NULL, &m_fpPlayers );
  3147. if( FAILED( hr ) )
  3148. {
  3149. newPlayer->Release();
  3150. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to initialize player record hr=0x%x", hr );
  3151. return TRUE;
  3152. }
  3153. queueParams.wFrameSize = m_dwCompressedFrameSize;
  3154. queueParams.bInnerQueueSize = m_lpdvfCompressionInfo->wInnerQueueSize;
  3155. queueParams.bMaxHighWaterMark = m_lpdvfCompressionInfo->wMaxHighWaterMark,
  3156. queueParams.iQuality = m_dvClientConfig.dwBufferQuality;
  3157. queueParams.iHops = 1;
  3158. queueParams.iAggr = m_dvClientConfig.dwBufferAggressiveness;
  3159. queueParams.bInitHighWaterMark = 2;
  3160. queueParams.wQueueId = lpdvCreatePlayer->dvidID;
  3161. queueParams.wMSPerFrame = m_lpdvfCompressionInfo->dwTimeout,
  3162. queueParams.pFramePool = m_pFramePool;
  3163. hr = newPlayer->CreateQueue( &queueParams );
  3164. if( FAILED( hr ) )
  3165. {
  3166. newPlayer->Release();
  3167. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create queue for player hr=0x%x", hr );
  3168. return TRUE;
  3169. }
  3170. hr = m_voiceNameTable.AddEntry( lpdvCreatePlayer->dvidID, newPlayer );
  3171. if( FAILED( hr ) )
  3172. {
  3173. // Main ref
  3174. newPlayer->Release();
  3175. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to add entry to nametable hr=0x%x", hr );
  3176. return TRUE;
  3177. }
  3178. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_FORWARDING )
  3179. {
  3180. newPlayer->SetAvailable(TRUE);
  3181. }
  3182. // Add a reference for the player to the "Playback Add List"
  3183. DNEnterCriticalSection( &m_csPlayAddList );
  3184. newPlayer->AddRef();
  3185. newPlayer->AddToPlayList( &m_blPlayAddPlayers );
  3186. DNLeaveCriticalSection( &m_csPlayAddList );
  3187. // Add a reference for the player to the "Notify Add List"
  3188. DNEnterCriticalSection( &m_csNotifyAddList );
  3189. newPlayer->AddRef();
  3190. newPlayer->AddToNotifyList( &m_blNotifyAddPlayers );
  3191. DNLeaveCriticalSection( &m_csNotifyAddList );
  3192. // This will now be released by the callback, unless this is multicast
  3193. //
  3194. // Release our personal reference
  3195. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_FORWARDING )
  3196. {
  3197. newPlayer->Release();
  3198. }
  3199. }
  3200. else
  3201. {
  3202. DPFX(DPFPREP, DVF_CONNECT_PROCEDURE_DEBUG_LEVEL, "Local player, no player record required" );
  3203. }
  3204. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER )
  3205. {
  3206. DVMSG_CREATEVOICEPLAYER dvCreatePlayer;
  3207. dvCreatePlayer.dvidPlayer = lpdvCreatePlayer->dvidID;
  3208. dvCreatePlayer.dwFlags = lpdvCreatePlayer->dwFlags;
  3209. dvCreatePlayer.dwSize = sizeof( DVMSG_CREATEVOICEPLAYER );
  3210. dvCreatePlayer.pvPlayerContext = NULL;
  3211. // Prevents double notification for local player
  3212. if( lpdvCreatePlayer->dvidID != m_lpSessionTransport->GetLocalID() )
  3213. {
  3214. NotifyQueue_Add( DVMSGID_CREATEVOICEPLAYER, &dvCreatePlayer, sizeof( DVMSG_CREATEVOICEPLAYER ),
  3215. newPlayer, NotifyComplete_RemotePlayer );
  3216. }
  3217. else if( !m_fLocalPlayerNotify )
  3218. {
  3219. // Add local player flag to notification
  3220. dvCreatePlayer.dwFlags |= DVPLAYERCAPS_LOCAL;
  3221. // Notify of local player (don't create player record)
  3222. NotifyQueue_Add( DVMSGID_CREATEVOICEPLAYER, &dvCreatePlayer, sizeof( DVMSG_CREATEVOICEPLAYER ),
  3223. this, NotifyComplete_LocalPlayer );
  3224. m_fLocalPlayerNotify = TRUE;
  3225. }
  3226. }
  3227. return TRUE;
  3228. }
  3229. #undef DPF_MODNAME
  3230. #define DPF_MODNAME "CDirectVoiceClientEngine::MigrateHost_RunElection"
  3231. //
  3232. // MigrateHost_RunElection
  3233. //
  3234. // Runs the host migration algorithm
  3235. //
  3236. HRESULT CDirectVoiceClientEngine::MigrateHost_RunElection( )
  3237. {
  3238. HRESULT hr;
  3239. if( m_dwCurrentState == DVCSTATE_DISCONNECTING )
  3240. {
  3241. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: We're disconnecting, no need to run algorithm" );
  3242. return TRUE;
  3243. }
  3244. DWORD dwTransportSessionType, dwTransportFlags;
  3245. hr = m_lpSessionTransport->GetTransportSettings( &dwTransportSessionType, &dwTransportFlags );
  3246. if( FAILED( hr ) )
  3247. {
  3248. DNASSERT( FALSE );
  3249. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to retrieve transport settings hr=0x%x", hr );
  3250. return DV_OK;
  3251. }
  3252. if( (m_dvSessionDesc.dwSessionType != DVSESSIONTYPE_PEER ) ||
  3253. (m_dvSessionDesc.dwFlags & DVSESSION_NOHOSTMIGRATION) ||
  3254. !(dwTransportFlags & DVTRANSPORT_MIGRATEHOST) )
  3255. {
  3256. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Host migration is disabled." );
  3257. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Host is gone. Session will exit soon." );
  3258. if( m_dwCurrentState == DVCSTATE_CONNECTING )
  3259. {
  3260. DPFX(DPFPREP, DVF_ERRORLEVEL, "Aborting connection..." );
  3261. SendConnectResult();
  3262. SetEvent( m_hConnectAck );
  3263. }
  3264. DoSessionLost(DVERR_SESSIONLOST);
  3265. return DV_OK;
  3266. }
  3267. // This shortcut prevents problems if this is called twice.
  3268. // But only if we're lucky enough this got set before this poin
  3269. // We also have to guard against this case in HostMigrateCreate
  3270. //
  3271. //
  3272. if( m_lpdvServerMigrated != NULL )
  3273. {
  3274. DPFX(DPFPREP, DVF_INFOLEVEL, "Skipping calling removeplayer again as host already migrated" );
  3275. return DV_OK;
  3276. }
  3277. DWORD dwHostOrderID = DVPROTOCOL_HOSTORDER_INVALID;
  3278. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Player 0x%x is running election algorithm", this->m_dvidLocal );
  3279. // Prevent double-create from host migration run. This can be called by removeplayer and by hostmigrateleave
  3280. // message.
  3281. CDVCSLock m_guardLock(&m_csClassLock);
  3282. m_guardLock.Lock();
  3283. // Trust me. Do this.
  3284. DVID dvidNewHost = m_dvidLocal;
  3285. // Check everyone else in the session, see who has lowest host order ID
  3286. dwHostOrderID = m_voiceNameTable.GetLowestHostOrderID(&dvidNewHost);
  3287. // We're lower then everyone else.. We should become new host
  3288. if( m_dwHostOrderID <= dwHostOrderID )
  3289. {
  3290. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: We're to become the new host" );
  3291. dvidNewHost = m_dvidLocal;
  3292. }
  3293. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: New Host is [0x%x] OrderID [0x%x] Current [0x%x]", dvidNewHost, dwHostOrderID, m_dwMigrateHostOrderID );
  3294. if( m_dwMigrateHostOrderID != DVPROTOCOL_HOSTORDER_INVALID && dwHostOrderID <= m_dwMigrateHostOrderID )
  3295. {
  3296. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: No need to run algorithm again -- in progress (0x%x)", dvidNewHost );
  3297. m_guardLock.Unlock();
  3298. return DV_OK;
  3299. }
  3300. m_dwMigrateHostOrderID = dwHostOrderID;
  3301. m_lpSessionTransport->MigrateHost( dvidNewHost );
  3302. m_dvidServer = m_lpSessionTransport->GetServerID();
  3303. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Host is (2) [0x%x]", m_dvidServer );
  3304. m_guardLock.Unlock();
  3305. // No one was found and we're not properly connected yet
  3306. if( dwHostOrderID == DVPROTOCOL_HOSTORDER_INVALID && m_dwCurrentState != DVCSTATE_CONNECTED )
  3307. {
  3308. DPFX(DPFPREP, DVF_ERRORLEVEL, "There is no one to take over session. Disconnect" );
  3309. // We're connecting.. expected behaviour
  3310. if( m_dwCurrentState == DVCSTATE_CONNECTING )
  3311. {
  3312. DPFX(DPFPREP, DVF_ERRORLEVEL, "Aborting connection..." );
  3313. SendConnectResult();
  3314. SetEvent( m_hConnectAck );
  3315. DoSessionLost(DVERR_SESSIONLOST);
  3316. }
  3317. // We're already disconnecting
  3318. else if( m_dwCurrentState == DVCSTATE_DISCONNECTING )
  3319. {
  3320. DPFX(DPFPREP, DVF_ERRORLEVEL, "Already disconnecting.." );
  3321. }
  3322. }
  3323. // Candidate was found, it's us!
  3324. else if( m_dwHostOrderID <= dwHostOrderID )
  3325. {
  3326. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: We think we're the host! Our Host Order ID=0x%x", m_dwHostOrderID );
  3327. hr = HandleLocalHostMigrateCreate();
  3328. if( FAILED( hr ) )
  3329. {
  3330. DPFX(DPFPREP, DVF_ERRORLEVEL, "Host migrate failed hr=0x%x", hr );
  3331. }
  3332. }
  3333. // Someone was elected -- not us!
  3334. //
  3335. // We send a settings confirm message in case we ignored host migration message
  3336. // during our election. (Small, but reproducable window).
  3337. //
  3338. // If we do get it after this host may get > 1 settings confirm from us (which is handled).
  3339. //
  3340. else if( dwHostOrderID != DVPROTOCOL_HOSTORDER_INVALID )
  3341. {
  3342. hr = Send_SettingsConfirm();
  3343. if( FAILED( hr ) )
  3344. {
  3345. DPFX(DPFPREP, DVF_ERRORLEVEL, "Settings confirm message failed! sent hr=0x%x", hr );
  3346. }
  3347. DVMSG_HOSTMIGRATED dvHostMigrated;
  3348. dvHostMigrated.dvidNewHostID = m_dvidServer;
  3349. dvHostMigrated.pdvServerInterface = NULL;
  3350. dvHostMigrated.dwSize = sizeof( DVMSG_HOSTMIGRATED );
  3351. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Sending notification because of election" );
  3352. NotifyQueue_Add( DVMSGID_HOSTMIGRATED, &dvHostMigrated, sizeof( DVMSG_HOSTMIGRATED ) );
  3353. }
  3354. return DV_OK;
  3355. }
  3356. #undef DPF_MODNAME
  3357. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleDeleteVoicePlayer"
  3358. //
  3359. // HandleDeleteVoicePlayer
  3360. //
  3361. // Handles the DVMSGID_DELETEVOICEPLAYER message.
  3362. //
  3363. BOOL CDirectVoiceClientEngine::HandleDeleteVoicePlayer( DVID dvidSource, PDVPROTOCOLMSG_PLAYERQUIT lpdvDeletePlayer, DWORD dwSize )
  3364. {
  3365. CVoicePlayer *pPlayer;
  3366. HRESULT hr;
  3367. if ( dwSize != sizeof( DVPROTOCOLMSG_PLAYERQUIT ) )
  3368. {
  3369. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleDeleteVoicePlayer() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3370. dwSize, dvidSource );
  3371. return FALSE;
  3372. }
  3373. hr = m_voiceNameTable.GetEntry( lpdvDeletePlayer->dvidID, &pPlayer, TRUE );
  3374. // If there is a player entry for the given ID,
  3375. // Handle removing them from the local player table
  3376. if( pPlayer != NULL )
  3377. {
  3378. // Remove the entry, this will also drop the reference count
  3379. hr = m_voiceNameTable.DeleteEntry( lpdvDeletePlayer->dvidID );
  3380. // Another thread has already remove the player -- we don't need to do the rest
  3381. // of this.
  3382. if( FAILED( hr ) )
  3383. {
  3384. DPFX(DPFPREP, 0, "Error, could not find entry 0x%x to delete hr=0x%x", dvidSource, hr );
  3385. pPlayer->Release();
  3386. return TRUE;
  3387. }
  3388. // Mark player record as disconnected
  3389. pPlayer->SetDisconnected();
  3390. // Wait for global object lock and then remove target
  3391. // CDVCSLock guardLock(&m_csClassLock);
  3392. // guardLock.Lock();
  3393. CheckForAndRemoveTarget( lpdvDeletePlayer->dvidID );
  3394. // guardLock.Unlock();
  3395. // If there are any buffers for this player, delete them
  3396. // We don't need to destroy them, we want to save them so the user can call
  3397. // Delete3DUserBuffer
  3398. //
  3399. //DeleteSoundTarget( lpdvDeletePlayer->dvidID );
  3400. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER )
  3401. {
  3402. DVMSG_DELETEVOICEPLAYER dvMsgDeletePlayer;
  3403. // Event for doing sync wait
  3404. HANDLE hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  3405. dvMsgDeletePlayer.dvidPlayer = lpdvDeletePlayer->dvidID;
  3406. dvMsgDeletePlayer.dwSize = sizeof( DVMSG_DELETEVOICEPLAYER );
  3407. dvMsgDeletePlayer.pvPlayerContext = pPlayer->GetContext();
  3408. pPlayer->SetContext( NULL );
  3409. // By making this synchronous we ensure that the voice notification has completed before the dplay8
  3410. // callback is called.
  3411. //
  3412. NotifyQueue_Add( DVMSGID_DELETEVOICEPLAYER, &dvMsgDeletePlayer, sizeof( DVMSG_DELETEVOICEPLAYER ), &hEvent, NotifyComplete_SyncWait );
  3413. if( hEvent )
  3414. {
  3415. WaitForSingleObject( hEvent, INFINITE );
  3416. CloseHandle( hEvent );
  3417. }
  3418. }
  3419. pPlayer->Release();
  3420. }
  3421. return TRUE;
  3422. }
  3423. #undef DPF_MODNAME
  3424. #define DPF_MODNAME "CDirectVoiceClientEngine::QueueSpeech"
  3425. //
  3426. // QueueSpeech
  3427. //
  3428. // Process and queue incoming audio
  3429. //
  3430. BOOL CDirectVoiceClientEngine::QueueSpeech( DVID dvidSource, PDVPROTOCOLMSG_SPEECHHEADER pdvSpeechHeader, PBYTE pbData, DWORD dwSize )
  3431. {
  3432. CVoicePlayer *pPlayerInfo;
  3433. HRESULT hr;
  3434. // Only start receiving voice if the local player is active
  3435. if( !m_fLocalPlayerAvailable )
  3436. {
  3437. DPFX(DPFPREP, 1, "Ignoring incoming audio, local player has not been indicated" );
  3438. return TRUE;
  3439. }
  3440. hr = m_voiceNameTable.GetEntry( dvidSource, &pPlayerInfo, TRUE );
  3441. if( FAILED( hr ) )
  3442. {
  3443. DPFX(DPFPREP, 1, "Received speech for player who is not in nametable hr=0x%x", hr );
  3444. return TRUE;
  3445. }
  3446. if( !pPlayerInfo->IsAvailable() )
  3447. {
  3448. DPFX(DPFPREP, 1, "Player is not yet available, ignoring speech" );
  3449. }
  3450. else
  3451. {
  3452. hr = pPlayerInfo->HandleReceive( pdvSpeechHeader, pbData, dwSize );
  3453. if( FAILED( hr ) )
  3454. {
  3455. pPlayerInfo->Release();
  3456. DPFX(DPFPREP, 1, "Received speech could not be buffered hr=0x%x", hr );
  3457. return TRUE;
  3458. }
  3459. // STATSBLOCK: Begin
  3460. m_pStatsBlob->m_dwPRESpeech++;
  3461. // STATSBLOCK: End
  3462. }
  3463. // Release our reference to the player
  3464. pPlayerInfo->Release();
  3465. return TRUE;
  3466. }
  3467. #undef DPF_MODNAME
  3468. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleSpeechWithFrom"
  3469. //
  3470. // HandleSpeech
  3471. //
  3472. // Handles speech data messages
  3473. //
  3474. BOOL CDirectVoiceClientEngine::HandleSpeechWithFrom( DVID dvidSource, PDVPROTOCOLMSG_SPEECHWITHFROM lpdvSpeech, DWORD dwSize )
  3475. {
  3476. HRESULT hr;
  3477. if ( dwSize < sizeof( DVPROTOCOLMSG_SPEECHWITHFROM ) )
  3478. {
  3479. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeechWithFrom() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3480. dwSize, dvidSource );
  3481. return FALSE;
  3482. }
  3483. if( !ValidateSpeechPacketSize( m_lpdvfCompressionInfo, dwSize - sizeof( DVPROTOCOLMSG_SPEECHWITHFROM ) ) )
  3484. {
  3485. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeechWithFrom() Ignoring message with invalid speech size, size=0x%x, from=0x%x",
  3486. dwSize, dvidSource );
  3487. return FALSE;
  3488. }
  3489. if( !ValidatePlayerDVID( lpdvSpeech->dvidFrom ) )
  3490. {
  3491. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeechWithFrom() Ignoring message with invalid player dvid, flags=0x%x, from=0x%x",
  3492. lpdvSpeech->dvidFrom, dvidSource );
  3493. return FALSE;
  3494. }
  3495. DPFX(DPFPREP, DVF_INFOLEVEL, "Received multicast speech!" );
  3496. if( lpdvSpeech->dvidFrom == m_dvidLocal )
  3497. {
  3498. DPFX(DPFPREP, DVF_INFOLEVEL, "Ignoring loopback speech!" );
  3499. return TRUE;
  3500. }
  3501. CVoicePlayer *pPlayerInfo;
  3502. hr = m_voiceNameTable.GetEntry( lpdvSpeech->dvidFrom, &pPlayerInfo, TRUE );
  3503. if( FAILED( hr ) )
  3504. {
  3505. DVPROTOCOLMSG_PLAYERJOIN dvPlayerJoin;
  3506. dvPlayerJoin.dwFlags = 0;
  3507. dvPlayerJoin.dvidID = lpdvSpeech->dvidFrom;
  3508. HandleCreateVoicePlayer( lpdvSpeech->dvidFrom, &dvPlayerJoin, sizeof( DVPROTOCOLMSG_PLAYERJOIN ) );
  3509. }
  3510. else
  3511. {
  3512. pPlayerInfo->Release();
  3513. }
  3514. return QueueSpeech( lpdvSpeech->dvidFrom, &lpdvSpeech->dvHeader, (PBYTE) &lpdvSpeech[1], dwSize-sizeof(DVPROTOCOLMSG_SPEECHWITHFROM) );
  3515. }
  3516. #undef DPF_MODNAME
  3517. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleSpeechBounce"
  3518. //
  3519. // HandleSpeech
  3520. //
  3521. // Handles speech data messages
  3522. //
  3523. BOOL CDirectVoiceClientEngine::HandleSpeechBounce( DVID dvidSource, PDVPROTOCOLMSG_SPEECHHEADER lpdvSpeech, DWORD dwSize )
  3524. {
  3525. if ( dwSize < sizeof( DVPROTOCOLMSG_SPEECHHEADER ) )
  3526. {
  3527. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeechBounce() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3528. dwSize, dvidSource );
  3529. return FALSE;
  3530. }
  3531. if( !ValidateSpeechPacketSize( m_lpdvfCompressionInfo, dwSize - sizeof( DVPROTOCOLMSG_SPEECHHEADER ) ) )
  3532. {
  3533. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeechBounce() Ignoring message with invalid speech size, size=0x%x, from=0x%x",
  3534. dwSize, dvidSource );
  3535. return FALSE;
  3536. }
  3537. DPFX(DPFPREP, DVF_INFOLEVEL, "Received speech bounce!" );
  3538. return QueueSpeech( dvidSource, lpdvSpeech, (PBYTE) &lpdvSpeech[1], dwSize - sizeof( DVPROTOCOLMSG_SPEECHHEADER ) );
  3539. }
  3540. #undef DPF_MODNAME
  3541. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleSpeech"
  3542. //
  3543. // HandleSpeech
  3544. //
  3545. // Handles speech data messages
  3546. //
  3547. BOOL CDirectVoiceClientEngine::HandleSpeech( DVID dvidSource, PDVPROTOCOLMSG_SPEECHHEADER lpdvSpeech, DWORD dwSize )
  3548. {
  3549. if ( dwSize < sizeof( DVPROTOCOLMSG_SPEECHHEADER ) )
  3550. {
  3551. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeech() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3552. dwSize, dvidSource );
  3553. return FALSE;
  3554. }
  3555. if( !ValidateSpeechPacketSize( m_lpdvfCompressionInfo, dwSize - sizeof( DVPROTOCOLMSG_SPEECHHEADER ) ) )
  3556. {
  3557. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSpeech() Ignoring message with invalid speech size, size=0x%x, from=0x%x",
  3558. dwSize, dvidSource );
  3559. return FALSE;
  3560. }
  3561. DPFX(DPFPREP, DVF_INFOLEVEL, "Received bare speech!" );
  3562. // Ignore speech from ourselves
  3563. if( dvidSource == m_dvidLocal )
  3564. {
  3565. DPFX(DPFPREP, DVF_INFOLEVEL, "Ignoring loopback speech!" );
  3566. return TRUE;
  3567. }
  3568. return QueueSpeech( dvidSource, lpdvSpeech, (PBYTE) &lpdvSpeech[1], dwSize - sizeof( DVPROTOCOLMSG_SPEECHHEADER ) );
  3569. }
  3570. #undef DPF_MODNAME
  3571. #define DPF_MODNAME "CDirectVoiceClientEngine::Cleanup"
  3572. //
  3573. // Cleanup
  3574. //
  3575. // WARNING: Do not call this function multiple times on the same object.
  3576. //
  3577. // This function shuts down the recording and playback threads, shuts down
  3578. // the sound system and unhooks the object from the dplay object.
  3579. //
  3580. // Called By:
  3581. // - DoDisconnect
  3582. // - Destructor
  3583. // - HandleConnectAccept
  3584. // - NotifyThread
  3585. //
  3586. // Locks Required:
  3587. // - Global Write Lock
  3588. //
  3589. void CDirectVoiceClientEngine::Cleanup()
  3590. {
  3591. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Cleanup called!" );
  3592. // Enter cleanup critical section. Only one instance should be in here at a time.
  3593. DNEnterCriticalSection( &m_csCleanupProtect );
  3594. CDVCSLock guardLock(&m_csClassLock);
  3595. guardLock.Lock();
  3596. // We only need to cleanup if we're not idle
  3597. if( m_dwCurrentState == DVCSTATE_IDLE )
  3598. {
  3599. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Cleanup not required" );
  3600. return;
  3601. }
  3602. if( m_hRecordThreadHandle )
  3603. {
  3604. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Signalling record to terminate" );
  3605. // Signal record thread to shutdown
  3606. SetEvent( m_hRecordTerminate );
  3607. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Record signalled" );
  3608. // Release write lock, if we don't we may have a deadlock
  3609. // because recordthread can't get dplay lock, which means
  3610. // can't shutdown
  3611. //
  3612. // Part of a three way deadlock case.
  3613. guardLock.Unlock();
  3614. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Waiting for record shutdown" );
  3615. WaitForSingleObject( m_hRecordDone, INFINITE );
  3616. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Record shutdown complete" );
  3617. // Re-acquire lock, we need it.
  3618. guardLock.Lock();
  3619. m_hRecordThreadHandle = NULL;
  3620. }
  3621. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Record cleanup" );
  3622. if( m_hRecordTerminate )
  3623. {
  3624. CloseHandle( m_hRecordTerminate );
  3625. m_hRecordTerminate = NULL;
  3626. }
  3627. if( m_hRecordDone )
  3628. {
  3629. CloseHandle( m_hRecordDone );
  3630. m_hRecordDone = NULL;
  3631. }
  3632. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Record cleanup complete" );
  3633. if( m_hPlaybackThreadHandle != NULL )
  3634. {
  3635. // Signal playback thread to shutdown
  3636. SetEvent( m_hPlaybackTerminate );
  3637. // Release write lock to prevent deadlock.
  3638. //
  3639. guardLock.Unlock();
  3640. WaitForSingleObject( m_hPlaybackDone, INFINITE );
  3641. // Re-acquire lock, we need it.
  3642. guardLock.Lock();
  3643. m_hPlaybackThreadHandle = NULL;
  3644. }
  3645. if( m_hPlaybackTerminate )
  3646. {
  3647. CloseHandle( m_hPlaybackTerminate );
  3648. m_hPlaybackTerminate = NULL;
  3649. }
  3650. if( m_hPlaybackDone )
  3651. {
  3652. CloseHandle( m_hPlaybackDone );
  3653. m_hPlaybackDone = NULL;
  3654. }
  3655. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Playback Thread Done" );
  3656. ClientStats_End();
  3657. DPFX(DPFPREP, DVF_INFOLEVEL, "Threads gone!" );
  3658. // If we're running in client/server we need destroy the server
  3659. // buffer.
  3660. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_MIXING ||
  3661. m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_ECHO )
  3662. {
  3663. DeInitializeClientServer();
  3664. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Client/Server Gone" );
  3665. }
  3666. guardLock.Unlock();
  3667. // Disable notifications, no notifications after this point can be made
  3668. NotifyQueue_Disable();
  3669. // The following code is extremely sensitive.
  3670. //
  3671. // Be careful about the order here, it's important.
  3672. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Waiting for outstanding sends" );
  3673. // Wait for outstanding buffer sends to complete before
  3674. // continuing. Otherwise you have potential crash / leak
  3675. // condition
  3676. WaitForBufferReturns();
  3677. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnecting transport" );
  3678. // After this function returns DirectPlay will no longer sends
  3679. // us indication, but some may still be in progress.
  3680. m_lpSessionTransport->DisableReceiveHook( );
  3681. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Flushing notification queue" );
  3682. // Ensuring that all notifications have been sent
  3683. NotifyQueue_Flush();
  3684. // Waiting for transport to return all the threads it is indicating into us on.
  3685. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Waiting for transport threads to complete" );
  3686. m_lpSessionTransport->WaitForDetachCompletion();
  3687. // Re-enable notifications
  3688. NotifyQueue_Enable();
  3689. guardLock.Lock();
  3690. if( m_pTimer != NULL )
  3691. {
  3692. delete m_pTimer;
  3693. m_pTimer = NULL;
  3694. }
  3695. if( m_thTimerInfo.hPlaybackTimerEvent != NULL )
  3696. {
  3697. CloseHandle( m_thTimerInfo.hPlaybackTimerEvent );
  3698. m_thTimerInfo.hPlaybackTimerEvent = NULL;
  3699. }
  3700. if( m_thTimerInfo.hRecordTimerEvent != NULL )
  3701. {
  3702. CloseHandle( m_thTimerInfo.hRecordTimerEvent );
  3703. m_thTimerInfo.hRecordTimerEvent = NULL;
  3704. }
  3705. CleanupPlaybackLists();
  3706. CleanupNotifyLists();
  3707. // Inform player of their own exit if they were connected!
  3708. if( m_fLocalPlayerNotify )
  3709. {
  3710. DVMSG_DELETEVOICEPLAYER dvMsgDelete;
  3711. dvMsgDelete.dvidPlayer = m_dvidLocal;
  3712. dvMsgDelete.dwSize = sizeof( DVMSG_DELETEVOICEPLAYER );
  3713. dvMsgDelete.pvPlayerContext = m_pvLocalPlayerContext;
  3714. m_pvLocalPlayerContext = NULL;
  3715. m_fLocalPlayerNotify = FALSE;
  3716. m_fLocalPlayerAvailable = FALSE;
  3717. TransmitMessage( DVMSGID_DELETEVOICEPLAYER, &dvMsgDelete, sizeof( DVMSG_DELETEVOICEPLAYER ) );
  3718. }
  3719. m_voiceNameTable.DeInitialize( (m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER), m_lpUserContext, m_lpMessageHandler);
  3720. // Hold off on the shutdown of the sound system so user notifications
  3721. // of delete players on unravel of nametable can be handled correctly.
  3722. //
  3723. ShutdownSoundSystem();
  3724. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Sound system shutdown" );
  3725. m_fpPlayers.Deinitialize();
  3726. FreeBuffers();
  3727. if( m_pFramePool != NULL )
  3728. {
  3729. DNEnterCriticalSection( &CDirectVoiceEngine::s_csSTLLock );
  3730. delete m_pFramePool;
  3731. m_pFramePool = NULL;
  3732. DNLeaveCriticalSection( &CDirectVoiceEngine::s_csSTLLock );
  3733. }
  3734. SetCurrentState( DVCSTATE_IDLE );
  3735. DNEnterCriticalSection( &m_csTargetLock );
  3736. if( m_pdvidTargets != NULL )
  3737. {
  3738. delete [] m_pdvidTargets;
  3739. m_pdvidTargets = NULL;
  3740. m_dwNumTargets = 0;
  3741. m_dwTargetVersion = 0;
  3742. }
  3743. DNLeaveCriticalSection( &m_csTargetLock );
  3744. DNLeaveCriticalSection( &m_csCleanupProtect );
  3745. ClientStats_Dump();
  3746. // Remove us from the list of apps running
  3747. PERF_RemoveEntry( m_perfInfo.guidInternalInstance, &m_perfAppInfo );
  3748. m_pStatsBlob = NULL;
  3749. ZeroMemory( &m_perfInfo, sizeof( PERF_APPLICATION ) );
  3750. ZeroMemory( &m_perfAppInfo, sizeof( PERF_APPLICATION_INFO ) );
  3751. SetEvent( m_hDisconnectAck );
  3752. guardLock.Unlock();
  3753. if( m_lpdvServerMigrated != NULL )
  3754. {
  3755. m_lpdvServerMigrated->Release();
  3756. m_lpdvServerMigrated = NULL;
  3757. }
  3758. }
  3759. // WaitForBufferReturns
  3760. //
  3761. // This function waits until oustanding sends have completed before continuing
  3762. // we use this to ensure we don't deregister with outstanding sends.
  3763. //
  3764. #undef DPF_MODNAME
  3765. #define DPF_MODNAME "CDirectVoiceClientEngine::WaitForBufferReturns"
  3766. void CDirectVoiceClientEngine::WaitForBufferReturns()
  3767. {
  3768. if( m_pBufferDescPool == NULL )
  3769. return;
  3770. while( 1 )
  3771. {
  3772. DNEnterCriticalSection( &m_pBufferDescPool->cs );
  3773. if( m_pBufferDescPool->nInUse == 0 )
  3774. {
  3775. DNLeaveCriticalSection( &m_pBufferDescPool->cs );
  3776. break;
  3777. }
  3778. DNLeaveCriticalSection( &m_pBufferDescPool->cs );
  3779. Sleep( 20 );
  3780. }
  3781. return;
  3782. }
  3783. #undef DPF_MODNAME
  3784. #define DPF_MODNAME "CDirectVoiceClientEngine::DoDisconnect"
  3785. //
  3786. // DoDisconnect
  3787. //
  3788. // Performs a disconnection and informs the callback function.
  3789. //
  3790. // Used for both session lost and normal disconnects.
  3791. //
  3792. // Called By:
  3793. // - NotifyThread
  3794. //
  3795. void CDirectVoiceClientEngine::DoDisconnect()
  3796. {
  3797. // Guard to prevent this function from being called more then once on the
  3798. // same object
  3799. if( m_dwCurrentState == DVCSTATE_IDLE ||
  3800. m_dwCurrentState == DVCSTATE_NOTINITIALIZED )
  3801. return;
  3802. m_dwCurrentState = DVCSTATE_DISCONNECTING;
  3803. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "DoDisconnect called!" );
  3804. Cleanup();
  3805. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Cleanup complete" );
  3806. if( m_fSessionLost )
  3807. {
  3808. DVMSG_SESSIONLOST dvSessionLost;
  3809. dvSessionLost.hrResult = m_hrDisconnectResult;
  3810. dvSessionLost.dwSize = sizeof( DVMSG_SESSIONLOST );
  3811. NotifyQueue_Add( DVMSGID_SESSIONLOST, &dvSessionLost, sizeof( DVMSG_SESSIONLOST ) );
  3812. }
  3813. else
  3814. {
  3815. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Signalling disconnect result" );
  3816. SendDisconnectResult();
  3817. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Done signalling disconnect" );
  3818. }
  3819. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Disconnect complete" );
  3820. }
  3821. #undef DPF_MODNAME
  3822. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleDisconnect"
  3823. // HandleDisconnect
  3824. //
  3825. // This function is called when a disconnect message is received from the
  3826. // server.
  3827. //
  3828. BOOL CDirectVoiceClientEngine::HandleDisconnectConfirm( DVID dvidSource, PDVPROTOCOLMSG_DISCONNECT lpdvDisconnect, DWORD dwSize )
  3829. {
  3830. if ( dwSize != sizeof( DVPROTOCOLMSG_DISCONNECT ) )
  3831. {
  3832. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleDisconnectConfirm() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3833. dwSize, dvidSource );
  3834. return FALSE;
  3835. }
  3836. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "DisconnectConfirm received, signalling worker [res=0x%x]", lpdvDisconnect->hresDisconnect );
  3837. DoSignalDisconnect( lpdvDisconnect->hresDisconnect );
  3838. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "DisconnectConfirm received, signalled worker" );
  3839. return TRUE;
  3840. }
  3841. #undef DPF_MODNAME
  3842. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleSessionLost"
  3843. BOOL CDirectVoiceClientEngine::HandleSessionLost( DVID dvidSource, PDVPROTOCOLMSG_SESSIONLOST lpdvSessionLost, DWORD dwSize )
  3844. {
  3845. if ( dwSize != sizeof( DVPROTOCOLMSG_SESSIONLOST ) )
  3846. {
  3847. DPFX( DPFPREP, DVF_ANTIHACK_DEBUG_LEVEL, "DVCE::HandleSessionLost() Ignoring incorrectly sized message, size=0x%x, from=0x%x",
  3848. dwSize, dvidSource );
  3849. return FALSE;
  3850. }
  3851. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::HandleSessionLost() begin" );
  3852. DPFX(DPFPREP, DVF_ERRORLEVEL, "<><><><><><><> Session Host has shutdown - Voice Session is gone." );
  3853. DoSessionLost( lpdvSessionLost->hresReason );
  3854. return TRUE;
  3855. }
  3856. #undef DPF_MODNAME
  3857. #define DPF_MODNAME "CDirectVoiceClientEngine::StartTransportSession"
  3858. HRESULT CDirectVoiceClientEngine::StartTransportSession( )
  3859. {
  3860. return S_OK;
  3861. }
  3862. #undef DPF_MODNAME
  3863. #define DPF_MODNAME "CDirectVoiceClientEngine::StopTransportSession"
  3864. // StopSession
  3865. //
  3866. // This function is called when the directplay session is lost or stops
  3867. // before DirectXVoice is disconnected.
  3868. //
  3869. HRESULT CDirectVoiceClientEngine::StopTransportSession()
  3870. {
  3871. DoSessionLost( DVERR_SESSIONLOST );
  3872. return DV_OK;
  3873. }
  3874. #undef DPF_MODNAME
  3875. #define DPF_MODNAME "CDirectVoiceClientEngine::AddPlayer"
  3876. HRESULT CDirectVoiceClientEngine::AddPlayer( DVID dvID )
  3877. {
  3878. return S_OK;
  3879. }
  3880. #undef DPF_MODNAME
  3881. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleLocalHostMigrateCreate"
  3882. HRESULT CDirectVoiceClientEngine::HandleLocalHostMigrateCreate()
  3883. {
  3884. LPDIRECTVOICESERVEROBJECT lpdvsServerObject = NULL;
  3885. LPBYTE lpSessionBuffer = NULL;
  3886. DWORD dwSessionSize = 0;
  3887. HRESULT hr = DP_OK;
  3888. CDirectVoiceDirectXTransport *pTransport;
  3889. // Prevent double-create from host migration run.
  3890. CDVCSLock m_guardLock(&m_csClassLock);
  3891. m_guardLock.Lock();
  3892. if( m_lpdvServerMigrated )
  3893. {
  3894. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Duplicate host create received. Ignoring" );
  3895. return DV_OK;
  3896. }
  3897. DPFX(DPFPREP, DVF_ERRORLEVEL, "Local client has become the new host. Creating a host" );
  3898. hr = DVS_Create( &lpdvsServerObject );
  3899. if( FAILED( hr ) )
  3900. {
  3901. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create server object. hr=0x%x", hr );
  3902. goto HOSTCREATE_FAILURE;
  3903. }
  3904. // Grab a reference local of the new host interface
  3905. m_lpdvServerMigrated = (LPDIRECTPLAYVOICESERVER) lpdvsServerObject;
  3906. m_guardLock.Unlock();
  3907. IncrementObjectCount();
  3908. lpdvsServerObject->lIntRefCnt++;
  3909. DVMSG_LOCALHOSTSETUP dvMsgLocalHostSetup;
  3910. dvMsgLocalHostSetup.dwSize = sizeof( DVMSG_LOCALHOSTSETUP );
  3911. dvMsgLocalHostSetup.pvContext = NULL;
  3912. dvMsgLocalHostSetup.pMessageHandler = NULL;
  3913. TransmitMessage( DVMSGID_LOCALHOSTSETUP, &dvMsgLocalHostSetup, sizeof( DVMSG_LOCALHOSTSETUP ) );
  3914. pTransport = (CDirectVoiceDirectXTransport *) m_lpSessionTransport;
  3915. hr = DV_Initialize( lpdvsServerObject, pTransport->GetTransportInterface(), dvMsgLocalHostSetup.pMessageHandler, dvMsgLocalHostSetup.pvContext, NULL, 0 );
  3916. if( FAILED( hr ) )
  3917. {
  3918. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to initialize the server object hr=0x%x", hr );
  3919. goto HOSTCREATE_FAILURE;
  3920. }
  3921. hr = lpdvsServerObject->lpDVServerEngine->HostMigrateStart( &m_dvSessionDesc, m_dwHostOrderID+DVMIGRATE_ORDERID_OFFSET );
  3922. if( FAILED( hr ) )
  3923. {
  3924. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error starting server object hr=0x%x", hr );
  3925. goto HOSTCREATE_FAILURE;
  3926. }
  3927. DVMSG_HOSTMIGRATED dvHostMigrated;
  3928. dvHostMigrated.dvidNewHostID = m_lpSessionTransport->GetLocalID();
  3929. dvHostMigrated.pdvServerInterface = (LPDIRECTPLAYVOICESERVER) lpdvsServerObject;
  3930. dvHostMigrated.dwSize = sizeof( DVMSG_HOSTMIGRATED );
  3931. NotifyQueue_Add( DVMSGID_HOSTMIGRATED, &dvHostMigrated, sizeof( DVMSG_HOSTMIGRATED ) );
  3932. return DV_OK;
  3933. HOSTCREATE_FAILURE:
  3934. DPFX(DPFPREP, DVF_ERRORLEVEL, "Informing clients of our failure to create host" );
  3935. Send_SessionLost();
  3936. if( lpdvsServerObject != NULL )
  3937. {
  3938. DVS_Release( lpdvsServerObject );
  3939. }
  3940. return hr;
  3941. }
  3942. // Handles remove player message
  3943. //
  3944. // This message triggers handling of host migration if the player
  3945. // who has dropped out happens to be the session host.
  3946. //
  3947. #undef DPF_MODNAME
  3948. #define DPF_MODNAME "CDirectVoiceClientEngine::RemovePlayer"
  3949. HRESULT CDirectVoiceClientEngine::RemovePlayer( DVID dvID )
  3950. {
  3951. HRESULT hr;
  3952. CDVCSLock m_guardLock(&m_csClassLock);
  3953. m_guardLock.Lock();
  3954. if( m_dwCurrentState == DVCSTATE_DISCONNECTING )
  3955. {
  3956. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Ignoring transport disconnect for 0x%x -- client is disconnecting", dvID );
  3957. return DV_OK;
  3958. }
  3959. m_guardLock.Unlock();
  3960. CVoicePlayer *pVoicePlayer;
  3961. /*hr = m_voiceNameTable.GetEntry( dvID, &pVoicePlayer, FALSE );
  3962. if( FAILED( hr ) )
  3963. {
  3964. DPFX(DPFPREP, 5, "Ignoring duplicate removeplayer hr=0x%x", hr );
  3965. return DV_OK;
  3966. }*/
  3967. DVPROTOCOLMSG_PLAYERQUIT dvPlayerQuit;
  3968. dvPlayerQuit.dwType = DVMSGID_DELETEVOICEPLAYER;
  3969. dvPlayerQuit.dvidID = dvID;
  3970. HandleDeleteVoicePlayer( 0, &dvPlayerQuit, sizeof( DVPROTOCOLMSG_PLAYERQUIT ) );
  3971. // The person who dropped out was the server
  3972. if( dvID == m_lpSessionTransport->GetServerID() )
  3973. {
  3974. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Checking to see if remove of 0x%x is host 0x%x", dvID, m_lpSessionTransport->GetServerID() );
  3975. DPFX(DPFPREP, DVF_HOSTMIGRATE_DEBUG_LEVEL, "HOST MIGRATION: Triggered by Remove Player Message" );
  3976. MigrateHost_RunElection();
  3977. }
  3978. return S_OK;
  3979. }
  3980. #undef DPF_MODNAME
  3981. #define DPF_MODNAME "CDirectVoiceClientEngine::SetCurrentState"
  3982. // SetCurrentState
  3983. //
  3984. // Sets the current state of the client engine
  3985. //
  3986. void CDirectVoiceClientEngine::SetCurrentState( DWORD dwState )
  3987. {
  3988. CDVCSLock m_guardLock(&m_csClassLock);
  3989. m_guardLock.Lock();
  3990. m_dwCurrentState = dwState;
  3991. m_guardLock.Unlock();
  3992. }
  3993. #undef DPF_MODNAME
  3994. #define DPF_MODNAME "CDirectVoiceClientEngine::CheckShouldSendMessage"
  3995. //
  3996. // CheckShouldSendMessage
  3997. //
  3998. // Checks the notification mask to see if the specified message type should
  3999. // be sent to the user.
  4000. //
  4001. BOOL CDirectVoiceClientEngine::CheckShouldSendMessage( DWORD dwMessageType )
  4002. {
  4003. if( m_lpMessageHandler == NULL )
  4004. {
  4005. return FALSE;
  4006. }
  4007. BFCSingleLock slLock( &m_csNotifyLock );
  4008. slLock.Lock();
  4009. BOOL fSend = FALSE;
  4010. if( m_dwNumMessageElements == 0 )
  4011. {
  4012. return TRUE;
  4013. }
  4014. else
  4015. {
  4016. for( DWORD dwIndex = 0; dwIndex < m_dwNumMessageElements; dwIndex++ )
  4017. {
  4018. if( m_lpdwMessageElements[dwIndex] == dwMessageType )
  4019. {
  4020. return TRUE;
  4021. }
  4022. }
  4023. }
  4024. return FALSE;
  4025. }
  4026. #undef DPF_MODNAME
  4027. #define DPF_MODNAME "CDirectVoiceClientEngine::TransmitMessage"
  4028. //
  4029. // TransmitMessage
  4030. //
  4031. // Called to send a notification to the user.
  4032. //
  4033. // Only the notify thread should call this function, all other threads should queue up
  4034. // notifications by calling NotifyQueue_Add.
  4035. //
  4036. // Called By:
  4037. // - NotifyThread.
  4038. //
  4039. void CDirectVoiceClientEngine::TransmitMessage( DWORD dwMessageType, LPVOID lpData, DWORD dwSize )
  4040. {
  4041. if( CheckShouldSendMessage( dwMessageType ) )
  4042. {
  4043. (*m_lpMessageHandler)( m_lpUserContext, dwMessageType, (!dwSize) ? NULL : lpData );
  4044. }
  4045. }
  4046. #undef DPF_MODNAME
  4047. #define DPF_MODNAME "CDirectVoiceClientEngine::CheckForDuplicateObjects"
  4048. HRESULT CDirectVoiceClientEngine::CheckForDuplicateObjects()
  4049. {
  4050. HRESULT hr;
  4051. LPKSPROPERTYSET lpksPropSet = NULL;
  4052. DSPROPERTY_DIRECTSOUND_OBJECTS_DATA* pDSList = NULL;
  4053. DSPROPERTY_DIRECTSOUNDCAPTURE_OBJECTS_DATA* pDSCList = NULL;
  4054. DWORD dwIndex;
  4055. BOOL fFound;
  4056. hr = DirectSoundPrivateCreate( &lpksPropSet );
  4057. if( FAILED( hr ) )
  4058. {
  4059. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to check for usage of duplicate devices hr=0x%x", hr );
  4060. return DV_OK;
  4061. }
  4062. hr = PrvGetDirectSoundObjects( lpksPropSet, GUID_NULL, &pDSList );
  4063. if( FAILED( hr ) )
  4064. {
  4065. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to retrieve process sound objs hr=0x%x", hr );
  4066. hr = DV_OK;
  4067. }
  4068. // Check list for duplicate of the one we are using.
  4069. else
  4070. {
  4071. // User specified a device, be nice and print a debug message if the object doesn't
  4072. // match the GUID
  4073. if( m_dvSoundDeviceConfig.lpdsPlaybackDevice != NULL )
  4074. {
  4075. fFound = FALSE;
  4076. // Check the internal list
  4077. for( dwIndex = 0; dwIndex < pDSList->Count; dwIndex++ )
  4078. {
  4079. // Check to see if object user specified matches this one.
  4080. if( pDSList->Objects[dwIndex].DirectSound == m_dvSoundDeviceConfig.lpdsPlaybackDevice )
  4081. {
  4082. if( m_dvSoundDeviceConfig.guidPlaybackDevice != pDSList->Objects[dwIndex].DeviceId )
  4083. {
  4084. // Expected behaviour with emulated object
  4085. DPFX(DPFPREP, DVF_ERRORLEVEL, "Specified GUID is not correct for specified dsound object" );
  4086. fFound = FALSE;
  4087. }
  4088. else
  4089. {
  4090. fFound = TRUE;
  4091. DPFX(DPFPREP, DVF_INFOLEVEL, "GUID for device matches object. User param valid" );
  4092. }
  4093. break;
  4094. }
  4095. }
  4096. // We didn't find specified object in dsound's list.
  4097. // Could be an error condition, but would prevent emulated objects from working.
  4098. if( !fFound )
  4099. {
  4100. DPFX(DPFPREP, DVF_ERRORLEVEL, "Specified DirectSound object does not exist" );
  4101. }
  4102. }
  4103. // No object specified.
  4104. else
  4105. {
  4106. // Check the internal list to see if we need to make use of an existing object
  4107. for( dwIndex = 0; dwIndex < pDSList->Count; dwIndex++ )
  4108. {
  4109. // We have a winner, a matching playback device.
  4110. if( m_dvSoundDeviceConfig.guidPlaybackDevice == pDSList->Objects[dwIndex].DeviceId )
  4111. {
  4112. hr = pDSList->Objects[dwIndex].DirectSound->QueryInterface( IID_IDirectSound, (void **) &m_dvSoundDeviceConfig.lpdsPlaybackDevice );
  4113. if( FAILED( hr ) )
  4114. {
  4115. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to retrieve dsound int for existing dsound object hr=0x%x", hr );
  4116. hr = DVERR_INVALIDDEVICE;
  4117. goto EXIT_SOUND_CHECK;
  4118. }
  4119. break;
  4120. }
  4121. }
  4122. }
  4123. }
  4124. hr = PrvGetDirectSoundCaptureObjects( lpksPropSet, GUID_NULL, &pDSCList );
  4125. if( FAILED( hr ) )
  4126. {
  4127. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to retrieve process cap objs hr=0x%x", hr );
  4128. hr = DV_OK;
  4129. }
  4130. // Check list for duplicate of the one we are using
  4131. else
  4132. {
  4133. // User specified a device, be nice and print a debug message if the object doesn't
  4134. // match the GUID
  4135. if( m_dvSoundDeviceConfig.lpdsCaptureDevice != NULL )
  4136. {
  4137. fFound = FALSE;
  4138. // Check the internal list
  4139. for( dwIndex = 0; dwIndex < pDSCList->Count; dwIndex++ )
  4140. {
  4141. // Check to see if object user specified matches this one.
  4142. if( pDSCList->Objects[dwIndex].DirectSoundCapture == m_dvSoundDeviceConfig.lpdsCaptureDevice )
  4143. {
  4144. if( m_dvSoundDeviceConfig.guidCaptureDevice != pDSCList->Objects[dwIndex].DeviceId )
  4145. {
  4146. // Expected behaviour with emulated object
  4147. DPFX(DPFPREP, DVF_ERRORLEVEL, "Specified GUID is not correct for specified dsound object" );
  4148. fFound = FALSE;
  4149. }
  4150. else
  4151. {
  4152. fFound = TRUE;
  4153. DPFX(DPFPREP, DVF_INFOLEVEL, "GUID for device matches object. User param valid" );
  4154. }
  4155. break;
  4156. }
  4157. }
  4158. // We didn't find specified object in dsound's list.
  4159. // Could be an error condition, but would prevent emulated objects from working.
  4160. if( !fFound )
  4161. {
  4162. DPFX(DPFPREP, DVF_ERRORLEVEL, "Specified DirectSoundCap object does not exist" );
  4163. }
  4164. }
  4165. }
  4166. EXIT_SOUND_CHECK:
  4167. if( pDSList != NULL )
  4168. {
  4169. delete [] pDSList;
  4170. }
  4171. if( pDSCList != NULL )
  4172. {
  4173. delete [] pDSCList;
  4174. }
  4175. lpksPropSet->Release();
  4176. return hr;
  4177. }
  4178. #undef DPF_MODNAME
  4179. #define DPF_MODNAME "CDirectVoiceClientEngine::InitializeSoundSystem"
  4180. // InitializeSoundSystem
  4181. //
  4182. // Starts up the sound system based on the parameters.
  4183. //
  4184. HRESULT CDirectVoiceClientEngine::InitializeSoundSystem()
  4185. {
  4186. HRESULT hr;
  4187. Diagnostics_Begin( s_fDumpDiagnostics, "dpv_main.txt" );
  4188. DSERRTRACK_Reset();
  4189. Diagnostics_DeviceInfo( &m_dvSoundDeviceConfig.guidPlaybackDevice, &m_dvSoundDeviceConfig.guidCaptureDevice );
  4190. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::InitializeSoundSystem() Begin" );
  4191. // Note: Mapping of default devices has already been performed.
  4192. // On Pre DX7.1 systems, all default devices map to GUID_NULL
  4193. // On DX7.1 and later, default device will have been mapped to their real GUIDs
  4194. //
  4195. m_dwCompressedFrameSize = m_lpdvfCompressionInfo->dwFrameLength;
  4196. m_dwUnCompressedFrameSize = DVCDB_CalcUnCompressedFrameSize( m_lpdvfCompressionInfo, s_lpwfxPlaybackFormat );
  4197. m_dwNumPerBuffer = m_lpdvfCompressionInfo->dwFramesPerBuffer;
  4198. // Setup the description for the main playback buffer
  4199. //
  4200. // Needs to be after above because it depends on proper values above
  4201. SetupPlaybackBufferDesc( &m_dsBufferDesc, NULL );
  4202. // If they gave us an object, just use it
  4203. if( m_dvSoundDeviceConfig.lpdsPlaybackDevice != NULL )
  4204. {
  4205. CDirectSoundPlaybackDevice *tmpDevice;
  4206. tmpDevice = new CDirectSoundPlaybackDevice( );
  4207. if( tmpDevice == NULL )
  4208. {
  4209. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to alloc memory" );
  4210. return DVERR_OUTOFMEMORY;
  4211. }
  4212. hr = tmpDevice->Initialize( m_dvSoundDeviceConfig.lpdsPlaybackDevice, m_dvSoundDeviceConfig.guidPlaybackDevice );
  4213. if( FAILED( hr ) )
  4214. {
  4215. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error initalizing playback device from specified object hr=0x%x", hr );
  4216. delete tmpDevice;
  4217. return hr;
  4218. }
  4219. m_audioPlaybackDevice = tmpDevice;
  4220. }
  4221. // If they gave us an object, just use it
  4222. if( m_dvSoundDeviceConfig.lpdsCaptureDevice != NULL )
  4223. {
  4224. CDirectSoundCaptureRecordDevice *tmpRecDevice;
  4225. tmpRecDevice = new CDirectSoundCaptureRecordDevice( );
  4226. if( tmpRecDevice == NULL )
  4227. {
  4228. if( m_audioPlaybackDevice != NULL )
  4229. {
  4230. delete m_audioPlaybackDevice;
  4231. m_audioPlaybackDevice = NULL;
  4232. }
  4233. Diagnostics_Write( DVF_ERRORLEVEL, "Failed to alloc memory" );
  4234. return DVERR_OUTOFMEMORY;
  4235. }
  4236. hr = tmpRecDevice->Initialize( m_dvSoundDeviceConfig.lpdsCaptureDevice, m_dvSoundDeviceConfig.guidCaptureDevice );
  4237. if( FAILED( hr ) )
  4238. {
  4239. Diagnostics_Write( DVF_ERRORLEVEL, "Error initializing record device from specified object hr=0x%x", hr );
  4240. if( m_audioPlaybackDevice != NULL )
  4241. {
  4242. delete m_audioPlaybackDevice;
  4243. m_audioPlaybackDevice = NULL;
  4244. }
  4245. delete tmpRecDevice;
  4246. return hr;
  4247. }
  4248. m_audioRecordDevice = tmpRecDevice;
  4249. }
  4250. // We were passed a buffer by the user
  4251. if( m_dvSoundDeviceConfig.lpdsMainBuffer )
  4252. {
  4253. m_audioPlaybackBuffer = new CDirectSoundPlaybackBuffer( m_dvSoundDeviceConfig.lpdsMainBuffer );
  4254. if( !m_audioPlaybackBuffer )
  4255. {
  4256. Diagnostics_Write( DVF_ERRORLEVEL, "Error allocating memory" );
  4257. if( m_audioPlaybackDevice )
  4258. {
  4259. delete m_audioPlaybackDevice;
  4260. m_audioPlaybackDevice = NULL;
  4261. }
  4262. if( m_audioRecordDevice )
  4263. {
  4264. delete m_audioRecordDevice;
  4265. m_audioRecordDevice = NULL;
  4266. }
  4267. return DVERR_OUTOFMEMORY;
  4268. }
  4269. DPFX(DPFPREP, DVF_INFOLEVEL, "Creating a buffer using buffer user gave us." );
  4270. }
  4271. // If we haven't initialized half duplex
  4272. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX) )
  4273. {
  4274. hr = InitFullDuplex( m_dvSoundDeviceConfig.hwndAppWindow,
  4275. m_dvSoundDeviceConfig.guidPlaybackDevice,
  4276. &m_audioPlaybackDevice,
  4277. &m_dsBufferDesc,
  4278. &m_audioPlaybackBuffer,
  4279. m_dvSoundDeviceConfig.guidCaptureDevice,
  4280. &m_audioRecordDevice,
  4281. &m_audioRecordBuffer,
  4282. m_lpdvfCompressionInfo->guidType,
  4283. this->s_lpwfxPrimaryFormat,
  4284. this->s_lpwfxPlaybackFormat,
  4285. this->s_fASO,
  4286. this->m_dvSoundDeviceConfig.dwMainBufferPriority,
  4287. this->m_dvSoundDeviceConfig.dwMainBufferFlags,
  4288. m_dvSoundDeviceConfig.dwFlags );
  4289. // Full duplex init failed, set the half duplex flag
  4290. if( FAILED( hr ) )
  4291. {
  4292. // Records are deleted here because not needed for half-duplex
  4293. if( m_audioRecordDevice != NULL )
  4294. {
  4295. delete m_audioRecordDevice;
  4296. m_audioRecordDevice = NULL;
  4297. }
  4298. if( m_audioRecordBuffer != NULL )
  4299. {
  4300. delete m_audioRecordBuffer;
  4301. m_audioRecordBuffer = NULL;
  4302. }
  4303. // If we got playbacks from user then let them fall through to half-duplex
  4304. // If we allocated them in InitFullDuplex - then it cleans up for itself on error
  4305. m_dvSoundDeviceConfig.dwFlags |= DVSOUNDCONFIG_HALFDUPLEX;
  4306. }
  4307. // Full duplex passed
  4308. else
  4309. {
  4310. }
  4311. if( hr == E_OUTOFMEMORY )
  4312. {
  4313. DPFX(DPFPREP, DVF_ERRORLEVEL, "Full duplex init received an E_OUTOFMEMORY, failing Initialization()." );
  4314. return DVERR_OUTOFMEMORY;
  4315. }
  4316. }
  4317. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX )
  4318. {
  4319. hr = InitHalfDuplex( m_dvSoundDeviceConfig.hwndAppWindow,
  4320. m_dvSoundDeviceConfig.guidPlaybackDevice,
  4321. &m_audioPlaybackDevice,
  4322. &m_dsBufferDesc,
  4323. &m_audioPlaybackBuffer,
  4324. m_lpdvfCompressionInfo->guidType,
  4325. this->s_lpwfxPrimaryFormat,
  4326. this->s_lpwfxPlaybackFormat,
  4327. this->m_dvSoundDeviceConfig.dwMainBufferPriority,
  4328. this->m_dvSoundDeviceConfig.dwMainBufferFlags,
  4329. m_dvSoundDeviceConfig.dwFlags );
  4330. if( FAILED( hr ) )
  4331. {
  4332. // if user supplied these then we need to nuke them here
  4333. // if user didn't supply, then they were nuked on error in InitHalfDuplex
  4334. if( m_audioPlaybackDevice != NULL )
  4335. {
  4336. delete m_audioPlaybackDevice;
  4337. m_audioPlaybackDevice = NULL;
  4338. }
  4339. if( m_audioPlaybackBuffer != NULL )
  4340. {
  4341. delete m_audioPlaybackBuffer;
  4342. m_audioPlaybackBuffer = NULL;
  4343. }
  4344. return hr;
  4345. }
  4346. }
  4347. // Build the frame pool
  4348. DNEnterCriticalSection( &CDirectVoiceEngine::s_csSTLLock );
  4349. m_pFramePool = new CFramePool( m_dwCompressedFrameSize );
  4350. DNLeaveCriticalSection( &CDirectVoiceEngine::s_csSTLLock );
  4351. m_lpstBufferList = NULL;
  4352. if( m_pFramePool == NULL )
  4353. {
  4354. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to allocate frame pool" );
  4355. return DVERR_OUTOFMEMORY;
  4356. }
  4357. if (!m_pFramePool->Init())
  4358. {
  4359. delete m_pFramePool;
  4360. return DVERR_OUTOFMEMORY;
  4361. }
  4362. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_SETCONVERSIONQUALITY )
  4363. {
  4364. if( m_dvSoundDeviceConfig.guidPlaybackDevice != GUID_NULL )
  4365. {
  4366. hr = m_audioPlaybackDevice->GetMixerQuality( &m_dwOriginalPlayQuality );
  4367. if( FAILED( hr ) )
  4368. {
  4369. DPFX(DPFPREP, 1, "Unable to get current playback quality hr=0x%x", hr );
  4370. m_dwOriginalPlayQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4371. }
  4372. // We're already at the setting, someone else probably runinng, disable restore
  4373. else if( m_dwOriginalPlayQuality == DIRECTSOUNDMIXER_SRCQUALITY_ADVANCED )
  4374. {
  4375. DPFX(DPFPREP, 1, "Quality setting is already at correct value. Will not set/restore" );
  4376. m_dwOriginalPlayQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4377. }
  4378. else
  4379. {
  4380. hr = m_audioPlaybackDevice->SetMixerQuality( DIRECTSOUNDMIXER_SRCQUALITY_ADVANCED );
  4381. }
  4382. }
  4383. else
  4384. {
  4385. hr = DVERR_NOTSUPPORTED;
  4386. }
  4387. if( FAILED( hr ) )
  4388. {
  4389. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to set mixer quality for playback device hr=0x%x", hr );
  4390. }
  4391. }
  4392. else
  4393. {
  4394. m_dwOriginalPlayQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4395. }
  4396. // Add a reference for the soundconfig struct if one doesn't exist
  4397. if( m_dvSoundDeviceConfig.lpdsPlaybackDevice == NULL )
  4398. {
  4399. m_dvSoundDeviceConfig.lpdsPlaybackDevice = m_audioPlaybackDevice->GetPlaybackDevice();
  4400. m_dvSoundDeviceConfig.lpdsPlaybackDevice->AddRef();
  4401. }
  4402. // If we're not half duplex, do some initial sets for the recording system
  4403. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX) &&
  4404. m_audioRecordDevice != NULL )
  4405. {
  4406. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_SETCONVERSIONQUALITY )
  4407. {
  4408. DPFX(DPFPREP, DVF_INFOLEVEL, "Setting quality" );
  4409. DV_DUMP_GUID( m_dvSoundDeviceConfig.guidCaptureDevice );
  4410. if( m_dvSoundDeviceConfig.lpdsCaptureDevice == NULL )
  4411. {
  4412. m_dvSoundDeviceConfig.lpdsCaptureDevice = m_audioRecordDevice->GetCaptureDevice();
  4413. m_dvSoundDeviceConfig.lpdsCaptureDevice->AddRef();
  4414. }
  4415. if( m_dvSoundDeviceConfig.guidCaptureDevice != GUID_NULL )
  4416. {
  4417. DPFX(DPFPREP, DVF_INFOLEVEL, "Setting quality 2" );
  4418. hr = m_audioRecordDevice->GetMixerQuality( &m_dwOriginalRecordQuality );
  4419. if( FAILED( hr ) )
  4420. {
  4421. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to get conversion quality hr=0x%x", hr );
  4422. m_dwOriginalRecordQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4423. }
  4424. // We're already at the setting, someone else probably runinng, disable restore
  4425. else if( m_dwOriginalRecordQuality == DIRECTSOUNDMIXER_SRCQUALITY_ADVANCED )
  4426. {
  4427. DPFX(DPFPREP, 1, "Play Quality setting is already at correct value. Will not set/restore" );
  4428. m_dwOriginalRecordQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4429. }
  4430. else
  4431. {
  4432. hr = m_audioRecordDevice->SetMixerQuality( DIRECTSOUNDMIXER_SRCQUALITY_ADVANCED );
  4433. }
  4434. }
  4435. else
  4436. {
  4437. hr = DVERR_NOTSUPPORTED;
  4438. }
  4439. if( FAILED( hr ) )
  4440. {
  4441. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Setting failed" );
  4442. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Unable to set mixer quality for record device hr=0x%x", hr );
  4443. }
  4444. }
  4445. else
  4446. {
  4447. m_dwOriginalRecordQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4448. }
  4449. // If we haven't set no focus
  4450. if( !(m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_NOFOCUS ) )
  4451. {
  4452. // Recording is started muted
  4453. if( m_dvClientConfig.dwFlags & DVCLIENTCONFIG_RECORDMUTE )
  4454. {
  4455. DPFX(DPFPREP, DVF_INFOLEVEL, "Record Muted: Yielding focus" );
  4456. hr = m_audioRecordBuffer->YieldFocus();
  4457. }
  4458. else
  4459. {
  4460. DPFX(DPFPREP, DVF_INFOLEVEL, "Record Un-Muted: Attempting to reclaim focus" );
  4461. hr = m_audioRecordBuffer->ClaimFocus();
  4462. }
  4463. if( FAILED( hr ) )
  4464. {
  4465. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Focus set failed hr=0x%x", hr );
  4466. }
  4467. }
  4468. LONG lVolume;
  4469. hr = m_audioRecordBuffer->GetVolume( &lVolume );
  4470. if( FAILED( hr ) )
  4471. {
  4472. Diagnostics_Write( 0, "Unable to retrieve recording volume hr=0x%x", hr );
  4473. Diagnostics_Write(0, "Disabling recording controls" );
  4474. m_dvSoundDeviceConfig.dwFlags |= DVSOUNDCONFIG_NORECVOLAVAILABLE;
  4475. }
  4476. }
  4477. else
  4478. {
  4479. m_dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
  4480. }
  4481. // Initialize the sound target list
  4482. InitSoundTargetList();
  4483. hr = CreateGeneralBuffer();
  4484. if( FAILED( hr ) )
  4485. {
  4486. Diagnostics_Write( 0, "Error creating general buffer hr=0x%x", hr );
  4487. return hr;
  4488. }
  4489. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::InitializeSoundSystem() End" );
  4490. return DV_OK;
  4491. }
  4492. #undef DPF_MODNAME
  4493. #define DPF_MODNAME "CDirectVoiceClientEngine::ShutdownSoundSystem"
  4494. // ShutdownSoundSystem
  4495. //
  4496. // Stop the sound system
  4497. //
  4498. HRESULT CDirectVoiceClientEngine::ShutdownSoundSystem()
  4499. {
  4500. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::ShutdownSoundSystem() Begin" );
  4501. HRESULT hr;
  4502. FreeSoundTargetList();
  4503. #ifndef __DISABLE_SOUND
  4504. if( m_audioRecordBuffer != NULL )
  4505. {
  4506. delete m_audioRecordBuffer;
  4507. m_audioRecordBuffer = NULL;
  4508. }
  4509. if( m_dvSoundDeviceConfig.lpdsMainBuffer != NULL )
  4510. {
  4511. m_dvSoundDeviceConfig.lpdsMainBuffer->Release();
  4512. m_dvSoundDeviceConfig.lpdsMainBuffer = NULL;
  4513. }
  4514. if( m_audioRecordDevice != NULL )
  4515. {
  4516. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_SETCONVERSIONQUALITY )
  4517. {
  4518. if( m_dwOriginalRecordQuality != DV_CLIENT_SRCQUALITY_INVALID )
  4519. {
  4520. hr = m_audioRecordDevice->SetMixerQuality( m_dwOriginalRecordQuality );
  4521. if( FAILED( hr ) )
  4522. {
  4523. DPFX(DPFPREP, 1, "Failed to restore original recording quality hr=0x%x", hr );
  4524. }
  4525. }
  4526. m_dwOriginalRecordQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4527. }
  4528. delete m_audioRecordDevice;
  4529. m_audioRecordDevice = NULL;
  4530. }
  4531. if( m_audioPlaybackBuffer != NULL )
  4532. {
  4533. delete m_audioPlaybackBuffer;
  4534. m_audioPlaybackBuffer = NULL;
  4535. }
  4536. if( m_audioPlaybackDevice != NULL )
  4537. {
  4538. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_SETCONVERSIONQUALITY )
  4539. {
  4540. if( m_dwOriginalPlayQuality != DV_CLIENT_SRCQUALITY_INVALID )
  4541. {
  4542. hr = m_audioPlaybackDevice->SetMixerQuality( m_dwOriginalPlayQuality );
  4543. if( FAILED( hr ) )
  4544. {
  4545. DPFX(DPFPREP, 1, "Failed to restore original playback quality hr=0x%x", hr );
  4546. }
  4547. }
  4548. m_dwOriginalPlayQuality = DV_CLIENT_SRCQUALITY_INVALID;
  4549. }
  4550. delete m_audioPlaybackDevice;
  4551. m_audioPlaybackDevice = NULL;
  4552. }
  4553. #endif
  4554. if( m_dvSoundDeviceConfig.lpdsCaptureDevice != NULL )
  4555. {
  4556. m_dvSoundDeviceConfig.lpdsCaptureDevice->Release();
  4557. m_dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
  4558. }
  4559. if( m_dvSoundDeviceConfig.lpdsPlaybackDevice != NULL )
  4560. {
  4561. m_dvSoundDeviceConfig.lpdsPlaybackDevice->Release();
  4562. m_dvSoundDeviceConfig.lpdsPlaybackDevice = NULL;
  4563. }
  4564. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::ShutdownSoundSystem() End" );
  4565. Diagnostics_End();
  4566. return DV_OK;
  4567. }
  4568. #undef DPF_MODNAME
  4569. #define DPF_MODNAME "CDirectVoiceClientEngine::SetConnectResult"
  4570. // SetConnectResult
  4571. //
  4572. // This function stores the specified connect result code in m_hrOriginalConnectResult
  4573. // and stores the "translated" error code in m_hrConnectResult. m_hrConnectResult
  4574. // is used to return the result to the user.
  4575. //
  4576. // For some error codes the voice layer translates the code to a voice specific error,
  4577. // this is why this function is required.
  4578. //
  4579. void CDirectVoiceClientEngine::SetConnectResult( HRESULT hrConnectResult )
  4580. {
  4581. DPFX( DPFPREP, DVF_CONNECT_PROCEDURE_DEBUG_LEVEL, "CONNECT RESULT: Transition [0x%x] to [0x%x]", m_hrOriginalConnectResult, hrConnectResult );
  4582. m_hrOriginalConnectResult = hrConnectResult;
  4583. if( HRESULT_FACILITY( hrConnectResult ) == static_cast<HRESULT>(_FACDS) )
  4584. {
  4585. if( hrConnectResult == DSERR_ALLOCATED )
  4586. {
  4587. DPFX( DPFPREP, DVF_WARNINGLEVEL, "WARNING: Mapping DSERR_ALLOCATED --> DVERR_PLAYBACKSYSTEMERROR" );
  4588. m_hrConnectResult = DVERR_PLAYBACKSYSTEMERROR;
  4589. }
  4590. else if( hrConnectResult == DSERR_OUTOFMEMORY )
  4591. {
  4592. DPFX( DPFPREP, DVF_WARNINGLEVEL, "WARNING: Mapping DSERR_OUTOFMEMORY --> DVERR_OUTOFMEMORY" );
  4593. m_hrConnectResult = DVERR_OUTOFMEMORY;
  4594. }
  4595. else if( hrConnectResult == DSERR_NODRIVER )
  4596. {
  4597. DPFX( DPFPREP, DVF_WARNINGLEVEL, "WARNING: Mapping DSERR_NODRIVER --> DVERR_INVALIDDEVICE" );
  4598. m_hrConnectResult = DVERR_INVALIDDEVICE;
  4599. }
  4600. else
  4601. {
  4602. DPFX( DPFPREP, DVF_WARNINGLEVEL, "WARNING: Mapping 0x%x --> DVERR_SOUNDINITFAILURE", hrConnectResult );
  4603. m_hrConnectResult = DVERR_SOUNDINITFAILURE;
  4604. }
  4605. }
  4606. else
  4607. {
  4608. m_hrConnectResult = hrConnectResult;
  4609. }
  4610. }
  4611. #undef DPF_MODNAME
  4612. #define DPF_MODNAME "CDirectVoiceClientEngine::GetConnectResult"
  4613. // GetConnectResult
  4614. //
  4615. // This function stores the specified connect result code in m_hrOriginalConnectResult
  4616. // and stores the "translated" error code in m_hrConnectResult. m_hrConnectResult
  4617. // is used to return the result to the user.
  4618. //
  4619. // For some error codes the voice layer translates the code to a voice specific error,
  4620. // this is why this function is required.
  4621. //
  4622. HRESULT CDirectVoiceClientEngine::GetConnectResult( )
  4623. {
  4624. return m_hrConnectResult;
  4625. }
  4626. #undef DPF_MODNAME
  4627. #define DPF_MODNAME "CDirectVoiceClientEngine::SendConnectResult"
  4628. HRESULT CDirectVoiceClientEngine::SendConnectResult()
  4629. {
  4630. if( m_fConnectAsync )
  4631. {
  4632. DVMSG_CONNECTRESULT dvConnect;
  4633. dvConnect.hrResult = GetConnectResult();
  4634. dvConnect.dwSize = sizeof( DVMSG_CONNECTRESULT );
  4635. return NotifyQueue_Add( DVMSGID_CONNECTRESULT, &dvConnect, sizeof( DVMSG_CONNECTRESULT ) );
  4636. }
  4637. else
  4638. {
  4639. return DV_OK;
  4640. }
  4641. }
  4642. #undef DPF_MODNAME
  4643. #define DPF_MODNAME "CDirectVoiceClientEngine::SendDisconnectResult"
  4644. HRESULT CDirectVoiceClientEngine::SendDisconnectResult()
  4645. {
  4646. if( m_fDisconnectAsync )
  4647. {
  4648. DVMSG_DISCONNECTRESULT dvDisconnect;
  4649. dvDisconnect.hrResult = m_hrDisconnectResult;
  4650. dvDisconnect.dwSize = sizeof( DVMSG_DISCONNECTRESULT );
  4651. return NotifyQueue_Add( DVMSGID_DISCONNECTRESULT, &dvDisconnect, sizeof( DVMSG_DISCONNECTRESULT ) );
  4652. }
  4653. else
  4654. {
  4655. return DV_OK;
  4656. }
  4657. }
  4658. // NotifyThread
  4659. //
  4660. // All-purpose watch/notification thread.
  4661. //
  4662. // Wakes up to:
  4663. // - Check for multicast player timeout
  4664. // - Adjust parameters on notification
  4665. // - Sends notification messages to users.
  4666. // - Checks for timeouts on connect and disconnect calls
  4667. // - Sends level notifications
  4668. //
  4669. #undef DPF_MODNAME
  4670. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyThread"
  4671. void CDirectVoiceClientEngine::NotifyThread( void *lpParam )
  4672. {
  4673. CDirectVoiceClientEngine *This = (CDirectVoiceClientEngine *) lpParam;
  4674. HANDLE eventArray[5];
  4675. DWORD dwWaitPeriod;
  4676. LONG lWaitResult;
  4677. DWORD dwPowerLevel;
  4678. DWORD dwLastLevelNotify,
  4679. dwLastTimeoutCheck,
  4680. dwCurTime;
  4681. DVMSG_INPUTLEVEL dvInputLevel;
  4682. DVMSG_OUTPUTLEVEL dvOutputLevel;
  4683. dvInputLevel.dwSize = sizeof( DVMSG_INPUTLEVEL );
  4684. dvOutputLevel.dwSize = sizeof( DVMSG_OUTPUTLEVEL );
  4685. CNotifyElement *neElement;
  4686. HRESULT hr;
  4687. DVID dvidMessageTarget;
  4688. hr = COM_CoInitialize(NULL);
  4689. if( FAILED( hr ) )
  4690. {
  4691. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error initializing COM" );
  4692. SetEvent( This->m_hNotifyDone );
  4693. This->HandleThreadError( DVERR_GENERIC );
  4694. return;
  4695. }
  4696. eventArray[0] = This->m_hNotifyTerminate;
  4697. eventArray[1] = This->m_hNotifyChange;
  4698. eventArray[2] = This->m_hNotifyDisconnect;
  4699. eventArray[3] = This->m_hNewNotifyElement;
  4700. eventArray[4] = This->m_hNotifyConnect;
  4701. // Setup last times we checked to right now.
  4702. dwLastLevelNotify = GetTickCount();
  4703. dwLastTimeoutCheck = dwLastLevelNotify;
  4704. dwWaitPeriod = DV_CLIENT_NOTIFYWAKEUP_TIMEOUT;
  4705. while( 1 )
  4706. {
  4707. lWaitResult = WaitForMultipleObjects( 5, eventArray, FALSE, dwWaitPeriod );
  4708. DPFX(DPFPREP, DVF_INFOLEVEL, "Wakeing up!" );
  4709. // If we were woken up for a reason, handle the reason first
  4710. if( lWaitResult != WAIT_TIMEOUT )
  4711. {
  4712. lWaitResult -= WAIT_OBJECT_0;
  4713. if( lWaitResult == 0 )
  4714. {
  4715. DPFX(DPFPREP, DVF_INFOLEVEL, "Shutting down" );
  4716. break;
  4717. }
  4718. else if( lWaitResult == 1 )
  4719. {
  4720. DPFX(DPFPREP, DVF_INFOLEVEL, "Parameter Change" );
  4721. }
  4722. else if( lWaitResult == 2 )
  4723. {
  4724. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Processing disconnect request" );
  4725. This->DoDisconnect();
  4726. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "Finished processing disconnect" );
  4727. continue;
  4728. }
  4729. else if( lWaitResult == 3 )
  4730. {
  4731. hr = This->NotifyQueue_IndicateNext();
  4732. if( FAILED( hr ) )
  4733. {
  4734. DPFX(DPFPREP, DVF_ERRORLEVEL, "NotifyQueue_Get Failed hr=0x%x", hr );
  4735. }
  4736. }
  4737. else if( lWaitResult == 4 )
  4738. {
  4739. if( FAILED( This->GetConnectResult() ) )
  4740. {
  4741. This->Cleanup();
  4742. This->SendConnectResult();
  4743. SetEvent( This->m_hConnectAck );
  4744. }
  4745. else
  4746. {
  4747. This->DoConnectResponse();
  4748. }
  4749. continue;
  4750. }
  4751. }
  4752. if( This->m_dwCurrentState == DVCSTATE_IDLE )
  4753. {
  4754. continue;
  4755. }
  4756. // If we're connecting, check for timeout on the connection
  4757. // request.
  4758. if( This->m_dwCurrentState == DVCSTATE_CONNECTING && This->m_dwSynchBegin != 0 )
  4759. {
  4760. dwCurTime = GetTickCount();
  4761. if( ( dwCurTime - This->m_dwSynchBegin ) > DV_CLIENT_CONNECT_TIMEOUT )
  4762. {
  4763. DPFX(DPFPREP, DVF_ERRORLEVEL, "Connection Timed-Out. Returning NOVOICESESSION" );
  4764. This->SetConnectResult( DVERR_NOVOICESESSION );
  4765. This->Cleanup();
  4766. This->SendConnectResult();
  4767. SetEvent( This->m_hConnectAck );
  4768. continue;
  4769. }
  4770. else if( ( dwCurTime - This->m_dwLastConnectSent ) > DV_CLIENT_CONNECT_RETRY_TIMEOUT )
  4771. {
  4772. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Connect Request Timed-Out" );
  4773. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Re-sending connection request" );
  4774. hr = This->Send_ConnectRequest();
  4775. if( FAILED( hr ) )
  4776. {
  4777. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error sending connection request. Send error hr=0x%x", hr );
  4778. This->SetConnectResult( DVERR_SENDERROR );
  4779. This->Cleanup();
  4780. This->SendConnectResult();
  4781. SetEvent( This->m_hConnectAck );
  4782. continue;
  4783. }
  4784. This->m_dwLastConnectSent = dwCurTime;
  4785. }
  4786. }
  4787. // If we're disconnecting, check for timeout on the disconnect
  4788. else if( This->m_dwCurrentState == DVCSTATE_DISCONNECTING )
  4789. {
  4790. dwCurTime = GetTickCount();
  4791. DPFX(DPFPREP, DVF_INFOLEVEL, "Checking timeout on disconnect. Waited %d so far", dwCurTime - This->m_dwSynchBegin );
  4792. if( ( dwCurTime - This->m_dwSynchBegin ) > DV_CLIENT_DISCONNECT_TIMEOUT )
  4793. {
  4794. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Disconnect Request Timed-Out" );
  4795. This->DoSignalDisconnect( DVERR_TIMEOUT );
  4796. continue;
  4797. }
  4798. }
  4799. // Take care of the periodic checks
  4800. if (This->m_dwCurrentState == DVCSTATE_CONNECTED)
  4801. {
  4802. dwCurTime = GetTickCount();
  4803. // Update pending / other lists
  4804. This->UpdateActiveNotifyPendingList( );
  4805. // If we're running a multicast session.. check for timed-out users
  4806. if( This->m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_FORWARDING )
  4807. {
  4808. if( ( dwCurTime - dwLastTimeoutCheck ) > DV_MULTICAST_USERTIMEOUT_PERIOD )
  4809. {
  4810. This->CheckForUserTimeout(dwCurTime);
  4811. dwLastTimeoutCheck = dwCurTime;
  4812. }
  4813. }
  4814. // Check to see if it's time to notify about levels
  4815. if( This->m_dvClientConfig.dwNotifyPeriod != 0 && This->m_fLocalPlayerAvailable )
  4816. {
  4817. if( ( dwCurTime - dwLastLevelNotify ) > This->m_dvClientConfig.dwNotifyPeriod )
  4818. {
  4819. dvInputLevel.pvLocalPlayerContext = This->m_pvLocalPlayerContext;
  4820. dvOutputLevel.pvLocalPlayerContext = This->m_pvLocalPlayerContext;
  4821. if( !(This->m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX) )
  4822. {
  4823. dvInputLevel.dwPeakLevel = This->m_bLastPeak;
  4824. if( This->m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_NORECVOLAVAILABLE )
  4825. {
  4826. dvInputLevel.lRecordVolume = DSBVOLUME_MAX;
  4827. }
  4828. else
  4829. {
  4830. dvInputLevel.lRecordVolume = This->m_dvClientConfig.lRecordVolume;
  4831. }
  4832. This->NotifyQueue_Add( DVMSGID_INPUTLEVEL, &dvInputLevel, sizeof( DVMSG_INPUTLEVEL ) );
  4833. }
  4834. dvOutputLevel.dwPeakLevel = This->m_bLastPlaybackPeak;
  4835. dvOutputLevel.lOutputVolume = This->m_dvClientConfig.lPlaybackVolume;
  4836. This->NotifyQueue_Add( DVMSGID_OUTPUTLEVEL, &dvOutputLevel, sizeof( DVMSG_OUTPUTLEVEL ) );
  4837. This->SendPlayerLevels();
  4838. dwLastLevelNotify = dwCurTime;
  4839. }
  4840. }
  4841. }
  4842. }
  4843. // Flush out any remaining notifications
  4844. This->NotifyQueue_Flush();
  4845. SetEvent( This->m_hNotifyDone );
  4846. COM_CoUninitialize();
  4847. _endthread();
  4848. }
  4849. #undef DPF_MODNAME
  4850. #define DPF_MODNAME "CDirectVoiceClientEngine::SendPlayerLevels"
  4851. //
  4852. // SendPlayerLevels
  4853. //
  4854. // Send player level notifications, but only if there is a message handler
  4855. // and the player level message is active.
  4856. //
  4857. void CDirectVoiceClientEngine::SendPlayerLevels()
  4858. {
  4859. if( CheckShouldSendMessage( DVMSGID_PLAYEROUTPUTLEVEL ) )
  4860. {
  4861. DPFX(DPFPREP, DVF_INFOLEVEL, "SendPlayerLevels: Got Lock" );
  4862. CVoicePlayer *pCurrentPlayer;
  4863. BILINK *pblSearch;
  4864. DVMSG_PLAYEROUTPUTLEVEL dvPlayerLevel;
  4865. pblSearch = m_blNotifyActivePlayers.next;
  4866. while( pblSearch != &m_blNotifyActivePlayers )
  4867. {
  4868. pCurrentPlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  4869. if( pCurrentPlayer == NULL )
  4870. {
  4871. DPFX(DPFPREP, DVF_ERRORLEVEL, "Retrieved NULL player from active list" );
  4872. DNASSERT( FALSE );
  4873. return;
  4874. }
  4875. if( pCurrentPlayer->IsReceiving() )
  4876. {
  4877. dvPlayerLevel.dwSize = sizeof( DVMSG_PLAYEROUTPUTLEVEL );
  4878. dvPlayerLevel.dvidSourcePlayerID = pCurrentPlayer->GetPlayerID();
  4879. dvPlayerLevel.dwPeakLevel = pCurrentPlayer->GetLastPeak();
  4880. dvPlayerLevel.pvPlayerContext = pCurrentPlayer->GetContext();
  4881. NotifyQueue_Add( DVMSGID_PLAYEROUTPUTLEVEL, &dvPlayerLevel, sizeof( DVMSG_PLAYEROUTPUTLEVEL ) );
  4882. }
  4883. pblSearch = pblSearch->next;
  4884. }
  4885. }
  4886. DPFX(DPFPREP, DVF_INFOLEVEL, "SendPlayerLevels: Done Enum" );
  4887. return;
  4888. }
  4889. #undef DPF_MODNAME
  4890. #define DPF_MODNAME "CDirectVoiceClientEngine::CheckForUserTimeout"
  4891. //
  4892. // CheckForUserTimeout
  4893. //
  4894. // Run the list of users and check for user timeouts in multicast sessions
  4895. //
  4896. void CDirectVoiceClientEngine::CheckForUserTimeout( DWORD dwCurTime )
  4897. {
  4898. DPFX(DPFPREP, DVF_INFOLEVEL, "Got Lock" );
  4899. BILINK *pblSearch;
  4900. CVoicePlayer *pCurrentPlayer;
  4901. DVPROTOCOLMSG_PLAYERQUIT msgPlayerQuit;
  4902. pblSearch = m_blNotifyActivePlayers.next;
  4903. msgPlayerQuit.dwType = DVMSGID_DELETEVOICEPLAYER;
  4904. while( pblSearch != &m_blNotifyActivePlayers )
  4905. {
  4906. pCurrentPlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  4907. if( dwCurTime - pCurrentPlayer->GetLastPlayback() > DV_MULTICAST_USERTIMEOUT_PERIOD )
  4908. {
  4909. msgPlayerQuit.dvidID = pCurrentPlayer->GetPlayerID();
  4910. HandleDeleteVoicePlayer( pCurrentPlayer->GetPlayerID(), &msgPlayerQuit, sizeof( DVPROTOCOLMSG_PLAYERQUIT ) );
  4911. }
  4912. pblSearch = pblSearch->next;
  4913. }
  4914. DPFX(DPFPREP, DVF_INFOLEVEL, "Done Enum" );
  4915. return;
  4916. }
  4917. // Cleanup any outstanding entries on the playback lists
  4918. void CDirectVoiceClientEngine::CleanupPlaybackLists( )
  4919. {
  4920. BILINK *pblSearch;
  4921. CVoicePlayer *pVoicePlayer;
  4922. DNEnterCriticalSection( &m_csPlayAddList );
  4923. m_dwPlayActiveCount = 0;
  4924. pblSearch = m_blPlayActivePlayers.next;
  4925. while( pblSearch != &m_blPlayActivePlayers )
  4926. {
  4927. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blPlayList );
  4928. pVoicePlayer->RemoveFromPlayList();
  4929. pVoicePlayer->Release();
  4930. pblSearch = m_blPlayActivePlayers.next;
  4931. }
  4932. pblSearch = m_blPlayAddPlayers.next;
  4933. while( pblSearch != &m_blPlayAddPlayers )
  4934. {
  4935. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blPlayList );
  4936. pVoicePlayer->RemoveFromPlayList();
  4937. pVoicePlayer->Release();
  4938. pblSearch = m_blPlayAddPlayers.next;
  4939. }
  4940. DNLeaveCriticalSection( &m_csPlayAddList );
  4941. }
  4942. // Cleanup any outstanding entries on the playback lists
  4943. void CDirectVoiceClientEngine::CleanupNotifyLists( )
  4944. {
  4945. BILINK *pblSearch;
  4946. CVoicePlayer *pVoicePlayer;
  4947. DNEnterCriticalSection( &m_csNotifyAddList );
  4948. pblSearch = m_blNotifyActivePlayers.next;
  4949. while( pblSearch != &m_blNotifyActivePlayers )
  4950. {
  4951. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  4952. pVoicePlayer->RemoveFromNotifyList();
  4953. pVoicePlayer->Release();
  4954. pblSearch = m_blNotifyActivePlayers.next;
  4955. }
  4956. pblSearch = m_blNotifyAddPlayers.next;
  4957. while( pblSearch != &m_blNotifyAddPlayers )
  4958. {
  4959. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  4960. pVoicePlayer->RemoveFromNotifyList();
  4961. pVoicePlayer->Release();
  4962. pblSearch = m_blNotifyAddPlayers.next;
  4963. }
  4964. DNLeaveCriticalSection( &m_csNotifyAddList );
  4965. }
  4966. // UpdateActivePendingList
  4967. //
  4968. // This function looks at the pending list and moves those elements on the pending list to the active list
  4969. //
  4970. // This function also looks at the active list and removes those players who are disconnected
  4971. //
  4972. // There are three four lists in the system:
  4973. // - Playback Thread
  4974. // - Notify Thread
  4975. // - Host Migration List
  4976. //
  4977. void CDirectVoiceClientEngine::UpdateActivePlayPendingList( )
  4978. {
  4979. BILINK *pblSearch;
  4980. CVoicePlayer *pVoicePlayer;
  4981. DNEnterCriticalSection( &m_csPlayAddList );
  4982. // Add players who are pending
  4983. pblSearch = m_blPlayAddPlayers.next;
  4984. while( pblSearch != &m_blPlayAddPlayers )
  4985. {
  4986. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blPlayList );
  4987. pVoicePlayer->RemoveFromPlayList();
  4988. pVoicePlayer->AddToPlayList( &m_blPlayActivePlayers );
  4989. m_dwPlayActiveCount++;
  4990. pblSearch = m_blPlayAddPlayers.next;
  4991. }
  4992. DNLeaveCriticalSection( &m_csPlayAddList );
  4993. // Remove players who have disconnected
  4994. pblSearch = m_blPlayActivePlayers.next;
  4995. while( pblSearch != &m_blPlayActivePlayers )
  4996. {
  4997. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blPlayList );
  4998. pblSearch = pblSearch->next;
  4999. // If current player has disconnected, remove them from active list
  5000. // and release the reference the list has
  5001. if( pVoicePlayer->IsDisconnected() )
  5002. {
  5003. m_dwPlayActiveCount--;
  5004. pVoicePlayer->RemoveFromPlayList();
  5005. pVoicePlayer->Release();
  5006. }
  5007. }
  5008. }
  5009. void CDirectVoiceClientEngine::UpdateActiveNotifyPendingList( )
  5010. {
  5011. BILINK *pblSearch;
  5012. CVoicePlayer *pVoicePlayer;
  5013. DNEnterCriticalSection( &m_csNotifyAddList );
  5014. // Add players who are pending
  5015. pblSearch = m_blNotifyAddPlayers.next;
  5016. while( pblSearch != &m_blNotifyAddPlayers )
  5017. {
  5018. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  5019. pVoicePlayer->RemoveFromNotifyList();
  5020. pVoicePlayer->AddToNotifyList( &m_blNotifyActivePlayers );
  5021. pblSearch = m_blNotifyAddPlayers.next;
  5022. }
  5023. DNLeaveCriticalSection( &m_csNotifyAddList );
  5024. // Remove players who have disconnected
  5025. pblSearch = m_blNotifyActivePlayers.next;
  5026. while( pblSearch != &m_blNotifyActivePlayers )
  5027. {
  5028. pVoicePlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blNotifyList );
  5029. pblSearch = pblSearch->next;
  5030. // If current player has disconnected, remove them from active list
  5031. // and release the reference the list has
  5032. if( pVoicePlayer->IsDisconnected() )
  5033. {
  5034. pVoicePlayer->RemoveFromNotifyList();
  5035. pVoicePlayer->Release();
  5036. }
  5037. }
  5038. }
  5039. HRESULT CDirectVoiceClientEngine::CreateGeneralBuffer( )
  5040. {
  5041. HRESULT hr;
  5042. m_lpstGeneralBuffer = new CSoundTarget( DVID_REMAINING,
  5043. m_audioPlaybackDevice,
  5044. (CAudioPlaybackBuffer *) m_audioPlaybackBuffer,
  5045. &m_dsBufferDesc,
  5046. m_dvSoundDeviceConfig.dwMainBufferPriority,
  5047. m_dvSoundDeviceConfig.dwMainBufferFlags,
  5048. m_dwUnCompressedFrameSize );
  5049. if( m_lpstGeneralBuffer == NULL )
  5050. {
  5051. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to allocate General Buffer" );
  5052. return DVERR_OUTOFMEMORY;
  5053. }
  5054. hr = m_lpstGeneralBuffer->GetInitResult();
  5055. if( FAILED( hr ) )
  5056. {
  5057. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to initalize general buffer hr=0x%x", hr );
  5058. return hr;
  5059. }
  5060. if( m_lpstGeneralBuffer->Get3DBuffer() != NULL )
  5061. {
  5062. // Turn off 3D by default on the general buffer
  5063. m_lpstGeneralBuffer->Get3DBuffer()->SetMode( DS3DMODE_DISABLE, DS3D_IMMEDIATE );
  5064. }
  5065. // Set General Buffer Volume
  5066. if( m_lpstGeneralBuffer->GetBuffer() != NULL )
  5067. {
  5068. m_lpstGeneralBuffer->GetBuffer()->SetVolume( m_dvClientConfig.lPlaybackVolume );
  5069. }
  5070. hr = m_lpstGeneralBuffer->StartMix();
  5071. if( FAILED( hr ) )
  5072. {
  5073. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to start the mix hr=0x%x.", hr );
  5074. return hr;
  5075. }
  5076. return hr;
  5077. }
  5078. // New Playback Thread
  5079. #undef DPF_MODNAME
  5080. #define DPF_MODNAME "CDirectVoiceClientEngine::PlaybackThread"
  5081. void CDirectVoiceClientEngine::PlaybackThread( void *lParam )
  5082. {
  5083. DWORD dwCurPlayer;
  5084. BOOL bContinueEnum;
  5085. DVPROTOCOLMSG_PLAYERQUIT dvPlayerQuit;
  5086. DWORD dwResultSize;
  5087. LPBYTE lpSourceBuffer = NULL;
  5088. HANDLE hEventArray[2] = { NULL, NULL };
  5089. LONG lWaitResult;
  5090. Timer *lpTimer = NULL;
  5091. BOOL fMixed;
  5092. CSoundTarget *lpstCurrent = NULL;
  5093. BYTE bHighPeak;
  5094. HRESULT hr;
  5095. DWORD dwEchoState;
  5096. DWORD dwTmp;
  5097. BYTE bTmpPeak1, bTmpPeak2;
  5098. DVMSG_PLAYERVOICESTART dvMsgPlayerVoiceStart;
  5099. DVMSG_PLAYERVOICESTOP dvMsgPlayerVoiceStop;
  5100. DWORD dwCompressStart;
  5101. DWORD dwCompressTime;
  5102. CVoicePlayer *pCurrentPlayer;
  5103. BILINK *pblSearch;
  5104. BOOL fSilence, fLost;
  5105. DWORD dwCurrentTime;
  5106. DWORD dwSeqNum, dwMsgNum;
  5107. CFrame *frTmpFrame;
  5108. DWORD dwCurrentLead = 0;
  5109. DWORD dwAllowedLeadBuffers = 0;
  5110. DWORD dwAllowedLeadBytes = 0;
  5111. DWORD dwHalfBufferSize = 0;
  5112. BOOL fDesktopCurrent = TRUE;
  5113. dvMsgPlayerVoiceStart.dwSize = sizeof( DVMSG_PLAYERVOICESTART );
  5114. dvMsgPlayerVoiceStop.dwSize = sizeof( DVMSG_PLAYERVOICESTOP );
  5115. CDirectVoiceClientEngine *This = (CDirectVoiceClientEngine *) lParam;
  5116. // Pre-calc some sizes..
  5117. dwAllowedLeadBuffers = DV_CLIENT_BASE_LEAD_MAX;
  5118. dwAllowedLeadBytes = This->m_dwUnCompressedFrameSize * DV_CLIENT_BASE_LEAD_MAX;
  5119. dwHalfBufferSize = This->m_dwUnCompressedFrameSize * (This->m_dwNumPerBuffer/2);
  5120. hr = COM_CoInitialize(NULL);
  5121. if( FAILED( hr ) )
  5122. {
  5123. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error initializing COM on playback thread" );
  5124. This->HandleThreadError( DVERR_GENERIC );
  5125. goto EXIT_PLAYBACK;
  5126. }
  5127. lpSourceBuffer = new BYTE[This->m_dwUnCompressedFrameSize];
  5128. if( lpSourceBuffer == NULL )
  5129. {
  5130. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to allocate source buffer" );
  5131. This->HandleThreadError( DVERR_OUTOFMEMORY );
  5132. goto EXIT_PLAYBACK;
  5133. }
  5134. hEventArray[0] = This->m_hPlaybackTerminate;
  5135. hEventArray[1] = This->m_thTimerInfo.hPlaybackTimerEvent;
  5136. if( hEventArray[0] == NULL || hEventArray[1] == NULL )
  5137. {
  5138. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to initialize events" );
  5139. This->HandleThreadError( DVERR_GENERIC );
  5140. goto EXIT_PLAYBACK;
  5141. }
  5142. if( This->m_audioPlaybackDevice->IsEmulated() )
  5143. {
  5144. DPFX(DPFPREP, DVF_WARNINGLEVEL, "WARNING: DirectSound is in emulated mode" );
  5145. dwAllowedLeadBuffers += DV_CLIENT_EMULATED_LEAD_ADJUST;
  5146. dwAllowedLeadBytes += DV_CLIENT_EMULATED_LEAD_ADJUST * This->m_dwUnCompressedFrameSize;
  5147. }
  5148. else
  5149. {
  5150. DPFX(DPFPREP, DVF_WARNINGLEVEL, "NOTE: DirectSound is in standard mode" );
  5151. }
  5152. while( 1 )
  5153. {
  5154. lWaitResult = WaitForMultipleObjects( 2, hEventArray, FALSE, INFINITE );
  5155. if( lWaitResult == WAIT_OBJECT_0 )
  5156. {
  5157. break;
  5158. }
  5159. hr = This->m_lpstGeneralBuffer->GetCurrentLead( &dwCurrentLead );
  5160. if( FAILED( hr ) )
  5161. {
  5162. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to get current position of main buff hr=0x%x", hr );
  5163. This->HandleThreadError( hr );
  5164. goto EXIT_PLAYBACK;
  5165. }
  5166. while( 1 )
  5167. {
  5168. if( dwCurrentLead < (dwAllowedLeadBytes) )
  5169. {
  5170. DPFX(DPFPREP, DVF_INFOLEVEL, "PWI, Lead: %d Running a pass..", dwCurrentLead );
  5171. }
  5172. else if( dwCurrentLead > dwHalfBufferSize )
  5173. {
  5174. DPFX(DPFPREP, DVF_INFOLEVEL, "PWI, Lead: %d Running a pass (wraparound)..", dwCurrentLead );
  5175. }
  5176. else
  5177. {
  5178. DPFX(DPFPREP, DVF_INFOLEVEL, "PWI, Lead: %d NOT Running a pass..", dwCurrentLead );
  5179. break;
  5180. }
  5181. DNEnterCriticalSection( &This->m_thTimerInfo.csPlayCount );
  5182. // Only run a max of two times per iteration
  5183. if( This->m_thTimerInfo.lPlaybackCount > dwAllowedLeadBuffers )
  5184. This->m_thTimerInfo.lPlaybackCount = dwAllowedLeadBuffers;
  5185. else
  5186. This->m_thTimerInfo.lPlaybackCount--;
  5187. DNLeaveCriticalSection( &This->m_thTimerInfo.csPlayCount );
  5188. #ifdef WINNT
  5189. fDesktopCurrent = ( USER_SHARED_DATA->ActiveConsoleId == NtCurrentPeb()->SessionId);
  5190. #endif
  5191. bHighPeak = 0;
  5192. // Update list
  5193. This->UpdateActivePlayPendingList( );
  5194. if( This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_ECHOSUPPRESSION )
  5195. {
  5196. DNEnterCriticalSection( &This->m_lockPlaybackMode );
  5197. dwEchoState = This->m_dwEchoState;
  5198. DNLeaveCriticalSection( &This->m_lockPlaybackMode );
  5199. }
  5200. else
  5201. {
  5202. dwEchoState = DVCECHOSTATE_IDLE;
  5203. }
  5204. if( dwEchoState != DVCECHOSTATE_RECORDING )
  5205. {
  5206. dwCurrentTime = GetTickCount();
  5207. pblSearch = This->m_blPlayActivePlayers.next;
  5208. while( pblSearch != &This->m_blPlayActivePlayers )
  5209. {
  5210. pCurrentPlayer = CONTAINING_RECORD( pblSearch, CVoicePlayer, m_blPlayList );
  5211. pblSearch = pblSearch->next;
  5212. dwResultSize = This->m_dwUnCompressedFrameSize;
  5213. dwCompressStart = GetTickCount();
  5214. if( !pCurrentPlayer->IsInBoundConverterInitialized() )
  5215. {
  5216. hr = pCurrentPlayer->CreateInBoundConverter( This->m_lpdvfCompressionInfo->guidType, s_lpwfxPlaybackFormat );
  5217. if( FAILED( hr ) )
  5218. {
  5219. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to get create converter for player hr=0x%x", hr );
  5220. This->HandleThreadError( DVERR_GENERIC );
  5221. goto EXIT_PLAYBACK;
  5222. }
  5223. }
  5224. if( This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_PLAYBACKMUTE ||
  5225. !fDesktopCurrent )
  5226. {
  5227. frTmpFrame = pCurrentPlayer->Dequeue(&fLost, &fSilence);
  5228. frTmpFrame->Return();
  5229. continue;
  5230. }
  5231. hr = pCurrentPlayer->GetNextFrameAndDecompress( lpSourceBuffer, &dwResultSize, &fLost, &fSilence, &dwSeqNum, &dwMsgNum );
  5232. if( FAILED( hr ) )
  5233. {
  5234. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to get frame from player hr=0x%x", hr );
  5235. This->HandleThreadError( DVERR_GENERIC );
  5236. goto EXIT_PLAYBACK;
  5237. }
  5238. dwCompressTime = GetTickCount() - dwCompressStart;
  5239. // STATSBLOCK: Begin
  5240. This->m_pStatsBlob->m_dwPDTTotal += dwCompressTime;
  5241. if( dwCompressTime < This->m_pStatsBlob->m_dwPDTMin )
  5242. {
  5243. This->m_pStatsBlob->m_dwPDTMin = dwCompressTime;
  5244. }
  5245. if( dwCompressTime > This->m_pStatsBlob->m_dwPDTMax )
  5246. {
  5247. This->m_pStatsBlob->m_dwPDTMax = dwCompressTime;
  5248. }
  5249. // STATSBLOCK: End
  5250. // STATSBLOCK: Begin
  5251. if( fLost )
  5252. {
  5253. DPFX(DPFPREP, DVF_CLIENT_SEQNUM_DEBUG_LEVEL, "SEQ: Dequeue: Lost Frame" );
  5254. DPFX(DPFPREP, DVF_GLITCH_DEBUG_LEVEL, "GLITCH: Dequeue: Packet was lost. Speech gap will occur." );
  5255. This->m_pStatsBlob->m_dwPPDQLost++;
  5256. }
  5257. else if( fSilence )
  5258. {
  5259. DPFX(DPFPREP, DVF_CLIENT_SEQNUM_DEBUG_LEVEL, "SEQ: Dequeue: Silent Frame" );
  5260. This->m_pStatsBlob->m_dwPPDQSilent++;
  5261. }
  5262. else
  5263. {
  5264. DPFX(DPFPREP, DVF_CLIENT_SEQNUM_DEBUG_LEVEL, "SEQ: Dequeue: Msg [%d] Seq [%d]", dwMsgNum, dwSeqNum );
  5265. This->m_pStatsBlob->m_dwPPDQSpeech++;
  5266. }
  5267. // STATSBLOCK: End
  5268. // If the player sent us silence, increment the silent count
  5269. if( fSilence )
  5270. {
  5271. // If we're receiving on this user
  5272. if( pCurrentPlayer->IsReceiving() )
  5273. {
  5274. // If it exceeds the max.
  5275. if( (dwCurrentTime - pCurrentPlayer->GetLastPlayback()) > PLAYBACK_RECEIVESTOP_TIMEOUT )
  5276. {
  5277. pCurrentPlayer->SetReceiving(FALSE);
  5278. dvMsgPlayerVoiceStop.dwSize = sizeof( dvMsgPlayerVoiceStop );
  5279. dvMsgPlayerVoiceStop.dvidSourcePlayerID = pCurrentPlayer->GetPlayerID();
  5280. dvMsgPlayerVoiceStop.pvPlayerContext = pCurrentPlayer->GetContext();
  5281. This->NotifyQueue_Add( DVMSGID_PLAYERVOICESTOP, &dvMsgPlayerVoiceStop, sizeof( dvMsgPlayerVoiceStop ) );
  5282. This->m_dwActiveCount--;
  5283. if( This->m_dwActiveCount == 0 )
  5284. {
  5285. if( This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_ECHOSUPPRESSION )
  5286. {
  5287. DNEnterCriticalSection( &This->m_lockPlaybackMode );
  5288. if( This->m_dwEchoState == DVCECHOSTATE_PLAYBACK )
  5289. {
  5290. DPFX(DPFPREP, PLAYBACK_SWITCH_DEBUG_LEVEL, "%%%% Switching to idle mode" );
  5291. This->m_dwEchoState = DVCECHOSTATE_IDLE;
  5292. }
  5293. DNLeaveCriticalSection( &This->m_lockPlaybackMode );
  5294. }
  5295. }
  5296. }
  5297. }
  5298. }
  5299. else
  5300. {
  5301. // We receive data and this is the first one
  5302. if( !pCurrentPlayer->IsReceiving() )
  5303. {
  5304. dvMsgPlayerVoiceStart.dvidSourcePlayerID = pCurrentPlayer->GetPlayerID();
  5305. dvMsgPlayerVoiceStart.pvPlayerContext = pCurrentPlayer->GetContext();
  5306. This->NotifyQueue_Add( DVMSGID_PLAYERVOICESTART, &dvMsgPlayerVoiceStart, sizeof(DVMSG_PLAYERVOICESTART) );
  5307. This->m_dwActiveCount++;
  5308. if( This->m_dwActiveCount == 1 )
  5309. {
  5310. if( This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_ECHOSUPPRESSION )
  5311. {
  5312. DNEnterCriticalSection( &This->m_lockPlaybackMode );
  5313. if( This->m_dwEchoState == DVCECHOSTATE_IDLE )
  5314. {
  5315. DPFX(DPFPREP, PLAYBACK_SWITCH_DEBUG_LEVEL, "%%%% Switching to playback mode" );
  5316. This->m_dwEchoState = DVCECHOSTATE_PLAYBACK;
  5317. }
  5318. DNLeaveCriticalSection( &This->m_lockPlaybackMode );
  5319. }
  5320. }
  5321. pCurrentPlayer->SetReceiving(TRUE);
  5322. }
  5323. }
  5324. fMixed = FALSE;
  5325. // If the frame was not silence, decompress it and then
  5326. // mix it into the mixer buffer
  5327. if( !fSilence &&
  5328. fDesktopCurrent &&
  5329. !(This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_PLAYBACKMUTE)
  5330. )
  5331. {
  5332. DPFX(DPFPREP, DVF_INFOLEVEL, "Player: 0x%x getting frame.. it's speech", pCurrentPlayer->GetPlayerID() );
  5333. // Lock the buffer list
  5334. DNEnterCriticalSection( &This->m_csBufferLock );
  5335. lpstCurrent = This->m_lpstBufferList;
  5336. if( pCurrentPlayer->GetLastPeak() > bHighPeak )
  5337. {
  5338. bHighPeak = pCurrentPlayer->GetLastPeak();
  5339. }
  5340. // Loop through list looking for buffers to mix into
  5341. while( lpstCurrent != NULL )
  5342. {
  5343. if( lpstCurrent->GetTarget() == pCurrentPlayer->GetPlayerID() )
  5344. {
  5345. lpstCurrent->MixInSingle( lpSourceBuffer );
  5346. fMixed = TRUE;
  5347. }
  5348. else if( This->m_lpSessionTransport->IsPlayerInGroup( lpstCurrent->GetTarget(), pCurrentPlayer->GetPlayerID() ) )
  5349. {
  5350. lpstCurrent->MixIn( lpSourceBuffer );
  5351. fMixed = TRUE;
  5352. }
  5353. lpstCurrent = lpstCurrent->m_lpstNext;
  5354. }
  5355. DNLeaveCriticalSection( &This->m_csBufferLock );
  5356. if( !(This->m_dvClientConfig.dwFlags & DVCLIENTCONFIG_MUTEGLOBAL) )
  5357. {
  5358. // If we didn't mix into any user created buffers, then
  5359. // Mix into the main buffer
  5360. if( !fMixed )
  5361. {
  5362. if( This->m_dwPlayActiveCount == 1 )
  5363. {
  5364. hr = This->m_lpstGeneralBuffer->MixInSingle( lpSourceBuffer );
  5365. }
  5366. else
  5367. {
  5368. hr = This->m_lpstGeneralBuffer->MixIn( lpSourceBuffer );
  5369. }
  5370. if( FAILED( hr ) )
  5371. {
  5372. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to do mix." );
  5373. DNLeaveCriticalSection( &This->m_csBufferLock );
  5374. This->HandleThreadError( hr );
  5375. goto EXIT_PLAYBACK;
  5376. }
  5377. }
  5378. }
  5379. }
  5380. else
  5381. {
  5382. DPFX(DPFPREP, DVF_INFOLEVEL, "Player: 0x%x getting frame.. it's silence", pCurrentPlayer->GetPlayerID() );
  5383. }
  5384. } // for each player
  5385. } // not in the recording state and at least one talking
  5386. else
  5387. {
  5388. bHighPeak = 0;
  5389. }
  5390. // Lock the buffer list
  5391. DNEnterCriticalSection( &This->m_csBufferLock );
  5392. lpstCurrent = This->m_lpstBufferList;
  5393. #if defined(DEBUG) || defined(DBG)
  5394. This->CHECKLISTINTEGRITY();
  5395. #endif
  5396. // Loop through list looking for buffers to mix into
  5397. while( lpstCurrent != NULL )
  5398. {
  5399. #if defined(DEBUG) || defined(DBG)
  5400. This->CHECKLISTINTEGRITY();
  5401. #endif
  5402. hr = lpstCurrent->Commit();
  5403. if( FAILED( hr ) )
  5404. {
  5405. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error commiting to buffer hr=0x%x -- Locked by user?", hr );
  5406. DNLeaveCriticalSection( &This->m_csBufferLock );
  5407. if( hr == DSERR_NODRIVER )
  5408. {
  5409. hr = DVERR_INVALIDDEVICE;
  5410. }
  5411. else
  5412. {
  5413. hr = DVERR_LOCKEDBUFFER;
  5414. }
  5415. This->HandleThreadError( hr );
  5416. goto EXIT_PLAYBACK;
  5417. }
  5418. lpstCurrent = lpstCurrent->m_lpstNext;
  5419. }
  5420. This->m_bLastPlaybackPeak = bHighPeak;
  5421. hr = This->m_lpstGeneralBuffer->Commit();
  5422. if( FAILED( hr ) )
  5423. {
  5424. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error commiting to buffer hr=0x%x -- Locked by user?", hr );
  5425. DNLeaveCriticalSection( &This->m_csBufferLock );
  5426. if( hr == DSERR_NODRIVER )
  5427. {
  5428. hr = DVERR_INVALIDDEVICE;
  5429. }
  5430. else
  5431. {
  5432. hr = DVERR_LOCKEDBUFFER;
  5433. }
  5434. This->HandleThreadError( hr );
  5435. goto EXIT_PLAYBACK;
  5436. }
  5437. DNLeaveCriticalSection( &This->m_csBufferLock );
  5438. hr = This->m_lpstGeneralBuffer->GetCurrentLead( &dwCurrentLead );
  5439. if( FAILED( hr ) )
  5440. {
  5441. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to get current pos of main buffer hr=0x%x", hr );
  5442. This->HandleThreadError( hr );
  5443. goto EXIT_PLAYBACK;
  5444. }
  5445. }
  5446. }
  5447. EXIT_PLAYBACK:
  5448. if( lpTimer != NULL )
  5449. {
  5450. delete lpTimer;
  5451. }
  5452. DPFX(DPFPREP, DVF_INFOLEVEL, "PT: Exiting" );
  5453. // Stop the playback
  5454. //This->m_audioPlaybackBuffer->Stop();
  5455. // Deallocate the buffers
  5456. DPFX(DPFPREP, DVF_INFOLEVEL, "PT: mixer gone" );
  5457. if( lpSourceBuffer != NULL )
  5458. {
  5459. delete [] lpSourceBuffer;
  5460. }
  5461. DPFX(DPFPREP, DVF_INFOLEVEL, "PT: source gone" );
  5462. // Signal that thread is done.
  5463. SetEvent( This->m_hPlaybackDone );
  5464. DPFX(DPFPREP, DVF_INFOLEVEL, "PT: Shutdown complete" );
  5465. COM_CoUninitialize();
  5466. _endthread();
  5467. }
  5468. #undef DPF_MODNAME
  5469. #define DPF_MODNAME "CDirectVoiceClientEngine::SetPlaybackVolume"
  5470. //
  5471. // SetPlaybackVolume
  5472. //
  5473. // Sets the playback volume of all the playback buffers
  5474. //
  5475. HRESULT CDirectVoiceClientEngine::SetPlaybackVolume( LONG lVolume )
  5476. {
  5477. CSoundTarget *lpstCurrent;
  5478. CAudioPlaybackBuffer *lpdsBuffer;
  5479. m_audioPlaybackBuffer->SetVolume( lVolume );
  5480. // Lock the buffer list
  5481. DNEnterCriticalSection( &m_csBufferLock );
  5482. lpstCurrent = m_lpstBufferList;
  5483. // Loop through list of buffers setting the volume
  5484. while( lpstCurrent != NULL )
  5485. {
  5486. lpdsBuffer = lpstCurrent->GetBuffer();
  5487. if( lpdsBuffer != NULL )
  5488. {
  5489. lpdsBuffer->SetVolume( lVolume );
  5490. }
  5491. lpstCurrent = lpstCurrent->m_lpstNext;
  5492. }
  5493. // Lock the buffer list
  5494. DNLeaveCriticalSection( &m_csBufferLock );
  5495. return DV_OK;
  5496. }
  5497. #undef DPF_MODNAME
  5498. #define DPF_MODNAME "CDirectVoiceClientEngine::HandleThreadError"
  5499. //
  5500. // HandleThreadError
  5501. //
  5502. // Handles errors within threads. When an error occurs this function sets the
  5503. // session lost flag and notifies the notifythread to perform a disconnect.
  5504. //
  5505. void CDirectVoiceClientEngine::HandleThreadError( HRESULT hrResult )
  5506. {
  5507. DoSessionLost( hrResult );
  5508. }
  5509. #undef DPF_MODNAME
  5510. #define DPF_MODNAME "CDirectVoiceClientEngine::RecordThread"
  5511. // RecordThread
  5512. //
  5513. // Thread to handle compression / transmission
  5514. //
  5515. void CDirectVoiceClientEngine::RecordThread( void *lpParam )
  5516. {
  5517. CDirectVoiceClientEngine *This = (CDirectVoiceClientEngine *) lpParam;
  5518. DNASSERT( This != NULL );
  5519. DPFX(DPFPREP, DVF_ENTRYLEVEL, "DVCE::RecordThread() Begin" );
  5520. HANDLE hEventArray[2];
  5521. LONG lWaitResult;
  5522. HRESULT hr;
  5523. BOOL fContinue = FALSE;
  5524. DWORD dwNumRunsPerWakeup = 0;
  5525. CClientRecordSubSystem *subSystem = NULL;
  5526. hr = COM_CoInitialize(NULL);
  5527. if( FAILED( hr ) )
  5528. {
  5529. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error innitializing COM on record thread" );
  5530. goto EXIT_ERROR;
  5531. }
  5532. subSystem = new CClientRecordSubSystem( This );
  5533. // Check that the converter is valid
  5534. if( subSystem == NULL )
  5535. {
  5536. DPFX(DPFPREP, DVF_ERRORLEVEL, "Memory alloc failure" );
  5537. goto EXIT_ERROR;
  5538. }
  5539. hr = subSystem->Initialize();
  5540. if( FAILED( hr ) )
  5541. {
  5542. DPFX(DPFPREP, DVF_ERRORLEVEL, "Record Sub Error during init hr=0x%x", hr );
  5543. goto EXIT_ERROR;
  5544. }
  5545. hEventArray[0] = This->m_hRecordTerminate;
  5546. hEventArray[1] = This->m_thTimerInfo.hRecordTimerEvent;
  5547. // Loop while we're connected
  5548. while( 1 )
  5549. {
  5550. lWaitResult = WaitForMultipleObjects( 2, hEventArray, FALSE, INFINITE );
  5551. This->m_pStatsBlob->m_recStats.m_dwNumWakeups++;
  5552. if( lWaitResult == WAIT_OBJECT_0 )
  5553. {
  5554. break;
  5555. }
  5556. fContinue = TRUE;
  5557. dwNumRunsPerWakeup = 0;
  5558. while( fContinue )
  5559. {
  5560. // Wait for next frame, if this fails recording
  5561. // system is locked. Return
  5562. hr = subSystem->GetNextFrame( &fContinue );
  5563. if( FAILED( hr ) )
  5564. {
  5565. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to retrieve next frame hr = 0x%x", hr );
  5566. goto EXIT_ERROR;
  5567. }
  5568. if( !fContinue )
  5569. {
  5570. ResetEvent( This->m_thTimerInfo.hRecordTimerEvent );
  5571. break;
  5572. }
  5573. This->m_pStatsBlob->m_recStats.m_dwRPWTotal++;
  5574. dwNumRunsPerWakeup++;
  5575. DPFX(DPFPREP, DVF_INFOLEVEL, "> RTAR: Got" );
  5576. hr = subSystem->RecordFSM();
  5577. if( FAILED( hr ) )
  5578. {
  5579. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to update FSM hr=0x%x", hr );
  5580. goto EXIT_ERROR;
  5581. }
  5582. DPFX(DPFPREP, DVF_INFOLEVEL, "> RTAR: FSM" );
  5583. // Transmit the frame if need be.
  5584. hr = subSystem->TransmitFrame();
  5585. if( FAILED( hr ) )
  5586. {
  5587. DPFX(DPFPREP, DVF_ERRORLEVEL, "TransmitFrame Failed hr=0x%x", hr );
  5588. goto EXIT_ERROR;
  5589. }
  5590. DPFX(DPFPREP, DVF_INFOLEVEL, "> RTAR: Trans" );
  5591. }
  5592. if( dwNumRunsPerWakeup < This->m_pStatsBlob->m_recStats.m_dwRPWMin )
  5593. {
  5594. This->m_pStatsBlob->m_recStats.m_dwRPWMin = dwNumRunsPerWakeup;
  5595. }
  5596. if( dwNumRunsPerWakeup > This->m_pStatsBlob->m_recStats.m_dwRPWMax )
  5597. {
  5598. This->m_pStatsBlob->m_recStats.m_dwRPWMax = dwNumRunsPerWakeup;
  5599. }
  5600. }
  5601. // Delete the recording subsystem
  5602. delete subSystem;
  5603. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Record Sub Gone" );
  5604. COM_CoUninitialize();
  5605. SetEvent( This->m_hRecordDone );
  5606. DPFX(DPFPREP, DVF_ENTRYLEVEL, "Record Gone" );
  5607. return;
  5608. EXIT_ERROR:
  5609. if( subSystem )
  5610. delete subSystem;
  5611. This->HandleThreadError( DVERR_RECORDSYSTEMERROR );
  5612. COM_CoUninitialize();
  5613. SetEvent( This->m_hRecordDone );
  5614. _endthread();
  5615. }
  5616. #undef DPF_MODNAME
  5617. #define DPF_MODNAME "CDirectVoiceClientEngine::MigrateHost"
  5618. // MigrateHost
  5619. //
  5620. // This function is called by DV_HostMigrate when the local client has received a host
  5621. // migration notification.
  5622. //
  5623. //
  5624. HRESULT CDirectVoiceClientEngine::MigrateHost( DVID dvidNewHost, LPDIRECTPLAYVOICESERVER lpdvServer )
  5625. {
  5626. return DV_OK;
  5627. }
  5628. #undef DPF_MODNAME
  5629. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Flush"
  5630. //
  5631. // NotifyQueue_Flush
  5632. //
  5633. // This function does not return until all notifications have been indicated
  5634. //
  5635. void CDirectVoiceClientEngine::NotifyQueue_Flush()
  5636. {
  5637. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5638. slLock.Lock();
  5639. while( m_lpNotifyList )
  5640. {
  5641. NotifyQueue_IndicateNext();
  5642. }
  5643. }
  5644. #undef DPF_MODNAME
  5645. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_IndicateNext"
  5646. HRESULT CDirectVoiceClientEngine::NotifyQueue_IndicateNext()
  5647. {
  5648. HRESULT hr;
  5649. CNotifyElement *neElement;
  5650. hr = NotifyQueue_Get( &neElement );
  5651. if( FAILED( hr ) )
  5652. {
  5653. DPFX(DPFPREP, DVF_ERRORLEVEL, "NotifyQueue_Get Failed hr=0x%x", hr );
  5654. }
  5655. else
  5656. {
  5657. DPFX(DPFPREP, DVF_INFOLEVEL, "Sending notification type=0x%x", neElement->m_dwType );
  5658. if( neElement->m_etElementType == NOTIFY_FIXED )
  5659. {
  5660. TransmitMessage( neElement->m_dwType,
  5661. &neElement->m_element.fixed,
  5662. neElement->m_dwDataSize );
  5663. }
  5664. else
  5665. {
  5666. TransmitMessage( neElement->m_dwType,
  5667. neElement->m_element.dynamic.m_lpData,
  5668. neElement->m_dwDataSize );
  5669. }
  5670. DPFX(DPFPREP, DVF_INFOLEVEL, "Returning notification" );
  5671. NotifyQueue_ElementFree( neElement );
  5672. }
  5673. return hr;
  5674. }
  5675. #undef DPF_MODNAME
  5676. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Add"
  5677. // Queue up a notification for the user
  5678. HRESULT CDirectVoiceClientEngine::NotifyQueue_Add( DWORD dwMessageType, LPVOID lpData, DWORD dwDataSize, PVOID pvContext, CNotifyElement::PNOTIFY_COMPLETE pNotifyFunc )
  5679. {
  5680. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5681. slLock.Lock();
  5682. if( !m_fNotifyQueueEnabled )
  5683. {
  5684. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Ignoring indication, queue disabled" );
  5685. }
  5686. CNotifyElement *lpNewElement;
  5687. HRESULT hr;
  5688. lpNewElement = (CNotifyElement *) m_pfpNotifications->Get( m_pfpNotifications );
  5689. if( lpNewElement == NULL )
  5690. {
  5691. DPFX(DPFPREP, DVF_ERRORLEVEL, "Failed to get a block for a notifier" );
  5692. return DVERR_OUTOFMEMORY;
  5693. }
  5694. // Setup the notification callbacks, if there are ones
  5695. lpNewElement->pvContext = pvContext;
  5696. lpNewElement->pNotifyFunc = pNotifyFunc;
  5697. if( dwMessageType == DVMSGID_PLAYERVOICESTOP )
  5698. {
  5699. PDVMSG_PLAYERVOICESTOP pMsgStop = (PDVMSG_PLAYERVOICESTOP) lpData;
  5700. DNASSERT( pMsgStop->dwSize == sizeof( DVMSG_PLAYERVOICESTOP ) );
  5701. DNASSERT( dwDataSize == sizeof( DVMSG_PLAYERVOICESTOP ) );
  5702. }
  5703. if( dwMessageType == DVMSGID_PLAYERVOICESTART )
  5704. {
  5705. PDVMSG_PLAYERVOICESTART pMsgStart = (PDVMSG_PLAYERVOICESTART) lpData;
  5706. DNASSERT( pMsgStart->dwSize == sizeof( DVMSG_PLAYERVOICESTART ) );
  5707. DNASSERT( dwDataSize == sizeof( DVMSG_PLAYERVOICESTART ) );
  5708. }
  5709. if( dwMessageType == DVMSGID_PLAYEROUTPUTLEVEL )
  5710. {
  5711. PDVMSG_PLAYEROUTPUTLEVEL pMsgOutput = (PDVMSG_PLAYEROUTPUTLEVEL) lpData;
  5712. DNASSERT( pMsgOutput->dwSize == sizeof( DVMSG_PLAYEROUTPUTLEVEL ) );
  5713. DNASSERT( dwDataSize == sizeof( DVMSG_PLAYEROUTPUTLEVEL ) );
  5714. }
  5715. lpNewElement->m_lpNext = m_lpNotifyList;
  5716. lpNewElement->m_dwType = dwMessageType;
  5717. lpNewElement->m_dwDataSize = dwDataSize;
  5718. if( dwDataSize <= DV_CLIENT_NOTIFY_ELEMENT_SIZE )
  5719. {
  5720. lpNewElement->m_etElementType = NOTIFY_FIXED;
  5721. }
  5722. else
  5723. {
  5724. lpNewElement->m_etElementType = NOTIFY_DYNAMIC;
  5725. }
  5726. if( lpNewElement->m_etElementType == NOTIFY_FIXED )
  5727. {
  5728. memcpy( &lpNewElement->m_element.fixed, lpData, dwDataSize );
  5729. }
  5730. else if( lpNewElement->m_etElementType == NOTIFY_DYNAMIC )
  5731. {
  5732. lpNewElement->m_element.dynamic.m_lpData = new BYTE[dwDataSize];
  5733. if( lpNewElement->m_element.dynamic.m_lpData == NULL )
  5734. {
  5735. m_pfpNotifications->Release( m_pfpNotifications, lpNewElement );
  5736. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to alloc memory for notification" );
  5737. return DVERR_OUTOFMEMORY;
  5738. }
  5739. memcpy( lpNewElement->m_element.dynamic.m_lpData, lpData, dwDataSize );
  5740. }
  5741. else
  5742. {
  5743. DNASSERT( FALSE );
  5744. }
  5745. // Fixups for internal pointers
  5746. //
  5747. // Required for certain message types (currently only DVMSGID_SETTARGETS)
  5748. if( dwMessageType == DVMSGID_SETTARGETS )
  5749. {
  5750. PDVMSG_SETTARGETS pdvSetTarget;
  5751. if( lpNewElement->m_etElementType == NOTIFY_FIXED )
  5752. {
  5753. pdvSetTarget = (PDVMSG_SETTARGETS) &lpNewElement->m_element.fixed;
  5754. }
  5755. else
  5756. {
  5757. pdvSetTarget = (PDVMSG_SETTARGETS) lpNewElement->m_element.dynamic.m_lpData;
  5758. }
  5759. pdvSetTarget->pdvidTargets = (PDVID) &pdvSetTarget[1];
  5760. lpNewElement->m_dwDataSize = sizeof( DVMSG_SETTARGETS );
  5761. }
  5762. // We're ignoring this notification, call the completion immediately.
  5763. if( !m_fNotifyQueueEnabled )
  5764. {
  5765. DPFX(DPFPREP, DVF_WARNINGLEVEL, "Ignoring notification, calling completion immediately" );
  5766. NotifyQueue_ElementFree( lpNewElement );
  5767. return DV_OK;
  5768. }
  5769. m_lpNotifyList = lpNewElement;
  5770. ReleaseSemaphore( m_hNewNotifyElement, 1, NULL );
  5771. return DV_OK;
  5772. }
  5773. #undef DPF_MODNAME
  5774. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Get"
  5775. // Retrieve the next element in our queue.
  5776. //
  5777. // If there is an element, return DV_OK, otherwise DVERR_GENERIC.
  5778. //
  5779. HRESULT CDirectVoiceClientEngine::NotifyQueue_Get( CNotifyElement **pneElement )
  5780. {
  5781. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5782. slLock.Lock();
  5783. CNotifyElement *lpIterator = m_lpNotifyList;
  5784. CNotifyElement *lpTrailIterator = NULL;
  5785. if( lpIterator == NULL )
  5786. return DVERR_GENERIC;
  5787. // Move forward to the last element in the list
  5788. while( lpIterator->m_lpNext != NULL )
  5789. {
  5790. lpTrailIterator = lpIterator;
  5791. lpIterator = lpIterator->m_lpNext;
  5792. }
  5793. *pneElement = lpIterator;
  5794. // Remove the last element in the list
  5795. // Special case, only element on the list
  5796. if( lpTrailIterator == NULL )
  5797. {
  5798. m_lpNotifyList = NULL;
  5799. }
  5800. else
  5801. {
  5802. lpTrailIterator->m_lpNext = NULL;
  5803. }
  5804. return DV_OK;
  5805. }
  5806. #undef DPF_MODNAME
  5807. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Init"
  5808. HRESULT CDirectVoiceClientEngine::NotifyQueue_Init()
  5809. {
  5810. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5811. slLock.Lock();
  5812. m_pfpNotifications = FPM_Create( sizeof( CNotifyElement ), NULL, NULL, NULL, NULL, NULL, NULL );
  5813. if (m_pfpNotifications==NULL)
  5814. {
  5815. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create fixed pool for notify elements" );
  5816. return DVERR_OUTOFMEMORY;
  5817. }
  5818. m_lpNotifyList = NULL;
  5819. m_hNewNotifyElement = CreateSemaphore( NULL, 0, DVCLIENT_NOTIFY_MAXSEMCOUNT, NULL );
  5820. if (m_hNewNotifyElement==NULL)
  5821. {
  5822. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to create semaphore for notify elements" );
  5823. return DVERR_GENERIC;
  5824. }
  5825. m_fNotifyQueueEnabled = TRUE;
  5826. return DV_OK;
  5827. }
  5828. #undef DPF_MODNAME
  5829. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Enable"
  5830. void CDirectVoiceClientEngine::NotifyQueue_Enable()
  5831. {
  5832. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5833. slLock.Lock();
  5834. m_fNotifyQueueEnabled = TRUE;
  5835. }
  5836. #undef DPF_MODNAME
  5837. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Disable"
  5838. void CDirectVoiceClientEngine::NotifyQueue_Disable()
  5839. {
  5840. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5841. slLock.Lock();
  5842. m_fNotifyQueueEnabled = FALSE;
  5843. }
  5844. #undef DPF_MODNAME
  5845. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_ElementFree"
  5846. HRESULT CDirectVoiceClientEngine::NotifyQueue_ElementFree( CNotifyElement *lpElement )
  5847. {
  5848. // Call the notification function, if there is one
  5849. if( lpElement->pNotifyFunc )
  5850. {
  5851. (*lpElement->pNotifyFunc)(lpElement->pvContext,lpElement);
  5852. lpElement->pNotifyFunc = NULL;
  5853. lpElement->pvContext = NULL;
  5854. }
  5855. // If this element is dynamic free the associated memory
  5856. if( lpElement->m_etElementType == NOTIFY_DYNAMIC )
  5857. {
  5858. delete [] lpElement->m_element.dynamic.m_lpData;
  5859. }
  5860. // Return notifier to the fixed pool manager
  5861. m_pfpNotifications->Release( m_pfpNotifications, lpElement );
  5862. return DV_OK;
  5863. }
  5864. #undef DPF_MODNAME
  5865. #define DPF_MODNAME "CDirectVoiceClientEngine::NotifyQueue_Free"
  5866. HRESULT CDirectVoiceClientEngine::NotifyQueue_Free()
  5867. {
  5868. BFCSingleLock slLock( &m_csNotifyQueueLock );
  5869. slLock.Lock();
  5870. CNotifyElement *lpTmpElement;
  5871. CNotifyElement *lpIteratorElement;
  5872. lpIteratorElement = m_lpNotifyList;
  5873. while( lpIteratorElement != NULL )
  5874. {
  5875. lpTmpElement = lpIteratorElement;
  5876. lpIteratorElement = lpIteratorElement->m_lpNext;
  5877. NotifyQueue_ElementFree( lpTmpElement );
  5878. }
  5879. if( m_hNewNotifyElement != NULL )
  5880. {
  5881. CloseHandle( m_hNewNotifyElement );
  5882. }
  5883. if( m_pfpNotifications != NULL )
  5884. {
  5885. m_pfpNotifications->Fini(m_pfpNotifications);
  5886. m_pfpNotifications = NULL;
  5887. }
  5888. return DV_OK;
  5889. }
  5890. //
  5891. // HANDLERS FOR WHEN WE HANDLE REMOTE SERVERS
  5892. //
  5893. //
  5894. #undef DPF_MODNAME
  5895. #define DPF_MODNAME "CDirectVoiceClientEngine::CreateGroup"
  5896. HRESULT CDirectVoiceClientEngine::CreateGroup( DVID dvID )
  5897. {
  5898. return S_OK;
  5899. }
  5900. #undef DPF_MODNAME
  5901. #define DPF_MODNAME "CDirectVoiceClientEngine::DeleteGroup"
  5902. HRESULT CDirectVoiceClientEngine::DeleteGroup( DVID dvID )
  5903. {
  5904. CheckForAndRemoveTarget( dvID );
  5905. // If there are any buffers for this player, delete them
  5906. //
  5907. // Leave the buffer around so the user can call Delete3DSoundBuffer
  5908. //
  5909. // DeleteSoundTarget( dvID );
  5910. return S_OK;
  5911. }
  5912. #undef DPF_MODNAME
  5913. #define DPF_MODNAME "CDirectVoiceClientEngine::AddPlayerToGroup"
  5914. HRESULT CDirectVoiceClientEngine::AddPlayerToGroup( DVID dvidGroup, DVID dvidPlayer )
  5915. {
  5916. return S_OK;
  5917. }
  5918. #undef DPF_MODNAME
  5919. #define DPF_MODNAME "CDirectVoiceClientEngine::RemovePlayerFromGroup"
  5920. HRESULT CDirectVoiceClientEngine::RemovePlayerFromGroup( DVID dvidGroup, DVID dvidPlayer )
  5921. {
  5922. return S_OK;
  5923. }
  5924. #undef DPF_MODNAME
  5925. #define DPF_MODNAME "CDirectVoiceClientEngine::DoSessionLost"
  5926. void CDirectVoiceClientEngine::DoSessionLost( HRESULT hrDisconnectResult )
  5927. {
  5928. if( GetCurrentState() == DVCSTATE_CONNECTED )
  5929. {
  5930. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "#### SESSION LOST [hr=0x%x]", hrDisconnectResult );
  5931. m_fSessionLost = TRUE;
  5932. DoSignalDisconnect( hrDisconnectResult );
  5933. }
  5934. else if( GetCurrentState() == DVCSTATE_DISCONNECTING )
  5935. {
  5936. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "#### Received session lost during disconnect, completing disconnect" );
  5937. DoSignalDisconnect( m_hrDisconnectResult );
  5938. }
  5939. else
  5940. {
  5941. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "#### Received session lost but not connected or disconnecting, ignoring" );
  5942. }
  5943. }
  5944. #undef DPF_MODNAME
  5945. #define DPF_MODNAME "CDirectVoiceClientEngine::DoSignalDisconnect"
  5946. void CDirectVoiceClientEngine::DoSignalDisconnect( HRESULT hrDisconnectResult )
  5947. {
  5948. DPFX(DPFPREP, DVF_DISCONNECT_PROCEDURE_DEBUG_LEVEL, "#### Disconnecting [hr=0x%x]", hrDisconnectResult );
  5949. m_hrDisconnectResult = hrDisconnectResult;
  5950. SetEvent( m_hNotifyDisconnect );
  5951. }
  5952. #undef DPF_MODNAME
  5953. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Reset"
  5954. void CDirectVoiceClientEngine::ClientStats_Reset()
  5955. {
  5956. memset( &m_stats, 0x00, sizeof( ClientStatistics ) );
  5957. }
  5958. #undef DPF_MODNAME
  5959. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Dump_Record"
  5960. void CDirectVoiceClientEngine::ClientStats_Dump_Record()
  5961. {
  5962. if( m_dvSoundDeviceConfig.dwFlags & DVSOUNDCONFIG_HALFDUPLEX )
  5963. {
  5964. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Session was half duplex, no record stats available" );
  5965. return;
  5966. }
  5967. char tmpBuffer[200];
  5968. DWORD dwRecRunLength = m_pStatsBlob->m_recStats.m_dwTimeStop - m_pStatsBlob->m_recStats.m_dwTimeStart;
  5969. if( dwRecRunLength == 0 )
  5970. dwRecRunLength = 1;
  5971. DWORD dwNumInternalRuns = m_pStatsBlob->m_recStats.m_dwNumWakeups;
  5972. if( dwNumInternalRuns == 0 )
  5973. dwNumInternalRuns = 1;
  5974. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Start Lag (ms) : %u", m_pStatsBlob->m_recStats.m_dwStartLag );
  5975. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Record Run Length (ms) : %u", dwRecRunLength );
  5976. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Speech Size (Uncomp.) : %u", m_pStatsBlob->m_recStats.m_dwUnCompressedSize );
  5977. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Frames / Buffer : %u", m_pStatsBlob->m_recStats.m_dwFramesPerBuffer );
  5978. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Time / Frame (ms) : %u", m_pStatsBlob->m_recStats.m_dwFrameTime );
  5979. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Silence Timeout : %u", m_pStatsBlob->m_recStats.m_dwSilenceTimeout );
  5980. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "# of wakeups : %u", m_pStatsBlob->m_recStats.m_dwNumWakeups );
  5981. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Runs / Wakeup : Avg: %u [%u..%u]",
  5982. m_pStatsBlob->m_recStats.m_dwRPWTotal / dwNumInternalRuns,
  5983. m_pStatsBlob->m_recStats.m_dwRPWMin,
  5984. m_pStatsBlob->m_recStats.m_dwRPWMax );
  5985. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "# of messages : %u", m_pStatsBlob->m_recStats.m_dwNumMessages );
  5986. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Sent Frames : %u", m_pStatsBlob->m_recStats.m_dwSentFrames );
  5987. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Ignored Frames : %u", m_pStatsBlob->m_recStats.m_dwIgnoredFrames );
  5988. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Message Length (frames): Avg: %u [%u..%u]",
  5989. (m_pStatsBlob->m_recStats.m_dwNumMessages == 0) ? 0 : m_pStatsBlob->m_recStats.m_dwMLTotal / m_pStatsBlob->m_recStats.m_dwNumMessages,
  5990. m_pStatsBlob->m_recStats.m_dwMLMin,
  5991. m_pStatsBlob->m_recStats.m_dwMLMax );
  5992. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Header Size (bytes) : Avg: %u [%u..%u]",
  5993. (m_pStatsBlob->m_recStats.m_dwSentFrames == 0) ? 0 : m_pStatsBlob->m_recStats.m_dwHSTotal / m_pStatsBlob->m_recStats.m_dwSentFrames,
  5994. m_pStatsBlob->m_recStats.m_dwHSMin,
  5995. m_pStatsBlob->m_recStats.m_dwHSMax );
  5996. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Speech Size (bytes) : Avg: %u [%u..%u]",
  5997. (m_pStatsBlob->m_recStats.m_dwSentFrames == 0) ? 0 : m_pStatsBlob->m_recStats.m_dwCSTotal / m_pStatsBlob->m_recStats.m_dwSentFrames,
  5998. m_pStatsBlob->m_recStats.m_dwCSMin,
  5999. m_pStatsBlob->m_recStats.m_dwCSMax );
  6000. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Speech Convert (ms) : Avg: %u [%u..%u]",
  6001. (m_pStatsBlob->m_recStats.m_dwSentFrames == 0) ? 0 : (m_pStatsBlob->m_recStats.m_dwCTTotal / m_pStatsBlob->m_recStats.m_dwSentFrames),
  6002. m_pStatsBlob->m_recStats.m_dwCTMin,
  6003. m_pStatsBlob->m_recStats.m_dwCTMax );
  6004. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Record Resets : %u [Before Success %u..%u]", m_pStatsBlob->m_recStats.m_dwRRTotal,
  6005. m_pStatsBlob->m_recStats.m_dwRRMin,
  6006. m_pStatsBlob->m_recStats.m_dwRRMax );
  6007. sprintf( tmpBuffer, "Rec Movement (ms) : Avg: %u [%u..%u]",
  6008. m_pStatsBlob->m_recStats.m_dwRMMSTotal / dwNumInternalRuns,
  6009. m_pStatsBlob->m_recStats.m_dwRMMSMin,
  6010. m_pStatsBlob->m_recStats.m_dwRMMSMax );
  6011. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6012. sprintf( tmpBuffer, "Rec Movement (frames) : Avg: %.2f [%.2f..%.2f]",
  6013. ( ((float)m_pStatsBlob->m_recStats.m_dwRMMSTotal) / ((float) dwNumInternalRuns)) / ((float) m_pStatsBlob->m_recStats.m_dwFrameTime),
  6014. ((float) m_pStatsBlob->m_recStats.m_dwRMMSMin) / ((float) m_pStatsBlob->m_recStats.m_dwFrameTime),
  6015. ((float) m_pStatsBlob->m_recStats.m_dwRMMSMax) / ((float) m_pStatsBlob->m_recStats.m_dwFrameTime));
  6016. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6017. sprintf( tmpBuffer, "Rec Movement (bytes) : Avg: %u [%u..%u]",
  6018. m_pStatsBlob->m_recStats.m_dwRMBTotal / dwNumInternalRuns,
  6019. m_pStatsBlob->m_recStats.m_dwRMBMin,
  6020. m_pStatsBlob->m_recStats.m_dwRMBMax );
  6021. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6022. sprintf( tmpBuffer, "Rec Movement (frames) : Avg: %.2f [%.2f..%.2f]",
  6023. (((float) m_pStatsBlob->m_recStats.m_dwRMBTotal) / ((float) dwNumInternalRuns)) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize),
  6024. ((float) m_pStatsBlob->m_recStats.m_dwRMBMin) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize),
  6025. ((float) m_pStatsBlob->m_recStats.m_dwRMBMax) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize) );
  6026. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6027. sprintf( tmpBuffer, "Move Delta (ms) : Avg: %u [%u..%u]",
  6028. m_pStatsBlob->m_recStats.m_dwRTSLMTotal / dwNumInternalRuns,
  6029. m_pStatsBlob->m_recStats.m_dwRTSLMMin,
  6030. m_pStatsBlob->m_recStats.m_dwRTSLMMax );
  6031. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6032. sprintf( tmpBuffer, "Move Delta (frames) : Avg: %.2f [%.2f..%.2f]",
  6033. (((float) m_pStatsBlob->m_recStats.m_dwRTSLMTotal) / ((float) dwNumInternalRuns)) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize),
  6034. ((float) m_pStatsBlob->m_recStats.m_dwRTSLMMin) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize),
  6035. ((float) m_pStatsBlob->m_recStats.m_dwRTSLMMax) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize) );
  6036. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6037. sprintf( tmpBuffer, "Record Lag (bytes) : Avg: %u [%u..%u]",
  6038. m_pStatsBlob->m_recStats.m_dwRLTotal / dwNumInternalRuns,
  6039. m_pStatsBlob->m_recStats.m_dwRLMin ,
  6040. m_pStatsBlob->m_recStats.m_dwRLMax );
  6041. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6042. sprintf( tmpBuffer, "Record Lag (frames) : Avg: %.2f [%.2f..%.2f]",
  6043. (float) ((float) m_pStatsBlob->m_recStats.m_dwRLTotal / (float) dwNumInternalRuns) / ((float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize),
  6044. (float) ((float) m_pStatsBlob->m_recStats.m_dwRLMin) / (float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize,
  6045. (float) ((float) m_pStatsBlob->m_recStats.m_dwRLMax) / (float) m_pStatsBlob->m_recStats.m_dwUnCompressedSize );
  6046. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, tmpBuffer );
  6047. }
  6048. #undef DPF_MODNAME
  6049. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Dump_Playback"
  6050. void CDirectVoiceClientEngine::ClientStats_Dump_Playback()
  6051. {
  6052. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Playback Stats: " );
  6053. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Frames (silent) : %d", m_pStatsBlob->m_dwPPDQSilent );
  6054. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Frames (lost) : %d", m_pStatsBlob->m_dwPPDQLost );
  6055. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Frames (speech) : %d", m_pStatsBlob->m_dwPPDQSpeech );
  6056. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Speech Decompress (ms) : Avg: %u [%u..%u]",
  6057. (m_pStatsBlob->m_dwPPDQSpeech == 0) ? 0 : (m_pStatsBlob->m_dwPDTTotal / m_pStatsBlob->m_dwPPDQSpeech),
  6058. m_pStatsBlob->m_dwPDTMin,
  6059. m_pStatsBlob->m_dwPDTMax );
  6060. }
  6061. #undef DPF_MODNAME
  6062. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Dump_Receive"
  6063. void CDirectVoiceClientEngine::ClientStats_Dump_Receive()
  6064. {
  6065. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Receive Stats: " );
  6066. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Received Frames : %d", m_pStatsBlob->m_dwPRESpeech );
  6067. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "Received But Lost : %d", m_pStatsBlob->m_dwPRESpeech - m_pStatsBlob->m_dwPPDQSpeech );
  6068. }
  6069. #undef DPF_MODNAME
  6070. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Dump_Transmit"
  6071. void CDirectVoiceClientEngine::ClientStats_Dump_Transmit()
  6072. {
  6073. }
  6074. #undef DPF_MODNAME
  6075. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Dump"
  6076. void CDirectVoiceClientEngine::ClientStats_Dump()
  6077. {
  6078. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "STATS DUMP: ----------------------------------------------------[Begin] " );
  6079. ClientStats_Dump_Record();
  6080. ClientStats_Dump_Playback();
  6081. ClientStats_Dump_Receive();
  6082. ClientStats_Dump_Transmit();
  6083. DPFX(DPFPREP, DVF_STATS_DEBUG_LEVEL, "STATS DUMP: ------------------------------------------------------[End] " );
  6084. }
  6085. #undef DPF_MODNAME
  6086. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_Begin"
  6087. void CDirectVoiceClientEngine::ClientStats_Begin()
  6088. {
  6089. m_pStatsBlob->m_dwTimeStart = GetTickCount();
  6090. m_pStatsBlob->m_dwMaxBuffers = 1;
  6091. m_pStatsBlob->m_dwTotalBuffers = 1;
  6092. m_pStatsBlob->m_dwPDTMin = 0xFFFFFFFF;
  6093. }
  6094. #undef DPF_MODNAME
  6095. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientStats_End"
  6096. void CDirectVoiceClientEngine::ClientStats_End()
  6097. {
  6098. m_pStatsBlob->m_dwTimeStop = GetTickCount();
  6099. }
  6100. #undef DPF_MODNAME
  6101. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientBufferAlloc"
  6102. PVOID CDirectVoiceClientEngine::ClientBufferAlloc( void *const pv, const DWORD dwSize )
  6103. {
  6104. return new BYTE[dwSize];
  6105. }
  6106. #undef DPF_MODNAME
  6107. #define DPF_MODNAME "CDirectVoiceClientEngine::ClientBufferFree"
  6108. void CDirectVoiceClientEngine::ClientBufferFree( void *const pv, void *const pvBuffer )
  6109. {
  6110. delete pvBuffer;
  6111. }
  6112. #undef DPF_MODNAME
  6113. /*#define DPF_MODNAME "CDirectVoiceClientEngine::GetTransmitBuffer"
  6114. PDVTRANSPORT_BUFFERDESC CDirectVoiceClientEngine::GetTransmitBuffer( DWORD dwSize, LPVOID *ppvSendContext )
  6115. {
  6116. PDVTRANSPORT_BUFFERDESC pNewBuffer;
  6117. pNewBuffer = new DVTRANSPORT_BUFFERDESC;
  6118. pNewBuffer->pBufferData = new BYTE[dwSize];
  6119. pNewBuffer->dwBufferSize = dwSize;
  6120. pNewBuffer->lRefCount = 0;
  6121. pNewBuffer->dwObjectType = DVTRANSPORT_OBJECTTYPE_CLIENT;
  6122. pNewBuffer->dwFlags = 0;
  6123. *ppvSendContext = pNewBuffer;
  6124. return pNewBuffer;
  6125. }
  6126. #undef DPF_MODNAME
  6127. #define DPF_MODNAME "CDirectVoiceClientEngine::ReturnTransmitBuffer"
  6128. // ReturnTransmitBuffer
  6129. //
  6130. // PDVTRANSPORT_BUFFERDESC pBufferDesc - Buffer description of buffer to return
  6131. // LPVOID lpvContext - Context value to be used when returning the buffer
  6132. //
  6133. void CDirectVoiceClientEngine::ReturnTransmitBuffer( PVOID pvContext )
  6134. {
  6135. PDVTRANSPORT_BUFFERDESC pBufferDesc = (PDVTRANSPORT_BUFFERDESC) pvContext;
  6136. delete [] pBufferDesc->pBufferData;
  6137. delete pBufferDesc;
  6138. }
  6139. #undef DPF_MODNAME
  6140. #define DPF_MODNAME "CDirectVoiceClientEngine::SendComplete"
  6141. HRESULT CDirectVoiceClientEngine::SendComplete( PDVEVENTMSG_SENDCOMPLETE pSendComplete )
  6142. {
  6143. ReturnTransmitBuffer( pSendComplete->pvUserContext );
  6144. return DV_OK;
  6145. }*/
  6146. #undef DPF_MODNAME
  6147. #define DPF_MODNAME "CDirectVoiceClientEngine::GetTransmitBuffer"
  6148. PDVTRANSPORT_BUFFERDESC CDirectVoiceClientEngine::GetTransmitBuffer( DWORD dwSize, LPVOID *ppvSendContext )
  6149. {
  6150. PDVTRANSPORT_BUFFERDESC pNewBuffer = NULL;
  6151. DWORD dwFPMIndex = 0xFFFFFFFF;
  6152. DWORD dwWastedSpace = 0xFFFFFFFF;
  6153. DWORD dwSearchFPMIndex = 0;
  6154. DNEnterCriticalSection( &m_csTransmitBufferLock );
  6155. pNewBuffer = (PDVTRANSPORT_BUFFERDESC) m_pBufferDescPool->Get( m_pBufferDescPool );
  6156. DNLeaveCriticalSection( &m_csTransmitBufferLock );
  6157. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: Got a buffer desc address 0x%p", (void *) pNewBuffer );
  6158. if( pNewBuffer == NULL )
  6159. {
  6160. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error getting transmit buffer" );
  6161. goto GETTRANSMITBUFFER_ERROR;
  6162. }
  6163. pNewBuffer->lRefCount = 0;
  6164. pNewBuffer->dwObjectType = DVTRANSPORT_OBJECTTYPE_CLIENT;
  6165. pNewBuffer->dwFlags = 0;
  6166. pNewBuffer->pBufferData = NULL;
  6167. for( dwSearchFPMIndex = 0; dwSearchFPMIndex < m_dwNumPools; dwSearchFPMIndex++ )
  6168. {
  6169. // Potential pool
  6170. if( m_pdwBufferPoolSizes[dwSearchFPMIndex] >= dwSize )
  6171. {
  6172. if( m_pdwBufferPoolSizes[dwSearchFPMIndex] - dwSize < dwWastedSpace )
  6173. {
  6174. dwWastedSpace = m_pdwBufferPoolSizes[dwSearchFPMIndex] - dwSize;
  6175. dwFPMIndex = dwSearchFPMIndex;
  6176. }
  6177. }
  6178. }
  6179. if( dwFPMIndex == 0xFFFFFFFF )
  6180. {
  6181. DNASSERT( FALSE );
  6182. DPFX(DPFPREP, 0, "Could not find pool large enough for buffer" );
  6183. goto GETTRANSMITBUFFER_ERROR;
  6184. }
  6185. pNewBuffer->pvContext = m_pBufferPools[dwFPMIndex];
  6186. DNEnterCriticalSection( &m_csTransmitBufferLock );
  6187. pNewBuffer->pBufferData = (PBYTE) m_pBufferPools[dwFPMIndex]->Get(m_pBufferPools[dwFPMIndex]);
  6188. DNLeaveCriticalSection( &m_csTransmitBufferLock );
  6189. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: Got a buffer value at address 0x%p", (void *) pNewBuffer->pBufferData );
  6190. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: nInUse = %i", m_pBufferDescPool->nInUse );
  6191. if( pNewBuffer->pBufferData == NULL )
  6192. {
  6193. DPFX(DPFPREP, 0, "Error getting buffer for buffer desc" );
  6194. goto GETTRANSMITBUFFER_ERROR;
  6195. }
  6196. pNewBuffer->dwBufferSize = dwSize;
  6197. *ppvSendContext = pNewBuffer;
  6198. return pNewBuffer;
  6199. GETTRANSMITBUFFER_ERROR:
  6200. DNEnterCriticalSection( &m_csTransmitBufferLock );
  6201. if( pNewBuffer != NULL && pNewBuffer->pBufferData != NULL )
  6202. {
  6203. ((PFPOOL) pNewBuffer->pvContext)->Release( ((PFPOOL) pNewBuffer->pvContext), pNewBuffer->pBufferData );
  6204. }
  6205. if( pNewBuffer != NULL )
  6206. {
  6207. m_pBufferDescPool->Release( m_pBufferDescPool, pNewBuffer );
  6208. }
  6209. DNLeaveCriticalSection( &m_csTransmitBufferLock );
  6210. return NULL;
  6211. }
  6212. #undef DPF_MODNAME
  6213. #define DPF_MODNAME "CDirectVoiceClientEngine::ReturnTransmitBuffer"
  6214. // ReturnTransmitBuffer
  6215. //
  6216. // PDVTRANSPORT_BUFFERDESC pBufferDesc - Buffer description of buffer to return
  6217. // LPVOID lpvContext - Context value to be used when returning the buffer
  6218. //
  6219. void CDirectVoiceClientEngine::ReturnTransmitBuffer( PVOID pvContext )
  6220. {
  6221. PDVTRANSPORT_BUFFERDESC pBufferDesc = (PDVTRANSPORT_BUFFERDESC) pvContext;
  6222. PFPOOL pPool = (PFPOOL) pBufferDesc->pvContext;
  6223. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: Returning a buffer desc at address 0x%p", (void *) pBufferDesc );
  6224. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: Returning a buffer at address 0x%p", (void *) pBufferDesc->pBufferData );
  6225. DNEnterCriticalSection( &m_csTransmitBufferLock );
  6226. // Release memory
  6227. pPool->Release( pPool, pBufferDesc->pBufferData );
  6228. // Release buffer description
  6229. m_pBufferDescPool->Release( m_pBufferDescPool, pvContext );
  6230. DPFX(DPFPREP, DVF_BUFFERDESC_DEBUG_LEVEL, "BUFFERDESC: nInUse = %i", m_pBufferDescPool->nInUse );
  6231. DNLeaveCriticalSection( &m_csTransmitBufferLock );
  6232. }
  6233. #undef DPF_MODNAME
  6234. #define DPF_MODNAME "CDirectVoiceClientEngine::SendComplete"
  6235. HRESULT CDirectVoiceClientEngine::SendComplete( PDVEVENTMSG_SENDCOMPLETE pSendComplete )
  6236. {
  6237. ReturnTransmitBuffer( pSendComplete->pvUserContext );
  6238. return DV_OK;
  6239. }
  6240. #undef DPF_MODNAME
  6241. #define DPF_MODNAME "CDirectVoiceClientEngine::SetupInitialBuffers"
  6242. // SetupBuffersInitial
  6243. //
  6244. // This function sets up the first transmit buffers which do not vary
  6245. // in size w/the compression type.
  6246. //
  6247. HRESULT CDirectVoiceClientEngine::SetupInitialBuffers()
  6248. {
  6249. HRESULT hr = DV_OK;
  6250. DWORD dwIndex;
  6251. m_dwNumPools = CLIENT_POOLS_NUM;
  6252. m_pBufferDescPool = FPM_Create( sizeof(DVTRANSPORT_BUFFERDESC), NULL, NULL, NULL, NULL, &m_pStatsBlob->m_dwBDPOutstanding, &m_pStatsBlob->m_dwBDPAllocated );
  6253. if( m_pBufferDescPool == NULL )
  6254. {
  6255. DPFX(DPFPREP, 0, "Error allocating memory" );
  6256. hr = DVERR_OUTOFMEMORY;
  6257. goto SETUPBUFFERS_ERROR;
  6258. }
  6259. m_pBufferPools = new PFPOOL[m_dwNumPools];
  6260. if( m_pBufferPools == NULL )
  6261. {
  6262. DPFX(DPFPREP, 0, "Error allocating memory" );
  6263. hr = DVERR_OUTOFMEMORY;
  6264. goto SETUPBUFFERS_ERROR;
  6265. }
  6266. memset( m_pBufferPools, 0x00, sizeof( PFPOOL ) * m_dwNumPools );
  6267. m_pdwBufferPoolSizes = new DWORD[m_dwNumPools];
  6268. if( m_pdwBufferPoolSizes == NULL )
  6269. {
  6270. DPFX(DPFPREP, 0, "Error allocating memory" );
  6271. hr = DVERR_OUTOFMEMORY;
  6272. goto SETUPBUFFERS_ERROR;
  6273. }
  6274. m_pdwBufferPoolSizes[0] = CLIENT_POOLS_SIZE_MESSAGE;
  6275. m_pdwBufferPoolSizes[1] = CLIENT_POOLS_SIZE_PLAYERLIST;
  6276. m_pdwBufferPoolSizes[ m_dwNumPools-1] = 0;
  6277. for( dwIndex = 0; dwIndex < m_dwNumPools-1; dwIndex++ )
  6278. {
  6279. m_pBufferPools[dwIndex] = FPM_Create( m_pdwBufferPoolSizes[dwIndex], NULL, NULL, NULL, NULL, &m_pStatsBlob->m_dwBPOutstanding[dwIndex], &m_pStatsBlob->m_dwBPAllocated[dwIndex] );
  6280. if( m_pBufferPools == NULL )
  6281. {
  6282. DPFX(DPFPREP, 0, "Error creating transmit buffers" );
  6283. goto SETUPBUFFERS_ERROR;
  6284. }
  6285. }
  6286. m_pBufferPools[dwIndex] = NULL;
  6287. return DV_OK;
  6288. SETUPBUFFERS_ERROR:
  6289. FreeBuffers();
  6290. return hr;
  6291. }
  6292. #undef DPF_MODNAME
  6293. #define DPF_MODNAME "CDirectVoiceServerEngine::SetupSpeechBuffer"
  6294. // SetupSpeechBuffer
  6295. //
  6296. // This function sets up the buffer pool for speech sends, whose size will
  6297. // depend on the compression type. Must be done after we know CT but
  6298. // before we do first speech transmission.
  6299. //
  6300. HRESULT CDirectVoiceClientEngine::SetupSpeechBuffer()
  6301. {
  6302. if( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER ||
  6303. m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_ECHO )
  6304. {
  6305. m_pdwBufferPoolSizes[m_dwNumPools-1] = sizeof( DVPROTOCOLMSG_SPEECHHEADER )+m_dwCompressedFrameSize+COMPRESSION_SLUSH;
  6306. }
  6307. else
  6308. {
  6309. m_pdwBufferPoolSizes[m_dwNumPools-1] = sizeof( DVPROTOCOLMSG_SPEECHWITHTARGET ) + m_dwCompressedFrameSize +
  6310. (sizeof( DVID )*CLIENT_POOLS_NUM_TARGETS_BUFFERED)+COMPRESSION_SLUSH;
  6311. }
  6312. m_pBufferPools[m_dwNumPools-1] = FPM_Create( m_pdwBufferPoolSizes[m_dwNumPools-1], NULL, NULL, NULL, NULL, &m_pStatsBlob->m_dwBPOutstanding[m_dwNumPools-1], &m_pStatsBlob->m_dwBPAllocated[m_dwNumPools-1] );
  6313. if( m_pBufferPools == NULL )
  6314. {
  6315. DPFX(DPFPREP, 0, "Error creating transmit buffers" );
  6316. return DVERR_OUTOFMEMORY;
  6317. }
  6318. return DV_OK;
  6319. }
  6320. #undef DPF_MODNAME
  6321. #define DPF_MODNAME "CDirectVoiceServerEngine::FreeBuffers"
  6322. HRESULT CDirectVoiceClientEngine::FreeBuffers()
  6323. {
  6324. DWORD dwIndex;
  6325. if( m_pBufferPools != NULL )
  6326. {
  6327. for( dwIndex = 0; dwIndex < m_dwNumPools; dwIndex++ )
  6328. {
  6329. if( m_pBufferPools[dwIndex] != NULL )
  6330. m_pBufferPools[dwIndex]->Fini(m_pBufferPools[dwIndex],FALSE);
  6331. }
  6332. delete [] m_pBufferPools;
  6333. m_pBufferPools = NULL;
  6334. }
  6335. if( m_pdwBufferPoolSizes != NULL )
  6336. {
  6337. delete [] m_pdwBufferPoolSizes;
  6338. m_pdwBufferPoolSizes = 0;
  6339. }
  6340. if( m_pBufferDescPool != NULL )
  6341. {
  6342. m_pBufferDescPool->Fini( m_pBufferDescPool, FALSE );
  6343. m_pBufferDescPool = NULL;
  6344. }
  6345. m_dwNumPools = 0;
  6346. return DV_OK;
  6347. }
  6348. #undef DPF_MODNAME
  6349. #define DPF_MODNAME "CDirectVoiceClientEngine::ValidateSessionType"
  6350. BOOL CDirectVoiceClientEngine::ValidateSessionType( DWORD dwSessionType )
  6351. {
  6352. return (dwSessionType > 0 && dwSessionType < DVSESSIONTYPE_MAX);
  6353. }
  6354. #undef DPF_MODNAME
  6355. #define DPF_MODNAME "CDirectVoiceClientEngine::ValidateSessionFlags"
  6356. BOOL CDirectVoiceClientEngine::ValidateSessionFlags( DWORD dwFlags )
  6357. {
  6358. return (dwFlags < DVSESSION_MAX);
  6359. }
  6360. #undef DPF_MODNAME
  6361. #define DPF_MODNAME "CDirectVoiceClientEngine::ValidatePlayerFlags"
  6362. BOOL CDirectVoiceClientEngine::ValidatePlayerFlags(DWORD dwFlags)
  6363. {
  6364. return (dwFlags < DVPLAYERCAPS_MAX);
  6365. }
  6366. #undef DPF_MODNAME
  6367. #define DPF_MODNAME "CDirectVoiceClientEngine::ValidatePlayerDVID"
  6368. BOOL CDirectVoiceClientEngine::ValidatePlayerDVID(DVID dvid)
  6369. {
  6370. return (dvid != DVID_ALLPLAYERS);
  6371. }
  6372. #undef DPF_MODNAME
  6373. #define DPF_MODNAME "CDirectVoiceClientEngine::ValidatePacketType"
  6374. BOOL CDirectVoiceClientEngine::ValidatePacketType( PDVPROTOCOLMSG_FULLMESSAGE lpdvFullMessage )
  6375. {
  6376. switch( lpdvFullMessage->dvGeneric.dwType )
  6377. {
  6378. case DVMSGID_HOSTMIGRATELEAVE:
  6379. case DVMSGID_HOSTMIGRATED:
  6380. case DVMSGID_CREATEVOICEPLAYER:
  6381. case DVMSGID_DELETEVOICEPLAYER:
  6382. case DVMSGID_PLAYERLIST:
  6383. return ( ( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_PEER ) );
  6384. break;
  6385. case DVMSGID_SPEECHWITHFROM:
  6386. return ( ( m_dvSessionDesc.dwSessionType == DVSESSIONTYPE_FORWARDING) );
  6387. break;
  6388. case DVMSGID_SETTARGETS:
  6389. return ( m_dvSessionDesc.dwFlags & DVSESSION_SERVERCONTROLTARGET );
  6390. break;
  6391. }
  6392. return TRUE;
  6393. }