-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenbase.cpp
2858 lines (2246 loc) · 94.4 KB
/
renbase.cpp
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// File: RenBase.cpp
//
// Desc: DirectShow base classes.
//
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#include <streams.h> // DirectShow base class definitions
#include <mmsystem.h> // Needed for definition of timeGetTime
#include <limits.h> // Standard data type limit definitions
#include <measure.h> // Used for time critical log functions
#pragma warning(disable:4355)
// Helper function for clamping time differences
int inline TimeDiff(REFERENCE_TIME rt)
{
if (rt < - (50 * UNITS)) {
return -(50 * UNITS);
} else
if (rt > 50 * UNITS) {
return 50 * UNITS;
} else return (int)rt;
}
// Implements the CBaseRenderer class
CBaseRenderer::CBaseRenderer(REFCLSID RenderClass, // CLSID for this renderer
__in_opt LPCTSTR pName, // Debug ONLY description
__inout_opt LPUNKNOWN pUnk, // Aggregated owner object
__inout HRESULT *phr) : // General OLE return code
CBaseFilter(pName,pUnk,&m_InterfaceLock,RenderClass),
m_evComplete(TRUE, phr),
m_RenderEvent(FALSE, phr),
m_bAbort(FALSE),
m_pPosition(NULL),
m_ThreadSignal(TRUE, phr),
m_bStreaming(FALSE),
m_bEOS(FALSE),
m_bEOSDelivered(FALSE),
m_pMediaSample(NULL),
m_dwAdvise(0),
m_pQSink(NULL),
m_pInputPin(NULL),
m_bRepaintStatus(TRUE),
m_SignalTime(0),
m_bInReceive(FALSE),
m_EndOfStreamTimer(0)
{
if (SUCCEEDED(*phr)) {
Ready();
#ifdef PERF
m_idBaseStamp = MSR_REGISTER(TEXT("BaseRenderer: sample time stamp"));
m_idBaseRenderTime = MSR_REGISTER(TEXT("BaseRenderer: draw time (msec)"));
m_idBaseAccuracy = MSR_REGISTER(TEXT("BaseRenderer: Accuracy (msec)"));
#endif
}
}
// Delete the dynamically allocated IMediaPosition and IMediaSeeking helper
// object. The object is created when somebody queries us. These are standard
// control interfaces for seeking and setting start/stop positions and rates.
// We will probably also have made an input pin based on CRendererInputPin
// that has to be deleted, it's created when an enumerator calls our GetPin
CBaseRenderer::~CBaseRenderer()
{
ASSERT(m_bStreaming == FALSE);
ASSERT(m_EndOfStreamTimer == 0);
StopStreaming();
ClearPendingSample();
// Delete any IMediaPosition implementation
if (m_pPosition) {
delete m_pPosition;
m_pPosition = NULL;
}
// Delete any input pin created
if (m_pInputPin) {
delete m_pInputPin;
m_pInputPin = NULL;
}
// Release any Quality sink
ASSERT(m_pQSink == NULL);
}
// This returns the IMediaPosition and IMediaSeeking interfaces
HRESULT CBaseRenderer::GetMediaPositionInterface(REFIID riid, __deref_out void **ppv)
{
CAutoLock cObjectCreationLock(&m_ObjectCreationLock);
if (m_pPosition) {
return m_pPosition->NonDelegatingQueryInterface(riid,ppv);
}
CBasePin *pPin = GetPin(0);
if (NULL == pPin) {
return E_OUTOFMEMORY;
}
HRESULT hr = NOERROR;
// Create implementation of this dynamically since sometimes we may
// never try and do a seek. The helper object implements a position
// control interface (IMediaPosition) which in fact simply takes the
// calls normally from the filter graph and passes them upstream
m_pPosition = new CRendererPosPassThru(NAME("Renderer CPosPassThru"),
CBaseFilter::GetOwner(),
(HRESULT *) &hr,
pPin);
if (m_pPosition == NULL) {
return E_OUTOFMEMORY;
}
if (FAILED(hr)) {
delete m_pPosition;
m_pPosition = NULL;
return E_NOINTERFACE;
}
return GetMediaPositionInterface(riid,ppv);
}
// Overriden to say what interfaces we support and where
STDMETHODIMP CBaseRenderer::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
{
// Do we have this interface
if (riid == IID_IMediaPosition || riid == IID_IMediaSeeking) {
return GetMediaPositionInterface(riid,ppv);
} else {
return CBaseFilter::NonDelegatingQueryInterface(riid,ppv);
}
}
// This is called whenever we change states, we have a manual reset event that
// is signalled whenever we don't won't the source filter thread to wait in us
// (such as in a stopped state) and likewise is not signalled whenever it can
// wait (during paused and running) this function sets or resets the thread
// event. The event is used to stop source filter threads waiting in Receive
HRESULT CBaseRenderer::SourceThreadCanWait(BOOL bCanWait)
{
if (bCanWait == TRUE) {
m_ThreadSignal.Reset();
} else {
m_ThreadSignal.Set();
}
return NOERROR;
}
#ifdef DEBUG
// Dump the current renderer state to the debug terminal. The hardest part of
// the renderer is the window where we unlock everything to wait for a clock
// to signal it is time to draw or for the application to cancel everything
// by stopping the filter. If we get things wrong we can leave the thread in
// WaitForRenderTime with no way for it to ever get out and we will deadlock
void CBaseRenderer::DisplayRendererState()
{
DbgLog((LOG_TIMING, 1, TEXT("\nTimed out in WaitForRenderTime")));
// No way should this be signalled at this point
BOOL bSignalled = m_ThreadSignal.Check();
DbgLog((LOG_TIMING, 1, TEXT("Signal sanity check %d"),bSignalled));
// Now output the current renderer state variables
DbgLog((LOG_TIMING, 1, TEXT("Filter state %d"),m_State));
DbgLog((LOG_TIMING, 1, TEXT("Abort flag %d"),m_bAbort));
DbgLog((LOG_TIMING, 1, TEXT("Streaming flag %d"),m_bStreaming));
DbgLog((LOG_TIMING, 1, TEXT("Clock advise link %d"),m_dwAdvise));
DbgLog((LOG_TIMING, 1, TEXT("Current media sample %x"),m_pMediaSample));
DbgLog((LOG_TIMING, 1, TEXT("EOS signalled %d"),m_bEOS));
DbgLog((LOG_TIMING, 1, TEXT("EOS delivered %d"),m_bEOSDelivered));
DbgLog((LOG_TIMING, 1, TEXT("Repaint status %d"),m_bRepaintStatus));
// Output the delayed end of stream timer information
DbgLog((LOG_TIMING, 1, TEXT("End of stream timer %x"),m_EndOfStreamTimer));
DbgLog((LOG_TIMING, 1, TEXT("Deliver time %s"),CDisp((LONGLONG)m_SignalTime)));
// Should never timeout during a flushing state
BOOL bFlushing = m_pInputPin->IsFlushing();
DbgLog((LOG_TIMING, 1, TEXT("Flushing sanity check %d"),bFlushing));
// Display the time we were told to start at
DbgLog((LOG_TIMING, 1, TEXT("Last run time %s"),CDisp((LONGLONG)m_tStart.m_time)));
// Have we got a reference clock
if (m_pClock == NULL) return;
// Get the current time from the wall clock
CRefTime CurrentTime,StartTime,EndTime;
m_pClock->GetTime((REFERENCE_TIME*) &CurrentTime);
CRefTime Offset = CurrentTime - m_tStart;
// Display the current time from the clock
DbgLog((LOG_TIMING, 1, TEXT("Clock time %s"),CDisp((LONGLONG)CurrentTime.m_time)));
DbgLog((LOG_TIMING, 1, TEXT("Time difference %dms"),Offset.Millisecs()));
// Do we have a sample ready to render
if (m_pMediaSample == NULL) return;
m_pMediaSample->GetTime((REFERENCE_TIME*)&StartTime, (REFERENCE_TIME*)&EndTime);
DbgLog((LOG_TIMING, 1, TEXT("Next sample stream times (Start %d End %d ms)"),
StartTime.Millisecs(),EndTime.Millisecs()));
// Calculate how long it is until it is due for rendering
CRefTime Wait = (m_tStart + StartTime) - CurrentTime;
DbgLog((LOG_TIMING, 1, TEXT("Wait required %d ms"),Wait.Millisecs()));
}
#endif
// Wait until the clock sets the timer event or we're otherwise signalled. We
// set an arbitrary timeout for this wait and if it fires then we display the
// current renderer state on the debugger. It will often fire if the filter's
// left paused in an application however it may also fire during stress tests
// if the synchronisation with application seeks and state changes is faulty
#define RENDER_TIMEOUT 10000
HRESULT CBaseRenderer::WaitForRenderTime()
{
HANDLE WaitObjects[] = { m_ThreadSignal, m_RenderEvent };
DWORD Result = WAIT_TIMEOUT;
// Wait for either the time to arrive or for us to be stopped
OnWaitStart();
while (Result == WAIT_TIMEOUT) {
Result = WaitForMultipleObjects(2,WaitObjects,FALSE,RENDER_TIMEOUT);
#ifdef DEBUG
if (Result == WAIT_TIMEOUT) DisplayRendererState();
#endif
}
OnWaitEnd();
// We may have been awoken without the timer firing
if (Result == WAIT_OBJECT_0) {
return VFW_E_STATE_CHANGED;
}
SignalTimerFired();
return NOERROR;
}
// Poll waiting for Receive to complete. This really matters when
// Receive may set the palette and cause window messages
// The problem is that if we don't really wait for a renderer to
// stop processing we can deadlock waiting for a transform which
// is calling the renderer's Receive() method because the transform's
// Stop method doesn't know to process window messages to unblock
// the renderer's Receive processing
void CBaseRenderer::WaitForReceiveToComplete()
{
for (;;) {
if (!m_bInReceive) {
break;
}
MSG msg;
// Receive all interthread snedmessages
PeekMessage(&msg, NULL, WM_NULL, WM_NULL, PM_NOREMOVE);
Sleep(1);
}
// If the wakebit for QS_POSTMESSAGE is set, the PeekMessage call
// above just cleared the changebit which will cause some messaging
// calls to block (waitMessage, MsgWaitFor...) now.
// Post a dummy message to set the QS_POSTMESSAGE bit again
if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) {
// Send dummy message
PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0);
}
}
// A filter can have four discrete states, namely Stopped, Running, Paused,
// Intermediate. We are in an intermediate state if we are currently trying
// to pause but haven't yet got the first sample (or if we have been flushed
// in paused state and therefore still have to wait for a sample to arrive)
// This class contains an event called m_evComplete which is signalled when
// the current state is completed and is not signalled when we are waiting to
// complete the last state transition. As mentioned above the only time we
// use this at the moment is when we wait for a media sample in paused state
// If while we are waiting we receive an end of stream notification from the
// source filter then we know no data is imminent so we can reset the event
// This means that when we transition to paused the source filter must call
// end of stream on us or send us an image otherwise we'll hang indefinately
// Simple internal way of getting the real state
FILTER_STATE CBaseRenderer::GetRealState() {
return m_State;
}
// The renderer doesn't complete the full transition to paused states until
// it has got one media sample to render. If you ask it for its state while
// it's waiting it will return the state along with VFW_S_STATE_INTERMEDIATE
STDMETHODIMP CBaseRenderer::GetState(DWORD dwMSecs,FILTER_STATE *State)
{
CheckPointer(State,E_POINTER);
if (WaitDispatchingMessages(m_evComplete, dwMSecs) == WAIT_TIMEOUT) {
*State = m_State;
return VFW_S_STATE_INTERMEDIATE;
}
*State = m_State;
return NOERROR;
}
// If we're pausing and we have no samples we don't complete the transition
// to State_Paused and we return S_FALSE. However if the m_bAbort flag has
// been set then all samples are rejected so there is no point waiting for
// one. If we do have a sample then return NOERROR. We will only ever return
// VFW_S_STATE_INTERMEDIATE from GetState after being paused with no sample
// (calling GetState after either being stopped or Run will NOT return this)
HRESULT CBaseRenderer::CompleteStateChange(FILTER_STATE OldState)
{
// Allow us to be paused when disconnected
if (m_pInputPin->IsConnected() == FALSE) {
Ready();
return S_OK;
}
// Have we run off the end of stream
if (IsEndOfStream() == TRUE) {
Ready();
return S_OK;
}
// Make sure we get fresh data after being stopped
if (HaveCurrentSample() == TRUE) {
if (OldState != State_Stopped) {
Ready();
return S_OK;
}
}
NotReady();
return S_FALSE;
}
// When we stop the filter the things we do are:-
// Decommit the allocator being used in the connection
// Release the source filter if it's waiting in Receive
// Cancel any advise link we set up with the clock
// Any end of stream signalled is now obsolete so reset
// Allow us to be stopped when we are not connected
STDMETHODIMP CBaseRenderer::Stop()
{
CAutoLock cRendererLock(&m_InterfaceLock);
// Make sure there really is a state change
if (m_State == State_Stopped) {
return NOERROR;
}
// Is our input pin connected
if (m_pInputPin->IsConnected() == FALSE) {
NOTE("Input pin is not connected");
m_State = State_Stopped;
return NOERROR;
}
CBaseFilter::Stop();
// If we are going into a stopped state then we must decommit whatever
// allocator we are using it so that any source filter waiting in the
// GetBuffer can be released and unlock themselves for a state change
if (m_pInputPin->Allocator()) {
m_pInputPin->Allocator()->Decommit();
}
// Cancel any scheduled rendering
SetRepaintStatus(TRUE);
StopStreaming();
SourceThreadCanWait(FALSE);
ResetEndOfStream();
CancelNotification();
// There should be no outstanding clock advise
ASSERT(CancelNotification() == S_FALSE);
ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
ASSERT(m_EndOfStreamTimer == 0);
Ready();
WaitForReceiveToComplete();
m_bAbort = FALSE;
return NOERROR;
}
// When we pause the filter the things we do are:-
// Commit the allocator being used in the connection
// Allow a source filter thread to wait in Receive
// Cancel any clock advise link (we may be running)
// Possibly complete the state change if we have data
// Allow us to be paused when we are not connected
STDMETHODIMP CBaseRenderer::Pause()
{
CAutoLock cRendererLock(&m_InterfaceLock);
FILTER_STATE OldState = m_State;
ASSERT(m_pInputPin->IsFlushing() == FALSE);
// Make sure there really is a state change
if (m_State == State_Paused) {
return CompleteStateChange(State_Paused);
}
// Has our input pin been connected
if (m_pInputPin->IsConnected() == FALSE) {
NOTE("Input pin is not connected");
m_State = State_Paused;
return CompleteStateChange(State_Paused);
}
// Pause the base filter class
HRESULT hr = CBaseFilter::Pause();
if (FAILED(hr)) {
NOTE("Pause failed");
return hr;
}
// Enable EC_REPAINT events again
SetRepaintStatus(TRUE);
StopStreaming();
SourceThreadCanWait(TRUE);
CancelNotification();
ResetEndOfStreamTimer();
// If we are going into a paused state then we must commit whatever
// allocator we are using it so that any source filter can call the
// GetBuffer and expect to get a buffer without returning an error
if (m_pInputPin->Allocator()) {
m_pInputPin->Allocator()->Commit();
}
// There should be no outstanding advise
ASSERT(CancelNotification() == S_FALSE);
ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
ASSERT(m_EndOfStreamTimer == 0);
ASSERT(m_pInputPin->IsFlushing() == FALSE);
// When we come out of a stopped state we must clear any image we were
// holding onto for frame refreshing. Since renderers see state changes
// first we can reset ourselves ready to accept the source thread data
// Paused or running after being stopped causes the current position to
// be reset so we're not interested in passing end of stream signals
if (OldState == State_Stopped) {
m_bAbort = FALSE;
ClearPendingSample();
}
return CompleteStateChange(OldState);
}
// When we run the filter the things we do are:-
// Commit the allocator being used in the connection
// Allow a source filter thread to wait in Receive
// Signal the render event just to get us going
// Start the base class by calling StartStreaming
// Allow us to be run when we are not connected
// Signal EC_COMPLETE if we are not connected
STDMETHODIMP CBaseRenderer::Run(REFERENCE_TIME StartTime)
{
CAutoLock cRendererLock(&m_InterfaceLock);
FILTER_STATE OldState = m_State;
// Make sure there really is a state change
if (m_State == State_Running) {
return NOERROR;
}
// Send EC_COMPLETE if we're not connected
if (m_pInputPin->IsConnected() == FALSE) {
NotifyEvent(EC_COMPLETE,S_OK,(LONG_PTR)(IBaseFilter *)this);
m_State = State_Running;
return NOERROR;
}
Ready();
// Pause the base filter class
HRESULT hr = CBaseFilter::Run(StartTime);
if (FAILED(hr)) {
NOTE("Run failed");
return hr;
}
// Allow the source thread to wait
ASSERT(m_pInputPin->IsFlushing() == FALSE);
SourceThreadCanWait(TRUE);
SetRepaintStatus(FALSE);
// There should be no outstanding advise
ASSERT(CancelNotification() == S_FALSE);
ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
ASSERT(m_EndOfStreamTimer == 0);
ASSERT(m_pInputPin->IsFlushing() == FALSE);
// If we are going into a running state then we must commit whatever
// allocator we are using it so that any source filter can call the
// GetBuffer and expect to get a buffer without returning an error
if (m_pInputPin->Allocator()) {
m_pInputPin->Allocator()->Commit();
}
// When we come out of a stopped state we must clear any image we were
// holding onto for frame refreshing. Since renderers see state changes
// first we can reset ourselves ready to accept the source thread data
// Paused or running after being stopped causes the current position to
// be reset so we're not interested in passing end of stream signals
if (OldState == State_Stopped) {
m_bAbort = FALSE;
ClearPendingSample();
}
return StartStreaming();
}
// Return the number of input pins we support
int CBaseRenderer::GetPinCount()
{
if (m_pInputPin == NULL) {
// Try to create it
(void)GetPin(0);
}
return m_pInputPin != NULL ? 1 : 0;
}
// We only support one input pin and it is numbered zero
CBasePin *CBaseRenderer::GetPin(int n)
{
CAutoLock cObjectCreationLock(&m_ObjectCreationLock);
// Should only ever be called with zero
ASSERT(n == 0);
if (n != 0) {
return NULL;
}
// Create the input pin if not already done so
if (m_pInputPin == NULL) {
// hr must be initialized to NOERROR because
// CRendererInputPin's constructor only changes
// hr's value if an error occurs.
HRESULT hr = NOERROR;
m_pInputPin = new CRendererInputPin(this,&hr,L"In");
if (NULL == m_pInputPin) {
return NULL;
}
if (FAILED(hr)) {
delete m_pInputPin;
m_pInputPin = NULL;
return NULL;
}
}
return m_pInputPin;
}
// If "In" then return the IPin for our input pin, otherwise NULL and error
STDMETHODIMP CBaseRenderer::FindPin(LPCWSTR Id, __deref_out IPin **ppPin)
{
CheckPointer(ppPin,E_POINTER);
if (0==lstrcmpW(Id,L"In")) {
*ppPin = GetPin(0);
if (*ppPin) {
(*ppPin)->AddRef();
} else {
return E_OUTOFMEMORY;
}
} else {
*ppPin = NULL;
return VFW_E_NOT_FOUND;
}
return NOERROR;
}
// Called when the input pin receives an EndOfStream notification. If we have
// not got a sample, then notify EC_COMPLETE now. If we have samples, then set
// m_bEOS and check for this on completing samples. If we're waiting to pause
// then complete the transition to paused state by setting the state event
HRESULT CBaseRenderer::EndOfStream()
{
// Ignore these calls if we are stopped
if (m_State == State_Stopped) {
return NOERROR;
}
// If we have a sample then wait for it to be rendered
m_bEOS = TRUE;
if (m_pMediaSample) {
return NOERROR;
}
// If we are waiting for pause then we are now ready since we cannot now
// carry on waiting for a sample to arrive since we are being told there
// won't be any. This sets an event that the GetState function picks up
Ready();
// Only signal completion now if we are running otherwise queue it until
// we do run in StartStreaming. This is used when we seek because a seek
// causes a pause where early notification of completion is misleading
if (m_bStreaming) {
SendEndOfStream();
}
return NOERROR;
}
// When we are told to flush we should release the source thread
HRESULT CBaseRenderer::BeginFlush()
{
// If paused then report state intermediate until we get some data
if (m_State == State_Paused) {
NotReady();
}
SourceThreadCanWait(FALSE);
CancelNotification();
ClearPendingSample();
// Wait for Receive to complete
WaitForReceiveToComplete();
return NOERROR;
}
// After flushing the source thread can wait in Receive again
HRESULT CBaseRenderer::EndFlush()
{
// Reset the current sample media time
if (m_pPosition) m_pPosition->ResetMediaTime();
// There should be no outstanding advise
ASSERT(CancelNotification() == S_FALSE);
SourceThreadCanWait(TRUE);
return NOERROR;
}
// We can now send EC_REPAINTs if so required
HRESULT CBaseRenderer::CompleteConnect(IPin *pReceivePin)
{
// The caller should always hold the interface lock because
// the function uses CBaseFilter::m_State.
ASSERT(CritCheckIn(&m_InterfaceLock));
m_bAbort = FALSE;
if (State_Running == GetRealState()) {
HRESULT hr = StartStreaming();
if (FAILED(hr)) {
return hr;
}
SetRepaintStatus(FALSE);
} else {
SetRepaintStatus(TRUE);
}
return NOERROR;
}
// Called when we go paused or running
HRESULT CBaseRenderer::Active()
{
return NOERROR;
}
// Called when we go into a stopped state
HRESULT CBaseRenderer::Inactive()
{
if (m_pPosition) {
m_pPosition->ResetMediaTime();
}
// People who derive from this may want to override this behaviour
// to keep hold of the sample in some circumstances
ClearPendingSample();
return NOERROR;
}
// Tell derived classes about the media type agreed
HRESULT CBaseRenderer::SetMediaType(const CMediaType *pmt)
{
return NOERROR;
}
// When we break the input pin connection we should reset the EOS flags. When
// we are asked for either IMediaPosition or IMediaSeeking we will create a
// CPosPassThru object to handles media time pass through. When we're handed
// samples we store (by calling CPosPassThru::RegisterMediaTime) their media
// times so we can then return a real current position of data being rendered
HRESULT CBaseRenderer::BreakConnect()
{
// Do we have a quality management sink
if (m_pQSink) {
m_pQSink->Release();
m_pQSink = NULL;
}
// Check we have a valid connection
if (m_pInputPin->IsConnected() == FALSE) {
return S_FALSE;
}
// Check we are stopped before disconnecting
if (m_State != State_Stopped && !m_pInputPin->CanReconnectWhenActive()) {
return VFW_E_NOT_STOPPED;
}
SetRepaintStatus(FALSE);
ResetEndOfStream();
ClearPendingSample();
m_bAbort = FALSE;
if (State_Running == m_State) {
StopStreaming();
}
return NOERROR;
}
// Retrieves the sample times for this samples (note the sample times are
// passed in by reference not value). We return S_FALSE to say schedule this
// sample according to the times on the sample. We also return S_OK in
// which case the object should simply render the sample data immediately
HRESULT CBaseRenderer::GetSampleTimes(IMediaSample *pMediaSample,
__out REFERENCE_TIME *pStartTime,
__out REFERENCE_TIME *pEndTime)
{
ASSERT(m_dwAdvise == 0);
ASSERT(pMediaSample);
// If the stop time for this sample is before or the same as start time,
// then just ignore it (release it) and schedule the next one in line
// Source filters should always fill in the start and end times properly!
if (SUCCEEDED(pMediaSample->GetTime(pStartTime, pEndTime))) {
if (*pEndTime < *pStartTime) {
return VFW_E_START_TIME_AFTER_END;
}
} else {
// no time set in the sample... draw it now?
return S_OK;
}
// Can't synchronise without a clock so we return S_OK which tells the
// caller that the sample should be rendered immediately without going
// through the overhead of setting a timer advise link with the clock
if (m_pClock == NULL) {
return S_OK;
}
return ShouldDrawSampleNow(pMediaSample,pStartTime,pEndTime);
}
// By default all samples are drawn according to their time stamps so we
// return S_FALSE. Returning S_OK means draw immediately, this is used
// by the derived video renderer class in its quality management.
HRESULT CBaseRenderer::ShouldDrawSampleNow(IMediaSample *pMediaSample,
__out REFERENCE_TIME *ptrStart,
__out REFERENCE_TIME *ptrEnd)
{
return S_FALSE;
}
// We must always reset the current advise time to zero after a timer fires
// because there are several possible ways which lead us not to do any more
// scheduling such as the pending image being cleared after state changes
void CBaseRenderer::SignalTimerFired()
{
m_dwAdvise = 0;
}
// Cancel any notification currently scheduled. This is called by the owning
// window object when it is told to stop streaming. If there is no timer link
// outstanding then calling this is benign otherwise we go ahead and cancel
// We must always reset the render event as the quality management code can
// signal immediate rendering by setting the event without setting an advise
// link. If we're subsequently stopped and run the first attempt to setup an
// advise link with the reference clock will find the event still signalled
HRESULT CBaseRenderer::CancelNotification()
{
ASSERT(m_dwAdvise == 0 || m_pClock);
DWORD_PTR dwAdvise = m_dwAdvise;
// Have we a live advise link
if (m_dwAdvise) {
m_pClock->Unadvise(m_dwAdvise);
SignalTimerFired();
ASSERT(m_dwAdvise == 0);
}
// Clear the event and return our status
m_RenderEvent.Reset();
return (dwAdvise ? S_OK : S_FALSE);
}
// Responsible for setting up one shot advise links with the clock
// Return FALSE if the sample is to be dropped (not drawn at all)
// Return TRUE if the sample is to be drawn and in this case also
// arrange for m_RenderEvent to be set at the appropriate time
BOOL CBaseRenderer::ScheduleSample(IMediaSample *pMediaSample)
{
REFERENCE_TIME StartSample, EndSample;
// Is someone pulling our leg
if (pMediaSample == NULL) {
return FALSE;
}
// Get the next sample due up for rendering. If there aren't any ready
// then GetNextSampleTimes returns an error. If there is one to be done
// then it succeeds and yields the sample times. If it is due now then
// it returns S_OK other if it's to be done when due it returns S_FALSE
HRESULT hr = GetSampleTimes(pMediaSample, &StartSample, &EndSample);
if (FAILED(hr)) {
return FALSE;
}
// If we don't have a reference clock then we cannot set up the advise
// time so we simply set the event indicating an image to render. This
// will cause us to run flat out without any timing or synchronisation
if (hr == S_OK) {
EXECUTE_ASSERT(SetEvent((HANDLE) m_RenderEvent));
return TRUE;
}
ASSERT(m_dwAdvise == 0);
ASSERT(m_pClock);
ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
// We do have a valid reference clock interface so we can ask it to
// set an event when the image comes due for rendering. We pass in
// the reference time we were told to start at and also the current
// stream time which is the offset from the start reference time
hr = m_pClock->AdviseTime(
(REFERENCE_TIME) m_tStart, // Start run time
StartSample, // Stream time
(HEVENT)(HANDLE) m_RenderEvent, // Render notification
&m_dwAdvise); // Advise cookie
if (SUCCEEDED(hr)) {
return TRUE;
}
// We could not schedule the next sample for rendering despite the fact
// we have a valid sample here. This is a fair indication that either
// the system clock is wrong or the time stamp for the sample is duff
ASSERT(m_dwAdvise == 0);
return FALSE;
}
// This is called when a sample comes due for rendering. We pass the sample
// on to the derived class. After rendering we will initialise the timer for
// the next sample, NOTE signal that the last one fired first, if we don't
// do this it thinks there is still one outstanding that hasn't completed
HRESULT CBaseRenderer::Render(IMediaSample *pMediaSample)
{
// If the media sample is NULL then we will have been notified by the
// clock that another sample is ready but in the mean time someone has
// stopped us streaming which causes the next sample to be released
if (pMediaSample == NULL) {
return S_FALSE;
}
// If we have stopped streaming then don't render any more samples, the
// thread that got in and locked us and then reset this flag does not
// clear the pending sample as we can use it to refresh any output device
if (m_bStreaming == FALSE) {
return S_FALSE;
}
// Time how long the rendering takes
OnRenderStart(pMediaSample);
DoRenderSample(pMediaSample);