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