1 #include <lib/base/eerror.h>
2 #include <lib/base/filepush.h>
3 #include <lib/dvb/idvb.h>
4 #include <lib/dvb/dvb.h>
5 #include <lib/dvb/sec.h>
12 #include <sys/ioctl.h>
14 DEFINE_REF(eDVBRegisteredFrontend);
15 DEFINE_REF(eDVBRegisteredDemux);
17 DEFINE_REF(eDVBAllocatedFrontend);
19 eDVBAllocatedFrontend::eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe): m_fe(fe)
24 eDVBAllocatedFrontend::~eDVBAllocatedFrontend()
29 DEFINE_REF(eDVBAllocatedDemux);
31 eDVBAllocatedDemux::eDVBAllocatedDemux(eDVBRegisteredDemux *demux): m_demux(demux)
36 eDVBAllocatedDemux::~eDVBAllocatedDemux()
41 DEFINE_REF(eDVBResourceManager);
43 eDVBResourceManager *eDVBResourceManager::instance;
45 eDVBResourceManager::eDVBResourceManager()
49 m_sec = new eDVBSatelliteEquipmentControl(m_frontend);
53 /* search available adapters... */
58 while (eDVBAdapterLinux::exist(num_adapter))
60 addAdapter(new eDVBAdapterLinux(num_adapter));
64 eDebug("found %d adapter, %d frontends and %d demux",
65 m_adapter.size(), m_frontend.size(), m_demux.size());
69 DEFINE_REF(eDVBAdapterLinux);
70 eDVBAdapterLinux::eDVBAdapterLinux(int nr): m_nr(nr)
75 eDebug("scanning for frontends..");
80 #if HAVE_DVB_API_VERSION < 3
81 sprintf(filename, "/dev/dvb/card%d/frontend%d", m_nr, num_fe);
83 sprintf(filename, "/dev/dvb/adapter%d/frontend%d", m_nr, num_fe);
85 if (stat(filename, &s))
87 ePtr<eDVBFrontend> fe;
90 fe = new eDVBFrontend(m_nr, num_fe, ok);
92 m_frontend.push_back(fe);
102 #if HAVE_DVB_API_VERSION < 3
103 sprintf(filename, "/dev/dvb/card%d/demux%d", m_nr, num_demux);
105 sprintf(filename, "/dev/dvb/adapter%d/demux%d", m_nr, num_demux);
107 if (stat(filename, &s))
109 ePtr<eDVBDemux> demux;
111 demux = new eDVBDemux(m_nr, num_demux);
112 m_demux.push_back(demux);
118 int eDVBAdapterLinux::getNumDemux()
120 return m_demux.size();
123 RESULT eDVBAdapterLinux::getDemux(ePtr<eDVBDemux> &demux, int nr)
125 eSmartPtrList<eDVBDemux>::iterator i(m_demux.begin());
126 while (nr && (i != m_demux.end()))
132 if (i != m_demux.end())
140 int eDVBAdapterLinux::getNumFrontends()
142 return m_frontend.size();
145 RESULT eDVBAdapterLinux::getFrontend(ePtr<eDVBFrontend> &fe, int nr)
147 eSmartPtrList<eDVBFrontend>::iterator i(m_frontend.begin());
148 while (nr && (i != m_frontend.end()))
154 if (i != m_frontend.end())
162 int eDVBAdapterLinux::exist(int nr)
166 #if HAVE_DVB_API_VERSION < 3
167 sprintf(filename, "/dev/dvb/card%d", nr);
169 sprintf(filename, "/dev/dvb/adapter%d", nr);
171 if (!stat(filename, &s))
176 eDVBResourceManager::~eDVBResourceManager()
178 if (instance == this)
182 void eDVBResourceManager::addAdapter(iDVBAdapter *adapter)
184 int num_fe = adapter->getNumFrontends();
185 int num_demux = adapter->getNumDemux();
187 m_adapter.push_back(adapter);
190 for (i=0; i<num_demux; ++i)
192 ePtr<eDVBDemux> demux;
193 if (!adapter->getDemux(demux, i))
194 m_demux.push_back(new eDVBRegisteredDemux(demux, adapter));
197 for (i=0; i<num_fe; ++i)
199 ePtr<eDVBFrontend> frontend;
201 if (!adapter->getFrontend(frontend, i))
203 frontend->setSEC(m_sec);
204 m_frontend.push_back(new eDVBRegisteredFrontend(frontend, adapter));
209 RESULT eDVBResourceManager::allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm)
211 ePtr<eDVBRegisteredFrontend> best;
214 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
217 int c = i->m_frontend->isCompatibleWith(feparm);
227 fe = new eDVBAllocatedFrontend(best);
236 RESULT eDVBResourceManager::allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int nr)
238 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i, --nr)
239 if ((!nr) && !i->m_inuse)
241 fe = new eDVBAllocatedFrontend(i);
249 RESULT eDVBResourceManager::allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int cap)
251 /* find first unused demux which is on same adapter as frontend (or any, if PVR)
252 never use the first one unless we need a decoding demux. */
254 eDebug("allocate demux");
255 eSmartPtrList<eDVBRegisteredDemux>::iterator i(m_demux.begin());
257 if (i == m_demux.end())
261 /* FIXME: hardware demux policy */
262 if (!(cap & iDVBChannel::capDecode))
265 for (; i != m_demux.end(); ++i, ++n)
266 if ((!i->m_inuse) && ((!fe) || (i->m_adapter == fe->m_adapter)))
268 if ((cap & iDVBChannel::capDecode) && n)
271 demux = new eDVBAllocatedDemux(i);
273 demux->get().setSourceFrontend(fe->m_frontend->getID());
275 demux->get().setSourcePVR(0);
278 eDebug("demux not found");
282 RESULT eDVBResourceManager::setChannelList(iDVBChannelList *list)
288 RESULT eDVBResourceManager::getChannelList(ePtr<iDVBChannelList> &list)
297 RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel)
299 /* first, check if a channel is already existing. */
301 if (m_cached_channel)
303 eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
304 if(channelid==cache_chan->getChannelID())
306 eDebug("use cached_channel");
307 channel = m_cached_channel;
313 // eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
314 for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
316 // eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
317 if (i->m_channel_id == channelid)
319 // eDebug("found shared channel..");
320 channel = i->m_channel;
325 /* no currently available channel is tuned to this channelid. create a new one, if possible. */
329 eDebug("no channel list set!");
333 ePtr<iDVBFrontendParameters> feparm;
334 if (m_list->getChannelFrontendData(channelid, feparm))
336 eDebug("channel not found!");
340 /* allocate a frontend. */
342 ePtr<eDVBAllocatedFrontend> fe;
344 if (allocateFrontend(fe, feparm))
345 return errNoFrontend;
348 ePtr<eDVBChannel> ch;
349 ch = new eDVBChannel(this, fe);
351 res = ch->setChannel(channelid, feparm);
355 return errChidNotFound;
357 m_cached_channel = channel = ch;
362 RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel, int frontend_index)
364 ePtr<eDVBAllocatedFrontend> fe;
366 if (m_cached_channel)
369 if (allocateFrontendByIndex(fe, frontend_index))
370 return errNoFrontend;
373 ch = new eDVBChannel(this, fe);
380 RESULT eDVBResourceManager::allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel)
382 ePtr<eDVBAllocatedDemux> demux;
384 if (m_cached_channel)
388 ch = new eDVBChannel(this, 0);
394 RESULT eDVBResourceManager::addChannel(const eDVBChannelID &chid, eDVBChannel *ch)
396 m_active_channels.push_back(active_channel(chid, ch));
397 /* emit */ m_channelAdded(ch);
401 RESULT eDVBResourceManager::removeChannel(eDVBChannel *ch)
404 for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end();)
406 if (i->m_channel == ch)
408 i = m_active_channels.erase(i);
419 RESULT eDVBResourceManager::connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection)
421 connection = new eConnection((eDVBResourceManager*)this, m_channelAdded.connect(channelAdded));
425 bool eDVBResourceManager::canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm)
427 ePtr<eDVBRegisteredFrontend> best;
430 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
433 int c = i->m_frontend->isCompatibleWith(feparm);
441 bool eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID& ignore)
444 if (m_cached_channel)
446 eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
447 if(channelid==cache_chan->getChannelID())
451 /* first, check if a channel is already existing. */
452 // eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
453 for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
455 // eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
456 if (i->m_channel_id == channelid)
458 // eDebug("found shared channel..");
463 int *decremented_cached_channel_fe_usecount=NULL,
464 *decremented_fe_usecount=NULL;
466 for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
468 // eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
469 if (i->m_channel_id == ignore)
471 eDVBChannel *channel = (eDVBChannel*) &(*i->m_channel);
472 if (channel == &(*m_cached_channel) ? channel->getUseCount() == 2 : channel->getUseCount() == 1) // channel only used once..
474 ePtr<iDVBFrontend> fe;
475 if (!i->m_channel->getFrontend(fe))
477 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
479 if ( &(*fe) == &(*ii->m_frontend) )
482 decremented_fe_usecount = &ii->m_inuse;
483 if (channel == &(*m_cached_channel))
484 decremented_cached_channel_fe_usecount = decremented_fe_usecount;
494 if (!decremented_cached_channel_fe_usecount)
496 if (m_cached_channel)
498 eDVBChannel *channel = (eDVBChannel*) &(*m_cached_channel);
499 if (channel->getUseCount() == 1)
501 ePtr<iDVBFrontend> fe;
502 if (!channel->getFrontend(fe))
504 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
506 if ( &(*fe) == &(*ii->m_frontend) )
509 decremented_cached_channel_fe_usecount = &ii->m_inuse;
518 decremented_cached_channel_fe_usecount=NULL;
520 ePtr<iDVBFrontendParameters> feparm;
524 eDebug("no channel list set!");
529 if (m_list->getChannelFrontendData(channelid, feparm))
531 eDebug("channel not found!");
536 ret = canAllocateFrontend(feparm);
539 if (decremented_fe_usecount)
540 ++(*decremented_fe_usecount);
541 if (decremented_cached_channel_fe_usecount)
542 ++(*decremented_cached_channel_fe_usecount);
547 DEFINE_REF(eDVBChannel);
549 eDVBChannel::eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend): m_state(state_idle), m_mgr(mgr)
551 m_frontend = frontend;
556 m_frontend->get().connectStateChange(slot(*this, &eDVBChannel::frontendStateChanged), m_conn_frontendStateChanged);
559 eDVBChannel::~eDVBChannel()
562 m_mgr->removeChannel(this);
566 m_pvr_thread->stop();
567 ::close(m_pvr_fd_src);
568 ::close(m_pvr_fd_dst);
573 void eDVBChannel::frontendStateChanged(iDVBFrontend*fe)
575 int state, ourstate = 0;
577 /* if we are already in shutdown, don't change state. */
578 if (m_state == state_release)
581 if (fe->getState(state))
584 if (state == iDVBFrontend::stateLock)
586 eDebug("OURSTATE: ok");
588 } else if (state == iDVBFrontend::stateTuning)
590 eDebug("OURSTATE: tuning");
591 ourstate = state_tuning;
592 } else if (state == iDVBFrontend::stateLostLock)
594 /* on managed channels, we try to retune in order to re-acquire lock. */
597 eDebug("OURSTATE: lost lock, trying to retune");
598 ourstate = state_tuning;
599 m_frontend->get().tune(*m_feparm);
601 /* on unmanaged channels, we don't do this. the client will do this. */
603 eDebug("OURSTATE: lost lock, unavailable now.");
604 ourstate = state_unavailable;
606 } else if (state == iDVBFrontend::stateFailed)
608 eDebug("OURSTATE: failed");
609 ourstate = state_failed;
611 eFatal("state unknown");
613 if (ourstate != m_state)
616 m_stateChanged(this);
620 void eDVBChannel::pvrEvent(int event)
624 case eFilePushThread::evtEOF:
625 eDebug("eDVBChannel: End of file!");
626 m_event(this, evtEOF);
631 void eDVBChannel::AddUse()
636 void eDVBChannel::ReleaseUse()
640 m_state = state_release;
641 m_stateChanged(this);
645 RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid, ePtr<iDVBFrontendParameters> &feparm)
648 m_mgr->removeChannel(this);
655 eDebug("no frontend to tune!");
659 m_channel_id = channelid;
660 m_mgr->addChannel(channelid, this);
661 m_state = state_tuning;
662 /* if tuning fails, shutdown the channel immediately. */
664 res = m_frontend->get().tune(*feparm);
669 m_state = state_release;
670 m_stateChanged(this);
677 RESULT eDVBChannel::connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection)
679 connection = new eConnection((iDVBChannel*)this, m_stateChanged.connect(stateChange));
683 RESULT eDVBChannel::connectEvent(const Slot2<void,iDVBChannel*,int> &event, ePtr<eConnection> &connection)
685 connection = new eConnection((iDVBChannel*)this, m_event.connect(event));
689 RESULT eDVBChannel::getState(int &state)
695 RESULT eDVBChannel::setCIRouting(const eDVBCIRouting &routing)
700 RESULT eDVBChannel::getDemux(ePtr<iDVBDemux> &demux, int cap)
702 ePtr<eDVBAllocatedDemux> &our_demux = (cap & capDecode) ? m_decoder_demux : m_demux;
708 if (m_mgr->allocateDemux(m_frontend ? (eDVBRegisteredFrontend*)*m_frontend : (eDVBRegisteredFrontend*)0, our_demux, cap))
713 /* don't hold a reference to the decoding demux, we don't need it. */
719 RESULT eDVBChannel::getFrontend(ePtr<iDVBFrontend> &frontend)
721 frontend = &m_frontend->get();
728 RESULT eDVBChannel::playFile(const char *file)
733 m_pvr_thread->stop();
738 m_tstools.openFile(file);
740 /* DON'T EVEN THINK ABOUT FIXING THIS. FIX THE ATI SOURCES FIRST,
741 THEN DO A REAL FIX HERE! */
743 /* (this codepath needs to be improved anyway.) */
744 m_pvr_fd_dst = open("/dev/misc/pvr", O_WRONLY);
745 if (m_pvr_fd_dst < 0)
747 eDebug("can't open /dev/misc/pvr - you need to buy the new(!) $$$ box! (%m)"); // or wait for the driver to be improved.
751 m_pvr_fd_src = open(file, O_RDONLY|O_LARGEFILE);
752 if (m_pvr_fd_src < 0)
754 eDebug("can't open PVR m_pvr_fd_src file %s (%m)", file);
760 m_stateChanged(this);
762 m_pvr_thread = new eFilePushThread();
763 m_pvr_thread->enablePVRCommit(1);
764 m_pvr_thread->start(m_pvr_fd_src, m_pvr_fd_dst);
765 CONNECT(m_pvr_thread->m_event, eDVBChannel::pvrEvent);
770 RESULT eDVBChannel::getLength(pts_t &len)
772 return m_tstools.calcLen(len);
775 RESULT eDVBChannel::getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode)
781 /* getPTS for offset 0 is cached, so it doesn't harm. */
782 int r = m_tstools.getPTS(begin, pos);
785 eDebug("tstools getpts(0) failed!");
791 /* TODO: this is a gross hack. */
792 r = decoding_demux->getSTC(now, mode ? 128 : 0);
796 eDebug("demux getSTC failed");
800 // eDebug("STC: %08llx PTS: %08llx, diff %lld", now, pos, now - pos);
801 /* when we are less than 10 seconds before the start, return 0. */
802 /* (we're just waiting for the timespam to start) */
803 if ((now < pos) && ((pos - now) < 90000 * 10))
809 if (now < pos) /* wrap around */
810 pos = now + ((pts_t)1)<<33 - pos;
817 RESULT eDVBChannel::seekTo(iDVBDemux *decoding_demux, int relative, pts_t &pts)
819 int bitrate = m_tstools.calcBitrate(); /* in bits/s */
827 if (getCurrentPosition(decoding_demux, now, 0))
829 eDebug("seekTo: getCurrentPosition failed!");
838 off_t offset = (pts * (pts_t)bitrate) / 8ULL / 90000ULL;
840 seekToPosition(decoding_demux, offset);
844 RESULT eDVBChannel::seekToPosition(iDVBDemux *decoding_demux, const off_t &r)
846 /* when seeking, we have to ensure that all buffers are flushed.
847 there are basically 3 buffers:
848 a.) the filepush's internal buffer
849 b.) the PVR buffer (before demux)
850 c.) the ratebuffer (after demux)
852 it's important to clear them in the correct order, otherwise
853 the ratebuffer (for example) would immediately refill from
854 the not-yet-flushed PVR buffer.
856 eDebug("eDVBChannel: seekToPosition .. %llx", r);
857 m_pvr_thread->pause();
859 /* flush internal filepush buffer */
860 m_pvr_thread->flush();
862 /* HACK: flush PVR buffer */
863 ::ioctl(m_pvr_fd_dst, 0);
865 /* flush ratebuffers (video, audio) */
867 decoding_demux->flush();
869 /* demux will also flush all decoder.. */
870 m_pvr_thread->seek(SEEK_SET, r);
871 m_pvr_thread->resume();