+RESULT eDVBResourceManager::connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection)
+{
+ connection = new eConnection((eDVBResourceManager*)this, m_channelAdded.connect(channelAdded));
+ return 0;
+}
+
+bool eDVBResourceManager::canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm)
+{
+ ePtr<eDVBRegisteredFrontend> best;
+ int bestval = 0;
+
+ for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+ if (!i->m_inuse)
+ {
+ int c = i->m_frontend->isCompatibleWith(feparm);
+ if (c > bestval)
+ bestval = c;
+ }
+
+ return bestval>0;
+}
+
+bool eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID& ignore)
+{
+ bool ret=true;
+ if (m_cached_channel)
+ {
+ eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
+ if(channelid==cache_chan->getChannelID())
+ return ret;
+ }
+
+ /* first, check if a channel is already existing. */
+// eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
+ for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
+ {
+// eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
+ if (i->m_channel_id == channelid)
+ {
+// eDebug("found shared channel..");
+ return ret;
+ }
+ }
+
+ int *decremented_cached_channel_fe_usecount=NULL,
+ *decremented_fe_usecount=NULL;
+
+ for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
+ {
+// eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
+ if (i->m_channel_id == ignore)
+ {
+ eDVBChannel *channel = (eDVBChannel*) &(*i->m_channel);
+ // one eUsePtr<iDVBChannel> is used in eDVBServicePMTHandler
+ // another on eUsePtr<iDVBChannel> is used in the eDVBScan instance used in eDVBServicePMTHandler (for SDT scan)
+ // so we must check here if usecount is 3 (when the channel is equal to the cached channel)
+ // or 2 when the cached channel is not equal to the compared channel
+ if (channel == &(*m_cached_channel) ? channel->getUseCount() == 3 : channel->getUseCount() == 2) // channel only used once..
+ {
+ ePtr<iDVBFrontend> fe;
+ if (!i->m_channel->getFrontend(fe))
+ {
+ for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
+ {
+ if ( &(*fe) == &(*ii->m_frontend) )
+ {
+ --ii->m_inuse;
+ decremented_fe_usecount = &ii->m_inuse;
+ if (channel == &(*m_cached_channel))
+ decremented_cached_channel_fe_usecount = decremented_fe_usecount;
+ break;
+ }
+ }
+ }
+ }
+ break;
+ }
+ }
+
+ if (!decremented_cached_channel_fe_usecount)
+ {
+ if (m_cached_channel)
+ {
+ eDVBChannel *channel = (eDVBChannel*) &(*m_cached_channel);
+ if (channel->getUseCount() == 1)
+ {
+ ePtr<iDVBFrontend> fe;
+ if (!channel->getFrontend(fe))
+ {
+ for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
+ {
+ if ( &(*fe) == &(*ii->m_frontend) )
+ {
+ --ii->m_inuse;
+ decremented_cached_channel_fe_usecount = &ii->m_inuse;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ else
+ decremented_cached_channel_fe_usecount=NULL;
+
+ ePtr<iDVBFrontendParameters> feparm;
+
+ if (!m_list)
+ {
+ eDebug("no channel list set!");
+ ret = false;
+ goto error;
+ }
+
+ if (m_list->getChannelFrontendData(channelid, feparm))
+ {
+ eDebug("channel not found!");
+ ret = false;
+ goto error;
+ }
+
+ ret = canAllocateFrontend(feparm);
+
+error:
+ if (decremented_fe_usecount)
+ ++(*decremented_fe_usecount);
+ if (decremented_cached_channel_fe_usecount)
+ ++(*decremented_cached_channel_fe_usecount);
+
+ return ret;
+}
+