qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / dlls / strmbase / qualitycontrol.c
blob9bdc25104e6645df818c843988ca96916b55d2a0
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 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv) {
40 QualityControlImpl *This;
41 *ppv = HeapAlloc(GetProcessHeap(),0,sizeof(QualityControlImpl));
42 if (!*ppv)
43 return E_OUTOFMEMORY;
44 This = *ppv;
45 This->input = input;
46 This->self = self;
47 This->tonotify = NULL;
48 This->clock = NULL;
49 return S_OK;
52 HRESULT QualityControlImpl_Destroy(QualityControlImpl *This)
54 return HeapFree(GetProcessHeap(),0,This);
57 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
58 QualityControlImpl *This = (QualityControlImpl*)iface;
59 return IBaseFilter_QueryInterface(This->self, riid, ppv);
62 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
63 QualityControlImpl *This = (QualityControlImpl*)iface;
64 return IBaseFilter_AddRef(This->self);
67 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
68 QualityControlImpl *This = (QualityControlImpl*)iface;
69 return IBaseFilter_Release(This->self);
72 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
73 HRESULT hr = S_FALSE;
74 QualityControlImpl *This = (QualityControlImpl*)iface;
75 if (This->tonotify)
76 return IQualityControl_Notify(This->tonotify, This->self, qm);
77 if (This->input) {
78 IPin *to = NULL;
79 IPin_ConnectedTo(This->input, &to);
80 if (to) {
81 IQualityControl *qc = NULL;
82 IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
83 if (qc) {
84 hr = IQualityControl_Notify(qc, This->self, qm);
85 IQualityControl_Release(qc);
87 IPin_Release(to);
90 return hr;
93 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
94 QualityControlImpl *This = (QualityControlImpl*)iface;
95 This->tonotify = tonotify;
96 return S_OK;
99 /* Macros copied from gstreamer, weighted average between old average and new ones */
100 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
102 /* generic running average, this has a neutral window size */
103 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
105 /* the windows for these running averages are experimentally obtained.
106 * possitive values get averaged more while negative values use a small
107 * window so we can react faster to badness. */
108 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
109 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
111 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart) {
112 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
113 This->clockstart = tStart;
114 This->avg_rate = -1.0;
115 This->rendered = This->dropped = 0;
116 This->is_dropped = FALSE;
117 This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
121 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
122 This->clock = clock;
125 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
126 REFERENCE_TIME start, REFERENCE_TIME stop)
128 REFERENCE_TIME max_lateness = 200000;
130 /* we can add a valid stop time */
131 if (stop >= start)
132 max_lateness += stop;
133 else
134 max_lateness += start;
136 /* if the jitter bigger than duration and lateness we are too late */
137 if (start + jitter > max_lateness) {
138 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
139 /* !!emergency!!, if we did not receive anything valid for more than a
140 * second, render it anyway so the user sees something */
141 if (This->last_in_time < 0 ||
142 start - This->last_in_time < 10000000)
143 return TRUE;
144 FIXME("A lot of buffers are being dropped.\n");
145 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
147 This->last_in_time = start;
148 return FALSE;
151 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev) {
152 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
153 This->current_rstart = This->current_rstop = -1;
154 This->current_jitter = 0;
155 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
156 return S_OK;
158 if (start >= 0) {
159 REFERENCE_TIME now;
160 IReferenceClock_GetTime(This->clock, &now);
161 now -= This->clockstart;
163 jitter = now - start;
164 if (jitter <= -10000) {
165 DWORD_PTR cookie;
166 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
167 WaitForSingleObject(ev, INFINITE);
168 IReferenceClock_Unadvise(This->clock, cookie);
171 else
172 start = stop = -1;
173 This->current_rstart = start;
174 This->current_rstop = stop > start ? stop : start;
175 This->current_jitter = jitter;
176 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
177 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
178 if (This->is_dropped) {
179 This->dropped++;
180 if (!This->qos_handled)
181 return S_FALSE;
182 } else
183 This->rendered++;
184 return S_OK;
187 void QualityControlRender_DoQOS(QualityControlImpl *priv)
189 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
190 double rate;
192 if (!priv->clock || priv->current_rstart < 0)
193 return;
195 start = priv->current_rstart;
196 stop = priv->current_rstop;
197 jitter = priv->current_jitter;
199 if (jitter < 0) {
200 /* this is the time the buffer entered the sink */
201 if (start < -jitter)
202 entered = 0;
203 else
204 entered = start + jitter;
205 left = start;
206 } else {
207 /* this is the time the buffer entered the sink */
208 entered = start + jitter;
209 /* this is the time the buffer left the sink */
210 left = start + jitter;
213 /* calculate duration of the buffer */
214 if (stop >= start)
215 duration = stop - start;
216 else
217 duration = 0;
219 /* if we have the time when the last buffer left us, calculate
220 * processing time */
221 if (priv->last_left >= 0) {
222 if (entered > priv->last_left) {
223 pt = entered - priv->last_left;
224 } else {
225 pt = 0;
227 } else {
228 pt = priv->avg_pt;
231 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
232 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
233 "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
234 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
236 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
237 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
238 #undef XTIME
240 /* collect running averages. for first observations, we copy the
241 * values */
242 if (priv->avg_duration < 0)
243 priv->avg_duration = duration;
244 else
245 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
247 if (priv->avg_pt < 0)
248 priv->avg_pt = pt;
249 else
250 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
252 if (priv->avg_duration != 0)
253 rate =
254 (double)priv->avg_pt /
255 (double)priv->avg_duration;
256 else
257 rate = 0.0;
259 if (priv->last_left >= 0) {
260 if (priv->is_dropped || priv->avg_rate < 0.0) {
261 priv->avg_rate = rate;
262 } else {
263 if (rate > 1.0)
264 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
265 else
266 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
270 if (priv->avg_rate >= 0.0) {
271 HRESULT hr;
272 Quality q;
273 /* if we have a valid rate, start sending QoS messages */
274 if (priv->current_jitter < 0) {
275 /* make sure we never go below 0 when adding the jitter to the
276 * timestamp. */
277 if (priv->current_rstart < -priv->current_jitter)
278 priv->current_jitter = -priv->current_rstart;
280 else
281 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
282 q.Type = (jitter > 0 ? Famine : Flood);
283 q.Proportion = (LONG)(1000. / priv->avg_rate);
284 if (q.Proportion < 200)
285 q.Proportion = 200;
286 else if (q.Proportion > 5000)
287 q.Proportion = 5000;
288 q.Late = priv->current_jitter;
289 q.TimeStamp = priv->current_rstart;
290 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
291 hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
292 priv->qos_handled = hr == S_OK;
295 /* record when this buffer will leave us */
296 priv->last_left = left;
300 void QualityControlRender_BeginRender(QualityControlImpl *This) {
301 This->start = -1;
302 if (!This->clock)
303 return;
304 IReferenceClock_GetTime(This->clock, &This->start);
307 void QualityControlRender_EndRender(QualityControlImpl *This) {
308 REFERENCE_TIME elapsed;
309 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
310 return;
312 elapsed = This->start - This->stop;
313 if (elapsed < 0)
314 return;
315 if (This->avg_render < 0)
316 This->avg_render = elapsed;
317 else
318 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);