SpoutSenderNames.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. spoutSenderNames.h
  3. Spout sender management
  4. Thanks and credit to Malcolm Bechard for modifications to this class
  5. https://github.com/mbechard
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. Copyright (c) 2014-2017, Lynn Jarvis. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  16. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #pragma once
  26. #ifndef __spoutSenderNames__ // standard way as well
  27. #define __spoutSenderNames__
  28. #include <windowsx.h>
  29. #include <d3d9.h>
  30. #include <d3d11.h>
  31. #include <wingdi.h>
  32. #include <set>
  33. #include <map>
  34. #include <string>
  35. #include <vector>
  36. #include <unordered_map>
  37. #include "SpoutCommon.h"
  38. #include "SpoutSharedMemory.h"
  39. #define SPOUT_WAIT_TIMEOUT 100 // 100 msec wait for events
  40. // Now replaced by a global class variable // #define MaxSenders 10 // Max for list of Sender names
  41. #define SpoutMaxSenderNameLen 256
  42. // The texture information structure that is saved to shared memory
  43. // and used for communication between senders and receivers
  44. // unsigned __int32 is used for compatibility between 32bit and 64bit
  45. // See : http://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
  46. // This is also compatible with wyphon :
  47. // The structure is declared here so that this class is can be independent of opengl
  48. //
  49. // 03.07-16 - Use helper functions for conversion of 64bit HANDLE to unsigned __int32
  50. // and unsigned __int32 to 64bit HANDLE
  51. // https://msdn.microsoft.com/en-us/library/aa384267%28VS.85%29.aspx
  52. // in SpoutGLDXinterop.cpp and SpoutSenderNames
  53. //
  54. struct SharedTextureInfo {
  55. unsigned __int32 shareHandle;
  56. unsigned __int32 width;
  57. unsigned __int32 height;
  58. DWORD format; // Texture pixel format
  59. DWORD usage; // not used
  60. wchar_t description[128]; // Wyhon compatible description (not used)
  61. unsigned __int32 partnerId; // Wyphon id of partner that shared it with us (not unused)
  62. };
  63. class SPOUT_DLLEXP spoutSenderNames {
  64. public:
  65. spoutSenderNames();
  66. ~spoutSenderNames();
  67. // public functions
  68. // ------------------------------------------------------------
  69. // You must first register a sender name being using
  70. bool RegisterSenderName(const char* senderName);
  71. bool ReleaseSenderName(const char* senderName);
  72. bool FindSenderName (const char* Sendername);
  73. // ------------------------------------------------------------
  74. // Functions to retrieve info about the sender set map and the senders in it
  75. bool GetSenderNames (std::set<std::string> *Sendernames);
  76. int GetSenderCount();
  77. bool GetSenderNameInfo (int index, char* sendername, int sendernameMaxSize, unsigned int &width, unsigned int &height, HANDLE &dxShareHandle);
  78. // ------------------------------------------------------------
  79. // New for 2.005
  80. int GetMaxSenders();
  81. void SetMaxSenders(int maxSenders); // Set the maximum number of senders in a new sender map
  82. // ------------------------------------------------------------
  83. // Functions to read and write info to a sender memory map
  84. bool GetSenderInfo (const char* sendername, unsigned int &width, unsigned int &height, HANDLE &dxShareHandle, DWORD &dwFormat);
  85. bool SetSenderInfo (const char* sendername, unsigned int width, unsigned int height, HANDLE dxShareHandle, DWORD dwFormat);
  86. // Generic sender map info retrieval
  87. bool getSharedInfo (const char* SenderName, SharedTextureInfo* info);
  88. bool setSharedInfo (const char* SenderName, SharedTextureInfo* info);
  89. // ------------------------------------------------------------
  90. // Functions to maintain the active sender
  91. bool SetActiveSender (const char* Sendername);
  92. bool GetActiveSender (char Sendername[SpoutMaxSenderNameLen]);
  93. bool GetActiveSenderInfo (SharedTextureInfo* info);
  94. bool FindActiveSender (char activename[SpoutMaxSenderNameLen], unsigned int &width, unsigned int &height, HANDLE &hSharehandle, DWORD &dwFormat);
  95. // ------------------------------------------------------------
  96. // Functions to Create, Find or Update a sender without initializing DirectX or the GL/DX interop functions
  97. bool CreateSender (const char *sendername, unsigned int width, unsigned int height, HANDLE hSharehandle, DWORD dwFormat = 0);
  98. bool UpdateSender (const char *sendername, unsigned int width, unsigned int height, HANDLE hSharehandle, DWORD dwFormat = 0);
  99. bool CheckSender (const char *sendername, unsigned int &width, unsigned int &height, HANDLE &hSharehandle, DWORD &dwFormat);
  100. bool FindSender (char *sendername, unsigned int &width, unsigned int &height, HANDLE &hSharehandle, DWORD &dwFormat);
  101. // ------------------------------------------------------------
  102. // Debug function
  103. bool SenderDebug (const char *Sendername, int size);
  104. protected:
  105. // Sender name set management
  106. bool CreateSenderSet();
  107. bool GetSenderSet (std::set<std::string>& SenderNames);
  108. // Active sender management
  109. bool setActiveSenderName (const char* SenderName);
  110. bool getActiveSenderName (char SenderName[SpoutMaxSenderNameLen]);
  111. // Goes through the full list of sender names and cleans up
  112. // any that shouldn't still be around
  113. void cleanSenderSet();
  114. // Functions to manage shared memory map access
  115. static void readSenderSetFromBuffer(const char* buffer, std::set<std::string>& SenderNames, int maxSenders);
  116. static void writeBufferFromSenderSet(const std::set<std::string>& SenderNames, char *buffer, int maxSenders);
  117. SpoutSharedMemory m_senderNames;
  118. SpoutSharedMemory m_activeSender;
  119. // This should be a unordered_map of sender names ->SharedMemory
  120. // to handle multiple inputs and outputs all going through the
  121. // same spoutSenderNames class
  122. // Make this a pointer to avoid size differences between compilers
  123. // if the .dll is compiled with something different
  124. std::unordered_map<std::string, SpoutSharedMemory*>* m_senders;
  125. int m_MaxSenders; // user defined maximum for the number of senders - development testing only
  126. };
  127. #endif