extension.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * vim: set ts=4 :
  3. * =============================================================================
  4. * SourceMod Sample Extension
  5. * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
  6. * =============================================================================
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License, version 3.0, as published by the
  10. * Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, AlliedModders LLC gives you permission to link the
  21. * code of this program (as well as its derivative works) to "Half-Life 2," the
  22. * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
  23. * by the Valve Corporation. You must obey the GNU General Public License in
  24. * all respects for all other code used. Additionally, AlliedModders LLC grants
  25. * this exception to all derivative works. AlliedModders LLC defines further
  26. * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
  27. * or <http://www.sourcemod.net/license.php>.
  28. *
  29. * Version: $Id$
  30. */
  31. #ifndef _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
  32. #define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
  33. #include "smsdk_ext.h"
  34. #include <SKP_Silk_SDK_API.h>
  35. #include "ringbuffer.h"
  36. /**
  37. * @file extension.h
  38. * @brief Sample extension code header.
  39. */
  40. #define MAX_CLIENTS 16
  41. #ifdef _WIN32
  42. typedef __int64 int64;
  43. #else
  44. typedef long long int64;
  45. #endif
  46. class IClient;
  47. typedef void (*t_SV_BroadcastVoiceData)(IClient *, int, unsigned char *, int64);
  48. /**
  49. * @brief Sample implementation of the SDK Extension.
  50. * Note: Uncomment one of the pre-defined virtual functions in order to use it.
  51. */
  52. class CVoice : public SDKExtension
  53. {
  54. public:
  55. /**
  56. * @brief This is called after the initial loading sequence has been processed.
  57. *
  58. * @param error Error message buffer.
  59. * @param maxlength Size of error message buffer.
  60. * @param late Whether or not the module was loaded after map load.
  61. * @return True to succeed loading, false to fail.
  62. */
  63. virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
  64. /**
  65. * @brief This is called right before the extension is unloaded.
  66. */
  67. virtual void SDK_OnUnload();
  68. /**
  69. * @brief This is called once all known extensions have been loaded.
  70. * Note: It is is a good idea to add natives here, if any are provided.
  71. */
  72. virtual void SDK_OnAllLoaded();
  73. /**
  74. * @brief Called when the pause state is changed.
  75. */
  76. //virtual void SDK_OnPauseChange(bool paused);
  77. /**
  78. * @brief this is called when Core wants to know if your extension is working.
  79. *
  80. * @param error Error message buffer.
  81. * @param maxlength Size of error message buffer.
  82. * @return True if working, false otherwise.
  83. */
  84. //virtual bool QueryRunning(char *error, size_t maxlength);
  85. public:
  86. #if defined SMEXT_CONF_METAMOD
  87. /**
  88. * @brief Called when Metamod is attached, before the extension version is called.
  89. *
  90. * @param error Error buffer.
  91. * @param maxlength Maximum size of error buffer.
  92. * @param late Whether or not Metamod considers this a late load.
  93. * @return True to succeed, false to fail.
  94. */
  95. //virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late);
  96. /**
  97. * @brief Called when Metamod is detaching, after the extension version is called.
  98. * NOTE: By default this is blocked unless sent from SourceMod.
  99. *
  100. * @param error Error buffer.
  101. * @param maxlength Maximum size of error buffer.
  102. * @return True to succeed, false to fail.
  103. */
  104. //virtual bool SDK_OnMetamodUnload(char *error, size_t maxlength);
  105. /**
  106. * @brief Called when Metamod's pause state is changing.
  107. * NOTE: By default this is blocked unless sent from SourceMod.
  108. *
  109. * @param paused Pause state being set.
  110. * @param error Error buffer.
  111. * @param maxlength Maximum size of error buffer.
  112. * @return True to succeed, false to fail.
  113. */
  114. //virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength);
  115. #endif
  116. public:
  117. CVoice();
  118. void OnGameFrame(bool simulating);
  119. private:
  120. int m_ListenSocket;
  121. struct CClient
  122. {
  123. int m_Socket;
  124. size_t m_BufferWriteIndex;
  125. size_t m_LastLength;
  126. double m_LastValidData;
  127. bool m_New;
  128. } m_aClients[MAX_CLIENTS];
  129. struct pollfd m_aPollFds[1 + MAX_CLIENTS];
  130. int m_PollFds;
  131. CRingBuffer m_Buffer;
  132. double m_AvailableTime;
  133. struct CEncoderSettings
  134. {
  135. SKP_int InputSampleRate_kHz;
  136. SKP_int OutputSampleRate_kHz;
  137. SKP_int TargetBitRate_Kbps;
  138. SKP_int PacketSize_ms;
  139. SKP_int FrameSize_ms;
  140. SKP_int PacketLoss_perc;
  141. SKP_int Complexity;
  142. SKP_int InBandFEC;
  143. SKP_int DTX;
  144. } m_EncoderSettings;
  145. void *m_Silk_EncoderState;
  146. SKP_SILK_SDK_EncControlStruct m_Silk_EncoderControl;
  147. t_SV_BroadcastVoiceData m_SV_BroadcastVoiceData;
  148. void HandleNetwork();
  149. void OnDataReceived(CClient *pClient, int16_t *pData, size_t Samples);
  150. void HandleVoiceData();
  151. void SV_BroadcastVoiceData(IClient *pClient, int nBytes, unsigned char *pData);
  152. };
  153. #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_