1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979 |
- using System;
- using System.Runtime.InteropServices;
- using System.Drawing;
- using System.Runtime.InteropServices.ComTypes;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.Security.Permissions;
- using DirectShow;
- using System.Threading;
- using System.ComponentModel;
- using System.Reflection;
- using System.IO;
- namespace Sonic
- {
- #region Basic COM helper utilites classes
- [ComVisible(false)]
- public class COMHelper
- {
- #region BOOL
- public static BOOL TRUE { get { return (BOOL)1; } }
- public static BOOL FALSE { get { return (BOOL)0; } }
- #endregion
- #region FOURCC
- public static FOURCC MAKEFOURCC(char a1, char a2, char a3, char a4) { return new FOURCC(a1, a2, a3, a4); }
- public static FOURCC MAKEFOURCC(byte a1, byte a2, byte a3, byte a4) { return new FOURCC(a1, a2, a3, a4); }
- public static readonly FOURCC BI_RGB = (FOURCC)0;
- #endregion
- #region Helper Methods and constants
- public const int WM_NULL = 0x0000;
- public const int TIME_ONESHOT = 0x0000;
- public const int TIME_PERIODIC = 0x0001;
- public const int TIME_KILL_SYNCHRONOUS = 0x0100;
- public const object NULL = null;
- #if DEBUG
- public static void _TRACE(string _message) { if (!string.IsNullOrEmpty(_message)) Trace.WriteLine(_message); }
- public static void TRACE(string _message) { if (!string.IsNullOrEmpty(_message)) { _message += "\n"; API.OutputDebugString(_message); } }
-
- public static void TRACE_ENTER()
- {
- MethodBase _method = (new StackTrace(1,false)).GetFrame(0).GetMethod();
- TRACE(string.Format("{0}::{1}", _method.ReflectedType.Name, _method.Name));
- }
- 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); }
- #else
- public static void _TRACE(string _message) { }
- public static void TRACE(string _message) { }
- public static void TRACE_ENTER() {}
- public static void ASSERT(object _object) { }
- #endif
- public static bool SUCCEEDED(int hr) { return (hr >= 0); }
- public static bool FAILED(int hr) { return (hr < 0); }
- public static int HIWORD(int _value) { return ((_value >> 16) & 0xffff); }
- public static int LOWORD(int _value) { return (_value & 0xffff); }
- public const long MAX_LONG = 0x7FFFFFFFFFFFFFFF;
- public const long MILLISECONDS = (1000); // 10 ^ 3
- public const long NANOSECONDS = (1000000000); // 10 ^ 9
- public const long UNITS = (NANOSECONDS / 100); // 10 ^ 7
- #region LCID
- public const int LANG_NEUTRAL = 0x00;
- public const int LANG_INVARIANT = 0x7f;
- public const int SUBLANG_NEUTRAL = 0x00;
- public const int SUBLANG_DEFAULT = 0x01;
- public const int SUBLANG_SYS_DEFAULT = 0x02;
- public const int SUBLANG_CUSTOM_DEFAULT = 0x03;
- public const int SUBLANG_CUSTOM_UNSPECIFIED = 0x04;
- public const int SUBLANG_UI_CUSTOM_DEFAULT = 0x05;
- public const int SORT_DEFAULT = 0x0;
- public const int SORT_INVARIANT_MATH = 0x1;
- public static int LANG_SYSTEM_DEFAULT { get { return (MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT)); }}
- public static int LANG_USER_DEFAULT { get { return (MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)); }}
- public static int LOCALE_SYSTEM_DEFAULT { get { return (MAKELCID(LANG_SYSTEM_DEFAULT, SORT_DEFAULT)); }}
- public static int LOCALE_USER_DEFAULT { get { return (MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT)); }}
- public static int LOCALE_CUSTOM_DEFAULT { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_DEFAULT), SORT_DEFAULT)); }}
- public static int LOCALE_CUSTOM_UNSPECIFIED { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_UNSPECIFIED), SORT_DEFAULT)); }}
- public static int LOCALE_CUSTOM_UI_DEFAULT { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_UI_CUSTOM_DEFAULT), SORT_DEFAULT)); }}
- public static int LOCALE_NEUTRAL { get { return (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), SORT_DEFAULT)); }}
- public static int LOCALE_INVARIANT { get { return (MAKELCID(MAKELANGID(LANG_INVARIANT, SUBLANG_NEUTRAL), SORT_DEFAULT)); } }
- public static int MAKELANGID(int p, int s) { return ((((ushort)(s)) << 10) | (ushort)(p)); }
- public static int PRIMARYLANGID(int lgid) { return ((ushort)(lgid) & 0x3ff); }
- public static int SUBLANGID(int lgid) { return ((ushort )(lgid) >> 10); }
- public static int MAKELCID(int lgid,int srtid) { return ((int)((((int)((ushort )(srtid))) << 16) | ((int)((ushort)(lgid))))); }
- public static int MAKESORTLCID(int lgid,int srtid,int ver) { return ((int)((MAKELCID(lgid, srtid)) | (((int)((ushort)(ver))) << 20))); }
- public static int LANGIDFROMLCID(int lcid) { return ((ushort )(lcid)); }
- public static int SORTIDFROMLCID(int lcid) { return ((ushort)((((int)(lcid)) >> 16) & 0xf)); }
- public static int SORTVERSIONFROMLCID(int lcid) { return ((ushort)((((int)(lcid)) >> 20) & 0xf)); }
- #endregion
- #region Align
- public static int ALIGN16(int SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
- public static int ALIGN32(int SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
- public static uint ALIGN16(uint SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
- public static uint ALIGN32(uint SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
- public static long ALIGN16(long SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
- public static long ALIGN32(long SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
- public static ulong ALIGN16(ulong SZ) { unchecked { return (((SZ + 15) >> 4) << 4); } } // round up to a multiple of 16
- public static ulong ALIGN32(ulong SZ) { unchecked { return (((SZ + 31) >> 5) << 5); } } // round up to a multiple of 32
- #endregion
- #endregion
- #region HRESULT
- public static HRESULT S_OK { get { return (HRESULT)0; } }
- public static HRESULT S_FALSE { get { return (HRESULT)1; } }
- public static HRESULT NOERROR { get { return S_OK; } }
- public static HRESULT E_INVALIDARG { get { unchecked { return (HRESULT)0x80070057; } } }
- public static HRESULT E_NOINTERFACE { get { unchecked { return (HRESULT)0x80004002; } } }
- public static HRESULT E_NOTIMPL { get { unchecked { return (HRESULT)0x80004001; } } }
- public static HRESULT E_UNEXPECTED { get { unchecked { return (HRESULT)0x8000FFFF; } } }
- public static HRESULT E_FAIL { get { unchecked { return (HRESULT)0x80004005; } } }
- public static HRESULT E_POINTER { get { unchecked { return (HRESULT)0x80004003; } } }
- public static HRESULT E_OUTOFMEMORY { get { unchecked { return (HRESULT)0x8007000E; } } }
- public static HRESULT E_FILE_NOT_FOUND { get { unchecked { return (HRESULT)0x80070002; } } }
- public static HRESULT VFW_E_NOT_FOUND { get { unchecked { return (HRESULT)0x80040216; } } }
- public static HRESULT VFW_E_CANNOT_CONNECT { get { unchecked { return (HRESULT)0x80040217; } } }
- public static HRESULT VFW_E_NOT_CONNECTED { get { unchecked { return (HRESULT)0x80040209; } } }
- public static HRESULT VFW_E_TYPE_NOT_ACCEPTED { get { unchecked { return (HRESULT)0x8004022A; } } }
- public static HRESULT VFW_E_NO_ACCEPTABLE_TYPES { get { unchecked { return (HRESULT)0x80040207; } } }
- public static HRESULT VFW_E_INVALID_DIRECTION { get { unchecked { return (HRESULT)0x80040208; } } }
- public static HRESULT VFW_E_ALREADY_CONNECTED { get { unchecked { return (HRESULT)0x80040204; } } }
- public static HRESULT VFW_E_NOT_STOPPED { get { unchecked { return (HRESULT)0x80040224; } } }
- public static HRESULT VFW_E_NO_ALLOCATOR { get { unchecked { return (HRESULT)0x8004020A; } } }
- public static HRESULT VFW_S_NO_MORE_ITEMS { get { unchecked { return (HRESULT)0x00040103; } } }
- public static HRESULT VFW_E_INVALIDMEDIATYPE { get { unchecked { return (HRESULT)0x80040200; } } }
- public static HRESULT VFW_E_RUNTIME_ERROR { get { unchecked { return (HRESULT)0x8004020B; } } }
- public static HRESULT VFW_E_WRONG_STATE { get { unchecked { return (HRESULT)0x80040227; } } }
- public static HRESULT VFW_E_ENUM_OUT_OF_SYNC { get { unchecked { return (HRESULT)0x80040203; } } }
- public static HRESULT VFW_E_NO_CLOCK { get { unchecked { return (HRESULT)0x80040213; } } }
- public static HRESULT VFW_E_NOT_IN_GRAPH { get { unchecked { return (HRESULT)0x8004025F; } } }
- public static HRESULT VFW_E_START_TIME_AFTER_END { get { unchecked { return (HRESULT)0x80040228; } } }
- public static HRESULT VFW_S_STATE_INTERMEDIATE { get { unchecked { return (HRESULT)0x00040237; } } }
- public static HRESULT VFW_E_SAMPLE_REJECTED { get { unchecked { return (HRESULT)0x8004022B; } } }
- public static HRESULT VFW_E_STATE_CHANGED { get { unchecked { return (HRESULT)0x80040223; } } }
- public static HRESULT VFW_E_SAMPLE_TIME_NOT_SET { get { unchecked { return (HRESULT)0x80040249; } } }
- public static HRESULT VFW_E_TIMEOUT { get { unchecked { return (HRESULT)0x8004022E; } } }
- public static HRESULT VFW_S_NO_STOP_TIME { get { unchecked { return (HRESULT)0x00040270; } } }
- #endregion
- #region Enum
- [ComVisible(false)]
- [Flags]
- public enum CLSCTX : uint
- {
- CLSCTX_INPROC_SERVER = 0x1,
- CLSCTX_INPROC_HANDLER = 0x2,
- CLSCTX_LOCAL_SERVER = 0x4,
- CLSCTX_INPROC_SERVER16 = 0x8,
- CLSCTX_REMOTE_SERVER = 0x10,
- CLSCTX_INPROC_HANDLER16 = 0x20,
- CLSCTX_RESERVED1 = 0x40,
- CLSCTX_RESERVED2 = 0x80,
- CLSCTX_RESERVED3 = 0x100,
- CLSCTX_RESERVED4 = 0x200,
- CLSCTX_NO_CODE_DOWNLOAD = 0x400,
- CLSCTX_RESERVED5 = 0x800,
- CLSCTX_NO_CUSTOM_MARSHAL = 0x1000,
- CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000,
- CLSCTX_NO_FAILURE_LOG = 0x4000,
- CLSCTX_DISABLE_AAA = 0x8000,
- CLSCTX_ENABLE_AAA = 0x10000,
- CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000,
- CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000,
- CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000,
- CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
- CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER,
- CLSCTX_ALL = CLSCTX_SERVER | CLSCTX_INPROC_HANDLER
- }
- #endregion
- #region API
- [ComVisible(false)]
- public static class API
- {
- [DllImport("kernel32.dll", EntryPoint = "OutputDebugStringW", CharSet = CharSet.Unicode)]
- public static extern void OutputDebugString(string _text);
- [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
- public static extern void CopyMemory(IntPtr Destination, IntPtr Source, [MarshalAs(UnmanagedType.U4)] int Length);
-
- [DllImport("ole32.dll")]
- public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);
- [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
- public static extern int MkParseDisplayName(IBindCtx pbc, string szUserName, ref int pchEaten, out IMoniker ppmk);
- [DllImport("olepro32.dll")]
- public static extern int OleCreatePropertyFrame(
- IntPtr hwndOwner,
- int x,
- int y,
- [MarshalAs(UnmanagedType.LPWStr)] string lpszCaption,
- int cObjects,
- [MarshalAs(UnmanagedType.Interface, ArraySubType = UnmanagedType.IUnknown)]
- ref object ppUnk,
- int cPages,
- IntPtr lpPageClsID,
- int lcid,
- int dwReserved,
- IntPtr lpvReserved
- );
- [DllImport("ole32.dll")]
- public static extern int CoCreateInstance(
- [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
- IntPtr pUnkOuter,
- CLSCTX dwClsContext,
- [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
- out IntPtr rReturnedComObject
- );
- }
- #endregion
- }
- [ComVisible(false)]
- public sealed class BOOL : COMHelper,IComparable,ICloneable
- {
- #region Variables
- private bool m_Result = false;
- #endregion
- #region Constructor
- public BOOL()
- {
- }
- public BOOL(int _value)
- {
- m_Result = _value == 0 ? false : true;
- }
- public BOOL(bool _value)
- {
- m_Result = _value;
- }
- public BOOL(BOOL _value)
- : this(_value.m_Result)
- {
- }
- #endregion
- #region IComparable Members
- public int CompareTo(object obj)
- {
- if (obj is BOOL)
- {
- return m_Result.CompareTo(((BOOL)obj).m_Result);
- }
- return m_Result.CompareTo(obj);
- }
- #endregion
- #region ICloneable Members
- public object Clone()
- {
- return new BOOL(m_Result);
- }
- #endregion
- #region Overridden Methods
- public override bool Equals(object obj)
- {
- if (obj is BOOL)
- {
- return m_Result.Equals(((BOOL)obj).m_Result);
- }
- return m_Result.Equals(obj);
- }
- public override int GetHashCode()
- {
- return m_Result.GetHashCode();
- }
- public override string ToString()
- {
- return m_Result.ToString();
- }
- #endregion
- #region Methods
- public void Assert()
- {
- ASSERT(m_Result);
- }
- #endregion
- #region Static Methods
- public static new BOOL TRUE
- {
- get { return COMHelper.TRUE; }
- }
- public static new BOOL FALSE
- {
- get { return COMHelper.FALSE; }
- }
- #endregion
- #region Operators
- public static bool operator ==(BOOL _src, BOOL _dest)
- {
- if (System.Object.ReferenceEquals(_src, _dest))
- {
- return true;
- }
- if ((_src as Object) == (_dest as Object)) return true;
- if ((_src as Object) != null && (_dest as Object) != null)
- {
- return _src.m_Result == _dest.m_Result;
- }
- return false;
- }
- public static bool operator !=(BOOL _src, BOOL _dest)
- {
- return !(_src == _dest);
- }
- public static bool operator ==(BOOL _src, HRESULT _dest)
- {
- return _src == (BOOL)_dest;
- }
- public static bool operator !=(BOOL _src, HRESULT _dest)
- {
- return !(_src == _dest);
- }
- public static implicit operator bool(BOOL _bool)
- {
- if ((_bool as Object) == null) return false;
- return _bool.m_Result;
- }
- public static implicit operator int(BOOL _bool)
- {
- if ((_bool as Object) == null) return 0;
- return _bool.m_Result ? 1 : 0;
- }
- public static implicit operator HRESULT(BOOL _bool)
- {
- if ((_bool as Object) == null) return S_FALSE;
- return _bool.m_Result ? S_OK : S_FALSE;
- }
- public static explicit operator BOOL(HRESULT hr)
- {
- if ((hr as Object) == null) return FALSE;
- return new BOOL(hr == S_OK);
- }
- public static explicit operator BOOL(int _value)
- {
- return new BOOL(_value != 0);
- }
- public static explicit operator BOOL(bool _bool)
- {
- return new BOOL(_bool);
- }
- #endregion
- }
- [ComVisible(false)]
- public class HRESULT : COMHelper,IComparable,ICloneable
- {
- #region Variables
- private int m_Result = 0;
- #endregion
- #region Constructor
- public HRESULT()
- {
- }
- public HRESULT(int hr)
- {
- m_Result = hr;
- }
- public HRESULT(HRESULT hr)
- : this(hr.m_Result)
- {
- }
- #endregion
- #region Properties
- public bool Failed
- {
- get { return FAILED(m_Result); }
- }
- public bool Succeeded
- {
- get { return SUCCEEDED(m_Result); }
- }
- public string Text
- {
- get { return GetErrorText(); }
- }
- #endregion
- #region IComparable Members
- public int CompareTo(object obj)
- {
- if (obj is HRESULT)
- {
- return m_Result.CompareTo(((HRESULT)obj).m_Result);
- }
- return m_Result.CompareTo(obj);
- }
- #endregion
- #region ICloneable Members
- public object Clone()
- {
- return new HRESULT(m_Result);
- }
- #endregion
- #region Overridden Methods
- public override bool Equals(object obj)
- {
- if (obj is HRESULT)
- {
- return m_Result.Equals(((HRESULT)obj).m_Result);
- }
- return m_Result.Equals(obj);
- }
- public override int GetHashCode()
- {
- return m_Result.GetHashCode();
- }
- public override string ToString()
- {
- string _text = GetErrorText();
- if (_text != "")
- {
- _text = " ( " + _text + " )";
- }
- _text = "0x" + m_Result.ToString("x8") + _text;
- return _text;
- }
- #endregion
- #region Virtual Methods
- protected virtual string GetErrorText()
- {
- string _text = "";
- uint _length = 160;
- IntPtr _ptr = Marshal.AllocCoTaskMem((int)_length);
- try
- {
- if (AMGetErrorText(m_Result, _ptr, _length) > 0)
- {
- _text = Marshal.PtrToStringAuto(_ptr);
- }
- }
- catch
- {
- }
- Marshal.FreeCoTaskMem(_ptr);
- return _text;
- }
- #endregion
- #region Methods
- public void Assert()
- {
- ASSERT(SUCCEEDED(m_Result));
- }
- public void Throw()
- {
- if (FAILED(m_Result))
- {
- Marshal.ThrowExceptionForHR(m_Result);
- }
- }
- public void TraceWrite()
- {
- if (Failed)
- {
- StackFrame _frame = (new StackTrace(1, true)).GetFrame(0);
- MethodBase _method = _frame.GetMethod();
- string _file = _frame.GetFileName();
- _file = !string.IsNullOrEmpty(_file) ? Path.GetFileName(_file) : "";
- TRACE(string.Format("-- ASSERT -- Method {0}::{1}, HRESULT : {2},File : {3} Line : {4} ",
- _method.ReflectedType.Name,
- _method.Name,
- ToString(),
- _file,
- _frame.GetFileLineNumber()
- ));
- }
- }
- #endregion
- #region Imports
- [DllImport("quartz.dll", EntryPoint = "AMGetErrorText", CharSet = CharSet.Auto)]
- private static extern uint AMGetErrorText(int hr,IntPtr sText,[In] uint _length);
- #endregion
- #region Operators
- public static bool operator ==(HRESULT _src, HRESULT _dest)
- {
- if (System.Object.ReferenceEquals(_src, _dest))
- {
- return true;
- }
- if ((_src as Object) == (_dest as Object)) return true;
- if ((_src as Object) != null && (_dest as Object) != null)
- {
- return _src.m_Result == _dest.m_Result;
- }
- return false;
- }
- public static bool operator !=(HRESULT _src, HRESULT _dest)
- {
- return !(_src == _dest);
- }
- public static implicit operator bool(HRESULT hr)
- {
- if ((hr as Object) == null) return true;
- return (SUCCEEDED(hr.m_Result));
- }
- public static implicit operator int(HRESULT hr)
- {
- if ((hr as Object) == null) return 0;
- return hr.m_Result;
- }
- public static explicit operator HRESULT(int hr)
- {
- return new HRESULT(hr);
- }
- public static explicit operator HRESULT(uint hr)
- {
- unchecked
- {
- return new HRESULT((int)hr);
- }
- }
- #endregion
- }
- [ComVisible(false)]
- public sealed class FOURCC : COMHelper, IComparable, ICloneable
- {
- #region Constants
- private const ushort m_usB = 0x0000;
- private const ushort m_usC = 0x0010;
- private static readonly byte[] m_abtD = { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
- #endregion
- #region Variables
- private uint m_uiFourcc = 0;
- #endregion
- #region Constructors
- public FOURCC(int _value)
- {
- unchecked
- {
- m_uiFourcc = (uint)_value;
- }
- }
- public FOURCC(uint _value)
- {
- m_uiFourcc = _value;
- }
- public FOURCC(Guid _guid)
- {
- byte[] _data = _guid.ToByteArray();
- byte[] _result = { _data[0], _data[1], _data[2], _data[3] };
- m_uiFourcc = BitConverter.ToUInt32(_result, 0);
- }
- public FOURCC(string _value)
- {
- byte[] _data = new byte[4];
- for (int i = 0; i < _data.Length; i++)
- {
- if (i < _value.Length)
- {
- _data[i] = (byte)_value[i];
- }
- else
- {
- _data[i] = 0;
- }
- }
- m_uiFourcc = BitConverter.ToUInt32(_data, 0);
- }
- public FOURCC(byte[] _value)
- {
- byte[] _data = new byte[4];
- for (int i = 0; i < _data.Length; i++)
- {
- if (i < _value.Length)
- {
- _data[i] = _value[i];
- }
- else
- {
- _data[i] = 0;
- }
- }
- m_uiFourcc = BitConverter.ToUInt32(_data,0);
- }
- public FOURCC(char[] _value)
- {
- byte[] _data = new byte[4];
- for (int i = 0; i < _data.Length; i++)
- {
- if (i < _value.Length)
- {
- _data[i] = (byte)_value[i];
- }
- else
- {
- _data[i] = 0;
- }
- }
- m_uiFourcc = BitConverter.ToUInt32(_data, 0);
- }
- public FOURCC(char a1, char a2, char a3, char a4)
- {
- byte[] _data = new byte[] { (byte)a1, (byte)a2, (byte)a3, (byte)a4 };
- m_uiFourcc = BitConverter.ToUInt32(_data, 0);
- }
- public FOURCC(byte a1,byte a2,byte a3,byte a4)
- {
- byte[] _data = new byte[] { a1,a2,a3,a4 };
- m_uiFourcc = BitConverter.ToUInt32(_data, 0);
- }
- #endregion
- #region Overridden Methods
- public override bool Equals(object obj)
- {
- if (obj is FOURCC)
- {
- return m_uiFourcc.Equals(((FOURCC)obj).m_uiFourcc);
- }
- return m_uiFourcc.Equals(obj);
- }
- public override int GetHashCode()
- {
- return m_uiFourcc.GetHashCode();
- }
- public override string ToString()
- {
- byte[] _data = BitConverter.GetBytes(m_uiFourcc);
- string _result = " ( ";
- for (int i = 0; i < _data.Length; i++)
- {
- if (_data[i] <= 30 || _data[i] > 128)
- {
- _result = "";
- break;
- }
- else
- {
- _result += (char)_data[i];
- }
- }
- if (_result != "")
- {
- _result += " )";
- }
- return m_uiFourcc.ToString("x4") + _result;
- }
- #endregion
- #region IComparable Members
- public int CompareTo(object obj)
- {
- if (obj is FOURCC)
- {
- return m_uiFourcc.CompareTo(((FOURCC)obj).m_uiFourcc);
- }
- return m_uiFourcc.CompareTo(obj);
- }
- #endregion
- #region ICloneable Members
- public object Clone()
- {
- return new FOURCC(m_uiFourcc);
- }
- #endregion
- #region Operators
- public static bool operator ==(FOURCC _src, FOURCC _dest)
- {
- if (System.Object.ReferenceEquals(_src, _dest))
- {
- return true;
- }
- if ((_src as Object) == (_dest as Object)) return true;
- if ((_src as Object) != null && (_dest as Object) != null)
- {
- return _src.m_uiFourcc == _dest.m_uiFourcc;
- }
- return false;
- }
- public static bool operator !=(FOURCC _src, FOURCC _dest)
- {
- return !(_src == _dest);
- }
- public static explicit operator FOURCC(int _value)
- {
- return new FOURCC(_value);
- }
- public static explicit operator FOURCC(uint _value)
- {
- return new FOURCC(_value);
- }
- public static explicit operator FOURCC(Guid _value)
- {
- return new FOURCC(_value);
- }
- public static explicit operator FOURCC(string _value)
- {
- if (_value == null) return new FOURCC(0);
- return new FOURCC(_value);
- }
- public static explicit operator FOURCC(Array _value)
- {
- if (_value == null) return new FOURCC(0);
- byte[] _data = new byte[4];
- for (int i = 0; i < _data.Length; i++)
- {
- if (i < _value.Length)
- {
- _data[i] = (byte)Convert.ToByte(_value.GetValue(i));
- }
- else
- {
- _data[i] = 0;
- }
- }
- return new FOURCC(_data);
- }
- public static implicit operator int(FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return 0;
- unchecked
- {
- return (int)_fcc.m_uiFourcc;
- }
- }
- public static implicit operator uint(FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return 0;
- return _fcc.m_uiFourcc;
- }
- public static implicit operator byte[](FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return null;
- byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
- byte[] _result = new byte[_data.Length];
- for (int i = 0; i < _data.Length; i++)
- {
- _result[3 - i] = _data[i];
- }
- return _result;
- }
- public static implicit operator char[](FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return null;
- byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
- char[] _result = new char[_data.Length];
- for (int i = 0; i < _data.Length; i++)
- {
- _result[3 - i] = (char)_data[i];
- }
- return _result;
- }
- public static implicit operator string(FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return "";
- byte[] _data = BitConverter.GetBytes(_fcc.m_uiFourcc);
- string _result = "";
- for (int i = 0; i < _data.Length; i++)
- {
- _result += (char)_data[i];
- }
- return _result;
- }
- public static implicit operator Guid(FOURCC _fcc)
- {
- if ((_fcc as Object) == null) return Guid.Empty;
- 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]);
- }
- #endregion
- }
- [ComVisible(false)]
- [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
- public class VTableInterface : COMHelper, IDisposable
- {
- #region Delegates
- private delegate int QueryInterfaceProc(
- IntPtr pUnk,
- ref Guid riid,
- out IntPtr ppvObject
- );
- private delegate uint AddRefProc(IntPtr pUnk);
- #endregion
- #region Variables
- protected IntPtr m_pUnknown = IntPtr.Zero;
- protected bool m_bShouldRelease = false;
- #endregion
- #region Properties
- public IntPtr UnknownPtr
- {
- get { return m_pUnknown; }
- }
- public bool IsValid
- {
- get { return m_pUnknown != IntPtr.Zero; }
- }
- #endregion
- #region Constructor
- protected VTableInterface()
- : this(IntPtr.Zero, false)
- {
- }
- protected VTableInterface(IntPtr pUnknown, Type _type)
- : this(pUnknown, _type.GUID)
- {
- }
- protected VTableInterface(IntPtr pUnknown, Guid _guid)
- : this(IntPtr.Zero, false)
- {
- HRESULT hr = (HRESULT)Marshal.QueryInterface(pUnknown, ref _guid, out m_pUnknown);
- if (hr.Succeeded)
- {
- Marshal.Release(pUnknown);
- }
- }
- protected VTableInterface(IntPtr pUnknown)
- : this(pUnknown, false)
- {
- }
- protected VTableInterface(IntPtr pUnknown, bool bAddRef)
- {
- if (pUnknown != IntPtr.Zero)
- {
- m_pUnknown = pUnknown;
- if (bAddRef)
- {
- m_bShouldRelease = true;
- _AddRef();
- }
- }
- }
- protected VTableInterface(object _object, Guid _guid)
- : this(IntPtr.Zero, false)
- {
- if (_object != null)
- {
- IntPtr pUnknown = Marshal.GetIUnknownForObject(_object);
- HRESULT hr = (HRESULT)Marshal.QueryInterface(pUnknown, ref _guid, out m_pUnknown);
- if (hr.Succeeded)
- {
- Marshal.Release(pUnknown);
- }
- }
- }
- protected VTableInterface(object _object, Type _type)
- : this(_object, _type.GUID)
- {
-
- }
- ~VTableInterface()
- {
- Dispose();
- }
- #endregion
- #region IUnknown
- public HRESULT _QueryInterface(ref Guid riid, out IntPtr ppvObject)
- {
- ppvObject = IntPtr.Zero;
- if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;
- QueryInterfaceProc _Proc = GetProcDelegate<QueryInterfaceProc>(0);
- if (_Proc == null) return E_UNEXPECTED;
- return (HRESULT)_Proc(
- m_pUnknown,
- ref riid,
- out ppvObject
- );
- }
- public uint _AddRef()
- {
- if (m_pUnknown == IntPtr.Zero) return 0;
- AddRefProc _Proc = GetProcDelegate<AddRefProc>(1);
-
- if (_Proc == null) return 0;
- return _Proc(m_pUnknown);
- }
- public uint _Release()
- {
- if (m_pUnknown == IntPtr.Zero) return 0;
- AddRefProc _Proc = GetProcDelegate<AddRefProc>(2);
- if (_Proc == null) return 0;
- return _Proc(m_pUnknown);
- }
- public T _GetObject<T>() where T : class
- {
- Guid _guid = typeof(T).GUID;
- IntPtr _ptr;
- if (SUCCEEDED(_QueryInterface(ref _guid, out _ptr)))
- {
- _Release();
- return (T)Marshal.GetObjectForIUnknown(_ptr);
- }
- return null;
- }
- #endregion
- #region Methods
- public HRESULT IsInterface(Guid _guid)
- {
- IntPtr pvObject;
- HRESULT hr = _QueryInterface(ref _guid, out pvObject);
- if (hr.Succeeded)
- {
- _Release();
- }
- return hr;
- }
- public HRESULT IsInterface(Type _type)
- {
- return IsInterface(_type.GUID);
- }
- public T GetInterfaceImpl<T>(Guid _guid) where T : VTableInterface, new()
- {
- IntPtr pUnknown;
- if (SUCCEEDED(_QueryInterface(ref _guid,out pUnknown)))
- {
- T pT = new T();
- VTableInterface _interface = (VTableInterface)pT;
- _interface.m_pUnknown = pUnknown;
- return pT;
- }
- return null;
- }
- public T GetInterfaceImpl<T>(Type _type) where T : VTableInterface, new()
- {
- return GetInterfaceImpl<T>(_type.GUID);
- }
- public T GetInterfaceImpl<T>() where T : VTableInterface, new()
- {
- try
- {
- return GetInterfaceImpl<T>(typeof(T));
- }
- catch
- {
- }
- return null;
- }
- #endregion
- #region Helper Methods
- protected T TryCast<T>() where T : class
- {
- try
- {
- T _interface = (T)Marshal.GetTypedObjectForIUnknown(m_pUnknown, typeof(T));
- return _interface;
- }
- catch
- {
- }
- return null;
- }
- protected T GetProcDelegate<T>(int nIndex) where T : class
- {
- IntPtr pVtable = Marshal.ReadIntPtr(m_pUnknown);
- IntPtr pFunc = Marshal.ReadIntPtr(pVtable, nIndex * IntPtr.Size);
- return (Marshal.GetDelegateForFunctionPointer(pFunc, typeof(T))) as T;
- }
- #endregion
- #region IDisposable Members
- public void Dispose()
- {
- if (m_pUnknown != IntPtr.Zero)
- {
- if (m_bShouldRelease) _Release();
- m_pUnknown = IntPtr.Zero;
- }
- }
- #endregion
- }
- [ComVisible(false)]
- [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
- public class IUnknownImpl : VTableInterface
- {
- #region Constructor
- public IUnknownImpl(IntPtr pUnknown)
- : base(pUnknown,false)
- {
- }
- #endregion
- #region Methods
- public new T GetProcDelegate<T>(int nIndex) where T : class
- {
- return base.GetProcDelegate<T>(nIndex);
- }
- #endregion
- }
- #endregion
- #region Base COM object
- [ComVisible(false)]
- [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
- public class DSObject<T> : COMHelper, IDisposable where T : class
- {
- #region Variables
- protected T m_pUnknown = null;
- protected bool m_bReleaseOnDestroy = false;
- #endregion
- #region Properties
- public T Value
- {
- get { return m_pUnknown; }
- set { m_pUnknown = value; }
- }
- public bool IsReleased
- {
- get { return m_pUnknown == null; }
- }
- public bool IsValid
- {
- get { return !IsReleased; }
- }
- public bool ReleaseOnDestroy
- {
- get { return m_bReleaseOnDestroy; }
- set { m_bReleaseOnDestroy = value; }
- }
- public Guid GUID
- {
- get { return typeof(T).GUID; }
- }
- public object this[Type _type]
- {
- get { return QueryInterface(_type); }
- }
- #endregion
- #region Constructor
- public DSObject()
- {
- Guid _guid = this.GetType().GUID;
- if (_guid != Guid.Empty)
- {
- IntPtr _ptr;
- HRESULT hr = (HRESULT)API.CoCreateInstance(
- _guid,
- IntPtr.Zero,
- CLSCTX.CLSCTX_INPROC_SERVER,
- typeof(T).GUID,
- out _ptr);
- if (hr.Succeeded)
- {
- m_pUnknown = Marshal.GetObjectForIUnknown(_ptr) as T;
- m_bReleaseOnDestroy = true;
- }
- }
- }
- public DSObject(Guid _guid)
- {
- if (_guid != Guid.Empty)
- {
- IntPtr _ptr;
- HRESULT hr = (HRESULT)API.CoCreateInstance(
- _guid,
- IntPtr.Zero,
- CLSCTX.CLSCTX_INPROC_SERVER,
- typeof(T).GUID,
- out _ptr);
- if (hr.Succeeded)
- {
- m_pUnknown = Marshal.GetObjectForIUnknown(_ptr) as T;
- m_bReleaseOnDestroy = true;
- }
- }
- }
- protected DSObject(IntPtr _ptr)
- {
- if (_ptr != IntPtr.Zero)
- {
- m_pUnknown = (T)Marshal.GetObjectForIUnknown(_ptr);
- m_bReleaseOnDestroy = true;
- }
- }
- protected DSObject(T _unknown)
- {
- m_pUnknown = _unknown;
- m_bReleaseOnDestroy = true;
- }
- protected DSObject(T _unknown, bool bReleaseOnDestroy)
- : this(_unknown)
- {
- m_bReleaseOnDestroy = bReleaseOnDestroy;
- }
- ~DSObject()
- {
- Dispose();
- }
- #endregion
- #region Overriden Methods
- public override bool Equals(object obj)
- {
- if (obj == m_pUnknown)
- {
- return true;
- }
- if (obj is DSObject<T>)
- {
- return (obj as DSObject<T>).m_pUnknown == m_pUnknown;
- }
- return base.Equals(obj);
- }
- public override int GetHashCode()
- {
- if (m_pUnknown != null) return m_pUnknown.GetHashCode();
- return base.GetHashCode();
- }
- #endregion
- #region IDisposable Members
- public virtual void Dispose()
- {
- if (m_bReleaseOnDestroy && m_pUnknown != null)
- {
- if (Marshal.IsComObject(m_pUnknown))
- {
- Marshal.ReleaseComObject(m_pUnknown);
- }
- m_pUnknown = null;
- }
- }
- #endregion
- #region Public Methods
- public object QueryInterface(Guid _guid)
- {
- object _object = null;
- if (m_pUnknown != null)
- {
- IntPtr _this = Marshal.GetIUnknownForObject(m_pUnknown);
- IntPtr _interface = IntPtr.Zero;
- try
- {
- int hr = Marshal.QueryInterface(_this, ref _guid, out _interface);
- if (SUCCEEDED(hr) && _interface != IntPtr.Zero)
- {
- _object = Marshal.GetObjectForIUnknown(_interface);
- }
- }
- finally
- {
- Marshal.Release(_this);
- if (_interface != IntPtr.Zero) Marshal.Release(_interface);
- }
- }
- return _object;
- }
- public object QueryInterface(Type _type)
- {
- return QueryInterface(_type.GUID);
- }
- public bool IsSupported(Guid _guid)
- {
- if (m_pUnknown != null)
- {
- IntPtr _this = Marshal.GetIUnknownForObject(m_pUnknown);
- IntPtr _interface = IntPtr.Zero;
- try
- {
- return SUCCEEDED(Marshal.QueryInterface(_this, ref _guid, out _interface));
- }
- finally
- {
- Marshal.Release(_this);
- if (_interface != IntPtr.Zero)
- {
- Marshal.Release(_interface);
- }
- }
- }
- return false;
- }
- public bool IsSupported(Type _type)
- {
- Guid _guid = _type.GUID;
- if (_guid != Guid.Empty)
- {
- return IsSupported(_guid);
- }
- return false;
- }
- #endregion
- #region Operators
- public static bool operator ==(DSObject<T> _src, DSObject<T> _dest)
- {
- if (System.Object.ReferenceEquals(_src, _dest))
- {
- return true;
- }
- if ((_src as Object) == (_dest as Object)) return true;
- if (((_src as Object) == null || _src.m_pUnknown == null)
- && ((_dest as Object) == null || _dest.m_pUnknown == null))
- {
- return true;
- }
- if ((_src as Object) != null && (_dest as Object) != null)
- {
- return _src.m_pUnknown == _dest.m_pUnknown;
- }
- return false;
- }
- public static bool operator !=(DSObject<T> _src, DSObject<T> _dest)
- {
- return !(_src == _dest);
- }
- public static implicit operator T(DSObject<T> _object)
- {
- if (((object)_object) == null)
- {
- return null;
- }
- return _object.m_pUnknown;
- }
- public static implicit operator IntPtr(DSObject<T> _object)
- {
- if (((_object as Object) == null) || _object.m_pUnknown == null)
- {
- return IntPtr.Zero;
- }
- return Marshal.GetIUnknownForObject(_object.m_pUnknown);
- }
- public static implicit operator bool(DSObject<T> _object)
- {
- return (((object)_object) != null && _object.m_pUnknown != null);
- }
- public static explicit operator DSObject<T>(IntPtr _ptr)
- {
- return new DSObject<T>(_ptr);
- }
- public static explicit operator DSObject<T>(T _object)
- {
- return new DSObject<T>(_object);
- }
- #endregion
- }
- #endregion
- #region Base Moniker Enumerator object
- [ComVisible(false)]
- public class MonikerInfo : DSObject<IMoniker>
- {
- #region Fields
- public Guid ClassID
- {
- get
- {
- Guid _guid = Guid.Empty;
- if (m_pUnknown != null)
- {
- m_pUnknown.GetClassID(out _guid);
- }
- return _guid;
- }
- }
- public string Name
- {
- get { return GetPropBagValue("FriendlyName"); }
- }
- public string DevicePath
- {
- get
- {
- string _out = "";
- if (m_pUnknown != null)
- {
- m_pUnknown.GetDisplayName(null, null, out _out);
- }
- return _out;
- }
- }
- public string this[string _value]
- {
- get { return GetPropBagValue(_value); }
- }
- #endregion
- #region Constructor
- public MonikerInfo()
- : base(IntPtr.Zero)
- {
- }
- public MonikerInfo(IMoniker _moniker)
- : base(_moniker)
- {
- }
- public MonikerInfo(string _moniker)
- {
- IBindCtx _context = null;
- try
- {
- if (SUCCEEDED(API.CreateBindCtx(0, out _context)))
- {
- int n = 0;
- ASSERT(SUCCEEDED(API.MkParseDisplayName(_context, _moniker, ref n, out m_pUnknown)));
- }
- }
- finally
- {
- if (_context != null)
- {
- Marshal.ReleaseComObject(_context);
- }
- }
- }
- #endregion
- #region Public Methods
- public object Bind(Type _type)
- {
- object _object = null;
- if (m_pUnknown != null)
- {
- Guid _guid = _type.GUID;
- if (_guid != Guid.Empty)
- {
- try
- {
- m_pUnknown.BindToObject(null, null, ref _guid, out _object);
- }
- catch
- {
- _object = null;
- }
- }
- }
- return _object;
- }
- #endregion
- #region Protected Methods
- protected string GetPropBagValue(string sPropName)
- {
- if (m_pUnknown == null) return null;
- IPropertyBag _property = null;
- try
- {
- object _object = null;
- Guid _guid = typeof(IPropertyBag).GUID;
- m_pUnknown.BindToStorage(null, null, ref _guid, out _object);
- _property = (IPropertyBag)_object;
- object _value;
- int hr = _property.Read(sPropName, out _value, null);
- ASSERT(hr == S_OK);
- if (SUCCEEDED(hr))
- {
- return _value as string;
- }
- }
- catch
- {
- }
- finally
- {
- if (_property != null)
- {
- Marshal.ReleaseComObject(_property);
- _property = null;
- }
- }
- return null;
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSEnumerator<T> : DSObject<ICreateDevEnum>, IEnumerable<T> where T : MonikerInfo, new()
- {
- #region Variabless
- protected List<T> m_Objects = new List<T>();
- protected IEnumMoniker m_pEnumMoniker = null;
- protected Guid m_Category = Guid.Empty;
- #endregion
- #region Properties
- public Guid Category
- {
- get { return m_Category; }
- }
- public T this[int index]
- {
- get { return m_Objects[index]; }
- }
- public T this[string name]
- {
- get
- {
- foreach (T _info in m_Objects)
- {
- if (_info.Name == name || _info.DevicePath == name)
- {
- return _info;
- }
- }
- return null;
- }
- }
- public T this[Guid _clsid]
- {
- get
- {
- foreach (T _info in m_Objects)
- {
- if (_info.ClassID == _clsid)
- {
- return _info;
- }
- }
- return null;
- }
- }
- public int Count
- {
- get { return m_Objects.Count; }
- }
- public List<T> Objects
- {
- get { return m_Objects; }
- }
- #endregion
- #region Constructor
- public DSEnumerator(Guid _category)
- : base(new Guid("62BE5D10-60EB-11d0-BD3B-00A0C911CE86"))
- {
- int hr = m_pUnknown.CreateClassEnumerator(ref _category, out m_pEnumMoniker, 0);
- ASSERT(hr == S_OK);
- if (SUCCEEDED(hr))
- {
- if (hr != S_FALSE)
- {
- IMoniker[] aMonikers = new IMoniker[1];
- while ((m_pEnumMoniker.Next(1, aMonikers, IntPtr.Zero) == 0))
- {
- T _moniker = new T();
- _moniker.Value = aMonikers[0];
- m_Objects.Add(_moniker);
- }
- }
- }
- }
- #endregion
- #region IDisposable Members
- public override void Dispose()
- {
- if (m_pEnumMoniker != null)
- {
- Marshal.ReleaseComObject(m_pEnumMoniker);
- m_pEnumMoniker = null;
- }
- while (m_Objects.Count > 0)
- {
- T _moniker = m_Objects[0];
- _moniker.Dispose();
- m_Objects.RemoveAt(0);
- }
- base.Dispose();
- }
- #endregion
- #region IEnumerable<T> Members
- public IEnumerator<T> GetEnumerator()
- {
- return m_Objects.GetEnumerator();
- }
- #endregion
- #region IEnumerable Members
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return m_Objects.GetEnumerator();
- }
- #endregion
- }
- #endregion
- #region Device Categories
- [ComVisible(false)]
- public class DSDevice : MonikerInfo
- {
- #region Fields
- public DSFilter Filter
- {
- get
- {
- DSFilter _filter = null;
- if (m_pUnknown != null)
- {
- _filter = new DSFilter(this.DevicePath);
- if (_filter.IsReleased)
- {
- _filter.Dispose();
- _filter = null;
- }
- }
- return _filter;
- }
- }
- #endregion
- #region Constructor
- public DSDevice()
- {
- }
- #endregion
- #region Overridden Methods
- public override string ToString()
- {
- string _value = this.Name;
- if (_value != null && _value != "") return _value;
- _value = this.DevicePath;
- if (_value != null && _value != "") return _value;
- return base.ToString();
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSCategory : DSEnumerator<DSDevice>
- {
- #region Properties
- public new DSFilter this[int index]
- {
- get
- {
- DSDevice _device = base[index];
- if (_device)
- {
- return _device.Filter;
- }
- return null;
- }
- }
- public new DSFilter this[string name]
- {
- get
- {
- DSDevice _device = base[name];
- if (_device)
- {
- return _device.Filter;
- }
- return null;
- }
- }
- public new DSFilter this[Guid _clsid]
- {
- get
- {
- DSDevice _device = base[_clsid];
- if (_device)
- {
- return _device.Filter;
- }
- return null;
- }
- }
- #endregion
- #region Constructor
- public DSCategory(Guid _category)
- : base(_category)
- {
- }
- #endregion
- }
- #endregion
- #region Pin Helper
- [ComVisible(false)]
- public class DSPin : DSObject<IPin>
- {
- #region Constructor
- public DSPin(IntPtr _pin)
- : base(_pin)
- {
- }
- public DSPin(IPin _pin)
- : base(_pin)
- {
- }
- #endregion
- #region Properties
- public PinDirection Direction
- {
- get
- {
- PinDirection _direction;
- m_pUnknown.QueryDirection(out _direction);
- return _direction;
- }
- }
- public bool IsConnected
- {
- get
- {
- return ((ConnectedTo != null) && ConnectedTo == true);
- }
- }
- public string Name
- {
- get
- {
- if (this)
- {
- PinInfo _info;
- if (SUCCEEDED(m_pUnknown.QueryPinInfo(out _info)))
- {
- _info.filter = null;
- return _info.name;
- }
- }
- return "";
- }
- }
- public DSFilter Filter
- {
- get
- {
- if (this)
- {
- PinInfo _info;
- if (SUCCEEDED(m_pUnknown.QueryPinInfo(out _info)))
- {
- return new DSFilter(_info.filter);
- }
- }
- return null;
- }
- }
- public DSPin ConnectedTo
- {
- get
- {
- if (this)
- {
- IntPtr pConnected;
- if (SUCCEEDED(m_pUnknown.ConnectedTo(out pConnected)))
- {
- return new DSPin(pConnected);
- }
- }
- return null;
- }
- }
- public AMMediaType ConnectionMediaType
- {
- get
- {
- if (this)
- {
- AMMediaType mt = new AMMediaType();
- if (SUCCEEDED(m_pUnknown.ConnectionMediaType(mt)))
- {
- return mt;
- }
- mt.Free();
- }
- return null;
- }
- }
- public AMMediaType Format
- {
- get
- {
- if (this)
- {
- IAMStreamConfig _config = (IAMStreamConfig)QueryInterface(typeof(IAMStreamConfig));
- if (_config != null)
- {
- AMMediaType mt;
- if (SUCCEEDED(_config.GetFormat(out mt)))
- {
- return mt;
- }
- }
- }
- return null;
- }
- set
- {
- if (this)
- {
- IAMStreamConfig _config = (IAMStreamConfig)QueryInterface(typeof(IAMStreamConfig));
- if (_config != null)
- {
- int hr = _config.SetFormat(value);
- ASSERT(SUCCEEDED(hr));
- }
- }
- }
- }
- public List<AMMediaType> MediaTypes
- {
- get
- {
- List<AMMediaType> _list = new List<AMMediaType>();
- if (this)
- {
- IEnumMediaTypes pEnum;
- IntPtr _ptr;
- if (SUCCEEDED(m_pUnknown.EnumMediaTypes(out _ptr)))
- {
- pEnum = (IEnumMediaTypes)Marshal.GetObjectForIUnknown(_ptr);
- IntPtr ulMediaCount = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(uint)));
- IntPtr pTypes = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
- try
- {
- do
- {
- int hr = pEnum.Next(1, pTypes, ulMediaCount);
- if (hr != S_OK)
- {
- break;
- }
- ASSERT(Marshal.ReadInt32(ulMediaCount) == 1);
- IntPtr _ptrStructure = Marshal.ReadIntPtr(pTypes);
- try
- {
- if (_ptrStructure != IntPtr.Zero)
- {
- _list.Add((AMMediaType)Marshal.PtrToStructure(_ptrStructure, typeof(AMMediaType)));
- }
- }
- finally
- {
- if (_ptrStructure != IntPtr.Zero)
- {
- Marshal.FreeCoTaskMem(_ptrStructure);
- }
- }
- }
- while (true);
- }
- finally
- {
- Marshal.FreeCoTaskMem(ulMediaCount);
- Marshal.FreeCoTaskMem(pTypes);
- }
- }
- }
- return _list;
- }
- }
- public Guid Category
- {
- get
- {
- Guid _result = Guid.Empty;
- int iSize = Marshal.SizeOf(typeof(Guid));
- IntPtr ipOut = Marshal.AllocCoTaskMem(iSize);
- try
- {
- int cbBytes;
- IKsPropertySet pKs = (IKsPropertySet)QueryInterface(typeof(IKsPropertySet));
- if (pKs != null)
- {
- Guid g = PropSetID.Pin;
- int hr = pKs.Get(g, (int)AMPropertyPin.Category, IntPtr.Zero, 0, ipOut, iSize, out cbBytes);
- if (SUCCEEDED(hr))
- {
- _result = (Guid)Marshal.PtrToStructure(ipOut, typeof(Guid));
- }
- }
- }
- finally
- {
- Marshal.FreeCoTaskMem(ipOut);
- ipOut = IntPtr.Zero;
- }
- return _result;
- }
- }
- public AMMediaType this[int index]
- {
- get
- {
- try
- {
- return MediaTypes[index];
- }
- catch
- {
- return null;
- }
- }
- }
- public int Count
- {
- get { return MediaTypes.Count; }
- }
- #endregion
- #region Methods
- public HRESULT ConnectDirect(DSPin _pin)
- {
- try
- {
- if (!_pin)
- {
- return Render();
- }
- if (!this) return E_POINTER;
- IGraphBuilder _graph = Filter.FilterGraph;
- if (_graph != null)
- {
- if (Direction != _pin.Direction)
- {
- if (Direction == PinDirection.Input)
- {
- return (HRESULT)_graph.ConnectDirect(_pin.m_pUnknown, m_pUnknown, null);
- }
- else
- {
- return (HRESULT)_graph.ConnectDirect(m_pUnknown, _pin.m_pUnknown, null);
- }
- }
- return VFW_E_INVALID_DIRECTION;
- }
- return VFW_E_NOT_IN_GRAPH;
- }
- catch (Exception _exception)
- {
- return (HRESULT)Marshal.GetHRForException(_exception);
- }
- }
- public HRESULT Connect(DSPin _pin)
- {
- try
- {
- HRESULT hr = ConnectDirect(_pin);
- if (hr.Succeeded)
- {
- return hr;
- }
- if (!_pin)
- {
- return Render();
- }
- if (!this) return E_POINTER;
- IGraphBuilder _graph = Filter.FilterGraph;
- if (_graph != null)
- {
- if (Direction != _pin.Direction)
- {
- if (Direction == PinDirection.Input)
- {
- return (HRESULT)_graph.Connect(_pin.m_pUnknown, m_pUnknown);
- }
- else
- {
- return (HRESULT)_graph.Connect(m_pUnknown, _pin.m_pUnknown);
- }
- }
- return VFW_E_INVALID_DIRECTION;
- }
- return VFW_E_NOT_IN_GRAPH;
- }
- catch (Exception _exception)
- {
- return (HRESULT)Marshal.GetHRForException(_exception);
- }
- }
- public HRESULT Disconnect()
- {
- DSPin _pin = null;
- try
- {
- if (!this) return E_POINTER;
- IGraphBuilder _graph = Filter.FilterGraph;
- if (_graph != null)
- {
- _pin = ConnectedTo;
- if (!_pin) return VFW_E_NOT_CONNECTED;
- int hr = _graph.Disconnect(m_pUnknown);
- if (SUCCEEDED(hr))
- {
- return (HRESULT)_graph.Disconnect(_pin.m_pUnknown);
- }
- return (HRESULT)hr;
- }
- return VFW_E_NOT_IN_GRAPH;
- }
- catch (Exception _exception)
- {
- return (HRESULT)Marshal.GetHRForException(_exception);
- }
- finally
- {
- if (_pin)
- {
- _pin.Dispose();
- _pin = null;
- }
- }
- }
- public HRESULT Render()
- {
- try
- {
- if (!this) return E_POINTER;
- IGraphBuilder _graph = Filter.FilterGraph;
- if (_graph != null)
- {
- return (HRESULT)_graph.Render(m_pUnknown);
- }
- return VFW_E_NOT_IN_GRAPH;
- }
- catch (Exception _exception)
- {
- return (HRESULT)Marshal.GetHRForException(_exception);
- }
- }
- public HRESULT IsAccepted(AMMediaType mt)
- {
- if (this)
- {
- return (HRESULT)m_pUnknown.QueryAccept(mt);
- }
- return (HRESULT)E_FAIL;
- }
- public DSPin RemoveFiltersChain()
- {
- DSPin _connected = ConnectedTo;
- if (_connected != null && _connected)
- {
- AMMediaType _type = ConnectionMediaType;
- Disconnect();
- List<DSPin> _pins = _connected.Filter.Pins;
- List<DSPin> _disconnect = new List<DSPin>();
- bool bShouldProceed = true;
- foreach (DSPin _pin in _pins)
- {
- if (_pin.IsConnected)
- {
- if (_pin.Direction != Direction)
- {
- bShouldProceed = false;
- break;
- }
- else
- {
- AMMediaType mt = _pin.ConnectionMediaType;
- if (mt.majorType != _type.majorType)
- {
- bShouldProceed = false;
- break;
- }
- _disconnect.Add(_pin);
- }
- }
- }
- if (bShouldProceed)
- {
- DSPin _result = null;
- foreach (DSPin _pin in _disconnect)
- {
- _result = _pin.RemoveFiltersChain();
- }
- DSFilter _filter = _connected.Filter;
- _filter.FilterGraph = null;
- _filter.Dispose();
- if (Direction == PinDirection.Output)
- {
- _connected = this;
- }
- else
- {
- _connected = _result;
- }
- }
- }
- return _connected;
- }
- #endregion
- #region Overriden Methods
- public override string ToString()
- {
- return " { " + base.ToString() + " } Name: '" + Name + "' ( " + Direction.ToString() + " )" + (IsConnected ? " Connected" : " Not Connected"); ;
- }
- #endregion
- }
- #endregion
- #region Filter Helper
- [ComVisible(false)]
- public class DSFilter : DSObject<IBaseFilter>
- {
- #region Constructor
- protected DSFilter()
- : base()
- {
- }
- public DSFilter(IBaseFilter _filter)
- : base(_filter)
- {
- }
- public DSFilter(Guid _filter)
- : base(_filter)
- {
- }
- public DSFilter(string _moniker)
- {
- MonikerInfo _info = new MonikerInfo(_moniker);
- m_pUnknown = (IBaseFilter)_info.Bind(typeof(IBaseFilter));
- }
- #endregion
- #region Properties
- public List<DSPin> Pins
- {
- get
- {
- List<DSPin> _pins = new List<DSPin>();
- if (this)
- {
- IPin[] pPins = new IPin[1];
- IEnumPins pEnum;
- if (SUCCEEDED(m_pUnknown.EnumPins(out pEnum)))
- {
- while (pEnum.Next(1, pPins, IntPtr.Zero) == S_OK)
- {
- if (pPins[0] != null)
- {
- _pins.Add(new DSPin(pPins[0]));
- }
- }
- }
- }
- return _pins;
- }
- }
- public List<DSPin> Output
- {
- get
- {
- List<DSPin> _pins = Pins;
- List<DSPin> _output = new List<DSPin>();
- foreach (DSPin _pin in _pins)
- {
- if (_pin.Direction == PinDirection.Output)
- {
- _output.Add(_pin);
- }
- }
- return _output;
- }
- }
- public List<DSPin> Input
- {
- get
- {
- List<DSPin> _pins = Pins;
- List<DSPin> _output = new List<DSPin>();
- foreach (DSPin _pin in _pins)
- {
- if (_pin.Direction == PinDirection.Input)
- {
- _output.Add(_pin);
- }
- }
- return _output;
- }
- }
- public string Name
- {
- get
- {
- if (this)
- {
- FilterInfo _info;
- if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
- {
- if (_info.pGraph != null)
- {
- Marshal.ReleaseComObject(_info.pGraph);
- }
- return _info.achName;
- }
- }
- return "";
- }
- }
- public bool HaveProperties
- {
- get { return IsSupported(typeof(ISpecifyPropertyPages)); }
- }
- public IGraphBuilder FilterGraph
- {
- get
- {
- IGraphBuilder _graph = null;
- if (this)
- {
- FilterInfo _info;
- if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
- {
- if (_info.pGraph != null)
- {
- _graph = (IGraphBuilder)_info.pGraph;
- Marshal.ReleaseComObject(_info.pGraph);
- }
- }
- }
- return _graph;
- }
- set
- {
- if (this)
- {
- if (value == null)
- {
- FilterInfo _info;
- if (SUCCEEDED(m_pUnknown.QueryFilterInfo(out _info)))
- {
- if (_info.pGraph != null)
- {
- IGraphBuilder _graph = (IGraphBuilder)_info.pGraph;
- _graph.RemoveFilter(m_pUnknown);
- Marshal.ReleaseComObject(_info.pGraph);
- }
- }
- }
- else
- {
- int hr = value.AddFilter(m_pUnknown, Name);
- ASSERT(SUCCEEDED(hr));
- }
- }
- }
- }
- public DSPin InputPin
- {
- get
- {
- List<DSPin> _pins = Input;
- if (_pins.Count > 0)
- return _pins[0];
- return null;
- }
- }
- public DSPin OutputPin
- {
- get
- {
- List<DSPin> _pins = Output;
- if (_pins.Count > 0)
- return _pins[0];
- return null;
- }
- }
- public DSPin this[int index]
- {
- get
- {
- return Pins[index];
- }
- }
- public DSPin this[string _name]
- {
- get
- {
- List<DSPin> _pins = Pins;
- foreach (DSPin _pin in _pins)
- {
- if (_pin.Name == _name) return _pin;
- }
- return null;
- }
- }
- public int Count
- {
- get { return Pins.Count; }
- }
- #endregion
- #region Public Methods
- public HRESULT ShowProperties()
- {
- return ShowProperties(IntPtr.Zero);
- }
- public HRESULT ShowProperties(IntPtr _hwnd)
- {
- if (DirectShow.BaseClasses.BasePropertyPage.ShowPropertyPages(m_pUnknown, _hwnd))
- {
- return S_OK;
- }
- return E_FAIL;
- }
- public DSPin GetPin(PinDirection _direction)
- {
- return GetPin(_direction, 0);
- }
- public DSPin GetPin(PinDirection _direction, int index)
- {
- List<DSPin> _pins = null;
- switch (_direction)
- {
- case PinDirection.Input:
- _pins = Input;
- break;
- case PinDirection.Output:
- _pins = Output;
- break;
- }
- ASSERT(_pins);
- if (index >= 0 && _pins.Count < index)
- {
- return _pins[index];
- }
- return null;
- }
- public DSPin GetPin(string _name)
- {
- List<DSPin> _pins = Pins;
- foreach (DSPin _pin in _pins)
- {
- if (_pin.Name == _name) return _pin;
- }
- return null;
- }
- public DSPin GetPin(Guid _category)
- {
- List<DSPin> _pins = Pins;
- foreach (DSPin _pin in _pins)
- {
- if (_pin.Category == _category) return _pin;
- }
- return null;
- }
- public HRESULT Connect(DSFilter _filter)
- {
- if (_filter)
- {
- List<DSPin> _pins;
- {
- _pins = _filter.Input;
- foreach (DSPin _pin in _pins)
- {
- HRESULT hr = Connect(_pin);
- if (hr.Succeeded) return hr;
- }
- }
- {
- _pins = _filter.Output;
- foreach (DSPin _pin in _pins)
- {
- HRESULT hr = Connect(_pin);
- if (hr.Succeeded) return hr;
- }
- }
- return VFW_E_CANNOT_CONNECT;
- }
- return E_POINTER;
- }
- public HRESULT Connect(DSPin _pin)
- {
- if (_pin)
- {
- List<DSPin> _pins;
- if (_pin.Direction == PinDirection.Output)
- {
- _pins = Input;
- }
- else
- {
- _pins = Output;
- }
- foreach (DSPin _connect in _pins)
- {
- if (!_connect.IsConnected)
- {
- HRESULT hr = _connect.Connect(_pin);
- if (hr.Succeeded) return hr;
- }
- }
- return VFW_E_CANNOT_CONNECT;
- }
- return E_POINTER;
- }
- public DSPin RemoveFiltersChain()
- {
- DSPin _input = null;
- DSPin _output = null;
- _output = RemoveFiltersChain(PinDirection.Output);
- _input = RemoveFiltersChain(PinDirection.Input);
- if (_output != null) return _output;
- return _input;
- }
- public DSPin RemoveFiltersChain(PinDirection _direction)
- {
- List<DSPin> _pins;
- if (_direction == PinDirection.Input)
- {
- _pins = Input;
- }
- else
- {
- _pins = Output;
- }
- DSPin _result = null;
- foreach (DSPin _pin in _pins)
- {
- DSPin _removed = _pin.RemoveFiltersChain();
- if (_removed != null && _result == null)
- {
- _result = _removed;
- }
- }
- return _result;
- }
- #endregion
- #region Overriden Methods
- public override string ToString()
- {
- return Name;
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSBaseSourceFilter : DSFilter
- {
- #region Properties
- public string FileName
- {
- get
- {
- IFileSourceFilter _source = (IFileSourceFilter)QueryInterface(typeof(IFileSourceFilter));
- if (_source != null)
- {
- string _path;
- int hr = _source.GetCurFile(out _path, null);
- if (SUCCEEDED(hr))
- {
- return _path;
- }
- }
- return null;
- }
- set
- {
- IFileSourceFilter _source = (IFileSourceFilter)QueryInterface(typeof(IFileSourceFilter));
- if (_source != null)
- {
- int hr = _source.Load(value, null);
- ASSERT(SUCCEEDED(hr));
- }
- }
- }
- #endregion
- #region Constructor
- protected DSBaseSourceFilter()
- : base()
- {
- }
- public DSBaseSourceFilter(IBaseFilter _filter)
- : base(_filter)
- {
- }
- public DSBaseSourceFilter(Guid _filter)
- : base(_filter)
- {
- }
- public DSBaseSourceFilter(string _moniker)
- : base(_moniker)
- {
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSBaseWriterFilter : DSFilter
- {
- #region Properties
- public string FileName
- {
- get
- {
- IFileSinkFilter _sink = (IFileSinkFilter)QueryInterface(typeof(IFileSinkFilter));
- if (_sink != null)
- {
- string _path;
- int hr = _sink.GetCurFile(out _path, null);
- if (SUCCEEDED(hr))
- {
- return _path;
- }
- }
- return null;
- }
- set
- {
- IFileSinkFilter _sink = (IFileSinkFilter)QueryInterface(typeof(IFileSinkFilter));
- if (_sink != null)
- {
- int hr = _sink.SetFileName(value, null);
- ASSERT(SUCCEEDED(hr));
- }
- }
- }
- #endregion
- #region Constructor
- protected DSBaseWriterFilter()
- : base()
- {
- }
- public DSBaseWriterFilter(IBaseFilter _filter)
- : base(_filter)
- {
- }
- public DSBaseWriterFilter(Guid _filter)
- : base(_filter)
- {
- }
- public DSBaseWriterFilter(string _moniker)
- : base(_moniker)
- {
- }
- #endregion
- }
- #endregion
- #region Categories
- [ComVisible(false)]
- public class DSVideoCaptureCategory : DSCategory
- {
- #region Constructor
- public DSVideoCaptureCategory()
- : base(FilterCategory.VideoInputDevice)
- {
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSAudioCaptureCategory : DSCategory
- {
- #region Constructor
- public DSAudioCaptureCategory()
- : base(FilterCategory.AudioInputDevice)
- {
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSVideoCompressorsCategory : DSCategory
- {
- #region Constructor
- public DSVideoCompressorsCategory()
- : base(FilterCategory.VideoCompressorCategory)
- {
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSAudioCompressorsCategory : DSCategory
- {
- #region Constructor
- public DSAudioCompressorsCategory()
- : base(FilterCategory.AudioCompressorCategory)
- {
- }
- #endregion
- }
- [ComVisible(false)]
- public class DSAudioRenderersCategory : DSCategory
- {
- #region Constructor
- public DSAudioRenderersCategory()
- : base(FilterCategory.AudioRendererCategory)
- {
- }
- #endregion
- }
- #endregion
- #region Filters
- [ComVisible(false)]
- [Guid("f8388a40-d5bb-11d0-be5a-0080c706568e")]
- public class DSInfTeeFilter : DSFilter
- {
- public DSInfTeeFilter()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("cc58e280-8aa1-11d1-b3f1-00aa003761c5")]
- public class DSSmartTeeFilter : DSFilter
- {
- public DSSmartTeeFilter()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("8596e5f0-0da5-11d0-bd21-00a0c911ce86")]
- public class DSFileWriter : DSBaseWriterFilter
- {
- public DSFileWriter()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("e436ebb5-524f-11ce-9f53-0020af0ba770")]
- public class DSFileSourceAsync : DSBaseSourceFilter
- {
- public DSFileSourceAsync()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("79376820-07d0-11cf-a24d-0020afd79767")]
- public class DSDSoundRendererFilter : DSFilter
- {
- public DSDSoundRendererFilter()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("70e102b0-5556-11ce-97c0-00aa0055595a")]
- public class DSVideoRenderer : DSFilter
- {
- public DSVideoRenderer()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("c1f400a4-3f08-11d3-9f0b-006008039e37")]
- public class DSNullRenderer : DSFilter
- {
- public DSNullRenderer()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("c1f400a0-3f08-11d3-9f0b-006008039e37")]
- public class DSSampleGrabberFilter : DSFilter, ISampleGrabberCB
- {
- #region Delegates
- [ComVisible(false)]
- public delegate void SampleEventHandler(DSSampleGrabberFilter _filter, IntPtr _buffer, int _length);
- #endregion
- #region Variables
- protected ISampleGrabber m_SampleGrabber = null;
- protected bool m_BufferedSamples = false;
- protected bool m_bOneShot = false;
- #endregion
- #region Events
- public event SampleEventHandler OnSample = null;
- #endregion
- #region Properties
- public AMMediaType MediaType
- {
- get
- {
- if (m_SampleGrabber != null)
- {
- AMMediaType mt = new AMMediaType();
- if (SUCCEEDED(m_SampleGrabber.GetConnectedMediaType(mt)))
- {
- return mt;
- }
- mt.Free();
- }
- return null;
- }
- set
- {
- if (m_SampleGrabber != null)
- {
- HRESULT hr = (HRESULT)m_SampleGrabber.SetMediaType(value);
- hr.Assert();
- }
- }
- }
- public bool BufferedSamples
- {
- get { return m_BufferedSamples; }
- set
- {
- if (m_BufferedSamples != value)
- {
- HRESULT hr = NOERROR;
- if (m_SampleGrabber != null)
- {
- hr = (HRESULT)m_SampleGrabber.SetBufferSamples(value);
- hr.Assert();
- }
- if (hr.Succeeded)
- {
- m_BufferedSamples = value;
- }
- }
- }
- }
- public bool OneShot
- {
- get { return m_bOneShot; }
- set
- {
- if (m_bOneShot != value)
- {
- HRESULT hr = NOERROR;
- if (m_SampleGrabber != null)
- {
- hr = (HRESULT)m_SampleGrabber.SetOneShot(value);
- hr.Assert();
- }
- if (hr.Succeeded)
- {
- m_bOneShot = value;
- }
- }
- }
- }
- #endregion
- #region Constructor
- public DSSampleGrabberFilter()
- : base()
- {
- m_SampleGrabber = (ISampleGrabber)QueryInterface(typeof(ISampleGrabber));
- ASSERT(m_SampleGrabber);
- if (m_SampleGrabber != null)
- {
- HRESULT hr = (HRESULT)m_SampleGrabber.SetCallback(this, 1);
- hr.Assert();
- hr = (HRESULT)m_SampleGrabber.SetBufferSamples(m_BufferedSamples);
- hr.Assert();
- hr = (HRESULT)m_SampleGrabber.SetOneShot(m_bOneShot);
- hr.Assert();
- }
- }
- #endregion
- #region ISampleGrabberCB Members
- public int SampleCB(double _time, IntPtr pSample)
- {
- return NOERROR;
- }
- public int BufferCB(double _time, IntPtr pBuffer, int _length)
- {
- if (OnSample != null)
- {
- OnSample(this, pBuffer, _length);
- }
- return NOERROR;
- }
- #endregion
- #region Overridden Methods
- public override void Dispose()
- {
- m_SampleGrabber = (ISampleGrabber)QueryInterface(typeof(ISampleGrabber));
- if (m_SampleGrabber != null)
- {
- m_SampleGrabber.SetCallback(null, 0);
- m_SampleGrabber = null;
- }
- base.Dispose();
- }
- #endregion
- }
- [ComVisible(false)]
- [Guid("7c23220e-55bb-11d3-8b16-00c04fb6bd3d")]
- public class DSWMAsfWritter : DSBaseWriterFilter
- {
- public DSWMAsfWritter()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("187463A0-5BB7-11d3-ACBE-0080C75E246E")]
- public class DSWMAsfReader : DSBaseSourceFilter
- {
- public DSWMAsfReader()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("E2510970-F137-11CE-8B67-00AA00A3F1A6")]
- public class DSAviMuxFilter : DSFilter
- {
- public DSAviMuxFilter()
- : base()
- {
- }
- }
- #endregion
- #region Classes
- [ComVisible(false)]
- [Guid("e436ebb3-524f-11ce-9f53-0020af0ba770")]
- public class DSGraphBuilder : DSObject<IGraphBuilder>
- {
- public DSGraphBuilder()
- : base()
- {
- }
- }
- [ComVisible(false)]
- [Guid("BF87B6E1-8C27-11d0-B3F0-00AA003761C5")]
- public class DSCaptureGraphBuilder2 : DSObject<ICaptureGraphBuilder2>
- {
- public DSCaptureGraphBuilder2()
- : base()
- {
- }
- }
- #endregion
- #region Base Filter Graph Builder
- [ComVisible(false)]
- [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
- public class DSFilterGraphBase : COMHelper, IDisposable
- {
- #region Constants
- protected const int WM_GRAPHNOTIFY = 0x00008001;
- #endregion
- #region Helper Classes
- [ComVisible(false)]
- protected class HelperForm : System.Windows.Forms.Form
- {
- protected override void WndProc(ref System.Windows.Forms.Message m)
- {
- if (m.Msg == WM_GRAPHNOTIFY)
- {
- try
- {
- if (m.LParam != IntPtr.Zero)
- {
- DSFilterGraphBase _graph = (DSFilterGraphBase)Marshal.GetObjectForIUnknown(m.LParam);
- if (_graph != null)
- {
- if (_graph.ProcessGraphMessage())
- {
- _graph.Stop();
- }
- }
- }
- }
- catch
- {
- }
- }
- base.WndProc(ref m);
- }
- }
- #endregion
- #region Variables
- protected System.Windows.Forms.Control m_VideoControl = null;
- protected System.Windows.Forms.Control m_EventControl = new HelperForm();
- protected IGraphBuilder m_GraphBuilder = null;
- protected IMediaControl m_MediaControl = null;
- protected IMediaSeeking m_MediaSeeking = null;
- protected IMediaEventEx m_MediaEventEx = null;
- protected IVideoWindow m_VideoWindow = null;
- protected IBasicAudio m_BasicAudio = null;
- protected IBasicVideo m_BasicVideo = null;
- protected IVideoFrameStep m_FrameStep = null;
- protected bool m_bMute = false;
- protected int m_iVolume = 0;
- protected double m_dRate = 1.0;
- protected bool m_bShouldPreview = true;
- protected List<DSFilter> m_Filters = new List<DSFilter>();
- #endregion
- #region Constructor
- public DSFilterGraphBase()
- {
- }
- ~DSFilterGraphBase()
- {
- Dispose();
- }
- #endregion
- #region Properties
- public bool IsRunning
- {
- get
- {
- if (m_MediaControl != null)
- {
- FilterState _state;
- int hr = 0;
- do
- {
- hr = m_MediaControl.GetState(200, out _state);
- }
- while (hr == 0x00040237 || hr == 0x00040268);
- if (hr == 0)
- {
- return _state == FilterState.Running;
- }
- }
- return false;
- }
- }
- public bool IsPaused
- {
- get
- {
- if (m_MediaControl != null)
- {
- FilterState _state;
- int hr = 0;
- do
- {
- hr = m_MediaControl.GetState(200, out _state);
- }
- while (hr == 0x00040237 || hr == 0x00040268);
- if (hr == 0)
- {
- return _state == FilterState.Paused;
- }
- }
- return false;
- }
- }
- public bool IsStopped
- {
- get
- {
- if (m_MediaControl != null)
- {
- FilterState _state;
- int hr = 0;
- do
- {
- hr = m_MediaControl.GetState(200, out _state);
- }
- while (hr == 0x00040237 || hr == 0x00040268);
- if (hr == 0)
- {
- return _state == FilterState.Stopped;
- }
- }
- return true;
- }
- }
- public long Position
- {
- get
- {
- if (m_MediaSeeking != null)
- {
- long _time;
- int hr = m_MediaSeeking.GetCurrentPosition(out _time);
- Debug.Assert(hr == 0);
- if (hr == 0) return _time;
- }
- return -1;
- }
- set
- {
- if (m_MediaSeeking != null)
- {
- DsLong _stop = (long)0;
- DsLong _current = value;
- int hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
- Debug.Assert(hr >= 0);
- if (OnPositionChange != null) OnPositionChange(this, EventArgs.Empty);
- }
- }
- }
- public long Duration
- {
- get
- {
- if (m_MediaSeeking != null)
- {
- long _time;
- int hr = m_MediaSeeking.GetDuration(out _time);
- Debug.Assert(hr == 0);
- if (hr == 0) return _time;
- }
- return -1;
- }
- }
- public int Volume
- {
- get { return m_iVolume; }
- set
- {
- m_iVolume = value;
- if (!m_bMute)
- {
- SetVolume(value);
- }
- }
- }
- public bool Mute
- {
- get { return m_bMute; }
- set
- {
- if (m_bMute != value)
- {
- m_bMute = value;
- SetVolume(m_bMute ? -10000 : m_iVolume);
- }
- }
- }
- public double Rate
- {
- get
- {
- return m_dRate;
- }
- set
- {
- if (value > 0 && value <= 2.0)
- {
- if (m_MediaSeeking != null)
- {
- int hr = m_MediaSeeking.SetRate(value);
- if (hr == 0)
- {
- m_dRate = value;
- }
- }
- else
- {
- m_dRate = value;
- }
- }
- }
- }
- public System.Windows.Forms.Control VideoControl
- {
- get { return m_VideoControl; }
- set
- {
- if (m_VideoControl != value)
- {
- m_VideoControl = value;
- m_VideoControl.Resize += new EventHandler(VideoControl_Resize);
- m_VideoControl.VisibleChanged += new EventHandler(VideoControl_VisibleChanged);
- }
- }
- }
- public bool Visible
- {
- get
- {
- bool bVisible = ((m_VideoControl != null) ? m_VideoControl.Visible : false);
- return m_bShouldPreview || bVisible;
- }
- set
- {
- if (m_bShouldPreview != value)
- {
- m_bShouldPreview = value;
- if (m_VideoWindow != null)
- {
- m_VideoWindow.put_AutoShow(m_bShouldPreview ? -1 : 0);
- m_VideoWindow.put_Visible(m_bShouldPreview ? -1 : 0);
- }
- }
- }
- }
- public List<DSFilter> Filters
- {
- get
- {
- if (m_MediaControl == null)
- {
- while (m_Filters.Count > 0)
- {
- DSFilter _filter = m_Filters[0];
- m_Filters.RemoveAt(0);
- _filter.Dispose();
- }
- if (m_GraphBuilder != null)
- {
- IEnumFilters pEnum;
- if (SUCCEEDED(m_GraphBuilder.EnumFilters(out pEnum)))
- {
- IBaseFilter[] aFilters = new IBaseFilter[1];
- while (S_OK == pEnum.Next(1, aFilters, IntPtr.Zero))
- {
- m_Filters.Add(new DSFilter(aFilters[0]));
- }
- Marshal.ReleaseComObject(pEnum);
- }
- }
- }
- return m_Filters;
- }
- }
- public int Count
- {
- get { return Filters.Count; }
- }
- public DSFilter AudioRenderer
- {
- get
- {
- List<DSFilter> _filters = Filters;
- foreach (DSFilter _filter in _filters)
- {
- if (_filter.IsSupported(typeof(IBasicAudio).GUID))
- {
- return _filter;
- }
- }
- return null;
- }
- }
- public DSFilter VideoRenderer
- {
- get
- {
- List<DSFilter> _filters = Filters;
- foreach (DSFilter _filter in _filters)
- {
- if (_filter.IsSupported(typeof(IVideoWindow)))
- {
- return _filter;
- }
- }
- return null;
- }
- }
- public DSFilter this[int index]
- {
- get
- {
- try
- {
- return Filters[index];
- }
- catch
- {
- return null;
- }
- }
- }
- public DSFilter this[string _name]
- {
- get
- {
- List<DSFilter> _filters = Filters;
- foreach (DSFilter _filter in _filters)
- {
- if (_filter.Name == _name)
- {
- return _filter;
- }
- }
- return null;
- }
- }
- public bool IsAudioSupported
- {
- get
- {
- return ((AudioRenderer != null) && AudioRenderer == true);
- }
- }
- #endregion
- #region Events
- public event EventHandler OnPlaybackPrepared;
- public event EventHandler OnPlaybackStart;
- public event EventHandler OnPlaybackStop;
- public event EventHandler OnPlaybackPause;
- public event EventHandler OnPositionChange;
- public event EventHandler OnPlaybackReady;
- #endregion
- #region Public Methods
- public virtual HRESULT Start()
- {
- int hr = 0;
- if (m_MediaControl == null)
- {
- hr = Load();
- if (hr != 0) return (HRESULT)hr;
- }
- hr = 1;
- while (hr == 1)
- {
- hr = m_MediaControl.Run();
- if (hr == 1) Thread.Sleep(50);
- }
- if (hr == 0)
- {
- if (OnPlaybackStart != null) OnPlaybackStart(this, EventArgs.Empty);
- }
- return (HRESULT)hr;
- }
- public virtual HRESULT Pause()
- {
- int hr = 0;
- if (m_MediaControl == null)
- {
- hr = Load();
- if (hr != 0) return (HRESULT)hr;
- }
- if (!IsPaused)
- {
- hr = m_MediaControl.Pause();
- }
- else
- {
- return Start();
- }
- if (hr >= 0)
- {
- if (OnPlaybackPause != null) OnPlaybackPause(this, EventArgs.Empty);
- }
- return (HRESULT)hr;
- }
- public virtual HRESULT Stop()
- {
- if (m_MediaControl == null) return (HRESULT)E_POINTER;
- m_MediaControl.Stop();
- if (OnPlaybackStop != null) OnPlaybackStop(this, EventArgs.Empty);
- return Unload();
- }
- public virtual HRESULT StepForward()
- {
- if (m_FrameStep != null)
- {
- if (!IsPaused)
- {
- Pause();
- }
- int hr = m_FrameStep.Step(1, null);
- if (hr < 0)
- {
- long _time;
- hr = m_MediaSeeking.GetCurrentPosition(out _time);
- DsLong _stop = (long)0;
- TimeSpan _ts = new TimeSpan(0, 0, 1);
- _time += _ts.Ticks / 20;
- DsLong _current = _time;
- hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
- }
- return (HRESULT)hr;
- }
- return (HRESULT)E_POINTER;
- }
- public virtual HRESULT StepBackward()
- {
- if (m_FrameStep != null)
- {
- if (!IsPaused)
- {
- Pause();
- }
- long _time;
- int hr = m_MediaSeeking.GetCurrentPosition(out _time);
- DsLong _stop = (long)0;
- TimeSpan _ts = new TimeSpan(0, 0, 1);
- _time -= _ts.Ticks / 20;
- if (_time < 0) _time = 0;
- DsLong _current = _time;
- hr = m_MediaSeeking.SetPositions(_current, AMSeekingSeekingFlags.AbsolutePositioning, _stop, AMSeekingSeekingFlags.NoPositioning);
- return (HRESULT)hr;
- }
- return (HRESULT)E_POINTER;
- }
- #endregion
- #region Private Methods
- private void SetVolume(int _volume)
- {
- if (m_BasicAudio == null)
- {
- if (m_GraphBuilder == null)
- {
- return;
- }
- else
- {
- m_BasicAudio = (IBasicAudio)m_GraphBuilder;
- }
- }
- m_BasicAudio.put_Volume(_volume);
- }
- private HRESULT InitInterfaces()
- {
- CloseInterfaces();
- HRESULT hr = E_FAIL;
- try
- {
- m_GraphBuilder = (IGraphBuilder)new FilterGraph();
- hr = OnInitInterfaces();
- hr.Throw();
- return PreparePlayback();
- }
- catch (Exception _exception)
- {
- if (_exception is COMException)
- {
- hr = (HRESULT)((COMException)_exception).ErrorCode;
- }
- else
- {
- hr = E_UNEXPECTED;
- }
- }
- finally
- {
- if (hr.Succeeded)
- {
- while (m_Filters.Count > 0)
- {
- DSFilter _filter = m_Filters[0];
- m_Filters.RemoveAt(0);
- _filter.Dispose();
- }
- IEnumFilters pEnum;
- if (SUCCEEDED(m_GraphBuilder.EnumFilters(out pEnum)))
- {
- IBaseFilter[] aFilters = new IBaseFilter[1];
- while (S_OK == pEnum.Next(1, aFilters, IntPtr.Zero))
- {
- m_Filters.Add(new DSFilter(aFilters[0]));
- }
- Marshal.ReleaseComObject(pEnum);
- }
- }
- }
- CloseInterfaces();
- return hr;
- }
- private HRESULT CloseInterfaces()
- {
- try
- {
- OnCloseInterfaces();
- if (m_MediaEventEx != null)
- {
- m_MediaEventEx.SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero);
- m_MediaEventEx = null;
- }
- if (m_VideoWindow != null)
- {
- m_VideoWindow.put_Visible(0);
- m_VideoWindow.put_Owner(IntPtr.Zero);
- m_VideoWindow = null;
- }
- m_MediaSeeking = null;
- m_BasicVideo = null;
- m_BasicAudio = null;
- m_MediaControl = null;
- while (m_Filters.Count > 0)
- {
- DSFilter _filter = m_Filters[0];
- m_Filters.RemoveAt(0);
- _filter.Dispose();
- }
- if (m_GraphBuilder != null)
- {
- Marshal.ReleaseComObject(m_GraphBuilder);
- m_GraphBuilder = null;
- }
- GC.Collect();
- return (HRESULT)NOERROR;
- }
- catch
- {
- return (HRESULT)E_FAIL;
- }
- }
- #endregion
- #region Protected Methods
- protected virtual HRESULT Load()
- {
- HRESULT hr = InitInterfaces();
- if (hr.Succeeded)
- {
- if (OnPlaybackPrepared != null) OnPlaybackPrepared(this, EventArgs.Empty);
- }
- return hr;
- }
- protected virtual HRESULT Unload()
- {
- return CloseInterfaces();
- }
- protected virtual HRESULT OnInitInterfaces()
- {
- return (HRESULT)NOERROR;
- }
- protected virtual HRESULT OnCloseInterfaces()
- {
- return (HRESULT)NOERROR;
- }
- protected virtual void SettingUpVideoWindow()
- {
- if (m_VideoWindow != null)
- {
- if (m_VideoControl != null)
- {
- m_VideoWindow.put_Owner(m_VideoControl.Handle);
- m_VideoWindow.put_MessageDrain(m_VideoControl.Handle);
- m_VideoWindow.put_WindowStyle(0x40000000 | 0x04000000);
- ResizeVideoWindow();
- }
- m_VideoWindow.put_AutoShow(Visible ? -1 : 0);
- m_VideoWindow.put_Visible(Visible ? -1 : 0);
- }
- }
- protected virtual HRESULT PreparePlayback()
- {
- m_MediaControl = (IMediaControl)m_GraphBuilder;
- m_BasicVideo = (IBasicVideo)m_GraphBuilder;
- m_MediaSeeking = (IMediaSeeking)m_GraphBuilder;
- m_VideoWindow = (IVideoWindow)m_GraphBuilder;
- m_MediaEventEx = (IMediaEventEx)m_GraphBuilder;
- m_FrameStep = (IVideoFrameStep)m_GraphBuilder;
- SettingUpVideoWindow();
- int hr = m_MediaEventEx.SetNotifyWindow(m_EventControl.Handle, WM_GRAPHNOTIFY, Marshal.GetIUnknownForObject(this));
- Debug.Assert(hr == 0);
- SetVolume(m_bMute ? -10000 : m_iVolume);
- if (m_dRate != 1.0)
- {
- m_MediaSeeking.SetRate(m_dRate);
- m_MediaSeeking.GetRate(out m_dRate);
- }
- if (OnPlaybackReady != null) OnPlaybackReady(this, EventArgs.Empty);
- return (HRESULT)hr;
- }
- protected virtual bool ProcessGraphMessage()
- {
- if (m_MediaEventEx != null)
- {
- int hr = 0;
- int _param1, _param2;
- EventCode _code;
- while (hr == 0)
- {
- hr = m_MediaEventEx.GetEvent(out _code, out _param1, out _param2, 20);
- if (hr == 0)
- {
- hr = m_MediaEventEx.FreeEventParams(_code, _param1, _param2);
- if (_code == EventCode.Complete)
- {
- return true;
- }
- if (_code == EventCode.ErrorAbort)
- {
- return true;
- }
- if (_code == EventCode.DeviceLost)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- protected virtual void ResizeVideoWindow()
- {
- if (m_VideoWindow != null && m_VideoControl != null)
- {
- m_VideoWindow.SetWindowPosition(0, 0, m_VideoControl.Width, m_VideoControl.Height);
- }
- }
- #endregion
- #region Overridden Methods
- private void VideoControl_VisibleChanged(object sender, EventArgs e)
- {
- if (m_VideoWindow != null && m_VideoControl != null && m_bShouldPreview)
- {
- m_VideoWindow.put_AutoShow(m_VideoControl.Visible ? -1 : 0);
- m_VideoWindow.put_Visible(m_VideoControl.Visible ? -1 : 0);
- }
- }
- private void VideoControl_Resize(object sender, EventArgs e)
- {
- ResizeVideoWindow();
- }
- #endregion
- #region IDisposable Members
- public virtual void Dispose()
- {
- CloseInterfaces();
- }
- #endregion
- }
- [ComVisible(false)]
- public interface ISourceFileSupport
- {
- string FileName { get; set; }
- HRESULT Open();
- HRESULT Open(bool bStart);
- HRESULT Open(string sFileName);
- HRESULT Open(string sFileName, bool bStart);
- }
- [ComVisible(false)]
- public interface IFileDestSupport
- {
- string OutputFileName { get; set; }
- HRESULT Save();
- HRESULT Save(bool bStart);
- HRESULT Save(string sFileName);
- HRESULT Save(string sFileName, bool bStart);
- }
- [ComVisible(false)]
- public class DSFilePlayback : DSFilterGraphBase, ISourceFileSupport
- {
- #region Variables
- protected string m_sFileName = "";
- #endregion
- #region Properties
- public string FileName
- {
- get { return m_sFileName; }
- set { Open(value); }
- }
- #endregion
- #region Public Methods
- public HRESULT Open()
- {
- return Open(true);
- }
- public HRESULT Open(bool bStart)
- {
- return Open(m_sFileName, bStart);
- }
- public HRESULT Open(string sFileName)
- {
- return Open(sFileName, true);
- }
- public virtual HRESULT Open(string sFileName, bool bStart)
- {
- if (sFileName != null && sFileName != "")
- {
- m_sFileName = sFileName;
- if (bStart)
- {
- return Start();
- }
- else
- {
- return Load();
- }
- }
- return E_POINTER;
- }
- #endregion
- #region Overridden Methods
- protected override HRESULT OnInitInterfaces()
- {
- int hr = m_GraphBuilder.RenderFile(m_sFileName, null);
- if (hr < 0) Marshal.ThrowExceptionForHR(hr);
- return (HRESULT)hr;
- }
- #endregion
- }
- #endregion
- }
|