Support scrambled playback.
[vuplus_dvbapp] / lib / dvb / pmt.h
1 #ifndef __lib_dvb_dvbmid_h
2 #define __lib_dvb_dvbmid_h
3
4 #ifndef SWIG
5 #include <map>
6 #include <lib/base/buffer.h>
7 #include <lib/dvb/idvb.h>
8 #include <lib/dvb/dvb.h>
9 #include <lib/dvb/idemux.h>
10 #include <lib/dvb/esection.h>
11 #include <lib/python/python.h>
12 #include <lib/python/connections.h>
13 #include <dvbsi++/program_map_section.h>
14 #include <dvbsi++/program_association_section.h>
15
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/un.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 class eDVBCAService;
23 class eDVBScan;
24
25 #include <dvbsi++/application_information_section.h>
26 class OCSection : public LongCrcSection
27 {
28 protected:
29         void *data;
30
31 public:
32         OCSection(const uint8_t * const buffer)
33         : LongCrcSection(buffer)
34         {
35                 data = malloc(getSectionLength());
36                 memcpy(data, buffer, getSectionLength());
37         }
38         ~OCSection()
39         {
40                 free(data);
41         }
42         void *getData() { return data; }
43 };
44
45 struct channel_data: public Object
46 {
47         ePtr<eDVBChannel> m_channel;
48         ePtr<eConnection> m_stateChangedConn;
49         int m_prevChannelState;
50         int m_dataDemux;
51 };
52
53 // TODO .. put all static stuff into a 'eDVBCAServiceHandler class'
54
55 typedef std::map<eServiceReferenceDVB, eDVBCAService*> CAServiceMap;
56 typedef std::map<iDVBChannel*, channel_data*> ChannelMap;
57
58 class eDVBCAService: public Object
59 {
60         eIOBuffer m_buffer;
61         ePtr<eSocketNotifier> m_sn;
62         eServiceReferenceDVB m_service;
63         uint8_t m_used_demux[32];
64         unsigned int m_prev_build_hash;
65
66         int m_sock, m_clilen; 
67         struct sockaddr_un m_servaddr;
68         unsigned int m_sendstate;
69         unsigned char m_capmt[2048];
70         ePtr<eTimer> m_retryTimer;
71         void sendCAPMT();
72         void Connect();
73         void socketCB(int what);
74
75         static void DVBChannelAdded(eDVBChannel*);
76         static void DVBChannelStateChanged(iDVBChannel*);
77         static CAServiceMap exist;
78         static ChannelMap exist_channels;
79         static ePtr<eConnection> m_chanAddedConn;
80         static channel_data *getChannelData(eDVBChannelID &chid);
81
82         eDVBCAService();
83         ~eDVBCAService();
84 public:
85         static void registerChannelCallback(eDVBResourceManager *res_mgr);
86         static RESULT register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice );
87         static RESULT unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr );
88         void buildCAPMT(eTable<ProgramMapSection> *ptr);
89 };
90
91 #endif
92
93 #include <list>
94 #include <string>
95 class HbbTVApplicationInfo
96 {
97 public:
98         int m_OrgId;
99         int m_AppId;
100         int m_ControlCode;
101         short m_ProfileCode;
102         std::string m_HbbTVUrl;
103         std::string m_ApplicationName;
104 public:
105         HbbTVApplicationInfo(int controlCode, int orgid, int appid, std::string hbbtvUrl, std::string applicationName, int profileCode)
106                 : m_ControlCode(controlCode), m_HbbTVUrl(hbbtvUrl), m_ApplicationName(applicationName), m_OrgId(orgid), 
107                   m_AppId(appid), m_ProfileCode(profileCode)
108         {}
109 };
110 typedef std::list<HbbTVApplicationInfo *> HbbTVApplicationInfoList;
111 typedef HbbTVApplicationInfoList::iterator HbbTVApplicationInfoListIterator;
112 typedef HbbTVApplicationInfoList::const_iterator HbbTVApplicationInfoListConstIterator;
113
114 class eDVBServicePMTHandler: public Object
115 {
116 #ifndef SWIG
117         friend class eDVBCAService;
118         eServiceReferenceDVB m_reference;
119         ePtr<eDVBService> m_service;
120
121         int m_last_channel_state;
122         eDVBCAService *m_ca_servicePtr;
123         ePtr<eDVBScan> m_dvb_scan; // for sdt scan
124
125         eAUTable<eTable<ProgramMapSection> > m_PMT;
126         eAUTable<eTable<ProgramAssociationSection> > m_PAT;
127
128         eUsePtr<iDVBChannel> m_channel;
129         eUsePtr<iDVBPVRChannel> m_pvr_channel;
130         ePtr<eDVBResourceManager> m_resourceManager;
131         ePtr<iDVBDemux> m_demux, m_pvr_demux_tmp;
132
133         void channelStateChanged(iDVBChannel *);
134         ePtr<eConnection> m_channelStateChanged_connection;
135         void channelEvent(iDVBChannel *, int event);
136         ePtr<eConnection> m_channelEvent_connection;
137         void SDTScanEvent(int);
138         ePtr<eConnection> m_scan_event_connection;
139
140         eAUTable<eTable<ApplicationInformationSection> > m_AIT;
141         eAUTable<eTable<OCSection> > m_OC;
142
143         void registerCAService();
144         void PMTready(int error);
145         void PATready(int error);
146         
147         int m_pmt_pid;
148         
149         void AITready(int error);
150         void OCready(int error);
151         int m_dsmcc_pid;
152         int m_ait_pid;
153         HbbTVApplicationInfoList m_HbbTVApplications;
154         std::string m_HBBTVUrl;
155         std::string m_ApplicationName;
156         unsigned char m_AITData[4096];
157         
158         int m_use_decode_demux;
159         uint8_t m_decode_demux_num;
160         ePtr<eTimer> m_no_pat_entry_delay;
161         uint8_t mDemuxId;
162
163         bool m_pmt_ready;
164         bool m_ca_disabled;
165 public:
166         eDVBServicePMTHandler();
167         ~eDVBServicePMTHandler();
168 #endif
169
170 #ifdef SWIG
171 private:
172         eDVBServicePMTHandler();
173 public:
174 #endif
175
176         enum
177         {
178                 eventNoResources,  // a requested resource couldn't be allocated
179                 eventTuneFailed,   // tune failed
180                 eventNoPAT,        // no pat could be received (timeout)
181                 eventNoPATEntry,   // no pat entry for the corresponding SID could be found
182                 eventNoPMT,        // no pmt could be received (timeout)
183                 eventNewProgramInfo, // we just received a PMT
184                 eventTuned,        // a channel was sucessfully (re-)tuned in, you may start additional filters now
185                 
186                 eventPreStart,     // before start filepush thread
187                 eventSOF,          // seek pre start
188                 eventEOF,          // a file playback did end
189                 
190                 eventHBBTVInfo, /* HBBTV information was detected in the AIT */
191                 
192                 eventMisconfiguration, // a channel was not found in any list, or no frontend was found which could provide this channel
193                 eventNoDiskSpace,  // no disk space available
194                 eventStartPvrDescramble,   // start PVR Descramble Convert
195         };
196 #ifndef SWIG
197         Signal1<void,int> serviceEvent;
198
199         struct videoStream
200         {
201                 int pid;
202                 int component_tag;
203                 enum { vtMPEG2, vtMPEG4_H264, vtMPEG1, vtMPEG4_Part2, vtVC1, vtVC1_SM, vtH265_HEVC };
204                 int type;
205         };
206         
207         struct audioStream
208         {
209                 int pid,
210                     rdsPid; // hack for some radio services which transmit radiotext on different pid (i.e. harmony fm, HIT RADIO FFH, ...)
211                 enum { atMPEG, atAC3, atDTS, atAAC, atAACHE, atLPCM, atDTSHD, atDDP  };
212                 int type; // mpeg2, ac3, dts, ...
213                 
214                 int component_tag;
215                 std::string language_code; /* iso-639, if available. */
216         };
217
218         struct subtitleStream
219         {
220                 int pid;
221                 int subtitling_type;    /*  see ETSI EN 300 468 table 26 component_type
222                                                                         when stream_content is 0x03
223                                                                         0x10..0x13, 0x20..0x23 is used for dvb subtitles
224                                                                         0x01 is used for teletext subtitles */
225                 union
226                 {
227                         int composition_page_id;  // used for dvb subtitles
228                         int teletext_page_number;  // used for teletext subtitles
229                 };
230                 union
231                 {
232                         int ancillary_page_id;  // used for dvb subtitles
233                         int teletext_magazine_number;  // used for teletext subtitles
234                 };
235                 std::string language_code;
236                 bool operator<(const subtitleStream &s) const
237                 {
238                         if (pid != s.pid)
239                                 return pid < s.pid;
240                         if (teletext_page_number != s.teletext_page_number)
241                                 return teletext_page_number < s.teletext_page_number;
242                         return teletext_magazine_number < s.teletext_magazine_number;
243                 }
244         };
245
246         struct program
247         {
248                 struct capid_pair
249                 {
250                         uint16_t caid;
251                         int capid;
252                         bool operator< (const struct capid_pair &t) const { return t.caid < caid; }
253                 };
254                 std::vector<videoStream> videoStreams;
255                 std::vector<audioStream> audioStreams;
256                 int defaultAudioStream;
257                 std::vector<subtitleStream> subtitleStreams;
258                 std::list<capid_pair> caids;
259                 int pcrPid;
260                 int pmtPid;
261                 int textPid;
262                 int aitPid;
263                 int pmtVersion;
264                 bool isCached;
265                 bool isCrypted() { return !caids.empty(); }
266                 PyObject *createPythonObject();
267         };
268
269         int getProgramInfo(program &program);
270         int getDataDemux(ePtr<iDVBDemux> &demux);
271         int getDecodeDemux(ePtr<iDVBDemux> &demux);
272         PyObject *getCaIds(bool pair=false); // caid / ecmpid pair
273         PyObject *getHbbTVApplications(void); 
274         
275         int getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel);
276         int getServiceReference(eServiceReferenceDVB &service) { service = m_reference; return 0; }
277         int getService(ePtr<eDVBService> &service) { service = m_service; return 0; }
278         int getPMT(ePtr<eTable<ProgramMapSection> > &ptr) { return m_PMT.getCurrent(ptr); }
279         int getChannel(eUsePtr<iDVBChannel> &channel);
280         void resetCachedProgram() { m_have_cached_program = false; }
281         void sendEventNoPatEntry();
282
283         void getHBBTVUrl(std::string &ret) { ret = m_HBBTVUrl; }
284         void getDemuxID(int &id) { id = mDemuxId; }
285         void setCaDisable(bool disable) { m_ca_disabled = disable; }
286
287         enum serviceType
288         {
289                 livetv = 0,
290                 recording = 1,
291                 scrambled_recording = 2,
292                 playback = 3,
293                 timeshift_recording = 4,
294                 scrambled_timeshift_recording = 5,
295                 timeshift_playback = 6,
296                 streamserver = 7,
297                 scrambled_streamserver = 8,
298                 streamclient = 9,
299                 offline = 10,
300                 pvrDescramble = 11,
301         };
302
303         /* deprecated interface */
304         int tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0, serviceType type = livetv, bool descramble = true);
305
306         /* new interface */
307         int tuneExt(eServiceReferenceDVB &ref, int use_decode_demux, ePtr<iTsSource> &, const char *streaminfo_file, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0, serviceType type = livetv, bool descramble=true);
308
309         void free();
310         void addCaHandler();
311         void removeCaHandler();
312         bool isCiConnected();
313         bool isPmtReady() { return m_pmt_ready; }
314 private:
315         bool m_have_cached_program;
316         program m_cached_program;
317         bool m_descramble;
318         serviceType m_service_type;
319 #endif
320 };
321
322 #endif