add serviceinfo
[enigma2.git] / lib / dvb / pmt.cpp
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>
7
8 eDVBServicePMTHandler::eDVBServicePMTHandler(int record)
9         :m_pmt_pid(0xFFFF), m_ca_servicePtr(0)
10 {
11         m_record = record;
12         eDVBResourceManager::getInstance(m_resourceManager);
13         CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
14         CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
15 }
16
17 eDVBServicePMTHandler::~eDVBServicePMTHandler()
18 {
19         delete m_ca_servicePtr;
20 }
21
22 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
23 {
24         int state;
25         channel->getState(state);
26         
27         if ((m_last_channel_state != iDVBChannel::state_ok)
28                 && (state == iDVBChannel::state_ok) && (!m_demux))
29         {
30                 if (m_channel)
31                         if (m_channel->getDemux(m_demux, m_record ? 0 : iDVBChannel::capDecode))
32                                 eDebug("Allocating a demux for now tuned-in channel failed.");
33                 
34                 serviceEvent(eventTuned);
35                 
36                 if (m_demux)
37                 {
38                         eDebug("ok ... now we start!!");
39
40                         /* emit */ m_resourceManager->m_channelRunning(channel);
41
42                         m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
43
44                         if ( m_service && !m_service->cacheEmpty() )
45                                 serviceEvent(eventNewProgramInfo);
46                 }
47         } else if ((m_last_channel_state != iDVBChannel::state_failed) && 
48                         (state == iDVBChannel::state_failed))
49         {
50                 eDebug("tune failed.");
51                 serviceEvent(eventTuneFailed);
52         }
53 }
54
55 void eDVBServicePMTHandler::PMTready(int error)
56 {
57         if (error)
58                 serviceEvent(eventNoPMT);
59         else
60         {
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);
64                 if (m_ca_servicePtr)
65                         m_ca_servicePtr->buildCAPMT();
66         }
67 }
68
69 void eDVBServicePMTHandler::PATready(int)
70 {
71         eDebug("got PAT");
72         ePtr<eTable<ProgramAssociationSection> > ptr;
73         if (!m_PAT.getCurrent(ptr))
74         {
75                 int pmtpid = -1;
76                 std::vector<ProgramAssociationSection*>::const_iterator i;
77                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
78                 {
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();
84                 }
85                 if (pmtpid == -1)
86                         serviceEvent(eventNoPATEntry);
87                 else
88                 {
89                         m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
90                         m_pmt_pid = pmtpid;
91                 }
92         } else
93                 serviceEvent(eventNoPAT);
94 }
95
96 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
97 {
98         eDebug("got PMT");
99         ePtr<eTable<ProgramMapSection> > ptr;
100
101         program.videoStreams.clear();
102         program.audioStreams.clear();
103         program.pcrPid = -1;
104         program.pmtPid = m_pmt_pid < 0x1fff ? m_pmt_pid : -1;
105
106         if (!m_PMT.getCurrent(ptr))
107         {
108                 std::vector<ProgramMapSection*>::const_iterator i;
109                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
110                 {
111                         const ProgramMapSection &pmt = **i;
112                         program.pcrPid = pmt.getPcrPid();
113                         
114                         ElementaryStreamInfoConstIterator es;
115                         for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
116                         {
117                                 int isaudio = 0, isvideo = 0;
118                                 videoStream video;
119                                 audioStream audio;
120                                 
121                                 video.pid = (*es)->getPid();
122                                 audio.pid = (*es)->getPid();
123                                 
124                                 switch ((*es)->getType())
125                                 {
126                                 case 0x01: // MPEG 1 video
127                                 case 0x02: // MPEG 2 video
128                                         isvideo = 1;
129                                         break;
130                                 case 0x03: // MPEG 1 audio
131                                 case 0x04: // MPEG 2 audio:
132                                         isaudio = 1;
133                                         audio.type = audioStream::atMPEG;
134                                         break;
135                                 }
136                                 if (isaudio)
137                                         program.audioStreams.push_back(audio);
138                                 if (isvideo)
139                                         program.videoStreams.push_back(video);
140                         }
141                 }
142                 return 0;
143         }
144         else if ( m_service && !m_service->cacheEmpty() )
145         {
146                 int vpid = m_service->getCachePID(eDVBService::cVPID),
147                         apid_ac3 = m_service->getCachePID(eDVBService::cAPID),
148                         apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID),
149                         pcrpid = m_service->getCachePID(eDVBService::cPCRPID),
150                         cnt=0;
151                 if ( vpid != -1 )
152                 {
153                         videoStream s;
154                         s.pid = vpid;
155                         program.videoStreams.push_back(s);
156                         ++cnt;
157                 }
158                 if ( apid_ac3 != -1 )
159                 {
160                         audioStream s;
161                         s.type = audioStream::atAC3;
162                         s.pid = apid_ac3;
163                         program.audioStreams.push_back(s);
164                         ++cnt;
165                 }
166                 if ( apid_mpeg != -1 )
167                 {
168                         audioStream s;
169                         s.type = audioStream::atMPEG;
170                         s.pid = apid_mpeg;
171                         program.audioStreams.push_back(s);
172                         ++cnt;
173                 }
174                 if ( pcrpid != -1 )
175                 {
176                         ++cnt;
177                         program.pcrPid = pcrpid;
178                 }
179                 if ( cnt )
180                         return 0;
181         }
182         return -1;
183 }
184
185 int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
186 {
187         demux = m_demux;
188         if (demux)
189                 return 0;
190         else
191                 return -1;
192 }
193
194 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
195 {
196         pvr_channel = m_pvr_channel;
197         if (pvr_channel)
198                 return 0;
199         else
200                 return -1;
201 }
202
203 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
204 {
205         RESULT res;
206         m_reference = ref;
207         
208                 /* is this a normal (non PVR) channel? */
209         if (ref.path.empty())
210         {
211                 eDVBChannelID chid;
212                 ref.getChannelID(chid);
213                 res = m_resourceManager->allocateChannel(chid, m_channel);
214                 eDebug("allocate Channel: res %d", res);
215         } else
216         {
217                 eDVBMetaParser parser;
218                 
219                 if (parser.parseFile(ref.path))
220                         eWarning("no .meta file found, trying original service ref.");
221                 else
222                         m_reference = parser.m_ref;
223                 
224                 eDebug("alloc PVR");
225                         /* allocate PVR */
226                 res = m_resourceManager->allocatePVRChannel(m_pvr_channel);
227                 if (res)
228                         eDebug("allocatePVRChannel failed!\n");
229                 m_channel = m_pvr_channel;
230         }
231         
232         if (m_channel)
233         {
234                 m_channel->connectStateChange(
235                         slot(*this, &eDVBServicePMTHandler::channelStateChanged), 
236                         m_channelStateChanged_connection);
237                 m_last_channel_state = -1;
238                 channelStateChanged(m_channel);
239         } else
240         {
241                 serviceEvent(eventTuneFailed);
242                 return res;
243         }
244
245         if (m_pvr_channel)
246                 m_pvr_channel->playFile(ref.path.c_str());
247
248         ePtr<iDVBChannelList> db;
249         if (!m_resourceManager->getChannelList(db))
250                 db->getService((eServiceReferenceDVB&)m_reference, m_service);
251
252         return res;
253 }
254
255 void eDVBCAService::Connect()
256 {
257         memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
258         m_servaddr.sun_family = AF_UNIX;
259         strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
260         m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
261         m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
262         connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen);
263         fcntl(m_sock, F_SETFL, O_NONBLOCK);
264         int val=1;
265         setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
266 }
267
268 void eDVBCAService::buildCAPMT()
269 {
270         ePtr<eTable<ProgramMapSection> > ptr;
271
272         if (m_parent.m_PMT.getCurrent(ptr))
273                 return;
274
275         std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
276         if ( i != ptr->getSections().end() )
277         {
278                 CaProgramMapSection capmt(*i++, m_capmt == NULL ? 0x03 /*only*/: 0x05 /*update*/, 0x01 );
279
280                 while( i != ptr->getSections().end() )
281                 {
282 //                      eDebug("append");
283                         capmt.append(*i++);
284                 }
285
286                 // add our private descriptors to capmt
287                 uint8_t tmp[10];
288
289                 tmp[0]=0x84;  // pmt pid
290                 tmp[1]=0x02;
291                 tmp[2]=m_parent.m_pmt_pid>>8;
292                 tmp[3]=m_parent.m_pmt_pid&0xFF;
293                 capmt.injectDescriptor(tmp, false);
294
295                 tmp[0] = 0x82; // demux
296                 tmp[1] = 0x02;
297                 m_parent.m_demux->getCADemuxID(tmp[3]); // read section data from demux number
298                 tmp[2] = 1 << tmp[3];                   // descramble bitmask
299                 capmt.injectDescriptor(tmp, false);
300
301                 tmp[0] = 0x81; // dvbnamespace
302                 tmp[1] = 0x08;
303                 tmp[2] = m_parent.m_reference.getDVBNamespace().get()>>24;
304                 tmp[3]=(m_parent.m_reference.getDVBNamespace().get()>>16)&0xFF;
305                 tmp[4]=(m_parent.m_reference.getDVBNamespace().get()>>8)&0xFF;
306                 tmp[5]=m_parent.m_reference.getDVBNamespace().get()&0xFF;
307                 tmp[6]=m_parent.m_reference.getTransportStreamID().get()>>8;
308                 tmp[7]=m_parent.m_reference.getTransportStreamID().get()&0xFF;
309                 tmp[8]=m_parent.m_reference.getOriginalNetworkID().get()>>8;
310                 tmp[9]=m_parent.m_reference.getOriginalNetworkID().get()&0xFF;
311                 capmt.injectDescriptor(tmp, false);
312
313                 if ( !m_capmt )
314                         m_capmt = new uint8_t[2048];
315
316                 capmt.writeToBuffer(m_capmt);
317         }
318
319         if ( m_sendstate != 0xFFFFFFFF )
320                 m_sendstate=0;
321         sendCAPMT();
322 }
323
324 void eDVBCAService::sendCAPMT()
325 {
326         if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
327         {
328                 ::close(m_sock);
329                 Connect();
330         }
331
332         int wp=0;
333         if ( m_capmt[3] & 0x80 )
334         {
335                 int i=0;
336                 int lenbytes = m_capmt[3] & ~0x80;
337                 while(i < lenbytes)
338                         wp |= (m_capmt[4+i] << (8 * i++));
339                 wp+=4;
340                 wp+=lenbytes;
341         }
342         else
343         {
344                 wp = m_capmt[3];
345                 wp+=4;
346         }
347
348         if ( write(m_sock, m_capmt, wp) == wp )
349         {
350                 m_sendstate=0xFFFFFFFF;
351                 eDebug("[eDVBCAHandler] send %d bytes",wp);
352 #if 1
353                 for(int i=0;i<wp;i++)
354                         eDebugNoNewLine("%02x ", m_capmt[i]);
355                 eDebug("");
356 #endif
357         }
358         else
359         {
360                 switch(m_sendstate)
361                 {
362                         case 0xFFFFFFFF:
363                                 ++m_sendstate;
364                                 m_retryTimer.start(0,true);
365 //                              eDebug("[eDVBCAHandler] send failed .. immediate retry");
366                                 break;
367                         default:
368                                 m_retryTimer.start(5000,true);
369 //                              eDebug("[eDVBCAHandler] send failed .. retry in 5 sec");
370                                 break;
371                 }
372                 ++m_sendstate;
373         }
374 }