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"
32 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc
);
38 void QualityControlImpl_init(QualityControlImpl
*This
, IPin
*input
, IBaseFilter
*self
) {
41 This
->tonotify
= NULL
;
45 HRESULT WINAPI
QualityControlImpl_QueryInterface(IQualityControl
*iface
, REFIID riid
, void **ppv
) {
46 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
47 return IUnknown_QueryInterface(This
->self
, riid
, ppv
);
50 ULONG WINAPI
QualityControlImpl_AddRef(IQualityControl
*iface
) {
51 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
52 return IUnknown_AddRef(This
->self
);
55 ULONG WINAPI
QualityControlImpl_Release(IQualityControl
*iface
) {
56 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
57 return IUnknown_Release(This
->self
);
60 HRESULT WINAPI
QualityControlImpl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
62 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
64 return IQualityControl_Notify(This
->tonotify
, This
->self
, qm
);
67 IPin_ConnectedTo(This
->input
, &to
);
69 IQualityControl
*qc
= NULL
;
70 IUnknown_QueryInterface(to
, &IID_IQualityControl
, (void**)&qc
);
72 hr
= IQualityControl_Notify(qc
, This
->self
, qm
);
73 IQualityControl_Release(qc
);
81 HRESULT WINAPI
QualityControlImpl_SetSink(IQualityControl
*iface
, IQualityControl
*tonotify
) {
82 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
83 This
->tonotify
= tonotify
;
87 /* Macros copied from gstreamer, weighted average between old average and new ones */
88 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
90 /* generic running average, this has a neutral window size */
91 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
93 /* the windows for these running averages are experimentally obtained.
94 * possitive values get averaged more while negative values use a small
95 * window so we can react faster to badness. */
96 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
97 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
99 void QualityControlRender_Start(QualityControlImpl
*This
, REFERENCE_TIME tStart
) {
100 This
->avg_render
= This
->last_in_time
= This
->last_left
= This
->avg_duration
= This
->avg_pt
= -1;
101 This
->clockstart
= tStart
;
102 This
->avg_rate
= -1.0;
103 This
->rendered
= This
->dropped
= This
->is_dropped
= 0;
104 This
->qos_handled
= 1; /* Lie that will be corrected on first adjustment */
108 void QualityControlRender_SetClock(QualityControlImpl
*This
, IReferenceClock
*clock
) {
112 static BOOL
QualityControlRender_IsLate(QualityControlImpl
*This
, REFERENCE_TIME jitter
,
113 REFERENCE_TIME start
, REFERENCE_TIME stop
)
115 REFERENCE_TIME max_lateness
= 200000;
117 /* we can add a valid stop time */
119 max_lateness
+= stop
;
121 max_lateness
+= start
;
123 /* if the jitter bigger than duration and lateness we are too late */
124 if (start
+ jitter
> max_lateness
) {
125 WARN("buffer is too late %i > %i\n", (int)((start
+ jitter
)/10000), (int)(max_lateness
/10000));
126 /* !!emergency!!, if we did not receive anything valid for more than a
127 * second, render it anyway so the user sees something */
128 if (This
->last_in_time
< 0 ||
129 start
- This
->last_in_time
< 10000000)
131 FIXME("A lot of buffers are being dropped.\n");
132 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
134 This
->last_in_time
= start
;
138 HRESULT
QualityControlRender_WaitFor(QualityControlImpl
*This
, IMediaSample
*sample
, HANDLE ev
) {
139 REFERENCE_TIME start
= -1, stop
= -1, jitter
= 0;
140 This
->current_rstart
= This
->current_rstop
= -1;
141 This
->current_jitter
= 0;
142 if (!This
->clock
|| FAILED(IMediaSample_GetTime(sample
, &start
, &stop
)))
147 IReferenceClock_GetTime(This
->clock
, &now
);
148 now
-= This
->clockstart
;
150 jitter
= now
- start
;
151 if (jitter
<= -10000) {
153 IReferenceClock_AdviseTime(This
->clock
, This
->clockstart
, start
, (HEVENT
)ev
, &cookie
);
154 WaitForSingleObject(ev
, INFINITE
);
155 IReferenceClock_Unadvise(This
->clock
, cookie
);
160 This
->current_rstart
= start
;
161 This
->current_rstop
= stop
> start
? stop
: start
;
162 This
->current_jitter
= jitter
;
163 This
->is_dropped
= QualityControlRender_IsLate(This
, jitter
, start
, stop
);
164 TRACE("Dropped: %i %i %i %i\n", This
->is_dropped
, (int)(start
/10000), (int)(stop
/10000), (int)(jitter
/ 10000));
165 if (This
->is_dropped
) {
167 if (!This
->qos_handled
)
174 void QualityControlRender_DoQOS(QualityControlImpl
*priv
)
176 REFERENCE_TIME start
, stop
, jitter
, pt
, entered
, left
, duration
;
179 if (!priv
->clock
|| priv
->current_rstart
< 0)
182 start
= priv
->current_rstart
;
183 stop
= priv
->current_rstop
;
184 jitter
= priv
->current_jitter
;
187 /* this is the time the buffer entered the sink */
191 entered
= start
+ jitter
;
194 /* this is the time the buffer entered the sink */
195 entered
= start
+ jitter
;
196 /* this is the time the buffer left the sink */
197 left
= start
+ jitter
;
200 /* calculate duration of the buffer */
202 duration
= stop
- start
;
206 /* if we have the time when the last buffer left us, calculate
208 if (priv
->last_left
>= 0) {
209 if (entered
> priv
->last_left
) {
210 pt
= entered
- priv
->last_left
;
218 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
219 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
220 "duration %u.%03u, jitter %u.%03u\n", XTIME(start
), XTIME(entered
),
221 XTIME(left
), XTIME(pt
), XTIME(duration
), XTIME(jitter
));
223 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
224 XTIME(priv
->avg_duration
), XTIME(priv
->avg_pt
), priv
->avg_rate
);
227 /* collect running averages. for first observations, we copy the
229 if (priv
->avg_duration
< 0)
230 priv
->avg_duration
= duration
;
232 priv
->avg_duration
= UPDATE_RUNNING_AVG (priv
->avg_duration
, duration
);
234 if (priv
->avg_pt
< 0)
237 priv
->avg_pt
= UPDATE_RUNNING_AVG (priv
->avg_pt
, pt
);
239 if (priv
->avg_duration
!= 0)
241 (double)priv
->avg_pt
/
242 (double)priv
->avg_duration
;
246 if (priv
->last_left
>= 0) {
247 if (priv
->is_dropped
|| priv
->avg_rate
< 0.0) {
248 priv
->avg_rate
= rate
;
251 priv
->avg_rate
= UPDATE_RUNNING_AVG_N (priv
->avg_rate
, rate
);
253 priv
->avg_rate
= UPDATE_RUNNING_AVG_P (priv
->avg_rate
, rate
);
257 if (priv
->avg_rate
>= 0.0) {
260 /* if we have a valid rate, start sending QoS messages */
261 if (priv
->current_jitter
< 0) {
262 /* make sure we never go below 0 when adding the jitter to the
264 if (priv
->current_rstart
< -priv
->current_jitter
)
265 priv
->current_jitter
= -priv
->current_rstart
;
268 priv
->current_jitter
+= (priv
->current_rstop
- priv
->current_rstart
);
269 q
.Type
= (jitter
> 0 ? Famine
: Flood
);
270 q
.Proportion
= (LONG
)(1000. / priv
->avg_rate
);
271 if (q
.Proportion
< 200)
273 else if (q
.Proportion
> 5000)
275 q
.Late
= priv
->current_jitter
;
276 q
.TimeStamp
= priv
->current_rstart
;
277 TRACE("Late: %i from %i, rate: %g\n", (int)(q
.Late
/10000), (int)(q
.TimeStamp
/10000), 1./priv
->avg_rate
);
278 hr
= IQualityControl_Notify((IQualityControl
*)priv
, priv
->self
, q
);
279 priv
->qos_handled
= hr
== S_OK
;
282 /* record when this buffer will leave us */
283 priv
->last_left
= left
;
287 void QualityControlRender_BeginRender(QualityControlImpl
*This
) {
291 IReferenceClock_GetTime(This
->clock
, &This
->start
);
294 void QualityControlRender_EndRender(QualityControlImpl
*This
) {
295 REFERENCE_TIME elapsed
;
296 if (!This
->clock
|| This
->start
< 0 || FAILED(IReferenceClock_GetTime(This
->clock
, &This
->stop
)))
299 elapsed
= This
->start
- This
->stop
;
302 if (This
->avg_render
< 0)
303 This
->avg_render
= elapsed
;
305 This
->avg_render
= UPDATE_RUNNING_AVG (This
->avg_render
, elapsed
);