SpoutSenderNames.h 6.7 KB

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