SegServerImpl.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Copyright (c) 2015-2016, Intel Corporation
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the name of Intel Corporation nor the names of its contributors
  11. may be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  17. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "SegServerImpl.h"
  25. SegServerImpl* SegServerImpl::instance = nullptr;
  26. SegServerImpl::SegServerImpl()
  27. {
  28. m_comInit = false;
  29. m_serviceConnected = false;
  30. m_sharedBuffer = NULL;
  31. }
  32. SegServerImpl::~SegServerImpl()
  33. {
  34. if (m_server)
  35. {
  36. m_server->Release();
  37. m_server = nullptr;
  38. }
  39. instance = nullptr;
  40. };
  41. SegServer* SegServerImpl::CreateServer()
  42. {
  43. if (instance == nullptr)
  44. {
  45. instance = new SegServerImpl();
  46. return instance;
  47. }
  48. else
  49. {
  50. return nullptr;
  51. }
  52. }
  53. SegServer* SegServerImpl::GetServerInstance()
  54. {
  55. return instance;
  56. }
  57. SegServerImpl::ServiceStatus SegServerImpl::Init()
  58. {
  59. if (m_comInit && m_serviceConnected)
  60. return SERVICE_REINIT_ERROR;
  61. GUID guid = { 0 };
  62. m_bufferName = new wchar_t[40]();
  63. CoCreateGuid(&guid);
  64. StringFromGUID2(guid, m_bufferName, 40);
  65. if (!m_comInit)
  66. {
  67. HRESULT comInit = CoInitializeEx(0, COINIT_MULTITHREADED);
  68. if (FAILED(comInit))
  69. {
  70. comInit = CoInitialize(0);
  71. if (FAILED(comInit))
  72. {
  73. return COM_LIB_INIT_ERROR;
  74. }
  75. }
  76. m_comInit = true;
  77. }
  78. if (!m_serviceConnected)
  79. {
  80. HRESULT instanceCreate = CoCreateInstance(CLSID_SegProc, NULL, CLSCTX_LOCAL_SERVER, IID_ISegProc, (void**)&m_server);
  81. if (FAILED(instanceCreate)) {
  82. return SERVICE_INIT_ERROR;
  83. }
  84. HRESULT serverInit = m_server->Init(m_bufferName);
  85. if (FAILED(serverInit)) {
  86. return SERVICE_INIT_ERROR;
  87. }
  88. m_serviceConnected = true;
  89. }
  90. return SERVICE_NO_ERROR;
  91. }
  92. SegServerImpl::ServiceStatus SegServerImpl::Stop()
  93. {
  94. HRESULT errCode = m_server->Stop();
  95. if (FAILED(errCode)) {
  96. return SERVICE_FUNC_ERROR;
  97. }
  98. return SERVICE_NO_ERROR;
  99. }
  100. void SegServerImpl::SetFps(int fps)
  101. {
  102. m_server->SetFps(fps);
  103. }
  104. int SegServerImpl::GetFps()
  105. {
  106. int outFps = -1;
  107. m_server->GetFps(&outFps);
  108. return outFps;
  109. }
  110. void SegServerImpl::SetIVCAMMotionRangeTradeOff(int value)
  111. {
  112. m_server->SetIVCAMMotionRangeTradeOff(value);
  113. }
  114. int SegServerImpl::GetIVCAMMotionRangeTradeOff()
  115. {
  116. int outPropertyValue = -1;
  117. m_server->GetIVCAMMotionRangeTradeOff(&outPropertyValue);
  118. return outPropertyValue;
  119. }
  120. SegServerImpl::ServiceStatus SegServerImpl::GetFrame(SegImage** image)
  121. {
  122. ServiceStatus res = SERVICE_NO_ERROR;
  123. int frameId = -1;
  124. int bufferRealloc = -1;
  125. int frameSize = -1;
  126. if (FAILED(m_server->LockBuffer(&frameId, &frameSize, &bufferRealloc)))
  127. {
  128. return SHARED_MEMORY_ERROR;
  129. }
  130. if (bufferRealloc)
  131. {
  132. HANDLE hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, m_bufferName);
  133. if (hMapFile == NULL)
  134. {
  135. m_server->UnlockBuffer();
  136. return SHARED_MEMORY_ERROR;
  137. }
  138. m_sharedBuffer = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, frameSize * 2);
  139. if (m_sharedBuffer == NULL)
  140. {
  141. CloseHandle(hMapFile);
  142. m_server->UnlockBuffer();
  143. return SHARED_MEMORY_ERROR;
  144. }
  145. CloseHandle(hMapFile);
  146. }
  147. if(m_sharedBuffer && frameId != -1 && frameSize != -1)
  148. {
  149. size_t offset = (size_t)frameId * frameSize;
  150. //int width = ((int*)(m_sharedBuffer + offset))[0];
  151. //int height = ((int*)(m_sharedBuffer + offset))[1];
  152. //int pitch = ((int*)(m_sharedBuffer + offset))[2];
  153. FrameHeader* fhPtr = (FrameHeader*)(m_sharedBuffer + offset);
  154. int width = fhPtr->width;
  155. int height = fhPtr->height;
  156. int pitch = fhPtr->pitch;
  157. long long timestamp = fhPtr->timestamp;
  158. int frameNumber = fhPtr->frameNumber;
  159. void* data = (void*)(m_sharedBuffer + offset + sizeof(FrameHeader));
  160. SegImage* result = nullptr;
  161. try
  162. {
  163. result = new SegImage(width, height, pitch, data, timestamp, frameNumber);
  164. }
  165. catch (std::bad_alloc &/*e*/)
  166. {
  167. res = ALLOCATION_FAILURE;
  168. result = NULL;
  169. }
  170. *image = result;
  171. }
  172. else
  173. {
  174. res = SERVICE_NOT_READY;
  175. }
  176. m_server->UnlockBuffer();
  177. return res;
  178. }