extension.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 :
  53. public SDKExtension,
  54. public IConCommandBaseAccessor
  55. {
  56. public:
  57. /**
  58. * @brief This is called after the initial loading sequence has been processed.
  59. *
  60. * @param error Error message buffer.
  61. * @param maxlength Size of error message buffer.
  62. * @param late Whether or not the module was loaded after map load.
  63. * @return True to succeed loading, false to fail.
  64. */
  65. virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
  66. /**
  67. * @brief This is called right before the extension is unloaded.
  68. */
  69. virtual void SDK_OnUnload();
  70. /**
  71. * @brief This is called once all known extensions have been loaded.
  72. * Note: It is is a good idea to add natives here, if any are provided.
  73. */
  74. virtual void SDK_OnAllLoaded();
  75. /**
  76. * @brief Called when the pause state is changed.
  77. */
  78. //virtual void SDK_OnPauseChange(bool paused);
  79. /**
  80. * @brief this is called when Core wants to know if your extension is working.
  81. *
  82. * @param error Error message buffer.
  83. * @param maxlength Size of error message buffer.
  84. * @return True if working, false otherwise.
  85. */
  86. //virtual bool QueryRunning(char *error, size_t maxlength);
  87. public:
  88. #if defined SMEXT_CONF_METAMOD
  89. /**
  90. * @brief Called when Metamod is attached, before the extension version is called.
  91. *
  92. * @param error Error buffer.
  93. * @param maxlength Maximum size of error buffer.
  94. * @param late Whether or not Metamod considers this a late load.
  95. * @return True to succeed, false to fail.
  96. */
  97. virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late);
  98. /**
  99. * @brief Called when Metamod is detaching, after the extension version is called.
  100. * NOTE: By default this is blocked unless sent from SourceMod.
  101. *
  102. * @param error Error buffer.
  103. * @param maxlength Maximum size of error buffer.
  104. * @return True to succeed, false to fail.
  105. */
  106. //virtual bool SDK_OnMetamodUnload(char *error, size_t maxlength);
  107. /**
  108. * @brief Called when Metamod's pause state is changing.
  109. * NOTE: By default this is blocked unless sent from SourceMod.
  110. *
  111. * @param paused Pause state being set.
  112. * @param error Error buffer.
  113. * @param maxlength Maximum size of error buffer.
  114. * @return True to succeed, false to fail.
  115. */
  116. //virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength);
  117. #endif
  118. public: // IConCommandBaseAccessor
  119. virtual bool RegisterConCommandBase(ConCommandBase *pVar);
  120. public:
  121. CVoice();
  122. void OnGameFrame(bool simulating);
  123. private:
  124. int m_ListenSocket;
  125. struct CClient
  126. {
  127. int m_Socket;
  128. size_t m_BufferWriteIndex;
  129. size_t m_LastLength;
  130. double m_LastValidData;
  131. bool m_New;
  132. } m_aClients[MAX_CLIENTS];
  133. struct pollfd m_aPollFds[1 + MAX_CLIENTS];
  134. int m_PollFds;
  135. CRingBuffer m_Buffer;
  136. double m_AvailableTime;
  137. struct CEncoderSettings
  138. {
  139. SKP_int InputSampleRate_kHz;
  140. SKP_int OutputSampleRate_kHz;
  141. SKP_int TargetBitRate_Kbps;
  142. SKP_int PacketSize_ms;
  143. SKP_int FrameSize_ms;
  144. SKP_int PacketLoss_perc;
  145. SKP_int Complexity;
  146. SKP_int InBandFEC;
  147. SKP_int DTX;
  148. } m_EncoderSettings;
  149. void *m_Silk_EncoderState;
  150. SKP_SILK_SDK_EncControlStruct m_Silk_EncoderControl;
  151. t_SV_BroadcastVoiceData m_SV_BroadcastVoiceData;
  152. void HandleNetwork();
  153. void OnDataReceived(CClient *pClient, int16_t *pData, size_t Samples);
  154. void HandleVoiceData();
  155. void SV_BroadcastVoiceData(IClient *pClient, int nBytes, unsigned char *pData);
  156. };
  157. #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_