SpoutSDK.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. SpoutSDK.h
  3. The main SDK include file
  4. Copyright (c) 2014-2015, Lynn Jarvis. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  13. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #pragma once
  23. #ifndef __SpoutSDK__
  24. #define __SpoutSDK__
  25. #include <windows.h>
  26. #include <set>
  27. #include <string>
  28. #include <iostream>
  29. #include <fstream>
  30. #include <Mmsystem.h> // for timegettime
  31. #include <direct.h> // for _getcwd
  32. #include <shlwapi.h> // for path functions
  33. #include "Shellapi.h" // for shellexecute
  34. #pragma comment(lib, "shlwapi.lib") // for path functions
  35. #pragma comment(lib, "Shell32.lib") // for shellexecute
  36. #pragma comment(lib, "Advapi32.lib") // for registry functions
  37. #pragma comment(lib, "Version.lib") // for VersionInfo API
  38. #include "SpoutCommon.h"
  39. #include "spoutMemoryShare.h"
  40. #include "SpoutSenderNames.h"
  41. #include "spoutGLDXinterop.h"
  42. #if defined(__x86_64__) || defined(_M_X64)
  43. #define is64bit
  44. // #elif defined(__i386) || defined(_M_IX86)
  45. // x86 32-bit
  46. #endif
  47. class SPOUT_DLLEXP Spout {
  48. public:
  49. Spout();
  50. ~Spout();
  51. // ================== //
  52. // PUBLIC FUNCTIONS //
  53. // ================== //
  54. // Sender
  55. bool CreateSender (const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
  56. bool UpdateSender (const char* Sendername, unsigned int width, unsigned int height);
  57. void ReleaseSender (DWORD dwMsec = 0);
  58. bool SendTexture (GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO=0);
  59. bool SendImage (unsigned char* pixels, unsigned int width, unsigned int height, GLenum glFormat = GL_RGBA, bool bAlignment = true, bool bInvert=false);
  60. // Receiver
  61. bool CreateReceiver (char* Sendername, unsigned int &width, unsigned int &height, bool bUseActive = false);
  62. void ReleaseReceiver();
  63. bool ReceiveTexture (char* Sendername, unsigned int &width, unsigned int &height, GLuint TextureID = 0, GLuint TextureTarget = 0, bool bInvert = false, GLuint HostFBO=0);
  64. bool ReceiveImage (char* Sendername, unsigned int &width, unsigned int &height, unsigned char* pixels, GLenum glFormat = GL_RGBA, GLuint HostFBO=0);
  65. bool CheckReceiver (char* Sendername, unsigned int &width, unsigned int &height, bool &bConnected);
  66. bool GetImageSize (char* sendername, unsigned int &width, unsigned int &height, bool &bMemoryMode);
  67. bool BindSharedTexture();
  68. bool UnBindSharedTexture();
  69. bool DrawSharedTexture(float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = true);
  70. bool DrawToSharedTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = false, GLuint HostFBO = 0);
  71. int GetSenderCount ();
  72. bool GetSenderName (int index, char* sendername, int MaxSize = 256);
  73. bool GetSenderInfo (const char* sendername, unsigned int &width, unsigned int &height, HANDLE &dxShareHandle, DWORD &dwFormat);
  74. bool GetActiveSender(char* Sendername);
  75. bool SetActiveSender(const char* Sendername);
  76. // Utilities
  77. bool SetDX9(bool bDX9 = true); // User request to use DirectX 9 (default is DirectX 11)
  78. bool GetDX9(); // Return the flag that has been set
  79. bool SetMemoryShareMode(bool bMem = true);
  80. bool GetMemoryShareMode();
  81. int GetMaxSenders(); // Get maximum senders allowed
  82. void SetMaxSenders(int maxSenders); // Set maximum senders allowed
  83. int GetNumAdapters(); // Get the number of graphics adapters in the system
  84. bool GetAdapterName(int index, char *adaptername, int maxchars); // Get an adapter name
  85. bool SetAdapter(int index = 0); // Set required graphics adapter for output
  86. int GetAdapter(); // Get the SpoutDirectX global adapter index
  87. bool GetHostPath(const char *sendername, char *hostpath, int maxchars); // The path of the host that produced the sender
  88. int GetVerticalSync();
  89. bool SetVerticalSync(bool bSync = true);
  90. bool SelectSenderPanel(const char* message = NULL);
  91. bool CheckSpoutPanel(); // Public for debugging
  92. bool OpenSpout(); // Public for debugging
  93. // Registry read/write
  94. bool WritePathToRegistry (const char *filepath, const char *subkey, const char *valuename);
  95. bool ReadPathFromRegistry(char *filepath, const char *subkey, const char *valuename);
  96. bool RemovePathFromRegistry(const char *subkey, const char *valuename);
  97. spoutGLDXinterop interop; // Opengl/directx interop texture sharing
  98. // Public - for debugging only - to disable/enable texture access locks in SpoutDirectX.cpp
  99. void UseAccessLocks(bool bUseLocks);
  100. void SpoutCleanUp(bool bExit = false);
  101. void CleanSenders();
  102. /*
  103. //
  104. //
  105. // http://msdn.microsoft.com/en-us/library/windows/desktop/bb172558%28v=vs.85%29.aspx
  106. //
  107. // Compatible DX11/DX9 format for Texture2D
  108. // http://msdn.microsoft.com/en-us/library/windows/desktop/ff471324%28v=vs.85%29.aspx
  109. //
  110. // DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM
  111. // are compatible with DX9 - D3DFMT_A8B8G8R8
  112. //
  113. // Noted that DX11 -> DX9 only works if the DX11 format is set to DXGI_FORMAT_B8G8R8A8_UNORM
  114. // if the DX9 format is set to D3DFMT_A8B8G8R8
  115. DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
  116. DXGI_FORMAT_R8G8B8A8_UNORM = 28,
  117. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
  118. DXGI_FORMAT_R8G8B8A8_UINT = 30,
  119. DXGI_FORMAT_R8G8B8A8_SNORM = 31,
  120. DXGI_FORMAT_R8G8B8A8_SINT = 32,
  121. DXGI_FORMAT_B8G8R8A8_UNORM = 87,
  122. DXGI_FORMAT_B8G8R8X8_UNORM = 88,
  123. DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
  124. DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
  125. DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
  126. DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
  127. */
  128. protected :
  129. // ================================= //
  130. // PRIVATE VARIABLES AND FUNCTIONS //
  131. // ================================= //
  132. char g_SharedMemoryName[256];
  133. char UserSenderName[256]; // used for the sender selection dialog
  134. unsigned int g_Width;
  135. unsigned int g_Height;
  136. HANDLE g_ShareHandle;
  137. DWORD g_Format;
  138. GLuint g_TexID;
  139. HWND g_hWnd;
  140. bool bMemory; // force memoryshare flag
  141. bool bGLDXcompatible;
  142. bool bMemoryShareInitOK;
  143. bool bDxInitOK;
  144. bool bInitialized;
  145. bool bIsSending;
  146. bool bIsReceiving;
  147. bool bChangeRequested;
  148. bool bSpoutPanelOpened;
  149. bool bSpoutPanelActive;
  150. bool bUseActive; // Use the active sender for CreateReceiver
  151. SHELLEXECUTEINFOA ShExecInfo;
  152. bool GLDXcompatible();
  153. bool OpenReceiver (char *name, unsigned int& width, unsigned int& height);
  154. bool InitReceiver (HWND hwnd, char* sendername, unsigned int width, unsigned int height, bool bMemoryMode);
  155. bool InitSender (HWND hwnd, const char* sendername, unsigned int width, unsigned int height, DWORD dwFormat, bool bMemoryMode);
  156. bool InitMemoryShare(bool bReceiver);
  157. bool ReleaseMemoryShare();
  158. bool FlipVertical(unsigned char *src, unsigned int width, unsigned int height, GLenum glFormat = GL_RGB);
  159. // FPS calcs - TODO cleanup
  160. double timeNow, timeThen, elapsedTime, frameTime, lastFrameTime, frameRate, fps, PCFreq, waitMillis, millisForFrame;
  161. __int64 CounterStart;
  162. // Find a file version
  163. bool FindFileVersion(const char *filepath, DWORD &versMS, DWORD &versLS);
  164. // TODO - used ? cleanup
  165. void StartCounter();
  166. double GetCounter();
  167. };
  168. #endif