1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
//------------------------------------------------------------------------------
// File: CtlUtil.h
//
// Desc: DirectShow base classes.
//
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
// Base classes implementing IDispatch parsing for the basic control dual
// interfaces. Derive from these and implement just the custom method and
// property methods. We also implement CPosPassThru that can be used by
// renderers and transforms to pass by IMediaPosition and IMediaSeeking
#ifndef __CTLUTIL__
#define __CTLUTIL__
// OLE Automation has different ideas of TRUE and FALSE
#define OATRUE (-1)
#define OAFALSE (0)
// It's possible that we could replace this class with CreateStdDispatch
class CBaseDispatch
{
ITypeInfo * m_pti;
public:
CBaseDispatch() : m_pti(NULL) {}
~CBaseDispatch();
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
REFIID riid,
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
};
class AM_NOVTABLE CMediaControl :
public IMediaControl,
public CUnknown
{
CBaseDispatch m_basedisp;
public:
CMediaControl(const TCHAR *, LPUNKNOWN);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
};
class AM_NOVTABLE CMediaEvent :
public IMediaEventEx,
public CUnknown
{
CBaseDispatch m_basedisp;
public:
CMediaEvent(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
};
class AM_NOVTABLE CMediaPosition :
public IMediaPosition,
public CUnknown
{
CBaseDispatch m_basedisp;
public:
CMediaPosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
CMediaPosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT *phr);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
};
// OA-compatibility means that we must use double as the RefTime value,
// and REFERENCE_TIME (essentially a LONGLONG) within filters.
// this class converts between the two
class COARefTime : public CRefTime {
public:
COARefTime() {
};
COARefTime(CRefTime t)
: CRefTime(t)
{
};
COARefTime(REFERENCE_TIME t)
: CRefTime(t)
{
};
COARefTime(double d) {
m_time = (LONGLONG) (d * 10000000);
};
operator double() {
return double(m_time) / 10000000;
};
operator REFERENCE_TIME() {
return m_time;
};
COARefTime& operator=(const double& rd) {
m_time = (LONGLONG) (rd * 10000000);
return *this;
}
COARefTime& operator=(const REFERENCE_TIME& rt) {
m_time = rt;
return *this;
}
inline BOOL operator==(const COARefTime& rt)
{
return m_time == rt.m_time;
};
inline BOOL operator!=(const COARefTime& rt)
{
return m_time != rt.m_time;
};
inline BOOL operator < (const COARefTime& rt)
{
return m_time < rt.m_time;
};
inline BOOL operator > (const COARefTime& rt)
{
return m_time > rt.m_time;
};
inline BOOL operator >= (const COARefTime& rt)
{
return m_time >= rt.m_time;
};
inline BOOL operator <= (const COARefTime& rt)
{
return m_time <= rt.m_time;
};
inline COARefTime operator+(const COARefTime& rt)
{
return COARefTime(m_time + rt.m_time);
};
inline COARefTime operator-(const COARefTime& rt)
{
return COARefTime(m_time - rt.m_time);
};
inline COARefTime operator*(LONG l)
{
return COARefTime(m_time * l);
};
inline COARefTime operator/(LONG l)
{
return COARefTime(m_time / l);
};
private:
// Prevent bugs from constructing from LONG (which gets
// converted to double and then multiplied by 10000000
COARefTime(LONG);
LONG operator=(LONG);
};
// A utility class that handles IMediaPosition and IMediaSeeking on behalf
// of single-input pin renderers, or transform filters.
//
// Renderers will expose this from the filter; transform filters will
// expose it from the output pin and not the renderer.
//
// Create one of these, giving it your IPin* for your input pin, and delegate
// all IMediaPosition methods to it. It will query the input pin for
// IMediaPosition and respond appropriately.
//
// Call ForceRefresh if the pin connection changes.
//
// This class no longer caches the upstream IMediaPosition or IMediaSeeking
// it acquires it on each method call. This means ForceRefresh is not needed.
// The method is kept for source compatibility and to minimise the changes
// if we need to put it back later for performance reasons.
class CPosPassThru : public IMediaSeeking, public CMediaPosition
{
IPin *m_pPin;
HRESULT GetPeer(__deref_out IMediaPosition **ppMP);
HRESULT GetPeerSeeking(__deref_out IMediaSeeking **ppMS);
public:
CPosPassThru(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, IPin *);
DECLARE_IUNKNOWN
HRESULT ForceRefresh() {
return S_OK;
};
// override to return an accurate current position
virtual HRESULT GetMediaTime(__out LONGLONG *pStartTime, __out_opt LONGLONG *pEndTime) {
return E_FAIL;
}
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,__deref_out void **ppv);
// IMediaSeeking methods
STDMETHODIMP GetCapabilities( __out DWORD * pCapabilities );
STDMETHODIMP CheckCapabilities( __inout DWORD * pCapabilities );
STDMETHODIMP SetTimeFormat(const GUID * pFormat);
STDMETHODIMP GetTimeFormat(__out GUID *pFormat);
STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat);
STDMETHODIMP IsFormatSupported( const GUID * pFormat);
STDMETHODIMP QueryPreferredFormat( __out GUID *pFormat);
STDMETHODIMP ConvertTimeFormat(__out LONGLONG * pTarget,
__in_opt const GUID * pTargetFormat,
LONGLONG Source,
__in_opt const GUID * pSourceFormat );
STDMETHODIMP SetPositions( __inout_opt LONGLONG * pCurrent, DWORD CurrentFlags
, __inout_opt LONGLONG * pStop, DWORD StopFlags );
STDMETHODIMP GetPositions( __out_opt LONGLONG * pCurrent, __out_opt LONGLONG * pStop );
STDMETHODIMP GetCurrentPosition( __out LONGLONG * pCurrent );
STDMETHODIMP GetStopPosition( __out LONGLONG * pStop );
STDMETHODIMP SetRate( double dRate);
STDMETHODIMP GetRate( __out double * pdRate);
STDMETHODIMP GetDuration( __out LONGLONG *pDuration);
STDMETHODIMP GetAvailable( __out_opt LONGLONG *pEarliest, __out_opt LONGLONG *pLatest );
STDMETHODIMP GetPreroll( __out LONGLONG *pllPreroll );
// IMediaPosition properties
STDMETHODIMP get_Duration(__out REFTIME * plength);
STDMETHODIMP put_CurrentPosition(REFTIME llTime);
STDMETHODIMP get_StopTime(__out REFTIME * pllTime);
STDMETHODIMP put_StopTime(REFTIME llTime);
STDMETHODIMP get_PrerollTime(__out REFTIME * pllTime);
STDMETHODIMP put_PrerollTime(REFTIME llTime);
STDMETHODIMP get_Rate(__out double * pdRate);
STDMETHODIMP put_Rate(double dRate);
STDMETHODIMP get_CurrentPosition(__out REFTIME * pllTime);
STDMETHODIMP CanSeekForward(__out LONG *pCanSeekForward);
STDMETHODIMP CanSeekBackward(__out LONG *pCanSeekBackward);
private:
HRESULT GetSeekingLongLong( HRESULT (__stdcall IMediaSeeking::*pMethod)( LONGLONG * ),
__out LONGLONG * pll );
};
// Adds the ability to return a current position
class CRendererPosPassThru : public CPosPassThru
{
CCritSec m_PositionLock; // Locks access to our position
LONGLONG m_StartMedia; // Start media time last seen
LONGLONG m_EndMedia; // And likewise the end media
BOOL m_bReset; // Have media times been set
public:
// Used to help with passing media times through graph
CRendererPosPassThru(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, IPin *);
HRESULT RegisterMediaTime(IMediaSample *pMediaSample);
HRESULT RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime);
HRESULT GetMediaTime(__out LONGLONG *pStartTime,__out_opt LONGLONG *pEndTime);
HRESULT ResetMediaTime();
HRESULT EOS();
};
STDAPI CreatePosPassThru(
__in_opt LPUNKNOWN pAgg,
BOOL bRenderer,
IPin *pPin,
__deref_out IUnknown **ppPassThru
);
// A class that handles the IDispatch part of IBasicAudio and leaves the
// properties and methods themselves pure virtual.
class AM_NOVTABLE CBasicAudio : public IBasicAudio, public CUnknown
{
CBaseDispatch m_basedisp;
public:
CBasicAudio(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
};
// A class that handles the IDispatch part of IBasicVideo and leaves the
// properties and methods themselves pure virtual.
class AM_NOVTABLE CBaseBasicVideo : public IBasicVideo2, public CUnknown
{
CBaseDispatch m_basedisp;
public:
CBaseBasicVideo(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
STDMETHODIMP GetPreferredAspectRatio(
__out long *plAspectX,
__out long *plAspectY)
{
return E_NOTIMPL;
}
};
// A class that handles the IDispatch part of IVideoWindow and leaves the
// properties and methods themselves pure virtual.
class AM_NOVTABLE CBaseVideoWindow : public IVideoWindow, public CUnknown
{
CBaseDispatch m_basedisp;
public:
CBaseVideoWindow(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
/* IDispatch methods */
STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
STDMETHODIMP GetTypeInfo(
UINT itinfo,
LCID lcid,
__deref_out ITypeInfo ** pptinfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
__in_ecount(cNames) LPOLESTR * rgszNames,
UINT cNames,
LCID lcid,
__out_ecount(cNames) DISPID * rgdispid);
STDMETHODIMP Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
__in DISPPARAMS * pdispparams,
__out_opt VARIANT * pvarResult,
__out_opt EXCEPINFO * pexcepinfo,
__out_opt UINT * puArgErr);
};
// abstract class to help source filters with their implementation
// of IMediaPosition. Derive from this and set the duration (and stop
// position). Also override NotifyChange to do something when the properties
// change.
class AM_NOVTABLE CSourcePosition : public CMediaPosition
{
public:
CSourcePosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, __in CCritSec *);
// IMediaPosition methods
STDMETHODIMP get_Duration(__out REFTIME * plength);
STDMETHODIMP put_CurrentPosition(REFTIME llTime);
STDMETHODIMP get_StopTime(__out REFTIME * pllTime);
STDMETHODIMP put_StopTime(REFTIME llTime);
STDMETHODIMP get_PrerollTime(__out REFTIME * pllTime);
STDMETHODIMP put_PrerollTime(REFTIME llTime);
STDMETHODIMP get_Rate(__out double * pdRate);
STDMETHODIMP put_Rate(double dRate);
STDMETHODIMP CanSeekForward(__out LONG *pCanSeekForward);
STDMETHODIMP CanSeekBackward(__out LONG *pCanSeekBackward);
// override if you can return the data you are actually working on
STDMETHODIMP get_CurrentPosition(__out REFTIME * pllTime) {
return E_NOTIMPL;
};
protected:
// we call this to notify changes. Override to handle them
virtual HRESULT ChangeStart() PURE;
virtual HRESULT ChangeStop() PURE;
virtual HRESULT ChangeRate() PURE;
COARefTime m_Duration;
COARefTime m_Start;
COARefTime m_Stop;
double m_Rate;
CCritSec * m_pLock;
};
class AM_NOVTABLE CSourceSeeking :
public IMediaSeeking,
public CUnknown
{
public:
DECLARE_IUNKNOWN;
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
// IMediaSeeking methods
STDMETHODIMP IsFormatSupported(const GUID * pFormat);
STDMETHODIMP QueryPreferredFormat(__out GUID *pFormat);
STDMETHODIMP SetTimeFormat(const GUID * pFormat);
STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat);
STDMETHODIMP GetTimeFormat(__out GUID *pFormat);
STDMETHODIMP GetDuration(__out LONGLONG *pDuration);
STDMETHODIMP GetStopPosition(__out LONGLONG *pStop);
STDMETHODIMP GetCurrentPosition(__out LONGLONG *pCurrent);
STDMETHODIMP GetCapabilities( __out DWORD * pCapabilities );
STDMETHODIMP CheckCapabilities( __inout DWORD * pCapabilities );
STDMETHODIMP ConvertTimeFormat( __out LONGLONG * pTarget,
__in_opt const GUID * pTargetFormat,
LONGLONG Source,
__in_opt const GUID * pSourceFormat );
STDMETHODIMP SetPositions( __inout_opt LONGLONG * pCurrent, DWORD CurrentFlags
, __inout_opt LONGLONG * pStop, DWORD StopFlags );
STDMETHODIMP GetPositions( __out_opt LONGLONG * pCurrent, __out_opt LONGLONG * pStop );
STDMETHODIMP GetAvailable( __out_opt LONGLONG * pEarliest, __out_opt LONGLONG * pLatest );
STDMETHODIMP SetRate( double dRate);
STDMETHODIMP GetRate( __out double * pdRate);
STDMETHODIMP GetPreroll(__out LONGLONG *pPreroll);
protected:
// ctor
CSourceSeeking(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, __in CCritSec *);
// we call this to notify changes. Override to handle them
virtual HRESULT ChangeStart() PURE;
virtual HRESULT ChangeStop() PURE;
virtual HRESULT ChangeRate() PURE;
REFERENCE_TIME m_rtDuration; // length of stream
REFERENCE_TIME m_rtStart; // source will start here
REFERENCE_TIME m_rtStop; // source will stop here
double m_dRateSeeking;
// seeking capabilities
DWORD m_dwSeekingCaps;
CCritSec * m_pLock;
};
// Base classes supporting Deferred commands.
// Deferred commands are queued by calls to methods on the IQueueCommand
// interface, exposed by the filtergraph and by some filters. A successful
// call to one of these methods will return an IDeferredCommand interface
// representing the queued command.
//
// A CDeferredCommand object represents a single deferred command, and exposes
// the IDeferredCommand interface as well as other methods permitting time
// checks and actual execution. It contains a reference to the CCommandQueue
// object on which it is queued.
//
// CCommandQueue is a base class providing a queue of CDeferredCommand
// objects, and methods to add, remove, check status and invoke the queued
// commands. A CCommandQueue object would be part of an object that
// implemented IQueueCommand.
class CCmdQueue;
// take a copy of the params and store them. Release any allocated
// memory in destructor
class CDispParams : public DISPPARAMS
{
public:
CDispParams(UINT nArgs, __in_ecount(nArgs) VARIANT* pArgs, __inout_opt HRESULT *phr = NULL);
~CDispParams();
};
// CDeferredCommand lifetime is controlled by refcounts. Caller of
// InvokeAt.. gets a refcounted interface pointer, and the CCmdQueue
// object also holds a refcount on us. Calling Cancel or Invoke takes
// us off the CCmdQueue and thus reduces the refcount by 1. Once taken
// off the queue we cannot be put back on the queue.
class CDeferredCommand
: public CUnknown,
public IDeferredCommand
{
public:
CDeferredCommand(
__inout CCmdQueue * pQ,
__in_opt LPUNKNOWN pUnk, // aggregation outer unk
__inout HRESULT * phr,
__in LPUNKNOWN pUnkExecutor, // object that will execute this cmd
REFTIME time,
__in GUID* iid,
long dispidMethod,
short wFlags,
long cArgs,
__in_ecount(cArgs) VARIANT* pDispParams,
__out VARIANT* pvarResult,
__out short* puArgErr,
BOOL bStream
);
DECLARE_IUNKNOWN
// override this to publicise our interfaces
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __out void **ppv);
// IDeferredCommand methods
STDMETHODIMP Cancel();
STDMETHODIMP Confidence(
__out LONG* pConfidence);
STDMETHODIMP Postpone(
REFTIME newtime);
STDMETHODIMP GetHResult(
__out HRESULT* phrResult);
// other public methods
HRESULT Invoke();
// access methods
// returns TRUE if streamtime, FALSE if presentation time
BOOL IsStreamTime() {
return m_bStream;
};
CRefTime GetTime() {
return m_time;
};
REFIID GetIID() {
return *m_iid;
};
long GetMethod() {
return m_dispidMethod;
};
short GetFlags() {
return m_wFlags;
};
DISPPARAMS* GetParams() {
return &m_DispParams;
};
VARIANT* GetResult() {
return m_pvarResult;
};
protected:
CCmdQueue* m_pQueue;
// pUnk for the interface that we will execute the command on
LPUNKNOWN m_pUnk;
// stored command data
REFERENCE_TIME m_time;
GUID* m_iid;
long m_dispidMethod;
short m_wFlags;
VARIANT* m_pvarResult;
BOOL m_bStream;
CDispParams m_DispParams;
DISPID m_DispId; // For get and put
// we use this for ITypeInfo access
CBaseDispatch m_Dispatch;
// save retval here
HRESULT m_hrResult;
};
// a list of CDeferredCommand objects. this is a base class providing
// the basics of access to the list. If you want to use CDeferredCommand
// objects then your queue needs to be derived from this class.
class AM_NOVTABLE CCmdQueue
{
public:
CCmdQueue(__inout_opt HRESULT *phr = NULL);
virtual ~CCmdQueue();
// returns a new CDeferredCommand object that will be initialised with
// the parameters and will be added to the queue during construction.
// returns S_OK if successfully created otherwise an error and
// no object has been queued.
virtual HRESULT New(
__out CDeferredCommand **ppCmd,
__in LPUNKNOWN pUnk,
REFTIME time,
__in GUID* iid,
long dispidMethod,
short wFlags,
long cArgs,
__in_ecount(cArgs) VARIANT* pDispParams,
__out VARIANT* pvarResult,
__out short* puArgErr,
BOOL bStream
);
// called by the CDeferredCommand object to add and remove itself
// from the queue
virtual HRESULT Insert(__in CDeferredCommand* pCmd);
virtual HRESULT Remove(__in CDeferredCommand* pCmd);
// Command-Due Checking
//
// There are two schemes of synchronisation: coarse and accurate. In
// coarse mode, you wait till the time arrives and then execute the cmd.
// In accurate mode, you wait until you are processing the sample that
// will appear at the time, and then execute the command. It's up to the
// filter which one it will implement. The filtergraph will always
// implement coarse mode for commands queued at the filtergraph.
//
// If you want coarse sync, you probably want to wait until there is a
// command due, and then execute it. You can do this by calling
// GetDueCommand. If you have several things to wait for, get the
// event handle from GetDueHandle() and when this is signalled then call
// GetDueCommand. Stream time will only advance between calls to Run and
// EndRun. Note that to avoid an extra thread there is no guarantee that
// if the handle is set there will be a command ready. Each time the
// event is signalled, call GetDueCommand (probably with a 0 timeout);
// This may return E_ABORT.
//
// If you want accurate sync, you must call GetCommandDueFor, passing
// as a parameter the stream time of the samples you are about to process.
// This will return:
// -- a stream-time command due at or before that stream time
// -- a presentation-time command due at or before the
// time that stream time will be presented (only between Run
// and EndRun calls, since outside of this, the mapping from
// stream time to presentation time is not known.
// -- any presentation-time command due now.
// This means that if you want accurate synchronisation on samples that
// might be processed during Paused mode, you need to use
// stream-time commands.
//
// In all cases, commands remain queued until Invoked or Cancelled. The
// setting and resetting of the event handle is managed entirely by this
// queue object.
// set the clock used for timing
virtual HRESULT SetSyncSource(__in_opt IReferenceClock*);
// switch to run mode. Streamtime to Presentation time mapping known.
virtual HRESULT Run(REFERENCE_TIME tStreamTimeOffset);
// switch to Stopped or Paused mode. Time mapping not known.
virtual HRESULT EndRun();
// return a pointer to the next due command. Blocks for msTimeout
// milliseconds until there is a due command.
// Stream-time commands will only become due between Run and Endrun calls.
// The command remains queued until invoked or cancelled.
// Returns E_ABORT if timeout occurs, otherwise S_OK (or other error).
// Returns an AddRef-ed object
virtual HRESULT GetDueCommand(__out CDeferredCommand ** ppCmd, long msTimeout);
// return the event handle that will be signalled whenever
// there are deferred commands due for execution (when GetDueCommand
// will not block).
HANDLE GetDueHandle() {
return HANDLE(m_evDue);
};
// return a pointer to a command that will be due for a given time.
// Pass in a stream time here. The stream time offset will be passed
// in via the Run method.
// Commands remain queued until invoked or cancelled.
// This method will not block. It will report VFW_E_NOT_FOUND if there
// are no commands due yet.
// Returns an AddRef-ed object
virtual HRESULT GetCommandDueFor(REFERENCE_TIME tStream, __out CDeferredCommand**ppCmd);
// check if a given time is due (TRUE if it is due yet)
BOOL CheckTime(CRefTime time, BOOL bStream) {
// if no clock, nothing is due!
if (!m_pClock) {
return FALSE;
}
// stream time
if (bStream) {
// not valid if not running
if (!m_bRunning) {
return FALSE;
}
// add on known stream time offset to get presentation time
time += m_StreamTimeOffset;
}
CRefTime Now;
m_pClock->GetTime((REFERENCE_TIME*)&Now);
return (time <= Now);
};
protected:
// protect access to lists etc
CCritSec m_Lock;
// commands queued in presentation time are stored here
CGenericList<CDeferredCommand> m_listPresentation;
// commands queued in stream time are stored here
CGenericList<CDeferredCommand> m_listStream;
// set when any commands are due
CAMEvent m_evDue;
// creates an advise for the earliest time required, if any
void SetTimeAdvise(void);
// advise id from reference clock (0 if no outstanding advise)
DWORD_PTR m_dwAdvise;
// advise time is for this presentation time
CRefTime m_tCurrentAdvise;
// the reference clock we are using (addrefed)
IReferenceClock* m_pClock;
// true when running
BOOL m_bRunning;
// contains stream time offset when m_bRunning is true
CRefTime m_StreamTimeOffset;
};
#endif // __CTLUTIL__
|