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 #include "strmbase_private.h"
28 HRESULT
QualityControlImpl_Create(IPin
*input
, IBaseFilter
*self
, QualityControlImpl
**ppv
) {
29 QualityControlImpl
*This
;
30 *ppv
= HeapAlloc(GetProcessHeap(),0,sizeof(QualityControlImpl
));
36 This
->tonotify
= NULL
;
41 void QualityControlImpl_Destroy(QualityControlImpl
*This
)
43 HeapFree(GetProcessHeap(),0,This
);
46 HRESULT WINAPI
QualityControlImpl_QueryInterface(IQualityControl
*iface
, REFIID riid
, void **ppv
) {
47 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
48 return IBaseFilter_QueryInterface(This
->self
, riid
, ppv
);
51 ULONG WINAPI
QualityControlImpl_AddRef(IQualityControl
*iface
) {
52 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
53 return IBaseFilter_AddRef(This
->self
);
56 ULONG WINAPI
QualityControlImpl_Release(IQualityControl
*iface
) {
57 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
58 return IBaseFilter_Release(This
->self
);
61 HRESULT WINAPI
QualityControlImpl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
63 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
65 return IQualityControl_Notify(This
->tonotify
, This
->self
, qm
);
68 IPin_ConnectedTo(This
->input
, &to
);
70 IQualityControl
*qc
= NULL
;
71 IPin_QueryInterface(to
, &IID_IQualityControl
, (void**)&qc
);
73 hr
= IQualityControl_Notify(qc
, This
->self
, qm
);
74 IQualityControl_Release(qc
);
82 HRESULT WINAPI
QualityControlImpl_SetSink(IQualityControl
*iface
, IQualityControl
*tonotify
) {
83 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
84 This
->tonotify
= tonotify
;
88 /* Macros copied from gstreamer, weighted average between old average and new ones */
89 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
91 /* generic running average, this has a neutral window size */
92 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
94 /* the windows for these running averages are experimentally obtained.
95 * possitive values get averaged more while negative values use a small
96 * window so we can react faster to badness. */
97 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
98 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
100 void QualityControlRender_Start(QualityControlImpl
*This
, REFERENCE_TIME tStart
) {
101 This
->avg_render
= This
->last_in_time
= This
->last_left
= This
->avg_duration
= This
->avg_pt
= -1;
102 This
->clockstart
= tStart
;
103 This
->avg_rate
= -1.0;
104 This
->rendered
= This
->dropped
= 0;
105 This
->is_dropped
= FALSE
;
106 This
->qos_handled
= TRUE
; /* Lie that will be corrected on first adjustment */
110 void QualityControlRender_SetClock(QualityControlImpl
*This
, IReferenceClock
*clock
) {
114 static BOOL
QualityControlRender_IsLate(QualityControlImpl
*This
, REFERENCE_TIME jitter
,
115 REFERENCE_TIME start
, REFERENCE_TIME stop
)
117 REFERENCE_TIME max_lateness
= 200000;
119 /* we can add a valid stop time */
121 max_lateness
+= stop
;
123 max_lateness
+= start
;
125 /* if the jitter bigger than duration and lateness we are too late */
126 if (start
+ jitter
> max_lateness
) {
127 WARN("buffer is too late %i > %i\n", (int)((start
+ jitter
)/10000), (int)(max_lateness
/10000));
128 /* !!emergency!!, if we did not receive anything valid for more than a
129 * second, render it anyway so the user sees something */
130 if (This
->last_in_time
< 0 ||
131 start
- This
->last_in_time
< 10000000)
133 FIXME("A lot of buffers are being dropped.\n");
134 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
136 This
->last_in_time
= start
;
140 HRESULT
QualityControlRender_WaitFor(QualityControlImpl
*This
, IMediaSample
*sample
, HANDLE ev
) {
141 REFERENCE_TIME start
= -1, stop
= -1, jitter
= 0;
142 This
->current_rstart
= This
->current_rstop
= -1;
143 This
->current_jitter
= 0;
144 if (!This
->clock
|| FAILED(IMediaSample_GetTime(sample
, &start
, &stop
)))
149 IReferenceClock_GetTime(This
->clock
, &now
);
150 now
-= This
->clockstart
;
152 jitter
= now
- start
;
153 if (jitter
<= -10000) {
155 IReferenceClock_AdviseTime(This
->clock
, This
->clockstart
, start
, (HEVENT
)ev
, &cookie
);
156 WaitForSingleObject(ev
, INFINITE
);
157 IReferenceClock_Unadvise(This
->clock
, cookie
);
162 This
->current_rstart
= start
;
163 This
->current_rstop
= stop
> start
? stop
: start
;
164 This
->current_jitter
= jitter
;
165 This
->is_dropped
= QualityControlRender_IsLate(This
, jitter
, start
, stop
);
166 TRACE("Dropped: %i %i %i %i\n", This
->is_dropped
, (int)(start
/10000), (int)(stop
/10000), (int)(jitter
/ 10000));
167 if (This
->is_dropped
) {
169 if (!This
->qos_handled
)
176 void QualityControlRender_DoQOS(QualityControlImpl
*priv
)
178 REFERENCE_TIME start
, stop
, jitter
, pt
, entered
, left
, duration
;
181 if (!priv
->clock
|| priv
->current_rstart
< 0)
184 start
= priv
->current_rstart
;
185 stop
= priv
->current_rstop
;
186 jitter
= priv
->current_jitter
;
189 /* this is the time the buffer entered the sink */
193 entered
= start
+ jitter
;
196 /* this is the time the buffer entered the sink */
197 entered
= start
+ jitter
;
198 /* this is the time the buffer left the sink */
199 left
= start
+ jitter
;
202 /* calculate duration of the buffer */
204 duration
= stop
- start
;
208 /* if we have the time when the last buffer left us, calculate
210 if (priv
->last_left
>= 0) {
211 if (entered
> priv
->last_left
) {
212 pt
= entered
- priv
->last_left
;
220 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
221 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
222 "duration %u.%03u, jitter %u.%03u\n", XTIME(start
), XTIME(entered
),
223 XTIME(left
), XTIME(pt
), XTIME(duration
), XTIME(jitter
));
225 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
226 XTIME(priv
->avg_duration
), XTIME(priv
->avg_pt
), priv
->avg_rate
);
229 /* collect running averages. for first observations, we copy the
231 if (priv
->avg_duration
< 0)
232 priv
->avg_duration
= duration
;
234 priv
->avg_duration
= UPDATE_RUNNING_AVG (priv
->avg_duration
, duration
);
236 if (priv
->avg_pt
< 0)
239 priv
->avg_pt
= UPDATE_RUNNING_AVG (priv
->avg_pt
, pt
);
241 if (priv
->avg_duration
!= 0)
243 (double)priv
->avg_pt
/
244 (double)priv
->avg_duration
;
248 if (priv
->last_left
>= 0) {
249 if (priv
->is_dropped
|| priv
->avg_rate
< 0.0) {
250 priv
->avg_rate
= rate
;
253 priv
->avg_rate
= UPDATE_RUNNING_AVG_N (priv
->avg_rate
, rate
);
255 priv
->avg_rate
= UPDATE_RUNNING_AVG_P (priv
->avg_rate
, rate
);
259 if (priv
->avg_rate
>= 0.0) {
262 /* if we have a valid rate, start sending QoS messages */
263 if (priv
->current_jitter
< 0) {
264 /* make sure we never go below 0 when adding the jitter to the
266 if (priv
->current_rstart
< -priv
->current_jitter
)
267 priv
->current_jitter
= -priv
->current_rstart
;
270 priv
->current_jitter
+= (priv
->current_rstop
- priv
->current_rstart
);
271 q
.Type
= (jitter
> 0 ? Famine
: Flood
);
272 q
.Proportion
= (LONG
)(1000. / priv
->avg_rate
);
273 if (q
.Proportion
< 200)
275 else if (q
.Proportion
> 5000)
277 q
.Late
= priv
->current_jitter
;
278 q
.TimeStamp
= priv
->current_rstart
;
279 TRACE("Late: %i from %i, rate: %g\n", (int)(q
.Late
/10000), (int)(q
.TimeStamp
/10000), 1./priv
->avg_rate
);
280 hr
= IQualityControl_Notify((IQualityControl
*)priv
, priv
->self
, q
);
281 priv
->qos_handled
= hr
== S_OK
;
284 /* record when this buffer will leave us */
285 priv
->last_left
= left
;
289 void QualityControlRender_BeginRender(QualityControlImpl
*This
) {
293 IReferenceClock_GetTime(This
->clock
, &This
->start
);
296 void QualityControlRender_EndRender(QualityControlImpl
*This
) {
297 REFERENCE_TIME elapsed
;
298 if (!This
->clock
|| This
->start
< 0 || FAILED(IReferenceClock_GetTime(This
->clock
, &This
->stop
)))
301 elapsed
= This
->start
- This
->stop
;
304 if (This
->avg_render
< 0)
305 This
->avg_render
= elapsed
;
307 This
->avg_render
= UPDATE_RUNNING_AVG (This
->avg_render
, elapsed
);