Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.3 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef HLTVBROADCAST_HDR
  7. #define HLTVBROADCAST_HDR
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <filesystem.h>
  12. //#include "demo.h"
  13. #include "broadcast.h"
  14. #include "tier0/microprofiler.h"
  15. #include "tier1/utlincrementalvector.h"
  16. #include "steam/steam_api.h"
  17. #include "steam/isteamhttp.h"
  18. class CHLTVFrame;
  19. class CHLTVServer;
  20. class CEngineGotvSyncPacket; // forward declare protobuf message here
  21. class CHLTVBroadcast
  22. {
  23. protected:
  24. class CMemoryStream
  25. {
  26. protected:
  27. CUtlMemory< uint8 > m_Buffer; // data chunk that will get uploaded
  28. uint m_nCommitted; // the number of bytes in payload buffer that will need to be uploaded
  29. uint8 *m_pReserved;
  30. // the total number of bytes allocated in payload buffer includes committed portion + reserved portion that is being filled in right now. m_pPayloadReserved == NULL after upload, otherwise we forgot to commit.
  31. public:
  32. CMemoryStream();
  33. void *Reserve( uint nReserveBytes );
  34. const void *GetReservedBase()const { return m_Buffer.Base() + m_nCommitted; }
  35. const void *Base()const { return m_Buffer.Base(); }
  36. uint GetReservedSize()const { return m_Buffer.Count() - m_nCommitted; }
  37. void Commit( uint nCommitBytes );
  38. void Purge();
  39. bool IsEmpty()const { return m_nCommitted == 0; }
  40. uint GetCommitSize()const { return m_nCommitted; }
  41. void Reset() { m_nCommitted = 0; }
  42. void WriteCmdHeader( unsigned char cmd, int tick, int nPlayerSlot );
  43. };
  44. class CInStreamMsg : public bf_write
  45. {
  46. protected:
  47. CMemoryStream &m_Stream;
  48. // int m_nCmd;
  49. // int m_nTick;
  50. // int m_nPlayerSlot,
  51. public:
  52. CInStreamMsg( CMemoryStream &stream, const char *pDebugName, uint nReserveSize = 256 * 1024 ) :
  53. m_Stream( stream ),
  54. bf_write( pDebugName, stream.Reserve(nReserveSize), nReserveSize )
  55. {
  56. }
  57. ~CInStreamMsg()
  58. {
  59. m_Stream.Commit( GetNumBytesWritten() );
  60. }
  61. };
  62. class CInStreamMsgWithSize : public CInStreamMsg
  63. {
  64. protected:
  65. public:
  66. CInStreamMsgWithSize( CMemoryStream &stream, const char *pDebugName, uint nReserveSize = 256 * 1024 ) :
  67. CInStreamMsg( stream, pDebugName, nReserveSize )
  68. {
  69. this->WriteLong( 0 ); // we'll write this in the end
  70. }
  71. ~CInStreamMsgWithSize()
  72. {
  73. // the length of the message, after the initial 4 bytes that contain the length of the message. Binarily compatible with .dem file
  74. *( int32* )( this->GetBasePointer() ) = this->GetNumBytesWritten() - sizeof( int32 );
  75. }
  76. };
  77. class CHttpCallback : public CCallbackBase
  78. {
  79. public:
  80. CHttpCallback( CHLTVBroadcast *pParent, HTTPRequestHandle hRequest, const char *pResource );
  81. ~CHttpCallback();
  82. virtual void Run( void *pvParam ) OVERRIDE; // success; HTTPRequestCompleted_t
  83. virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) OVERRIDE; // result; HTTPRequestCompleted_t
  84. void DetachFromParent() { m_pParent = NULL; }
  85. void SetProtobufMsgForGCUponSuccess( const CEngineGotvSyncPacket *pProtobufMsgForGCUponSuccess );
  86. public:
  87. int m_nIncrementalVectorIndex;
  88. protected:
  89. virtual int GetCallbackSizeBytes() OVERRIDE { return sizeof( HTTPRequestCompleted_t ); }
  90. CHLTVBroadcast *m_pParent;
  91. HTTPRequestHandle m_hRequest;
  92. CUtlString m_Resource;
  93. const CEngineGotvSyncPacket *m_pProtobufMsgForGCUponSuccess;
  94. };
  95. public:
  96. CHLTVBroadcast( CHLTVServer *pHltvServer );
  97. virtual ~CHLTVBroadcast();
  98. int GetRecordingTick( void )
  99. {
  100. return m_nLastWrittenTick/* - m_nStartTick*/;
  101. }
  102. void OnMasterStarted();
  103. void StartRecording( const char *pBroadcastUrl );
  104. const char *GetUrl()const { return m_Url.Get(); }
  105. void SetSignonState( int state ) {}; // not need by HLTV recorder
  106. bool IsRecording( void );
  107. void PauseRecording( void ) {};
  108. void ResumeRecording( void ) {};
  109. void StopRecording();
  110. void DumpStats();
  111. void RecordCommand( const char *cmdstring );
  112. void RecordUserInput( int cmdnumber ) {}; // not need by HLTV recorder
  113. //void RecordMessages( bf_read &data, int bits );
  114. void RecordServerClasses( CMemoryStream &stream, ServerClass *pClasses );
  115. void RecordStringTables( CMemoryStream &stream );
  116. void ResetDemoInterpolation( void ) {};
  117. public:
  118. void WriteFrame( CHLTVFrame *pFrame, bf_write *additionaldata = NULL );
  119. void ResendStartup();
  120. void RecordSnapshot( CHLTVFrame * pFrame, bf_write * additionaldata, bf_write &msg, int nDeltaTick );
  121. void CloseFile();
  122. void Reset();
  123. void WriteServerInfo( CMemoryStream &stream );
  124. void WriteSignonData(); // write all necessary signon data and returns written bytes
  125. void WriteMessages( CMemoryStream &stream, unsigned char cmd );
  126. void SendSignonData();
  127. int GetMaxAckTickCount();
  128. void OnHttpRequestFailed();
  129. void OnHttpRequestResetContent();
  130. void OnHttpRequestSuccess();
  131. void Register( CHttpCallback *pCallback );
  132. void Unregister( CHttpCallback *pCallback );
  133. protected:
  134. void FlushCollectedStreams(const char *pExtraParams = "");
  135. CHttpCallback * Send( const char* pPath, CMemoryStream &stream );
  136. CHttpCallback * Send( const char* pPath, const void *pBase, uint nSize );
  137. CHttpCallback * LowLevelSend( const CUtlString &path, const void *pBase, uint nSize );
  138. protected:
  139. bool m_bIsRecording;
  140. int m_nFrameCount;
  141. int m_nKeyframeTick;
  142. int m_nStartTick;
  143. int m_nDeltaTick;
  144. int m_nSignonTick;
  145. int m_nCurrentTick;
  146. int m_nSignonDataAckTick; // when signon data was sent out last
  147. int m_nLastWrittenTick;
  148. uint64 m_nMasterCookie;
  149. CHLTVServer *m_pHltvServer;
  150. CMicroProfiler m_mpKeyframe, m_mpFrame, m_mpLowLevelSend;
  151. int64 m_nMaxKeyframeTicks, m_nDecayMaxKeyframeTicks, m_nMaxLowLevelSendTicks;
  152. int64 m_nKeyframeBytes, m_nDeltaFrameBytes;
  153. FileHandle_t m_pFile;
  154. // The following fields are exclusive to the HTTP broadcast implementation, they are not needed for writing into file, memory or netchan streams
  155. //HTTPRequestHandle m_hHTTPRequestHandle;
  156. CMemoryStream m_DeltaStream; // this is being collected every tick, and flushed every so often
  157. CMemoryStream m_SignonDataStream;
  158. int m_nSignonDataFragment;
  159. CUtlString m_Url;
  160. float m_flTimeout;
  161. int m_nFailedHttpRequests;
  162. friend class CHttpCallback;
  163. CUtlIncrementalVector< CHttpCallback > m_HttpRequests; // requests in flight
  164. int m_nHttpRequestBacklogHighWatermark;
  165. int m_nMatchFragmentCounter;
  166. float m_flBroadcastKeyframeInterval;
  167. };
  168. #endif // HLTVBROADCAST_HDR