Leaked source code of windows server 2003
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.

473 lines
9.5 KiB

  1. #include "precomp.h"
  2. #define ZONE_AP 1
  3. #define _GetPlatform() (m_dwState & DP_MASK_PLATFORM)
  4. #define _SetPlatform(s) (m_dwState = (m_dwState & ~DP_MASK_PLATFORM) | (s & DP_MASK_PLATFORM))
  5. ///////////////////////////////////////////////////////
  6. //
  7. // Public methods
  8. //
  9. MediaPacket::MediaPacket ( void )
  10. {
  11. _Construct ();
  12. }
  13. MediaPacket::~MediaPacket ( void )
  14. {
  15. _Destruct ();
  16. }
  17. HRESULT MediaPacket::Initialize ( MEDIAPACKETINIT * p )
  18. {
  19. HRESULT hr = DPR_SUCCESS;
  20. FX_ENTRY ("AdPckt::Init")
  21. if (p == NULL)
  22. {
  23. DEBUGMSG (ZONE_AP, ("%s: invalid parameter (null ptr)\r\n", _fx_));
  24. return DPR_INVALID_PARAMETER;
  25. }
  26. _Construct ();
  27. // we need to analyze flags to
  28. // warn conflicting or insufficient flags
  29. m_dwState |= p->dwFlags;
  30. // get handle of stream conversion
  31. m_hStrmConv = p->hStrmConv;
  32. // get handle of wave device
  33. // m_hDev = p->hDevAudio;
  34. m_hDev = NULL;
  35. // flags about prepared
  36. m_fDevPrepared = FALSE;
  37. m_fStrmPrepared = FALSE;
  38. // set up wave formats
  39. m_pStrmConvSrcFmt = p->pStrmConvSrcFmt;
  40. m_pStrmConvDstFmt = p->pStrmConvDstFmt;
  41. m_pDevFmt = p->pDevFmt;
  42. m_payload = p->payload;
  43. // net buffer
  44. if (p->cbSizeNetData)
  45. { // send or recv
  46. m_pNetData = (NETBUF *) LocalAlloc (LMEM_FIXED, p->cbSizeNetData + p->cbOffsetNetData + p->cbPayloadHeaderSize + sizeof (NETBUF));
  47. if (m_pNetData == NULL)
  48. {
  49. DEBUGMSG (ZONE_AP, ("%s: MemAlloc1 (%ld) failed\r\n",
  50. _fx_, (ULONG) (p->cbSizeNetData + p->cbOffsetNetData)));
  51. hr = DPR_OUT_OF_MEMORY;
  52. goto MyExit;
  53. }
  54. m_pNetData->data = (PBYTE) m_pNetData + p->cbOffsetNetData + p->cbPayloadHeaderSize + sizeof (NETBUF);
  55. m_pNetData->length = p->cbSizeNetData;
  56. m_cbMaxNetData = p->cbSizeNetData;
  57. m_pNetData->pool = NULL;
  58. }
  59. else
  60. {
  61. #ifdef PREP_HDR_PER_CONV
  62. // recv
  63. m_pNetData = NULL;
  64. #else
  65. hr = DPR_INVALID_PARAMETER;
  66. goto MyExit;
  67. #endif
  68. }
  69. m_index = p->index;
  70. // if m_pStrmConvDstFmt == m_pStrmConvSrcFmt,
  71. // then m_pRawData <-- m_pNetData
  72. // else allocate it
  73. if (IsSameMediaFormat (m_pStrmConvSrcFmt, m_pStrmConvDstFmt))
  74. {
  75. m_pRawData = m_pNetData;
  76. }
  77. else if (p->fDontAllocRawBufs)
  78. {
  79. m_pRawData = NULL;
  80. }
  81. else
  82. {
  83. m_pRawData = (NETBUF *) LocalAlloc (LMEM_FIXED, p->cbSizeRawData + p->cbOffsetRawData + sizeof(NETBUF));
  84. if (m_pRawData == NULL)
  85. {
  86. DEBUGMSG (ZONE_AP, ("%s: MemAlloc2 (%ld) failed\r\n",
  87. _fx_, (ULONG) (p->cbSizeRawData + p->cbOffsetRawData)));
  88. hr = DPR_OUT_OF_MEMORY;
  89. goto MyExit;
  90. }
  91. m_pRawData->data = (PBYTE) m_pRawData + sizeof(NETBUF) + p->cbOffsetRawData;
  92. m_pRawData->length = p->cbSizeRawData;
  93. m_pRawData->pool = NULL;
  94. }
  95. // if m_pDevFmt == m_pStrmConvSrcFmt (when SEND)
  96. // then m_pDevData <-- m_pRawData
  97. // else allocate it
  98. if (((m_dwState & DP_FLAG_SEND) &&
  99. IsSameMediaFormat (m_pStrmConvSrcFmt, m_pDevFmt)) ||
  100. ((m_dwState & DP_FLAG_RECV) &&
  101. IsSameMediaFormat (m_pStrmConvDstFmt, m_pDevFmt)))
  102. {
  103. // typical case - codec raw format matches that of i/o device
  104. m_pDevData = m_pRawData;
  105. }
  106. else
  107. {
  108. // codec raw format doesnt match that of device
  109. // BUGBUG: we dont really handle this case yet
  110. m_pDevData = (NETBUF *) LocalAlloc (LMEM_FIXED, p->cbSizeDevData + p->cbOffsetDevData + sizeof(NETBUF));
  111. if (m_pDevData == NULL)
  112. {
  113. DEBUGMSG (ZONE_AP, ("%s: MemAlloc3 (%ld) failed\r\n",
  114. _fx_, (ULONG) (p->cbSizeDevData + p->cbOffsetDevData)));
  115. hr = DPR_OUT_OF_MEMORY;
  116. goto MyExit;
  117. }
  118. m_pDevData->data = (PBYTE) m_pDevData + sizeof(NETBUF) + p->cbOffsetDevData;
  119. m_pDevData->length = p->cbSizeDevData;
  120. m_pDevData->pool = NULL;
  121. }
  122. MakeSilence ();
  123. MyExit:
  124. if (hr == DPR_SUCCESS)
  125. {
  126. m_fInitialized = TRUE;
  127. SetState (MP_STATE_RESET);
  128. }
  129. return hr;
  130. }
  131. HRESULT MediaPacket::Receive ( NETBUF *pNetBuf, DWORD timestamp, UINT seq, UINT fMark )
  132. {
  133. m_seq = seq;
  134. m_timestamp = timestamp;
  135. m_fMark = fMark;
  136. #ifdef PREP_HDR_PER_CONV
  137. m_pNetData = pNetBuf;
  138. #else
  139. if (pNetBuf) // pNetBuf may be NULL for video
  140. {
  141. if (pNetBuf->length > m_cbMaxNetData)
  142. return DPR_INVALID_PARAMETER;
  143. if (m_pNetData && pNetBuf)
  144. {
  145. CopyMemory (m_pNetData->data, pNetBuf->data,
  146. (m_pNetData->length = pNetBuf->length));
  147. }
  148. }
  149. #endif
  150. LOG(((m_dwState & DP_FLAG_VIDEO)? LOGMSG_VID_RECV: LOGMSG_AUD_RECV,m_index,seq,m_pNetData->length));
  151. SetState (MP_STATE_NET_IN_STREAM);
  152. return DPR_SUCCESS;
  153. }
  154. HRESULT MediaPacket::Recycle ( void )
  155. {
  156. HRESULT hr = DPR_SUCCESS;
  157. FX_ENTRY ("MdPckt::Recycle")
  158. LOG(((m_dwState & DP_FLAG_VIDEO)? LOGMSG_VID_RECYCLE: LOGMSG_AUD_RECYCLE, m_index));
  159. if (m_dwState & DP_FLAG_RECV)
  160. {
  161. if (m_pRawData && m_pRawData->pool) {
  162. m_pRawData->pool->ReturnBuffer((PVOID) m_pRawData);
  163. if (m_pDevData == m_pRawData)
  164. m_pDevData = NULL;
  165. m_pRawData = NULL;
  166. }
  167. #ifdef PREP_HDR_PER_CONV
  168. // free net data buffer
  169. if (m_pNetData && m_pNetData->pool) m_pNetData->pool->ReturnBuffer ((PVOID) m_pNetData);
  170. if (m_pNetData == m_pRawData) m_pRawData = NULL;
  171. m_pNetData = NULL;
  172. #endif
  173. }
  174. SetState (MP_STATE_RESET);
  175. return hr;
  176. }
  177. HRESULT MediaPacket::GetProp ( DWORD dwPropId, PDWORD_PTR pdwPropVal )
  178. {
  179. HRESULT hr = DPR_SUCCESS;
  180. FX_ENTRY ("AdPckt::GetProp")
  181. if (pdwPropVal)
  182. {
  183. switch (dwPropId)
  184. {
  185. case MP_PROP_STATE:
  186. *pdwPropVal = GetState ();
  187. break;
  188. case MP_PROP_PLATFORM:
  189. *pdwPropVal = _GetPlatform ();
  190. break;
  191. case MP_PROP_DEV_MEDIA_FORMAT:
  192. *pdwPropVal = (DWORD_PTR) m_pDevFmt;
  193. break;
  194. case MP_PROP_DEV_DATA:
  195. *pdwPropVal = (DWORD_PTR) m_pDevData;
  196. break;
  197. case MP_PROP_DEV_HANDLE:
  198. *pdwPropVal = (DWORD_PTR) m_hDev;
  199. break;
  200. case MP_PROP_DEV_MEDIA_HDR:
  201. *pdwPropVal = (DWORD_PTR) m_pDevHdr;
  202. break;
  203. case MP_PROP_IN_STREAM_FORMAT:
  204. *pdwPropVal = (DWORD_PTR) m_pStrmConvSrcFmt;
  205. break;
  206. case MP_PROP_OUT_STREAM_FORMAT:
  207. *pdwPropVal = (DWORD_PTR) m_pStrmConvDstFmt;
  208. break;
  209. case MP_PROP_TIMESTAMP:
  210. *pdwPropVal = (DWORD) m_timestamp;
  211. break;
  212. case MP_PROP_INDEX:
  213. *pdwPropVal = (DWORD) m_index;
  214. break;
  215. case MP_PROP_PREAMBLE:
  216. *pdwPropVal = (DWORD) m_fMark;
  217. break;
  218. case MP_PROP_FILTER_HEADER:
  219. *pdwPropVal = (DWORD_PTR) m_pStrmConvHdr;
  220. break;
  221. case MP_PROP_MAX_NET_LENGTH:
  222. *pdwPropVal = m_cbMaxNetData;
  223. break;
  224. default:
  225. hr = DPR_INVALID_PROP_ID;
  226. break;
  227. }
  228. }
  229. else
  230. {
  231. hr = DPR_INVALID_PARAMETER;
  232. }
  233. return hr;
  234. }
  235. HRESULT MediaPacket::SetProp ( DWORD dwPropId, DWORD_PTR dwPropVal )
  236. {
  237. HRESULT hr = DPR_SUCCESS;
  238. FX_ENTRY ("AdPckt::SetProp")
  239. switch (dwPropId)
  240. {
  241. case MP_PROP_STATE:
  242. SetState ((DWORD)dwPropVal);
  243. break;
  244. case MP_PROP_PLATFORM:
  245. _SetPlatform ((DWORD)dwPropVal);
  246. break;
  247. case MP_PROP_DEV_MEDIA_FORMAT:
  248. case MP_PROP_IN_STREAM_FORMAT:
  249. case MP_PROP_OUT_STREAM_FORMAT:
  250. hr = DPR_IMPOSSIBLE_SET_PROP;
  251. break;
  252. case MP_PROP_TIMESTAMP:
  253. m_timestamp = (DWORD)dwPropVal;
  254. break;
  255. case MP_PROP_PREAMBLE:
  256. m_fMark = dwPropVal ? 1 : 0;
  257. break;
  258. default:
  259. hr = DPR_INVALID_PROP_ID;
  260. break;
  261. }
  262. return hr;
  263. }
  264. HRESULT MediaPacket::Release ( void )
  265. {
  266. _Destruct ();
  267. return DPR_SUCCESS;
  268. }
  269. BOOL MediaPacket::SetDecodeBuffer(NETBUF *pBuf)
  270. {
  271. ASSERT(!m_pRawData);
  272. m_pRawData = pBuf;
  273. if (!m_pDevData) m_pDevData = pBuf;
  274. return TRUE;
  275. }
  276. ///////////////////////////////////////////////////////
  277. //
  278. // Private methods
  279. //
  280. void MediaPacket::_Construct ( void )
  281. {
  282. m_hStrmConv = NULL;
  283. m_pStrmConvHdr = NULL;
  284. m_pStrmConvSrcFmt = NULL;
  285. m_pStrmConvDstFmt = NULL;
  286. m_hDev = NULL;
  287. m_pDevHdr = NULL;
  288. m_pDevFmt = NULL;
  289. m_pDevData = NULL;
  290. m_pRawData = NULL;
  291. m_pNetData = NULL;
  292. m_dwState = 0;
  293. m_fBusy = FALSE;
  294. m_timestamp = 0;
  295. m_seq = 0;
  296. m_index = 0;
  297. m_fMark = 0;
  298. m_cbValidRawData = 0;
  299. m_fRendering = FALSE;
  300. m_fInitialized = FALSE;
  301. }
  302. void MediaPacket::_Destruct ( void )
  303. {
  304. if (m_fInitialized)
  305. {
  306. if (m_pDevHdr) MemFree (m_pDevHdr);
  307. m_pDevHdr = NULL;
  308. if (m_pStrmConvHdr) MemFree (m_pStrmConvHdr);
  309. m_pStrmConvHdr = NULL;
  310. if (m_pDevData == m_pRawData) m_pDevData = NULL;
  311. if (m_pRawData == m_pNetData) m_pRawData = NULL;
  312. if (m_pDevData) {
  313. if (m_pDevData->pool)
  314. m_pDevData->pool->ReturnBuffer((PVOID) m_pDevData);
  315. else
  316. LocalFree (m_pDevData);
  317. m_pDevData = NULL;
  318. }
  319. if (m_pRawData) {
  320. if (m_pRawData->pool)
  321. m_pRawData->pool->ReturnBuffer((PVOID) m_pRawData);
  322. else
  323. LocalFree (m_pRawData);
  324. m_pRawData = NULL;
  325. }
  326. if (m_pNetData && m_pNetData->pool)
  327. m_pNetData->pool->ReturnBuffer ((PVOID) m_pNetData);
  328. else if (m_pNetData)
  329. LocalFree (m_pNetData);
  330. m_pNetData = NULL;
  331. SetState (MP_STATE_RESET);
  332. m_fInitialized = FALSE;
  333. }
  334. }
  335. HRESULT MediaPacket::GetDevData(PVOID *ppData, PUINT pcbData)
  336. {
  337. if (!ppData || !pcbData)
  338. return DPR_INVALID_PARAMETER;
  339. if (m_pDevData) {
  340. *ppData = m_pDevData->data;
  341. *pcbData = m_pDevData->length;
  342. } else {
  343. *ppData = NULL;
  344. *pcbData = 0;
  345. }
  346. return DPR_SUCCESS;
  347. }
  348. HRESULT MediaPacket::GetNetData(PVOID *ppData, PUINT pcbData)
  349. {
  350. if (!ppData || !pcbData)
  351. return DPR_INVALID_PARAMETER;
  352. if (m_pNetData) {
  353. *ppData = m_pNetData->data;
  354. *pcbData = m_pNetData->length;
  355. } else {
  356. *ppData = NULL;
  357. *pcbData = 0;
  358. }
  359. return DPR_SUCCESS;
  360. }
  361. HRESULT MediaPacket::SetNetLength(UINT uLength)
  362. {
  363. if ((m_pNetData) && (m_pNetData->data))
  364. {
  365. m_pNetData->length = uLength;
  366. }
  367. else
  368. {
  369. return E_FAIL;
  370. }
  371. return S_OK;
  372. }
  373.