implement /tmp/camd.socket support
[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()
9         :m_pmt_pid(0xFFFF), m_ca_servicePtr(0)
10 {
11         eDVBResourceManager::getInstance(m_resourceManager);
12         CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
13         CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
14 }
15
16 eDVBServicePMTHandler::~eDVBServicePMTHandler()
17 {
18         delete m_ca_servicePtr;
19 }
20
21 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
22 {
23         int state;
24         channel->getState(state);
25         
26         if ((m_last_channel_state != iDVBChannel::state_ok)
27                 && (state == iDVBChannel::state_ok) && (!m_demux))
28         {
29                 if (m_channel)
30                         if (m_channel->getDemux(m_demux))
31                                 eDebug("shit it failed.. again.");
32                 
33                 serviceEvent(eventTuned);
34                 
35                 if (m_demux)
36                 {
37                         eDebug("ok ... now we start!!");
38
39                         /* emit */ m_resourceManager->m_channelRunning(channel);
40
41                         m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
42
43                         if ( m_service && !m_service->cacheEmpty() )
44                                 serviceEvent(eventNewProgramInfo);
45                 }
46         }
47 }
48
49 void eDVBServicePMTHandler::PMTready(int error)
50 {
51         if (error)
52                 serviceEvent(eventNoPMT);
53         else
54         {
55                 serviceEvent(eventNewProgramInfo);
56                 if (!m_pvr_channel && !m_ca_servicePtr)   // don't send campmt to camd.socket for playbacked services
57                         m_ca_servicePtr = new eDVBCAService(*this);
58                 if (m_ca_servicePtr)
59                         m_ca_servicePtr->buildCAPMT();
60         }
61 }
62
63 void eDVBServicePMTHandler::PATready(int)
64 {
65         eDebug("got PAT");
66         ePtr<eTable<ProgramAssociationSection> > ptr;
67         if (!m_PAT.getCurrent(ptr))
68         {
69                 int pmtpid = -1;
70                 std::vector<ProgramAssociationSection*>::const_iterator i;
71                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
72                 {
73                         const ProgramAssociationSection &pat = **i;
74                         ProgramAssociationConstIterator program;
75                         for (program = pat.getPrograms()->begin(); program != pat.getPrograms()->end(); ++program)
76                                 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
77                                         pmtpid = (*program)->getProgramMapPid();
78                 }
79                 if (pmtpid == -1)
80                         serviceEvent(eventNoPATEntry);
81                 else
82                 {
83                         m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
84                         m_pmt_pid = pmtpid;
85                 }
86         } else
87                 serviceEvent(eventNoPAT);
88 }
89
90 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
91 {
92         eDebug("got PMT");
93         ePtr<eTable<ProgramMapSection> > ptr;
94
95         program.videoStreams.clear();
96         program.audioStreams.clear();
97         program.pcrPid = -1;
98
99         if (!m_PMT.getCurrent(ptr))
100         {
101                 std::vector<ProgramMapSection*>::const_iterator i;
102                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
103                 {
104                         const ProgramMapSection &pmt = **i;
105                         program.pcrPid = pmt.getPcrPid();
106                         
107                         ElementaryStreamInfoConstIterator es;
108                         for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
109                         {
110                                 int isaudio = 0, isvideo = 0;
111                                 videoStream video;
112                                 audioStream audio;
113                                 
114                                 video.pid = (*es)->getPid();
115                                 audio.pid = (*es)->getPid();
116                                 
117                                 switch ((*es)->getType())
118                                 {
119                                 case 0x01: // MPEG 1 video
120                                 case 0x02: // MPEG 2 video
121                                         isvideo = 1;
122                                         break;
123                                 case 0x03: // MPEG 1 audio
124                                 case 0x04: // MPEG 2 audio:
125                                         isaudio = 1;
126                                         audio.type = audioStream::atMPEG;
127                                         break;
128                                 }
129                                 if (isaudio)
130                                         program.audioStreams.push_back(audio);
131                                 if (isvideo)
132                                         program.videoStreams.push_back(video);
133                         }
134                 }
135                 return 0;
136         }
137         else if ( m_service && !m_service->cacheEmpty() )
138         {
139                 int vpid = m_service->getCachePID(eDVBService::cVPID),
140                         apid_ac3 = m_service->getCachePID(eDVBService::cAPID),
141                         apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID),
142                         pcrpid = m_service->getCachePID(eDVBService::cPCRPID),
143                         cnt=0;
144                 if ( vpid != -1 )
145                 {
146                         videoStream s;
147                         s.pid = vpid;
148                         program.videoStreams.push_back(s);
149                         ++cnt;
150                 }
151                 if ( apid_ac3 != -1 )
152                 {
153                         audioStream s;
154                         s.type = audioStream::atAC3;
155                         s.pid = apid_ac3;
156                         program.audioStreams.push_back(s);
157                         ++cnt;
158                 }
159                 if ( apid_mpeg != -1 )
160                 {
161                         audioStream s;
162                         s.type = audioStream::atMPEG;
163                         s.pid = apid_mpeg;
164                         program.audioStreams.push_back(s);
165                         ++cnt;
166                 }
167                 if ( pcrpid != -1 )
168                 {
169                         ++cnt;
170                         program.pcrPid = pcrpid;
171                 }
172                 if ( cnt )
173                         return 0;
174         }
175         return -1;
176 }
177
178 int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
179 {
180         demux = m_demux;
181         if (demux)
182                 return 0;
183         else
184                 return -1;
185 }
186
187 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
188 {
189         pvr_channel = m_pvr_channel;
190         if (pvr_channel)
191                 return 0;
192         else
193                 return -1;
194 }
195
196 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
197 {
198         RESULT res;
199         m_reference = ref;
200         
201 //      ref.path = "/viva.ts"; // hrhr.
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[2]);  // descramble on demux
288                 m_parent.m_demux->getCADemuxID(tmp[3]);  // get section data from demux1
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 }