-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.h
151 lines (125 loc) · 4.68 KB
/
scheduler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//////////////////////////////////////////////////////////////////////////
//
// Scheduler.h: Schedules when video frames are presented.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
//////////////////////////////////////////////////////////////////////////
/* Modifications made from the sample EVR Presenter are covered under
*
* Copyright (C) 2014 Andrew Van Til
* http://babgvant.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
struct SchedulerCallback;
//-----------------------------------------------------------------------------
// Scheduler class
//
// Schedules when a sample should be displayed.
//
// Note: Presentation of each sample is performed by another object which
// must implement SchedulerCallback::PresentSample.
//
// General design:
// The scheduler generally receives samples before their presentation time. It
// puts the samples on a queue and presents them in FIFO order on a worker
// thread. The scheduler communicates with the worker thread by posting thread
// messages.
//
// The caller has the option of presenting samples immediately (for example,
// for repaints).
//-----------------------------------------------------------------------------
class Scheduler
{
public:
Scheduler();
virtual ~Scheduler();
void SetCallback(SchedulerCallback *pCB)
{
m_pCB = pCB;
}
void SetFrameRate(const MFRatio& fps);
float GetClockRate() {
AutoLock lock(m_schedCritSec);
return m_fRate;
}
void SetClockRate(float fRate) {
AutoLock lock(m_schedCritSec);
m_fRate = fRate;
}
int GetFrameDropThreshold() {
AutoLock lock(m_schedCritSec);
return m_iFrameDropThreshold;
}
void SetFrameDropThreshold(int iThreshold) {
AutoLock lock(m_schedCritSec);
m_iFrameDropThreshold = iThreshold;
}
const LONGLONG& LastSampleTime() const { return m_LastSampleTime; }
const LONGLONG& FrameDuration() const { return m_PerFrameInterval; }
bool GetUseMfTimeCalc()
{
AutoLock lock(m_schedCritSec);
return m_bUseMfTimeCalc;
}
void SetUseMfTimeCalc(bool useMfTimeCalc)
{
AutoLock lock(m_schedCritSec);
m_bUseMfTimeCalc = useMfTimeCalc;
}
HRESULT StartScheduler(IMFClock *pClock);
HRESULT StopScheduler();
HRESULT ScheduleSample(IMFSample *pSample, BOOL bPresentNow);
HRESULT ProcessSamplesInQueue(LONG *plNextSleep);
HRESULT ProcessSample(IMFSample *pSample, LONG *plNextSleep);
HRESULT Flush();
// ThreadProc for the scheduler thread.
static DWORD WINAPI SchedulerThreadProc(LPVOID lpParameter);
private:
// non-static version of SchedulerThreadProc.
DWORD SchedulerThreadProcPrivate();
private:
ThreadSafeQueue<IMFSample> m_ScheduledSamples; // Samples waiting to be presented.
IMFClock *m_pClock; // Presentation clock. Can be NULL.
SchedulerCallback *m_pCB; // Weak reference; do not delete.
DWORD m_dwThreadID;
HANDLE m_hSchedulerThread;
HANDLE m_hThreadReadyEvent;
HANDLE m_hFlushEvent;
float m_fRate; // Playback rate.
MFTIME m_PerFrameInterval; // Duration of each frame.
LONGLONG m_PerFrame_1_4th; // 1/4th of the frame duration.
MFTIME m_LastSampleTime; // Most recent sample time.
bool m_bUseMfTimeCalc;
int m_iFrameDropThreshold;
CritSec m_schedCritSec;
};
//-----------------------------------------------------------------------------
// SchedulerCallback
//
// Defines the callback method to present samples.
//-----------------------------------------------------------------------------
struct SchedulerCallback
{
virtual HRESULT PresentSample(IMFSample *pSample, LONGLONG llTarget, LONGLONG timeDelta, LONGLONG remainingInQueue, LONGLONG frameDurationDiv4) = 0;
};