DSHelper.cs 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices.ComTypes;
  5. using System.Diagnostics;
  6. using System.Collections.Generic;
  7. using System.Security.Permissions;
  8. using DirectShow;
  9. using System.Threading;
  10. using System.ComponentModel;
  11. using System.Reflection;
  12. using System.IO;
  13. namespace Sonic
  14. {
  15. #region Basic COM helper utilites classes
  16. [ComVisible(false)]
  17. public class COMHelper
  18. {
  19. #region BOOL
  20. public static BOOL TRUE { get { return (BOOL)1; } }
  21. public static BOOL FALSE { get { return (BOOL)0; } }
  22. #endregion
  23. #region FOURCC
  24. public static FOURCC MAKEFOURCC(char a1, char a2, char a3, char a4) { return new FOURCC(a1, a2, a3, a4); }
  25. public static FOURCC MAKEFOURCC(byte a1, byte a2, byte a3, byte a4) { return new FOURCC(a1, a2, a3, a4); }
  26. public static readonly FOURCC BI_RGB = (FOURCC)0;
  27. #endregion
  28. #region Helper Methods and constants
  29. public const int WM_NULL = 0x0000;
  30. public const int TIME_ONESHOT = 0x0000;
  31. public const int TIME_PERIODIC = 0x0001;
  32. public const int TIME_KILL_SYNCHRONOUS = 0x0100;
  33. public const object NULL = null;
  34. #if DEBUG
  35. public static void _TRACE(string _message) { if (!string.IsNullOrEmpty(_message)) Trace.WriteLine(_message); }
  36. public static void TRACE(string _message) { if (!string.IsNullOrEmpty(_message)) { _message += "\n"; API.OutputDebugString(_message); } }
  37. public static void TRACE_ENTER()
  38. {
  39. MethodBase _method = (new StackTrace(1,false)).GetFrame(0).GetMethod();
  40. TRACE(string.Format("{0}::{1}", _method.ReflectedType.Name, _method.Name));
  41. }
  42. public static void ASSERT(object _object) { if (_object is BOOL) Debug.Assert((bool)_object); if (_object is HRESULT) Debug.Assert((bool)_object); else if (_object is bool) Debug.Assert((bool)_object); else Debug.Assert(_object != null); }
  43. #else
  44. public static void _TRACE(string _message) { }
  45. public static void TRACE(string _message) { }
  46. public static void TRACE_ENTER() {}
  47. public static void ASSERT(object _object) { }
  48. #endif
  49. public static bool SUCCEEDED(int hr) { return (hr >= 0); }
  50. public static bool FAILED(int hr) { return (hr < 0); }
  51. public static int HIWORD(int _value) { return ((_value >> 16) & 0xffff); }
  52. public static int LOWORD(int _value) { return (_value & 0xffff); }
  53. public const long MAX_LONG = 0x7FFFFFFFFFFFFFFF;
  54. public const long MILLISECONDS = (1000); // 10 ^ 3
  55. public const long NANOSECONDS = (1000000000); // 10 ^ 9
  56. public const long UNITS = (NANOSECONDS / 100); // 10 ^ 7
  57. #region LCID
  58. public const int LANG_NEUTRAL = 0x00;
  59. public const int LANG_INVARIANT = 0x7f;
  60. public const int SUBLANG_NEUTRAL = 0x00;
  61. public const int SUBLANG_DEFAULT = 0x01;
  62. public const int SUBLANG_SYS_DEFAULT = 0x02;
  63. public const int SUBLANG_CUSTOM_DEFAULT = 0x03;
  64. public const int SUBLANG_CUSTOM_UNSPECIFIED = 0x04;
  65. public const int SUBLANG_UI_CUSTOM_DEFAULT = 0x05;
  66. public const int SORT_DEFAULT = 0x0;
  67. public const int SORT_INVARIANT_MATH = 0x1;
  68. public static int LANG_SYSTEM_DEFAULT { get { return (MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT)); }}
  69. public static int LANG_USER_DEFAULT { get { return (MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)); }}
  70. public static int LOCALE_SYSTEM_DEFAULT { get { return (MAKELCID(LANG_SYSTEM_DEFAULT, SORT_DEFAULT)); }}
  71. public static int LOCALE_USER_DEFAULT { get { return (MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT)); }}
  72. public static int LOCALE_CUSTOM_DEFAULT { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_DEFAULT), SORT_DEFAULT)); }}
  73. public static int LOCALE_CUSTOM_UNSPECIFIED { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_UNSPECIFIED), SORT_DEFAULT)); }}
  74. public static int LOCALE_CUSTOM_UI_DEFAULT { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_UI_CUSTOM_DEFAULT), SORT_DEFAULT)); }}
  75. public static int LOCALE_NEUTRAL { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), SORT_DEFAULT)); }}
  76. public static int LOCALE_INVARIANT { get { return (MAKELCID(MAKELANGID(LANG_INVARIANT, SUBLANG_NEUTRAL), SORT_DEFAULT)); } }
  77. public static int MAKELANGID(int p, int s) { return ((((ushort)(s)) << 10) | (ushort)(p)); }
  78. public static int PRIMARYLANGID(int lgid) { return ((ushort)(lgid) & 0x3ff); }
  79. public static int SUBLANGID(int lgid) { return ((ushort )(lgid) >> 10); }
  80. public static int MAKELCID(int lgid,int srtid) { return ((int)((((int)((ushort )(srtid))) << 16) | ((int)((ushort)(lgid))))); }
  81. public static int MAKESORTLCID(int lgid,int srtid,int ver) { return ((int)((MAKELCID(lgid, srtid)) | (((int)((ushort)(ver))) << 20))); }
  82. public static int LANGIDFROMLCID(int lcid) { return ((ushort )(lcid)); }
  83. public static int SORTIDFROMLCID(int lcid) { return ((ushort)((((int)(lcid)) >> 16) & 0xf)); }
  84. public static int SORTVERSIONFROMLCID(int lcid) { return ((ushort)((((int)(lcid)) >> 20) & 0xf)); }
  85. #endregion
  86. #region Align
  87. public static int ALIGN16(int SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
  88. public static int ALIGN32(int SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
  89. public static uint ALIGN16(uint SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
  90. public static uint ALIGN32(uint SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
  91. public static long ALIGN16(long SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
  92. public static long ALIGN32(long SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
  93. public static ulong ALIGN16(ulong SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
  94. public static ulong ALIGN32(ulong SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
  95. #endregion
  96. #endregion
  97. #region HRESULT
  98. public static HRESULT S_OK { get { return (HRESULT)0; } }
  99. public static HRESULT S_FALSE { get { return (HRESULT)1; } }
  100. public static HRESULT NOERROR { get { return S_OK; } }
  101. public static HRESULT E_INVALIDARG { get { unchecked { return (HRESULT)0x80070057; } } }
  102. public static HRESULT E_NOINTERFACE { get { unchecked { return (HRESULT)0x80004002; } } }
  103. public static HRESULT E_NOTIMPL { get { unchecked { return (HRESULT)0x80004001; } } }
  104. public static HRESULT E_UNEXPECTED { get { unchecked { return (HRESULT)0x8000FFFF; } } }
  105. public static HRESULT E_FAIL { get { unchecked { return (HRESULT)0x80004005; } } }
  106. public static HRESULT E_POINTER { get { unchecked { return (HRESULT)0x80004003; } } }
  107. public static HRESULT E_OUTOFMEMORY { get { unchecked { return (HRESULT)0x8007000E; } } }
  108. public static HRESULT E_FILE_NOT_FOUND { get { unchecked { return (HRESULT)0x80070002; } } }
  109. public static HRESULT VFW_E_NOT_FOUND { get { unchecked { return (HRESULT)0x80040216; } } }
  110. public static HRESULT VFW_E_CANNOT_CONNECT { get { unchecked { return (HRESULT)0x80040217; } } }
  111. public static HRESULT VFW_E_NOT_CONNECTED { get { unchecked { return (HRESULT)0x80040209; } } }
  112. public static HRESULT VFW_E_TYPE_NOT_ACCEPTED { get { unchecked { return (HRESULT)0x8004022A; } } }
  113. public static HRESULT VFW_E_NO_ACCEPTABLE_TYPES { get { unchecked { return (HRESULT)0x80040207; } } }
  114. public static HRESULT VFW_E_INVALID_DIRECTION { get { unchecked { return (HRESULT)0x80040208; } } }
  115. public static HRESULT VFW_E_ALREADY_CONNECTED { get { unchecked { return (HRESULT)0x80040204; } } }
  116. public static HRESULT VFW_E_NOT_STOPPED { get { unchecked { return (HRESULT)0x80040224; } } }
  117. public static HRESULT VFW_E_NO_ALLOCATOR { get { unchecked { return (HRESULT)0x8004020A; } } }
  118. public static HRESULT VFW_S_NO_MORE_ITEMS { get { unchecked { return (HRESULT)0x00040103; } } }
  119. public static HRESULT VFW_E_INVALIDMEDIATYPE { get { unchecked { return (HRESULT)0x80040200; } } }
  120. public static HRESULT VFW_E_RUNTIME_ERROR { get { unchecked { return (HRESULT)0x8004020B; } } }
  121. public static HRESULT VFW_E_WRONG_STATE { get { unchecked { return (HRESULT)0x80040227; } } }
  122. public static HRESULT VFW_E_ENUM_OUT_OF_SYNC { get { unchecked { return (HRESULT)0x80040203; } } }
  123. public static HRESULT VFW_E_NO_CLOCK { get { unchecked { return (HRESULT)0x80040213; } } }
  124. public static HRESULT VFW_E_NOT_IN_GRAPH { get { unchecked { return (HRESULT)0x8004025F; } } }
  125. public static HRESULT VFW_E_START_TIME_AFTER_END { get { unchecked { return (HRESULT)0x80040228; } } }
  126. public static HRESULT VFW_S_STATE_INTERMEDIATE { get { unchecked { return (HRESULT)0x00040237; } } }
  127. public static HRESULT VFW_E_SAMPLE_REJECTED { get { unchecked { return (HRESULT)0x8004022B; } } }
  128. public static HRESULT VFW_E_STATE_CHANGED { get { unchecked { return (HRESULT)0x80040223; } } }
  129. public static HRESULT VFW_E_SAMPLE_TIME_NOT_SET { get { unchecked { return (HRESULT)0x80040249; } } }
  130. public static HRESULT VFW_E_TIMEOUT { get { unchecked { return (HRESULT)0x8004022E; } } }
  131. public static HRESULT VFW_S_NO_STOP_TIME { get { unchecked { return (HRESULT)0x00040270; } } }
  132. #endregion
  133. #region Enum
  134. [ComVisible(false)]
  135. [Flags]
  136. public enum CLSCTX : uint
  137. {
  138. CLSCTX_INPROC_SERVER = 0x1,
  139. CLSCTX_INPROC_HANDLER = 0x2,
  140. CLSCTX_LOCAL_SERVER = 0x4,
  141. CLSCTX_INPROC_SERVER16 = 0x8,
  142. CLSCTX_REMOTE_SERVER = 0x10,
  143. CLSCTX_INPROC_HANDLER16 = 0x20,
  144. CLSCTX_RESERVED1 = 0x40,
  145. CLSCTX_RESERVED2 = 0x80,
  146. CLSCTX_RESERVED3 = 0x100,
  147. CLSCTX_RESERVED4 = 0x200,
  148. CLSCTX_NO_CODE_DOWNLOAD = 0x400,
  149. CLSCTX_RESERVED5 = 0x800,
  150. CLSCTX_NO_CUSTOM_MARSHAL = 0x1000,
  151. CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000,
  152. CLSCTX_NO_FAILURE_LOG = 0x4000,
  153. CLSCTX_DISABLE_AAA = 0x8000,
  154. CLSCTX_ENABLE_AAA = 0x10000,
  155. CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000,
  156. CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000,
  157. CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000,
  158. CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
  159. CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER,
  160. CLSCTX_ALL = CLSCTX_SERVER | CLSCTX_INPROC_HANDLER
  161. }
  162. #endregion
  163. #region API
  164. [ComVisible(false)]
  165. public static class API
  166. {
  167. [DllImport("kernel32.dll", EntryPoint = "OutputDebugStringW", CharSet = CharSet.Unicode)]
  168. public static extern void OutputDebugString(string _text);
  169. [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
  170. public static extern void CopyMemory(IntPtr Destination, IntPtr Source, [MarshalAs(UnmanagedType.U4)] int Length);
  171. [DllImport("ole32.dll")]
  172. public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);
  173. [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
  174. public static extern int MkParseDisplayName(IBindCtx pbc, string szUserName, ref int pchEaten, out IMoniker ppmk);
  175. [DllImport("olepro32.dll")]
  176. public static extern int OleCreatePropertyFrame(
  177. IntPtr hwndOwner,
  178. int x,
  179. int y,
  180. [MarshalAs(UnmanagedType.LPWStr)] string lpszCaption,
  181. int cObjects,
  182. [MarshalAs(UnmanagedType.Interface, ArraySubType = UnmanagedType.IUnknown)]
  183. ref object ppUnk,
  184. int cPages,
  185. IntPtr lpPageClsID,
  186. int lcid,
  187. int dwReserved,
  188. IntPtr lpvReserved
  189. );
  190. [DllImport("ole32.dll")]
  191. public static extern int CoCreateInstance(
  192. [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
  193. IntPtr pUnkOuter,
  194. CLSCTX dwClsContext,
  195. [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
  196. out IntPtr rReturnedComObject
  197. );
  198. }
  199. #endregion
  200. }
  201. [ComVisible(false)]
  202. public sealed class BOOL : COMHelper,IComparable,ICloneable
  203. {
  204. #region Variables
  205. private bool m_Result = false;
  206. #endregion
  207. #region Constructor
  208. public BOOL()
  209. {
  210. }
  211. public BOOL(int _value)
  212. {
  213. m_Result = _value == 0 ? false : true;
  214. }
  215. public BOOL(bool _value)
  216. {
  217. m_Result = _value;
  218. }
  219. public BOOL(BOOL _value)
  220. : this(_value.m_Result)
  221. {
  222. }
  223. #endregion
  224. #region IComparable Members
  225. public int CompareTo(object obj)
  226. {
  227. if (obj is BOOL)
  228. {
  229. return m_Result.CompareTo(((BOOL)obj).m_Result);
  230. }
  231. return m_Result.CompareTo(obj);
  232. }
  233. #endregion
  234. #region ICloneable Members
  235. public object Clone()
  236. {
  237. return new BOOL(m_Result);
  238. }
  239. #endregion
  240. #region Overridden Methods
  241. public override bool Equals(object obj)
  242. {
  243. if (obj is BOOL)
  244. {
  245. return m_Result.Equals(((BOOL)obj).m_Result);
  246. }
  247. return m_Result.Equals(obj);
  248. }
  249. public override int GetHashCode()
  250. {
  251. return m_Result.GetHashCode();
  252. }
  253. public override string ToString()
  254. {
  255. return m_Result.ToString();
  256. }
  257. #endregion
  258. #region Methods
  259. public void Assert()
  260. {
  261. ASSERT(m_Result);
  262. }
  263. #endregion
  264. #region Static Methods
  265. public static new BOOL TRUE
  266. {
  267. get { return COMHelper.TRUE; }
  268. }
  269. public static new BOOL FALSE
  270. {
  271. get { return COMHelper.FALSE; }
  272. }
  273. #endregion
  274. #region Operators
  275. public static bool operator ==(BOOL _src, BOOL _dest)
  276. {
  277. if (System.Object.ReferenceEquals(_src, _dest))
  278. {
  279. return true;
  280. }
  281. if ((_src as Object) == (_dest as Object)) return true;
  282. if ((_src as Object) != null && (_dest as Object) != null)
  283. {
  284. return _src.m_Result == _dest.m_Result;
  285. }
  286. return false;
  287. }
  288. public static bool operator !=(BOOL _src, BOOL _dest)
  289. {
  290. return !(_src == _dest);
  291. }
  292. public static bool operator ==(BOOL _src, HRESULT _dest)
  293. {
  294. return _src == (BOOL)_dest;
  295. }
  296. public static bool operator !=(BOOL _src, HRESULT _dest)
  297. {
  298. return !(_src == _dest);
  299. }
  300. public static implicit operator bool(BOOL _bool)
  301. {
  302. if ((_bool as Object) == null) return false;
  303. return _bool.m_Result;
  304. }
  305. public static implicit operator int(BOOL _bool)
  306. {
  307. if ((_bool as Object) == null) return 0;
  308. return _bool.m_Result ? 1 : 0;
  309. }
  310. public static implicit operator HRESULT(BOOL _bool)
  311. {
  312. if ((_bool as Object) == null) return S_FALSE;
  313. return _bool.m_Result ? S_OK : S_FALSE;
  314. }
  315. public static explicit operator BOOL(HRESULT hr)
  316. {
  317. if ((hr as Object) == null) return FALSE;
  318. return new BOOL(hr == S_OK);
  319. }
  320. public static explicit operator BOOL(int _value)
  321. {
  322. return new BOOL(_value != 0);
  323. }
  324. public static explicit operator BOOL(bool _bool)
  325. {
  326. return new BOOL(_bool);
  327. }
  328. #endregion
  329. }
  330. [ComVisible(false)]
  331. public class HRESULT : COMHelper,IComparable,ICloneable
  332. {
  333. #region Variables
  334. private int m_Result = 0;
  335. #endregion
  336. #region Constructor
  337. public HRESULT()
  338. {
  339. }
  340. public HRESULT(int hr)
  341. {
  342. m_Result = hr;
  343. }
  344. public HRESULT(HRESULT hr)
  345. : this(hr.m_Result)
  346. {
  347. }
  348. #endregion
  349. #region Properties
  350. public bool Failed
  351. {
  352. get { return FAILED(m_Result); }
  353. }
  354. public bool Succeeded
  355. {
  356. get { return SUCCEEDED(m_Result); }
  357. }
  358. public string Text
  359. {
  360. get { return GetErrorText(); }
  361. }
  362. #endregion
  363. #region IComparable Members
  364. public int CompareTo(object obj)
  365. {
  366. if (obj is HRESULT)
  367. {
  368. return m_Result.CompareTo(((HRESULT)obj).m_Result);
  369. }
  370. return m_Result.CompareTo(obj);
  371. }
  372. #endregion
  373. #region ICloneable Members
  374. public object Clone()
  375. {
  376. return new HRESULT(m_Result);
  377. }
  378. #endregion
  379. #region Overridden Methods
  380. public override bool Equals(object obj)
  381. {
  382. if (obj is HRESULT)
  383. {
  384. return m_Result.Equals(((HRESULT)obj).m_Result);
  385. }
  386. return m_Result.Equals(obj);
  387. }
  388. public override int GetHashCode()
  389. {
  390. return m_Result.GetHashCode();
  391. }
  392. public override string ToString()
  393. {
  394. string _text = GetErrorText();
  395. if (_text != "")
  396. {
  397. _text = " ( " + _text + " )";
  398. }
  399. _text = "0x" + m_Result.ToString("x8") + _text;
  400. return _text;
  401. }
  402. #endregion
  403. #region Virtual Methods
  404. protected virtual string GetErrorText()
  405. {
  406. string _text = "";
  407. uint _length = 160;
  408. IntPtr _ptr = Marshal.AllocCoTaskMem((int)_length);
  409. try
  410. {
  411. if (AMGetErrorText(m_Result, _ptr, _length) > 0)
  412. {
  413. _text = Marshal.PtrToStringAuto(_ptr);
  414. }
  415. }
  416. catch
  417. {
  418. }
  419. Marshal.FreeCoTaskMem(_ptr);
  420. return _text;
  421. }
  422. #endregion
  423. #region Methods
  424. public void Assert()
  425. {
  426. ASSERT(SUCCEEDED(m_Result));
  427. }
  428. public void Throw()
  429. {
  430. if (FAILED(m_Result))
  431. {
  432. Marshal.ThrowExceptionForHR(m_Result);
  433. }
  434. }
  435. public void TraceWrite()
  436. {
  437. if (Failed)
  438. {
  439. StackFrame _frame = (new StackTrace(1, true)).GetFrame(0);
  440. MethodBase _method = _frame.GetMethod();
  441. string _file = _frame.GetFileName();
  442. _file = !string.IsNullOrEmpty(_file) ? Path.GetFileName(_file) : "";
  443. TRACE(string.Format("-- ASSERT -- Method {0}::{1}, HRESULT : {2},File : {3} Line : {4} ",
  444. _method.ReflectedType.Name,
  445. _method.Name,
  446. ToString(),
  447. _file,
  448. _frame.GetFileLineNumber()
  449. ));
  450. }
  451. }
  452. #endregion
  453. #region Imports
  454. [DllImport("quartz.dll", EntryPoint = "AMGetErrorText", CharSet = CharSet.Auto)]
  455. private static extern uint AMGetErrorText(int hr,IntPtr sText,[In] uint _length);
  456. #endregion
  457. #region Operators
  458. public static bool operator ==(HRESULT _src, HRESULT _dest)
  459. {
  460. if (System.Object.ReferenceEquals(_src, _dest))
  461. {
  462. return true;
  463. }
  464. if ((_src as Object) == (_dest as Object)) return true;
  465. if ((_src as Object) != null && (_dest as Object) != null)
  466. {
  467. return _src.m_Result == _dest.m_Result;
  468. }
  469. return false;
  470. }
  471. public static bool operator !=(HRESULT _src, HRESULT _dest)
  472. {
  473. return !(_src == _dest);
  474. }
  475. public static implicit operator bool(HRESULT hr)
  476. {
  477. if ((hr as Object) == null) return true;
  478. return (SUCCEEDED(hr.m_Result));
  479. }
  480. public static implicit operator int(HRESULT hr)
  481. {
  482. if ((hr as Object) == null) return 0;
  483. return hr.m_Result;
  484. }
  485. public static explicit operator HRESULT(int hr)
  486. {
  487. return new HRESULT(hr);
  488. }
  489. public static explicit operator HRESULT(uint hr)
  490. {
  491. unchecked
  492. {
  493. return new HRESULT((int)hr);
  494. }
  495. }
  496. #endregion
  497. }
  498. [ComVisible(false)]
  499. public sealed class FOURCC : COMHelper, IComparable, ICloneable
  500. {
  501. #region Constants
  502. private const ushort m_usB = 0x0000;
  503. private const ushort m_usC = 0x0010;
  504. private static readonly byte[] m_abtD = { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
  505. #endregion
  506. #region Variables
  507. private uint m_uiFourcc = 0;
  508. #endregion
  509. #region Constructors
  510. public FOURCC(int _value)
  511. {
  512. unchecked
  513. {
  514. m_uiFourcc = (uint)_value;
  515. }
  516. }
  517. public FOURCC(uint _value)
  518. {
  519. m_uiFourcc = _value;
  520. }
  521. public FOURCC(Guid _guid)
  522. {
  523. byte[] _data = _guid.ToByteArray();
  524. byte[] _result = { _data[0], _data[1], _data[2], _data[3] };
  525. m_uiFourcc = BitConverter.ToUInt32(_result, 0);
  526. }
  527. public FOURCC(string _value)
  528. {
  529. byte[] _data = new byte[4];
  530. for (int i = 0; i < _data.Length; i++)
  531. {
  532. if (i < _value.Length)
  533. {
  534. _data[i] = (byte)_value[i];
  535. }
  536. else
  537. {
  538. _data[i] = 0;
  539. }
  540. }
  541. m_uiFourcc = BitConverter.ToUInt32(_data, 0);
  542. }
  543. public FOURCC(byte[] _value)
  544. {
  545. byte[] _data = new byte[4];
  546. for (int i = 0; i < _data.Length; i++)
  547. {
  548. if (i < _value.Length)
  549. {
  550. _data[i] = _value[i];
  551. }
  552. else
  553. {
  554. _data[i] = 0;
  555. }
  556. }
  557. m_uiFourcc = BitConverter.ToUInt32(_data,0);
  558. }
  559. public FOURCC(char[] _value)
  560. {
  561. byte[] _data = new byte[4];
  562. for (int i = 0; i < _data.Length; i++)
  563. {
  564. if (i < _value.Length)
  565. {
  566. _data[i] = (byte)_value[i];
  567. }
  568. else
  569. {
  570. _data[i] = 0;
  571. }
  572. }
  573. m_uiFourcc = BitConverter.ToUInt32(_data, 0);
  574. }
  575. public FOURCC(char a1, char a2, char a3, char a4)
  576. {
  577. byte[] _data = new byte[] { (byte)a1, (byte)a2, (byte)a3, (byte)a4 };
  578. m_uiFourcc = BitConverter.ToUInt32(_data, 0);
  579. }
  580. public FOURCC(byte a1,byte a2,byte a3,byte a4)
  581. {
  582. byte[] _data = new byte[] { a1,a2,a3,a4 };
  583. m_uiFourcc = BitConverter.ToUInt32(_data, 0);
  584. }
  585. #endregion
  586. #region Overridden Methods
  587. public override bool Equals(object obj)
  588. {
  589. if (obj is FOURCC)
  590. {
  591. return m_uiFourcc.Equals(((FOURCC)obj).m_uiFourcc);
  592. }
  593. return m_uiFourcc.Equals(obj);
  594. }
  595. public override int GetHashCode()
  596. {
  597. return m_uiFourcc.GetHashCode();
  598. }
  599. public override string ToString()
  600. {
  601. byte[] _data = BitConverter.GetBytes(m_uiFourcc);
  602. string _result = " ( ";
  603. for (int i = 0; i < _data.Length; i++)
  604. {
  605. if (_data[i] <= 30 || _data[i] > 128)
  606. {
  607. _result = "";
  608. break;
  609. }
  610. else
  611. {
  612. _result += (char)_data[i];
  613. }
  614. }
  615. if (_result != "")
  616. {
  617. _result += " )";
  618. }
  619. return m_uiFourcc.ToString("x4") + _result;
  620. }
  621. #endregion
  622. #region IComparable Members
  623. public int CompareTo(object obj)
  624. {
  625. if (obj is FOURCC)
  626. {
  627. return m_uiFourcc.CompareTo(((FOURCC)obj).m_uiFourcc);
  628. }
  629. return m_uiFourcc.CompareTo(obj);
  630. }
  631. #endregion
  632. #region ICloneable Members
  633. public object Clone()
  634. {
  635. return new FOURCC(m_uiFourcc);
  636. }
  637. #endregion
  638. #region Operators
  639. public static bool operator ==(FOURCC _src, FOURCC _dest)
  640. {
  641. if (System.Object.ReferenceEquals(_src, _dest))
  642. {
  643. return true;
  644. }
  645. if ((_src as Object) == (_dest as Object)) return true;
  646. if ((_src as Object) != null && (_dest as Object) != null)
  647. {
  648. return _src.m_uiFourcc == _dest.m_uiFourcc;
  649. }
  650. return false;
  651. }
  652. public static bool operator !=(FOURCC _src, FOURCC _dest)
  653. {
  654. return !(_src == _dest);
  655. }
  656. public static explicit operator FOURCC(int _value)
  657. {
  658. return new FOURCC(_value);
  659. }
  660. public static explicit operator FOURCC(uint _value)
  661. {
  662. return new FOURCC(_value);
  663. }
  664. public static explicit operator FOURCC(Guid _value)
  665. {
  666. return new FOURCC(_value);
  667. }
  668. public static explicit operator FOURCC(string _value)
  669. {
  670. if (_value == null) return new FOURCC(0);
  671. return new FOURCC(_value);
  672. }
  673. public static explicit operator FOURCC(Array _value)
  674. {
  675. if (_value == null) return new FOURCC(0);
  676. byte[] _data = new byte[4];
  677. for (int i = 0; i < _data.Length; i++)
  678. {
  679. if (i < _value.Length)
  680. {
  681. _data[i] = (byte)Convert.ToByte(_value.GetValue(i));
  682. }
  683. else
  684. {
  685. _data[i] = 0;
  686. }
  687. }
  688. return new FOURCC(_data);
  689. }
  690. public static implicit operator int(FOURCC _fcc)
  691. {
  692. if ((_fcc as Object) == null) return 0;
  693. unchecked
  694. {
  695. return (int)_fcc.m_uiFourcc;
  696. }
  697. }
  698. public static implicit operator uint(FOURCC _fcc)
  699. {
  700. if ((_fcc as Object) == null) return 0;
  701. return _fcc.m_uiFourcc;
  702. }
  703. public static implicit operator byte[](FOURCC _fcc)
  704. {
  705. if ((_fcc as Object) == null) return null;
  706. byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
  707. byte[] _result = new byte[_data.Length];
  708. for (int i = 0; i < _data.Length; i++)
  709. {
  710. _result[3 - i] = _data[i];
  711. }
  712. return _result;
  713. }
  714. public static implicit operator char[](FOURCC _fcc)
  715. {
  716. if ((_fcc as Object) == null) return null;
  717. byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
  718. char[] _result = new char[_data.Length];
  719. for (int i = 0; i < _data.Length; i++)
  720. {
  721. _result[3 - i] = (char)_data[i];
  722. }
  723. return _result;
  724. }
  725. public static implicit operator string(FOURCC _fcc)
  726. {
  727. if ((_fcc as Object) == null) return "";
  728. byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
  729. string _result = "";
  730. for (int i = 0; i < _data.Length; i++)
  731. {
  732. _result += (char)_data[i];
  733. }
  734. return _result;
  735. }
  736. public static implicit operator Guid(FOURCC _fcc)
  737. {
  738. if ((_fcc as Object) == null) return Guid.Empty;
  739. return new Guid(_fcc.m_uiFourcc, m_usB, m_usC, m_abtD[0], m_abtD[1], m_abtD[2], m_abtD[3], m_abtD[4], m_abtD[5], m_abtD[6], m_abtD[7]);
  740. }
  741. #endregion
  742. }
  743. [ComVisible(false)]
  744. [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  745. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  746. public class VTableInterface : COMHelper, IDisposable
  747. {
  748. #region Delegates
  749. private delegate int QueryInterfaceProc(
  750. IntPtr pUnk,
  751. ref Guid riid,
  752. out IntPtr ppvObject
  753. );
  754. private delegate uint AddRefProc(IntPtr pUnk);
  755. #endregion
  756. #region Variables
  757. protected IntPtr m_pUnknown = IntPtr.Zero;
  758. protected bool m_bShouldRelease = false;
  759. #endregion
  760. #region Properties
  761. public IntPtr UnknownPtr
  762. {
  763. get { return m_pUnknown; }
  764. }
  765. public bool IsValid
  766. {
  767. get { return m_pUnknown != IntPtr.Zero; }
  768. }
  769. #endregion
  770. #region Constructor
  771. protected VTableInterface()
  772. : this(IntPtr.Zero, false)
  773. {
  774. }
  775. protected VTableInterface(IntPtr pUnknown, Type _type)
  776. : this(pUnknown, _type.GUID)
  777. {
  778. }
  779. protected VTableInterface(IntPtr pUnknown, Guid _guid)
  780. : this(IntPtr.Zero, false)
  781. {
  782. HRESULT hr = (HRESULT)Marshal.QueryInterface(pUnknown, ref _guid, out m_pUnknown);
  783. if (hr.Succeeded)
  784. {
  785. Marshal.Release(pUnknown);
  786. }
  787. }
  788. protected VTableInterface(IntPtr pUnknown)
  789. : this(pUnknown, false)
  790. {
  791. }
  792. protected VTableInterface(IntPtr pUnknown, bool bAddRef)
  793. {
  794. if (pUnknown != IntPtr.Zero)
  795. {
  796. m_pUnknown = pUnknown;
  797. if (bAddRef)
  798. {
  799. m_bShouldRelease = true;
  800. _AddRef();
  801. }
  802. }
  803. }
  804. protected VTableInterface(object _object, Guid _guid)
  805. : this(IntPtr.Zero, false)
  806. {
  807. if (_object != null)
  808. {
  809. IntPtr pUnknown = Marshal.GetIUnknownForObject(_object);
  810. HRESULT hr = (HRESULT)Marshal.QueryInterface(pUnknown, ref _guid, out m_pUnknown);
  811. if (hr.Succeeded)
  812. {
  813. Marshal.Release(pUnknown);
  814. }
  815. }
  816. }
  817. protected VTableInterface(object _object, Type _type)
  818. : this(_object, _type.GUID)
  819. {
  820. }
  821. ~VTableInterface()
  822. {
  823. Dispose();
  824. }
  825. #endregion
  826. #region IUnknown
  827. public HRESULT _QueryInterface(ref Guid riid, out IntPtr ppvObject)
  828. {
  829. ppvObject = IntPtr.Zero;
  830. if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;
  831. QueryInterfaceProc _Proc = GetProcDelegate<QueryInterfaceProc>(0);
  832. if (_Proc == null) return E_UNEXPECTED;
  833. return (HRESULT)_Proc(
  834. m_pUnknown,
  835. ref riid,
  836. out ppvObject
  837. );
  838. }
  839. public uint _AddRef()
  840. {
  841. if (m_pUnknown == IntPtr.Zero) return 0;
  842. AddRefProc _Proc = GetProcDelegate<AddRefProc>(1);
  843. if (_Proc == null) return 0;
  844. return _Proc(m_pUnknown);
  845. }
  846. public uint _Release()
  847. {
  848. if (m_pUnknown == IntPtr.Zero) return 0;
  849. AddRefProc _Proc = GetProcDelegate<AddRefProc>(2);
  850. if (_Proc == null) return 0;
  851. return _Proc(m_pUnknown);
  852. }
  853. public T _GetObject<T>() where T : class
  854. {
  855. Guid _guid = typeof(T).GUID;
  856. IntPtr _ptr;
  857. if (SUCCEEDED(_QueryInterface(ref _guid, out _ptr)))
  858. {
  859. _Release();
  860. return (T)Marshal.GetObjectForIUnknown(_ptr);
  861. }
  862. return null;
  863. }
  864. #endregion
  865. #region Methods
  866. public HRESULT IsInterface(Guid _guid)
  867. {
  868. IntPtr pvObject;
  869. HRESULT hr = _QueryInterface(ref _guid, out pvObject);
  870. if (hr.Succeeded)
  871. {
  872. _Release();
  873. }
  874. return hr;
  875. }
  876. public HRESULT IsInterface(Type _type)
  877. {
  878. return IsInterface(_type.GUID);
  879. }
  880. public T GetInterfaceImpl<T>(Guid _guid) where T : VTableInterface, new()
  881. {
  882. IntPtr pUnknown;
  883. if (SUCCEEDED(_QueryInterface(ref _guid,out pUnknown)))
  884. {
  885. T pT = new T();
  886. VTableInterface _interface = (VTableInterface)pT;
  887. _interface.m_pUnknown = pUnknown;
  888. return pT;
  889. }
  890. return null;
  891. }
  892. public T GetInterfaceImpl<T>(Type _type) where T : VTableInterface, new()
  893. {
  894. return GetInterfaceImpl<T>(_type.GUID);
  895. }
  896. public T GetInterfaceImpl<T>() where T : VTableInterface, new()
  897. {
  898. try
  899. {
  900. return GetInterfaceImpl<T>(typeof(T));
  901. }
  902. catch
  903. {
  904. }
  905. return null;
  906. }
  907. #endregion
  908. #region Helper Methods
  909. protected T TryCast<T>() where T : class
  910. {
  911. try
  912. {
  913. T _interface = (T)Marshal.GetTypedObjectForIUnknown(m_pUnknown, typeof(T));
  914. return _interface;
  915. }
  916. catch
  917. {
  918. }
  919. return null;
  920. }
  921. protected T GetProcDelegate<T>(int nIndex) where T : class
  922. {
  923. IntPtr pVtable = Marshal.ReadIntPtr(m_pUnknown);
  924. IntPtr pFunc = Marshal.ReadIntPtr(pVtable, nIndex * IntPtr.Size);
  925. return (Marshal.GetDelegateForFunctionPointer(pFunc, typeof(T))) as T;
  926. }
  927. #endregion
  928. #region IDisposable Members
  929. public void Dispose()
  930. {
  931. if (m_pUnknown != IntPtr.Zero)
  932. {
  933. if (m_bShouldRelease) _Release();
  934. m_pUnknown = IntPtr.Zero;
  935. }
  936. }
  937. #endregion
  938. }
  939. [ComVisible(false)]
  940. [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  941. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  942. public class IUnknownImpl : VTableInterface
  943. {
  944. #region Constructor
  945. public IUnknownImpl(IntPtr pUnknown)
  946. : base(pUnknown,false)
  947. {
  948. }
  949. #endregion
  950. #region Methods
  951. public new T GetProcDelegate<T>(int nIndex) where T : class
  952. {
  953. return base.GetProcDelegate<T>(nIndex);
  954. }
  955. #endregion
  956. }
  957. #endregion
  958. #region Base COM object
  959. [ComVisible(false)]
  960. [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  961. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  962. public class DSObject<T> : COMHelper, IDisposable where T : class
  963. {
  964. #region Variables
  965. protected T m_pUnknown = null;
  966. protected bool m_bReleaseOnDestroy = false;
  967. #endregion
  968. #region Properties
  969. public T Value
  970. {
  971. get { return m_pUnknown; }
  972. set { m_pUnknown = value; }
  973. }
  974. public bool IsReleased
  975. {
  976. get { return m_pUnknown == null; }
  977. }
  978. public bool IsValid
  979. {
  980. get { return !IsReleased; }
  981. }
  982. public bool ReleaseOnDestroy
  983. {
  984. get { return m_bReleaseOnDestroy; }
  985. set { m_bReleaseOnDestroy = value; }
  986. }
  987. public Guid GUID
  988. {
  989. get { return typeof(T).GUID; }
  990. }
  991. public object this[Type _type]
  992. {
  993. get { return QueryInterface(_type); }
  994. }
  995. #endregion
  996. #region Constructor
  997. public DSObject()
  998. {
  999. Guid _guid = this.GetType().GUID;
  1000. if (_guid != Guid.Empty)
  1001. {
  1002. IntPtr _ptr;
  1003. HRESULT hr = (HRESULT)API.CoCreateInstance(
  1004. _guid,
  1005. IntPtr.Zero,
  1006. CLSCTX.CLSCTX_INPROC_SERVER,
  1007. typeof(T).GUID,
  1008. out _ptr);
  1009. if (hr.Succeeded)
  1010. {
  1011. m_pUnknown = Marshal.GetObjectForIUnknown(_ptr) as T;
  1012. m_bReleaseOnDestroy = true;
  1013. }
  1014. }
  1015. }
  1016. public DSObject(Guid _guid)
  1017. {
  1018. if (_guid != Guid.Empty)
  1019. {
  1020. IntPtr _ptr;
  1021. HRESULT hr = (HRESULT)API.CoCreateInstance(
  1022. _guid,
  1023. IntPtr.Zero,
  1024. CLSCTX.CLSCTX_INPROC_SERVER,
  1025. typeof(T).GUID,
  1026. out _ptr);
  1027. if (hr.Succeeded)
  1028. {
  1029. m_pUnknown = Marshal.GetObjectForIUnknown(_ptr) as T;
  1030. m_bReleaseOnDestroy = true;
  1031. }
  1032. }
  1033. }
  1034. protected DSObject(IntPtr _ptr)
  1035. {
  1036. if (_ptr != IntPtr.Zero)
  1037. {
  1038. m_pUnknown = (T)Marshal.GetObjectForIUnknown(_ptr);
  1039. m_bReleaseOnDestroy = true;
  1040. }
  1041. }
  1042. protected DSObject(T _unknown)
  1043. {
  1044. m_pUnknown = _unknown;
  1045. m_bReleaseOnDestroy = true;
  1046. }
  1047. protected DSObject(T _unknown, bool bReleaseOnDestroy)
  1048. : this(_unknown)
  1049. {
  1050. m_bReleaseOnDestroy = bReleaseOnDestroy;
  1051. }
  1052. ~DSObject()
  1053. {
  1054. Dispose();
  1055. }
  1056. #endregion
  1057. #region Overriden Methods
  1058. public override bool Equals(object obj)
  1059. {
  1060. if (obj == m_pUnknown)
  1061. {
  1062. return true;
  1063. }
  1064. if (obj is DSObject<T>)
  1065. {
  1066. return (obj as DSObject<T>).m_pUnknown == m_pUnknown;
  1067. }
  1068. return base.Equals(obj);
  1069. }
  1070. public override int GetHashCode()
  1071. {
  1072. if (m_pUnknown != null) return m_pUnknown.GetHashCode();
  1073. return base.GetHashCode();
  1074. }
  1075. #endregion
  1076. #region IDisposable Members
  1077. public virtual void Dispose()
  1078. {
  1079. if (m_bReleaseOnDestroy && m_pUnknown != null)
  1080. {
  1081. if (Marshal.IsComObject(m_pUnknown))
  1082. {
  1083. Marshal.ReleaseComObject(m_pUnknown);
  1084. }
  1085. m_pUnknown = null;
  1086. }
  1087. }
  1088. #endregion
  1089. #region Public Methods
  1090. public object QueryInterface(Guid _guid)
  1091. {
  1092. object _object = null;
  1093. if (m_pUnknown != null)
  1094. {
  1095. IntPtr _this = Marshal.GetIUnknownForObject(m_pUnknown);
  1096. IntPtr _interface = IntPtr.Zero;
  1097. try
  1098. {
  1099. int hr = Marshal.QueryInterface(_this, ref _guid, out _interface);
  1100. if (SUCCEEDED(hr) && _interface != IntPtr.Zero)
  1101. {
  1102. _object = Marshal.GetObjectForIUnknown(_interface);
  1103. }
  1104. }
  1105. finally
  1106. {
  1107. Marshal.Release(_this);
  1108. if (_interface != IntPtr.Zero) Marshal.Release(_interface);
  1109. }
  1110. }
  1111. return _object;
  1112. }
  1113. public object QueryInterface(Type _type)
  1114. {
  1115. return QueryInterface(_type.GUID);
  1116. }
  1117. public bool IsSupported(Guid _guid)
  1118. {
  1119. if (m_pUnknown != null)
  1120. {
  1121. IntPtr _this = Marshal.GetIUnknownForObject(m_pUnknown);
  1122. IntPtr _interface = IntPtr.Zero;
  1123. try
  1124. {
  1125. return SUCCEEDED(Marshal.QueryInterface(_this, ref _guid, out _interface));
  1126. }
  1127. finally
  1128. {
  1129. Marshal.Release(_this);
  1130. if (_interface != IntPtr.Zero)
  1131. {
  1132. Marshal.Release(_interface);
  1133. }
  1134. }
  1135. }
  1136. return false;
  1137. }
  1138. public bool IsSupported(Type _type)
  1139. {
  1140. Guid _guid = _type.GUID;
  1141. if (_guid != Guid.Empty)
  1142. {
  1143. return IsSupported(_guid);
  1144. }
  1145. return false;
  1146. }
  1147. #endregion
  1148. #region Operators
  1149. public static bool operator ==(DSObject<T> _src, DSObject<T> _dest)
  1150. {
  1151. if (System.Object.ReferenceEquals(_src, _dest))
  1152. {
  1153. return true;
  1154. }
  1155. if ((_src as Object) == (_dest as Object)) return true;
  1156. if (((_src as Object) == null || _src.m_pUnknown == null)
  1157. && ((_dest as Object) == null || _dest.m_pUnknown == null))
  1158. {
  1159. return true;
  1160. }
  1161. if ((_src as Object) != null && (_dest as Object) != null)
  1162. {
  1163. return _src.m_pUnknown == _dest.m_pUnknown;
  1164. }
  1165. return false;
  1166. }
  1167. public static bool operator !=(DSObject<T> _src, DSObject<T> _dest)
  1168. {
  1169. return !(_src == _dest);
  1170. }
  1171. public static implicit operator T(DSObject<T> _object)
  1172. {
  1173. if (((object)_object) == null)
  1174. {
  1175. return null;
  1176. }
  1177. return _object.m_pUnknown;
  1178. }
  1179. public static implicit operator IntPtr(DSObject<T> _object)
  1180. {
  1181. if (((_object as Object) == null) || _object.m_pUnknown == null)
  1182. {
  1183. return IntPtr.Zero;
  1184. }
  1185. return Marshal.GetIUnknownForObject(_object.m_pUnknown);
  1186. }
  1187. public static implicit operator bool(DSObject<T> _object)
  1188. {
  1189. return (((object)_object) != null && _object.m_pUnknown != null);
  1190. }
  1191. public static explicit operator DSObject<T>(IntPtr _ptr)
  1192. {
  1193. return new DSObject<T>(_ptr);
  1194. }
  1195. public static explicit operator DSObject<T>(T _object)
  1196. {
  1197. return new DSObject<T>(_object);
  1198. }
  1199. #endregion
  1200. }
  1201. #endregion
  1202. #region Base Moniker Enumerator object
  1203. [ComVisible(false)]
  1204. public class MonikerInfo : DSObject<IMoniker>
  1205. {
  1206. #region Fields
  1207. public Guid ClassID
  1208. {
  1209. get
  1210. {
  1211. Guid _guid = Guid.Empty;
  1212. if (m_pUnknown != null)
  1213. {
  1214. m_pUnknown.GetClassID(out _guid);
  1215. }
  1216. return _guid;
  1217. }
  1218. }
  1219. public string Name
  1220. {
  1221. get { return GetPropBagValue("FriendlyName"); }
  1222. }
  1223. public string DevicePath
  1224. {
  1225. get
  1226. {
  1227. string _out = "";
  1228. if (m_pUnknown != null)
  1229. {
  1230. m_pUnknown.GetDisplayName(null, null, out _out);
  1231. }
  1232. return _out;
  1233. }
  1234. }
  1235. public string this[string _value]
  1236. {
  1237. get { return GetPropBagValue(_value); }
  1238. }
  1239. #endregion
  1240. #region Constructor
  1241. public MonikerInfo()
  1242. : base(IntPtr.Zero)
  1243. {
  1244. }
  1245. public MonikerInfo(IMoniker _moniker)
  1246. : base(_moniker)
  1247. {
  1248. }
  1249. public MonikerInfo(string _moniker)
  1250. {
  1251. IBindCtx _context = null;
  1252. try
  1253. {
  1254. if (SUCCEEDED(API.CreateBindCtx(0, out _context)))
  1255. {
  1256. int n = 0;
  1257. ASSERT(SUCCEEDED(API.MkParseDisplayName(_context, _moniker, ref n, out m_pUnknown)));
  1258. }
  1259. }
  1260. finally
  1261. {
  1262. if (_context != null)
  1263. {
  1264. Marshal.ReleaseComObject(_context);
  1265. }
  1266. }
  1267. }
  1268. #endregion
  1269. #region Public Methods
  1270. public object Bind(Type _type)
  1271. {
  1272. object _object = null;
  1273. if (m_pUnknown != null)
  1274. {
  1275. Guid _guid = _type.GUID;
  1276. if (_guid != Guid.Empty)
  1277. {
  1278. try
  1279. {
  1280. m_pUnknown.BindToObject(null, null, ref _guid, out _object);
  1281. }
  1282. catch
  1283. {
  1284. _object = null;
  1285. }
  1286. }
  1287. }
  1288. return _object;
  1289. }
  1290. #endregion
  1291. #region Protected Methods
  1292. protected string GetPropBagValue(string sPropName)
  1293. {
  1294. if (m_pUnknown == null) return null;
  1295. IPropertyBag _property = null;
  1296. try
  1297. {
  1298. object _object = null;
  1299. Guid _guid = typeof(IPropertyBag).GUID;
  1300. m_pUnknown.BindToStorage(null, null, ref _guid, out _object);
  1301. _property = (IPropertyBag)_object;
  1302. object _value;
  1303. int hr = _property.Read(sPropName, out _value, null);
  1304. ASSERT(hr == S_OK);
  1305. if (SUCCEEDED(hr))
  1306. {
  1307. return _value as string;
  1308. }
  1309. }
  1310. catch
  1311. {
  1312. }
  1313. finally
  1314. {
  1315. if (_property != null)
  1316. {
  1317. Marshal.ReleaseComObject(_property);
  1318. _property = null;
  1319. }
  1320. }
  1321. return null;
  1322. }
  1323. #endregion
  1324. }
  1325. [ComVisible(false)]
  1326. public class DSEnumerator<T> : DSObject<ICreateDevEnum>, IEnumerable<T> where T : MonikerInfo, new()
  1327. {
  1328. #region Variabless
  1329. protected List<T> m_Objects = new List<T>();
  1330. protected IEnumMoniker m_pEnumMoniker = null;
  1331. protected Guid m_Category = Guid.Empty;
  1332. #endregion
  1333. #region Properties
  1334. public Guid Category
  1335. {
  1336. get { return m_Category; }
  1337. }
  1338. public T this[int index]
  1339. {
  1340. get { return m_Objects[index]; }
  1341. }
  1342. public T this[string name]
  1343. {
  1344. get
  1345. {
  1346. foreach (T _info in m_Objects)
  1347. {
  1348. if (_info.Name == name || _info.DevicePath == name)
  1349. {
  1350. return _info;
  1351. }
  1352. }
  1353. return null;
  1354. }
  1355. }
  1356. public T this[Guid _clsid]
  1357. {
  1358. get
  1359. {
  1360. foreach (T _info in m_Objects)
  1361. {
  1362. if (_info.ClassID == _clsid)
  1363. {
  1364. return _info;
  1365. }
  1366. }
  1367. return null;
  1368. }
  1369. }
  1370. public int Count
  1371. {
  1372. get { return m_Objects.Count; }
  1373. }
  1374. public List<T> Objects
  1375. {
  1376. get { return m_Objects; }
  1377. }
  1378. #endregion
  1379. #region Constructor
  1380. public DSEnumerator(Guid _category)
  1381. : base(new Guid("62BE5D10-60EB-11d0-BD3B-00A0C911CE86"))
  1382. {
  1383. int hr = m_pUnknown.CreateClassEnumerator(ref _category, out m_pEnumMoniker, 0);
  1384. ASSERT(hr == S_OK);
  1385. if (SUCCEEDED(hr))
  1386. {
  1387. if (hr != S_FALSE)
  1388. {
  1389. IMoniker[] aMonikers = new IMoniker[1];
  1390. while ((m_pEnumMoniker.Next(1, aMonikers, IntPtr.Zero) == 0))
  1391. {
  1392. T _moniker = new T();
  1393. _moniker.Value = aMonikers[0];
  1394. m_Objects.Add(_moniker);
  1395. }
  1396. }
  1397. }
  1398. }
  1399. #endregion
  1400. #region IDisposable Members
  1401. public override void Dispose()
  1402. {
  1403. if (m_pEnumMoniker != null)
  1404. {
  1405. Marshal.ReleaseComObject(m_pEnumMoniker);
  1406. m_pEnumMoniker = null;
  1407. }
  1408. while (m_Objects.Count > 0)
  1409. {
  1410. T _moniker = m_Objects[0];
  1411. _moniker.Dispose();
  1412. m_Objects.RemoveAt(0);
  1413. }
  1414. base.Dispose();
  1415. }
  1416. #endregion
  1417. #region IEnumerable<T> Members
  1418. public IEnumerator<T> GetEnumerator()
  1419. {
  1420. return m_Objects.GetEnumerator();
  1421. }
  1422. #endregion
  1423. #region IEnumerable Members
  1424. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  1425. {
  1426. return m_Objects.GetEnumerator();
  1427. }
  1428. #endregion
  1429. }
  1430. #endregion
  1431. #region Device Categories
  1432. [ComVisible(false)]
  1433. public class DSDevice : MonikerInfo
  1434. {
  1435. #region Fields
  1436. public DSFilter Filter
  1437. {
  1438. get
  1439. {
  1440. DSFilter _filter = null;
  1441. if (m_pUnknown != null)
  1442. {
  1443. _filter = new DSFilter(this.DevicePath);
  1444. if (_filter.IsReleased)
  1445. {
  1446. _filter.Dispose();
  1447. _filter = null;
  1448. }
  1449. }
  1450. return _filter;
  1451. }
  1452. }
  1453. #endregion
  1454. #region Constructor
  1455. public DSDevice()
  1456. {
  1457. }
  1458. #endregion
  1459. #region Overridden Methods
  1460. public override string ToString()
  1461. {
  1462. string _value = this.Name;
  1463. if (_value != null && _value != "") return _value;
  1464. _value = this.DevicePath;
  1465. if (_value != null && _value != "") return _value;
  1466. return base.ToString();
  1467. }
  1468. #endregion
  1469. }
  1470. [ComVisible(false)]
  1471. public class DSCategory : DSEnumerator<DSDevice>
  1472. {
  1473. #region Properties
  1474. public new DSFilter this[int index]
  1475. {
  1476. get
  1477. {
  1478. DSDevice _device = base[index];
  1479. if (_device)
  1480. {
  1481. return _device.Filter;
  1482. }
  1483. return null;
  1484. }
  1485. }
  1486. public new DSFilter this[string name]
  1487. {
  1488. get
  1489. {
  1490. DSDevice _device = base[name];
  1491. if (_device)
  1492. {
  1493. return _device.Filter;
  1494. }
  1495. return null;
  1496. }
  1497. }
  1498. public new DSFilter this[Guid _clsid]
  1499. {
  1500. get
  1501. {
  1502. DSDevice _device = base[_clsid];
  1503. if (_device)
  1504. {
  1505. return _device.Filter;
  1506. }
  1507. return null;
  1508. }
  1509. }
  1510. #endregion
  1511. #region Constructor
  1512. public DSCategory(Guid _category)
  1513. : base(_category)
  1514. {
  1515. }
  1516. #endregion
  1517. }
  1518. #endregion
  1519. #region Pin Helper
  1520. [ComVisible(false)]
  1521. public class DSPin : DSObject<IPin>
  1522. {
  1523. #region Constructor
  1524. public DSPin(IntPtr _pin)
  1525. : base(_pin)
  1526. {
  1527. }
  1528. public DSPin(IPin _pin)
  1529. : base(_pin)
  1530. {
  1531. }
  1532. #endregion
  1533. #region Properties
  1534. public PinDirection Direction
  1535. {
  1536. get
  1537. {
  1538. PinDirection _direction;
  1539. m_pUnknown.QueryDirection(out _direction);
  1540. return _direction;
  1541. }
  1542. }
  1543. public bool IsConnected
  1544. {
  1545. get
  1546. {
  1547. return ((ConnectedTo != null) && ConnectedTo == true);
  1548. }
  1549. }
  1550. public string Name
  1551. {
  1552. get
  1553. {
  1554. if (this)
  1555. {
  1556. PinInfo _info;
  1557. if (SUCCEEDED(m_pUnknown.QueryPinInfo(out _info)))
  1558. {
  1559. _info.filter = null;
  1560. return _info.name;
  1561. }
  1562. }
  1563. return "";
  1564. }
  1565. }
  1566. public DSFilter Filter
  1567. {
  1568. get
  1569. {
  1570. if (this)
  1571. {
  1572. PinInfo _info;
  1573. if (SUCCEEDED(m_pUnknown.QueryPinInfo(out _info)))
  1574. {
  1575. return new DSFilter(_info.filter);
  1576. }
  1577. }
  1578. return null;
  1579. }
  1580. }
  1581. public DSPin ConnectedTo
  1582. {
  1583. get
  1584. {
  1585. if (this)
  1586. {
  1587. IntPtr pConnected;
  1588. if (SUCCEEDED(m_pUnknown.ConnectedTo(out pConnected)))
  1589. {
  1590. return new DSPin(pConnected);
  1591. }
  1592. }
  1593. return null;
  1594. }
  1595. }
  1596. public AMMediaType ConnectionMediaType
  1597. {
  1598. get
  1599. {
  1600. if (this)
  1601. {
  1602. AMMediaType mt = new AMMediaType();
  1603. if (SUCCEEDED(m_pUnknown.ConnectionMediaType(mt)))
  1604. {
  1605. return mt;
  1606. }
  1607. mt.Free();
  1608. }
  1609. return null;
  1610. }
  1611. }
  1612. public AMMediaType Format
  1613. {
  1614. get
  1615. {
  1616. if (this)
  1617. {
  1618. IAMStreamConfig _config = (IAMStreamConfig)QueryInterface(typeof(IAMStreamConfig));
  1619. if (_config != null)
  1620. {
  1621. AMMediaType mt;
  1622. if (SUCCEEDED(_config.GetFormat(out mt)))
  1623. {
  1624. return mt;
  1625. }
  1626. }
  1627. }
  1628. return null;
  1629. }
  1630. set
  1631. {
  1632. if (this)
  1633. {
  1634. IAMStreamConfig _config = (IAMStreamConfig)QueryInterface(typeof(IAMStreamConfig));
  1635. if (_config != null)
  1636. {
  1637. int hr = _config.SetFormat(value);
  1638. ASSERT(SUCCEEDED(hr));
  1639. }
  1640. }
  1641. }
  1642. }
  1643. public List<AMMediaType> MediaTypes
  1644. {
  1645. get
  1646. {
  1647. List<AMMediaType> _list = new List<AMMediaType>();
  1648. if (this)
  1649. {
  1650. IEnumMediaTypes pEnum;
  1651. IntPtr _ptr;
  1652. if (SUCCEEDED(m_pUnknown.EnumMediaTypes(out _ptr)))
  1653. {
  1654. pEnum = (IEnumMediaTypes)Marshal.GetObjectForIUnknown(_ptr);
  1655. IntPtr ulMediaCount = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(uint)));
  1656. IntPtr pTypes = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
  1657. try
  1658. {
  1659. do
  1660. {
  1661. int hr = pEnum.Next(1, pTypes, ulMediaCount);
  1662. if (hr != S_OK)
  1663. {
  1664. break;
  1665. }
  1666. ASSERT(Marshal.ReadInt32(ulMediaCount) == 1);
  1667. IntPtr _ptrStructure = Marshal.ReadIntPtr(pTypes);
  1668. try
  1669. {
  1670. if (_ptrStructure != IntPtr.Zero)
  1671. {
  1672. _list.Add((AMMediaType)Marshal.PtrToStructure(_ptrStructure, typeof(AMMediaType)));
  1673. }
  1674. }
  1675. finally
  1676. {
  1677. if (_ptrStructure != IntPtr.Zero)
  1678. {
  1679. Marshal.FreeCoTaskMem(_ptrStructure);
  1680. }
  1681. }
  1682. }
  1683. while (true);
  1684. }
  1685. finally
  1686. {
  1687. Marshal.FreeCoTaskMem(ulMediaCount);
  1688. Marshal.FreeCoTaskMem(pTypes);
  1689. }
  1690. }
  1691. }
  1692. return _list;
  1693. }
  1694. }
  1695. public Guid Category
  1696. {
  1697. get
  1698. {
  1699. Guid _result = Guid.Empty;
  1700. int iSize = Marshal.SizeOf(typeof(Guid));
  1701. IntPtr ipOut = Marshal.AllocCoTaskMem(iSize);
  1702. try
  1703. {
  1704. int cbBytes;
  1705. IKsPropertySet pKs = (IKsPropertySet)QueryInterface(typeof(IKsPropertySet));
  1706. if (pKs != null)
  1707. {
  1708. Guid g = PropSetID.Pin;
  1709. int hr = pKs.Get(g, (int)AMPropertyPin.Category, IntPtr.Zero, 0, ipOut, iSize, out cbBytes);
  1710. if (SUCCEEDED(hr))
  1711. {
  1712. _result = (Guid)Marshal.PtrToStructure(ipOut, typeof(Guid));
  1713. }
  1714. }
  1715. }
  1716. finally
  1717. {
  1718. Marshal.FreeCoTaskMem(ipOut);
  1719. ipOut = IntPtr.Zero;
  1720. }
  1721. return _result;
  1722. }
  1723. }
  1724. public AMMediaType this[int index]
  1725. {
  1726. get
  1727. {
  1728. try
  1729. {
  1730. return MediaTypes[index];
  1731. }
  1732. catch
  1733. {
  1734. return null;
  1735. }
  1736. }
  1737. }
  1738. public int Count
  1739. {
  1740. get { return MediaTypes.Count; }
  1741. }
  1742. #endregion
  1743. #region Methods
  1744. public HRESULT ConnectDirect(DSPin _pin)
  1745. {
  1746. try
  1747. {
  1748. if (!_pin)
  1749. {
  1750. return Render();
  1751. }
  1752. if (!this) return E_POINTER;
  1753. IGraphBuilder _graph = Filter.FilterGraph;
  1754. if (_graph != null)
  1755. {
  1756. if (Direction != _pin.Direction)
  1757. {
  1758. if (Direction == PinDirection.Input)
  1759. {
  1760. return (HRESULT)_graph.ConnectDirect(_pin.m_pUnknown, m_pUnknown, null);
  1761. }
  1762. else
  1763. {
  1764. return (HRESULT)_graph.ConnectDirect(m_pUnknown, _pin.m_pUnknown, null);
  1765. }
  1766. }
  1767. return VFW_E_INVALID_DIRECTION;
  1768. }
  1769. return VFW_E_NOT_IN_GRAPH;
  1770. }
  1771. catch (Exception _exception)
  1772. {
  1773. return (HRESULT)Marshal.GetHRForException(_exception);
  1774. }
  1775. }
  1776. public HRESULT Connect(DSPin _pin)
  1777. {
  1778. try
  1779. {
  1780. HRESULT hr = ConnectDirect(_pin);
  1781. if (hr.Succeeded)
  1782. {
  1783. return hr;
  1784. }
  1785. if (!_pin)
  1786. {
  1787. return Render();
  1788. }
  1789. if (!this) return E_POINTER;
  1790. IGraphBuilder _graph = Filter.FilterGraph;
  1791. if (_graph != null)
  1792. {
  1793. if (Direction != _pin.Direction)
  1794. {
  1795. if (Direction == PinDirection.Input)
  1796. {
  1797. return (HRESULT)_graph.Connect(_pin.m_pUnknown, m_pUnknown);
  1798. }
  1799. else
  1800. {
  1801. return (HRESULT)_graph.Connect(m_pUnknown, _pin.m_pUnknown);
  1802. }
  1803. }
  1804. return VFW_E_INVALID_DIRECTION;
  1805. }
  1806. return VFW_E_NOT_IN_GRAPH;
  1807. }
  1808. catch (Exception _exception)
  1809. {
  1810. return (HRESULT)Marshal.GetHRForException(_exception);
  1811. }
  1812. }
  1813. public HRESULT Disconnect()
  1814. {
  1815. DSPin _pin = null;
  1816. try
  1817. {
  1818. if (!this) return E_POINTER;
  1819. IGraphBuilder _graph = Filter.FilterGraph;
  1820. if (_graph != null)
  1821. {
  1822. _pin = ConnectedTo;
  1823. if (!_pin) return VFW_E_NOT_CONNECTED;
  1824. int hr = _graph.Disconnect(m_pUnknown);
  1825. if (SUCCEEDED(hr))
  1826. {
  1827. return (HRESULT)_graph.Disconnect(_pin.m_pUnknown);
  1828. }
  1829. return (HRESULT)hr;
  1830. }
  1831. return VFW_E_NOT_IN_GRAPH;
  1832. }
  1833. catch (Exception _exception)
  1834. {
  1835. return (HRESULT)Marshal.GetHRForException(_exception);
  1836. }
  1837. finally
  1838. {
  1839. if (_pin)
  1840. {
  1841. _pin.Dispose();
  1842. _pin = null;
  1843. }
  1844. }
  1845. }
  1846. public HRESULT Render()
  1847. {
  1848. try
  1849. {
  1850. if (!this) return E_POINTER;
  1851. IGraphBuilder _graph = Filter.FilterGraph;
  1852. if (_graph != null)
  1853. {
  1854. return (HRESULT)_graph.Render(m_pUnknown);
  1855. }
  1856. return VFW_E_NOT_IN_GRAPH;
  1857. }
  1858. catch (Exception _exception)
  1859. {
  1860. return (HRESULT)Marshal.GetHRForException(_exception);
  1861. }
  1862. }
  1863. public HRESULT IsAccepted(AMMediaType mt)
  1864. {
  1865. if (this)
  1866. {
  1867. return (HRESULT)m_pUnknown.QueryAccept(mt);
  1868. }
  1869. return (HRESULT)E_FAIL;
  1870. }
  1871. public DSPin RemoveFiltersChain()
  1872. {
  1873. DSPin _connected = ConnectedTo;
  1874. if (_connected != null && _connected)
  1875. {
  1876. AMMediaType _type = ConnectionMediaType;
  1877. Disconnect();
  1878. List<DSPin> _pins = _connected.Filter.Pins;
  1879. List<DSPin> _disconnect = new List<DSPin>();
  1880. bool bShouldProceed = true;
  1881. foreach (DSPin _pin in _pins)
  1882. {
  1883. if (_pin.IsConnected)
  1884. {
  1885. if (_pin.Direction != Direction)
  1886. {
  1887. bShouldProceed = false;
  1888. break;
  1889. }
  1890. else
  1891. {
  1892. AMMediaType mt = _pin.ConnectionMediaType;
  1893. if (mt.majorType != _type.majorType)
  1894. {
  1895. bShouldProceed = false;
  1896. break;
  1897. }
  1898. _disconnect.Add(_pin);
  1899. }
  1900. }
  1901. }
  1902. if (bShouldProceed)
  1903. {
  1904. DSPin _result = null;
  1905. foreach (DSPin _pin in _disconnect)
  1906. {
  1907. _result = _pin.RemoveFiltersChain();
  1908. }
  1909. DSFilter _filter = _connected.Filter;
  1910. _filter.FilterGraph = null;
  1911. _filter.Dispose();
  1912. if (Direction == PinDirection.Output)
  1913. {
  1914. _connected = this;
  1915. }
  1916. else
  1917. {
  1918. _connected = _result;
  1919. }
  1920. }
  1921. }
  1922. return _connected;
  1923. }
  1924. #endregion
  1925. #region Overriden Methods
  1926. public override string ToString()
  1927. {
  1928. return " { " + base.ToString() + " } Name: '" + Name + "' ( " + Direction.ToString() + " )" + (IsConnected ? " Connected" : " Not Connected"); ;
  1929. }
  1930. #endregion
  1931. }
  1932. #endregion
  1933. #region Filter Helper
  1934. [ComVisible(false)]
  1935. public class DSFilter : DSObject<IBaseFilter>
  1936. {
  1937. #region Constructor
  1938. protected DSFilter()
  1939. : base()
  1940. {
  1941. }
  1942. public DSFilter(IBaseFilter _filter)
  1943. : base(_filter)
  1944. {
  1945. }
  1946. public DSFilter(Guid _filter)
  1947. : base(_filter)
  1948. {
  1949. }
  1950. public DSFilter(string _moniker)
  1951. {
  1952. MonikerInfo _info = new MonikerInfo(_moniker);
  1953. m_pUnknown = (IBaseFilter)_info.Bind(typeof(IBaseFilter));
  1954. }
  1955. #endregion
  1956. #region Properties
  1957. public List<DSPin> Pins
  1958. {
  1959. get
  1960. {
  1961. List<DSPin> _pins = new List<DSPin>();
  1962. if (this)
  1963. {
  1964. IPin[] pPins = new IPin[1];
  1965. IEnumPins pEnum;
  1966. if (SUCCEEDED(m_pUnknown.EnumPins(out pEnum)))
  1967. {
  1968. while (pEnum.Next(1, pPins, IntPtr.Zero) == S_OK)
  1969. {
  1970. if (pPins[0] != null)
  1971. {
  1972. _pins.Add(new DSPin(pPins[0]));
  1973. }
  1974. }
  1975. }
  1976. }
  1977. return _pins;
  1978. }
  1979. }
  1980. public List<DSPin> Output
  1981. {
  1982. get
  1983. {
  1984. List<DSPin> _pins = Pins;
  1985. List<DSPin> _output = new List<DSPin>();
  1986. foreach (DSPin _pin in _pins)
  1987. {
  1988. if (_pin.Direction == PinDirection.Output)
  1989. {
  1990. _output.Add(_pin);
  1991. }
  1992. }
  1993. return _output;
  1994. }
  1995. }
  1996. public List<DSPin> Input
  1997. {
  1998. get
  1999. {
  2000. List<DSPin> _pins = Pins;
  2001. List<DSPin> _output = new List<DSPin>();
  2002. foreach (DSPin _pin in _pins)
  2003. {
  2004. if (_pin.Direction == PinDirection.Input)
  2005. {
  2006. _output.Add(_pin);
  2007. }
  2008. }
  2009. return _output;
  2010. }
  2011. }
  2012. public string Name
  2013. {
  2014. get
  2015. {
  2016. if (this)
  2017. {
  2018. FilterInfo _info;
  2019. if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
  2020. {
  2021. if (_info.pGraph != null)
  2022. {
  2023. Marshal.ReleaseComObject(_info.pGraph);
  2024. }
  2025. return _info.achName;
  2026. }
  2027. }
  2028. return "";
  2029. }
  2030. }
  2031. public bool HaveProperties
  2032. {
  2033. get { return IsSupported(typeof(ISpecifyPropertyPages)); }
  2034. }
  2035. public IGraphBuilder FilterGraph
  2036. {
  2037. get
  2038. {
  2039. IGraphBuilder _graph = null;
  2040. if (this)
  2041. {
  2042. FilterInfo _info;
  2043. if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
  2044. {
  2045. if (_info.pGraph != null)
  2046. {
  2047. _graph = (IGraphBuilder)_info.pGraph;
  2048. Marshal.ReleaseComObject(_info.pGraph);
  2049. }
  2050. }
  2051. }
  2052. return _graph;
  2053. }
  2054. set
  2055. {
  2056. if (this)
  2057. {
  2058. if (value == null)
  2059. {
  2060. FilterInfo _info;
  2061. if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
  2062. {
  2063. if (_info.pGraph != null)
  2064. {
  2065. IGraphBuilder _graph = (IGraphBuilder)_info.pGraph;
  2066. _graph.RemoveFilter(m_pUnknown);
  2067. Marshal.ReleaseComObject(_info.pGraph);
  2068. }
  2069. }
  2070. }
  2071. else
  2072. {
  2073. int hr = value.AddFilter(m_pUnknown, Name);
  2074. ASSERT(SUCCEEDED(hr));
  2075. }
  2076. }
  2077. }
  2078. }
  2079. public DSPin InputPin
  2080. {
  2081. get
  2082. {
  2083. List<DSPin> _pins = Input;
  2084. if (_pins.Count > 0)
  2085. return _pins[0];
  2086. return null;
  2087. }
  2088. }
  2089. public DSPin OutputPin
  2090. {
  2091. get
  2092. {
  2093. List<DSPin> _pins = Output;
  2094. if (_pins.Count > 0)
  2095. return _pins[0];
  2096. return null;
  2097. }
  2098. }
  2099. public DSPin this[int index]
  2100. {
  2101. get
  2102. {
  2103. return Pins[index];
  2104. }
  2105. }
  2106. public DSPin this[string _name]
  2107. {
  2108. get
  2109. {
  2110. List<DSPin> _pins = Pins;
  2111. foreach (DSPin _pin in _pins)
  2112. {
  2113. if (_pin.Name == _name) return _pin;
  2114. }
  2115. return null;
  2116. }
  2117. }
  2118. public int Count
  2119. {
  2120. get { return Pins.Count; }
  2121. }
  2122. #endregion
  2123. #region Public Methods
  2124. public HRESULT ShowProperties()
  2125. {
  2126. return ShowProperties(IntPtr.Zero);
  2127. }
  2128. public HRESULT ShowProperties(IntPtr _hwnd)
  2129. {
  2130. if (DirectShow.BaseClasses.BasePropertyPage.ShowPropertyPages(m_pUnknown, _hwnd))
  2131. {
  2132. return S_OK;
  2133. }
  2134. return E_FAIL;
  2135. }
  2136. public DSPin GetPin(PinDirection _direction)
  2137. {
  2138. return GetPin(_direction, 0);
  2139. }
  2140. public DSPin GetPin(PinDirection _direction, int index)
  2141. {
  2142. List<DSPin> _pins = null;
  2143. switch (_direction)
  2144. {
  2145. case PinDirection.Input:
  2146. _pins = Input;
  2147. break;
  2148. case PinDirection.Output:
  2149. _pins = Output;
  2150. break;
  2151. }
  2152. ASSERT(_pins);
  2153. if (index >= 0 && _pins.Count < index)
  2154. {
  2155. return _pins[index];
  2156. }
  2157. return null;
  2158. }
  2159. public DSPin GetPin(string _name)
  2160. {
  2161. List<DSPin> _pins = Pins;
  2162. foreach (DSPin _pin in _pins)
  2163. {
  2164. if (_pin.Name == _name) return _pin;
  2165. }
  2166. return null;
  2167. }
  2168. public DSPin GetPin(Guid _category)
  2169. {
  2170. List<DSPin> _pins = Pins;
  2171. foreach (DSPin _pin in _pins)
  2172. {
  2173. if (_pin.Category == _category) return _pin;
  2174. }
  2175. return null;
  2176. }
  2177. public HRESULT Connect(DSFilter _filter)
  2178. {
  2179. if (_filter)
  2180. {
  2181. List<DSPin> _pins;
  2182. {
  2183. _pins = _filter.Input;
  2184. foreach (DSPin _pin in _pins)
  2185. {
  2186. HRESULT hr = Connect(_pin);
  2187. if (hr.Succeeded) return hr;
  2188. }
  2189. }
  2190. {
  2191. _pins = _filter.Output;
  2192. foreach (DSPin _pin in _pins)
  2193. {
  2194. HRESULT hr = Connect(_pin);
  2195. if (hr.Succeeded) return hr;
  2196. }
  2197. }
  2198. return VFW_E_CANNOT_CONNECT;
  2199. }
  2200. return E_POINTER;
  2201. }
  2202. public HRESULT Connect(DSPin _pin)
  2203. {
  2204. if (_pin)
  2205. {
  2206. List<DSPin> _pins;
  2207. if (_pin.Direction == PinDirection.Output)
  2208. {
  2209. _pins = Input;
  2210. }
  2211. else
  2212. {
  2213. _pins = Output;
  2214. }
  2215. foreach (DSPin _connect in _pins)
  2216. {
  2217. if (!_connect.IsConnected)
  2218. {
  2219. HRESULT hr = _connect.Connect(_pin);
  2220. if (hr.Succeeded) return hr;
  2221. }
  2222. }
  2223. return VFW_E_CANNOT_CONNECT;
  2224. }
  2225. return E_POINTER;
  2226. }
  2227. public DSPin RemoveFiltersChain()
  2228. {
  2229. DSPin _input = null;
  2230. DSPin _output = null;
  2231. _output = RemoveFiltersChain(PinDirection.Output);
  2232. _input = RemoveFiltersChain(PinDirection.Input);
  2233. if (_output != null) return _output;
  2234. return _input;
  2235. }
  2236. public DSPin RemoveFiltersChain(PinDirection _direction)
  2237. {
  2238. List<DSPin> _pins;
  2239. if (_direction == PinDirection.Input)
  2240. {
  2241. _pins = Input;
  2242. }
  2243. else
  2244. {
  2245. _pins = Output;
  2246. }
  2247. DSPin _result = null;
  2248. foreach (DSPin _pin in _pins)
  2249. {
  2250. DSPin _removed = _pin.RemoveFiltersChain();
  2251. if (_removed != null && _result == null)
  2252. {
  2253. _result = _removed;
  2254. }
  2255. }
  2256. return _result;
  2257. }
  2258. #endregion
  2259. #region Overriden Methods
  2260. public override string ToString()
  2261. {
  2262. return Name;
  2263. }
  2264. #endregion
  2265. }
  2266. [ComVisible(false)]
  2267. public class DSBaseSourceFilter : DSFilter
  2268. {
  2269. #region Properties
  2270. public string FileName
  2271. {
  2272. get
  2273. {
  2274. IFileSourceFilter _source = (IFileSourceFilter)QueryInterface(typeof(IFileSourceFilter));
  2275. if (_source != null)
  2276. {
  2277. string _path;
  2278. int hr = _source.GetCurFile(out _path, null);
  2279. if (SUCCEEDED(hr))
  2280. {
  2281. return _path;
  2282. }
  2283. }
  2284. return null;
  2285. }
  2286. set
  2287. {
  2288. IFileSourceFilter _source = (IFileSourceFilter)QueryInterface(typeof(IFileSourceFilter));
  2289. if (_source != null)
  2290. {
  2291. int hr = _source.Load(value, null);
  2292. ASSERT(SUCCEEDED(hr));
  2293. }
  2294. }
  2295. }
  2296. #endregion
  2297. #region Constructor
  2298. protected DSBaseSourceFilter()
  2299. : base()
  2300. {
  2301. }
  2302. public DSBaseSourceFilter(IBaseFilter _filter)
  2303. : base(_filter)
  2304. {
  2305. }
  2306. public DSBaseSourceFilter(Guid _filter)
  2307. : base(_filter)
  2308. {
  2309. }
  2310. public DSBaseSourceFilter(string _moniker)
  2311. : base(_moniker)
  2312. {
  2313. }
  2314. #endregion
  2315. }
  2316. [ComVisible(false)]
  2317. public class DSBaseWriterFilter : DSFilter
  2318. {
  2319. #region Properties
  2320. public string FileName
  2321. {
  2322. get
  2323. {
  2324. IFileSinkFilter _sink = (IFileSinkFilter)QueryInterface(typeof(IFileSinkFilter));
  2325. if (_sink != null)
  2326. {
  2327. string _path;
  2328. int hr = _sink.GetCurFile(out _path, null);
  2329. if (SUCCEEDED(hr))
  2330. {
  2331. return _path;
  2332. }
  2333. }
  2334. return null;
  2335. }
  2336. set
  2337. {
  2338. IFileSinkFilter _sink = (IFileSinkFilter)QueryInterface(typeof(IFileSinkFilter));
  2339. if (_sink != null)
  2340. {
  2341. int hr = _sink.SetFileName(value, null);
  2342. ASSERT(SUCCEEDED(hr));
  2343. }
  2344. }
  2345. }
  2346. #endregion
  2347. #region Constructor
  2348. protected DSBaseWriterFilter()
  2349. : base()
  2350. {
  2351. }
  2352. public DSBaseWriterFilter(IBaseFilter _filter)
  2353. : base(_filter)
  2354. {
  2355. }
  2356. public DSBaseWriterFilter(Guid _filter)
  2357. : base(_filter)
  2358. {
  2359. }
  2360. public DSBaseWriterFilter(string _moniker)
  2361. : base(_moniker)
  2362. {
  2363. }
  2364. #endregion
  2365. }
  2366. #endregion
  2367. #region Categories
  2368. [ComVisible(false)]
  2369. public class DSVideoCaptureCategory : DSCategory
  2370. {
  2371. #region Constructor
  2372. public DSVideoCaptureCategory()
  2373. : base(FilterCategory.VideoInputDevice)
  2374. {
  2375. }
  2376. #endregion
  2377. }
  2378. [ComVisible(false)]
  2379. public class DSAudioCaptureCategory : DSCategory
  2380. {
  2381. #region Constructor
  2382. public DSAudioCaptureCategory()
  2383. : base(FilterCategory.AudioInputDevice)
  2384. {
  2385. }
  2386. #endregion
  2387. }
  2388. [ComVisible(false)]
  2389. public class DSVideoCompressorsCategory : DSCategory
  2390. {
  2391. #region Constructor
  2392. public DSVideoCompressorsCategory()
  2393. : base(FilterCategory.VideoCompressorCategory)
  2394. {
  2395. }
  2396. #endregion
  2397. }
  2398. [ComVisible(false)]
  2399. public class DSAudioCompressorsCategory : DSCategory
  2400. {
  2401. #region Constructor
  2402. public DSAudioCompressorsCategory()
  2403. : base(FilterCategory.AudioCompressorCategory)
  2404. {
  2405. }
  2406. #endregion
  2407. }
  2408. [ComVisible(false)]
  2409. public class DSAudioRenderersCategory : DSCategory
  2410. {
  2411. #region Constructor
  2412. public DSAudioRenderersCategory()
  2413. : base(FilterCategory.AudioRendererCategory)
  2414. {
  2415. }
  2416. #endregion
  2417. }
  2418. #endregion
  2419. #region Filters
  2420. [ComVisible(false)]
  2421. [Guid("f8388a40-d5bb-11d0-be5a-0080c706568e")]
  2422. public class DSInfTeeFilter : DSFilter
  2423. {
  2424. public DSInfTeeFilter()
  2425. : base()
  2426. {
  2427. }
  2428. }
  2429. [ComVisible(false)]
  2430. [Guid("cc58e280-8aa1-11d1-b3f1-00aa003761c5")]
  2431. public class DSSmartTeeFilter : DSFilter
  2432. {
  2433. public DSSmartTeeFilter()
  2434. : base()
  2435. {
  2436. }
  2437. }
  2438. [ComVisible(false)]
  2439. [Guid("8596e5f0-0da5-11d0-bd21-00a0c911ce86")]
  2440. public class DSFileWriter : DSBaseWriterFilter
  2441. {
  2442. public DSFileWriter()
  2443. : base()
  2444. {
  2445. }
  2446. }
  2447. [ComVisible(false)]
  2448. [Guid("e436ebb5-524f-11ce-9f53-0020af0ba770")]
  2449. public class DSFileSourceAsync : DSBaseSourceFilter
  2450. {
  2451. public DSFileSourceAsync()
  2452. : base()
  2453. {
  2454. }
  2455. }
  2456. [ComVisible(false)]
  2457. [Guid("79376820-07d0-11cf-a24d-0020afd79767")]
  2458. public class DSDSoundRendererFilter : DSFilter
  2459. {
  2460. public DSDSoundRendererFilter()
  2461. : base()
  2462. {
  2463. }
  2464. }
  2465. [ComVisible(false)]
  2466. [Guid("70e102b0-5556-11ce-97c0-00aa0055595a")]
  2467. public class DSVideoRenderer : DSFilter
  2468. {
  2469. public DSVideoRenderer()
  2470. : base()
  2471. {
  2472. }
  2473. }
  2474. [ComVisible(false)]
  2475. [Guid("c1f400a4-3f08-11d3-9f0b-006008039e37")]
  2476. public class DSNullRenderer : DSFilter
  2477. {
  2478. public DSNullRenderer()
  2479. : base()
  2480. {
  2481. }
  2482. }
  2483. [ComVisible(false)]
  2484. [Guid("c1f400a0-3f08-11d3-9f0b-006008039e37")]
  2485. public class DSSampleGrabberFilter : DSFilter, ISampleGrabberCB
  2486. {
  2487. #region Delegates
  2488. [ComVisible(false)]
  2489. public delegate void SampleEventHandler(DSSampleGrabberFilter _filter, IntPtr _buffer, int _length);
  2490. #endregion
  2491. #region Variables
  2492. protected ISampleGrabber m_SampleGrabber = null;
  2493. protected bool m_BufferedSamples = false;
  2494. protected bool m_bOneShot = false;
  2495. #endregion
  2496. #region Events
  2497. public event SampleEventHandler OnSample = null;
  2498. #endregion
  2499. #region Properties
  2500. public AMMediaType MediaType
  2501. {
  2502. get
  2503. {
  2504. if (m_SampleGrabber != null)
  2505. {
  2506. AMMediaType mt = new AMMediaType();
  2507. if (SUCCEEDED(m_SampleGrabber.GetConnectedMediaType(mt)))
  2508. {
  2509. return mt;
  2510. }
  2511. mt.Free();
  2512. }
  2513. return null;
  2514. }
  2515. set
  2516. {
  2517. if (m_SampleGrabber != null)
  2518. {
  2519. HRESULT hr = (HRESULT)m_SampleGrabber.SetMediaType(value);
  2520. hr.Assert();
  2521. }
  2522. }
  2523. }
  2524. public bool BufferedSamples
  2525. {
  2526. get { return m_BufferedSamples; }
  2527. set
  2528. {
  2529. if (m_BufferedSamples != value)
  2530. {
  2531. HRESULT hr = NOERROR;
  2532. if (m_SampleGrabber != null)
  2533. {
  2534. hr = (HRESULT)m_SampleGrabber.SetBufferSamples(value);
  2535. hr.Assert();
  2536. }
  2537. if (hr.Succeeded)
  2538. {
  2539. m_BufferedSamples = value;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. public bool OneShot
  2545. {
  2546. get { return m_bOneShot; }
  2547. set
  2548. {
  2549. if (m_bOneShot != value)
  2550. {
  2551. HRESULT hr = NOERROR;
  2552. if (m_SampleGrabber != null)
  2553. {
  2554. hr = (HRESULT)m_SampleGrabber.SetOneShot(value);
  2555. hr.Assert();
  2556. }
  2557. if (hr.Succeeded)
  2558. {
  2559. m_bOneShot = value;
  2560. }
  2561. }
  2562. }
  2563. }
  2564. #endregion
  2565. #region Constructor
  2566. public DSSampleGrabberFilter()
  2567. : base()
  2568. {
  2569. m_SampleGrabber = (ISampleGrabber)QueryInterface(typeof(ISampleGrabber));
  2570. ASSERT(m_SampleGrabber);
  2571. if (m_SampleGrabber != null)
  2572. {
  2573. HRESULT hr = (HRESULT)m_SampleGrabber.SetCallback(this, 1);
  2574. hr.Assert();
  2575. hr = (HRESULT)m_SampleGrabber.SetBufferSamples(m_BufferedSamples);
  2576. hr.Assert();
  2577. hr = (HRESULT)m_SampleGrabber.SetOneShot(m_bOneShot);
  2578. hr.Assert();
  2579. }
  2580. }
  2581. #endregion
  2582. #region ISampleGrabberCB Members
  2583. public int SampleCB(double _time, IntPtr pSample)
  2584. {
  2585. return NOERROR;
  2586. }
  2587. public int BufferCB(double _time, IntPtr pBuffer, int _length)
  2588. {
  2589. if (OnSample != null)
  2590. {
  2591. OnSample(this, pBuffer, _length);
  2592. }
  2593. return NOERROR;
  2594. }
  2595. #endregion
  2596. #region Overridden Methods
  2597. public override void Dispose()
  2598. {
  2599. m_SampleGrabber = (ISampleGrabber)QueryInterface(typeof(ISampleGrabber));
  2600. if (m_SampleGrabber != null)
  2601. {
  2602. m_SampleGrabber.SetCallback(null, 0);
  2603. m_SampleGrabber = null;
  2604. }
  2605. base.Dispose();
  2606. }
  2607. #endregion
  2608. }
  2609. [ComVisible(false)]
  2610. [Guid("7c23220e-55bb-11d3-8b16-00c04fb6bd3d")]
  2611. public class DSWMAsfWritter : DSBaseWriterFilter
  2612. {
  2613. public DSWMAsfWritter()
  2614. : base()
  2615. {
  2616. }
  2617. }
  2618. [ComVisible(false)]
  2619. [Guid("187463A0-5BB7-11d3-ACBE-0080C75E246E")]
  2620. public class DSWMAsfReader : DSBaseSourceFilter
  2621. {
  2622. public DSWMAsfReader()
  2623. : base()
  2624. {
  2625. }
  2626. }
  2627. [ComVisible(false)]
  2628. [Guid("E2510970-F137-11CE-8B67-00AA00A3F1A6")]
  2629. public class DSAviMuxFilter : DSFilter
  2630. {
  2631. public DSAviMuxFilter()
  2632. : base()
  2633. {
  2634. }
  2635. }
  2636. #endregion
  2637. #region Classes
  2638. [ComVisible(false)]
  2639. [Guid("e436ebb3-524f-11ce-9f53-0020af0ba770")]
  2640. public class DSGraphBuilder : DSObject<IGraphBuilder>
  2641. {
  2642. public DSGraphBuilder()
  2643. : base()
  2644. {
  2645. }
  2646. }
  2647. [ComVisible(false)]
  2648. [Guid("BF87B6E1-8C27-11d0-B3F0-00AA003761C5")]
  2649. public class DSCaptureGraphBuilder2 : DSObject<ICaptureGraphBuilder2>
  2650. {
  2651. public DSCaptureGraphBuilder2()
  2652. : base()
  2653. {
  2654. }
  2655. }
  2656. #endregion
  2657. #region Base Filter Graph Builder
  2658. [ComVisible(false)]
  2659. [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  2660. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  2661. public class DSFilterGraphBase : COMHelper, IDisposable
  2662. {
  2663. #region Constants
  2664. protected const int WM_GRAPHNOTIFY = 0x00008001;
  2665. #endregion
  2666. #region Helper Classes
  2667. [ComVisible(false)]
  2668. protected class HelperForm : System.Windows.Forms.Form
  2669. {
  2670. protected override void WndProc(ref System.Windows.Forms.Message m)
  2671. {
  2672. if (m.Msg == WM_GRAPHNOTIFY)
  2673. {
  2674. try
  2675. {
  2676. if (m.LParam != IntPtr.Zero)
  2677. {
  2678. DSFilterGraphBase _graph = (DSFilterGraphBase)Marshal.GetObjectForIUnknown(m.LParam);
  2679. if (_graph != null)
  2680. {
  2681. if (_graph.ProcessGraphMessage())
  2682. {
  2683. _graph.Stop();
  2684. }
  2685. }
  2686. }
  2687. }
  2688. catch
  2689. {
  2690. }
  2691. }
  2692. base.WndProc(ref m);
  2693. }
  2694. }
  2695. #endregion
  2696. #region Variables
  2697. protected System.Windows.Forms.Control m_VideoControl = null;
  2698. protected System.Windows.Forms.Control m_EventControl = new HelperForm();
  2699. protected IGraphBuilder m_GraphBuilder = null;
  2700. protected IMediaControl m_MediaControl = null;
  2701. protected IMediaSeeking m_MediaSeeking = null;
  2702. protected IMediaEventEx m_MediaEventEx = null;
  2703. protected IVideoWindow m_VideoWindow = null;
  2704. protected IBasicAudio m_BasicAudio = null;
  2705. protected IBasicVideo m_BasicVideo = null;
  2706. protected IVideoFrameStep m_FrameStep = null;
  2707. protected bool m_bMute = false;
  2708. protected int m_iVolume = 0;
  2709. protected double m_dRate = 1.0;
  2710. protected bool m_bShouldPreview = true;
  2711. protected List<DSFilter> m_Filters = new List<DSFilter>();
  2712. #endregion
  2713. #region Constructor
  2714. public DSFilterGraphBase()
  2715. {
  2716. }
  2717. ~DSFilterGraphBase()
  2718. {
  2719. Dispose();
  2720. }
  2721. #endregion
  2722. #region Properties
  2723. public bool IsRunning
  2724. {
  2725. get
  2726. {
  2727. if (m_MediaControl != null)
  2728. {
  2729. FilterState _state;
  2730. int hr = 0;
  2731. do
  2732. {
  2733. hr = m_MediaControl.GetState(200, out _state);
  2734. }
  2735. while (hr == 0x00040237 || hr == 0x00040268);
  2736. if (hr == 0)
  2737. {
  2738. return _state == FilterState.Running;
  2739. }
  2740. }
  2741. return false;
  2742. }
  2743. }
  2744. public bool IsPaused
  2745. {
  2746. get
  2747. {
  2748. if (m_MediaControl != null)
  2749. {
  2750. FilterState _state;
  2751. int hr = 0;
  2752. do
  2753. {
  2754. hr = m_MediaControl.GetState(200, out _state);
  2755. }
  2756. while (hr == 0x00040237 || hr == 0x00040268);
  2757. if (hr == 0)
  2758. {
  2759. return _state == FilterState.Paused;
  2760. }
  2761. }
  2762. return false;
  2763. }
  2764. }
  2765. public bool IsStopped
  2766. {
  2767. get
  2768. {
  2769. if (m_MediaControl != null)
  2770. {
  2771. FilterState _state;
  2772. int hr = 0;
  2773. do
  2774. {
  2775. hr = m_MediaControl.GetState(200, out _state);
  2776. }
  2777. while (hr == 0x00040237 || hr == 0x00040268);
  2778. if (hr == 0)
  2779. {
  2780. return _state == FilterState.Stopped;
  2781. }
  2782. }
  2783. return true;
  2784. }
  2785. }
  2786. public long Position
  2787. {
  2788. get
  2789. {
  2790. if (m_MediaSeeking != null)
  2791. {
  2792. long _time;
  2793. int hr = m_MediaSeeking.GetCurrentPosition(out _time);
  2794. Debug.Assert(hr == 0);
  2795. if (hr == 0) return _time;
  2796. }
  2797. return -1;
  2798. }
  2799. set
  2800. {
  2801. if (m_MediaSeeking != null)
  2802. {
  2803. DsLong _stop = (long)0;
  2804. DsLong _current = value;
  2805. int hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
  2806. Debug.Assert(hr >= 0);
  2807. if (OnPositionChange != null) OnPositionChange(this, EventArgs.Empty);
  2808. }
  2809. }
  2810. }
  2811. public long Duration
  2812. {
  2813. get
  2814. {
  2815. if (m_MediaSeeking != null)
  2816. {
  2817. long _time;
  2818. int hr = m_MediaSeeking.GetDuration(out _time);
  2819. Debug.Assert(hr == 0);
  2820. if (hr == 0) return _time;
  2821. }
  2822. return -1;
  2823. }
  2824. }
  2825. public int Volume
  2826. {
  2827. get { return m_iVolume; }
  2828. set
  2829. {
  2830. m_iVolume = value;
  2831. if (!m_bMute)
  2832. {
  2833. SetVolume(value);
  2834. }
  2835. }
  2836. }
  2837. public bool Mute
  2838. {
  2839. get { return m_bMute; }
  2840. set
  2841. {
  2842. if (m_bMute != value)
  2843. {
  2844. m_bMute = value;
  2845. SetVolume(m_bMute ? -10000 : m_iVolume);
  2846. }
  2847. }
  2848. }
  2849. public double Rate
  2850. {
  2851. get
  2852. {
  2853. return m_dRate;
  2854. }
  2855. set
  2856. {
  2857. if (value > 0 && value <= 2.0)
  2858. {
  2859. if (m_MediaSeeking != null)
  2860. {
  2861. int hr = m_MediaSeeking.SetRate(value);
  2862. if (hr == 0)
  2863. {
  2864. m_dRate = value;
  2865. }
  2866. }
  2867. else
  2868. {
  2869. m_dRate = value;
  2870. }
  2871. }
  2872. }
  2873. }
  2874. public System.Windows.Forms.Control VideoControl
  2875. {
  2876. get { return m_VideoControl; }
  2877. set
  2878. {
  2879. if (m_VideoControl != value)
  2880. {
  2881. m_VideoControl = value;
  2882. m_VideoControl.Resize += new EventHandler(VideoControl_Resize);
  2883. m_VideoControl.VisibleChanged += new EventHandler(VideoControl_VisibleChanged);
  2884. }
  2885. }
  2886. }
  2887. public bool Visible
  2888. {
  2889. get
  2890. {
  2891. bool bVisible = ((m_VideoControl != null) ? m_VideoControl.Visible : false);
  2892. return m_bShouldPreview || bVisible;
  2893. }
  2894. set
  2895. {
  2896. if (m_bShouldPreview != value)
  2897. {
  2898. m_bShouldPreview = value;
  2899. if (m_VideoWindow != null)
  2900. {
  2901. m_VideoWindow.put_AutoShow(m_bShouldPreview ? -1 : 0);
  2902. m_VideoWindow.put_Visible(m_bShouldPreview ? -1 : 0);
  2903. }
  2904. }
  2905. }
  2906. }
  2907. public List<DSFilter> Filters
  2908. {
  2909. get
  2910. {
  2911. if (m_MediaControl == null)
  2912. {
  2913. while (m_Filters.Count > 0)
  2914. {
  2915. DSFilter _filter = m_Filters[0];
  2916. m_Filters.RemoveAt(0);
  2917. _filter.Dispose();
  2918. }
  2919. if (m_GraphBuilder != null)
  2920. {
  2921. IEnumFilters pEnum;
  2922. if (SUCCEEDED(m_GraphBuilder.EnumFilters(out pEnum)))
  2923. {
  2924. IBaseFilter[] aFilters = new IBaseFilter[1];
  2925. while (S_OK == pEnum.Next(1, aFilters, IntPtr.Zero))
  2926. {
  2927. m_Filters.Add(new DSFilter(aFilters[0]));
  2928. }
  2929. Marshal.ReleaseComObject(pEnum);
  2930. }
  2931. }
  2932. }
  2933. return m_Filters;
  2934. }
  2935. }
  2936. public int Count
  2937. {
  2938. get { return Filters.Count; }
  2939. }
  2940. public DSFilter AudioRenderer
  2941. {
  2942. get
  2943. {
  2944. List<DSFilter> _filters = Filters;
  2945. foreach (DSFilter _filter in _filters)
  2946. {
  2947. if (_filter.IsSupported(typeof(IBasicAudio).GUID))
  2948. {
  2949. return _filter;
  2950. }
  2951. }
  2952. return null;
  2953. }
  2954. }
  2955. public DSFilter VideoRenderer
  2956. {
  2957. get
  2958. {
  2959. List<DSFilter> _filters = Filters;
  2960. foreach (DSFilter _filter in _filters)
  2961. {
  2962. if (_filter.IsSupported(typeof(IVideoWindow)))
  2963. {
  2964. return _filter;
  2965. }
  2966. }
  2967. return null;
  2968. }
  2969. }
  2970. public DSFilter this[int index]
  2971. {
  2972. get
  2973. {
  2974. try
  2975. {
  2976. return Filters[index];
  2977. }
  2978. catch
  2979. {
  2980. return null;
  2981. }
  2982. }
  2983. }
  2984. public DSFilter this[string _name]
  2985. {
  2986. get
  2987. {
  2988. List<DSFilter> _filters = Filters;
  2989. foreach (DSFilter _filter in _filters)
  2990. {
  2991. if (_filter.Name == _name)
  2992. {
  2993. return _filter;
  2994. }
  2995. }
  2996. return null;
  2997. }
  2998. }
  2999. public bool IsAudioSupported
  3000. {
  3001. get
  3002. {
  3003. return ((AudioRenderer != null) && AudioRenderer == true);
  3004. }
  3005. }
  3006. #endregion
  3007. #region Events
  3008. public event EventHandler OnPlaybackPrepared;
  3009. public event EventHandler OnPlaybackStart;
  3010. public event EventHandler OnPlaybackStop;
  3011. public event EventHandler OnPlaybackPause;
  3012. public event EventHandler OnPositionChange;
  3013. public event EventHandler OnPlaybackReady;
  3014. #endregion
  3015. #region Public Methods
  3016. public virtual HRESULT Start()
  3017. {
  3018. int hr = 0;
  3019. if (m_MediaControl == null)
  3020. {
  3021. hr = Load();
  3022. if (hr != 0) return (HRESULT)hr;
  3023. }
  3024. hr = 1;
  3025. while (hr == 1)
  3026. {
  3027. hr = m_MediaControl.Run();
  3028. if (hr == 1) Thread.Sleep(50);
  3029. }
  3030. if (hr == 0)
  3031. {
  3032. if (OnPlaybackStart != null) OnPlaybackStart(this, EventArgs.Empty);
  3033. }
  3034. return (HRESULT)hr;
  3035. }
  3036. public virtual HRESULT Pause()
  3037. {
  3038. int hr = 0;
  3039. if (m_MediaControl == null)
  3040. {
  3041. hr = Load();
  3042. if (hr != 0) return (HRESULT)hr;
  3043. }
  3044. if (!IsPaused)
  3045. {
  3046. hr = m_MediaControl.Pause();
  3047. }
  3048. else
  3049. {
  3050. return Start();
  3051. }
  3052. if (hr >= 0)
  3053. {
  3054. if (OnPlaybackPause != null) OnPlaybackPause(this, EventArgs.Empty);
  3055. }
  3056. return (HRESULT)hr;
  3057. }
  3058. public virtual HRESULT Stop()
  3059. {
  3060. if (m_MediaControl == null) return (HRESULT)E_POINTER;
  3061. m_MediaControl.Stop();
  3062. if (OnPlaybackStop != null) OnPlaybackStop(this, EventArgs.Empty);
  3063. return Unload();
  3064. }
  3065. public virtual HRESULT StepForward()
  3066. {
  3067. if (m_FrameStep != null)
  3068. {
  3069. if (!IsPaused)
  3070. {
  3071. Pause();
  3072. }
  3073. int hr = m_FrameStep.Step(1, null);
  3074. if (hr < 0)
  3075. {
  3076. long _time;
  3077. hr = m_MediaSeeking.GetCurrentPosition(out _time);
  3078. DsLong _stop = (long)0;
  3079. TimeSpan _ts = new TimeSpan(0, 0, 1);
  3080. _time += _ts.Ticks / 20;
  3081. DsLong _current = _time;
  3082. hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
  3083. }
  3084. return (HRESULT)hr;
  3085. }
  3086. return (HRESULT)E_POINTER;
  3087. }
  3088. public virtual HRESULT StepBackward()
  3089. {
  3090. if (m_FrameStep != null)
  3091. {
  3092. if (!IsPaused)
  3093. {
  3094. Pause();
  3095. }
  3096. long _time;
  3097. int hr = m_MediaSeeking.GetCurrentPosition(out _time);
  3098. DsLong _stop = (long)0;
  3099. TimeSpan _ts = new TimeSpan(0, 0, 1);
  3100. _time -= _ts.Ticks / 20;
  3101. if (_time < 0) _time = 0;
  3102. DsLong _current = _time;
  3103. hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
  3104. return (HRESULT)hr;
  3105. }
  3106. return (HRESULT)E_POINTER;
  3107. }
  3108. #endregion
  3109. #region Private Methods
  3110. private void SetVolume(int _volume)
  3111. {
  3112. if (m_BasicAudio == null)
  3113. {
  3114. if (m_GraphBuilder == null)
  3115. {
  3116. return;
  3117. }
  3118. else
  3119. {
  3120. m_BasicAudio = (IBasicAudio)m_GraphBuilder;
  3121. }
  3122. }
  3123. m_BasicAudio.put_Volume(_volume);
  3124. }
  3125. private HRESULT InitInterfaces()
  3126. {
  3127. CloseInterfaces();
  3128. HRESULT hr = E_FAIL;
  3129. try
  3130. {
  3131. m_GraphBuilder = (IGraphBuilder)new FilterGraph();
  3132. hr = OnInitInterfaces();
  3133. hr.Throw();
  3134. return PreparePlayback();
  3135. }
  3136. catch (Exception _exception)
  3137. {
  3138. if (_exception is COMException)
  3139. {
  3140. hr = (HRESULT)((COMException)_exception).ErrorCode;
  3141. }
  3142. else
  3143. {
  3144. hr = E_UNEXPECTED;
  3145. }
  3146. }
  3147. finally
  3148. {
  3149. if (hr.Succeeded)
  3150. {
  3151. while (m_Filters.Count > 0)
  3152. {
  3153. DSFilter _filter = m_Filters[0];
  3154. m_Filters.RemoveAt(0);
  3155. _filter.Dispose();
  3156. }
  3157. IEnumFilters pEnum;
  3158. if (SUCCEEDED(m_GraphBuilder.EnumFilters(out pEnum)))
  3159. {
  3160. IBaseFilter[] aFilters = new IBaseFilter[1];
  3161. while (S_OK == pEnum.Next(1, aFilters, IntPtr.Zero))
  3162. {
  3163. m_Filters.Add(new DSFilter(aFilters[0]));
  3164. }
  3165. Marshal.ReleaseComObject(pEnum);
  3166. }
  3167. }
  3168. }
  3169. CloseInterfaces();
  3170. return hr;
  3171. }
  3172. private HRESULT CloseInterfaces()
  3173. {
  3174. try
  3175. {
  3176. OnCloseInterfaces();
  3177. if (m_MediaEventEx != null)
  3178. {
  3179. m_MediaEventEx.SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero);
  3180. m_MediaEventEx = null;
  3181. }
  3182. if (m_VideoWindow != null)
  3183. {
  3184. m_VideoWindow.put_Visible(0);
  3185. m_VideoWindow.put_Owner(IntPtr.Zero);
  3186. m_VideoWindow = null;
  3187. }
  3188. m_MediaSeeking = null;
  3189. m_BasicVideo = null;
  3190. m_BasicAudio = null;
  3191. m_MediaControl = null;
  3192. while (m_Filters.Count > 0)
  3193. {
  3194. DSFilter _filter = m_Filters[0];
  3195. m_Filters.RemoveAt(0);
  3196. _filter.Dispose();
  3197. }
  3198. if (m_GraphBuilder != null)
  3199. {
  3200. Marshal.ReleaseComObject(m_GraphBuilder);
  3201. m_GraphBuilder = null;
  3202. }
  3203. GC.Collect();
  3204. return (HRESULT)NOERROR;
  3205. }
  3206. catch
  3207. {
  3208. return (HRESULT)E_FAIL;
  3209. }
  3210. }
  3211. #endregion
  3212. #region Protected Methods
  3213. protected virtual HRESULT Load()
  3214. {
  3215. HRESULT hr = InitInterfaces();
  3216. if (hr.Succeeded)
  3217. {
  3218. if (OnPlaybackPrepared != null) OnPlaybackPrepared(this, EventArgs.Empty);
  3219. }
  3220. return hr;
  3221. }
  3222. protected virtual HRESULT Unload()
  3223. {
  3224. return CloseInterfaces();
  3225. }
  3226. protected virtual HRESULT OnInitInterfaces()
  3227. {
  3228. return (HRESULT)NOERROR;
  3229. }
  3230. protected virtual HRESULT OnCloseInterfaces()
  3231. {
  3232. return (HRESULT)NOERROR;
  3233. }
  3234. protected virtual void SettingUpVideoWindow()
  3235. {
  3236. if (m_VideoWindow != null)
  3237. {
  3238. if (m_VideoControl != null)
  3239. {
  3240. m_VideoWindow.put_Owner(m_VideoControl.Handle);
  3241. m_VideoWindow.put_MessageDrain(m_VideoControl.Handle);
  3242. m_VideoWindow.put_WindowStyle(0x40000000 | 0x04000000);
  3243. ResizeVideoWindow();
  3244. }
  3245. m_VideoWindow.put_AutoShow(Visible ? -1 : 0);
  3246. m_VideoWindow.put_Visible(Visible ? -1 : 0);
  3247. }
  3248. }
  3249. protected virtual HRESULT PreparePlayback()
  3250. {
  3251. m_MediaControl = (IMediaControl)m_GraphBuilder;
  3252. m_BasicVideo = (IBasicVideo)m_GraphBuilder;
  3253. m_MediaSeeking = (IMediaSeeking)m_GraphBuilder;
  3254. m_VideoWindow = (IVideoWindow)m_GraphBuilder;
  3255. m_MediaEventEx = (IMediaEventEx)m_GraphBuilder;
  3256. m_FrameStep = (IVideoFrameStep)m_GraphBuilder;
  3257. SettingUpVideoWindow();
  3258. int hr = m_MediaEventEx.SetNotifyWindow(m_EventControl.Handle, WM_GRAPHNOTIFY, Marshal.GetIUnknownForObject(this));
  3259. Debug.Assert(hr == 0);
  3260. SetVolume(m_bMute ? -10000 : m_iVolume);
  3261. if (m_dRate != 1.0)
  3262. {
  3263. m_MediaSeeking.SetRate(m_dRate);
  3264. m_MediaSeeking.GetRate(out m_dRate);
  3265. }
  3266. if (OnPlaybackReady != null) OnPlaybackReady(this, EventArgs.Empty);
  3267. return (HRESULT)hr;
  3268. }
  3269. protected virtual bool ProcessGraphMessage()
  3270. {
  3271. if (m_MediaEventEx != null)
  3272. {
  3273. int hr = 0;
  3274. int _param1, _param2;
  3275. EventCode _code;
  3276. while (hr == 0)
  3277. {
  3278. hr = m_MediaEventEx.GetEvent(out _code, out _param1, out _param2, 20);
  3279. if (hr == 0)
  3280. {
  3281. hr = m_MediaEventEx.FreeEventParams(_code, _param1, _param2);
  3282. if (_code == EventCode.Complete)
  3283. {
  3284. return true;
  3285. }
  3286. if (_code == EventCode.ErrorAbort)
  3287. {
  3288. return true;
  3289. }
  3290. if (_code == EventCode.DeviceLost)
  3291. {
  3292. return true;
  3293. }
  3294. }
  3295. }
  3296. }
  3297. return false;
  3298. }
  3299. protected virtual void ResizeVideoWindow()
  3300. {
  3301. if (m_VideoWindow != null && m_VideoControl != null)
  3302. {
  3303. m_VideoWindow.SetWindowPosition(0, 0, m_VideoControl.Width, m_VideoControl.Height);
  3304. }
  3305. }
  3306. #endregion
  3307. #region Overridden Methods
  3308. private void VideoControl_VisibleChanged(object sender, EventArgs e)
  3309. {
  3310. if (m_VideoWindow != null && m_VideoControl != null && m_bShouldPreview)
  3311. {
  3312. m_VideoWindow.put_AutoShow(m_VideoControl.Visible ? -1 : 0);
  3313. m_VideoWindow.put_Visible(m_VideoControl.Visible ? -1 : 0);
  3314. }
  3315. }
  3316. private void VideoControl_Resize(object sender, EventArgs e)
  3317. {
  3318. ResizeVideoWindow();
  3319. }
  3320. #endregion
  3321. #region IDisposable Members
  3322. public virtual void Dispose()
  3323. {
  3324. CloseInterfaces();
  3325. }
  3326. #endregion
  3327. }
  3328. [ComVisible(false)]
  3329. public interface ISourceFileSupport
  3330. {
  3331. string FileName { get; set; }
  3332. HRESULT Open();
  3333. HRESULT Open(bool bStart);
  3334. HRESULT Open(string sFileName);
  3335. HRESULT Open(string sFileName, bool bStart);
  3336. }
  3337. [ComVisible(false)]
  3338. public interface IFileDestSupport
  3339. {
  3340. string OutputFileName { get; set; }
  3341. HRESULT Save();
  3342. HRESULT Save(bool bStart);
  3343. HRESULT Save(string sFileName);
  3344. HRESULT Save(string sFileName, bool bStart);
  3345. }
  3346. [ComVisible(false)]
  3347. public class DSFilePlayback : DSFilterGraphBase, ISourceFileSupport
  3348. {
  3349. #region Variables
  3350. protected string m_sFileName = "";
  3351. #endregion
  3352. #region Properties
  3353. public string FileName
  3354. {
  3355. get { return m_sFileName; }
  3356. set { Open(value); }
  3357. }
  3358. #endregion
  3359. #region Public Methods
  3360. public HRESULT Open()
  3361. {
  3362. return Open(true);
  3363. }
  3364. public HRESULT Open(bool bStart)
  3365. {
  3366. return Open(m_sFileName, bStart);
  3367. }
  3368. public HRESULT Open(string sFileName)
  3369. {
  3370. return Open(sFileName, true);
  3371. }
  3372. public virtual HRESULT Open(string sFileName, bool bStart)
  3373. {
  3374. if (sFileName != null && sFileName != "")
  3375. {
  3376. m_sFileName = sFileName;
  3377. if (bStart)
  3378. {
  3379. return Start();
  3380. }
  3381. else
  3382. {
  3383. return Load();
  3384. }
  3385. }
  3386. return E_POINTER;
  3387. }
  3388. #endregion
  3389. #region Overridden Methods
  3390. protected override HRESULT OnInitInterfaces()
  3391. {
  3392. int hr = m_GraphBuilder.RenderFile(m_sFileName, null);
  3393. if (hr < 0) Marshal.ThrowExceptionForHR(hr);
  3394. return (HRESULT)hr;
  3395. }
  3396. #endregion
  3397. }
  3398. #endregion
  3399. }