DSoundVoiceAdapter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief Connection between DirectSound and RakVoice
  12. ///
  13. /*
  14. This sample uses RakVoice along with DirectSound 8.
  15. Instead of using IDirectSoundBuffer::GetCurrentPosition / IDirectSoundCaptureBuffer::GetCurrentPosition
  16. to keep track of the play/write cursors, notifications are used instead.
  17. Speex encodes/decodes sound in blocks (frames), so the size of the used DirectSound buffers is
  18. multiple of the Speex's frame size. This makes it easier to implement DirectSound notifications at the end
  19. of each frame.
  20. */
  21. #ifndef __DSOUNDVOICEADAPTER_H
  22. #define __DSOUNDVOICEADAPTER_H
  23. #include "RakVoice.h"
  24. // If you get:
  25. // Error 1 fatal error C1083: Cannot open include file: 'dsound.h': No such file or directory
  26. // It is because this project depends on Directx SDK. You must have the DirectX SDK installed.
  27. // Also, check if you have the DXSDK_DIR environment variable point to the right DX SDK path,
  28. // or change the project settings to point to the right include and lib directories
  29. #include "dsound.h"
  30. #include "dxerr.h"
  31. namespace RakNet {
  32. class DSoundVoiceAdapter
  33. {
  34. public:
  35. // --------------------------------------------------------------------------------------------
  36. // User functions
  37. // --------------------------------------------------------------------------------------------
  38. /// Returns the singleton
  39. static DSoundVoiceAdapter* Instance();
  40. /// \brief Setups the connection between RakVoice and DirectSound
  41. /// This function initializes all the required DirectSound objects for you. If you already have
  42. /// initialized DirectSound on your own, use the other supplied SetupAdater function, where you can
  43. /// pass your own DirectSound device object
  44. /// \param[in] rakVoice RakVoice object to use, fully Initialized AND attached to a RakPeerInterface.
  45. /// \param[in] hwnd Your Window Handle. Required for DirectSound initialization
  46. /// \param[in] dwCoopLevel DirectSound cooperative level. Required for DirectSound initialization
  47. bool SetupAdapter(RakVoice *rakVoice, HWND hwnd, DWORD dwCoopLevel=DSSCL_EXCLUSIVE);
  48. /// \brief Setups the connection between RakVoice and DirectSound
  49. /// \param[in] rakVoice RakVoice object to use, fully Initialized AND attached to a RakPeerInterface.
  50. /// \param[in] pDS DirectSound Device Object to use.
  51. /// \pre IMPORTANT : Don't forget to initialized and attach rakVoice, before calling this method.
  52. /// \return true on success, false if an error occurred.
  53. bool SetupAdapter(RakVoice *rakVoice, IDirectSound8 *pDS);
  54. /// \brief Releases any resources used
  55. void Release();
  56. /// \brief This needs to be called once per game frame
  57. void Update();
  58. /// \brief Turns on/off outgoing traffic
  59. /// \param[in] true to mute, false to allow outgoing traffic.
  60. void SetMute(bool mute);
  61. /// \brief Returns the used DirectSound Device object
  62. IDirectSound8* GetDSDeviceObject();
  63. private:
  64. enum
  65. {
  66. FRAMES_IN_SOUND = 2 // Number of voice frames the DirectSound buffers will contain
  67. };
  68. /// Internal setup function called after proper directsound initialization
  69. bool SetupAdapter(RakVoice *rakVoice);
  70. bool SetupIncomingBuffer();
  71. bool SetupOutgoingBuffer();
  72. void BroadcastFrame(void *ptr);
  73. DSoundVoiceAdapter();
  74. DSoundVoiceAdapter(DSoundVoiceAdapter &) {}
  75. static DSoundVoiceAdapter instance;
  76. RakVoice *rakVoice;
  77. IDirectSound8 *ds; // Pointer to the DirectSound Device Object
  78. IDirectSoundCapture8 *dsC; // Pointer to the DirectSound Capture Device Object
  79. IDirectSoundBuffer8 *dsbIncoming; // DirectSound buffer for incoming sound (what you'll hear)
  80. IDirectSoundCaptureBuffer8 *dsbOutgoing; // DirectSound buffer used for outgoing sound (capture from your microphone and send to the other players)
  81. bool mute;
  82. // DirectSound notification positions with the required Win32 Event objects
  83. DSBPOSITIONNOTIFY incomingBufferNotifications[FRAMES_IN_SOUND];
  84. DSBPOSITIONNOTIFY outgoingBufferNotifications[FRAMES_IN_SOUND];
  85. };
  86. } // namespace RakNet
  87. #endif
粤ICP备19079148号