RakVoice.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. #include "RakVoice.h"
  11. #include "speex/speex.h"
  12. #include "speex/speex_preprocess.h"
  13. #include "BitStream.h"
  14. #include "PacketPriority.h"
  15. #include "MessageIdentifiers.h"
  16. #include "BitStream.h"
  17. #include "RakPeerInterface.h"
  18. #include <stdlib.h>
  19. #include "GetTime.h"
  20. #ifdef _DEBUG
  21. #include <stdio.h>
  22. #endif
  23. using namespace RakNet;
  24. //#define PRINT_DEBUG_INFO
  25. #define SAMPLESIZE 2
  26. #ifdef PRINT_DEBUG_INFO
  27. #include <stdio.h>
  28. #endif
  29. int RakNet::VoiceChannelComp( const RakNetGUID &key, VoiceChannel * const &data )
  30. {
  31. if (key < data->guid)
  32. return -1;
  33. if (key == data->guid)
  34. return 0;
  35. return 1;
  36. }
  37. RakVoice::RakVoice()
  38. {
  39. bufferedOutput=0;
  40. defaultEncoderComplexity=2;
  41. defaultVADState=true;
  42. defaultDENOISEState=false;
  43. defaultVBRState=false;
  44. loopbackMode=false;
  45. }
  46. RakVoice::~RakVoice()
  47. {
  48. Deinit();
  49. }
  50. void RakVoice::Init(unsigned short sampleRate, unsigned bufferSizeBytes)
  51. {
  52. // Record the parameters
  53. RakAssert(sampleRate==8000 || sampleRate==16000 || sampleRate==32000);
  54. this->sampleRate=sampleRate;
  55. this->bufferSizeBytes=bufferSizeBytes;
  56. bufferedOutputCount=bufferSizeBytes/SAMPLESIZE;
  57. bufferedOutput = (float*) rakMalloc_Ex(sizeof(float)*bufferedOutputCount, _FILE_AND_LINE_);
  58. unsigned i;
  59. for (i=0; i < bufferedOutputCount; i++)
  60. bufferedOutput[i]=0.0f;
  61. zeroBufferedOutput=false;
  62. }
  63. void RakVoice::Deinit(void)
  64. {
  65. // check pointer before free
  66. if (bufferedOutput)
  67. {
  68. rakFree_Ex(bufferedOutput, _FILE_AND_LINE_ );
  69. bufferedOutput = 0;
  70. CloseAllChannels();
  71. }
  72. }
  73. void RakVoice::SetLoopbackMode(bool enabled)
  74. {
  75. if (enabled)
  76. {
  77. Packet p;
  78. RakNet::BitStream out;
  79. out.Write((unsigned char)ID_RAKVOICE_OPEN_CHANNEL_REQUEST);
  80. out.Write((int32_t)sampleRate);
  81. p.data=out.GetData();
  82. p.systemAddress=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
  83. p.guid=UNASSIGNED_RAKNET_GUID;
  84. p.length=out.GetNumberOfBytesUsed();
  85. OpenChannel(&p);
  86. }
  87. else
  88. {
  89. FreeChannelMemory(UNASSIGNED_RAKNET_GUID);
  90. }
  91. loopbackMode=enabled;
  92. }
  93. bool RakVoice::IsLoopbackMode(void) const
  94. {
  95. return loopbackMode;
  96. }
  97. void RakVoice::RequestVoiceChannel(RakNetGUID recipient)
  98. {
  99. // Send a reliable ordered message to the other system to open a voice channel
  100. RakNet::BitStream out;
  101. out.Write((unsigned char)ID_RAKVOICE_OPEN_CHANNEL_REQUEST);
  102. out.Write((int32_t)sampleRate);
  103. SendUnified(&out, HIGH_PRIORITY, RELIABLE_ORDERED,0,recipient,false);
  104. }
  105. void RakVoice::CloseVoiceChannel(RakNetGUID recipient)
  106. {
  107. FreeChannelMemory(recipient);
  108. // Send a message to the remote system telling them to close the channel
  109. RakNet::BitStream out;
  110. out.Write((unsigned char)ID_RAKVOICE_CLOSE_CHANNEL);
  111. SendUnified(&out, HIGH_PRIORITY, RELIABLE_ORDERED,0,recipient,false);
  112. }
  113. void RakVoice::CloseAllChannels(void)
  114. {
  115. RakNet::BitStream out;
  116. out.Write((unsigned char)ID_RAKVOICE_CLOSE_CHANNEL);
  117. // Free the memory for all channels
  118. unsigned index;
  119. for (index=0; index < voiceChannels.Size(); index++)
  120. {
  121. SendUnified(&out, HIGH_PRIORITY, RELIABLE_ORDERED,0,voiceChannels[index]->guid,false);
  122. FreeChannelMemory(index,false);
  123. }
  124. voiceChannels.Clear(false, _FILE_AND_LINE_);
  125. }
  126. bool RakVoice::SendFrame(RakNetGUID recipient, void *inputBuffer)
  127. {
  128. bool objectExists;
  129. unsigned index;
  130. VoiceChannel *channel;
  131. index = voiceChannels.GetIndexFromKey(recipient, &objectExists);
  132. if (objectExists)
  133. {
  134. unsigned totalBufferSize;
  135. unsigned remainingBufferSize;
  136. channel=voiceChannels[index];
  137. totalBufferSize=bufferSizeBytes * FRAME_OUTGOING_BUFFER_COUNT;
  138. if (channel->outgoingWriteIndex >= channel->outgoingReadIndex)
  139. remainingBufferSize=totalBufferSize-(channel->outgoingWriteIndex-channel->outgoingReadIndex);
  140. else
  141. remainingBufferSize=channel->outgoingReadIndex-channel->outgoingWriteIndex;
  142. #ifdef _DEBUG
  143. RakAssert(remainingBufferSize>0 && remainingBufferSize <= totalBufferSize);
  144. // printf("SendFrame: buff=%i writeIndex=%i readIndex=%i\n",remainingBufferSize, channel->outgoingWriteIndex, channel->outgoingReadIndex);
  145. //printf("Writing %i bytes to write offset %i. %i %i.\n", bufferSizeBytes, channel->outgoingWriteIndex, *((char*)inputBuffer+channel->outgoingWriteIndex), *((char*)inputBuffer+channel->outgoingWriteIndex+bufferSizeBytes-1));
  146. #endif
  147. // Copy encoded sound to the outgoing buffer for that channel. This has to be fast, since this function is likely to be called from a locked buffer
  148. // I allocated the buffer to be a size multiple of bufferSizeBytes so don't have to watch for overflow on this line
  149. memcpy(channel->outgoingBuffer + channel->outgoingWriteIndex, inputBuffer, bufferSizeBytes );
  150. #ifdef _DEBUG
  151. RakAssert(channel->outgoingWriteIndex+bufferSizeBytes <= totalBufferSize);
  152. #endif
  153. // Increment the write index, wrapping if needed.
  154. channel->outgoingWriteIndex+=bufferSizeBytes;
  155. #ifdef _DEBUG
  156. // Verify that the write is aligned to the size of outgoingBuffer
  157. RakAssert(channel->outgoingWriteIndex <= totalBufferSize);
  158. #endif
  159. if (channel->outgoingWriteIndex==totalBufferSize)
  160. channel->outgoingWriteIndex=0;
  161. if (bufferSizeBytes >= remainingBufferSize) // Would go past the current read position
  162. {
  163. #ifdef _DEBUG
  164. // This is actually a warning - it means that FRAME_OUTGOING_BUFFER_COUNT wasn't big enough and old data is being overwritten
  165. RakAssert(0);
  166. #endif
  167. // Force the read index up one block
  168. channel->outgoingReadIndex=(channel->outgoingReadIndex+channel->speexOutgoingFrameSampleCount * SAMPLESIZE)%totalBufferSize;
  169. }
  170. return true;
  171. }
  172. return false;
  173. }
  174. bool RakVoice::IsSendingVoiceDataTo(RakNetGUID recipient)
  175. {
  176. bool objectExists;
  177. unsigned index;
  178. index = voiceChannels.GetIndexFromKey(recipient, &objectExists);
  179. // Free the memory for this channel
  180. if (objectExists)
  181. return voiceChannels[index]->isSendingVoiceData;
  182. return false;
  183. }
  184. void RakVoice::ReceiveFrame(void *outputBuffer)
  185. {
  186. short *out = (short*)outputBuffer;
  187. unsigned i;
  188. // Convert the floats to final 16-bits output
  189. for (i=0; i < bufferSizeBytes / SAMPLESIZE; i++)
  190. {
  191. if (bufferedOutput[i]>32767.0f)
  192. out[i]=32767;
  193. else if (bufferedOutput[i]<-32768.0f)
  194. out[i]=-32768;
  195. else
  196. out[i]=(short)bufferedOutput[i];
  197. }
  198. // Done with this block. Zero all the values in Update
  199. zeroBufferedOutput=true;
  200. }
  201. int RakVoice::GetSampleRate(void) const
  202. {
  203. return sampleRate;
  204. }
  205. int RakVoice::GetBufferSizeBytes(void) const
  206. {
  207. return bufferSizeBytes;
  208. }
  209. bool RakVoice::IsInitialized(void) const
  210. {
  211. // Use bufferedOutput to tell if the object was not initialized
  212. return (bufferedOutput!=0);
  213. }
  214. RakPeerInterface* RakVoice::GetRakPeerInterface(void) const
  215. {
  216. return rakPeerInterface;
  217. }
  218. unsigned RakVoice::GetBufferedBytesToSend(RakNetGUID guid) const
  219. {
  220. bool objectExists;
  221. VoiceChannel *channel;
  222. unsigned totalBufferSize=bufferSizeBytes * FRAME_OUTGOING_BUFFER_COUNT;
  223. if (guid!=UNASSIGNED_RAKNET_GUID)
  224. {
  225. unsigned index = voiceChannels.GetIndexFromKey(guid, &objectExists);
  226. channel = voiceChannels[index];
  227. if (objectExists)
  228. {
  229. if (channel->outgoingWriteIndex>=channel->outgoingReadIndex)
  230. return channel->outgoingWriteIndex-channel->outgoingReadIndex;
  231. else
  232. return channel->outgoingWriteIndex + (totalBufferSize-channel->outgoingReadIndex);
  233. }
  234. }
  235. else
  236. {
  237. unsigned total=0;
  238. for (unsigned i=0; i < voiceChannels.Size(); i++)
  239. {
  240. channel=voiceChannels[i];
  241. if (channel->outgoingWriteIndex>=channel->outgoingReadIndex)
  242. total+=channel->outgoingWriteIndex-channel->outgoingReadIndex;
  243. else
  244. total+=channel->outgoingWriteIndex + (totalBufferSize-channel->outgoingReadIndex);
  245. }
  246. return total;
  247. }
  248. return 0;
  249. }
  250. unsigned RakVoice::GetBufferedBytesToReturn(RakNetGUID guid) const
  251. {
  252. bool objectExists;
  253. VoiceChannel *channel;
  254. unsigned totalBufferSize=bufferSizeBytes * FRAME_OUTGOING_BUFFER_COUNT;
  255. if (guid!=UNASSIGNED_RAKNET_GUID)
  256. {
  257. unsigned index = voiceChannels.GetIndexFromKey(guid, &objectExists);
  258. channel = voiceChannels[index];
  259. if (objectExists)
  260. {
  261. if (channel->incomingReadIndex <= channel->incomingWriteIndex)
  262. return channel->incomingWriteIndex-channel->incomingReadIndex;
  263. else
  264. return totalBufferSize-channel->incomingReadIndex+channel->incomingWriteIndex;
  265. }
  266. }
  267. else
  268. {
  269. unsigned total=0;
  270. for (unsigned i=0; i < voiceChannels.Size(); i++)
  271. {
  272. channel=voiceChannels[i];
  273. if (channel->incomingReadIndex <= channel->incomingWriteIndex)
  274. total+=channel->incomingWriteIndex-channel->incomingReadIndex;
  275. else
  276. total+=totalBufferSize-channel->incomingReadIndex+channel->incomingWriteIndex;
  277. }
  278. return total;
  279. }
  280. return 0;
  281. }
  282. void RakVoice::OnShutdown(void)
  283. {
  284. CloseAllChannels();
  285. }
  286. void RakVoice::Update(void)
  287. {
  288. unsigned i,j, bytesAvailable, speexFramesAvailable, speexBlockSize;
  289. unsigned bytesWaitingToReturn;
  290. int bytesWritten;
  291. VoiceChannel *channel;
  292. char *inputBuffer;
  293. char tempOutput[2048];
  294. // 1 byte for ID, and 2 bytes(short) for Message number
  295. static const int headerSize=sizeof(unsigned char) + sizeof(unsigned short);
  296. // First byte is ID for RakNet
  297. tempOutput[0]=ID_RAKVOICE_DATA;
  298. RakNet::TimeMS currentTime = RakNet::GetTimeMS();
  299. // Size of VoiceChannel::incomingBuffer and VoiceChannel::outgoingBuffer arrays
  300. unsigned totalBufferSize=bufferSizeBytes * FRAME_OUTGOING_BUFFER_COUNT;
  301. // Allow all channels to write, and set the output to zero in preparation
  302. if (zeroBufferedOutput)
  303. {
  304. for (i=0; i < bufferedOutputCount; i++)
  305. bufferedOutput[i]=0.0f;
  306. for (i=0; i < voiceChannels.Size(); i++)
  307. voiceChannels[i]->copiedOutgoingBufferToBufferedOutput=false;
  308. zeroBufferedOutput=false;
  309. }
  310. // For each channel
  311. for (i=0; i < voiceChannels.Size(); i++)
  312. {
  313. channel=voiceChannels[i];
  314. if (currentTime - channel->lastSend > 50) // Throttle to 20 sends a second
  315. {
  316. channel->isSendingVoiceData=false;
  317. // Circular buffer so I have to do this to count how many bytes are available
  318. if (channel->outgoingWriteIndex>=channel->outgoingReadIndex)
  319. bytesAvailable=channel->outgoingWriteIndex-channel->outgoingReadIndex;
  320. else
  321. bytesAvailable=channel->outgoingWriteIndex + (totalBufferSize-channel->outgoingReadIndex);
  322. // Speex returns how many frames it encodes per block. Each frame is of byte length sampleSize.
  323. speexBlockSize = channel->speexOutgoingFrameSampleCount * SAMPLESIZE;
  324. #ifdef PRINT_DEBUG_INFO
  325. static int lastPrint=0;
  326. if (i==0 && currentTime-lastPrint > 2000)
  327. {
  328. lastPrint=currentTime;
  329. unsigned bytesWaitingToReturn;
  330. if (channel->incomingReadIndex <= channel->incomingWriteIndex)
  331. bytesWaitingToReturn=channel->incomingWriteIndex-channel->incomingReadIndex;
  332. else
  333. bytesWaitingToReturn=totalBufferSize-channel->incomingReadIndex+channel->incomingWriteIndex;
  334. printf("%i bytes to send. incomingMessageNumber=%i. bytesWaitingToReturn=%i.\n", bytesAvailable, channel->incomingMessageNumber, bytesWaitingToReturn );
  335. }
  336. #endif
  337. #ifdef _TEST_LOOPBACK
  338. /*
  339. if (bufferSizeBytes<bytesAvailable)
  340. {
  341. printf("Update: bytesAvailable=%i writeIndex=%i readIndex=%i\n",bytesAvailable, channel->outgoingWriteIndex, channel->outgoingReadIndex);
  342. memcpy(channel->incomingBuffer + channel->incomingWriteIndex, channel->outgoingBuffer+channel->outgoingReadIndex, bufferSizeBytes);
  343. channel->incomingWriteIndex=(channel->incomingWriteIndex+bufferSizeBytes) % totalBufferSize;
  344. channel->outgoingReadIndex=(channel->outgoingReadIndex+bufferSizeBytes) % totalBufferSize;
  345. }
  346. return;
  347. */
  348. #endif
  349. // Find out how many frames we can read out of the buffer for speex to encode and send these out.
  350. speexFramesAvailable = bytesAvailable / speexBlockSize;
  351. // Encode all available frames and send them unreliable sequenced
  352. if (speexFramesAvailable > 0)
  353. {
  354. SpeexBits speexBits;
  355. speex_bits_init(&speexBits);
  356. while (speexFramesAvailable-- > 0)
  357. {
  358. speex_bits_reset(&speexBits);
  359. // If the input data would wrap around the buffer, copy it to another buffer first
  360. if (channel->outgoingReadIndex + speexBlockSize >= totalBufferSize)
  361. {
  362. #ifdef _DEBUG
  363. RakAssert(speexBlockSize < 2048-1);
  364. #endif
  365. unsigned t;
  366. for (t=0; t < speexBlockSize; t++)
  367. tempOutput[t+headerSize]=channel->outgoingBuffer[t%totalBufferSize];
  368. inputBuffer=tempOutput+headerSize;
  369. }
  370. else
  371. inputBuffer=channel->outgoingBuffer+channel->outgoingReadIndex;
  372. #ifdef _DEBUG
  373. /*
  374. printf("In: ");
  375. if (shortSampleType)
  376. {
  377. short *blah = (short*) inputBuffer;
  378. for (int p=0; p < 5; p++)
  379. {
  380. printf("%.i ", blah[p]);
  381. }
  382. }
  383. else
  384. {
  385. float *blah = (float*) inputBuffer;
  386. for (int p=0; p < 5; p++)
  387. {
  388. printf("%.3f ", blah[p]);
  389. }
  390. }
  391. printf("\n");
  392. */
  393. #endif
  394. int is_speech=1;
  395. // Run preprocessor if required
  396. if (defaultDENOISEState||defaultVADState){
  397. is_speech=speex_preprocess((SpeexPreprocessState*)channel->pre_state,(spx_int16_t*) inputBuffer, NULL );
  398. }
  399. if ((is_speech)||(!defaultVADState)){
  400. is_speech = speex_encode_int(channel->enc_state, (spx_int16_t*) inputBuffer, &speexBits);
  401. }
  402. channel->outgoingReadIndex=(channel->outgoingReadIndex+speexBlockSize)%totalBufferSize;
  403. // If no speech detected, don't send this frame
  404. if ((!is_speech)&&(defaultVADState)){
  405. continue;
  406. }
  407. channel->isSendingVoiceData=true;
  408. #ifdef _DEBUG
  409. // printf("Update: bytesAvailable=%i writeIndex=%i readIndex=%i\n",bytesAvailable, channel->outgoingWriteIndex, channel->outgoingReadIndex);
  410. #endif
  411. bytesWritten = speex_bits_write(&speexBits, tempOutput+headerSize, 2048-headerSize);
  412. #ifdef _DEBUG
  413. // If this assert hits then you need to increase the size of the temp buffer, but this is really a bug because
  414. // voice packets should never be bigger than a few hundred bytes.
  415. RakAssert(bytesWritten!=2048-headerSize);
  416. #endif
  417. // static int bytesSent=0;
  418. // bytesSent+= bytesWritten+headerSize;
  419. // printf("bytesSent=%i\n", bytesSent);
  420. #ifdef PRINT_DEBUG_INFO
  421. static int voicePacketsSent=0;
  422. printf("%i ", voicePacketsSent++);
  423. #endif
  424. // at +1, because the first byte in the buffer has the ID for RakNet.
  425. memcpy(tempOutput+1, &channel->outgoingMessageNumber, sizeof(unsigned short));
  426. channel->outgoingMessageNumber++;
  427. RakNet::BitStream tempOutputBs((unsigned char*) tempOutput,bytesWritten+headerSize,false);
  428. SendUnified(&tempOutputBs, HIGH_PRIORITY, UNRELIABLE,0,channel->guid,false);
  429. if (loopbackMode)
  430. {
  431. Packet p;
  432. p.length=bytesWritten+1;
  433. p.data=(unsigned char*)tempOutput;
  434. p.guid=channel->guid;
  435. p.systemAddress=rakPeerInterface->GetSystemAddressFromGuid(p.guid);
  436. OnVoiceData(&p);
  437. }
  438. }
  439. speex_bits_destroy(&speexBits);
  440. channel->lastSend=currentTime;
  441. }
  442. }
  443. // As sound buffer blocks fill up, I add their values to RakVoice::bufferedOutput . Then when the user calls ReceiveFrame they get that value, already
  444. // processed. This is necessary because that function needs to run as fast as possible so I remove all processing there that I can. Otherwise the sound
  445. // plays back distorted and popping
  446. if (channel->copiedOutgoingBufferToBufferedOutput==false)
  447. {
  448. if (channel->incomingReadIndex <= channel->incomingWriteIndex)
  449. bytesWaitingToReturn=channel->incomingWriteIndex-channel->incomingReadIndex;
  450. else
  451. bytesWaitingToReturn=totalBufferSize-channel->incomingReadIndex+channel->incomingWriteIndex;
  452. if (bytesWaitingToReturn==0)
  453. {
  454. channel->bufferOutput=true;
  455. }
  456. else if (channel->bufferOutput==false || bytesWaitingToReturn > bufferSizeBytes*2)
  457. {
  458. // Block running this again until the user calls ReceiveFrame since every call to ReceiveFrame only gets zero or one output blocks from
  459. // each channel
  460. channel->copiedOutgoingBufferToBufferedOutput=true;
  461. // Stop buffering output. We won't start buffering again until there isn't enough data to read.
  462. channel->bufferOutput=false;
  463. // Cap to the size of the output buffer. But we do write less if less is available, with the rest silence
  464. if (bytesWaitingToReturn > bufferSizeBytes)
  465. {
  466. bytesWaitingToReturn=bufferSizeBytes;
  467. }
  468. else
  469. {
  470. // Align the write index so when we increment the partial block read (which is always aligned) it computes out to 0 bytes waiting
  471. channel->incomingWriteIndex=channel->incomingReadIndex+bufferSizeBytes;
  472. if (channel->incomingWriteIndex==totalBufferSize)
  473. channel->incomingWriteIndex=0;
  474. }
  475. short *in = (short *) (channel->incomingBuffer+channel->incomingReadIndex);
  476. for (j=0; j < bytesWaitingToReturn / SAMPLESIZE; j++)
  477. {
  478. // Write short to float so if the range goes over the range of a float we can still add and subtract the correct final value.
  479. // It will be clamped at the end
  480. bufferedOutput[j]+=in[j%(totalBufferSize/SAMPLESIZE)];
  481. }
  482. // Update the read index. Always update by bufferSizeBytes, not bytesWaitingToReturn.
  483. // if bytesWaitingToReturn < bufferSizeBytes then the rest is silence since this means the buffer ran out or we stopped sending.
  484. channel->incomingReadIndex+=bufferSizeBytes;
  485. if (channel->incomingReadIndex==totalBufferSize)
  486. channel->incomingReadIndex=0;
  487. // printf("%f %f\n", channel->incomingReadIndex/(float)bufferSizeBytes, channel->incomingWriteIndex/(float)bufferSizeBytes);
  488. }
  489. }
  490. }
  491. }
  492. PluginReceiveResult RakVoice::OnReceive(Packet *packet)
  493. {
  494. RakAssert(packet);
  495. switch (packet->data[0])
  496. {
  497. case ID_RAKVOICE_OPEN_CHANNEL_REQUEST:
  498. OnOpenChannelRequest(packet);
  499. break;
  500. case ID_RAKVOICE_OPEN_CHANNEL_REPLY:
  501. OnOpenChannelReply(packet);
  502. break;
  503. case ID_RAKVOICE_CLOSE_CHANNEL:
  504. FreeChannelMemory(packet->guid);
  505. break;
  506. case ID_RAKVOICE_DATA:
  507. OnVoiceData(packet);
  508. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  509. }
  510. return RR_CONTINUE_PROCESSING;
  511. }
  512. void RakVoice::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
  513. {
  514. (void)systemAddress;
  515. if (lostConnectionReason==LCR_CLOSED_BY_USER)
  516. CloseVoiceChannel(rakNetGUID);
  517. else
  518. FreeChannelMemory(rakNetGUID);
  519. }
  520. void RakVoice::OnOpenChannelRequest(Packet *packet)
  521. {
  522. if (voiceChannels.HasData(packet->guid))
  523. return;
  524. // If the system is not initialized, just return
  525. if (bufferedOutput==0)
  526. return;
  527. OpenChannel(packet);
  528. RakNet::BitStream out;
  529. out.Write((unsigned char)ID_RAKVOICE_OPEN_CHANNEL_REPLY);
  530. out.Write((int32_t)sampleRate);
  531. SendUnified(&out, HIGH_PRIORITY, RELIABLE_ORDERED,0,packet->systemAddress,false);
  532. }
  533. void RakVoice::OnOpenChannelReply(Packet *packet)
  534. {
  535. if (voiceChannels.HasData(packet->guid))
  536. return;
  537. OpenChannel(packet);
  538. }
  539. void RakVoice::OpenChannel(Packet *packet)
  540. {
  541. RakNet::BitStream in(packet->data, packet->length, false);
  542. in.IgnoreBits(8);
  543. FreeChannelMemory(packet->guid);
  544. VoiceChannel *channel=RakNet::OP_NEW<VoiceChannel>( _FILE_AND_LINE_ );
  545. channel->guid=packet->guid;
  546. channel->isSendingVoiceData=false;
  547. int sampleRate;
  548. in.Read(sampleRate);
  549. channel->remoteSampleRate=sampleRate;
  550. if (channel->remoteSampleRate!=8000 && channel->remoteSampleRate!=16000 && channel->remoteSampleRate!=32000)
  551. {
  552. #ifdef _DEBUG
  553. RakAssert(0);
  554. #endif
  555. RakNet::OP_DELETE(channel, _FILE_AND_LINE_);
  556. return;
  557. }
  558. if (sampleRate==8000)
  559. channel->enc_state=speex_encoder_init(&speex_nb_mode);
  560. else if (sampleRate==16000)
  561. channel->enc_state=speex_encoder_init(&speex_wb_mode);
  562. else // 32000
  563. channel->enc_state=speex_encoder_init(&speex_uwb_mode);
  564. if (channel->remoteSampleRate==8000)
  565. channel->dec_state=speex_decoder_init(&speex_nb_mode);
  566. else if (channel->remoteSampleRate==16000)
  567. channel->dec_state=speex_decoder_init(&speex_wb_mode);
  568. else // 32000
  569. channel->dec_state=speex_decoder_init(&speex_uwb_mode);
  570. // make sure encoder and decoder are created
  571. RakAssert((channel->enc_state)&&(channel->dec_state));
  572. int ret;
  573. ret=speex_encoder_ctl(channel->enc_state, SPEEX_GET_FRAME_SIZE, &channel->speexOutgoingFrameSampleCount);
  574. RakAssert(ret==0);
  575. channel->outgoingBuffer = (char*) rakMalloc_Ex(bufferSizeBytes * FRAME_OUTGOING_BUFFER_COUNT, _FILE_AND_LINE_);
  576. channel->outgoingReadIndex=0;
  577. channel->outgoingWriteIndex=0;
  578. channel->bufferOutput=true;
  579. channel->outgoingMessageNumber=0;
  580. channel->copiedOutgoingBufferToBufferedOutput=false;
  581. ret=speex_decoder_ctl(channel->dec_state, SPEEX_GET_FRAME_SIZE, &channel->speexIncomingFrameSampleCount);
  582. RakAssert(ret==0);
  583. channel->incomingBuffer = (char*) rakMalloc_Ex(bufferSizeBytes * FRAME_INCOMING_BUFFER_COUNT, _FILE_AND_LINE_);
  584. channel->incomingReadIndex=0;
  585. channel->incomingWriteIndex=0;
  586. channel->lastSend=0;
  587. channel->incomingMessageNumber=0;
  588. // Initialize preprocessor
  589. channel->pre_state = speex_preprocess_state_init(channel->speexOutgoingFrameSampleCount, sampleRate);
  590. RakAssert(channel->pre_state);
  591. // Set encoder default parameters
  592. SetEncoderParameter(channel->enc_state, SPEEX_SET_VBR, (defaultVBRState) ? 1 : 0 );
  593. SetEncoderParameter(channel->enc_state, SPEEX_SET_COMPLEXITY, defaultEncoderComplexity);
  594. // Set preprocessor default parameters
  595. SetPreprocessorParameter(channel->pre_state, SPEEX_PREPROCESS_SET_DENOISE, (defaultDENOISEState) ? 1 : 2);
  596. SetPreprocessorParameter(channel->pre_state, SPEEX_PREPROCESS_SET_VAD, (defaultVADState) ? 1 : 2);
  597. voiceChannels.Insert(packet->guid, channel, true, _FILE_AND_LINE_);
  598. }
  599. void RakVoice::SetEncoderParameter(void* enc_state, int vartype, int val)
  600. {
  601. if (enc_state){
  602. // Set parameter for just one encoder
  603. int ret = speex_encoder_ctl(enc_state, vartype, &val);
  604. RakAssert(ret==0);
  605. } else {
  606. // Set parameter for all encoders
  607. for (unsigned int index=0; index < voiceChannels.Size(); index++)
  608. {
  609. int ret = speex_encoder_ctl(voiceChannels[index]->enc_state, vartype, &val);
  610. RakAssert(ret==0);
  611. }
  612. }
  613. }
  614. void RakVoice::SetPreprocessorParameter(void* pre_state, int vartype, int val)
  615. {
  616. if (pre_state){
  617. // Set parameter for just one preprocessor
  618. int ret = speex_preprocess_ctl((SpeexPreprocessState*)pre_state, vartype, &val);
  619. RakAssert(ret==0);
  620. } else {
  621. // Set parameter for all decoders
  622. for (unsigned int index=0; index < voiceChannels.Size(); index++)
  623. {
  624. int ret = speex_preprocess_ctl((SpeexPreprocessState*)voiceChannels[index]->pre_state, vartype, &val);
  625. RakAssert(ret==0);
  626. }
  627. }
  628. }
  629. void RakVoice::SetEncoderComplexity(int complexity)
  630. {
  631. RakAssert((complexity>=0)&&(complexity<=10));
  632. SetEncoderParameter(NULL, SPEEX_SET_COMPLEXITY, complexity);
  633. defaultEncoderComplexity = complexity;
  634. }
  635. void RakVoice::SetVAD(bool enable)
  636. {
  637. SetPreprocessorParameter(NULL, SPEEX_PREPROCESS_SET_VAD, (enable)? 1 : 2);
  638. defaultVADState = enable;
  639. }
  640. void RakVoice::SetNoiseFilter(bool enable)
  641. {
  642. SetPreprocessorParameter(NULL, SPEEX_PREPROCESS_SET_DENOISE, (enable) ? 1 : 2);
  643. defaultDENOISEState = enable;
  644. }
  645. void RakVoice::SetVBR(bool enable)
  646. {
  647. SetEncoderParameter(NULL, SPEEX_SET_VBR, (enable) ? 1 : 0);
  648. defaultVBRState = enable;
  649. }
  650. int RakVoice::GetEncoderComplexity(void)
  651. {
  652. return defaultEncoderComplexity;
  653. }
  654. bool RakVoice::IsVADActive(void)
  655. {
  656. return defaultVADState;
  657. }
  658. bool RakVoice::IsNoiseFilterActive()
  659. {
  660. return defaultDENOISEState;
  661. }
  662. bool RakVoice::IsVBRActive()
  663. {
  664. return defaultVBRState;
  665. }
  666. void RakVoice::FreeChannelMemory(RakNetGUID recipient)
  667. {
  668. bool objectExists;
  669. unsigned index;
  670. index = voiceChannels.GetIndexFromKey(recipient, &objectExists);
  671. // Free the memory for this channel
  672. if (objectExists)
  673. {
  674. FreeChannelMemory(index, true);
  675. }
  676. }
  677. void RakVoice::FreeChannelMemory(unsigned index, bool removeIndex)
  678. {
  679. VoiceChannel *channel;
  680. channel=voiceChannels[index];
  681. speex_encoder_destroy(channel->enc_state);
  682. speex_decoder_destroy(channel->dec_state);
  683. speex_preprocess_state_destroy((SpeexPreprocessState*)channel->pre_state);
  684. rakFree_Ex(channel->incomingBuffer, _FILE_AND_LINE_ );
  685. rakFree_Ex(channel->outgoingBuffer, _FILE_AND_LINE_ );
  686. RakNet::OP_DELETE(channel, _FILE_AND_LINE_);
  687. if (removeIndex)
  688. voiceChannels.RemoveAtIndex(index);
  689. }
  690. void RakVoice::OnVoiceData(Packet *packet)
  691. {
  692. bool objectExists;
  693. unsigned index;
  694. unsigned short packetMessageNumber, messagesSkipped;
  695. VoiceChannel *channel;
  696. char tempOutput[2048];
  697. unsigned int i;
  698. // 1 byte for ID, 2 bytes(short) for message number
  699. static const int headerSize=sizeof(unsigned char) + sizeof(unsigned short);
  700. index = voiceChannels.GetIndexFromKey(packet->guid, &objectExists);
  701. if (objectExists)
  702. {
  703. SpeexBits speexBits;
  704. speex_bits_init(&speexBits);
  705. channel=voiceChannels[index];
  706. memcpy(&packetMessageNumber, packet->data+1, sizeof(unsigned short));
  707. // Intentional overflow
  708. messagesSkipped=packetMessageNumber-channel->incomingMessageNumber;
  709. if (messagesSkipped > ((unsigned short)-1)/2)
  710. {
  711. #ifdef PRINT_DEBUG_INFO
  712. printf("--- UNDERFLOW ---\n");
  713. #endif
  714. // Underflow, just ignore it
  715. return;
  716. }
  717. #ifdef PRINT_DEBUG_INFO
  718. if (messagesSkipped>0)
  719. printf("%i messages skipped\n", messagesSkipped);
  720. #endif
  721. // Don't do more than 100 ms of messages skipped. Discard the rest.
  722. int maxSkip = (int)(100.0f / (float) sampleRate);
  723. for (i=0; i < (unsigned) messagesSkipped && i < (unsigned) maxSkip; i++)
  724. {
  725. speex_decode_int(channel->dec_state, 0, (spx_int16_t*)tempOutput);
  726. // Write to buffer a 'message skipped' interpolation
  727. WriteOutputToChannel(channel, tempOutput);
  728. }
  729. channel->incomingMessageNumber=packetMessageNumber+1;
  730. // Write to incomingBuffer the decoded data
  731. speex_bits_read_from(&speexBits, (char*)(packet->data+headerSize), packet->length-headerSize);
  732. speex_decode_int(channel->dec_state, &speexBits, (spx_int16_t*)tempOutput);
  733. #ifdef _DEBUG
  734. {
  735. /*
  736. printf("Out: ");
  737. if (channel->remoteIsShortSampleType)
  738. {
  739. short *blah = (short*) tempOutput;
  740. for (int p=0; p < 5; p++)
  741. {
  742. printf("%.i ", blah[p]);
  743. }
  744. }
  745. else
  746. {
  747. float *blah = (float*) tempOutput;
  748. for (int p=0; p < 5; p++)
  749. {
  750. printf("%.3f ", blah[p]);
  751. }
  752. }
  753. printf("\n");
  754. */
  755. }
  756. #endif
  757. // Write to buffer
  758. WriteOutputToChannel(channel, tempOutput);
  759. speex_bits_destroy(&speexBits);
  760. }
  761. }
  762. void RakVoice::WriteOutputToChannel(VoiceChannel *channel, char *dataToWrite)
  763. {
  764. unsigned totalBufferSize;
  765. unsigned remainingBufferSize;
  766. unsigned speexBlockSize;
  767. totalBufferSize=bufferSizeBytes * FRAME_INCOMING_BUFFER_COUNT;
  768. if (channel->incomingWriteIndex >= channel->incomingReadIndex)
  769. remainingBufferSize=totalBufferSize-(channel->incomingWriteIndex-channel->incomingReadIndex);
  770. else
  771. remainingBufferSize=channel->incomingReadIndex-channel->incomingWriteIndex;
  772. // Speex returns how many frames it encodes per block. Each frame is of byte length sampleSize.
  773. speexBlockSize = channel->speexIncomingFrameSampleCount * SAMPLESIZE;
  774. if (channel->incomingWriteIndex+speexBlockSize <= totalBufferSize)
  775. {
  776. memcpy(channel->incomingBuffer + channel->incomingWriteIndex, dataToWrite, speexBlockSize);
  777. }
  778. else
  779. {
  780. memcpy(channel->incomingBuffer + channel->incomingWriteIndex, dataToWrite, totalBufferSize-channel->incomingWriteIndex);
  781. memcpy(channel->incomingBuffer, dataToWrite, speexBlockSize-(totalBufferSize-channel->incomingWriteIndex));
  782. }
  783. channel->incomingWriteIndex=(channel->incomingWriteIndex+speexBlockSize) % totalBufferSize;
  784. #ifdef _DEBUG
  785. //printf("WriteOutputToChannel: buff=%i writeIndex=%i readIndex=%i\n",remainingBufferSize, channel->incomingWriteIndex, channel->incomingReadIndex);
  786. #endif
  787. if (bufferSizeBytes >= remainingBufferSize) // Would go past the current read position
  788. {
  789. #ifdef _DEBUG
  790. // This is actually a warning - it means that FRAME_INCOMING_BUFFER_COUNT wasn't big enough and old data is being overwritten
  791. RakAssert(0);
  792. #endif
  793. // Force the read index up one block
  794. channel->incomingReadIndex+=bufferSizeBytes;
  795. if (channel->incomingReadIndex==totalBufferSize)
  796. channel->incomingReadIndex=0;
  797. }
  798. }
粤ICP备19079148号