BDACard.cpp
Upload User: endongvci
Upload Date: 2016-07-19
Package Size: 72k
Code Size: 9k
Category:

DirextX

Development Platform:

Visual C++

  1. /**
  2.  * BDACardCollection.cpp
  3.  * Copyright (C) 2004 Nate
  4.  *
  5.  * This file is part of DigitalWatch, a free DTV watching and recording
  6.  * program for the VisionPlus DVB-T.
  7.  *
  8.  * DigitalWatch is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * DigitalWatch is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with DigitalWatch; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22. //this is a file from DigitalWatch 2 that i've hacked up to work here.
  23. #include "BDACard.h"
  24. #include "StdAfx.h"
  25. #include "GlobalFunctions.h"
  26. //#include <dshow.h>
  27. #include <ks.h> // Must be included before ksmedia.h
  28. #include <ksmedia.h> // Must be included before bdamedia.h
  29. #include <bdatypes.h> // Must be included before bdamedia.h
  30. #include <bdamedia.h>
  31. BDACard::BDACard()
  32. {
  33. bActive = FALSE;
  34. bNew = FALSE;
  35. bDetected = FALSE;
  36. }
  37. BDACard::~BDACard()
  38. {
  39. m_pCapturePin.Release();
  40. RemoveFilters();
  41. m_piGraphBuilder.Release();
  42. }
  43. void BDACard::SetLogCallback(LogMessageCallback *callback)
  44. {
  45. LogMessageCaller::SetLogCallback(callback);
  46. tunerDevice.SetLogCallback(callback);
  47. demodDevice.SetLogCallback(callback);
  48. captureDevice.SetLogCallback(callback);
  49. graphTools.SetLogCallback(callback);
  50. }
  51. HRESULT BDACard::LoadFromXML(XMLElement *pElement)
  52. {
  53. XMLAttribute *attr;
  54. attr = pElement->Attributes.Item(L"active");
  55. bActive = (attr) && (attr->value[0] == '1');
  56. XMLElement *device;
  57. device = pElement->Elements.Item(L"Tuner");
  58. if (device == NULL)
  59. {
  60. return (log << "Cannot add device without setting tuner name and device pathn").Write(E_FAIL);
  61. }
  62. attr = device->Attributes.Item(L"device");
  63. strCopy(tunerDevice.strDevicePath, (attr ? attr->value : L""));
  64. attr = device->Attributes.Item(L"name");
  65. strCopy(tunerDevice.strFriendlyName, (attr ? attr->value : L""));
  66. tunerDevice.bValid = TRUE;
  67. (log << "Card - " << (bActive ? "Active" : "Not Active") << "n").Write();
  68. LogMessageIndent indent(&log);
  69. (log << tunerDevice.strFriendlyName << "n").Write();
  70. (log << "  " << tunerDevice.strDevicePath << "n").Write();
  71. device = pElement->Elements.Item(L"Demod");
  72. if (device != NULL)
  73. {
  74. attr = device->Attributes.Item(L"device");
  75. strCopy(demodDevice.strDevicePath, (attr ? attr->value : L""));
  76. attr = device->Attributes.Item(L"name");
  77. strCopy(demodDevice.strFriendlyName, (attr ? attr->value : L""));
  78. demodDevice.bValid = TRUE;
  79. (log << demodDevice.strFriendlyName << "n").Write();
  80. (log << "  " << demodDevice.strDevicePath << "n").Write();
  81. }
  82. device = pElement->Elements.Item(L"Capture");
  83. if (device != NULL)
  84. {
  85. attr = device->Attributes.Item(L"device");
  86. strCopy(captureDevice.strDevicePath, (attr ? attr->value : L""));
  87. attr = device->Attributes.Item(L"name");
  88. strCopy(captureDevice.strFriendlyName, (attr ? attr->value : L""));
  89. captureDevice.bValid = TRUE;
  90. (log << captureDevice.strFriendlyName << "n").Write();
  91. (log << "  " << captureDevice.strDevicePath << "n").Write();
  92. }
  93. return S_OK;
  94. }
  95. HRESULT BDACard::SaveToXML(XMLElement *pElement)
  96. {
  97. pElement->Attributes.Add(new XMLAttribute(L"active", (bActive ? L"1" : L"0")));
  98. pElement->Attributes.Add(new XMLAttribute(L"detected", (bDetected ? L"1" : L"0")));
  99. if (tunerDevice.bValid)
  100. {
  101. XMLElement *device;
  102. device = new XMLElement(L"Tuner");
  103. device->Attributes.Add(new XMLAttribute(L"device", tunerDevice.strDevicePath));
  104. device->Attributes.Add(new XMLAttribute(L"name", tunerDevice.strFriendlyName));
  105. pElement->Elements.Add(device);
  106. }
  107. if (demodDevice.bValid)
  108. {
  109. XMLElement *device;
  110. device = new XMLElement(L"Demod");
  111. device->Attributes.Add(new XMLAttribute(L"device", demodDevice.strDevicePath));
  112. device->Attributes.Add(new XMLAttribute(L"name", demodDevice.strFriendlyName));
  113. pElement->Elements.Add(device);
  114. }
  115. if (captureDevice.bValid)
  116. {
  117. XMLElement *device;
  118. device = new XMLElement(L"Capture");
  119. device->Attributes.Add(new XMLAttribute(L"device", captureDevice.strDevicePath));
  120. device->Attributes.Add(new XMLAttribute(L"name", captureDevice.strFriendlyName));
  121. pElement->Elements.Add(device);
  122. }
  123. return S_OK;
  124. }
  125. HRESULT BDACard::AddFilters(IGraphBuilder* piGraphBuilder)
  126. {
  127. HRESULT hr;
  128. m_piGraphBuilder = piGraphBuilder;
  129. if FAILED(hr = graphTools.AddFilterByDevicePath(m_piGraphBuilder, &m_pBDATuner, tunerDevice.strDevicePath, tunerDevice.strFriendlyName))
  130. {
  131. return (log << "Cannot load Tuner Device: " << hr << "n").Show(hr);
  132. }
  133. if (demodDevice.bValid)
  134. {
  135. if FAILED(hr = graphTools.AddFilterByDevicePath(m_piGraphBuilder, &m_pBDADemod, demodDevice.strDevicePath, demodDevice.strFriendlyName))
  136. {
  137. return (log << "Cannot load Demod Device: " << hr << "n").Show(hr);
  138. }
  139. }
  140. if (captureDevice.bValid)
  141. {
  142. if FAILED(hr = graphTools.AddFilterByDevicePath(m_piGraphBuilder, &m_pBDACapture, captureDevice.strDevicePath, captureDevice.strFriendlyName))
  143. {
  144. return (log << "Cannot load Capture Device: " << hr << "n").Show(hr);
  145. }
  146. }
  147. return S_OK;
  148. }
  149. HRESULT BDACard::RemoveFilters()
  150. {
  151. m_pCapturePin.Release();
  152. if (m_pBDACapture)
  153. {
  154. m_piGraphBuilder->RemoveFilter(m_pBDACapture);
  155. m_pBDACapture.Release();
  156. }
  157. if (m_pBDADemod)
  158. {
  159. m_piGraphBuilder->RemoveFilter(m_pBDADemod);
  160. m_pBDADemod.Release();
  161. }
  162. if (m_pBDATuner)
  163. {
  164. m_piGraphBuilder->RemoveFilter(m_pBDATuner);
  165. m_pBDATuner.Release();
  166. }
  167. return S_OK;
  168. }
  169. HRESULT BDACard::Connect(IBaseFilter* pSource)
  170. {
  171. HRESULT hr;
  172. if (!m_piGraphBuilder)
  173. return E_FAIL;
  174.     if FAILED(hr = graphTools.ConnectFilters(m_piGraphBuilder, pSource, m_pBDATuner))
  175. {
  176. return (log << "Failed to connect Network Provider to Tuner Filter: " << hr << "n").Show(hr);
  177. }
  178. IBaseFilter *piLastFilter = NULL;
  179. if (captureDevice.bValid)
  180. {
  181. if (demodDevice.bValid)
  182. {
  183. if FAILED(hr = graphTools.ConnectFilters(m_piGraphBuilder, m_pBDATuner, m_pBDADemod))
  184. {
  185. return (log << "Failed to connect Tuner Filter to Demod Filter: " << hr << "n").Show(hr);
  186. }
  187. if FAILED(hr = graphTools.ConnectFilters(m_piGraphBuilder, m_pBDADemod, m_pBDACapture))
  188. {
  189. return (log << "Failed to connect Demod Filter to Capture Filter: " << hr << "n").Show(hr);
  190. }
  191. }
  192. else
  193. {
  194. if FAILED(hr = graphTools.ConnectFilters(m_piGraphBuilder, m_pBDATuner, m_pBDACapture))
  195. {
  196. return (log << "Failed to connect Tuner Filter to Capture Filter: " << hr << "n").Show(hr);
  197. }
  198. }
  199. piLastFilter = m_pBDACapture;
  200. }
  201. else if (demodDevice.bValid)
  202. {
  203. if FAILED(hr = graphTools.ConnectFilters(m_piGraphBuilder, m_pBDATuner, m_pBDADemod))
  204. {
  205. return (log << "Failed to connect Tuner Filter to Demod Filter: " << hr << "n").Show(hr);
  206. }
  207. piLastFilter = m_pBDADemod;
  208. }
  209. else
  210. {
  211. piLastFilter = m_pBDATuner;
  212. }
  213. m_pCapturePin.Release();
  214. if FAILED(hr = graphTools.FindPinByMediaType(piLastFilter, MEDIATYPE_Stream, KSDATAFORMAT_SUBTYPE_BDA_MPEG2_TRANSPORT, &m_pCapturePin, REQUESTED_PINDIR_OUTPUT))
  215. {
  216. //If that failed then try the other mpeg2 type, but this shouldn't happen.
  217. if FAILED(hr = graphTools.FindPinByMediaType(piLastFilter, MEDIATYPE_Stream, MEDIASUBTYPE_MPEG2_TRANSPORT, &m_pCapturePin, REQUESTED_PINDIR_OUTPUT))
  218. {
  219. return (log << "Failed to find Tranport Stream pin on last filter: " << hr << "n").Show(hr);
  220. }
  221. }
  222. return S_OK;
  223. }
  224. HRESULT BDACard::GetCapturePin(IPin** pCapturePin)
  225. {
  226. if (!m_pCapturePin)
  227. return E_POINTER;
  228. *pCapturePin = m_pCapturePin;
  229. (*pCapturePin)->AddRef();
  230. return S_OK;
  231. }
  232. HRESULT BDACard::GetSignalStatistics(BOOL &locked, BOOL &present, long &strength, long &quality)
  233. {
  234. HRESULT hr;
  235. //Get IID_IBDA_Topology
  236. CComPtr <IBDA_Topology> bdaNetTop;
  237. if (FAILED(hr = m_pBDATuner.QueryInterface(&bdaNetTop)))
  238. {
  239. return (log << "Cannot Find IID_IBDA_Topologyn").Show(hr);
  240. }
  241. ULONG NodeTypes;
  242. ULONG NodeType[32];
  243. //ULONG Interfaces;
  244. //GUID Interface[32];
  245. CComPtr <IUnknown> iNode;
  246. long longVal;
  247. longVal = strength = quality = 0;
  248. BYTE byteVal;
  249. byteVal = locked = present = 0;
  250. if (FAILED(hr = bdaNetTop->GetNodeTypes(&NodeTypes, 32, NodeType)))
  251. {
  252. return (log << "Cannot get node typesn").Show(hr);
  253. }
  254. for ( int i=0 ; i<NodeTypes ; i++ )
  255. {
  256. hr = bdaNetTop->GetControlNode(0, 1, NodeType[i], &iNode);
  257. if (hr == S_OK)
  258. {
  259. CComPtr <IBDA_SignalStatistics> pSigStats;
  260. hr = iNode.QueryInterface(&pSigStats);
  261. if (hr == S_OK)
  262. {
  263. longVal = 0;
  264. if (SUCCEEDED(hr = pSigStats->get_SignalStrength(&longVal)))
  265. strength = longVal;
  266. longVal = 0;
  267. if (SUCCEEDED(hr = pSigStats->get_SignalQuality(&longVal)))
  268. quality = longVal;
  269. byteVal = 0;
  270. if (SUCCEEDED(hr = pSigStats->get_SignalLocked(&byteVal)))
  271. locked = byteVal;
  272. byteVal = 0;
  273. if (SUCCEEDED(hr = pSigStats->get_SignalPresent(&byteVal)))
  274. present = byteVal;
  275. pSigStats.Release();
  276. }
  277. iNode.Release();
  278. }
  279. }
  280. bdaNetTop.Release();
  281. return S_OK;
  282. }