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
29 #include "wine/strmbase.h"
30 #include "strmbase_private.h"
33 #include "wine/debug.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
));
47 This
->tonotify
= NULL
;
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
) {
74 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
76 return IQualityControl_Notify(This
->tonotify
, This
->self
, qm
);
79 IPin_ConnectedTo(This
->input
, &to
);
81 IQualityControl
*qc
= NULL
;
82 IPin_QueryInterface(to
, &IID_IQualityControl
, (void**)&qc
);
84 hr
= IQualityControl_Notify(qc
, This
->self
, qm
);
85 IQualityControl_Release(qc
);
93 HRESULT WINAPI
QualityControlImpl_SetSink(IQualityControl
*iface
, IQualityControl
*tonotify
) {
94 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
95 This
->tonotify
= tonotify
;
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
= This
->is_dropped
= 0;
116 This
->qos_handled
= 1; /* Lie that will be corrected on first adjustment */
120 void QualityControlRender_SetClock(QualityControlImpl
*This
, IReferenceClock
*clock
) {
124 static BOOL
QualityControlRender_IsLate(QualityControlImpl
*This
, REFERENCE_TIME jitter
,
125 REFERENCE_TIME start
, REFERENCE_TIME stop
)
127 REFERENCE_TIME max_lateness
= 200000;
129 /* we can add a valid stop time */
131 max_lateness
+= stop
;
133 max_lateness
+= start
;
135 /* if the jitter bigger than duration and lateness we are too late */
136 if (start
+ jitter
> max_lateness
) {
137 WARN("buffer is too late %i > %i\n", (int)((start
+ jitter
)/10000), (int)(max_lateness
/10000));
138 /* !!emergency!!, if we did not receive anything valid for more than a
139 * second, render it anyway so the user sees something */
140 if (This
->last_in_time
< 0 ||
141 start
- This
->last_in_time
< 10000000)
143 FIXME("A lot of buffers are being dropped.\n");
144 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
146 This
->last_in_time
= start
;
150 HRESULT
QualityControlRender_WaitFor(QualityControlImpl
*This
, IMediaSample
*sample
, HANDLE ev
) {
151 REFERENCE_TIME start
= -1, stop
= -1, jitter
= 0;
152 This
->current_rstart
= This
->current_rstop
= -1;
153 This
->current_jitter
= 0;
154 if (!This
->clock
|| FAILED(IMediaSample_GetTime(sample
, &start
, &stop
)))
159 IReferenceClock_GetTime(This
->clock
, &now
);
160 now
-= This
->clockstart
;
162 jitter
= now
- start
;
163 if (jitter
<= -10000) {
165 IReferenceClock_AdviseTime(This
->clock
, This
->clockstart
, start
, (HEVENT
)ev
, &cookie
);
166 WaitForSingleObject(ev
, INFINITE
);
167 IReferenceClock_Unadvise(This
->clock
, cookie
);
172 This
->current_rstart
= start
;
173 This
->current_rstop
= stop
> start
? stop
: start
;
174 This
->current_jitter
= jitter
;
175 This
->is_dropped
= QualityControlRender_IsLate(This
, jitter
, start
, stop
);
176 TRACE("Dropped: %i %i %i %i\n", This
->is_dropped
, (int)(start
/10000), (int)(stop
/10000), (int)(jitter
/ 10000));
177 if (This
->is_dropped
) {
179 if (!This
->qos_handled
)
186 void QualityControlRender_DoQOS(QualityControlImpl
*priv
)
188 REFERENCE_TIME start
, stop
, jitter
, pt
, entered
, left
, duration
;
191 if (!priv
->clock
|| priv
->current_rstart
< 0)
194 start
= priv
->current_rstart
;
195 stop
= priv
->current_rstop
;
196 jitter
= priv
->current_jitter
;
199 /* this is the time the buffer entered the sink */
203 entered
= start
+ jitter
;
206 /* this is the time the buffer entered the sink */
207 entered
= start
+ jitter
;
208 /* this is the time the buffer left the sink */
209 left
= start
+ jitter
;
212 /* calculate duration of the buffer */
214 duration
= stop
- start
;
218 /* if we have the time when the last buffer left us, calculate
220 if (priv
->last_left
>= 0) {
221 if (entered
> priv
->last_left
) {
222 pt
= entered
- priv
->last_left
;
230 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
231 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
232 "duration %u.%03u, jitter %u.%03u\n", XTIME(start
), XTIME(entered
),
233 XTIME(left
), XTIME(pt
), XTIME(duration
), XTIME(jitter
));
235 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
236 XTIME(priv
->avg_duration
), XTIME(priv
->avg_pt
), priv
->avg_rate
);
239 /* collect running averages. for first observations, we copy the
241 if (priv
->avg_duration
< 0)
242 priv
->avg_duration
= duration
;
244 priv
->avg_duration
= UPDATE_RUNNING_AVG (priv
->avg_duration
, duration
);
246 if (priv
->avg_pt
< 0)
249 priv
->avg_pt
= UPDATE_RUNNING_AVG (priv
->avg_pt
, pt
);
251 if (priv
->avg_duration
!= 0)
253 (double)priv
->avg_pt
/
254 (double)priv
->avg_duration
;
258 if (priv
->last_left
>= 0) {
259 if (priv
->is_dropped
|| priv
->avg_rate
< 0.0) {
260 priv
->avg_rate
= rate
;
263 priv
->avg_rate
= UPDATE_RUNNING_AVG_N (priv
->avg_rate
, rate
);
265 priv
->avg_rate
= UPDATE_RUNNING_AVG_P (priv
->avg_rate
, rate
);
269 if (priv
->avg_rate
>= 0.0) {
272 /* if we have a valid rate, start sending QoS messages */
273 if (priv
->current_jitter
< 0) {
274 /* make sure we never go below 0 when adding the jitter to the
276 if (priv
->current_rstart
< -priv
->current_jitter
)
277 priv
->current_jitter
= -priv
->current_rstart
;
280 priv
->current_jitter
+= (priv
->current_rstop
- priv
->current_rstart
);
281 q
.Type
= (jitter
> 0 ? Famine
: Flood
);
282 q
.Proportion
= (LONG
)(1000. / priv
->avg_rate
);
283 if (q
.Proportion
< 200)
285 else if (q
.Proportion
> 5000)
287 q
.Late
= priv
->current_jitter
;
288 q
.TimeStamp
= priv
->current_rstart
;
289 TRACE("Late: %i from %i, rate: %g\n", (int)(q
.Late
/10000), (int)(q
.TimeStamp
/10000), 1./priv
->avg_rate
);
290 hr
= IQualityControl_Notify((IQualityControl
*)priv
, priv
->self
, q
);
291 priv
->qos_handled
= hr
== S_OK
;
294 /* record when this buffer will leave us */
295 priv
->last_left
= left
;
299 void QualityControlRender_BeginRender(QualityControlImpl
*This
) {
303 IReferenceClock_GetTime(This
->clock
, &This
->start
);
306 void QualityControlRender_EndRender(QualityControlImpl
*This
) {
307 REFERENCE_TIME elapsed
;
308 if (!This
->clock
|| This
->start
< 0 || FAILED(IReferenceClock_GetTime(This
->clock
, &This
->stop
)))
311 elapsed
= This
->start
- This
->stop
;
314 if (This
->avg_render
< 0)
315 This
->avg_render
= elapsed
;
317 This
->avg_render
= UPDATE_RUNNING_AVG (This
->avg_render
, elapsed
);