Source code of Windows XP (NT5)
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.

275 lines
9.2 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: frame.h
  6. * Content: declaration of the CFrame and CFramePool classes
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/16/99 pnewson Created
  12. * 07/22/99 rodtoll Updated target to be DWORD
  13. * 08/03/99 pnewson General clean up, updated target to DVID
  14. * 01/14/2000 rodtoll Updated to support multiple targets. Frame will
  15. * automatically allocate memory as needed for targets.
  16. * rodtoll Added SetEqual function to making copying of frame
  17. * in Queue easier.
  18. * rodtoll Added support for "user controlled memory" frames.
  19. * When the default constructor is used with the UserOwn_XXXX
  20. * functions the frames use user specified buffers.
  21. * (Removes a buffer copy when queueing data).
  22. * 01/31/2000 pnewson replace SAssert with DNASSERT
  23. * 03/29/2000 rodtoll Bug #30753 - Added volatile to the class definition
  24. * 07/09/2000 rodtoll Added signature bytes
  25. *
  26. ***************************************************************************/
  27. #ifndef _FRAME_H_
  28. #define _FRAME_H_
  29. // forward declaration
  30. class CFramePool;
  31. #define VSIG_FRAME 'MRFV'
  32. #define VSIG_FRAME_FREE 'MRF_'
  33. // This class is designed to manage one frame of sound data.
  34. //
  35. // tag: fr
  36. volatile class CFrame
  37. {
  38. private:
  39. DWORD m_dwSignature;
  40. // The critical section object which is used to protect the
  41. // return method. This is passed in by CInputQueue2 and/or CInnerQueue
  42. // class, so that the return method is serialized with calls to
  43. // Reset, Enqueue and Dequeue. If no critical section is passed,
  44. // the Return member should not be used, and this frame should
  45. // not be part of a managed pool.
  46. DNCRITICAL_SECTION *m_pCriticalSection;
  47. // Length of the data within the frame. There may be less then a whole
  48. // frame worth of data in the buffer due to compression/decompression may
  49. // result in a slightly different size buffer.
  50. WORD m_wFrameLength;
  51. // The size of this frame. It would be easier to make
  52. // this a class constant, but we're probably going to want to
  53. // toy with the frame sizes while we're optimizing, and
  54. // we may even get really fancy in the future and have
  55. // the client and server negotiate the frame size at connection
  56. // time, all of which will be easier if we bite the bullet now
  57. // and make this a member variable. Note this is constant,
  58. // so once a frame is instantiated, it's size is permanently set.
  59. WORD m_wFrameSize;
  60. // The client number this frame is coming from or
  61. // going to.
  62. WORD m_wClientId;
  63. // The frame sequence number.
  64. BYTE m_wSeqNum;
  65. // The message number the frame is part of
  66. BYTE m_bMsgNum;
  67. // The target of the frame
  68. PDVID m_pdvidTargets;
  69. DWORD m_dwNumTargets;
  70. DWORD m_dwMaxTargets;
  71. bool m_fOwned;
  72. // A flag to specify that this frame contains nothing but silence.
  73. // When this flag is set, the data in the frame buffer should not
  74. // be used - it's probably not valid.
  75. BYTE m_bIsSilence;
  76. // A pointer to the frame's data
  77. BYTE* m_pbData;
  78. // If this frame is part of a managed frame pool, this
  79. // member will be non-null.
  80. CFramePool* m_pFramePool;
  81. // If this frame is part of a managed frame pool, this
  82. // points to the "primary" pointer to this frame.
  83. // When the frame is unlocked, and therefore returned
  84. // to the pool, the pointer this member points to will
  85. // be set to null. This action is protected by the
  86. // critical section passed to the class.
  87. CFrame** m_ppfrPrimary;
  88. // A flag to indicate if this frame was "lost". This is
  89. // used to distinguish the silent frames pulled from the
  90. // queue between messages from the dead space caused by
  91. // a known lost packet.
  92. bool m_fIsLost;
  93. // don't allow copy construction or assignment of these
  94. // structures, as this would kill our performance, and
  95. // we don't want to do it by accident
  96. CFrame(const CFrame& fr);
  97. CFrame& operator=(const CFrame& fr);
  98. public:
  99. // This constructor sets all the frame's info, and allocates
  100. // the data buffer, but does not set the data inside the buffer
  101. // to anything. Defaults are provided for all the parameters
  102. // except for the frame size. Note: no default constructor,
  103. // since you must specify the frame size.
  104. CFrame(WORD wFrameSize,
  105. WORD wClientNum = 0,
  106. BYTE wSeqNum = 0,
  107. BYTE bMsgNum = 0,
  108. BYTE bIsSilence = 0,
  109. CFramePool *pFramePool = NULL,
  110. DNCRITICAL_SECTION* pCriticalSection = NULL,
  111. CFrame** ppfrPrimary = NULL);
  112. // A frame which manages user ownded memory
  113. CFrame();
  114. // The destructor cleans up the memory allocated by the
  115. // constructor
  116. ~CFrame();
  117. inline DWORD GetNumTargets() const { return m_dwNumTargets; };
  118. inline const PDVID const GetTargetList() const { return m_pdvidTargets; };
  119. // Length of the data within the buffer
  120. WORD GetFrameLength() const { return m_wFrameLength; }
  121. // returns the frame size, (the length of the data buffer)
  122. WORD GetFrameSize() const { return m_wFrameSize; }
  123. HRESULT SetEqual( const CFrame &frSourceFrame );
  124. // These are just a bunch of set and get functions for
  125. // the simple parts of the class, the client id, the
  126. // sequence number, the silence flag, etc.
  127. HRESULT GetTargets( PDVID pdvidTargets, PDWORD pdwNumTargets ) const;
  128. HRESULT SetTargets( PDVID pdvidTargets, DWORD dwNumTargets );
  129. BYTE GetMsgNum() const { return m_bMsgNum; }
  130. void SetMsgNum( BYTE msgNum ) { m_bMsgNum = msgNum; }
  131. void SetClientId(WORD wClientId) { m_wClientId = wClientId; }
  132. WORD GetClientId() const { return m_wClientId; }
  133. void SetSeqNum(BYTE wSeqNum) { m_wSeqNum = wSeqNum; }
  134. BYTE GetSeqNum() const { return m_wSeqNum; }
  135. void SetIsSilence(BYTE bIsSilence) { m_bIsSilence = bIsSilence; }
  136. void SetFrameLength(const WORD &length) { m_wFrameLength = length; }
  137. BYTE GetIsSilence() const { return m_bIsSilence; }
  138. bool GetIsLost() const { return m_fIsLost; }
  139. void SetIsLost(bool fIsLost) { m_fIsLost = fIsLost; }
  140. // Now we have the functions which handle the data. This
  141. // class is pretty trusting, because it will give out the
  142. // pointer to it's data. This is to avoid all non-required
  143. // buffer copies. For example, when you hand a buffer to
  144. // a wave in function, you can give it the pointer to this
  145. // buffer, and it will fill in the frame's buffer directly.
  146. // Between this function and the GetFrameSize() and
  147. // GetFrameLength() functions, you can do anything you want
  148. // with the buffer.
  149. BYTE* GetDataPointer() const { return m_pbData; }
  150. // This copies the data from another frame into this frame
  151. void CopyData(const CFrame& fr)
  152. {
  153. memcpy(m_pbData, fr.GetDataPointer(), fr.GetFrameLength() );
  154. m_wFrameLength = fr.GetFrameLength();
  155. }
  156. void UserOwn_SetData( BYTE *pbData, DWORD dwLength )
  157. {
  158. m_pbData = pbData;
  159. m_wFrameLength = dwLength;
  160. m_wFrameSize = dwLength;
  161. }
  162. void UserOwn_SetTargets( PDVID pdvidTargets, DWORD dwNumTargets )
  163. {
  164. m_pdvidTargets = pdvidTargets;
  165. m_dwNumTargets = dwNumTargets;
  166. m_dwMaxTargets = dwNumTargets;
  167. }
  168. // This copies data from a buffer into this frame's
  169. // buffer.
  170. void CopyData(const BYTE* pbData, WORD wFrameLength);
  171. // If this frame is part of a frame pool managed by a
  172. // CFramePool object, then call this function when you
  173. // are done with the frame and want to return it to the
  174. // pool.
  175. void Return();
  176. void SetCriticalSection(DNCRITICAL_SECTION* pCrit) { m_pCriticalSection = pCrit; }
  177. void SetPrimaryPointer(CFrame** ppfrPrimary) { m_ppfrPrimary = ppfrPrimary; }
  178. void SetFramePool(CFramePool* pFramePool) { m_pFramePool = pFramePool; }
  179. };
  180. // This class manages a pool of frames, to reduce memory requirements.
  181. // Only a few buffers are actually in use at any time by the queue
  182. // class, and yet it may have to allocate hundreds of them unless
  183. // a class such as this is used to manage their reuse.
  184. volatile class CFramePool
  185. {
  186. private:
  187. // the pool is simply a vector of frame objects
  188. std::vector<CFrame *> m_vpfrPool;
  189. // All the frames in the pool must be the same size,
  190. // which is stored here.
  191. WORD m_wFrameSize;
  192. // This critical section is used to exclude the Get()
  193. // and return members from each other.
  194. DNCRITICAL_SECTION m_lock;
  195. BOOL m_fCritSecInited;
  196. public:
  197. // Each frame pool manages frames of a certain size,
  198. // so they can be easily reused. If you need multiple
  199. // different frame sizes, you'll need more than one
  200. // frame pool.
  201. CFramePool(WORD wFrameSize);
  202. ~CFramePool();
  203. BOOL Init()
  204. {
  205. if (DNInitializeCriticalSection( &m_lock ))
  206. {
  207. m_fCritSecInited = TRUE;
  208. return TRUE;
  209. }
  210. else
  211. {
  212. return FALSE;
  213. }
  214. }
  215. // Use Get to retrieve a frame from the pool. ppfrPrimary
  216. // is a pointer to a point that you want set to null when
  217. // this frame is returned to the pool. pCriticalSection
  218. // points to a critical section that will be entered before
  219. // setting the pointer to null, and left after setting the
  220. // pointer to null. This is so external classes (such as
  221. // CInnerQueue) can pass in a critical section that they also
  222. // use to before examining the pointer referred to by ppfrPrimary
  223. CFrame* Get(DNCRITICAL_SECTION* pCriticalSection, CFrame** ppfrPrimary);
  224. // Call Return to give a frame back to the frame pool.
  225. // This may set a pointer to null and enter a critical
  226. // section, as described in Get() above.
  227. void Return(CFrame* pFrame);
  228. };
  229. #endif /* _FRAME_H_ */