Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2844 lines
97 KiB

  1. //------------------------------------------------------------------------------
  2. // File: RenBase.cpp
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #include <streams.h> // DirectShow base class definitions
  9. #include <mmsystem.h> // Needed for definition of timeGetTime
  10. #include <limits.h> // Standard data type limit definitions
  11. #include <measure.h> // Used for time critical log functions
  12. #pragma warning(disable:4355)
  13. // Helper function for clamping time differences
  14. int inline TimeDiff(REFERENCE_TIME rt)
  15. {
  16. if (rt < - (50 * UNITS)) {
  17. return -(50 * UNITS);
  18. } else
  19. if (rt > 50 * UNITS) {
  20. return 50 * UNITS;
  21. } else return (int)rt;
  22. }
  23. // Implements the CBaseRenderer class
  24. CBaseRenderer::CBaseRenderer(REFCLSID RenderClass, // CLSID for this renderer
  25. TCHAR *pName, // Debug ONLY description
  26. LPUNKNOWN pUnk, // Aggregated owner object
  27. HRESULT *phr) : // General OLE return code
  28. CBaseFilter(pName,pUnk,&m_InterfaceLock,RenderClass),
  29. m_evComplete(TRUE),
  30. m_bAbort(FALSE),
  31. m_pPosition(NULL),
  32. m_ThreadSignal(TRUE),
  33. m_bStreaming(FALSE),
  34. m_bEOS(FALSE),
  35. m_bEOSDelivered(FALSE),
  36. m_pMediaSample(NULL),
  37. m_dwAdvise(0),
  38. m_pQSink(NULL),
  39. m_pInputPin(NULL),
  40. m_bRepaintStatus(TRUE),
  41. m_SignalTime(0),
  42. m_bInReceive(FALSE),
  43. m_EndOfStreamTimer(0)
  44. {
  45. Ready();
  46. #ifdef PERF
  47. m_idBaseStamp = MSR_REGISTER(TEXT("BaseRenderer: sample time stamp"));
  48. m_idBaseRenderTime = MSR_REGISTER(TEXT("BaseRenderer: draw time (msec)"));
  49. m_idBaseAccuracy = MSR_REGISTER(TEXT("BaseRenderer: Accuracy (msec)"));
  50. #endif
  51. }
  52. // Delete the dynamically allocated IMediaPosition and IMediaSeeking helper
  53. // object. The object is created when somebody queries us. These are standard
  54. // control interfaces for seeking and setting start/stop positions and rates.
  55. // We will probably also have made an input pin based on CRendererInputPin
  56. // that has to be deleted, it's created when an enumerator calls our GetPin
  57. CBaseRenderer::~CBaseRenderer()
  58. {
  59. ASSERT(m_bStreaming == FALSE);
  60. ASSERT(m_EndOfStreamTimer == 0);
  61. StopStreaming();
  62. ClearPendingSample();
  63. // Delete any IMediaPosition implementation
  64. if (m_pPosition) {
  65. delete m_pPosition;
  66. m_pPosition = NULL;
  67. }
  68. // Delete any input pin created
  69. if (m_pInputPin) {
  70. delete m_pInputPin;
  71. m_pInputPin = NULL;
  72. }
  73. // Release any Quality sink
  74. ASSERT(m_pQSink == NULL);
  75. }
  76. // This returns the IMediaPosition and IMediaSeeking interfaces
  77. HRESULT CBaseRenderer::GetMediaPositionInterface(REFIID riid,void **ppv)
  78. {
  79. CAutoLock cObjectCreationLock(&m_ObjectCreationLock);
  80. if (m_pPosition) {
  81. return m_pPosition->NonDelegatingQueryInterface(riid,ppv);
  82. }
  83. HRESULT hr = NOERROR;
  84. // Create implementation of this dynamically since sometimes we may
  85. // never try and do a seek. The helper object implements a position
  86. // control interface (IMediaPosition) which in fact simply takes the
  87. // calls normally from the filter graph and passes them upstream
  88. m_pPosition = new CRendererPosPassThru(NAME("Renderer CPosPassThru"),
  89. CBaseFilter::GetOwner(),
  90. (HRESULT *) &hr,
  91. GetPin(0));
  92. if (m_pPosition == NULL) {
  93. return E_OUTOFMEMORY;
  94. }
  95. if (FAILED(hr)) {
  96. delete m_pPosition;
  97. m_pPosition = NULL;
  98. return E_NOINTERFACE;
  99. }
  100. return GetMediaPositionInterface(riid,ppv);
  101. }
  102. // Overriden to say what interfaces we support and where
  103. STDMETHODIMP CBaseRenderer::NonDelegatingQueryInterface(REFIID riid,void **ppv)
  104. {
  105. // Do we have this interface
  106. if (riid == IID_IMediaPosition || riid == IID_IMediaSeeking) {
  107. return GetMediaPositionInterface(riid,ppv);
  108. } else {
  109. return CBaseFilter::NonDelegatingQueryInterface(riid,ppv);
  110. }
  111. }
  112. // This is called whenever we change states, we have a manual reset event that
  113. // is signalled whenever we don't won't the source filter thread to wait in us
  114. // (such as in a stopped state) and likewise is not signalled whenever it can
  115. // wait (during paused and running) this function sets or resets the thread
  116. // event. The event is used to stop source filter threads waiting in Receive
  117. HRESULT CBaseRenderer::SourceThreadCanWait(BOOL bCanWait)
  118. {
  119. if (bCanWait == TRUE) {
  120. m_ThreadSignal.Reset();
  121. } else {
  122. m_ThreadSignal.Set();
  123. }
  124. return NOERROR;
  125. }
  126. #ifdef DEBUG
  127. // Dump the current renderer state to the debug terminal. The hardest part of
  128. // the renderer is the window where we unlock everything to wait for a clock
  129. // to signal it is time to draw or for the application to cancel everything
  130. // by stopping the filter. If we get things wrong we can leave the thread in
  131. // WaitForRenderTime with no way for it to ever get out and we will deadlock
  132. void CBaseRenderer::DisplayRendererState()
  133. {
  134. DbgLog((LOG_TIMING, 1, TEXT("\nTimed out in WaitForRenderTime")));
  135. // No way should this be signalled at this point
  136. BOOL bSignalled = m_ThreadSignal.Check();
  137. DbgLog((LOG_TIMING, 1, TEXT("Signal sanity check %d"),bSignalled));
  138. // Now output the current renderer state variables
  139. DbgLog((LOG_TIMING, 1, TEXT("Filter state %d"),m_State));
  140. DbgLog((LOG_TIMING, 1, TEXT("Abort flag %d"),m_bAbort));
  141. DbgLog((LOG_TIMING, 1, TEXT("Streaming flag %d"),m_bStreaming));
  142. DbgLog((LOG_TIMING, 1, TEXT("Clock advise link %d"),m_dwAdvise));
  143. DbgLog((LOG_TIMING, 1, TEXT("Current media sample %x"),m_pMediaSample));
  144. DbgLog((LOG_TIMING, 1, TEXT("EOS signalled %d"),m_bEOS));
  145. DbgLog((LOG_TIMING, 1, TEXT("EOS delivered %d"),m_bEOSDelivered));
  146. DbgLog((LOG_TIMING, 1, TEXT("Repaint status %d"),m_bRepaintStatus));
  147. // Output the delayed end of stream timer information
  148. DbgLog((LOG_TIMING, 1, TEXT("End of stream timer %x"),m_EndOfStreamTimer));
  149. DbgLog((LOG_TIMING, 1, TEXT("Deliver time %s"),CDisp((LONGLONG)m_SignalTime)));
  150. // Should never timeout during a flushing state
  151. BOOL bFlushing = m_pInputPin->IsFlushing();
  152. DbgLog((LOG_TIMING, 1, TEXT("Flushing sanity check %d"),bFlushing));
  153. // Display the time we were told to start at
  154. DbgLog((LOG_TIMING, 1, TEXT("Last run time %s"),CDisp((LONGLONG)m_tStart.m_time)));
  155. // Have we got a reference clock
  156. if (m_pClock == NULL) return;
  157. // Get the current time from the wall clock
  158. CRefTime CurrentTime,StartTime,EndTime;
  159. m_pClock->GetTime((REFERENCE_TIME*) &CurrentTime);
  160. CRefTime Offset = CurrentTime - m_tStart;
  161. // Display the current time from the clock
  162. DbgLog((LOG_TIMING, 1, TEXT("Clock time %s"),CDisp((LONGLONG)CurrentTime.m_time)));
  163. DbgLog((LOG_TIMING, 1, TEXT("Time difference %dms"),Offset.Millisecs()));
  164. // Do we have a sample ready to render
  165. if (m_pMediaSample == NULL) return;
  166. m_pMediaSample->GetTime((REFERENCE_TIME*)&StartTime, (REFERENCE_TIME*)&EndTime);
  167. DbgLog((LOG_TIMING, 1, TEXT("Next sample stream times (Start %d End %d ms)"),
  168. StartTime.Millisecs(),EndTime.Millisecs()));
  169. // Calculate how long it is until it is due for rendering
  170. CRefTime Wait = (m_tStart + StartTime) - CurrentTime;
  171. DbgLog((LOG_TIMING, 1, TEXT("Wait required %d ms"),Wait.Millisecs()));
  172. }
  173. #endif
  174. // Wait until the clock sets the timer event or we're otherwise signalled. We
  175. // set an arbitrary timeout for this wait and if it fires then we display the
  176. // current renderer state on the debugger. It will often fire if the filter's
  177. // left paused in an application however it may also fire during stress tests
  178. // if the synchronisation with application seeks and state changes is faulty
  179. #define RENDER_TIMEOUT 10000
  180. HRESULT CBaseRenderer::WaitForRenderTime()
  181. {
  182. HANDLE WaitObjects[] = { m_ThreadSignal, m_RenderEvent };
  183. DWORD Result = WAIT_TIMEOUT;
  184. // Wait for either the time to arrive or for us to be stopped
  185. OnWaitStart();
  186. while (Result == WAIT_TIMEOUT) {
  187. Result = WaitForMultipleObjects(2,WaitObjects,FALSE,RENDER_TIMEOUT);
  188. #ifdef DEBUG
  189. if (Result == WAIT_TIMEOUT) DisplayRendererState();
  190. #endif
  191. }
  192. OnWaitEnd();
  193. // We may have been awoken without the timer firing
  194. if (Result == WAIT_OBJECT_0) {
  195. return VFW_E_STATE_CHANGED;
  196. }
  197. SignalTimerFired();
  198. return NOERROR;
  199. }
  200. // Poll waiting for Receive to complete. This really matters when
  201. // Receive may set the palette and cause window messages
  202. // The problem is that if we don't really wait for a renderer to
  203. // stop processing we can deadlock waiting for a transform which
  204. // is calling the renderer's Receive() method because the transform's
  205. // Stop method doesn't know to process window messages to unblock
  206. // the renderer's Receive processing
  207. void CBaseRenderer::WaitForReceiveToComplete()
  208. {
  209. for (;;) {
  210. if (!m_bInReceive) {
  211. break;
  212. }
  213. MSG msg;
  214. // Receive all interthread snedmessages
  215. PeekMessage(&msg, NULL, WM_NULL, WM_NULL, PM_NOREMOVE);
  216. Sleep(1);
  217. }
  218. // If the wakebit for QS_POSTMESSAGE is set, the PeekMessage call
  219. // above just cleared the changebit which will cause some messaging
  220. // calls to block (waitMessage, MsgWaitFor...) now.
  221. // Post a dummy message to set the QS_POSTMESSAGE bit again
  222. if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) {
  223. // Send dummy message
  224. PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0);
  225. }
  226. }
  227. // A filter can have four discrete states, namely Stopped, Running, Paused,
  228. // Intermediate. We are in an intermediate state if we are currently trying
  229. // to pause but haven't yet got the first sample (or if we have been flushed
  230. // in paused state and therefore still have to wait for a sample to arrive)
  231. // This class contains an event called m_evComplete which is signalled when
  232. // the current state is completed and is not signalled when we are waiting to
  233. // complete the last state transition. As mentioned above the only time we
  234. // use this at the moment is when we wait for a media sample in paused state
  235. // If while we are waiting we receive an end of stream notification from the
  236. // source filter then we know no data is imminent so we can reset the event
  237. // This means that when we transition to paused the source filter must call
  238. // end of stream on us or send us an image otherwise we'll hang indefinately
  239. // Simple internal way of getting the real state
  240. FILTER_STATE CBaseRenderer::GetRealState() {
  241. return m_State;
  242. }
  243. // The renderer doesn't complete the full transition to paused states until
  244. // it has got one media sample to render. If you ask it for its state while
  245. // it's waiting it will return the state along with VFW_S_STATE_INTERMEDIATE
  246. STDMETHODIMP CBaseRenderer::GetState(DWORD dwMSecs,FILTER_STATE *State)
  247. {
  248. CheckPointer(State,E_POINTER);
  249. if (WaitDispatchingMessages(m_evComplete, dwMSecs) == WAIT_TIMEOUT) {
  250. *State = m_State;
  251. return VFW_S_STATE_INTERMEDIATE;
  252. }
  253. *State = m_State;
  254. return NOERROR;
  255. }
  256. // If we're pausing and we have no samples we don't complete the transition
  257. // to State_Paused and we return S_FALSE. However if the m_bAbort flag has
  258. // been set then all samples are rejected so there is no point waiting for
  259. // one. If we do have a sample then return NOERROR. We will only ever return
  260. // VFW_S_STATE_INTERMEDIATE from GetState after being paused with no sample
  261. // (calling GetState after either being stopped or Run will NOT return this)
  262. HRESULT CBaseRenderer::CompleteStateChange(FILTER_STATE OldState)
  263. {
  264. // Allow us to be paused when disconnected
  265. if (m_pInputPin->IsConnected() == FALSE) {
  266. Ready();
  267. return S_OK;
  268. }
  269. // Have we run off the end of stream
  270. if (IsEndOfStream() == TRUE) {
  271. Ready();
  272. return S_OK;
  273. }
  274. // Make sure we get fresh data after being stopped
  275. if (HaveCurrentSample() == TRUE) {
  276. if (OldState != State_Stopped) {
  277. Ready();
  278. return S_OK;
  279. }
  280. }
  281. NotReady();
  282. return S_FALSE;
  283. }
  284. // When we stop the filter the things we do are:-
  285. // Decommit the allocator being used in the connection
  286. // Release the source filter if it's waiting in Receive
  287. // Cancel any advise link we set up with the clock
  288. // Any end of stream signalled is now obsolete so reset
  289. // Allow us to be stopped when we are not connected
  290. STDMETHODIMP CBaseRenderer::Stop()
  291. {
  292. CAutoLock cRendererLock(&m_InterfaceLock);
  293. // Make sure there really is a state change
  294. if (m_State == State_Stopped) {
  295. return NOERROR;
  296. }
  297. // Is our input pin connected
  298. if (m_pInputPin->IsConnected() == FALSE) {
  299. NOTE("Input pin is not connected");
  300. m_State = State_Stopped;
  301. return NOERROR;
  302. }
  303. CBaseFilter::Stop();
  304. // If we are going into a stopped state then we must decommit whatever
  305. // allocator we are using it so that any source filter waiting in the
  306. // GetBuffer can be released and unlock themselves for a state change
  307. if (m_pInputPin->Allocator()) {
  308. m_pInputPin->Allocator()->Decommit();
  309. }
  310. // Cancel any scheduled rendering
  311. SetRepaintStatus(TRUE);
  312. StopStreaming();
  313. SourceThreadCanWait(FALSE);
  314. ResetEndOfStream();
  315. CancelNotification();
  316. // There should be no outstanding clock advise
  317. ASSERT(CancelNotification() == S_FALSE);
  318. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  319. ASSERT(m_EndOfStreamTimer == 0);
  320. Ready();
  321. WaitForReceiveToComplete();
  322. m_bAbort = FALSE;
  323. return NOERROR;
  324. }
  325. // When we pause the filter the things we do are:-
  326. // Commit the allocator being used in the connection
  327. // Allow a source filter thread to wait in Receive
  328. // Cancel any clock advise link (we may be running)
  329. // Possibly complete the state change if we have data
  330. // Allow us to be paused when we are not connected
  331. STDMETHODIMP CBaseRenderer::Pause()
  332. {
  333. CAutoLock cRendererLock(&m_InterfaceLock);
  334. FILTER_STATE OldState = m_State;
  335. ASSERT(m_pInputPin->IsFlushing() == FALSE);
  336. // Make sure there really is a state change
  337. if (m_State == State_Paused) {
  338. return CompleteStateChange(State_Paused);
  339. }
  340. // Has our input pin been connected
  341. if (m_pInputPin->IsConnected() == FALSE) {
  342. NOTE("Input pin is not connected");
  343. m_State = State_Paused;
  344. return CompleteStateChange(State_Paused);
  345. }
  346. // Pause the base filter class
  347. HRESULT hr = CBaseFilter::Pause();
  348. if (FAILED(hr)) {
  349. NOTE("Pause failed");
  350. return hr;
  351. }
  352. // Enable EC_REPAINT events again
  353. SetRepaintStatus(TRUE);
  354. StopStreaming();
  355. SourceThreadCanWait(TRUE);
  356. CancelNotification();
  357. ResetEndOfStreamTimer();
  358. // If we are going into a paused state then we must commit whatever
  359. // allocator we are using it so that any source filter can call the
  360. // GetBuffer and expect to get a buffer without returning an error
  361. if (m_pInputPin->Allocator()) {
  362. m_pInputPin->Allocator()->Commit();
  363. }
  364. // There should be no outstanding advise
  365. ASSERT(CancelNotification() == S_FALSE);
  366. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  367. ASSERT(m_EndOfStreamTimer == 0);
  368. ASSERT(m_pInputPin->IsFlushing() == FALSE);
  369. // When we come out of a stopped state we must clear any image we were
  370. // holding onto for frame refreshing. Since renderers see state changes
  371. // first we can reset ourselves ready to accept the source thread data
  372. // Paused or running after being stopped causes the current position to
  373. // be reset so we're not interested in passing end of stream signals
  374. if (OldState == State_Stopped) {
  375. m_bAbort = FALSE;
  376. ClearPendingSample();
  377. }
  378. return CompleteStateChange(OldState);
  379. }
  380. // When we run the filter the things we do are:-
  381. // Commit the allocator being used in the connection
  382. // Allow a source filter thread to wait in Receive
  383. // Signal the render event just to get us going
  384. // Start the base class by calling StartStreaming
  385. // Allow us to be run when we are not connected
  386. // Signal EC_COMPLETE if we are not connected
  387. STDMETHODIMP CBaseRenderer::Run(REFERENCE_TIME StartTime)
  388. {
  389. CAutoLock cRendererLock(&m_InterfaceLock);
  390. FILTER_STATE OldState = m_State;
  391. // Make sure there really is a state change
  392. if (m_State == State_Running) {
  393. return NOERROR;
  394. }
  395. // Send EC_COMPLETE if we're not connected
  396. if (m_pInputPin->IsConnected() == FALSE) {
  397. NotifyEvent(EC_COMPLETE,S_OK,(LONG_PTR)(IBaseFilter *)this);
  398. m_State = State_Running;
  399. return NOERROR;
  400. }
  401. Ready();
  402. // Pause the base filter class
  403. HRESULT hr = CBaseFilter::Run(StartTime);
  404. if (FAILED(hr)) {
  405. NOTE("Run failed");
  406. return hr;
  407. }
  408. // Allow the source thread to wait
  409. ASSERT(m_pInputPin->IsFlushing() == FALSE);
  410. SourceThreadCanWait(TRUE);
  411. SetRepaintStatus(FALSE);
  412. // There should be no outstanding advise
  413. ASSERT(CancelNotification() == S_FALSE);
  414. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  415. ASSERT(m_EndOfStreamTimer == 0);
  416. ASSERT(m_pInputPin->IsFlushing() == FALSE);
  417. // If we are going into a running state then we must commit whatever
  418. // allocator we are using it so that any source filter can call the
  419. // GetBuffer and expect to get a buffer without returning an error
  420. if (m_pInputPin->Allocator()) {
  421. m_pInputPin->Allocator()->Commit();
  422. }
  423. // When we come out of a stopped state we must clear any image we were
  424. // holding onto for frame refreshing. Since renderers see state changes
  425. // first we can reset ourselves ready to accept the source thread data
  426. // Paused or running after being stopped causes the current position to
  427. // be reset so we're not interested in passing end of stream signals
  428. if (OldState == State_Stopped) {
  429. m_bAbort = FALSE;
  430. ClearPendingSample();
  431. }
  432. return StartStreaming();
  433. }
  434. // Return the number of input pins we support
  435. int CBaseRenderer::GetPinCount()
  436. {
  437. return 1;
  438. }
  439. // We only support one input pin and it is numbered zero
  440. CBasePin *CBaseRenderer::GetPin(int n)
  441. {
  442. CAutoLock cObjectCreationLock(&m_ObjectCreationLock);
  443. // Should only ever be called with zero
  444. ASSERT(n == 0);
  445. if (n != 0) {
  446. return NULL;
  447. }
  448. // Create the input pin if not already done so
  449. if (m_pInputPin == NULL) {
  450. // hr must be initialized to NOERROR because
  451. // CRendererInputPin's constructor only changes
  452. // hr's value if an error occurs.
  453. HRESULT hr = NOERROR;
  454. m_pInputPin = new CRendererInputPin(this,&hr,L"In");
  455. if (NULL == m_pInputPin) {
  456. return NULL;
  457. }
  458. if (FAILED(hr)) {
  459. delete m_pInputPin;
  460. m_pInputPin = NULL;
  461. return NULL;
  462. }
  463. }
  464. return m_pInputPin;
  465. }
  466. // If "In" then return the IPin for our input pin, otherwise NULL and error
  467. STDMETHODIMP CBaseRenderer::FindPin(LPCWSTR Id, IPin **ppPin)
  468. {
  469. CheckPointer(ppPin,E_POINTER);
  470. if (0==lstrcmpW(Id,L"In")) {
  471. *ppPin = GetPin(0);
  472. ASSERT(*ppPin);
  473. (*ppPin)->AddRef();
  474. } else {
  475. *ppPin = NULL;
  476. return VFW_E_NOT_FOUND;
  477. }
  478. return NOERROR;
  479. }
  480. // Called when the input pin receives an EndOfStream notification. If we have
  481. // not got a sample, then notify EC_COMPLETE now. If we have samples, then set
  482. // m_bEOS and check for this on completing samples. If we're waiting to pause
  483. // then complete the transition to paused state by setting the state event
  484. HRESULT CBaseRenderer::EndOfStream()
  485. {
  486. // Ignore these calls if we are stopped
  487. if (m_State == State_Stopped) {
  488. return NOERROR;
  489. }
  490. // If we have a sample then wait for it to be rendered
  491. m_bEOS = TRUE;
  492. if (m_pMediaSample) {
  493. return NOERROR;
  494. }
  495. // If we are waiting for pause then we are now ready since we cannot now
  496. // carry on waiting for a sample to arrive since we are being told there
  497. // won't be any. This sets an event that the GetState function picks up
  498. Ready();
  499. // Only signal completion now if we are running otherwise queue it until
  500. // we do run in StartStreaming. This is used when we seek because a seek
  501. // causes a pause where early notification of completion is misleading
  502. if (m_bStreaming) {
  503. SendEndOfStream();
  504. }
  505. return NOERROR;
  506. }
  507. // When we are told to flush we should release the source thread
  508. HRESULT CBaseRenderer::BeginFlush()
  509. {
  510. // If paused then report state intermediate until we get some data
  511. if (m_State == State_Paused) {
  512. NotReady();
  513. }
  514. SourceThreadCanWait(FALSE);
  515. CancelNotification();
  516. ClearPendingSample();
  517. // Wait for Receive to complete
  518. WaitForReceiveToComplete();
  519. return NOERROR;
  520. }
  521. // After flushing the source thread can wait in Receive again
  522. HRESULT CBaseRenderer::EndFlush()
  523. {
  524. // Reset the current sample media time
  525. if (m_pPosition) m_pPosition->ResetMediaTime();
  526. // There should be no outstanding advise
  527. ASSERT(CancelNotification() == S_FALSE);
  528. SourceThreadCanWait(TRUE);
  529. return NOERROR;
  530. }
  531. // We can now send EC_REPAINTs if so required
  532. HRESULT CBaseRenderer::CompleteConnect(IPin *pReceivePin)
  533. {
  534. // The caller should always hold the interface lock because
  535. // the function uses CBaseFilter::m_State.
  536. ASSERT(CritCheckIn(&m_InterfaceLock));
  537. m_bAbort = FALSE;
  538. if (State_Running == GetRealState()) {
  539. HRESULT hr = StartStreaming();
  540. if (FAILED(hr)) {
  541. return hr;
  542. }
  543. SetRepaintStatus(FALSE);
  544. } else {
  545. SetRepaintStatus(TRUE);
  546. }
  547. return NOERROR;
  548. }
  549. // Called when we go paused or running
  550. HRESULT CBaseRenderer::Active()
  551. {
  552. return NOERROR;
  553. }
  554. // Called when we go into a stopped state
  555. HRESULT CBaseRenderer::Inactive()
  556. {
  557. if (m_pPosition) {
  558. m_pPosition->ResetMediaTime();
  559. }
  560. // People who derive from this may want to override this behaviour
  561. // to keep hold of the sample in some circumstances
  562. ClearPendingSample();
  563. return NOERROR;
  564. }
  565. // Tell derived classes about the media type agreed
  566. HRESULT CBaseRenderer::SetMediaType(const CMediaType *pmt)
  567. {
  568. return NOERROR;
  569. }
  570. // When we break the input pin connection we should reset the EOS flags. When
  571. // we are asked for either IMediaPosition or IMediaSeeking we will create a
  572. // CPosPassThru object to handles media time pass through. When we're handed
  573. // samples we store (by calling CPosPassThru::RegisterMediaTime) their media
  574. // times so we can then return a real current position of data being rendered
  575. HRESULT CBaseRenderer::BreakConnect()
  576. {
  577. // Do we have a quality management sink
  578. if (m_pQSink) {
  579. m_pQSink->Release();
  580. m_pQSink = NULL;
  581. }
  582. // Check we have a valid connection
  583. if (m_pInputPin->IsConnected() == FALSE) {
  584. return S_FALSE;
  585. }
  586. // Check we are stopped before disconnecting
  587. if (m_State != State_Stopped && !m_pInputPin->CanReconnectWhenActive()) {
  588. return VFW_E_NOT_STOPPED;
  589. }
  590. SetRepaintStatus(FALSE);
  591. ResetEndOfStream();
  592. ClearPendingSample();
  593. m_bAbort = FALSE;
  594. if (State_Running == m_State) {
  595. StopStreaming();
  596. }
  597. return NOERROR;
  598. }
  599. // Retrieves the sample times for this samples (note the sample times are
  600. // passed in by reference not value). We return S_FALSE to say schedule this
  601. // sample according to the times on the sample. We also return S_OK in
  602. // which case the object should simply render the sample data immediately
  603. HRESULT CBaseRenderer::GetSampleTimes(IMediaSample *pMediaSample,
  604. REFERENCE_TIME *pStartTime,
  605. REFERENCE_TIME *pEndTime)
  606. {
  607. ASSERT(m_dwAdvise == 0);
  608. ASSERT(pMediaSample);
  609. // If the stop time for this sample is before or the same as start time,
  610. // then just ignore it (release it) and schedule the next one in line
  611. // Source filters should always fill in the start and end times properly!
  612. if (SUCCEEDED(pMediaSample->GetTime(pStartTime, pEndTime))) {
  613. if (*pEndTime < *pStartTime) {
  614. return VFW_E_START_TIME_AFTER_END;
  615. }
  616. } else {
  617. // no time set in the sample... draw it now?
  618. return S_OK;
  619. }
  620. // Can't synchronise without a clock so we return S_OK which tells the
  621. // caller that the sample should be rendered immediately without going
  622. // through the overhead of setting a timer advise link with the clock
  623. if (m_pClock == NULL) {
  624. return S_OK;
  625. }
  626. return ShouldDrawSampleNow(pMediaSample,pStartTime,pEndTime);
  627. }
  628. // By default all samples are drawn according to their time stamps so we
  629. // return S_FALSE. Returning S_OK means draw immediately, this is used
  630. // by the derived video renderer class in its quality management.
  631. HRESULT CBaseRenderer::ShouldDrawSampleNow(IMediaSample *pMediaSample,
  632. REFERENCE_TIME *ptrStart,
  633. REFERENCE_TIME *ptrEnd)
  634. {
  635. return S_FALSE;
  636. }
  637. // We must always reset the current advise time to zero after a timer fires
  638. // because there are several possible ways which lead us not to do any more
  639. // scheduling such as the pending image being cleared after state changes
  640. void CBaseRenderer::SignalTimerFired()
  641. {
  642. m_dwAdvise = 0;
  643. }
  644. // Cancel any notification currently scheduled. This is called by the owning
  645. // window object when it is told to stop streaming. If there is no timer link
  646. // outstanding then calling this is benign otherwise we go ahead and cancel
  647. // We must always reset the render event as the quality management code can
  648. // signal immediate rendering by setting the event without setting an advise
  649. // link. If we're subsequently stopped and run the first attempt to setup an
  650. // advise link with the reference clock will find the event still signalled
  651. HRESULT CBaseRenderer::CancelNotification()
  652. {
  653. ASSERT(m_dwAdvise == 0 || m_pClock);
  654. DWORD_PTR dwAdvise = m_dwAdvise;
  655. // Have we a live advise link
  656. if (m_dwAdvise) {
  657. m_pClock->Unadvise(m_dwAdvise);
  658. SignalTimerFired();
  659. ASSERT(m_dwAdvise == 0);
  660. }
  661. // Clear the event and return our status
  662. m_RenderEvent.Reset();
  663. return (dwAdvise ? S_OK : S_FALSE);
  664. }
  665. // Responsible for setting up one shot advise links with the clock
  666. // Return FALSE if the sample is to be dropped (not drawn at all)
  667. // Return TRUE if the sample is to be drawn and in this case also
  668. // arrange for m_RenderEvent to be set at the appropriate time
  669. BOOL CBaseRenderer::ScheduleSample(IMediaSample *pMediaSample)
  670. {
  671. REFERENCE_TIME StartSample, EndSample;
  672. // Is someone pulling our leg
  673. if (pMediaSample == NULL) {
  674. return FALSE;
  675. }
  676. // Get the next sample due up for rendering. If there aren't any ready
  677. // then GetNextSampleTimes returns an error. If there is one to be done
  678. // then it succeeds and yields the sample times. If it is due now then
  679. // it returns S_OK other if it's to be done when due it returns S_FALSE
  680. HRESULT hr = GetSampleTimes(pMediaSample, &StartSample, &EndSample);
  681. if (FAILED(hr)) {
  682. return FALSE;
  683. }
  684. // If we don't have a reference clock then we cannot set up the advise
  685. // time so we simply set the event indicating an image to render. This
  686. // will cause us to run flat out without any timing or synchronisation
  687. if (hr == S_OK) {
  688. EXECUTE_ASSERT(SetEvent((HANDLE) m_RenderEvent));
  689. return TRUE;
  690. }
  691. ASSERT(m_dwAdvise == 0);
  692. ASSERT(m_pClock);
  693. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  694. // We do have a valid reference clock interface so we can ask it to
  695. // set an event when the image comes due for rendering. We pass in
  696. // the reference time we were told to start at and also the current
  697. // stream time which is the offset from the start reference time
  698. hr = m_pClock->AdviseTime(
  699. (REFERENCE_TIME) m_tStart, // Start run time
  700. StartSample, // Stream time
  701. (HEVENT)(HANDLE) m_RenderEvent, // Render notification
  702. &m_dwAdvise); // Advise cookie
  703. if (SUCCEEDED(hr)) {
  704. return TRUE;
  705. }
  706. // We could not schedule the next sample for rendering despite the fact
  707. // we have a valid sample here. This is a fair indication that either
  708. // the system clock is wrong or the time stamp for the sample is duff
  709. ASSERT(m_dwAdvise == 0);
  710. return FALSE;
  711. }
  712. // This is called when a sample comes due for rendering. We pass the sample
  713. // on to the derived class. After rendering we will initialise the timer for
  714. // the next sample, NOTE signal that the last one fired first, if we don't
  715. // do this it thinks there is still one outstanding that hasn't completed
  716. HRESULT CBaseRenderer::Render(IMediaSample *pMediaSample)
  717. {
  718. // If the media sample is NULL then we will have been notified by the
  719. // clock that another sample is ready but in the mean time someone has
  720. // stopped us streaming which causes the next sample to be released
  721. if (pMediaSample == NULL) {
  722. return S_FALSE;
  723. }
  724. // If we have stopped streaming then don't render any more samples, the
  725. // thread that got in and locked us and then reset this flag does not
  726. // clear the pending sample as we can use it to refresh any output device
  727. if (m_bStreaming == FALSE) {
  728. return S_FALSE;
  729. }
  730. // Time how long the rendering takes
  731. OnRenderStart(pMediaSample);
  732. DoRenderSample(pMediaSample);
  733. OnRenderEnd(pMediaSample);
  734. return NOERROR;
  735. }
  736. // Checks if there is a sample waiting at the renderer
  737. BOOL CBaseRenderer::HaveCurrentSample()
  738. {
  739. CAutoLock cRendererLock(&m_RendererLock);
  740. return (m_pMediaSample == NULL ? FALSE : TRUE);
  741. }
  742. // Returns the current sample waiting at the video renderer. We AddRef the
  743. // sample before returning so that should it come due for rendering the
  744. // person who called this method will hold the remaining reference count
  745. // that will stop the sample being added back onto the allocator free list
  746. IMediaSample *CBaseRenderer::GetCurrentSample()
  747. {
  748. CAutoLock cRendererLock(&m_RendererLock);
  749. if (m_pMediaSample) {
  750. m_pMediaSample->AddRef();
  751. }
  752. return m_pMediaSample;
  753. }
  754. // Called when the source delivers us a sample. We go through a few checks to
  755. // make sure the sample can be rendered. If we are running (streaming) then we
  756. // have the sample scheduled with the reference clock, if we are not streaming
  757. // then we have received an sample in paused mode so we can complete any state
  758. // transition. On leaving this function everything will be unlocked so an app
  759. // thread may get in and change our state to stopped (for example) in which
  760. // case it will also signal the thread event so that our wait call is stopped
  761. HRESULT CBaseRenderer::PrepareReceive(IMediaSample *pMediaSample)
  762. {
  763. CAutoLock cInterfaceLock(&m_InterfaceLock);
  764. m_bInReceive = TRUE;
  765. // Check our flushing and filter state
  766. // This function must hold the interface lock because it calls
  767. // CBaseInputPin::Receive() and CBaseInputPin::Receive() uses
  768. // CBasePin::m_bRunTimeError.
  769. HRESULT hr = m_pInputPin->CBaseInputPin::Receive(pMediaSample);
  770. if (hr != NOERROR) {
  771. m_bInReceive = FALSE;
  772. return E_FAIL;
  773. }
  774. // Has the type changed on a media sample. We do all rendering
  775. // synchronously on the source thread, which has a side effect
  776. // that only one buffer is ever outstanding. Therefore when we
  777. // have Receive called we can go ahead and change the format
  778. // Since the format change can cause a SendMessage we just don't
  779. // lock
  780. if (m_pInputPin->SampleProps()->pMediaType) {
  781. hr = m_pInputPin->SetMediaType(
  782. (CMediaType *)m_pInputPin->SampleProps()->pMediaType);
  783. if (FAILED(hr)) {
  784. m_bInReceive = FALSE;
  785. return hr;
  786. }
  787. }
  788. CAutoLock cSampleLock(&m_RendererLock);
  789. ASSERT(IsActive() == TRUE);
  790. ASSERT(m_pInputPin->IsFlushing() == FALSE);
  791. ASSERT(m_pInputPin->IsConnected() == TRUE);
  792. ASSERT(m_pMediaSample == NULL);
  793. // Return an error if we already have a sample waiting for rendering
  794. // source pins must serialise the Receive calls - we also check that
  795. // no data is being sent after the source signalled an end of stream
  796. if (m_pMediaSample || m_bEOS || m_bAbort) {
  797. Ready();
  798. m_bInReceive = FALSE;
  799. return E_UNEXPECTED;
  800. }
  801. // Store the media times from this sample
  802. if (m_pPosition) m_pPosition->RegisterMediaTime(pMediaSample);
  803. // Schedule the next sample if we are streaming
  804. if ((m_bStreaming == TRUE) && (ScheduleSample(pMediaSample) == FALSE)) {
  805. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  806. ASSERT(CancelNotification() == S_FALSE);
  807. m_bInReceive = FALSE;
  808. return VFW_E_SAMPLE_REJECTED;
  809. }
  810. // Store the sample end time for EC_COMPLETE handling
  811. m_SignalTime = m_pInputPin->SampleProps()->tStop;
  812. // BEWARE we sometimes keep the sample even after returning the thread to
  813. // the source filter such as when we go into a stopped state (we keep it
  814. // to refresh the device with) so we must AddRef it to keep it safely. If
  815. // we start flushing the source thread is released and any sample waiting
  816. // will be released otherwise GetBuffer may never return (see BeginFlush)
  817. m_pMediaSample = pMediaSample;
  818. m_pMediaSample->AddRef();
  819. if (m_bStreaming == FALSE) {
  820. SetRepaintStatus(TRUE);
  821. }
  822. return NOERROR;
  823. }
  824. // Called by the source filter when we have a sample to render. Under normal
  825. // circumstances we set an advise link with the clock, wait for the time to
  826. // arrive and then render the data using the PURE virtual DoRenderSample that
  827. // the derived class will have overriden. After rendering the sample we may
  828. // also signal EOS if it was the last one sent before EndOfStream was called
  829. HRESULT CBaseRenderer::Receive(IMediaSample *pSample)
  830. {
  831. ASSERT(pSample);
  832. // It may return VFW_E_SAMPLE_REJECTED code to say don't bother
  833. HRESULT hr = PrepareReceive(pSample);
  834. ASSERT(m_bInReceive == SUCCEEDED(hr));
  835. if (FAILED(hr)) {
  836. if (hr == VFW_E_SAMPLE_REJECTED) {
  837. return NOERROR;
  838. }
  839. return hr;
  840. }
  841. // We realize the palette in "PrepareRender()" so we have to give away the
  842. // filter lock here.
  843. if (m_State == State_Paused) {
  844. PrepareRender();
  845. // no need to use InterlockedExchange
  846. m_bInReceive = FALSE;
  847. {
  848. // We must hold both these locks
  849. CAutoLock cRendererLock(&m_InterfaceLock);
  850. if (m_State == State_Stopped)
  851. return NOERROR;
  852. m_bInReceive = TRUE;
  853. CAutoLock cSampleLock(&m_RendererLock);
  854. OnReceiveFirstSample(pSample);
  855. }
  856. Ready();
  857. }
  858. // Having set an advise link with the clock we sit and wait. We may be
  859. // awoken by the clock firing or by a state change. The rendering call
  860. // will lock the critical section and check we can still render the data
  861. hr = WaitForRenderTime();
  862. if (FAILED(hr)) {
  863. m_bInReceive = FALSE;
  864. return NOERROR;
  865. }
  866. PrepareRender();
  867. // Set this here and poll it until we work out the locking correctly
  868. // It can't be right that the streaming stuff grabs the interface
  869. // lock - after all we want to be able to wait for this stuff
  870. // to complete
  871. m_bInReceive = FALSE;
  872. // We must hold both these locks
  873. CAutoLock cRendererLock(&m_InterfaceLock);
  874. // since we gave away the filter wide lock, the sate of the filter could
  875. // have chnaged to Stopped
  876. if (m_State == State_Stopped)
  877. return NOERROR;
  878. CAutoLock cSampleLock(&m_RendererLock);
  879. // Deal with this sample
  880. Render(m_pMediaSample);
  881. ClearPendingSample();
  882. SendEndOfStream();
  883. CancelNotification();
  884. return NOERROR;
  885. }
  886. // This is called when we stop or are inactivated to clear the pending sample
  887. // We release the media sample interface so that they can be allocated to the
  888. // source filter again, unless of course we are changing state to inactive in
  889. // which case GetBuffer will return an error. We must also reset the current
  890. // media sample to NULL so that we know we do not currently have an image
  891. HRESULT CBaseRenderer::ClearPendingSample()
  892. {
  893. CAutoLock cRendererLock(&m_RendererLock);
  894. if (m_pMediaSample) {
  895. m_pMediaSample->Release();
  896. m_pMediaSample = NULL;
  897. }
  898. return NOERROR;
  899. }
  900. // Used to signal end of stream according to the sample end time
  901. void CALLBACK EndOfStreamTimer(UINT uID, // Timer identifier
  902. UINT uMsg, // Not currently used
  903. DWORD_PTR dwUser,// User information
  904. DWORD_PTR dw1, // Windows reserved
  905. DWORD_PTR dw2) // is also reserved
  906. {
  907. CBaseRenderer *pRenderer = (CBaseRenderer *) dwUser;
  908. NOTE1("EndOfStreamTimer called (%d)",uID);
  909. pRenderer->TimerCallback();
  910. }
  911. // Do the timer callback work
  912. void CBaseRenderer::TimerCallback()
  913. {
  914. // Lock for synchronization (but don't hold this lock when calling
  915. // timeKillEvent)
  916. CAutoLock cRendererLock(&m_RendererLock);
  917. // See if we should signal end of stream now
  918. if (m_EndOfStreamTimer) {
  919. m_EndOfStreamTimer = 0;
  920. SendEndOfStream();
  921. }
  922. }
  923. // If we are at the end of the stream signal the filter graph but do not set
  924. // the state flag back to FALSE. Once we drop off the end of the stream we
  925. // leave the flag set (until a subsequent ResetEndOfStream). Each sample we
  926. // get delivered will update m_SignalTime to be the last sample's end time.
  927. // We must wait this long before signalling end of stream to the filtergraph
  928. #define TIMEOUT_DELIVERYWAIT 50
  929. #define TIMEOUT_RESOLUTION 10
  930. HRESULT CBaseRenderer::SendEndOfStream()
  931. {
  932. ASSERT(CritCheckIn(&m_RendererLock));
  933. if (m_bEOS == FALSE || m_bEOSDelivered || m_EndOfStreamTimer) {
  934. return NOERROR;
  935. }
  936. // If there is no clock then signal immediately
  937. if (m_pClock == NULL) {
  938. return NotifyEndOfStream();
  939. }
  940. // How long into the future is the delivery time
  941. REFERENCE_TIME Signal = m_tStart + m_SignalTime;
  942. REFERENCE_TIME CurrentTime;
  943. m_pClock->GetTime(&CurrentTime);
  944. LONG Delay = LONG((Signal - CurrentTime) / 10000);
  945. // Dump the timing information to the debugger
  946. NOTE1("Delay until end of stream delivery %d",Delay);
  947. NOTE1("Current %s",(LPCTSTR)CDisp((LONGLONG)CurrentTime));
  948. NOTE1("Signal %s",(LPCTSTR)CDisp((LONGLONG)Signal));
  949. // Wait for the delivery time to arrive
  950. if (Delay < TIMEOUT_DELIVERYWAIT) {
  951. return NotifyEndOfStream();
  952. }
  953. // Signal a timer callback on another worker thread
  954. m_EndOfStreamTimer = CompatibleTimeSetEvent((UINT) Delay, // Period of timer
  955. TIMEOUT_RESOLUTION, // Timer resolution
  956. EndOfStreamTimer, // Callback function
  957. DWORD_PTR(this), // Used information
  958. TIME_ONESHOT); // Type of callback
  959. if (m_EndOfStreamTimer == 0) {
  960. return NotifyEndOfStream();
  961. }
  962. return NOERROR;
  963. }
  964. // Signals EC_COMPLETE to the filtergraph manager
  965. HRESULT CBaseRenderer::NotifyEndOfStream()
  966. {
  967. CAutoLock cRendererLock(&m_RendererLock);
  968. ASSERT(m_bEOSDelivered == FALSE);
  969. ASSERT(m_EndOfStreamTimer == 0);
  970. // Has the filter changed state
  971. if (m_bStreaming == FALSE) {
  972. ASSERT(m_EndOfStreamTimer == 0);
  973. return NOERROR;
  974. }
  975. // Reset the end of stream timer
  976. m_EndOfStreamTimer = 0;
  977. // If we've been using the IMediaPosition interface, set it's start
  978. // and end media "times" to the stop position by hand. This ensures
  979. // that we actually get to the end, even if the MPEG guestimate has
  980. // been bad or if the quality management dropped the last few frames
  981. if (m_pPosition) m_pPosition->EOS();
  982. m_bEOSDelivered = TRUE;
  983. NOTE("Sending EC_COMPLETE...");
  984. return NotifyEvent(EC_COMPLETE,S_OK,(LONG_PTR)(IBaseFilter *)this);
  985. }
  986. // Reset the end of stream flag, this is typically called when we transfer to
  987. // stopped states since that resets the current position back to the start so
  988. // we will receive more samples or another EndOfStream if there aren't any. We
  989. // keep two separate flags one to say we have run off the end of the stream
  990. // (this is the m_bEOS flag) and another to say we have delivered EC_COMPLETE
  991. // to the filter graph. We need the latter otherwise we can end up sending an
  992. // EC_COMPLETE every time the source changes state and calls our EndOfStream
  993. HRESULT CBaseRenderer::ResetEndOfStream()
  994. {
  995. ResetEndOfStreamTimer();
  996. CAutoLock cRendererLock(&m_RendererLock);
  997. m_bEOS = FALSE;
  998. m_bEOSDelivered = FALSE;
  999. m_SignalTime = 0;
  1000. return NOERROR;
  1001. }
  1002. // Kills any outstanding end of stream timer
  1003. void CBaseRenderer::ResetEndOfStreamTimer()
  1004. {
  1005. ASSERT(CritCheckOut(&m_RendererLock));
  1006. if (m_EndOfStreamTimer) {
  1007. timeKillEvent(m_EndOfStreamTimer);
  1008. m_EndOfStreamTimer = 0;
  1009. }
  1010. }
  1011. // This is called when we start running so that we can schedule any pending
  1012. // image we have with the clock and display any timing information. If we
  1013. // don't have any sample but we have queued an EOS flag then we send it. If
  1014. // we do have a sample then we wait until that has been rendered before we
  1015. // signal the filter graph otherwise we may change state before it's done
  1016. HRESULT CBaseRenderer::StartStreaming()
  1017. {
  1018. CAutoLock cRendererLock(&m_RendererLock);
  1019. if (m_bStreaming == TRUE) {
  1020. return NOERROR;
  1021. }
  1022. // Reset the streaming times ready for running
  1023. m_bStreaming = TRUE;
  1024. timeBeginPeriod(1);
  1025. OnStartStreaming();
  1026. // There should be no outstanding advise
  1027. ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0));
  1028. ASSERT(CancelNotification() == S_FALSE);
  1029. // If we have an EOS and no data then deliver it now
  1030. if (m_pMediaSample == NULL) {
  1031. return SendEndOfStream();
  1032. }
  1033. // Have the data rendered
  1034. ASSERT(m_pMediaSample);
  1035. if (!ScheduleSample(m_pMediaSample))
  1036. m_RenderEvent.Set();
  1037. return NOERROR;
  1038. }
  1039. // This is called when we stop streaming so that we can set our internal flag
  1040. // indicating we are not now to schedule any more samples arriving. The state
  1041. // change methods in the filter implementation take care of cancelling any
  1042. // clock advise link we have set up and clearing any pending sample we have
  1043. HRESULT CBaseRenderer::StopStreaming()
  1044. {
  1045. CAutoLock cRendererLock(&m_RendererLock);
  1046. m_bEOSDelivered = FALSE;
  1047. if (m_bStreaming == TRUE) {
  1048. m_bStreaming = FALSE;
  1049. OnStopStreaming();
  1050. timeEndPeriod(1);
  1051. }
  1052. return NOERROR;
  1053. }
  1054. // We have a boolean flag that is reset when we have signalled EC_REPAINT to
  1055. // the filter graph. We set this when we receive an image so that should any
  1056. // conditions arise again we can send another one. By having a flag we ensure
  1057. // we don't flood the filter graph with redundant calls. We do not set the
  1058. // event when we receive an EndOfStream call since there is no point in us
  1059. // sending further EC_REPAINTs. In particular the AutoShowWindow method and
  1060. // the DirectDraw object use this method to control the window repainting
  1061. void CBaseRenderer::SetRepaintStatus(BOOL bRepaint)
  1062. {
  1063. CAutoLock cSampleLock(&m_RendererLock);
  1064. m_bRepaintStatus = bRepaint;
  1065. }
  1066. // Pass the window handle to the upstream filter
  1067. void CBaseRenderer::SendNotifyWindow(IPin *pPin,HWND hwnd)
  1068. {
  1069. IMediaEventSink *pSink;
  1070. // Does the pin support IMediaEventSink
  1071. HRESULT hr = pPin->QueryInterface(IID_IMediaEventSink,(void **)&pSink);
  1072. if (SUCCEEDED(hr)) {
  1073. pSink->Notify(EC_NOTIFY_WINDOW,LONG_PTR(hwnd),0);
  1074. pSink->Release();
  1075. }
  1076. NotifyEvent(EC_NOTIFY_WINDOW,LONG_PTR(hwnd),0);
  1077. }
  1078. // Signal an EC_REPAINT to the filter graph. This can be used to have data
  1079. // sent to us. For example when a video window is first displayed it may
  1080. // not have an image to display, at which point it signals EC_REPAINT. The
  1081. // filtergraph will either pause the graph if stopped or if already paused
  1082. // it will call put_CurrentPosition of the current position. Setting the
  1083. // current position to itself has the stream flushed and the image resent
  1084. #define RLOG(_x_) DbgLog((LOG_TRACE,1,TEXT(_x_)));
  1085. void CBaseRenderer::SendRepaint()
  1086. {
  1087. CAutoLock cSampleLock(&m_RendererLock);
  1088. ASSERT(m_pInputPin);
  1089. // We should not send repaint notifications when...
  1090. // - An end of stream has been notified
  1091. // - Our input pin is being flushed
  1092. // - The input pin is not connected
  1093. // - We have aborted a video playback
  1094. // - There is a repaint already sent
  1095. if (m_bAbort == FALSE) {
  1096. if (m_pInputPin->IsConnected() == TRUE) {
  1097. if (m_pInputPin->IsFlushing() == FALSE) {
  1098. if (IsEndOfStream() == FALSE) {
  1099. if (m_bRepaintStatus == TRUE) {
  1100. IPin *pPin = (IPin *) m_pInputPin;
  1101. NotifyEvent(EC_REPAINT,(LONG_PTR) pPin,0);
  1102. SetRepaintStatus(FALSE);
  1103. RLOG("Sending repaint");
  1104. }
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. // When a video window detects a display change (WM_DISPLAYCHANGE message) it
  1111. // can send an EC_DISPLAY_CHANGED event code along with the renderer pin. The
  1112. // filtergraph will stop everyone and reconnect our input pin. As we're then
  1113. // reconnected we can accept the media type that matches the new display mode
  1114. // since we may no longer be able to draw the current image type efficiently
  1115. BOOL CBaseRenderer::OnDisplayChange()
  1116. {
  1117. // Ignore if we are not connected yet
  1118. CAutoLock cSampleLock(&m_RendererLock);
  1119. if (m_pInputPin->IsConnected() == FALSE) {
  1120. return FALSE;
  1121. }
  1122. RLOG("Notification of EC_DISPLAY_CHANGE");
  1123. // Pass our input pin as parameter on the event
  1124. IPin *pPin = (IPin *) m_pInputPin;
  1125. m_pInputPin->AddRef();
  1126. NotifyEvent(EC_DISPLAY_CHANGED,(LONG_PTR) pPin,0);
  1127. SetAbortSignal(TRUE);
  1128. ClearPendingSample();
  1129. m_pInputPin->Release();
  1130. return TRUE;
  1131. }
  1132. // Called just before we start drawing.
  1133. // Store the current time in m_trRenderStart to allow the rendering time to be
  1134. // logged. Log the time stamp of the sample and how late it is (neg is early)
  1135. void CBaseRenderer::OnRenderStart(IMediaSample *pMediaSample)
  1136. {
  1137. #ifdef PERF
  1138. REFERENCE_TIME trStart, trEnd;
  1139. pMediaSample->GetTime(&trStart, &trEnd);
  1140. MSR_INTEGER(m_idBaseStamp, (int)trStart); // dump low order 32 bits
  1141. m_pClock->GetTime(&m_trRenderStart);
  1142. MSR_INTEGER(0, (int)m_trRenderStart);
  1143. REFERENCE_TIME trStream;
  1144. trStream = m_trRenderStart-m_tStart; // convert reftime to stream time
  1145. MSR_INTEGER(0,(int)trStream);
  1146. const int trLate = (int)(trStream - trStart);
  1147. MSR_INTEGER(m_idBaseAccuracy, trLate/10000); // dump in mSec
  1148. #endif
  1149. } // OnRenderStart
  1150. // Called directly after drawing an image.
  1151. // calculate the time spent drawing and log it.
  1152. void CBaseRenderer::OnRenderEnd(IMediaSample *pMediaSample)
  1153. {
  1154. #ifdef PERF
  1155. REFERENCE_TIME trNow;
  1156. m_pClock->GetTime(&trNow);
  1157. MSR_INTEGER(0,(int)trNow);
  1158. int t = (int)((trNow - m_trRenderStart)/10000); // convert UNITS->msec
  1159. MSR_INTEGER(m_idBaseRenderTime, t);
  1160. #endif
  1161. } // OnRenderEnd
  1162. // Constructor must be passed the base renderer object
  1163. CRendererInputPin::CRendererInputPin(CBaseRenderer *pRenderer,
  1164. HRESULT *phr,
  1165. LPCWSTR pPinName) :
  1166. CBaseInputPin(NAME("Renderer pin"),
  1167. pRenderer,
  1168. &pRenderer->m_InterfaceLock,
  1169. (HRESULT *) phr,
  1170. pPinName)
  1171. {
  1172. m_pRenderer = pRenderer;
  1173. ASSERT(m_pRenderer);
  1174. }
  1175. // Signals end of data stream on the input pin
  1176. STDMETHODIMP CRendererInputPin::EndOfStream()
  1177. {
  1178. CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock);
  1179. CAutoLock cSampleLock(&m_pRenderer->m_RendererLock);
  1180. // Make sure we're streaming ok
  1181. HRESULT hr = CheckStreaming();
  1182. if (hr != NOERROR) {
  1183. return hr;
  1184. }
  1185. // Pass it onto the renderer
  1186. hr = m_pRenderer->EndOfStream();
  1187. if (SUCCEEDED(hr)) {
  1188. hr = CBaseInputPin::EndOfStream();
  1189. }
  1190. return hr;
  1191. }
  1192. // Signals start of flushing on the input pin - we do the final reset end of
  1193. // stream with the renderer lock unlocked but with the interface lock locked
  1194. // We must do this because we call timeKillEvent, our timer callback method
  1195. // has to take the renderer lock to serialise our state. Therefore holding a
  1196. // renderer lock when calling timeKillEvent could cause a deadlock condition
  1197. STDMETHODIMP CRendererInputPin::BeginFlush()
  1198. {
  1199. CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock);
  1200. {
  1201. CAutoLock cSampleLock(&m_pRenderer->m_RendererLock);
  1202. CBaseInputPin::BeginFlush();
  1203. m_pRenderer->BeginFlush();
  1204. }
  1205. return m_pRenderer->ResetEndOfStream();
  1206. }
  1207. // Signals end of flushing on the input pin
  1208. STDMETHODIMP CRendererInputPin::EndFlush()
  1209. {
  1210. CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock);
  1211. CAutoLock cSampleLock(&m_pRenderer->m_RendererLock);
  1212. HRESULT hr = m_pRenderer->EndFlush();
  1213. if (SUCCEEDED(hr)) {
  1214. hr = CBaseInputPin::EndFlush();
  1215. }
  1216. return hr;
  1217. }
  1218. // Pass the sample straight through to the renderer object
  1219. STDMETHODIMP CRendererInputPin::Receive(IMediaSample *pSample)
  1220. {
  1221. HRESULT hr = m_pRenderer->Receive(pSample);
  1222. if (FAILED(hr)) {
  1223. // A deadlock could occur if the caller holds the renderer lock and
  1224. // attempts to acquire the interface lock.
  1225. ASSERT(CritCheckOut(&m_pRenderer->m_RendererLock));
  1226. {
  1227. // The interface lock must be held when the filter is calling
  1228. // IsStopped() or IsFlushing(). The interface lock must also
  1229. // be held because the function uses m_bRunTimeError.
  1230. CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock);
  1231. // We do not report errors which occur while the filter is stopping,
  1232. // flushing or if the m_bAbort flag is set . Errors are expected to
  1233. // occur during these operations and the streaming thread correctly
  1234. // handles the errors.
  1235. if (!IsStopped() && !IsFlushing() && !m_pRenderer->m_bAbort && !m_bRunTimeError) {
  1236. // EC_ERRORABORT's first parameter is the error which caused
  1237. // the event and its' last parameter is 0. See the Direct
  1238. // Show SDK documentation for more information.
  1239. m_pRenderer->NotifyEvent(EC_ERRORABORT,hr,0);
  1240. {
  1241. CAutoLock alRendererLock(&m_pRenderer->m_RendererLock);
  1242. if (m_pRenderer->IsStreaming() && !m_pRenderer->IsEndOfStreamDelivered()) {
  1243. m_pRenderer->NotifyEndOfStream();
  1244. }
  1245. }
  1246. m_bRunTimeError = TRUE;
  1247. }
  1248. }
  1249. }
  1250. return hr;
  1251. }
  1252. // Called when the input pin is disconnected
  1253. HRESULT CRendererInputPin::BreakConnect()
  1254. {
  1255. HRESULT hr = m_pRenderer->BreakConnect();
  1256. if (FAILED(hr)) {
  1257. return hr;
  1258. }
  1259. return CBaseInputPin::BreakConnect();
  1260. }
  1261. // Called when the input pin is connected
  1262. HRESULT CRendererInputPin::CompleteConnect(IPin *pReceivePin)
  1263. {
  1264. HRESULT hr = m_pRenderer->CompleteConnect(pReceivePin);
  1265. if (FAILED(hr)) {
  1266. return hr;
  1267. }
  1268. return CBaseInputPin::CompleteConnect(pReceivePin);
  1269. }
  1270. // Give the pin id of our one and only pin
  1271. STDMETHODIMP CRendererInputPin::QueryId(LPWSTR *Id)
  1272. {
  1273. CheckPointer(Id,E_POINTER);
  1274. *Id = (LPWSTR)CoTaskMemAlloc(8);
  1275. if (*Id == NULL) {
  1276. return E_OUTOFMEMORY;
  1277. }
  1278. lstrcpyW(*Id, L"In");
  1279. return NOERROR;
  1280. }
  1281. // Will the filter accept this media type
  1282. HRESULT CRendererInputPin::CheckMediaType(const CMediaType *pmt)
  1283. {
  1284. return m_pRenderer->CheckMediaType(pmt);
  1285. }
  1286. // Called when we go paused or running
  1287. HRESULT CRendererInputPin::Active()
  1288. {
  1289. return m_pRenderer->Active();
  1290. }
  1291. // Called when we go into a stopped state
  1292. HRESULT CRendererInputPin::Inactive()
  1293. {
  1294. // The caller must hold the interface lock because
  1295. // this function uses m_bRunTimeError.
  1296. ASSERT(CritCheckIn(&m_pRenderer->m_InterfaceLock));
  1297. m_bRunTimeError = FALSE;
  1298. return m_pRenderer->Inactive();
  1299. }
  1300. // Tell derived classes about the media type agreed
  1301. HRESULT CRendererInputPin::SetMediaType(const CMediaType *pmt)
  1302. {
  1303. HRESULT hr = CBaseInputPin::SetMediaType(pmt);
  1304. if (FAILED(hr)) {
  1305. return hr;
  1306. }
  1307. return m_pRenderer->SetMediaType(pmt);
  1308. }
  1309. // We do not keep an event object to use when setting up a timer link with
  1310. // the clock but are given a pointer to one by the owning object through the
  1311. // SetNotificationObject method - this must be initialised before starting
  1312. // We can override the default quality management process to have it always
  1313. // draw late frames, this is currently done by having the following registry
  1314. // key (actually an INI key) called DrawLateFrames set to 1 (default is 0)
  1315. const TCHAR AMQUALITY[] = TEXT("ActiveMovie");
  1316. const TCHAR DRAWLATEFRAMES[] = TEXT("DrawLateFrames");
  1317. CBaseVideoRenderer::CBaseVideoRenderer(
  1318. REFCLSID RenderClass, // CLSID for this renderer
  1319. TCHAR *pName, // Debug ONLY description
  1320. LPUNKNOWN pUnk, // Aggregated owner object
  1321. HRESULT *phr) : // General OLE return code
  1322. CBaseRenderer(RenderClass,pName,pUnk,phr),
  1323. m_cFramesDropped(0),
  1324. m_cFramesDrawn(0),
  1325. m_bSupplierHandlingQuality(FALSE)
  1326. {
  1327. ResetStreamingTimes();
  1328. #ifdef PERF
  1329. m_idTimeStamp = MSR_REGISTER(TEXT("Frame time stamp"));
  1330. m_idEarliness = MSR_REGISTER(TEXT("Earliness fudge"));
  1331. m_idTarget = MSR_REGISTER(TEXT("Target (mSec)"));
  1332. m_idSchLateTime = MSR_REGISTER(TEXT("mSec late when scheduled"));
  1333. m_idDecision = MSR_REGISTER(TEXT("Scheduler decision code"));
  1334. m_idQualityRate = MSR_REGISTER(TEXT("Quality rate sent"));
  1335. m_idQualityTime = MSR_REGISTER(TEXT("Quality time sent"));
  1336. m_idWaitReal = MSR_REGISTER(TEXT("Render wait"));
  1337. // m_idWait = MSR_REGISTER(TEXT("wait time recorded (msec)"));
  1338. m_idFrameAccuracy = MSR_REGISTER(TEXT("Frame accuracy (msecs)"));
  1339. m_bDrawLateFrames = GetProfileInt(AMQUALITY, DRAWLATEFRAMES, FALSE);
  1340. //m_idSendQuality = MSR_REGISTER(TEXT("Processing Quality message"));
  1341. m_idRenderAvg = MSR_REGISTER(TEXT("Render draw time Avg"));
  1342. m_idFrameAvg = MSR_REGISTER(TEXT("FrameAvg"));
  1343. m_idWaitAvg = MSR_REGISTER(TEXT("WaitAvg"));
  1344. m_idDuration = MSR_REGISTER(TEXT("Duration"));
  1345. m_idThrottle = MSR_REGISTER(TEXT("Audio-video throttle wait"));
  1346. // m_idDebug = MSR_REGISTER(TEXT("Debug stuff"));
  1347. #endif // PERF
  1348. } // Constructor
  1349. // Destructor is just a placeholder
  1350. CBaseVideoRenderer::~CBaseVideoRenderer()
  1351. {
  1352. ASSERT(m_dwAdvise == 0);
  1353. }
  1354. // The timing functions in this class are called by the window object and by
  1355. // the renderer's allocator.
  1356. // The windows object calls timing functions as it receives media sample
  1357. // images for drawing using GDI.
  1358. // The allocator calls timing functions when it starts passing DCI/DirectDraw
  1359. // surfaces which are not rendered in the same way; The decompressor writes
  1360. // directly to the surface with no separate rendering, so those code paths
  1361. // call direct into us. Since we only ever hand out DCI/DirectDraw surfaces
  1362. // when we have allocated one and only one image we know there cannot be any
  1363. // conflict between the two.
  1364. //
  1365. // We use timeGetTime to return the timing counts we use (since it's relative
  1366. // performance we are interested in rather than absolute compared to a clock)
  1367. // The window object sets the accuracy of the system clock (normally 1ms) by
  1368. // calling timeBeginPeriod/timeEndPeriod when it changes streaming states
  1369. // Reset all times controlling streaming.
  1370. // Set them so that
  1371. // 1. Frames will not initially be dropped
  1372. // 2. The first frame will definitely be drawn (achieved by saying that there
  1373. // has not ben a frame drawn for a long time).
  1374. HRESULT CBaseVideoRenderer::ResetStreamingTimes()
  1375. {
  1376. m_trLastDraw = -1000; // set up as first frame since ages (1 sec) ago
  1377. m_tStreamingStart = timeGetTime();
  1378. m_trRenderAvg = 0;
  1379. m_trFrameAvg = -1; // -1000 fps == "unset"
  1380. m_trDuration = 0; // 0 - strange value
  1381. m_trRenderLast = 0;
  1382. m_trWaitAvg = 0;
  1383. m_tRenderStart = 0;
  1384. m_cFramesDrawn = 0;
  1385. m_cFramesDropped = 0;
  1386. m_iTotAcc = 0;
  1387. m_iSumSqAcc = 0;
  1388. m_iSumSqFrameTime = 0;
  1389. m_trFrame = 0; // hygeine - not really needed
  1390. m_trLate = 0; // hygeine - not really needed
  1391. m_iSumFrameTime = 0;
  1392. m_nNormal = 0;
  1393. m_trEarliness = 0;
  1394. m_trTarget = -300000; // 30mSec early
  1395. m_trThrottle = 0;
  1396. m_trRememberStampForPerf = 0;
  1397. #ifdef PERF
  1398. m_trRememberFrameForPerf = 0;
  1399. #endif
  1400. return NOERROR;
  1401. } // ResetStreamingTimes
  1402. // Reset all times controlling streaming. Note that we're now streaming. We
  1403. // don't need to set the rendering event to have the source filter released
  1404. // as it is done during the Run processing. When we are run we immediately
  1405. // release the source filter thread and draw any image waiting (that image
  1406. // may already have been drawn once as a poster frame while we were paused)
  1407. HRESULT CBaseVideoRenderer::OnStartStreaming()
  1408. {
  1409. ResetStreamingTimes();
  1410. return NOERROR;
  1411. } // OnStartStreaming
  1412. // Called at end of streaming. Fixes times for property page report
  1413. HRESULT CBaseVideoRenderer::OnStopStreaming()
  1414. {
  1415. m_tStreamingStart = timeGetTime()-m_tStreamingStart;
  1416. return NOERROR;
  1417. } // OnStopStreaming
  1418. // Called when we start waiting for a rendering event.
  1419. // Used to update times spent waiting and not waiting.
  1420. void CBaseVideoRenderer::OnWaitStart()
  1421. {
  1422. MSR_START(m_idWaitReal);
  1423. } // OnWaitStart
  1424. // Called when we are awoken from the wait in the window OR by our allocator
  1425. // when it is hanging around until the next sample is due for rendering on a
  1426. // DCI/DirectDraw surface. We add the wait time into our rolling average.
  1427. // We grab the interface lock so that we're serialised with the application
  1428. // thread going through the run code - which in due course ends up calling
  1429. // ResetStreaming times - possibly as we run through this section of code
  1430. void CBaseVideoRenderer::OnWaitEnd()
  1431. {
  1432. #ifdef PERF
  1433. MSR_STOP(m_idWaitReal);
  1434. // for a perf build we want to know just exactly how late we REALLY are.
  1435. // even if this means that we have to look at the clock again.
  1436. REFERENCE_TIME trRealStream; // the real time now expressed as stream time.
  1437. #if 0
  1438. m_pClock->GetTime(&trRealStream); // Calling clock here causes W95 deadlock!
  1439. #else
  1440. // We will be discarding overflows like mad here!
  1441. // This is wrong really because timeGetTime() can wrap but it's
  1442. // only for PERF
  1443. REFERENCE_TIME tr = timeGetTime()*10000;
  1444. trRealStream = tr + m_llTimeOffset;
  1445. #endif
  1446. trRealStream -= m_tStart; // convert to stream time (this is a reftime)
  1447. if (m_trRememberStampForPerf==0) {
  1448. // This is probably the poster frame at the start, and it is not scheduled
  1449. // in the usual way at all. Just count it. The rememberstamp gets set
  1450. // in ShouldDrawSampleNow, so this does invalid frame recording until we
  1451. // actually start playing.
  1452. PreparePerformanceData(0, 0);
  1453. } else {
  1454. int trLate = (int)(trRealStream - m_trRememberStampForPerf);
  1455. int trFrame = (int)(tr - m_trRememberFrameForPerf);
  1456. PreparePerformanceData(trLate, trFrame);
  1457. }
  1458. m_trRememberFrameForPerf = tr;
  1459. #endif //PERF
  1460. } // OnWaitEnd
  1461. // Put data on one side that describes the lateness of the current frame.
  1462. // We don't yet know whether it will actually be drawn. In direct draw mode,
  1463. // this decision is up to the filter upstream, and it could change its mind.
  1464. // The rules say that if it did draw it must call Receive(). One way or
  1465. // another we eventually get into either OnRenderStart or OnDirectRender and
  1466. // these both call RecordFrameLateness to update the statistics.
  1467. void CBaseVideoRenderer::PreparePerformanceData(int trLate, int trFrame)
  1468. {
  1469. m_trLate = trLate;
  1470. m_trFrame = trFrame;
  1471. } // PreparePerformanceData
  1472. // update the statistics:
  1473. // m_iTotAcc, m_iSumSqAcc, m_iSumSqFrameTime, m_iSumFrameTime, m_cFramesDrawn
  1474. // Note that because the properties page reports using these variables,
  1475. // 1. We need to be inside a critical section
  1476. // 2. They must all be updated together. Updating the sums here and the count
  1477. // elsewhere can result in imaginary jitter (i.e. attempts to find square roots
  1478. // of negative numbers) in the property page code.
  1479. void CBaseVideoRenderer::RecordFrameLateness(int trLate, int trFrame)
  1480. {
  1481. // Record how timely we are.
  1482. int tLate = trLate/10000;
  1483. // Best estimate of moment of appearing on the screen is average of
  1484. // start and end draw times. Here we have only the end time. This may
  1485. // tend to show us as spuriously late by up to 1/2 frame rate achieved.
  1486. // Decoder probably monitors draw time. We don't bother.
  1487. MSR_INTEGER( m_idFrameAccuracy, tLate );
  1488. // This is a kludge - we can get frames that are very late
  1489. // especially (at start-up) and they invalidate the statistics.
  1490. // So ignore things that are more than 1 sec off.
  1491. if (tLate>1000 || tLate<-1000) {
  1492. if (m_cFramesDrawn<=1) {
  1493. tLate = 0;
  1494. } else if (tLate>0) {
  1495. tLate = 1000;
  1496. } else {
  1497. tLate = -1000;
  1498. }
  1499. }
  1500. // The very first frame often has a invalid time, so don't
  1501. // count it into the statistics. (???)
  1502. if (m_cFramesDrawn>1) {
  1503. m_iTotAcc += tLate;
  1504. m_iSumSqAcc += (tLate*tLate);
  1505. }
  1506. // calculate inter-frame time. Doesn't make sense for first frame
  1507. // second frame suffers from invalid first frame stamp.
  1508. if (m_cFramesDrawn>2) {
  1509. int tFrame = trFrame/10000; // convert to mSec else it overflows
  1510. // This is a kludge. It can overflow anyway (a pause can cause
  1511. // a very long inter-frame time) and it overflows at 2**31/10**7
  1512. // or about 215 seconds i.e. 3min 35sec
  1513. if (tFrame>1000||tFrame<0) tFrame = 1000;
  1514. m_iSumSqFrameTime += tFrame*tFrame;
  1515. ASSERT(m_iSumSqFrameTime>=0);
  1516. m_iSumFrameTime += tFrame;
  1517. }
  1518. ++m_cFramesDrawn;
  1519. } // RecordFrameLateness
  1520. void CBaseVideoRenderer::ThrottleWait()
  1521. {
  1522. if (m_trThrottle>0) {
  1523. int iThrottle = m_trThrottle/10000; // convert to mSec
  1524. MSR_INTEGER( m_idThrottle, iThrottle);
  1525. DbgLog((LOG_TRACE, 0, TEXT("Throttle %d ms"), iThrottle));
  1526. Sleep(iThrottle);
  1527. } else {
  1528. Sleep(0);
  1529. }
  1530. } // ThrottleWait
  1531. // Whenever a frame is rendered it goes though either OnRenderStart
  1532. // or OnDirectRender. Data that are generated during ShouldDrawSample
  1533. // are added to the statistics by calling RecordFrameLateness from both
  1534. // these two places.
  1535. // Called in place of OnRenderStart..OnRenderEnd
  1536. // When a DirectDraw image is drawn
  1537. void CBaseVideoRenderer::OnDirectRender(IMediaSample *pMediaSample)
  1538. {
  1539. int time = 0;
  1540. m_trRenderAvg = 0;
  1541. m_trRenderLast = 5000000; // If we mode switch, we do NOT want this
  1542. // to inhibit the new average getting going!
  1543. // so we set it to half a second
  1544. // MSR_INTEGER(m_idRenderAvg, m_trRenderAvg/10000);
  1545. RecordFrameLateness(m_trLate, m_trFrame);
  1546. ThrottleWait();
  1547. } // OnDirectRender
  1548. // Called just before we start drawing. All we do is to get the current clock
  1549. // time (from the system) and return. We have to store the start render time
  1550. // in a member variable because it isn't used until we complete the drawing
  1551. // The rest is just performance logging.
  1552. void CBaseVideoRenderer::OnRenderStart(IMediaSample *pMediaSample)
  1553. {
  1554. RecordFrameLateness(m_trLate, m_trFrame);
  1555. m_tRenderStart = timeGetTime();
  1556. } // OnRenderStart
  1557. // Called directly after drawing an image. We calculate the time spent in the
  1558. // drawing code and if this doesn't appear to have any odd looking spikes in
  1559. // it then we add it to the current average draw time. Measurement spikes may
  1560. // occur if the drawing thread is interrupted and switched to somewhere else.
  1561. void CBaseVideoRenderer::OnRenderEnd(IMediaSample *pMediaSample)
  1562. {
  1563. // The renderer time can vary erratically if we are interrupted so we do
  1564. // some smoothing to help get more sensible figures out but even that is
  1565. // not enough as figures can go 9,10,9,9,83,9 and we must disregard 83
  1566. int tr = (timeGetTime() - m_tRenderStart)*10000; // convert mSec->UNITS
  1567. if (tr < m_trRenderAvg*2 || tr < 2 * m_trRenderLast) {
  1568. // DO_MOVING_AVG(m_trRenderAvg, tr);
  1569. m_trRenderAvg = (tr + (AVGPERIOD-1)*m_trRenderAvg)/AVGPERIOD;
  1570. }
  1571. m_trRenderLast = tr;
  1572. ThrottleWait();
  1573. } // OnRenderEnd
  1574. STDMETHODIMP CBaseVideoRenderer::SetSink( IQualityControl * piqc)
  1575. {
  1576. m_pQSink = piqc;
  1577. return NOERROR;
  1578. } // SetSink
  1579. STDMETHODIMP CBaseVideoRenderer::Notify( IBaseFilter * pSelf, Quality q)
  1580. {
  1581. // NOTE: We are NOT getting any locks here. We could be called
  1582. // asynchronously and possibly even on a time critical thread of
  1583. // someone else's - so we do the minumum. We only set one state
  1584. // variable (an integer) and if that happens to be in the middle
  1585. // of another thread reading it they will just get either the new
  1586. // or the old value. Locking would achieve no more than this.
  1587. // It might be nice to check that we are being called from m_pGraph, but
  1588. // it turns out to be a millisecond or so per throw!
  1589. // This is heuristics, these numbers are aimed at being "what works"
  1590. // rather than anything based on some theory.
  1591. // We use a hyperbola because it's easy to calculate and it includes
  1592. // a panic button asymptote (which we push off just to the left)
  1593. // The throttling fits the following table (roughly)
  1594. // Proportion Throttle (msec)
  1595. // >=1000 0
  1596. // 900 3
  1597. // 800 7
  1598. // 700 11
  1599. // 600 17
  1600. // 500 25
  1601. // 400 35
  1602. // 300 50
  1603. // 200 72
  1604. // 125 100
  1605. // 100 112
  1606. // 50 146
  1607. // 0 200
  1608. // (some evidence that we could go for a sharper kink - e.g. no throttling
  1609. // until below the 750 mark - might give fractionally more frames on a
  1610. // P60-ish machine). The easy way to get these coefficients is to use
  1611. // Renbase.xls follow the instructions therein using excel solver.
  1612. if (q.Proportion>=1000) { m_trThrottle = 0; }
  1613. else {
  1614. // The DWORD is to make quite sure I get unsigned arithmetic
  1615. // as the constant is between 2**31 and 2**32
  1616. m_trThrottle = -330000 + (388880000/(q.Proportion+167));
  1617. }
  1618. return NOERROR;
  1619. } // Notify
  1620. // Send a message to indicate what our supplier should do about quality.
  1621. // Theory:
  1622. // What a supplier wants to know is "is the frame I'm working on NOW
  1623. // going to be late?".
  1624. // F1 is the frame at the supplier (as above)
  1625. // Tf1 is the due time for F1
  1626. // T1 is the time at that point (NOW!)
  1627. // Tr1 is the time that f1 WILL actually be rendered
  1628. // L1 is the latency of the graph for frame F1 = Tr1-T1
  1629. // D1 (for delay) is how late F1 will be beyond its due time i.e.
  1630. // D1 = (Tr1-Tf1) which is what the supplier really wants to know.
  1631. // Unfortunately Tr1 is in the future and is unknown, so is L1
  1632. //
  1633. // We could estimate L1 by its value for a previous frame,
  1634. // L0 = Tr0-T0 and work off
  1635. // D1' = ((T1+L0)-Tf1) = (T1 + (Tr0-T0) -Tf1)
  1636. // Rearranging terms:
  1637. // D1' = (T1-T0) + (Tr0-Tf1)
  1638. // adding (Tf0-Tf0) and rearranging again:
  1639. // = (T1-T0) + (Tr0-Tf0) + (Tf0-Tf1)
  1640. // = (T1-T0) - (Tf1-Tf0) + (Tr0-Tf0)
  1641. // But (Tr0-Tf0) is just D0 - how late frame zero was, and this is the
  1642. // Late field in the quality message that we send.
  1643. // The other two terms just state what correction should be applied before
  1644. // using the lateness of F0 to predict the lateness of F1.
  1645. // (T1-T0) says how much time has actually passed (we have lost this much)
  1646. // (Tf1-Tf0) says how much time should have passed if we were keeping pace
  1647. // (we have gained this much).
  1648. //
  1649. // Suppliers should therefore work off:
  1650. // Quality.Late + (T1-T0) - (Tf1-Tf0)
  1651. // and see if this is "acceptably late" or even early (i.e. negative).
  1652. // They get T1 and T0 by polling the clock, they get Tf1 and Tf0 from
  1653. // the time stamps in the frames. They get Quality.Late from us.
  1654. //
  1655. HRESULT CBaseVideoRenderer::SendQuality(REFERENCE_TIME trLate,
  1656. REFERENCE_TIME trRealStream)
  1657. {
  1658. Quality q;
  1659. HRESULT hr;
  1660. // If we are the main user of time, then report this as Flood/Dry.
  1661. // If our suppliers are, then report it as Famine/Glut.
  1662. //
  1663. // We need to take action, but avoid hunting. Hunting is caused by
  1664. // 1. Taking too much action too soon and overshooting
  1665. // 2. Taking too long to react (so averaging can CAUSE hunting).
  1666. //
  1667. // The reason why we use trLate as well as Wait is to reduce hunting;
  1668. // if the wait time is coming down and about to go into the red, we do
  1669. // NOT want to rely on some average which is only telling is that it used
  1670. // to be OK once.
  1671. q.TimeStamp = (REFERENCE_TIME)trRealStream;
  1672. if (m_trFrameAvg<0) {
  1673. q.Type = Famine; // guess
  1674. }
  1675. // Is the greater part of the time taken bltting or something else
  1676. else if (m_trFrameAvg > 2*m_trRenderAvg) {
  1677. q.Type = Famine; // mainly other
  1678. } else {
  1679. q.Type = Flood; // mainly bltting
  1680. }
  1681. q.Proportion = 1000; // default
  1682. if (m_trFrameAvg<0) {
  1683. // leave it alone - we don't know enough
  1684. }
  1685. else if ( trLate> 0 ) {
  1686. // try to catch up over the next second
  1687. // We could be Really, REALLY late, but rendering all the frames
  1688. // anyway, just because it's so cheap.
  1689. q.Proportion = 1000 - (int)((trLate)/(UNITS/1000));
  1690. if (q.Proportion<500) {
  1691. q.Proportion = 500; // don't go daft. (could've been negative!)
  1692. } else {
  1693. }
  1694. } else if ( m_trWaitAvg>20000
  1695. && trLate<-20000
  1696. ){
  1697. // Go cautiously faster - aim at 2mSec wait.
  1698. if (m_trWaitAvg>=m_trFrameAvg) {
  1699. // This can happen because of some fudges.
  1700. // The waitAvg is how long we originally planned to wait
  1701. // The frameAvg is more honest.
  1702. // It means that we are spending a LOT of time waiting
  1703. q.Proportion = 2000; // double.
  1704. } else {
  1705. if (m_trFrameAvg+20000 > m_trWaitAvg) {
  1706. q.Proportion
  1707. = 1000 * (m_trFrameAvg / (m_trFrameAvg + 20000 - m_trWaitAvg));
  1708. } else {
  1709. // We're apparently spending more than the whole frame time waiting.
  1710. // Assume that the averages are slightly out of kilter, but that we
  1711. // are indeed doing a lot of waiting. (This leg probably never
  1712. // happens, but the code avoids any potential divide by zero).
  1713. q.Proportion = 2000;
  1714. }
  1715. }
  1716. if (q.Proportion>2000) {
  1717. q.Proportion = 2000; // don't go crazy.
  1718. }
  1719. }
  1720. // Tell the supplier how late frames are when they get rendered
  1721. // That's how late we are now.
  1722. // If we are in directdraw mode then the guy upstream can see the drawing
  1723. // times and we'll just report on the start time. He can figure out any
  1724. // offset to apply. If we are in DIB Section mode then we will apply an
  1725. // extra offset which is half of our drawing time. This is usually small
  1726. // but can sometimes be the dominant effect. For this we will use the
  1727. // average drawing time rather than the last frame. If the last frame took
  1728. // a long time to draw and made us late, that's already in the lateness
  1729. // figure. We should not add it in again unless we expect the next frame
  1730. // to be the same. We don't, we expect the average to be a better shot.
  1731. // In direct draw mode the RenderAvg will be zero.
  1732. q.Late = trLate + m_trRenderAvg/2;
  1733. // log what we're doing
  1734. MSR_INTEGER(m_idQualityRate, q.Proportion);
  1735. MSR_INTEGER( m_idQualityTime, (int)q.Late / 10000 );
  1736. // A specific sink interface may be set through IPin
  1737. if (m_pQSink==NULL) {
  1738. // Get our input pin's peer. We send quality management messages
  1739. // to any nominated receiver of these things (set in the IPin
  1740. // interface), or else to our source filter.
  1741. IQualityControl *pQC = NULL;
  1742. IPin *pOutputPin = m_pInputPin->GetConnected();
  1743. ASSERT(pOutputPin != NULL);
  1744. // And get an AddRef'd quality control interface
  1745. hr = pOutputPin->QueryInterface(IID_IQualityControl,(void**) &pQC);
  1746. if (SUCCEEDED(hr)) {
  1747. m_pQSink = pQC;
  1748. }
  1749. }
  1750. if (m_pQSink) {
  1751. return m_pQSink->Notify(this,q);
  1752. }
  1753. return S_FALSE;
  1754. } // SendQuality
  1755. // We are called with a valid IMediaSample image to decide whether this is to
  1756. // be drawn or not. There must be a reference clock in operation.
  1757. // Return S_OK if it is to be drawn Now (as soon as possible)
  1758. // Return S_FALSE if it is to be drawn when it's due
  1759. // Return an error if we want to drop it
  1760. // m_nNormal=-1 indicates that we dropped the previous frame and so this
  1761. // one should be drawn early. Respect it and update it.
  1762. // Use current stream time plus a number of heuristics (detailed below)
  1763. // to make the decision
  1764. HRESULT CBaseVideoRenderer::ShouldDrawSampleNow(IMediaSample *pMediaSample,
  1765. REFERENCE_TIME *ptrStart,
  1766. REFERENCE_TIME *ptrEnd)
  1767. {
  1768. // Don't call us unless there's a clock interface to synchronise with
  1769. ASSERT(m_pClock);
  1770. MSR_INTEGER(m_idTimeStamp, (int)((*ptrStart)>>32)); // high order 32 bits
  1771. MSR_INTEGER(m_idTimeStamp, (int)(*ptrStart)); // low order 32 bits
  1772. // We lose a bit of time depending on the monitor type waiting for the next
  1773. // screen refresh. On average this might be about 8mSec - so it will be
  1774. // later than we think when the picture appears. To compensate a bit
  1775. // we bias the media samples by -8mSec i.e. 80000 UNITs.
  1776. // We don't ever make a stream time negative (call it paranoia)
  1777. if (*ptrStart>=80000) {
  1778. *ptrStart -= 80000;
  1779. *ptrEnd -= 80000; // bias stop to to retain valid frame duration
  1780. }
  1781. // Cache the time stamp now. We will want to compare what we did with what
  1782. // we started with (after making the monitor allowance).
  1783. m_trRememberStampForPerf = *ptrStart;
  1784. // Get reference times (current and late)
  1785. REFERENCE_TIME trRealStream; // the real time now expressed as stream time.
  1786. m_pClock->GetTime(&trRealStream);
  1787. #ifdef PERF
  1788. // While the reference clock is expensive:
  1789. // Remember the offset from timeGetTime and use that.
  1790. // This overflows all over the place, but when we subtract to get
  1791. // differences the overflows all cancel out.
  1792. m_llTimeOffset = trRealStream-timeGetTime()*10000;
  1793. #endif
  1794. trRealStream -= m_tStart; // convert to stream time (this is a reftime)
  1795. // We have to wory about two versions of "lateness". The truth, which we
  1796. // try to work out here and the one measured against m_trTarget which
  1797. // includes long term feedback. We report statistics against the truth
  1798. // but for operational decisions we work to the target.
  1799. // We use TimeDiff to make sure we get an integer because we
  1800. // may actually be late (or more likely early if there is a big time
  1801. // gap) by a very long time.
  1802. const int trTrueLate = TimeDiff(trRealStream - *ptrStart);
  1803. const int trLate = trTrueLate;
  1804. MSR_INTEGER(m_idSchLateTime, trTrueLate/10000);
  1805. // Send quality control messages upstream, measured against target
  1806. HRESULT hr = SendQuality(trLate, trRealStream);
  1807. // Note: the filter upstream is allowed to this FAIL meaning "you do it".
  1808. m_bSupplierHandlingQuality = (hr==S_OK);
  1809. // Decision time! Do we drop, draw when ready or draw immediately?
  1810. const int trDuration = (int)(*ptrEnd - *ptrStart);
  1811. {
  1812. // We need to see if the frame rate of the file has just changed.
  1813. // This would make comparing our previous frame rate with the current
  1814. // frame rate inefficent. Hang on a moment though. I've seen files
  1815. // where the frames vary between 33 and 34 mSec so as to average
  1816. // 30fps. A minor variation like that won't hurt us.
  1817. int t = m_trDuration/32;
  1818. if ( trDuration > m_trDuration+t
  1819. || trDuration < m_trDuration-t
  1820. ) {
  1821. // There's a major variation. Reset the average frame rate to
  1822. // exactly the current rate to disable decision 9002 for this frame,
  1823. // and remember the new rate.
  1824. m_trFrameAvg = trDuration;
  1825. m_trDuration = trDuration;
  1826. }
  1827. }
  1828. MSR_INTEGER(m_idEarliness, m_trEarliness/10000);
  1829. MSR_INTEGER(m_idRenderAvg, m_trRenderAvg/10000);
  1830. MSR_INTEGER(m_idFrameAvg, m_trFrameAvg/10000);
  1831. MSR_INTEGER(m_idWaitAvg, m_trWaitAvg/10000);
  1832. MSR_INTEGER(m_idDuration, trDuration/10000);
  1833. #ifdef PERF
  1834. if (S_OK==pMediaSample->IsDiscontinuity()) {
  1835. MSR_INTEGER(m_idDecision, 9000);
  1836. }
  1837. #endif
  1838. // Control the graceful slide back from slow to fast machine mode.
  1839. // After a frame drop accept an early frame and set the earliness to here
  1840. // If this frame is already later than the earliness then slide it to here
  1841. // otherwise do the standard slide (reduce by about 12% per frame).
  1842. // Note: earliness is normally NEGATIVE
  1843. BOOL bJustDroppedFrame
  1844. = ( m_bSupplierHandlingQuality
  1845. // Can't use the pin sample properties because we might
  1846. // not be in Receive when we call this
  1847. && (S_OK == pMediaSample->IsDiscontinuity()) // he just dropped one
  1848. )
  1849. || (m_nNormal==-1); // we just dropped one
  1850. // Set m_trEarliness (slide back from slow to fast machine mode)
  1851. if (trLate>0) {
  1852. m_trEarliness = 0; // we are no longer in fast machine mode at all!
  1853. } else if ( (trLate>=m_trEarliness) || bJustDroppedFrame) {
  1854. m_trEarliness = trLate; // Things have slipped of their own accord
  1855. } else {
  1856. m_trEarliness = m_trEarliness - m_trEarliness/8; // graceful slide
  1857. }
  1858. // prepare the new wait average - but don't pollute the old one until
  1859. // we have finished with it.
  1860. int trWaitAvg;
  1861. {
  1862. // We never mix in a negative wait. This causes us to believe in fast machines
  1863. // slightly more.
  1864. int trL = trLate<0 ? -trLate : 0;
  1865. trWaitAvg = (trL + m_trWaitAvg*(AVGPERIOD-1))/AVGPERIOD;
  1866. }
  1867. int trFrame;
  1868. {
  1869. REFERENCE_TIME tr = trRealStream - m_trLastDraw; // Cd be large - 4 min pause!
  1870. if (tr>10000000) {
  1871. tr = 10000000; // 1 second - arbitrarily.
  1872. }
  1873. trFrame = int(tr);
  1874. }
  1875. // We will DRAW this frame IF...
  1876. if (
  1877. // ...the time we are spending drawing is a small fraction of the total
  1878. // observed inter-frame time so that dropping it won't help much.
  1879. (3*m_trRenderAvg <= m_trFrameAvg)
  1880. // ...or our supplier is NOT handling things and the next frame would
  1881. // be less timely than this one or our supplier CLAIMS to be handling
  1882. // things, and is now less than a full FOUR frames late.
  1883. || ( m_bSupplierHandlingQuality
  1884. ? (trLate <= trDuration*4)
  1885. : (trLate+trLate < trDuration)
  1886. )
  1887. // ...or we are on average waiting for over eight milliseconds then
  1888. // this may be just a glitch. Draw it and we'll hope to catch up.
  1889. || (m_trWaitAvg > 80000)
  1890. // ...or we haven't drawn an image for over a second. We will update
  1891. // the display, which stops the video looking hung.
  1892. // Do this regardless of how late this media sample is.
  1893. || ((trRealStream - m_trLastDraw) > UNITS)
  1894. ) {
  1895. HRESULT Result;
  1896. // We are going to play this frame. We may want to play it early.
  1897. // We will play it early if we think we are in slow machine mode.
  1898. // If we think we are NOT in slow machine mode, we will still play
  1899. // it early by m_trEarliness as this controls the graceful slide back.
  1900. // and in addition we aim at being m_trTarget late rather than "on time".
  1901. BOOL bPlayASAP = FALSE;
  1902. // we will play it AT ONCE (slow machine mode) if...
  1903. // ...we are playing catch-up
  1904. if ( bJustDroppedFrame) {
  1905. bPlayASAP = TRUE;
  1906. MSR_INTEGER(m_idDecision, 9001);
  1907. }
  1908. // ...or if we are running below the true frame rate
  1909. // exact comparisons are glitchy, for these measurements,
  1910. // so add an extra 5% or so
  1911. else if ( (m_trFrameAvg > trDuration + trDuration/16)
  1912. // It's possible to get into a state where we are losing ground, but
  1913. // are a very long way ahead. To avoid this or recover from it
  1914. // we refuse to play early by more than 10 frames.
  1915. && (trLate > - trDuration*10)
  1916. ){
  1917. bPlayASAP = TRUE;
  1918. MSR_INTEGER(m_idDecision, 9002);
  1919. }
  1920. #if 0
  1921. // ...or if we have been late and are less than one frame early
  1922. else if ( (trLate + trDuration > 0)
  1923. && (m_trWaitAvg<=20000)
  1924. ) {
  1925. bPlayASAP = TRUE;
  1926. MSR_INTEGER(m_idDecision, 9003);
  1927. }
  1928. #endif
  1929. // We will NOT play it at once if we are grossly early. On very slow frame
  1930. // rate movies - e.g. clock.avi - it is not a good idea to leap ahead just
  1931. // because we got starved (for instance by the net) and dropped one frame
  1932. // some time or other. If we are more than 900mSec early, then wait.
  1933. if (trLate<-9000000) {
  1934. bPlayASAP = FALSE;
  1935. }
  1936. if (bPlayASAP) {
  1937. m_nNormal = 0;
  1938. MSR_INTEGER(m_idDecision, 0);
  1939. // When we are here, we are in slow-machine mode. trLate may well
  1940. // oscillate between negative and positive when the supplier is
  1941. // dropping frames to keep sync. We should not let that mislead
  1942. // us into thinking that we have as much as zero spare time!
  1943. // We just update with a zero wait.
  1944. m_trWaitAvg = (m_trWaitAvg*(AVGPERIOD-1))/AVGPERIOD;
  1945. // Assume that we draw it immediately. Update inter-frame stats
  1946. m_trFrameAvg = (trFrame + m_trFrameAvg*(AVGPERIOD-1))/AVGPERIOD;
  1947. #ifndef PERF
  1948. // If this is NOT a perf build, then report what we know so far
  1949. // without looking at the clock any more. This assumes that we
  1950. // actually wait for exactly the time we hope to. It also reports
  1951. // how close we get to the manipulated time stamps that we now have
  1952. // rather than the ones we originally started with. It will
  1953. // therefore be a little optimistic. However it's fast.
  1954. PreparePerformanceData(trTrueLate, trFrame);
  1955. #endif
  1956. m_trLastDraw = trRealStream;
  1957. if (m_trEarliness > trLate) {
  1958. m_trEarliness = trLate; // if we are actually early, this is neg
  1959. }
  1960. Result = S_OK; // Draw it now
  1961. } else {
  1962. ++m_nNormal;
  1963. // Set the average frame rate to EXACTLY the ideal rate.
  1964. // If we are exiting slow-machine mode then we will have caught up
  1965. // and be running ahead, so as we slide back to exact timing we will
  1966. // have a longer than usual gap at this point. If we record this
  1967. // real gap then we'll think that we're running slow and go back
  1968. // into slow-machine mode and vever get it straight.
  1969. m_trFrameAvg = trDuration;
  1970. MSR_INTEGER(m_idDecision, 1);
  1971. // Play it early by m_trEarliness and by m_trTarget
  1972. {
  1973. int trE = m_trEarliness;
  1974. if (trE < -m_trFrameAvg) {
  1975. trE = -m_trFrameAvg;
  1976. }
  1977. *ptrStart += trE; // N.B. earliness is negative
  1978. }
  1979. int Delay = -trTrueLate;
  1980. Result = Delay<=0 ? S_OK : S_FALSE; // OK = draw now, FALSE = wait
  1981. m_trWaitAvg = trWaitAvg;
  1982. // Predict when it will actually be drawn and update frame stats
  1983. if (Result==S_FALSE) { // We are going to wait
  1984. trFrame = TimeDiff(*ptrStart-m_trLastDraw);
  1985. m_trLastDraw = *ptrStart;
  1986. } else {
  1987. // trFrame is already = trRealStream-m_trLastDraw;
  1988. m_trLastDraw = trRealStream;
  1989. }
  1990. #ifndef PERF
  1991. int iAccuracy;
  1992. if (Delay>0) {
  1993. // Report lateness based on when we intend to play it
  1994. iAccuracy = TimeDiff(*ptrStart-m_trRememberStampForPerf);
  1995. } else {
  1996. // Report lateness based on playing it *now*.
  1997. iAccuracy = trTrueLate; // trRealStream-RememberStampForPerf;
  1998. }
  1999. PreparePerformanceData(iAccuracy, trFrame);
  2000. #endif
  2001. }
  2002. return Result;
  2003. }
  2004. // We are going to drop this frame!
  2005. // Of course in DirectDraw mode the guy upstream may draw it anyway.
  2006. // This will probably give a large negative wack to the wait avg.
  2007. m_trWaitAvg = trWaitAvg;
  2008. #ifdef PERF
  2009. // Respect registry setting - debug only!
  2010. if (m_bDrawLateFrames) {
  2011. return S_OK; // draw it when it's ready
  2012. } // even though it's late.
  2013. #endif
  2014. // We are going to drop this frame so draw the next one early
  2015. // n.b. if the supplier is doing direct draw then he may draw it anyway
  2016. // but he's doing something funny to arrive here in that case.
  2017. MSR_INTEGER(m_idDecision, 2);
  2018. m_nNormal = -1;
  2019. return E_FAIL; // drop it
  2020. } // ShouldDrawSampleNow
  2021. // NOTE we're called by both the window thread and the source filter thread
  2022. // so we have to be protected by a critical section (locked before called)
  2023. // Also, when the window thread gets signalled to render an image, it always
  2024. // does so regardless of how late it is. All the degradation is done when we
  2025. // are scheduling the next sample to be drawn. Hence when we start an advise
  2026. // link to draw a sample, that sample's time will always become the last one
  2027. // drawn - unless of course we stop streaming in which case we cancel links
  2028. BOOL CBaseVideoRenderer::ScheduleSample(IMediaSample *pMediaSample)
  2029. {
  2030. // We override ShouldDrawSampleNow to add quality management
  2031. BOOL bDrawImage = CBaseRenderer::ScheduleSample(pMediaSample);
  2032. if (bDrawImage == FALSE) {
  2033. ++m_cFramesDropped;
  2034. return FALSE;
  2035. }
  2036. // m_cFramesDrawn must NOT be updated here. It has to be updated
  2037. // in RecordFrameLateness at the same time as the other statistics.
  2038. return TRUE;
  2039. }
  2040. // Implementation of IQualProp interface needed to support the property page
  2041. // This is how the property page gets the data out of the scheduler. We are
  2042. // passed into the constructor the owning object in the COM sense, this will
  2043. // either be the video renderer or an external IUnknown if we're aggregated.
  2044. // We initialise our CUnknown base class with this interface pointer. Then
  2045. // all we have to do is to override NonDelegatingQueryInterface to expose
  2046. // our IQualProp interface. The AddRef and Release are handled automatically
  2047. // by the base class and will be passed on to the appropriate outer object
  2048. STDMETHODIMP CBaseVideoRenderer::get_FramesDroppedInRenderer(int *pcFramesDropped)
  2049. {
  2050. CheckPointer(pcFramesDropped,E_POINTER);
  2051. CAutoLock cVideoLock(&m_InterfaceLock);
  2052. *pcFramesDropped = m_cFramesDropped;
  2053. return NOERROR;
  2054. } // get_FramesDroppedInRenderer
  2055. // Set *pcFramesDrawn to the number of frames drawn since
  2056. // streaming started.
  2057. STDMETHODIMP CBaseVideoRenderer::get_FramesDrawn( int *pcFramesDrawn)
  2058. {
  2059. CheckPointer(pcFramesDrawn,E_POINTER);
  2060. CAutoLock cVideoLock(&m_InterfaceLock);
  2061. *pcFramesDrawn = m_cFramesDrawn;
  2062. return NOERROR;
  2063. } // get_FramesDrawn
  2064. // Set iAvgFrameRate to the frames per hundred secs since
  2065. // streaming started. 0 otherwise.
  2066. STDMETHODIMP CBaseVideoRenderer::get_AvgFrameRate( int *piAvgFrameRate)
  2067. {
  2068. CheckPointer(piAvgFrameRate,E_POINTER);
  2069. CAutoLock cVideoLock(&m_InterfaceLock);
  2070. int t;
  2071. if (m_bStreaming) {
  2072. t = timeGetTime()-m_tStreamingStart;
  2073. } else {
  2074. t = m_tStreamingStart;
  2075. }
  2076. if (t<=0) {
  2077. *piAvgFrameRate = 0;
  2078. ASSERT(m_cFramesDrawn == 0);
  2079. } else {
  2080. // i is frames per hundred seconds
  2081. *piAvgFrameRate = MulDiv(100000, m_cFramesDrawn, t);
  2082. }
  2083. return NOERROR;
  2084. } // get_AvgFrameRate
  2085. // Set *piAvg to the average sync offset since streaming started
  2086. // in mSec. The sync offset is the time in mSec between when the frame
  2087. // should have been drawn and when the frame was actually drawn.
  2088. STDMETHODIMP CBaseVideoRenderer::get_AvgSyncOffset( int *piAvg)
  2089. {
  2090. CheckPointer(piAvg,E_POINTER);
  2091. CAutoLock cVideoLock(&m_InterfaceLock);
  2092. if (NULL==m_pClock) {
  2093. *piAvg = 0;
  2094. return NOERROR;
  2095. }
  2096. // Note that we didn't gather the stats on the first frame
  2097. // so we use m_cFramesDrawn-1 here
  2098. if (m_cFramesDrawn<=1) {
  2099. *piAvg = 0;
  2100. } else {
  2101. *piAvg = (int)(m_iTotAcc / (m_cFramesDrawn-1));
  2102. }
  2103. return NOERROR;
  2104. } // get_AvgSyncOffset
  2105. // To avoid dragging in the maths library - a cheap
  2106. // approximate integer square root.
  2107. // We do this by getting a starting guess which is between 1
  2108. // and 2 times too large, followed by THREE iterations of
  2109. // Newton Raphson. (That will give accuracy to the nearest mSec
  2110. // for the range in question - roughly 0..1000)
  2111. //
  2112. // It would be faster to use a linear interpolation and ONE NR, but
  2113. // who cares. If anyone does - the best linear interpolation is
  2114. // to approximates sqrt(x) by
  2115. // y = x * (sqrt(2)-1) + 1 - 1/sqrt(2) + 1/(8*(sqrt(2)-1))
  2116. // 0r y = x*0.41421 + 0.59467
  2117. // This minimises the maximal error in the range in question.
  2118. // (error is about +0.008883 and then one NR will give error .0000something
  2119. // (Of course these are integers, so you can't just multiply by 0.41421
  2120. // you'd have to do some sort of MulDiv).
  2121. // Anyone wanna check my maths? (This is only for a property display!)
  2122. int isqrt(int x)
  2123. {
  2124. int s = 1;
  2125. // Make s an initial guess for sqrt(x)
  2126. if (x > 0x40000000) {
  2127. s = 0x8000; // prevent any conceivable closed loop
  2128. } else {
  2129. while (s*s<x) { // loop cannot possible go more than 31 times
  2130. s = 2*s; // normally it goes about 6 times
  2131. }
  2132. // Three NR iterations.
  2133. if (x==0) {
  2134. s= 0; // Wouldn't it be tragic to divide by zero whenever our
  2135. // accuracy was perfect!
  2136. } else {
  2137. s = (s*s+x)/(2*s);
  2138. if (s>=0) s = (s*s+x)/(2*s);
  2139. if (s>=0) s = (s*s+x)/(2*s);
  2140. }
  2141. }
  2142. return s;
  2143. }
  2144. //
  2145. // Do estimates for standard deviations for per-frame
  2146. // statistics
  2147. //
  2148. HRESULT CBaseVideoRenderer::GetStdDev(
  2149. int nSamples,
  2150. int *piResult,
  2151. LONGLONG llSumSq,
  2152. LONGLONG iTot
  2153. )
  2154. {
  2155. CheckPointer(piResult,E_POINTER);
  2156. CAutoLock cVideoLock(&m_InterfaceLock);
  2157. if (NULL==m_pClock) {
  2158. *piResult = 0;
  2159. return NOERROR;
  2160. }
  2161. // If S is the Sum of the Squares of observations and
  2162. // T the Total (i.e. sum) of the observations and there were
  2163. // N observations, then an estimate of the standard deviation is
  2164. // sqrt( (S - T**2/N) / (N-1) )
  2165. if (nSamples<=1) {
  2166. *piResult = 0;
  2167. } else {
  2168. LONGLONG x;
  2169. // First frames have invalid stamps, so we get no stats for them
  2170. // So we need 2 frames to get 1 datum, so N is cFramesDrawn-1
  2171. // so we use m_cFramesDrawn-1 here
  2172. x = llSumSq - llMulDiv(iTot, iTot, nSamples, 0);
  2173. x = x / (nSamples-1);
  2174. ASSERT(x>=0);
  2175. *piResult = isqrt((LONG)x);
  2176. }
  2177. return NOERROR;
  2178. }
  2179. // Set *piDev to the standard deviation in mSec of the sync offset
  2180. // of each frame since streaming started.
  2181. STDMETHODIMP CBaseVideoRenderer::get_DevSyncOffset( int *piDev)
  2182. {
  2183. // First frames have invalid stamps, so we get no stats for them
  2184. // So we need 2 frames to get 1 datum, so N is cFramesDrawn-1
  2185. return GetStdDev(m_cFramesDrawn - 1,
  2186. piDev,
  2187. m_iSumSqAcc,
  2188. m_iTotAcc);
  2189. } // get_DevSyncOffset
  2190. // Set *piJitter to the standard deviation in mSec of the inter-frame time
  2191. // of frames since streaming started.
  2192. STDMETHODIMP CBaseVideoRenderer::get_Jitter( int *piJitter)
  2193. {
  2194. // First frames have invalid stamps, so we get no stats for them
  2195. // So second frame gives invalid inter-frame time
  2196. // So we need 3 frames to get 1 datum, so N is cFramesDrawn-2
  2197. return GetStdDev(m_cFramesDrawn - 2,
  2198. piJitter,
  2199. m_iSumSqFrameTime,
  2200. m_iSumFrameTime);
  2201. } // get_Jitter
  2202. // Overidden to return our IQualProp interface
  2203. STDMETHODIMP
  2204. CBaseVideoRenderer::NonDelegatingQueryInterface(REFIID riid,VOID **ppv)
  2205. {
  2206. // We return IQualProp and delegate everything else
  2207. if (riid == IID_IQualProp) {
  2208. return GetInterface( (IQualProp *)this, ppv);
  2209. } else if (riid == IID_IQualityControl) {
  2210. return GetInterface( (IQualityControl *)this, ppv);
  2211. }
  2212. return CBaseRenderer::NonDelegatingQueryInterface(riid,ppv);
  2213. }
  2214. // Override JoinFilterGraph so that, just before leaving
  2215. // the graph we can send an EC_WINDOW_DESTROYED event
  2216. STDMETHODIMP
  2217. CBaseVideoRenderer::JoinFilterGraph(IFilterGraph *pGraph,LPCWSTR pName)
  2218. {
  2219. // Since we send EC_ACTIVATE, we also need to ensure
  2220. // we send EC_WINDOW_DESTROYED or the resource manager may be
  2221. // holding us as a focus object
  2222. if (!pGraph && m_pGraph) {
  2223. // We were in a graph and now we're not
  2224. // Do this properly in case we are aggregated
  2225. IBaseFilter* pFilter;
  2226. QueryInterface(IID_IBaseFilter,(void **) &pFilter);
  2227. NotifyEvent(EC_WINDOW_DESTROYED, (LPARAM) pFilter, 0);
  2228. pFilter->Release();
  2229. }
  2230. return CBaseFilter::JoinFilterGraph(pGraph, pName);
  2231. }
  2232. // This removes a large number of level 4 warnings from the
  2233. // Microsoft compiler which in this case are not very useful
  2234. #pragma warning(disable: 4514)