strmbase: Initialize QualityControl struct on allocation.
[wine.git] / dlls / strmbase / qualitycontrol.c
blob46e0193453c321d10be30adadcb786310824ea3c
1 /*
2 * Quality Control Interfaces
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
6 * rendering qos functions based on, the original can be found at
7 * gstreamer/libs/gst/base/gstbasesink.c which has copyright notice:
9 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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
26 #define COBJMACROS
28 #include "dshow.h"
29 #include "wine/strmbase.h"
30 #include "strmbase_private.h"
32 #include "uuids.h"
33 #include "wine/debug.h"
35 #include <assert.h>
37 WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc);
39 #define XTIME_FMT "%u.%03u"
40 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
42 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv)
44 QualityControlImpl *This;
45 TRACE("%p, %p, %p\n", input, self, ppv);
46 *ppv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(QualityControlImpl));
47 if (!*ppv)
48 return E_OUTOFMEMORY;
49 This = *ppv;
50 This->input = input;
51 This->self = self;
52 This->tonotify = NULL;
53 This->clock = NULL;
54 This->current_rstart = This->current_rstop = -1;
55 TRACE("-> %p\n", This);
56 return S_OK;
59 void QualityControlImpl_Destroy(QualityControlImpl *This)
61 HeapFree(GetProcessHeap(),0,This);
64 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
66 QualityControlImpl *This = (QualityControlImpl*)iface;
67 return IBaseFilter_QueryInterface(This->self, riid, ppv);
70 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface)
72 QualityControlImpl *This = (QualityControlImpl*)iface;
73 return IBaseFilter_AddRef(This->self);
76 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface)
78 QualityControlImpl *This = (QualityControlImpl*)iface;
79 return IBaseFilter_Release(This->self);
82 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
84 HRESULT hr = S_FALSE;
85 QualityControlImpl *This = (QualityControlImpl*)iface;
87 TRACE("%p %p { 0x%x %u " XTIME_FMT " " XTIME_FMT " }\n",
88 This, sender, qm.Type, qm.Proportion,
89 XTIME(qm.Late), XTIME(qm.TimeStamp));
91 if (This->tonotify)
92 return IQualityControl_Notify(This->tonotify, This->self, qm);
94 if (This->input) {
95 IPin *to = NULL;
96 IPin_ConnectedTo(This->input, &to);
97 if (to) {
98 IQualityControl *qc = NULL;
99 IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
100 if (qc) {
101 hr = IQualityControl_Notify(qc, This->self, qm);
102 IQualityControl_Release(qc);
104 IPin_Release(to);
108 return hr;
111 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
113 QualityControlImpl *This = (QualityControlImpl*)iface;
114 TRACE("%p %p\n", This, tonotify);
115 This->tonotify = tonotify;
116 return S_OK;
119 /* Macros copied from gstreamer, weighted average between old average and new ones */
120 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
122 /* generic running average, this has a neutral window size */
123 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
125 /* the windows for these running averages are experimentally obtained.
126 * possitive values get averaged more while negative values use a small
127 * window so we can react faster to badness. */
128 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
129 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
131 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart)
133 TRACE("%p " XTIME_FMT "\n", This, XTIME(tStart));
134 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
135 This->clockstart = tStart;
136 This->avg_rate = -1.0;
137 This->rendered = This->dropped = 0;
138 This->is_dropped = FALSE;
139 This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
143 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock)
145 TRACE("%p %p\n", This, clock);
146 This->clock = clock;
149 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
150 REFERENCE_TIME start, REFERENCE_TIME stop)
152 REFERENCE_TIME max_lateness = 200000;
154 TRACE("%p " XTIME_FMT " " XTIME_FMT " " XTIME_FMT "\n",
155 This, XTIME(jitter), XTIME(start), XTIME(stop));
157 /* we can add a valid stop time */
158 if (stop >= start)
159 max_lateness += stop;
160 else
161 max_lateness += start;
163 /* if the jitter bigger than duration and lateness we are too late */
164 if (start + jitter > max_lateness) {
165 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
166 /* !!emergency!!, if we did not receive anything valid for more than a
167 * second, render it anyway so the user sees something */
168 if (This->last_in_time < 0 ||
169 start - This->last_in_time < 10000000)
170 return TRUE;
171 FIXME("A lot of buffers are being dropped.\n");
172 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
174 This->last_in_time = start;
175 return FALSE;
178 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev)
180 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
182 TRACE("%p %p %p\n", This, sample, ev);
184 This->current_rstart = This->current_rstop = -1;
185 This->current_jitter = 0;
186 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
187 return S_OK;
189 if (start >= 0) {
190 REFERENCE_TIME now;
191 IReferenceClock_GetTime(This->clock, &now);
192 now -= This->clockstart;
194 jitter = now - start;
195 if (jitter <= -10000) {
196 DWORD_PTR cookie;
197 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
198 WaitForSingleObject(ev, INFINITE);
199 IReferenceClock_Unadvise(This->clock, cookie);
202 else
203 start = stop = -1;
204 This->current_rstart = start;
205 This->current_rstop = stop > start ? stop : start;
206 This->current_jitter = jitter;
207 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
208 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
209 if (This->is_dropped) {
210 This->dropped++;
211 if (!This->qos_handled)
212 return S_FALSE;
213 } else
214 This->rendered++;
215 return S_OK;
218 void QualityControlRender_DoQOS(QualityControlImpl *priv)
220 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
221 double rate;
223 TRACE("%p\n", priv);
225 if (!priv->clock || priv->current_rstart < 0)
226 return;
228 start = priv->current_rstart;
229 stop = priv->current_rstop;
230 jitter = priv->current_jitter;
232 if (jitter < 0) {
233 /* this is the time the buffer entered the sink */
234 if (start < -jitter)
235 entered = 0;
236 else
237 entered = start + jitter;
238 left = start;
239 } else {
240 /* this is the time the buffer entered the sink */
241 entered = start + jitter;
242 /* this is the time the buffer left the sink */
243 left = start + jitter;
246 /* calculate duration of the buffer */
247 if (stop >= start)
248 duration = stop - start;
249 else
250 duration = 0;
252 /* if we have the time when the last buffer left us, calculate
253 * processing time */
254 if (priv->last_left >= 0) {
255 if (entered > priv->last_left) {
256 pt = entered - priv->last_left;
257 } else {
258 pt = 0;
260 } else {
261 pt = priv->avg_pt;
264 TRACE("start: " XTIME_FMT ", entered " XTIME_FMT ", left " XTIME_FMT ", pt: " XTIME_FMT ", "
265 "duration " XTIME_FMT ", jitter " XTIME_FMT "\n", XTIME(start), XTIME(entered),
266 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
268 TRACE("avg_duration: " XTIME_FMT ", avg_pt: " XTIME_FMT ", avg_rate: %g\n",
269 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
271 /* collect running averages. for first observations, we copy the
272 * values */
273 if (priv->avg_duration < 0)
274 priv->avg_duration = duration;
275 else
276 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
278 if (priv->avg_pt < 0)
279 priv->avg_pt = pt;
280 else
281 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
283 if (priv->avg_duration != 0)
284 rate =
285 (double)priv->avg_pt /
286 (double)priv->avg_duration;
287 else
288 rate = 0.0;
290 if (priv->last_left >= 0) {
291 if (priv->is_dropped || priv->avg_rate < 0.0) {
292 priv->avg_rate = rate;
293 } else {
294 if (rate > 1.0)
295 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
296 else
297 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
301 if (priv->avg_rate >= 0.0) {
302 HRESULT hr;
303 Quality q;
304 /* if we have a valid rate, start sending QoS messages */
305 if (priv->current_jitter < 0) {
306 /* make sure we never go below 0 when adding the jitter to the
307 * timestamp. */
308 if (priv->current_rstart < -priv->current_jitter)
309 priv->current_jitter = -priv->current_rstart;
311 else
312 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
313 q.Type = (jitter > 0 ? Famine : Flood);
314 q.Proportion = (LONG)(1000. / priv->avg_rate);
315 if (q.Proportion < 200)
316 q.Proportion = 200;
317 else if (q.Proportion > 5000)
318 q.Proportion = 5000;
319 q.Late = priv->current_jitter;
320 q.TimeStamp = priv->current_rstart;
321 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
322 hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
323 priv->qos_handled = hr == S_OK;
326 /* record when this buffer will leave us */
327 priv->last_left = left;
331 void QualityControlRender_BeginRender(QualityControlImpl *This)
333 TRACE("%p\n", This);
335 This->start = -1;
337 if (!This->clock)
338 return;
340 IReferenceClock_GetTime(This->clock, &This->start);
341 TRACE("at: " XTIME_FMT "\n", XTIME(This->start));
344 void QualityControlRender_EndRender(QualityControlImpl *This)
346 REFERENCE_TIME elapsed;
348 TRACE("%p\n", This);
350 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
351 return;
353 elapsed = This->start - This->stop;
354 if (elapsed < 0)
355 return;
356 if (This->avg_render < 0)
357 This->avg_render = elapsed;
358 else
359 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);