main.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "Gets.h"
  20. #include "RakSleep.h"
  21. #include "RakVoice.h"
  22. #include "RakNetStatistics.h"
  23. #include "GetTime.h"
  24. #include "RakAssert.h"
  25. #include "fmod.hpp"
  26. #include "fmod_errors.h"
  27. #include "FMODVoiceAdapter.h"
  28. #if defined(_PS3) || defined(__PS3__)
  29. #include "Console2Includes.h"
  30. #include "fmodps3.h"
  31. #endif
  32. // Reads and writes per second of the sound data
  33. // Speex only supports these 3 values
  34. #define SAMPLE_RATE (8000)
  35. //#define SAMPLE_RATE (16000)
  36. //#define SAMPLE_RATE (32000)
  37. #define FRAMES_PER_BUFFER (2048 / (32000 / SAMPLE_RATE))
  38. // define sample type. Only short(16 bits sound) is supported at the moment.
  39. typedef short SAMPLE;
  40. RakNet::RakPeerInterface *rakPeer=NULL;
  41. FMOD::System *fmodSystem=NULL;
  42. RakNet::RakVoice rakVoice;
  43. bool mute;
  44. void FMOD_ERRCHECK(FMOD_RESULT result)
  45. {
  46. if (result != FMOD_OK)
  47. {
  48. printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  49. #if defined(INTERACTIVE)
  50. system("pause");
  51. #endif
  52. exit(-1);
  53. }
  54. }
  55. struct myStat{
  56. unsigned int time;
  57. unsigned int bitsRec;
  58. unsigned int bitsSent;
  59. };
  60. void LogStats(){
  61. RakNet::RakNetStatistics *rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
  62. char buffer[1024];
  63. StatisticsToString(rss,buffer,1);
  64. printf(buffer);
  65. }
  66. // Prints the current encoder parameters
  67. void PrintParameters(void)
  68. {
  69. printf("\nComplexity=%3d Noise filter=%3s VAD=%3s VBR=%3s\n"
  70. ,rakVoice.GetEncoderComplexity()
  71. ,(rakVoice.IsNoiseFilterActive()) ? "ON" : "OFF"
  72. ,(rakVoice.IsVADActive()) ? "ON" : "OFF"
  73. ,(rakVoice.IsVBRActive()) ? "ON" : "OFF");
  74. }
  75. int main(void)
  76. {
  77. FMOD_RESULT result;
  78. unsigned int version;
  79. /*
  80. Create a System object and initialize.
  81. */
  82. result = FMOD::System_Create(&fmodSystem);
  83. RakAssert(result>=0);
  84. result = fmodSystem->getVersion(&version);
  85. RakAssert(result>=0);
  86. if (version < FMOD_VERSION)
  87. {
  88. printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
  89. return -1;
  90. }
  91. // result = fmodSystem->init(100, FMOD_INIT_NORMAL, (void *)&extradriverdata);
  92. result = fmodSystem->init(100, FMOD_INIT_NORMAL, 0);
  93. RakAssert(result>=0);
  94. // ERRCHECK(result);
  95. printf("A sample on how to use RakVoice. You need a microphone for this sample.\n");
  96. printf("RakVoice relies on Speex for voice encoding and decoding.\n");
  97. printf("See DependentExtensions/RakVoice/speex-1.1.12 for speex projects.\n");
  98. printf("For windows, I had to define HAVE_CONFIG_H, include win32/config.h,\n");
  99. printf("and include the files under libspeex, except those that start with test.\n");
  100. printf("Difficulty: Advanced\n\n");
  101. mute=false;
  102. bool quit;
  103. char ch;
  104. char port[256];
  105. rakPeer = RakNet::RakPeerInterface::GetInstance();
  106. #if defined(INTERACTIVE)
  107. printf("Enter local port: ");
  108. Gets(port, sizeof(port));
  109. if (port[0]==0)
  110. #endif
  111. strcpy(port, "60000");
  112. RakNet::SocketDescriptor socketDescriptor(atoi(port),0);
  113. rakPeer->Startup(4, &socketDescriptor, 1);
  114. rakPeer->SetMaximumIncomingConnections(4);
  115. rakPeer->AttachPlugin(&rakVoice);
  116. rakVoice.Init(SAMPLE_RATE, FRAMES_PER_BUFFER*sizeof(SAMPLE));
  117. // Initialize our connection with FMOD
  118. if (!RakNet::FMODVoiceAdapter::Instance()->SetupAdapter(fmodSystem, &rakVoice)){
  119. printf("An error occurred while initializing FMOD sounds.\n");
  120. exit(-1);
  121. }
  122. RakNet::Packet *p;
  123. quit=false;
  124. #if defined(INTERACTIVE)
  125. printf("(Q)uit. (C)onnect. (D)isconnect. (M)ute. ' ' for stats.\n");
  126. printf("(+/-)encoder complexity. (N)oise filter on/off. (V)AD on/off. (B)vbr on/off.\n");
  127. #else
  128. rakPeer->Connect("1.1.1.1", 60000, 0,0);
  129. #endif
  130. PrintParameters();
  131. while (!quit)
  132. {
  133. #if defined(INTERACTIVE)
  134. if (kbhit())
  135. {
  136. ch=getch();
  137. if (ch=='+'){
  138. // Increase encoder complexity
  139. int v = rakVoice.GetEncoderComplexity();
  140. if (v<10) rakVoice.SetEncoderComplexity(v+1);
  141. PrintParameters();
  142. }
  143. else if (ch=='-'){
  144. // Decrease encoder complexity
  145. int v = rakVoice.GetEncoderComplexity();
  146. if (v>0) rakVoice.SetEncoderComplexity(v-1);
  147. PrintParameters();
  148. }
  149. else if (ch=='n'){
  150. // Turn on/off noise filter
  151. rakVoice.SetNoiseFilter(!rakVoice.IsNoiseFilterActive());
  152. PrintParameters();
  153. }
  154. else if (ch=='v') {
  155. // Turn on/off Voice detection
  156. rakVoice.SetVAD(!rakVoice.IsVADActive());
  157. PrintParameters();
  158. }
  159. else if (ch=='b') {
  160. // Turn on/off VBR
  161. rakVoice.SetVBR(!rakVoice.IsVBRActive());
  162. PrintParameters();
  163. }
  164. else if (ch=='y')
  165. {
  166. quit=true;
  167. }
  168. else if (ch=='c')
  169. {
  170. char ip[256];
  171. printf("\nEnter IP of remote system: ");
  172. Gets(ip, sizeof(ip));
  173. if (ip[0]==0)
  174. strcpy(ip, "127.0.0.1");
  175. printf("\nEnter port of remote system: ");
  176. Gets(port, sizeof(port));
  177. if (port[0]==0)
  178. strcpy(port, "60000");
  179. rakPeer->Connect(ip, atoi(port), 0,0);
  180. }
  181. else if (ch=='m')
  182. {
  183. mute=!mute;
  184. RakNet::FMODVoiceAdapter::Instance()->SetMute(mute);
  185. if (mute)
  186. printf("\nNow muted.\n");
  187. else
  188. printf("\nNo longer muted.\n");
  189. }
  190. else if (ch=='d')
  191. {
  192. rakPeer->Shutdown(100,0);
  193. }
  194. else if (ch==' ')
  195. {
  196. char message[2048];
  197. RakNet::RakNetStatistics *rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
  198. StatisticsToString(rss, message, 2);
  199. printf("%s", message);
  200. }
  201. else if (ch=='q')
  202. quit=true;
  203. ch=0;
  204. }
  205. #endif
  206. p=rakPeer->Receive();
  207. while (p)
  208. {
  209. if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  210. {
  211. printf("\nID_CONNECTION_REQUEST_ACCEPTED from %s\n", p->systemAddress.ToString());
  212. rakVoice.RequestVoiceChannel(p->guid);
  213. }
  214. else if (p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REQUEST)
  215. {
  216. printf("\nOpen Channel request from %s\n", p->systemAddress.ToString());
  217. }
  218. else if (p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REPLY)
  219. {
  220. printf("\nGot new channel from %s\n", p->systemAddress.ToString());
  221. }
  222. rakPeer->DeallocatePacket(p);
  223. p=rakPeer->Receive();
  224. }
  225. fmodSystem->update();
  226. // Update or connection with FMOD
  227. RakNet::FMODVoiceAdapter::Instance()->Update();
  228. // LogStats();
  229. RakSleep(20);
  230. }
  231. // Release any FMOD resources we used, and shutdown FMOD itself
  232. RakNet::FMODVoiceAdapter::Instance()->Release();
  233. fmodSystem->release();
  234. rakPeer->Shutdown(300);
  235. rakPeer->DetachPlugin(&rakVoice);
  236. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  237. return 0;
  238. }
粤ICP备19079148号