2010-11-05 23:19:10 +01:00
|
|
|
/*
|
|
|
|
* Quality Control Interfaces
|
|
|
|
*
|
2010-12-11 14:25:28 +01:00
|
|
|
* Copyright 2010 Maarten Lankhorst for CodeWeavers
|
2010-11-05 23:19:10 +01:00
|
|
|
*
|
2010-11-09 23:42:49 +01:00
|
|
|
* rendering qos functions based on, the original can be found at
|
|
|
|
* gstreamer/libs/gst/base/gstbasesink.c which has copyright notice:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
2010-11-05 23:19:10 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-04-02 14:52:15 +02:00
|
|
|
#include "strmbase_private.h"
|
2010-11-05 23:19:10 +01:00
|
|
|
|
2010-11-09 23:42:49 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc);
|
2010-11-05 23:19:10 +01:00
|
|
|
|
2019-10-28 00:28:03 +01:00
|
|
|
HRESULT QualityControlImpl_Create(struct strmbase_pin *pin, QualityControlImpl **ppv)
|
2016-05-17 20:43:36 +02:00
|
|
|
{
|
2012-04-02 14:52:15 +02:00
|
|
|
QualityControlImpl *This;
|
2016-05-17 21:02:49 +02:00
|
|
|
*ppv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(QualityControlImpl));
|
2012-04-16 08:53:47 +02:00
|
|
|
if (!*ppv)
|
2012-04-02 14:52:15 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
This = *ppv;
|
2019-10-28 00:28:03 +01:00
|
|
|
This->pin = pin;
|
2010-11-05 23:19:10 +01:00
|
|
|
This->tonotify = NULL;
|
2016-05-17 21:02:49 +02:00
|
|
|
This->current_rstart = This->current_rstop = -1;
|
2016-05-17 20:43:36 +02:00
|
|
|
TRACE("-> %p\n", This);
|
2012-04-02 14:52:15 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-19 10:57:52 +02:00
|
|
|
void QualityControlImpl_Destroy(QualityControlImpl *This)
|
2012-04-02 14:52:15 +02:00
|
|
|
{
|
2015-10-19 10:57:52 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,This);
|
2010-11-05 23:19:10 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 11:19:17 +02:00
|
|
|
static inline QualityControlImpl *impl_from_IQualityControl(IQualityControl *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, QualityControlImpl, IQualityControl_iface);
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
|
|
|
|
{
|
2016-07-18 11:19:17 +02:00
|
|
|
QualityControlImpl *This = impl_from_IQualityControl(iface);
|
2019-10-28 00:28:03 +01:00
|
|
|
return IBaseFilter_QueryInterface(&This->pin->filter->IBaseFilter_iface, riid, ppv);
|
2010-11-05 23:19:10 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface)
|
|
|
|
{
|
2016-07-18 11:19:17 +02:00
|
|
|
QualityControlImpl *This = impl_from_IQualityControl(iface);
|
2019-10-28 00:28:03 +01:00
|
|
|
return IBaseFilter_AddRef(&This->pin->filter->IBaseFilter_iface);
|
2010-11-05 23:19:10 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface)
|
|
|
|
{
|
2016-07-18 11:19:17 +02:00
|
|
|
QualityControlImpl *This = impl_from_IQualityControl(iface);
|
2019-10-28 00:28:03 +01:00
|
|
|
return IBaseFilter_Release(&This->pin->filter->IBaseFilter_iface);
|
2010-11-05 23:19:10 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
|
|
|
|
{
|
2016-07-18 11:19:17 +02:00
|
|
|
QualityControlImpl *This = impl_from_IQualityControl(iface);
|
2010-11-05 23:19:10 +01:00
|
|
|
HRESULT hr = S_FALSE;
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("iface %p, sender %p, type %#x, proportion %u, late %s, timestamp %s.\n",
|
|
|
|
iface, sender, qm.Type, qm.Proportion, debugstr_time(qm.Late), debugstr_time(qm.TimeStamp));
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2010-11-05 23:19:10 +01:00
|
|
|
if (This->tonotify)
|
2019-10-28 00:28:03 +01:00
|
|
|
return IQualityControl_Notify(This->tonotify, &This->pin->filter->IBaseFilter_iface, qm);
|
|
|
|
|
|
|
|
if (This->pin->peer)
|
|
|
|
{
|
|
|
|
IQualityControl *qc = NULL;
|
|
|
|
IPin_QueryInterface(This->pin->peer, &IID_IQualityControl, (void **)&qc);
|
|
|
|
if (qc)
|
|
|
|
{
|
|
|
|
hr = IQualityControl_Notify(qc, &This->pin->filter->IBaseFilter_iface, qm);
|
|
|
|
IQualityControl_Release(qc);
|
2010-11-05 23:19:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2010-11-05 23:19:10 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
|
|
|
|
{
|
2016-07-18 11:19:17 +02:00
|
|
|
QualityControlImpl *This = impl_from_IQualityControl(iface);
|
2016-05-17 20:43:36 +02:00
|
|
|
TRACE("%p %p\n", This, tonotify);
|
2010-11-05 23:19:10 +01:00
|
|
|
This->tonotify = tonotify;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2010-11-09 23:42:49 +01:00
|
|
|
|
|
|
|
/* Macros copied from gstreamer, weighted average between old average and new ones */
|
|
|
|
#define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
|
|
|
|
|
|
|
|
/* generic running average, this has a neutral window size */
|
|
|
|
#define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
|
|
|
|
|
|
|
|
/* the windows for these running averages are experimentally obtained.
|
2019-03-12 13:41:37 +01:00
|
|
|
* positive values get averaged more while negative values use a small
|
2010-11-09 23:42:49 +01:00
|
|
|
* window so we can react faster to badness. */
|
|
|
|
#define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
|
|
|
|
#define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart)
|
|
|
|
{
|
2010-11-09 23:42:49 +01:00
|
|
|
This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
|
|
|
|
This->clockstart = tStart;
|
|
|
|
This->avg_rate = -1.0;
|
2013-11-25 22:27:35 +01:00
|
|
|
This->rendered = This->dropped = 0;
|
|
|
|
This->is_dropped = FALSE;
|
|
|
|
This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
|
2010-11-09 23:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
|
|
|
|
REFERENCE_TIME start, REFERENCE_TIME stop)
|
|
|
|
{
|
|
|
|
REFERENCE_TIME max_lateness = 200000;
|
|
|
|
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("jitter %s, start %s, stop %s.\n", debugstr_time(jitter),
|
|
|
|
debugstr_time(start), debugstr_time(stop));
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2010-11-09 23:42:49 +01:00
|
|
|
/* we can add a valid stop time */
|
|
|
|
if (stop >= start)
|
|
|
|
max_lateness += stop;
|
|
|
|
else
|
|
|
|
max_lateness += start;
|
|
|
|
|
|
|
|
/* if the jitter bigger than duration and lateness we are too late */
|
|
|
|
if (start + jitter > max_lateness) {
|
|
|
|
WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
|
|
|
|
/* !!emergency!!, if we did not receive anything valid for more than a
|
|
|
|
* second, render it anyway so the user sees something */
|
|
|
|
if (This->last_in_time < 0 ||
|
|
|
|
start - This->last_in_time < 10000000)
|
|
|
|
return TRUE;
|
|
|
|
FIXME("A lot of buffers are being dropped.\n");
|
|
|
|
FIXME("There may be a timestamping problem, or this computer is too slow.\n");
|
|
|
|
}
|
|
|
|
This->last_in_time = start;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QualityControlRender_DoQOS(QualityControlImpl *priv)
|
|
|
|
{
|
|
|
|
REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
|
|
|
|
double rate;
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
TRACE("%p\n", priv);
|
|
|
|
|
2019-10-28 00:28:04 +01:00
|
|
|
if (!priv->pin->filter->pClock || priv->current_rstart < 0)
|
2010-11-09 23:42:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
start = priv->current_rstart;
|
|
|
|
stop = priv->current_rstop;
|
|
|
|
jitter = priv->current_jitter;
|
|
|
|
|
|
|
|
if (jitter < 0) {
|
|
|
|
/* this is the time the buffer entered the sink */
|
|
|
|
if (start < -jitter)
|
|
|
|
entered = 0;
|
|
|
|
else
|
|
|
|
entered = start + jitter;
|
|
|
|
left = start;
|
|
|
|
} else {
|
|
|
|
/* this is the time the buffer entered the sink */
|
|
|
|
entered = start + jitter;
|
|
|
|
/* this is the time the buffer left the sink */
|
|
|
|
left = start + jitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate duration of the buffer */
|
|
|
|
if (stop >= start)
|
|
|
|
duration = stop - start;
|
|
|
|
else
|
|
|
|
duration = 0;
|
|
|
|
|
|
|
|
/* if we have the time when the last buffer left us, calculate
|
|
|
|
* processing time */
|
|
|
|
if (priv->last_left >= 0) {
|
|
|
|
if (entered > priv->last_left) {
|
|
|
|
pt = entered - priv->last_left;
|
|
|
|
} else {
|
|
|
|
pt = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pt = priv->avg_pt;
|
|
|
|
}
|
|
|
|
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("start %s, entered %s, left %s, pt %s, duration %s, jitter %s.\n",
|
|
|
|
debugstr_time(start), debugstr_time(entered), debugstr_time(left),
|
|
|
|
debugstr_time(pt), debugstr_time(duration), debugstr_time(jitter));
|
2010-11-09 23:42:49 +01:00
|
|
|
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("average duration %s, average pt %s, average rate %.16e.\n",
|
|
|
|
debugstr_time(priv->avg_duration), debugstr_time(priv->avg_pt), priv->avg_rate);
|
2010-11-09 23:42:49 +01:00
|
|
|
|
|
|
|
/* collect running averages. for first observations, we copy the
|
|
|
|
* values */
|
|
|
|
if (priv->avg_duration < 0)
|
|
|
|
priv->avg_duration = duration;
|
|
|
|
else
|
|
|
|
priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
|
|
|
|
|
|
|
|
if (priv->avg_pt < 0)
|
|
|
|
priv->avg_pt = pt;
|
|
|
|
else
|
|
|
|
priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
|
|
|
|
|
|
|
|
if (priv->avg_duration != 0)
|
|
|
|
rate =
|
|
|
|
(double)priv->avg_pt /
|
|
|
|
(double)priv->avg_duration;
|
|
|
|
else
|
|
|
|
rate = 0.0;
|
|
|
|
|
|
|
|
if (priv->last_left >= 0) {
|
|
|
|
if (priv->is_dropped || priv->avg_rate < 0.0) {
|
|
|
|
priv->avg_rate = rate;
|
|
|
|
} else {
|
|
|
|
if (rate > 1.0)
|
|
|
|
priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
|
|
|
|
else
|
|
|
|
priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->avg_rate >= 0.0) {
|
|
|
|
HRESULT hr;
|
|
|
|
Quality q;
|
|
|
|
/* if we have a valid rate, start sending QoS messages */
|
|
|
|
if (priv->current_jitter < 0) {
|
|
|
|
/* make sure we never go below 0 when adding the jitter to the
|
|
|
|
* timestamp. */
|
|
|
|
if (priv->current_rstart < -priv->current_jitter)
|
|
|
|
priv->current_jitter = -priv->current_rstart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
priv->current_jitter += (priv->current_rstop - priv->current_rstart);
|
|
|
|
q.Type = (jitter > 0 ? Famine : Flood);
|
|
|
|
q.Proportion = (LONG)(1000. / priv->avg_rate);
|
2010-11-29 10:44:15 +01:00
|
|
|
if (q.Proportion < 200)
|
|
|
|
q.Proportion = 200;
|
|
|
|
else if (q.Proportion > 5000)
|
|
|
|
q.Proportion = 5000;
|
2010-11-09 23:42:49 +01:00
|
|
|
q.Late = priv->current_jitter;
|
|
|
|
q.TimeStamp = priv->current_rstart;
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("Late: %s from %s, rate: %g\n", debugstr_time(q.Late), debugstr_time(q.TimeStamp), 1./priv->avg_rate);
|
2019-10-28 00:28:03 +01:00
|
|
|
hr = IQualityControl_Notify(&priv->IQualityControl_iface, &priv->pin->filter->IBaseFilter_iface, q);
|
2010-11-09 23:42:49 +01:00
|
|
|
priv->qos_handled = hr == S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* record when this buffer will leave us */
|
|
|
|
priv->last_left = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-02 05:49:46 +02:00
|
|
|
void QualityControlRender_BeginRender(QualityControlImpl *This, REFERENCE_TIME start, REFERENCE_TIME stop)
|
2016-05-17 20:43:36 +02:00
|
|
|
{
|
2010-11-09 23:42:49 +01:00
|
|
|
This->start = -1;
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2019-07-02 05:49:46 +02:00
|
|
|
This->current_rstart = start;
|
|
|
|
This->current_rstop = max(stop, start);
|
|
|
|
|
|
|
|
if (start >= 0)
|
|
|
|
{
|
|
|
|
REFERENCE_TIME now;
|
2019-10-28 00:28:04 +01:00
|
|
|
IReferenceClock_GetTime(This->pin->filter->pClock, &now);
|
2019-07-02 05:49:46 +02:00
|
|
|
This->current_jitter = (now - This->clockstart) - start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
This->current_jitter = 0;
|
|
|
|
|
|
|
|
/* FIXME: This isn't correct; we don't drop samples, nor should. */
|
|
|
|
This->is_dropped = QualityControlRender_IsLate(This, This->current_jitter, start, stop);
|
2019-10-06 02:18:49 +02:00
|
|
|
TRACE("dropped %d, start %s, stop %s, jitter %s.\n", This->is_dropped,
|
|
|
|
debugstr_time(start), debugstr_time(stop), debugstr_time(This->current_jitter));
|
2019-07-02 05:49:46 +02:00
|
|
|
if (This->is_dropped)
|
|
|
|
This->dropped++;
|
|
|
|
else
|
|
|
|
This->rendered++;
|
|
|
|
|
2019-10-28 00:28:04 +01:00
|
|
|
if (!This->pin->filter->pClock)
|
2010-11-09 23:42:49 +01:00
|
|
|
return;
|
2016-05-17 20:43:36 +02:00
|
|
|
|
2019-10-28 00:28:04 +01:00
|
|
|
IReferenceClock_GetTime(This->pin->filter->pClock, &This->start);
|
2019-10-06 02:18:49 +02:00
|
|
|
|
|
|
|
TRACE("Starting at %s.\n", debugstr_time(This->start));
|
2010-11-09 23:42:49 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:36 +02:00
|
|
|
void QualityControlRender_EndRender(QualityControlImpl *This)
|
|
|
|
{
|
2010-11-09 23:42:49 +01:00
|
|
|
REFERENCE_TIME elapsed;
|
2016-05-17 20:43:36 +02:00
|
|
|
|
|
|
|
TRACE("%p\n", This);
|
|
|
|
|
2019-10-28 00:28:04 +01:00
|
|
|
if (!This->pin->filter->pClock || This->start < 0
|
|
|
|
|| FAILED(IReferenceClock_GetTime(This->pin->filter->pClock, &This->stop)))
|
2010-11-09 23:42:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
elapsed = This->start - This->stop;
|
|
|
|
if (elapsed < 0)
|
|
|
|
return;
|
|
|
|
if (This->avg_render < 0)
|
|
|
|
This->avg_render = elapsed;
|
|
|
|
else
|
|
|
|
This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
|
|
|
|
}
|