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
;
152 /* AdviseTime is bugged, so don't use it at all */
153 if (jitter
< -200000) {
155 IReferenceClock_AdviseTime(This
->clock
, clockstart
, start
, ev
, &cookie
);
156 WaitForSingleObject(ev
, INFINITE
);
157 IReferenceClock_Unadvise(This
->clock
, cookie
);
161 TRACE("Sleeping for %i ms\n", (int)-jitter
/10000);
162 WaitForSingleObject(ev
, -jitter
/10000);
167 This
->current_rstart
= start
;
168 This
->current_rstop
= stop
> start
? stop
: start
;
169 This
->current_jitter
= jitter
;
170 This
->is_dropped
= QualityControlRender_IsLate(This
, jitter
, start
, stop
);
171 TRACE("Dropped: %i %i %i %i\n", This
->is_dropped
, (int)(start
/10000), (int)(stop
/10000), (int)(jitter
/ 10000));
172 if (This
->is_dropped
) {
174 if (!This
->qos_handled
)
181 void QualityControlRender_DoQOS(QualityControlImpl
*priv
)
183 REFERENCE_TIME start
, stop
, jitter
, pt
, entered
, left
, duration
;
189 start
= priv
->current_rstart
;
190 stop
= priv
->current_rstop
;
191 jitter
= priv
->current_jitter
;
194 /* this is the time the buffer entered the sink */
198 entered
= start
+ jitter
;
201 /* this is the time the buffer entered the sink */
202 entered
= start
+ jitter
;
203 /* this is the time the buffer left the sink */
204 left
= start
+ jitter
;
207 /* calculate duration of the buffer */
209 duration
= stop
- start
;
213 /* if we have the time when the last buffer left us, calculate
215 if (priv
->last_left
>= 0) {
216 if (entered
> priv
->last_left
) {
217 pt
= entered
- priv
->last_left
;
225 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
226 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
227 "duration %u.%03u, jitter %u.%03u\n", XTIME(start
), XTIME(entered
),
228 XTIME(left
), XTIME(pt
), XTIME(duration
), XTIME(jitter
));
230 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
231 XTIME(priv
->avg_duration
), XTIME(priv
->avg_pt
), priv
->avg_rate
);
234 /* collect running averages. for first observations, we copy the
236 if (priv
->avg_duration
< 0)
237 priv
->avg_duration
= duration
;
239 priv
->avg_duration
= UPDATE_RUNNING_AVG (priv
->avg_duration
, duration
);
241 if (priv
->avg_pt
< 0)
244 priv
->avg_pt
= UPDATE_RUNNING_AVG (priv
->avg_pt
, pt
);
246 if (priv
->avg_duration
!= 0)
248 (double)priv
->avg_pt
/
249 (double)priv
->avg_duration
;
253 if (priv
->last_left
>= 0) {
254 if (priv
->is_dropped
|| priv
->avg_rate
< 0.0) {
255 priv
->avg_rate
= rate
;
258 priv
->avg_rate
= UPDATE_RUNNING_AVG_N (priv
->avg_rate
, rate
);
260 priv
->avg_rate
= UPDATE_RUNNING_AVG_P (priv
->avg_rate
, rate
);
264 if (priv
->avg_rate
>= 0.0) {
267 /* if we have a valid rate, start sending QoS messages */
268 if (priv
->current_jitter
< 0) {
269 /* make sure we never go below 0 when adding the jitter to the
271 if (priv
->current_rstart
< -priv
->current_jitter
)
272 priv
->current_jitter
= -priv
->current_rstart
;
275 priv
->current_jitter
+= (priv
->current_rstop
- priv
->current_rstart
);
276 q
.Type
= (jitter
> 0 ? Famine
: Flood
);
277 q
.Proportion
= (LONG
)(1000. / priv
->avg_rate
);
278 if (q
.Proportion
< 500)
280 else if (q
.Proportion
> 2000)
282 q
.Late
= priv
->current_jitter
;
283 q
.TimeStamp
= priv
->current_rstart
;
284 TRACE("Late: %i from %i, rate: %g\n", (int)(q
.Late
/10000), (int)(q
.TimeStamp
/10000), 1./priv
->avg_rate
);
285 hr
= IQualityControl_Notify((IQualityControl
*)priv
, priv
->self
, q
);
286 priv
->qos_handled
= hr
== S_OK
;
289 /* record when this buffer will leave us */
290 priv
->last_left
= left
;
294 void QualityControlRender_BeginRender(QualityControlImpl
*This
) {
298 IReferenceClock_GetTime(This
->clock
, &This
->start
);
301 void QualityControlRender_EndRender(QualityControlImpl
*This
) {
302 REFERENCE_TIME elapsed
;
303 if (!This
->clock
|| This
->start
< 0 || FAILED(IReferenceClock_GetTime(This
->clock
, &This
->stop
)))
306 elapsed
= This
->start
- This
->stop
;
309 if (This
->avg_render
< 0)
310 This
->avg_render
= elapsed
;
312 This
->avg_render
= UPDATE_RUNNING_AVG (This
->avg_render
, elapsed
);