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