Handle resource requests through singleton.

Many applications have multiple objects dealing with audio, and each
needs to request audio resources from resource policy manager. Resource
policy manager handles audio resources per-manager. Each ResourceSet
instance creates new manager, which causes applications having multiple
audio objects to fight for the audio resources internally, even though
the streams are played from the same process id.

To overcome this in QtMultimedia applications, handle all ResourceSet
operations through singleton. This way one client has only one manager
id registered, and resource acquiring and releasing can be limited to
only when no resources are acquired or all resources are freed. To
reduce unnecessary noise from resource policy plugin to the clients,
keep track of client states and only notify clients which themselves
request for resources etc.

Change-Id: Ifa4488a9f6298a3f601399e9d339f7bd819be757
Done-with: Juho Hämäläinen <juho.hamalainen@tieto.com>
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
This commit is contained in:
Robin Burchell
2014-06-03 11:07:23 +02:00
committed by The Qt Project
parent 7e41e842f8
commit ae567c3f35
5 changed files with 440 additions and 79 deletions

View File

@@ -40,99 +40,62 @@
**
****************************************************************************/
#include <QGlobalStatic>
#include <policy/resource.h>
#include <policy/resources.h>
#include <policy/resource-set.h>
#include "resourcepolicyimpl.h"
#include "resourcepolicyint.h"
Q_GLOBAL_STATIC(ResourcePolicyInt, globalResourcePolicyInt);
ResourcePolicyImpl::ResourcePolicyImpl(QObject *parent)
: QMediaPlayerResourceSetInterface(parent)
, m_status(Initial)
, m_videoEnabled(false)
{
m_resourceSet = new ResourcePolicy::ResourceSet("player", this);
m_resourceSet->setAlwaysReply();
ResourcePolicyInt *set = globalResourcePolicyInt;
set->addClient(this);
}
ResourcePolicy::AudioResource *audioResource = new ResourcePolicy::AudioResource("player");
audioResource->setProcessID(QCoreApplication::applicationPid());
audioResource->setStreamTag("media.name", "*");
m_resourceSet->addResourceObject(audioResource);
m_resourceSet->update();
connect(m_resourceSet, SIGNAL(resourcesGranted(QList<ResourcePolicy::ResourceType>)),
this, SLOT(handleResourcesGranted()));
connect(m_resourceSet, SIGNAL(resourcesDenied()),
this, SLOT(handleResourcesDenied()));
connect(m_resourceSet, SIGNAL(lostResources()),
this, SLOT(handleResourcesLost()));
connect(m_resourceSet, SIGNAL(resourcesReleasedByManager()),
this, SLOT(handleResourcesLost()));
ResourcePolicyImpl::~ResourcePolicyImpl()
{
ResourcePolicyInt *set = globalResourcePolicyInt;
set->removeClient(this);
}
bool ResourcePolicyImpl::isVideoEnabled() const
{
return m_videoEnabled;
ResourcePolicyInt *set = globalResourcePolicyInt;
return set->isVideoEnabled(this);
}
void ResourcePolicyImpl::setVideoEnabled(bool videoEnabled)
{
if (m_videoEnabled != videoEnabled) {
m_videoEnabled = videoEnabled;
if (videoEnabled)
m_resourceSet->addResource(ResourcePolicy::VideoPlaybackType);
else
m_resourceSet->deleteResource(ResourcePolicy::VideoPlaybackType);
m_resourceSet->update();
}
ResourcePolicyInt *set = globalResourcePolicyInt;
set->setVideoEnabled(this, videoEnabled);
}
void ResourcePolicyImpl::acquire()
{
m_status = RequestedResource;
m_resourceSet->acquire();
ResourcePolicyInt *set = globalResourcePolicyInt;
set->acquire(this);
}
void ResourcePolicyImpl::release()
{
m_resourceSet->release();
m_status = Initial;
ResourcePolicyInt *set = globalResourcePolicyInt;
set->release(this);
}
bool ResourcePolicyImpl::isGranted() const
{
return m_status == GrantedResource;
ResourcePolicyInt *set = globalResourcePolicyInt;
return set->isGranted(this);
}
bool ResourcePolicyImpl::isAvailable() const
{
// TODO: is this used? what is it for?
qWarning() << Q_FUNC_INFO << "Stub";
return true;
ResourcePolicyInt *set = globalResourcePolicyInt;
return set->isAvailable();
}
void ResourcePolicyImpl::handleResourcesGranted()
{
m_status = GrantedResource;
emit resourcesGranted();
}
void ResourcePolicyImpl::handleResourcesDenied()
{
m_status = Initial;
emit resourcesDenied();
}
void ResourcePolicyImpl::handleResourcesLost()
{
if (m_status != Initial) {
m_status = Initial;
emit resourcesLost();
}
m_resourceSet->release();
}