main.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #define INTERACTIVE
  11. #if defined(INTERACTIVE)
  12. #include "Kbhit.h"
  13. #endif
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <memory.h>
  17. #include "RakPeerInterface.h"
  18. #include "MessageIdentifiers.h"
  19. #include "RakSleep.h"
  20. #include "RakVoice.h"
  21. #include "RakNetStatistics.h"
  22. #include "GetTime.h"
  23. #include "RakAssert.h"
  24. #include "Gets.h"
  25. #include "DSoundVoiceAdapter.h"
  26. // Reads and writes per second of the sound data
  27. // Speex only supports these 3 values
  28. #define SAMPLE_RATE (8000)
  29. //#define SAMPLE_RATE (16000)
  30. //#define SAMPLE_RATE (32000)
  31. #define FRAMES_PER_BUFFER (2048 / (32000 / SAMPLE_RATE))
  32. // define sample type. Only short(16 bits sound) is supported at the moment.
  33. typedef short SAMPLE;
  34. RakNet::RakPeerInterface *rakPeer=NULL;
  35. RakNet::RakVoice rakVoice;
  36. struct myStat{
  37. unsigned int time;
  38. unsigned int bitsRec;
  39. unsigned int bitsSent;
  40. };
  41. // Keeps a record of the last 20 calls, to give a faster response to traffic change
  42. void LogStats(){
  43. const int numStats = 20;
  44. static myStat data[numStats];
  45. for(int i=0; i<=numStats-2; i++){
  46. data[i] = data[i+1];
  47. }
  48. RakNet::RakNetStatistics *rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
  49. unsigned int currTime = RakNet::GetTimeMS();
  50. data[numStats-1].time = currTime;
  51. data[numStats-1].bitsSent = BYTES_TO_BITS(rss->runningTotal[RakNet::USER_MESSAGE_BYTES_SENT]);
  52. data[numStats-1].bitsRec = BYTES_TO_BITS(rss->runningTotal[RakNet::USER_MESSAGE_BYTES_RECEIVED_PROCESSED]);
  53. float totalTime = (data[numStats-1].time - data[0].time) / 1000.f ;
  54. unsigned int totalBitsSent = data[numStats-1].bitsSent - data[0].bitsSent;
  55. unsigned int totalBitsRec = data[numStats-1].bitsRec - data[0].bitsRec;
  56. float bpsSent = totalBitsSent/totalTime;
  57. float bpsRec = totalBitsRec/totalTime;
  58. float avgBpsSent = rss->valueOverLastSecond[RakNet::USER_MESSAGE_BYTES_SENT];
  59. float avgBpsRec = rss->valueOverLastSecond[RakNet::USER_MESSAGE_BYTES_RECEIVED_PROCESSED];
  60. printf("avgKbpsSent=%02.1f avgKbpsRec=%02.1f kbpsSent=%02.1f kbpsRec=%02.1f \r", avgBpsSent/1000, avgBpsRec/1000, bpsSent/1000 , bpsRec/1000, rakVoice.GetBufferedBytesToReturn(RakNet::UNASSIGNED_RAKNET_GUID));
  61. //printf("MsgBuf=%6i SndBuf=%10i RcvBuf=%10i \r", rakVoice.GetRakPeerInterface()->GetStatistics(RakNet::UNASSIGNED_SYSTEM_ADDRESS)->messageSendBuffer[HIGH_PRIORITY], rakVoice.GetBufferedBytesToSend(RakNet::UNASSIGNED_SYSTEM_ADDRESS), rakVoice.GetBufferedBytesToReturn(RakNet::UNASSIGNED_SYSTEM_ADDRESS));
  62. }
  63. // Prints the current encoder parameters
  64. void PrintParameters(void)
  65. {
  66. printf("\nComplexity=%3d Noise filter=%3s VAD=%3s VBR=%3s\n"
  67. ,rakVoice.GetEncoderComplexity()
  68. ,(rakVoice.IsNoiseFilterActive()) ? "ON" : "OFF"
  69. ,(rakVoice.IsVADActive()) ? "ON" : "OFF"
  70. ,(rakVoice.IsVBRActive()) ? "ON" : "OFF");
  71. }
  72. //
  73. // Utility function to obtain a Console Window Handle (HWND), as explained in:
  74. // http://support.microsoft.com/kb/124103
  75. //
  76. HWND GetConsoleHwnd(void)
  77. {
  78. #define MY_BUFSIZE 1024 // Buffer size for console window titles.
  79. HWND hwndFound; // This is what is returned to the caller.
  80. TCHAR pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
  81. // WindowTitle.
  82. TCHAR pszOldWindowTitle[MY_BUFSIZE]; // Contains original
  83. // WindowTitle.
  84. // Fetch current window title.
  85. GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  86. // Format a "unique" NewWindowTitle.
  87. wsprintf(pszNewWindowTitle,TEXT("%d/%d"),
  88. GetTickCount(),
  89. GetCurrentProcessId());
  90. // Change current window title.
  91. SetConsoleTitle(pszNewWindowTitle);
  92. // Ensure window title has been updated.
  93. Sleep(40);
  94. // Look for NewWindowTitle.
  95. hwndFound=FindWindow(NULL, pszNewWindowTitle);
  96. // Restore original window title.
  97. SetConsoleTitle(pszOldWindowTitle);
  98. return(hwndFound);
  99. }
  100. int main(void)
  101. {
  102. printf("A sample on how to use RakVoice together with DirectSound.\n");
  103. printf("You need a microphone for this sample.\n");
  104. printf("RakVoice relies on Speex for voice encoding and decoding.\n");
  105. printf("See DependentExtensions/RakVoice/speex-1.2beta3 for speex projects.\n");
  106. printf("For windows, I had to define HAVE_CONFIG_H, include win32/config.h,\n");
  107. printf("and include the files under libspeex, except those that start with test.\n");
  108. printf("Difficulty: Advanced\n\n");
  109. bool mute=false;
  110. bool quit;
  111. char ch;
  112. char port[256];
  113. rakPeer = RakNet::RakPeerInterface::GetInstance();
  114. #if defined(INTERACTIVE)
  115. printf("Enter local port: ");
  116. Gets(port, sizeof(port));
  117. if (port[0]==0)
  118. #endif
  119. strcpy(port, "60000");
  120. RakNet::SocketDescriptor socketDescriptor(atoi(port),0);
  121. rakPeer->Startup(4, &socketDescriptor, 1);
  122. rakPeer->SetMaximumIncomingConnections(4);
  123. rakPeer->AttachPlugin(&rakVoice);
  124. rakVoice.Init(SAMPLE_RATE, FRAMES_PER_BUFFER*sizeof(SAMPLE));
  125. // Initialize our connection with DirectSound
  126. if (!RakNet::DSoundVoiceAdapter::Instance()->SetupAdapter(&rakVoice, GetConsoleHwnd(), DSSCL_EXCLUSIVE))
  127. {
  128. printf("An error occurred while initializing DirectSound.\n");
  129. exit(-1);
  130. }
  131. RakNet::Packet *p;
  132. quit=false;
  133. #if defined(INTERACTIVE)
  134. printf("(Q)uit. (C)onnect. (D)isconnect. (M)ute. ' ' for stats.\n");
  135. printf("(+/-)encoder complexity. (N)oise filter on/off. (V)AD on/off. (B)vbr on/off.\n");
  136. #else
  137. rakPeer->Connect("1.1.1.1", 60000, 0,0);
  138. #endif
  139. PrintParameters();
  140. while (!quit)
  141. {
  142. #if defined(INTERACTIVE)
  143. if (kbhit())
  144. {
  145. ch=getch();
  146. if (ch=='+'){
  147. // Increase encoder complexity
  148. int v = rakVoice.GetEncoderComplexity();
  149. if (v<10) rakVoice.SetEncoderComplexity(v+1);
  150. PrintParameters();
  151. }
  152. else if (ch=='-'){
  153. // Decrease encoder complexity
  154. int v = rakVoice.GetEncoderComplexity();
  155. if (v>0) rakVoice.SetEncoderComplexity(v-1);
  156. PrintParameters();
  157. }
  158. else if (ch=='n'){
  159. // Turn on/off noise filter
  160. rakVoice.SetNoiseFilter(!rakVoice.IsNoiseFilterActive());
  161. PrintParameters();
  162. }
  163. else if (ch=='v') {
  164. // Turn on/off Voice detection
  165. rakVoice.SetVAD(!rakVoice.IsVADActive());
  166. PrintParameters();
  167. }
  168. else if (ch=='b') {
  169. // Turn on/off VBR
  170. rakVoice.SetVBR(!rakVoice.IsVBRActive());
  171. PrintParameters();
  172. }
  173. else if (ch=='y')
  174. {
  175. quit=true;
  176. }
  177. else if (ch=='c')
  178. {
  179. char ip[256];
  180. printf("\nEnter IP of remote system: ");
  181. Gets(ip, sizeof(ip));
  182. if (ip[0]==0)
  183. strcpy(ip, "127.0.0.1");
  184. printf("\nEnter port of remote system: ");
  185. Gets(port, sizeof(port));
  186. if (port[0]==0)
  187. strcpy(port, "60000");
  188. rakPeer->Connect(ip, atoi(port), 0,0);
  189. }
  190. else if (ch=='m')
  191. {
  192. mute=!mute;
  193. RakNet::DSoundVoiceAdapter::Instance()->SetMute(mute);
  194. if (mute)
  195. printf("\nNow muted.\n");
  196. else
  197. printf("\nNo longer muted.\n");
  198. }
  199. else if (ch=='d')
  200. {
  201. rakPeer->Shutdown(100,0);
  202. }
  203. else if (ch==' ')
  204. {
  205. char message[2048];
  206. RakNet::RakNetStatistics *rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
  207. StatisticsToString(rss, message, 2);
  208. printf("%s", message);
  209. }
  210. else if (ch=='q')
  211. quit=true;
  212. ch=0;
  213. }
  214. #endif
  215. p=rakPeer->Receive();
  216. while (p)
  217. {
  218. if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  219. {
  220. printf("\nID_CONNECTION_REQUEST_ACCEPTED from %s\n", p->systemAddress.ToString());
  221. rakVoice.RequestVoiceChannel(p->guid);
  222. }
  223. else if (p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REQUEST)
  224. {
  225. printf("\nOpen Channel request from %s\n", p->systemAddress.ToString());
  226. }
  227. else if (p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REPLY)
  228. {
  229. printf("\nGot new channel from %s\n", p->systemAddress.ToString());
  230. }
  231. rakPeer->DeallocatePacket(p);
  232. p=rakPeer->Receive();
  233. }
  234. // Update our connection with DirectSound
  235. RakNet::DSoundVoiceAdapter::Instance()->Update();
  236. LogStats();
  237. RakSleep(20);
  238. }
  239. // Release any FMOD resources we used, and shutdown FMOD itself
  240. RakNet::DSoundVoiceAdapter::Instance()->Release();
  241. rakPeer->Shutdown(300);
  242. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  243. return 0;
  244. }
粤ICP备19079148号