1 #include <lib/base/eerror.h>
2 #include <lib/dvb/pmt.h>
3 #include <lib/dvb/specs.h>
4 #include <lib/dvb/dvb.h>
5 #include <lib/dvb/metaparser.h>
6 #include <dvbsi++/ca_program_map_section.h>
8 eDVBServicePMTHandler::eDVBServicePMTHandler(int record)
9 :m_pmt_pid(0xFFFF), m_ca_servicePtr(0)
12 eDVBResourceManager::getInstance(m_resourceManager);
13 CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
14 CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
17 eDVBServicePMTHandler::~eDVBServicePMTHandler()
19 delete m_ca_servicePtr;
22 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
25 channel->getState(state);
27 if ((m_last_channel_state != iDVBChannel::state_ok)
28 && (state == iDVBChannel::state_ok) && (!m_demux))
31 if (m_channel->getDemux(m_demux, m_record ? 0 : iDVBChannel::capDecode))
32 eDebug("Allocating a demux for now tuned-in channel failed.");
34 serviceEvent(eventTuned);
38 eDebug("ok ... now we start!!");
40 /* emit */ m_resourceManager->m_channelRunning(channel);
42 m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
44 if ( m_service && !m_service->cacheEmpty() )
45 serviceEvent(eventNewProgramInfo);
47 } else if ((m_last_channel_state != iDVBChannel::state_failed) &&
48 (state == iDVBChannel::state_failed))
50 eDebug("tune failed.");
51 serviceEvent(eventTuneFailed);
55 void eDVBServicePMTHandler::PMTready(int error)
58 serviceEvent(eventNoPMT);
61 serviceEvent(eventNewProgramInfo);
62 if (!m_pvr_channel && !m_ca_servicePtr) // don't send campmt to camd.socket for playbacked services
63 m_ca_servicePtr = new eDVBCAService(*this);
65 m_ca_servicePtr->buildCAPMT();
69 void eDVBServicePMTHandler::PATready(int)
72 ePtr<eTable<ProgramAssociationSection> > ptr;
73 if (!m_PAT.getCurrent(ptr))
76 std::vector<ProgramAssociationSection*>::const_iterator i;
77 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
79 const ProgramAssociationSection &pat = **i;
80 ProgramAssociationConstIterator program;
81 for (program = pat.getPrograms()->begin(); program != pat.getPrograms()->end(); ++program)
82 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
83 pmtpid = (*program)->getProgramMapPid();
86 serviceEvent(eventNoPATEntry);
89 m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
93 serviceEvent(eventNoPAT);
96 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
98 ePtr<eTable<ProgramMapSection> > ptr;
100 program.videoStreams.clear();
101 program.audioStreams.clear();
103 program.pmtPid = m_pmt_pid < 0x1fff ? m_pmt_pid : -1;
105 if (!m_PMT.getCurrent(ptr))
107 std::vector<ProgramMapSection*>::const_iterator i;
108 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
110 const ProgramMapSection &pmt = **i;
111 program.pcrPid = pmt.getPcrPid();
113 ElementaryStreamInfoConstIterator es;
114 for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
116 int isaudio = 0, isvideo = 0;
120 video.pid = (*es)->getPid();
121 audio.pid = (*es)->getPid();
123 switch ((*es)->getType())
125 case 0x01: // MPEG 1 video
126 case 0x02: // MPEG 2 video
129 case 0x03: // MPEG 1 audio
130 case 0x04: // MPEG 2 audio:
132 audio.type = audioStream::atMPEG;
136 program.audioStreams.push_back(audio);
138 program.videoStreams.push_back(video);
143 else if ( m_service && !m_service->cacheEmpty() )
145 int vpid = m_service->getCachePID(eDVBService::cVPID),
146 apid_ac3 = m_service->getCachePID(eDVBService::cAPID),
147 apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID),
148 pcrpid = m_service->getCachePID(eDVBService::cPCRPID),
154 program.videoStreams.push_back(s);
157 if ( apid_ac3 != -1 )
160 s.type = audioStream::atAC3;
162 program.audioStreams.push_back(s);
165 if ( apid_mpeg != -1 )
168 s.type = audioStream::atMPEG;
170 program.audioStreams.push_back(s);
176 program.pcrPid = pcrpid;
184 int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
193 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
195 pvr_channel = m_pvr_channel;
202 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
207 /* is this a normal (non PVR) channel? */
208 if (ref.path.empty())
211 ref.getChannelID(chid);
212 res = m_resourceManager->allocateChannel(chid, m_channel);
213 eDebug("allocate Channel: res %d", res);
216 eDVBMetaParser parser;
218 if (parser.parseFile(ref.path))
219 eWarning("no .meta file found, trying original service ref.");
221 m_reference = parser.m_ref;
225 res = m_resourceManager->allocatePVRChannel(m_pvr_channel);
227 eDebug("allocatePVRChannel failed!\n");
228 m_channel = m_pvr_channel;
233 m_channel->connectStateChange(
234 slot(*this, &eDVBServicePMTHandler::channelStateChanged),
235 m_channelStateChanged_connection);
236 m_last_channel_state = -1;
237 channelStateChanged(m_channel);
240 serviceEvent(eventTuneFailed);
245 m_pvr_channel->playFile(ref.path.c_str());
247 ePtr<iDVBChannelList> db;
248 if (!m_resourceManager->getChannelList(db))
249 db->getService((eServiceReferenceDVB&)m_reference, m_service);
254 void eDVBCAService::Connect()
256 memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
257 m_servaddr.sun_family = AF_UNIX;
258 strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
259 m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
260 m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
261 connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen);
262 fcntl(m_sock, F_SETFL, O_NONBLOCK);
264 setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
267 void eDVBCAService::buildCAPMT()
269 ePtr<eTable<ProgramMapSection> > ptr;
271 if (m_parent.m_PMT.getCurrent(ptr))
274 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
275 if ( i != ptr->getSections().end() )
277 CaProgramMapSection capmt(*i++, m_capmt == NULL ? 0x03 /*only*/: 0x05 /*update*/, 0x01 );
279 while( i != ptr->getSections().end() )
285 // add our private descriptors to capmt
288 tmp[0]=0x84; // pmt pid
290 tmp[2]=m_parent.m_pmt_pid>>8;
291 tmp[3]=m_parent.m_pmt_pid&0xFF;
292 capmt.injectDescriptor(tmp, false);
294 tmp[0] = 0x82; // demux
296 m_parent.m_demux->getCADemuxID(tmp[3]); // read section data from demux number
297 tmp[2] = 1 << tmp[3]; // descramble bitmask
298 capmt.injectDescriptor(tmp, false);
300 tmp[0] = 0x81; // dvbnamespace
302 tmp[2] = m_parent.m_reference.getDVBNamespace().get()>>24;
303 tmp[3]=(m_parent.m_reference.getDVBNamespace().get()>>16)&0xFF;
304 tmp[4]=(m_parent.m_reference.getDVBNamespace().get()>>8)&0xFF;
305 tmp[5]=m_parent.m_reference.getDVBNamespace().get()&0xFF;
306 tmp[6]=m_parent.m_reference.getTransportStreamID().get()>>8;
307 tmp[7]=m_parent.m_reference.getTransportStreamID().get()&0xFF;
308 tmp[8]=m_parent.m_reference.getOriginalNetworkID().get()>>8;
309 tmp[9]=m_parent.m_reference.getOriginalNetworkID().get()&0xFF;
310 capmt.injectDescriptor(tmp, false);
313 m_capmt = new uint8_t[2048];
315 capmt.writeToBuffer(m_capmt);
318 if ( m_sendstate != 0xFFFFFFFF )
323 void eDVBCAService::sendCAPMT()
325 if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
332 if ( m_capmt[3] & 0x80 )
335 int lenbytes = m_capmt[3] & ~0x80;
337 wp |= (m_capmt[4+i] << (8 * i++));
347 if ( write(m_sock, m_capmt, wp) == wp )
349 m_sendstate=0xFFFFFFFF;
350 eDebug("[eDVBCAHandler] send %d bytes",wp);
352 for(int i=0;i<wp;i++)
353 eDebugNoNewLine("%02x ", m_capmt[i]);
363 m_retryTimer.start(0,true);
364 // eDebug("[eDVBCAHandler] send failed .. immediate retry");
367 m_retryTimer.start(5000,true);
368 // eDebug("[eDVBCAHandler] send failed .. retry in 5 sec");