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 void QualityControlImpl_Destroy(QualityControlImpl
*This
)
54 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
= 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
) {
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 */
132 max_lateness
+= stop
;
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)
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
;
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
)))
160 IReferenceClock_GetTime(This
->clock
, &now
);
161 now
-= This
->clockstart
;
163 jitter
= now
- start
;
164 if (jitter
<= -10000) {
166 IReferenceClock_AdviseTime(This
->clock
, This
->clockstart
, start
, (HEVENT
)ev
, &cookie
);
167 WaitForSingleObject(ev
, INFINITE
);
168 IReferenceClock_Unadvise(This
->clock
, cookie
);
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
) {
180 if (!This
->qos_handled
)
187 void QualityControlRender_DoQOS(QualityControlImpl
*priv
)
189 REFERENCE_TIME start
, stop
, jitter
, pt
, entered
, left
, duration
;
192 if (!priv
->clock
|| priv
->current_rstart
< 0)
195 start
= priv
->current_rstart
;
196 stop
= priv
->current_rstop
;
197 jitter
= priv
->current_jitter
;
200 /* this is the time the buffer entered the sink */
204 entered
= start
+ jitter
;
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 */
215 duration
= stop
- start
;
219 /* if we have the time when the last buffer left us, calculate
221 if (priv
->last_left
>= 0) {
222 if (entered
> priv
->last_left
) {
223 pt
= entered
- priv
->last_left
;
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
);
240 /* collect running averages. for first observations, we copy the
242 if (priv
->avg_duration
< 0)
243 priv
->avg_duration
= duration
;
245 priv
->avg_duration
= UPDATE_RUNNING_AVG (priv
->avg_duration
, duration
);
247 if (priv
->avg_pt
< 0)
250 priv
->avg_pt
= UPDATE_RUNNING_AVG (priv
->avg_pt
, pt
);
252 if (priv
->avg_duration
!= 0)
254 (double)priv
->avg_pt
/
255 (double)priv
->avg_duration
;
259 if (priv
->last_left
>= 0) {
260 if (priv
->is_dropped
|| priv
->avg_rate
< 0.0) {
261 priv
->avg_rate
= rate
;
264 priv
->avg_rate
= UPDATE_RUNNING_AVG_N (priv
->avg_rate
, rate
);
266 priv
->avg_rate
= UPDATE_RUNNING_AVG_P (priv
->avg_rate
, rate
);
270 if (priv
->avg_rate
>= 0.0) {
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
277 if (priv
->current_rstart
< -priv
->current_jitter
)
278 priv
->current_jitter
= -priv
->current_rstart
;
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)
286 else if (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
) {
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
)))
312 elapsed
= This
->start
- This
->stop
;
315 if (This
->avg_render
< 0)
316 This
->avg_render
= elapsed
;
318 This
->avg_render
= UPDATE_RUNNING_AVG (This
->avg_render
, elapsed
);