RakVoice.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Voice compression and transmission interface
  12. #ifndef __RAK_VOICE_H
  13. #define __RAK_VOICE_H
  14. #include "RakNetTypes.h"
  15. #include "PluginInterface2.h"
  16. #include "DS_OrderedList.h"
  17. #include "NativeTypes.h"
  18. namespace RakNet {
  19. class RakPeerInterface;
  20. // How many frames large to make the circular buffers in the VoiceChannel structure
  21. #define FRAME_OUTGOING_BUFFER_COUNT 100
  22. #define FRAME_INCOMING_BUFFER_COUNT 100
  23. /// \internal
  24. struct VoiceChannel
  25. {
  26. RakNetGUID guid;
  27. void *enc_state;
  28. void *dec_state;
  29. void *pre_state;
  30. unsigned int remoteSampleRate;
  31. // Circular buffer of unencoded sound data read from the user.
  32. char *outgoingBuffer;
  33. // Each frame sent to speex requires this many samples, of whatever size you are using.
  34. int speexOutgoingFrameSampleCount;
  35. // Index in is bytes.
  36. // Write index points to the next byte to write to, which must be free.
  37. unsigned outgoingReadIndex, outgoingWriteIndex;
  38. bool isSendingVoiceData;
  39. bool bufferOutput;
  40. bool copiedOutgoingBufferToBufferedOutput;
  41. unsigned short outgoingMessageNumber;
  42. // Circular buffer of unencoded sound data to be passed to the user. Each element in the buffer is of size bufferSizeBytes bytes.
  43. char *incomingBuffer;
  44. int speexIncomingFrameSampleCount;
  45. unsigned incomingReadIndex, incomingWriteIndex; // Index in bytes
  46. unsigned short incomingMessageNumber; // The ID_VOICE message number we expect to get. Used to drop out of order and detect how many missing packets in a sequence
  47. RakNet::TimeMS lastSend;
  48. };
  49. int VoiceChannelComp( const RakNetGUID &key, VoiceChannel * const &data );
  50. /// Voice compression and transmission interface
  51. class RAK_DLL_EXPORT RakVoice : public PluginInterface2
  52. {
  53. public:
  54. RakVoice();
  55. virtual ~RakVoice();
  56. // --------------------------------------------------------------------------------------------
  57. // User functions
  58. // --------------------------------------------------------------------------------------------
  59. /// \brief Starts RakVoice
  60. /// \param[in] speexSampleRate 8000, 16000, or 32000
  61. /// \param[in] bufferSizeBytes How many bytes long inputBuffer and outputBuffer are in SendFrame and ReceiveFrame are. Should be your sample size * the number of samples to encode at once.
  62. void Init(unsigned short speexSampleRate, unsigned bufferSizeBytes);
  63. /// \brief Changes encoder complexity
  64. /// Specifying higher values might help when encoding non-speech sounds.
  65. /// \param[in] complexity 0 to 10. The higher the value, the more CPU it needs. Recommended values are from 2 to 4.
  66. void SetEncoderComplexity(int complexity);
  67. /// \brief Enables or disables VAD (Voice Activity Detection)
  68. /// Enabling VAD can help reduce the amount of data transmitted, by automatically disabling outgoing data, when no voice is detected.
  69. /// Don't turn this off or the receive buffer fills up and you eventually get very long delays!!
  70. /// \pre Only applies to encoder.
  71. /// \param[in] enable true to enable, false to disable. True by default
  72. void SetVAD(bool enable);
  73. /// \brief Enables or disables the noise filter
  74. /// \pre Only applies to encoder.
  75. /// \param[in] enable true to enable, false to disable.
  76. void SetNoiseFilter(bool enable);
  77. /// \brief Enables or disables VBR
  78. /// VBR is variable bitrate. Uses less bandwidth but more CPU if on.
  79. /// \pre Only applies to encoder.
  80. /// \param[in] enable true to enable VBR, false to disable
  81. void SetVBR(bool enable);
  82. /// \brief Returns the complexity of the encoder
  83. /// \pre Only applies to encoder.
  84. /// \return a value from 0 to 10.
  85. int GetEncoderComplexity(void);
  86. /// \brief Returns current state of VAD.
  87. /// \pre Only applies to encoder.
  88. /// \return true if VAD is enable, false otherwise
  89. bool IsVADActive(void);
  90. /// \brief Returns the current state of the noise filter
  91. /// \pre Only applies to encoder.
  92. /// \return true if the noise filter is active, false otherwise.
  93. bool IsNoiseFilterActive();
  94. /// \brief Returns the current state of VBR
  95. /// \pre Only applies to encoder.
  96. /// \return true if VBR is active, false otherwise.
  97. bool IsVBRActive();
  98. /// Shuts down RakVoice
  99. void Deinit(void);
  100. /// \brief Opens a channel to another connected system
  101. /// You will get ID_RAKVOICE_OPEN_CHANNEL_REPLY on success
  102. /// \param[in] recipient Which system to open a channel to
  103. void RequestVoiceChannel(RakNetGUID recipient);
  104. /// \brief Closes an existing voice channel.
  105. /// Other system will get ID_RAKVOICE_CLOSE_CHANNEL
  106. /// \param[in] recipient Which system to close a channel with
  107. void CloseVoiceChannel(RakNetGUID recipient);
  108. /// \brief Closes all existing voice channels
  109. /// Other systems will get ID_RAKVOICE_CLOSE_CHANNEL
  110. void CloseAllChannels(void);
  111. /// \brief Sends voice data to a system on an open channel
  112. /// \pre \a recipient must refer to a system with an open channel via RequestVoiceChannel
  113. /// \param[in] recipient The system to send voice data to
  114. /// \param[in] inputBuffer The voice data. The size of inputBuffer should be what was specified as bufferSizeBytes in Init
  115. bool SendFrame(RakNetGUID recipient, void *inputBuffer);
  116. /// \brief Returns if we are currently sending voice data, accounting for voice activity detection
  117. /// \param[in] Which system to check
  118. /// \return If we are sending voice data for the specified system
  119. bool IsSendingVoiceDataTo(RakNetGUID recipient);
  120. /// \brief Gets decoded voice data, from one or more remote senders
  121. /// \param[out] outputBuffer The voice data. The size of outputBuffer should be what was specified as bufferSizeBytes in Init
  122. void ReceiveFrame(void *outputBuffer);
  123. /// Returns the value sample rate, as passed to Init
  124. /// \return the sample rate
  125. int GetSampleRate(void) const;
  126. /// Returns the buffer size in bytes, as passed to Init
  127. /// \return buffer size in bytes
  128. int GetBufferSizeBytes(void) const;
  129. /// Returns true or false, indicating if the object has been initialized
  130. /// \return true if initialized, false otherwise.
  131. bool IsInitialized(void) const;
  132. /// Returns the RakPeerInterface that the object is attached to.
  133. /// \return the respective RakPeerInterface, or NULL not attached.
  134. RakPeerInterface* GetRakPeerInterface(void) const;
  135. /// How many bytes are on the write buffer, waiting to be passed to a call to RakPeer::Send (internally)
  136. /// This should remain at a fairly small near-constant size as outgoing data is sent to the Send function
  137. /// \param[in] guid The system to query, or RakNet::UNASSIGNED_SYSTEM_ADDRESS for the sum of all channels.
  138. /// \return Number of bytes on the write buffer
  139. unsigned GetBufferedBytesToSend(RakNetGUID guid) const;
  140. /// How many bytes are on the read buffer, waiting to be passed to a call to ReceiveFrame
  141. /// This should remain at a fairly small near-constant size as incoming data is read out at the same rate as outgoing data from the remote system
  142. /// \param[in] guid The system to query, or RakNet::UNASSIGNED_SYSTEM_ADDRESS for the sum of all channels.
  143. /// \return Number of bytes on the read buffer.
  144. unsigned GetBufferedBytesToReturn(RakNetGUID guid) const;
  145. /// Enables/disables loopback mode
  146. /// \param[in] true to enable, false to disable
  147. void SetLoopbackMode(bool enabled);
  148. /// Returns true or false, indicating if the loopback mode is enabled
  149. /// \return true if enabled, false otherwise.
  150. bool IsLoopbackMode(void) const;
  151. // --------------------------------------------------------------------------------------------
  152. // Message handling functions
  153. // --------------------------------------------------------------------------------------------
  154. virtual void OnShutdown(void);
  155. virtual void Update(void);
  156. virtual PluginReceiveResult OnReceive(Packet *packet);
  157. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  158. protected:
  159. void OnOpenChannelRequest(Packet *packet);
  160. void OnOpenChannelReply(Packet *packet);
  161. virtual void OnVoiceData(Packet *packet);
  162. void OpenChannel(Packet *packet);
  163. void FreeChannelMemory(RakNetGUID recipient);
  164. void FreeChannelMemory(unsigned index, bool removeIndex);
  165. void WriteOutputToChannel(VoiceChannel *channel, char *dataToWrite);
  166. void SetEncoderParameter(void* enc_state, int vartype, int val);
  167. void SetPreprocessorParameter(void* pre_state, int vartype, int val);
  168. DataStructures::OrderedList<RakNetGUID, VoiceChannel*, VoiceChannelComp> voiceChannels;
  169. int32_t sampleRate;
  170. unsigned bufferSizeBytes;
  171. float *bufferedOutput;
  172. unsigned bufferedOutputCount;
  173. bool zeroBufferedOutput;
  174. int defaultEncoderComplexity;
  175. bool defaultVADState;
  176. bool defaultDENOISEState;
  177. bool defaultVBRState;
  178. bool loopbackMode;
  179. };
  180. } // namespace RakNet
  181. #endif
粤ICP备19079148号