[STRMBASE] Sync with Wine Staging 1.9.16. CORE-11866
[reactos.git] / reactos / sdk / 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 #define XTIME_FMT "%u.%03u"
29 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
30
31 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv)
32 {
33 QualityControlImpl *This;
34 TRACE("%p, %p, %p\n", input, self, ppv);
35 *ppv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(QualityControlImpl));
36 if (!*ppv)
37 return E_OUTOFMEMORY;
38 This = *ppv;
39 This->input = input;
40 This->self = self;
41 This->tonotify = NULL;
42 This->clock = NULL;
43 This->current_rstart = This->current_rstop = -1;
44 TRACE("-> %p\n", This);
45 return S_OK;
46 }
47
48 void QualityControlImpl_Destroy(QualityControlImpl *This)
49 {
50 HeapFree(GetProcessHeap(),0,This);
51 }
52
53 static inline QualityControlImpl *impl_from_IQualityControl(IQualityControl *iface)
54 {
55 return CONTAINING_RECORD(iface, QualityControlImpl, IQualityControl_iface);
56 }
57
58 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
59 {
60 QualityControlImpl *This = impl_from_IQualityControl(iface);
61 return IBaseFilter_QueryInterface(This->self, riid, ppv);
62 }
63
64 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface)
65 {
66 QualityControlImpl *This = impl_from_IQualityControl(iface);
67 return IBaseFilter_AddRef(This->self);
68 }
69
70 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface)
71 {
72 QualityControlImpl *This = impl_from_IQualityControl(iface);
73 return IBaseFilter_Release(This->self);
74 }
75
76 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
77 {
78 QualityControlImpl *This = impl_from_IQualityControl(iface);
79 HRESULT hr = S_FALSE;
80
81 TRACE("%p %p { 0x%x %u " XTIME_FMT " " XTIME_FMT " }\n",
82 This, sender, qm.Type, qm.Proportion,
83 XTIME(qm.Late), XTIME(qm.TimeStamp));
84
85 if (This->tonotify)
86 return IQualityControl_Notify(This->tonotify, This->self, qm);
87
88 if (This->input) {
89 IPin *to = NULL;
90 IPin_ConnectedTo(This->input, &to);
91 if (to) {
92 IQualityControl *qc = NULL;
93 IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
94 if (qc) {
95 hr = IQualityControl_Notify(qc, This->self, qm);
96 IQualityControl_Release(qc);
97 }
98 IPin_Release(to);
99 }
100 }
101
102 return hr;
103 }
104
105 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
106 {
107 QualityControlImpl *This = impl_from_IQualityControl(iface);
108 TRACE("%p %p\n", This, tonotify);
109 This->tonotify = tonotify;
110 return S_OK;
111 }
112
113 /* Macros copied from gstreamer, weighted average between old average and new ones */
114 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
115
116 /* generic running average, this has a neutral window size */
117 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
118
119 /* the windows for these running averages are experimentally obtained.
120 * possitive values get averaged more while negative values use a small
121 * window so we can react faster to badness. */
122 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
123 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
124
125 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart)
126 {
127 TRACE("%p " XTIME_FMT "\n", This, XTIME(tStart));
128 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
129 This->clockstart = tStart;
130 This->avg_rate = -1.0;
131 This->rendered = This->dropped = 0;
132 This->is_dropped = FALSE;
133 This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
134 }
135
136
137 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock)
138 {
139 TRACE("%p %p\n", This, clock);
140 This->clock = clock;
141 }
142
143 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
144 REFERENCE_TIME start, REFERENCE_TIME stop)
145 {
146 REFERENCE_TIME max_lateness = 200000;
147
148 TRACE("%p " XTIME_FMT " " XTIME_FMT " " XTIME_FMT "\n",
149 This, XTIME(jitter), XTIME(start), XTIME(stop));
150
151 /* we can add a valid stop time */
152 if (stop >= start)
153 max_lateness += stop;
154 else
155 max_lateness += start;
156
157 /* if the jitter bigger than duration and lateness we are too late */
158 if (start + jitter > max_lateness) {
159 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
160 /* !!emergency!!, if we did not receive anything valid for more than a
161 * second, render it anyway so the user sees something */
162 if (This->last_in_time < 0 ||
163 start - This->last_in_time < 10000000)
164 return TRUE;
165 FIXME("A lot of buffers are being dropped.\n");
166 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
167 }
168 This->last_in_time = start;
169 return FALSE;
170 }
171
172 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev)
173 {
174 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
175
176 TRACE("%p %p %p\n", This, sample, ev);
177
178 This->current_rstart = This->current_rstop = -1;
179 This->current_jitter = 0;
180 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
181 return S_OK;
182
183 if (start >= 0) {
184 REFERENCE_TIME now;
185 IReferenceClock_GetTime(This->clock, &now);
186 now -= This->clockstart;
187
188 jitter = now - start;
189 if (jitter <= -10000) {
190 DWORD_PTR cookie;
191 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
192 WaitForSingleObject(ev, INFINITE);
193 IReferenceClock_Unadvise(This->clock, cookie);
194 }
195 }
196 else
197 start = stop = -1;
198 This->current_rstart = start;
199 This->current_rstop = stop > start ? stop : start;
200 This->current_jitter = jitter;
201 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
202 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
203 if (This->is_dropped) {
204 This->dropped++;
205 if (!This->qos_handled)
206 return S_FALSE;
207 } else
208 This->rendered++;
209 return S_OK;
210 }
211
212 void QualityControlRender_DoQOS(QualityControlImpl *priv)
213 {
214 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
215 double rate;
216
217 TRACE("%p\n", priv);
218
219 if (!priv->clock || priv->current_rstart < 0)
220 return;
221
222 start = priv->current_rstart;
223 stop = priv->current_rstop;
224 jitter = priv->current_jitter;
225
226 if (jitter < 0) {
227 /* this is the time the buffer entered the sink */
228 if (start < -jitter)
229 entered = 0;
230 else
231 entered = start + jitter;
232 left = start;
233 } else {
234 /* this is the time the buffer entered the sink */
235 entered = start + jitter;
236 /* this is the time the buffer left the sink */
237 left = start + jitter;
238 }
239
240 /* calculate duration of the buffer */
241 if (stop >= start)
242 duration = stop - start;
243 else
244 duration = 0;
245
246 /* if we have the time when the last buffer left us, calculate
247 * processing time */
248 if (priv->last_left >= 0) {
249 if (entered > priv->last_left) {
250 pt = entered - priv->last_left;
251 } else {
252 pt = 0;
253 }
254 } else {
255 pt = priv->avg_pt;
256 }
257
258 TRACE("start: " XTIME_FMT ", entered " XTIME_FMT ", left " XTIME_FMT ", pt: " XTIME_FMT ", "
259 "duration " XTIME_FMT ", jitter " XTIME_FMT "\n", XTIME(start), XTIME(entered),
260 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
261
262 TRACE("avg_duration: " XTIME_FMT ", avg_pt: " XTIME_FMT ", avg_rate: %g\n",
263 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
264
265 /* collect running averages. for first observations, we copy the
266 * values */
267 if (priv->avg_duration < 0)
268 priv->avg_duration = duration;
269 else
270 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
271
272 if (priv->avg_pt < 0)
273 priv->avg_pt = pt;
274 else
275 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
276
277 if (priv->avg_duration != 0)
278 rate =
279 (double)priv->avg_pt /
280 (double)priv->avg_duration;
281 else
282 rate = 0.0;
283
284 if (priv->last_left >= 0) {
285 if (priv->is_dropped || priv->avg_rate < 0.0) {
286 priv->avg_rate = rate;
287 } else {
288 if (rate > 1.0)
289 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
290 else
291 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
292 }
293 }
294
295 if (priv->avg_rate >= 0.0) {
296 HRESULT hr;
297 Quality q;
298 /* if we have a valid rate, start sending QoS messages */
299 if (priv->current_jitter < 0) {
300 /* make sure we never go below 0 when adding the jitter to the
301 * timestamp. */
302 if (priv->current_rstart < -priv->current_jitter)
303 priv->current_jitter = -priv->current_rstart;
304 }
305 else
306 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
307 q.Type = (jitter > 0 ? Famine : Flood);
308 q.Proportion = (LONG)(1000. / priv->avg_rate);
309 if (q.Proportion < 200)
310 q.Proportion = 200;
311 else if (q.Proportion > 5000)
312 q.Proportion = 5000;
313 q.Late = priv->current_jitter;
314 q.TimeStamp = priv->current_rstart;
315 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
316 hr = IQualityControl_Notify(&priv->IQualityControl_iface, priv->self, q);
317 priv->qos_handled = hr == S_OK;
318 }
319
320 /* record when this buffer will leave us */
321 priv->last_left = left;
322 }
323
324
325 void QualityControlRender_BeginRender(QualityControlImpl *This)
326 {
327 TRACE("%p\n", This);
328
329 This->start = -1;
330
331 if (!This->clock)
332 return;
333
334 IReferenceClock_GetTime(This->clock, &This->start);
335 TRACE("at: " XTIME_FMT "\n", XTIME(This->start));
336 }
337
338 void QualityControlRender_EndRender(QualityControlImpl *This)
339 {
340 REFERENCE_TIME elapsed;
341
342 TRACE("%p\n", This);
343
344 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
345 return;
346
347 elapsed = This->start - This->stop;
348 if (elapsed < 0)
349 return;
350 if (This->avg_render < 0)
351 This->avg_render = elapsed;
352 else
353 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
354 }