PropertyPageCollection.cs
Upload User: wuming6209
Upload Date: 2013-06-06
Package Size: 161k
Code Size: 8k
Category:

Video Capture

Development Platform:

Visual C++

  1. // ------------------------------------------------------------------
  2. // DirectX.Capture
  3. //
  4. // History:
  5. // 2003-Jan-24 BL - created
  6. //
  7. // Copyright (c) 2003 Brian Low
  8. // ------------------------------------------------------------------
  9. using System;
  10. using System.Diagnostics;
  11. using System.Collections;
  12. using System.Runtime.InteropServices;
  13. using DShowNET;
  14. namespace DirectX.Capture
  15. {
  16. /// <summary>
  17. ///  A collection of available PropertyPages in a DirectShow
  18. ///  filter graph. It is up to the driver manufacturer to implement
  19. ///  a property pages on their drivers. The list of supported 
  20. ///  property pages will vary from driver to driver.
  21. /// </summary>
  22. public class PropertyPageCollection : CollectionBase, IDisposable
  23. {
  24. // --------------- Constructors / Destructor ----------------
  25. /// <summary> Initialize collection with no property pages. </summary>
  26. internal PropertyPageCollection()
  27. {
  28. InnerList.Capacity = 1;
  29. }
  30. /// <summary> Initialize collection with property pages from existing graph. </summary>
  31. internal PropertyPageCollection(
  32. ICaptureGraphBuilder2 graphBuilder, 
  33. IBaseFilter videoDeviceFilter, IBaseFilter audioDeviceFilter,
  34. IBaseFilter videoCompressorFilter, IBaseFilter audioCompressorFilter, 
  35. SourceCollection videoSources, SourceCollection audioSources)
  36. {
  37. addFromGraph( graphBuilder, 
  38. videoDeviceFilter, audioDeviceFilter,
  39. videoCompressorFilter, audioCompressorFilter,
  40. videoSources, audioSources );
  41. }
  42. /// <summary> Destructor. Release unmanaged resources. </summary>
  43. ~PropertyPageCollection()
  44. {
  45. Dispose();
  46. }
  47. // ----------------- Public Properties ------------------
  48. /// <summary> Empty the collection. </summary>
  49. public new void Clear()
  50. {
  51. for ( int c = 0; c < InnerList.Count; c++ )
  52. this[c].Dispose();
  53. InnerList.Clear();
  54. }
  55. /// <summary> Release unmanaged resources </summary>
  56. public void Dispose()
  57. {
  58. Clear();
  59. InnerList.Capacity = 1;
  60. }
  61. // ---------------- Private Methods --------------------
  62. /// <summary> Get the filter at the specified index. </summary>
  63. public PropertyPage this[int index]
  64. {
  65. get { return( (PropertyPage) InnerList[index] ); }
  66. }
  67. // ------------------ Public Methods --------------------
  68. /// <summary> Populate the collection by looking for commonly implemented property pages. </summary>
  69. protected void addFromGraph(
  70. ICaptureGraphBuilder2 graphBuilder, 
  71. IBaseFilter videoDeviceFilter, IBaseFilter audioDeviceFilter,
  72. IBaseFilter videoCompressorFilter, IBaseFilter audioCompressorFilter, 
  73. SourceCollection videoSources, SourceCollection audioSources)
  74. {
  75. object filter = null;
  76. Guid cat;
  77. Guid med;
  78. Guid iid;
  79. int hr;
  80. Trace.Assert( graphBuilder != null );
  81. // 1. the video capture filter
  82. addIfSupported( videoDeviceFilter, "Video Capture Device" );
  83. // 2. the video capture pin
  84. cat = PinCategory.Capture;
  85. med = MediaType.Interleaved; 
  86. iid = typeof(IAMStreamConfig).GUID;
  87. hr = graphBuilder.FindInterface( 
  88. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  89. if ( hr != 0 )
  90. {
  91. med = MediaType.Video ;
  92. hr = graphBuilder.FindInterface( 
  93. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  94. if ( hr != 0 )
  95. filter = null;
  96. }
  97. addIfSupported( filter, "Video Capture Pin" );
  98. // 3. the video preview pin
  99. cat = PinCategory.Preview;
  100. med = MediaType.Interleaved; 
  101. iid = typeof(IAMStreamConfig).GUID;
  102. hr = graphBuilder.FindInterface( 
  103. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  104. if ( hr != 0 )
  105. {
  106. med = MediaType.Video ;
  107. hr = graphBuilder.FindInterface( 
  108. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  109. if ( hr != 0 )
  110. filter = null;
  111. }
  112. addIfSupported( filter, "Video Preview Pin" );
  113. // 4. the video crossbar(s)
  114. ArrayList crossbars = new ArrayList();
  115. int num = 1;
  116. for ( int c = 0; c < videoSources.Count; c++ )
  117. {
  118. CrossbarSource s = videoSources[c] as CrossbarSource;
  119. if ( s != null )
  120. {
  121. if ( crossbars.IndexOf( s.Crossbar ) < 0 )
  122. {
  123. crossbars.Add( s.Crossbar );
  124. if ( addIfSupported( s.Crossbar, "Video Crossbar " + ( num==1 ? "" : num.ToString() ) ) )
  125. num++;
  126. }
  127. }
  128. }
  129. crossbars.Clear();
  130. // 5. the video compressor
  131. addIfSupported( videoCompressorFilter, "Video Compressor" );
  132. // 6. the video TV tuner
  133. cat = PinCategory.Capture;
  134. med = MediaType.Interleaved; 
  135. iid = typeof(IAMTVTuner).GUID;
  136. hr = graphBuilder.FindInterface( 
  137. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  138. if ( hr != 0 )
  139. {
  140. med = MediaType.Video ;
  141. hr = graphBuilder.FindInterface( 
  142. ref cat, ref med, videoDeviceFilter, ref iid, out filter );
  143. if ( hr != 0 )
  144. filter = null;
  145. }
  146. addIfSupported( filter, "TV Tuner" );
  147. // 7. the video compressor (VFW)
  148. IAMVfwCompressDialogs compressDialog = videoCompressorFilter as IAMVfwCompressDialogs;
  149. if ( compressDialog != null )
  150. {
  151. VfwCompressorPropertyPage page = new VfwCompressorPropertyPage( "Video Compressor", compressDialog );
  152. InnerList.Add( page );
  153. }
  154. // 8. the audio capture filter
  155. addIfSupported( audioDeviceFilter, "Audio Capture Device" );
  156. // 9. the audio capture pin
  157. cat = PinCategory.Capture;
  158. med = MediaType.Audio; 
  159. iid = typeof(IAMStreamConfig).GUID;
  160. hr = graphBuilder.FindInterface( 
  161. ref cat, ref med, audioDeviceFilter, ref iid, out filter );
  162. if ( hr != 0 )
  163. {
  164. filter = null;
  165. }
  166. addIfSupported( filter, "Audio Capture Pin" );
  167. // 9. the audio preview pin
  168. cat = PinCategory.Preview;
  169. med = MediaType.Audio; 
  170. iid = typeof(IAMStreamConfig).GUID;
  171. hr = graphBuilder.FindInterface( 
  172. ref cat, ref med, audioDeviceFilter, ref iid, out filter );
  173. if ( hr != 0 )
  174. {
  175. filter = null;
  176. }
  177. addIfSupported( filter, "Audio Preview Pin" );
  178. // 10. the audio crossbar(s)
  179. num = 1;
  180. for ( int c = 0; c < audioSources.Count; c++ )
  181. {
  182. CrossbarSource s = audioSources[c] as CrossbarSource;
  183. if ( s != null )
  184. {
  185. if ( crossbars.IndexOf( s.Crossbar ) < 0 )
  186. {
  187. crossbars.Add( s.Crossbar );
  188. if ( addIfSupported( s.Crossbar, "Audio Crossbar " + ( num==1 ? "" : num.ToString() ) ) )
  189. num++;
  190. }
  191. }
  192. }
  193. crossbars.Clear();
  194. // 11. the audio compressor
  195. addIfSupported( audioCompressorFilter, "Audio Compressor" );
  196. }
  197. /// <summary> 
  198. ///  Returns the object as an ISpecificPropertyPage
  199. ///  if the object supports the ISpecificPropertyPage
  200. ///  interface and has at least one property page.
  201. /// </summary>
  202. protected bool addIfSupported(object o, string name)
  203. {
  204. ISpecifyPropertyPages specifyPropertyPages = null;
  205. DsCAUUID cauuid = new DsCAUUID();
  206. bool wasAdded = false;
  207. // Determine if the object supports the interface
  208. // and has at least 1 property page
  209. try
  210. {
  211. specifyPropertyPages = o as ISpecifyPropertyPages;
  212. if ( specifyPropertyPages != null )
  213. {
  214. int hr = specifyPropertyPages.GetPages( out cauuid );
  215. if ( ( hr != 0 ) || ( cauuid.cElems <= 0 ) )
  216. specifyPropertyPages = null;
  217. }
  218. }
  219. finally
  220. {
  221. if( cauuid.pElems != IntPtr.Zero )
  222. Marshal.FreeCoTaskMem( cauuid.pElems );
  223. }
  224. // Add the page to the internal collection
  225. if ( specifyPropertyPages != null )
  226. {
  227. DirectShowPropertyPage p = new DirectShowPropertyPage( name, specifyPropertyPages );
  228. InnerList.Add( p );
  229. wasAdded = true;
  230. }
  231. return( wasAdded );
  232. }
  233. }
  234. }