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