remove debug output
[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         ePtr<eTable<ProgramMapSection> > ptr;
99
100         program.videoStreams.clear();
101         program.audioStreams.clear();
102         program.pcrPid = -1;
103         program.pmtPid = m_pmt_pid < 0x1fff ? m_pmt_pid : -1;
104
105         if (!m_PMT.getCurrent(ptr))
106         {
107                 std::vector<ProgramMapSection*>::const_iterator i;
108                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
109                 {
110                         const ProgramMapSection &pmt = **i;
111                         program.pcrPid = pmt.getPcrPid();
112                         
113                         ElementaryStreamInfoConstIterator es;
114                         for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
115                         {
116                                 int isaudio = 0, isvideo = 0;
117                                 videoStream video;
118                                 audioStream audio;
119                                 
120                                 video.pid = (*es)->getPid();
121                                 audio.pid = (*es)->getPid();
122                                 
123                                 switch ((*es)->getType())
124                                 {
125                                 case 0x01: // MPEG 1 video
126                                 case 0x02: // MPEG 2 video
127                                         isvideo = 1;
128                                         break;
129                                 case 0x03: // MPEG 1 audio
130                                 case 0x04: // MPEG 2 audio:
131                                         isaudio = 1;
132                                         audio.type = audioStream::atMPEG;
133                                         break;
134                                 }
135                                 if (isaudio)
136                                         program.audioStreams.push_back(audio);
137                                 if (isvideo)
138                                         program.videoStreams.push_back(video);
139                         }
140                 }
141                 return 0;
142         }
143         else if ( m_service && !m_service->cacheEmpty() )
144         {
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),
149                         cnt=0;
150                 if ( vpid != -1 )
151                 {
152                         videoStream s;
153                         s.pid = vpid;
154                         program.videoStreams.push_back(s);
155                         ++cnt;
156                 }
157                 if ( apid_ac3 != -1 )
158                 {
159                         audioStream s;
160                         s.type = audioStream::atAC3;
161                         s.pid = apid_ac3;
162                         program.audioStreams.push_back(s);
163                         ++cnt;
164                 }
165                 if ( apid_mpeg != -1 )
166                 {
167                         audioStream s;
168                         s.type = audioStream::atMPEG;
169                         s.pid = apid_mpeg;
170                         program.audioStreams.push_back(s);
171                         ++cnt;
172                 }
173                 if ( pcrpid != -1 )
174                 {
175                         ++cnt;
176                         program.pcrPid = pcrpid;
177                 }
178                 if ( cnt )
179                         return 0;
180         }
181         return -1;
182 }
183
184 int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
185 {
186         demux = m_demux;
187         if (demux)
188                 return 0;
189         else
190                 return -1;
191 }
192
193 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
194 {
195         pvr_channel = m_pvr_channel;
196         if (pvr_channel)
197                 return 0;
198         else
199                 return -1;
200 }
201
202 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
203 {
204         RESULT res;
205         m_reference = ref;
206         
207                 /* is this a normal (non PVR) channel? */
208         if (ref.path.empty())
209         {
210                 eDVBChannelID chid;
211                 ref.getChannelID(chid);
212                 res = m_resourceManager->allocateChannel(chid, m_channel);
213                 eDebug("allocate Channel: res %d", res);
214         } else
215         {
216                 eDVBMetaParser parser;
217                 
218                 if (parser.parseFile(ref.path))
219                         eWarning("no .meta file found, trying original service ref.");
220                 else
221                         m_reference = parser.m_ref;
222                 
223                 eDebug("alloc PVR");
224                         /* allocate PVR */
225                 res = m_resourceManager->allocatePVRChannel(m_pvr_channel);
226                 if (res)
227                         eDebug("allocatePVRChannel failed!\n");
228                 m_channel = m_pvr_channel;
229         }
230         
231         if (m_channel)
232         {
233                 m_channel->connectStateChange(
234                         slot(*this, &eDVBServicePMTHandler::channelStateChanged), 
235                         m_channelStateChanged_connection);
236                 m_last_channel_state = -1;
237                 channelStateChanged(m_channel);
238         } else
239         {
240                 serviceEvent(eventTuneFailed);
241                 return res;
242         }
243
244         if (m_pvr_channel)
245                 m_pvr_channel->playFile(ref.path.c_str());
246
247         ePtr<iDVBChannelList> db;
248         if (!m_resourceManager->getChannelList(db))
249                 db->getService((eServiceReferenceDVB&)m_reference, m_service);
250
251         return res;
252 }
253
254 void eDVBCAService::Connect()
255 {
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);
263         int val=1;
264         setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
265 }
266
267 void eDVBCAService::buildCAPMT()
268 {
269         ePtr<eTable<ProgramMapSection> > ptr;
270
271         if (m_parent.m_PMT.getCurrent(ptr))
272                 return;
273
274         std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
275         if ( i != ptr->getSections().end() )
276         {
277                 CaProgramMapSection capmt(*i++, m_capmt == NULL ? 0x03 /*only*/: 0x05 /*update*/, 0x01 );
278
279                 while( i != ptr->getSections().end() )
280                 {
281 //                      eDebug("append");
282                         capmt.append(*i++);
283                 }
284
285                 // add our private descriptors to capmt
286                 uint8_t tmp[10];
287
288                 tmp[0]=0x84;  // pmt pid
289                 tmp[1]=0x02;
290                 tmp[2]=m_parent.m_pmt_pid>>8;
291                 tmp[3]=m_parent.m_pmt_pid&0xFF;
292                 capmt.injectDescriptor(tmp, false);
293
294                 tmp[0] = 0x82; // demux
295                 tmp[1] = 0x02;
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);
299
300                 tmp[0] = 0x81; // dvbnamespace
301                 tmp[1] = 0x08;
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);
311
312                 if ( !m_capmt )
313                         m_capmt = new uint8_t[2048];
314
315                 capmt.writeToBuffer(m_capmt);
316         }
317
318         if ( m_sendstate != 0xFFFFFFFF )
319                 m_sendstate=0;
320         sendCAPMT();
321 }
322
323 void eDVBCAService::sendCAPMT()
324 {
325         if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
326         {
327                 ::close(m_sock);
328                 Connect();
329         }
330
331         int wp=0;
332         if ( m_capmt[3] & 0x80 )
333         {
334                 int i=0;
335                 int lenbytes = m_capmt[3] & ~0x80;
336                 while(i < lenbytes)
337                         wp |= (m_capmt[4+i] << (8 * i++));
338                 wp+=4;
339                 wp+=lenbytes;
340         }
341         else
342         {
343                 wp = m_capmt[3];
344                 wp+=4;
345         }
346
347         if ( write(m_sock, m_capmt, wp) == wp )
348         {
349                 m_sendstate=0xFFFFFFFF;
350                 eDebug("[eDVBCAHandler] send %d bytes",wp);
351 #if 1
352                 for(int i=0;i<wp;i++)
353                         eDebugNoNewLine("%02x ", m_capmt[i]);
354                 eDebug("");
355 #endif
356         }
357         else
358         {
359                 switch(m_sendstate)
360                 {
361                         case 0xFFFFFFFF:
362                                 ++m_sendstate;
363                                 m_retryTimer.start(0,true);
364 //                              eDebug("[eDVBCAHandler] send failed .. immediate retry");
365                                 break;
366                         default:
367                                 m_retryTimer.start(5000,true);
368 //                              eDebug("[eDVBCAHandler] send failed .. retry in 5 sec");
369                                 break;
370                 }
371                 ++m_sendstate;
372         }
373 }