[STRMBASE]
[reactos.git] / reactos / lib / 3rdparty / strmbase / qualitycontrol.c
1 /*
2 * Quality Control Interfaces
3 *
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
5 *
6 * rendering qos functions based on, the original can be found at
7 * gstreamer/libs/gst/base/gstbasesink.c which has copyright notice:
8 *
9 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include "strmbase_private.h"
27
28 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv) {
29 QualityControlImpl *This;
30 *ppv = HeapAlloc(GetProcessHeap(),0,sizeof(QualityControlImpl));
31 if (!*ppv)
32 return E_OUTOFMEMORY;
33 This = *ppv;
34 This->input = input;
35 This->self = self;
36 This->tonotify = NULL;
37 This->clock = NULL;
38 return S_OK;
39 }
40
41 HRESULT QualityControlImpl_Destroy(QualityControlImpl *This)
42 {
43 return HeapFree(GetProcessHeap(),0,This);
44 }
45
46 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
47 QualityControlImpl *This = (QualityControlImpl*)iface;
48 return IBaseFilter_QueryInterface(This->self, riid, ppv);
49 }
50
51 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
52 QualityControlImpl *This = (QualityControlImpl*)iface;
53 return IBaseFilter_AddRef(This->self);
54 }
55
56 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
57 QualityControlImpl *This = (QualityControlImpl*)iface;
58 return IBaseFilter_Release(This->self);
59 }
60
61 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
62 HRESULT hr = S_FALSE;
63 QualityControlImpl *This = (QualityControlImpl*)iface;
64 if (This->tonotify)
65 return IQualityControl_Notify(This->tonotify, This->self, qm);
66 if (This->input) {
67 IPin *to = NULL;
68 IPin_ConnectedTo(This->input, &to);
69 if (to) {
70 IQualityControl *qc = NULL;
71 IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
72 if (qc) {
73 hr = IQualityControl_Notify(qc, This->self, qm);
74 IQualityControl_Release(qc);
75 }
76 IPin_Release(to);
77 }
78 }
79 return hr;
80 }
81
82 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
83 QualityControlImpl *This = (QualityControlImpl*)iface;
84 This->tonotify = tonotify;
85 return S_OK;
86 }
87
88 /* Macros copied from gstreamer, weighted average between old average and new ones */
89 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
90
91 /* generic running average, this has a neutral window size */
92 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
93
94 /* the windows for these running averages are experimentally obtained.
95 * possitive values get averaged more while negative values use a small
96 * window so we can react faster to badness. */
97 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
98 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
99
100 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart) {
101 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
102 This->clockstart = tStart;
103 This->avg_rate = -1.0;
104 This->rendered = This->dropped = This->is_dropped = 0;
105 This->qos_handled = 1; /* Lie that will be corrected on first adjustment */
106 }
107
108
109 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
110 This->clock = clock;
111 }
112
113 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
114 REFERENCE_TIME start, REFERENCE_TIME stop)
115 {
116 REFERENCE_TIME max_lateness = 200000;
117
118 /* we can add a valid stop time */
119 if (stop >= start)
120 max_lateness += stop;
121 else
122 max_lateness += start;
123
124 /* if the jitter bigger than duration and lateness we are too late */
125 if (start + jitter > max_lateness) {
126 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
127 /* !!emergency!!, if we did not receive anything valid for more than a
128 * second, render it anyway so the user sees something */
129 if (This->last_in_time < 0 ||
130 start - This->last_in_time < 10000000)
131 return TRUE;
132 FIXME("A lot of buffers are being dropped.\n");
133 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
134 }
135 This->last_in_time = start;
136 return FALSE;
137 }
138
139 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev) {
140 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
141 This->current_rstart = This->current_rstop = -1;
142 This->current_jitter = 0;
143 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
144 return S_OK;
145
146 if (start >= 0) {
147 REFERENCE_TIME now;
148 IReferenceClock_GetTime(This->clock, &now);
149 now -= This->clockstart;
150
151 jitter = now - start;
152 if (jitter <= -10000) {
153 DWORD_PTR cookie;
154 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
155 WaitForSingleObject(ev, INFINITE);
156 IReferenceClock_Unadvise(This->clock, cookie);
157 }
158 }
159 else
160 start = stop = -1;
161 This->current_rstart = start;
162 This->current_rstop = stop > start ? stop : start;
163 This->current_jitter = jitter;
164 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
165 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
166 if (This->is_dropped) {
167 This->dropped++;
168 if (!This->qos_handled)
169 return S_FALSE;
170 } else
171 This->rendered++;
172 return S_OK;
173 }
174
175 void QualityControlRender_DoQOS(QualityControlImpl *priv)
176 {
177 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
178 double rate;
179
180 if (!priv->clock || priv->current_rstart < 0)
181 return;
182
183 start = priv->current_rstart;
184 stop = priv->current_rstop;
185 jitter = priv->current_jitter;
186
187 if (jitter < 0) {
188 /* this is the time the buffer entered the sink */
189 if (start < -jitter)
190 entered = 0;
191 else
192 entered = start + jitter;
193 left = start;
194 } else {
195 /* this is the time the buffer entered the sink */
196 entered = start + jitter;
197 /* this is the time the buffer left the sink */
198 left = start + jitter;
199 }
200
201 /* calculate duration of the buffer */
202 if (stop >= start)
203 duration = stop - start;
204 else
205 duration = 0;
206
207 /* if we have the time when the last buffer left us, calculate
208 * processing time */
209 if (priv->last_left >= 0) {
210 if (entered > priv->last_left) {
211 pt = entered - priv->last_left;
212 } else {
213 pt = 0;
214 }
215 } else {
216 pt = priv->avg_pt;
217 }
218
219 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
220 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
221 "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
222 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
223
224 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
225 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
226 #undef XTIME
227
228 /* collect running averages. for first observations, we copy the
229 * values */
230 if (priv->avg_duration < 0)
231 priv->avg_duration = duration;
232 else
233 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
234
235 if (priv->avg_pt < 0)
236 priv->avg_pt = pt;
237 else
238 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
239
240 if (priv->avg_duration != 0)
241 rate =
242 (double)priv->avg_pt /
243 (double)priv->avg_duration;
244 else
245 rate = 0.0;
246
247 if (priv->last_left >= 0) {
248 if (priv->is_dropped || priv->avg_rate < 0.0) {
249 priv->avg_rate = rate;
250 } else {
251 if (rate > 1.0)
252 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
253 else
254 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
255 }
256 }
257
258 if (priv->avg_rate >= 0.0) {
259 HRESULT hr;
260 Quality q;
261 /* if we have a valid rate, start sending QoS messages */
262 if (priv->current_jitter < 0) {
263 /* make sure we never go below 0 when adding the jitter to the
264 * timestamp. */
265 if (priv->current_rstart < -priv->current_jitter)
266 priv->current_jitter = -priv->current_rstart;
267 }
268 else
269 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
270 q.Type = (jitter > 0 ? Famine : Flood);
271 q.Proportion = (LONG)(1000. / priv->avg_rate);
272 if (q.Proportion < 200)
273 q.Proportion = 200;
274 else if (q.Proportion > 5000)
275 q.Proportion = 5000;
276 q.Late = priv->current_jitter;
277 q.TimeStamp = priv->current_rstart;
278 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
279 hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
280 priv->qos_handled = hr == S_OK;
281 }
282
283 /* record when this buffer will leave us */
284 priv->last_left = left;
285 }
286
287
288 void QualityControlRender_BeginRender(QualityControlImpl *This) {
289 This->start = -1;
290 if (!This->clock)
291 return;
292 IReferenceClock_GetTime(This->clock, &This->start);
293 }
294
295 void QualityControlRender_EndRender(QualityControlImpl *This) {
296 REFERENCE_TIME elapsed;
297 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
298 return;
299
300 elapsed = This->start - This->stop;
301 if (elapsed < 0)
302 return;
303 if (This->avg_render < 0)
304 This->avg_render = elapsed;
305 else
306 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
307 }