enigma2 20130815 (master) -> 20130831 (master)
[enigma2.git] / usr / include / enigma2 / 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 <set>
7 #include <lib/base/buffer.h>
8 #include <lib/dvb/idvb.h>
9 #include <lib/dvb/dvb.h>
10 #include <lib/dvb/idemux.h>
11 #include <lib/dvb/esection.h>
12 #include <lib/python/python.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 struct channel_data: public sigc::trackable
26 {
27         ePtr<eDVBChannel> m_channel;
28         ePtr<eConnection> m_stateChangedConn;
29         int m_prevChannelState;
30         int m_dataDemux;
31 };
32
33 // TODO .. put all static stuff into a 'eDVBCAServiceHandler class'
34
35 typedef std::map<eServiceReferenceDVB, eDVBCAService*> CAServiceMap;
36 typedef std::map<iDVBChannel*, channel_data*> ChannelMap;
37
38 class eDVBCAService: public sigc::trackable
39 {
40         eIOBuffer m_buffer;
41         ePtr<eSocketNotifier> m_sn;
42         eServiceReferenceDVB m_service;
43         uint8_t m_used_demux[32];
44         unsigned int m_prev_build_hash;
45
46         int m_sock, m_clilen; 
47         struct sockaddr_un m_servaddr;
48         unsigned int m_sendstate;
49         unsigned char m_capmt[2048];
50         ePtr<eTimer> m_retryTimer;
51         void sendCAPMT();
52         void Connect();
53         void socketCB(int what);
54
55         static void DVBChannelAdded(eDVBChannel*);
56         static void DVBChannelStateChanged(iDVBChannel*);
57         static CAServiceMap exist;
58         static ChannelMap exist_channels;
59         static ePtr<eConnection> m_chanAddedConn;
60         static channel_data *getChannelData(eDVBChannelID &chid);
61
62         eDVBCAService();
63         ~eDVBCAService();
64 public:
65         static void registerChannelCallback(eDVBResourceManager *res_mgr);
66         static RESULT register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice );
67         static RESULT unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr );
68         void buildCAPMT(eTable<ProgramMapSection> *ptr);
69 };
70
71 #endif
72
73 class eDVBServicePMTHandler: public sigc::trackable
74 {
75 #ifndef SWIG
76         friend class eDVBCAService;
77         eServiceReferenceDVB m_reference;
78         ePtr<eDVBService> m_service;
79
80         static std::map<uint32_t, uint8_t> sdt_versions;
81
82         int m_last_channel_state;
83         eDVBCAService *m_ca_servicePtr;
84         ePtr<eDVBScan> m_dvb_scan; // for sdt scan
85
86         eAUTable<eTable<ProgramMapSection> > m_PMT;
87         eAUTable<eTable<ProgramAssociationSection> > m_PAT;
88
89         ePtr<eConnection> m_SDTConn;
90         ePtr<iDVBSectionReader> m_SDTReader;
91         void readSDTdata(const __u8 *data, int len);
92
93         eUsePtr<iDVBChannel> m_channel;
94         eUsePtr<iDVBPVRChannel> m_pvr_channel;
95         ePtr<eDVBResourceManager> m_resourceManager;
96         ePtr<iDVBDemux> m_demux, m_pvr_demux_tmp;
97
98         void channelStateChanged(iDVBChannel *);
99         ePtr<eConnection> m_channelStateChanged_connection;
100         void channelEvent(iDVBChannel *, int event);
101         ePtr<eConnection> m_channelEvent_connection;
102         void SDTScanEvent(int);
103         ePtr<eConnection> m_scan_event_connection;
104
105         void PMTready(int error);
106         void PATready(int error);
107         int m_pmt_pid;
108
109         int m_use_decode_demux;
110         uint8_t m_decode_demux_num;
111         ePtr<eTimer> m_no_pat_entry_delay;
112 public:
113         eDVBServicePMTHandler();
114         ~eDVBServicePMTHandler();
115 #endif
116
117 #ifdef SWIG
118 private:
119         eDVBServicePMTHandler();
120 public:
121 #endif
122
123         enum
124         {
125                 eventNoResources,  // a requested resource couldn't be allocated
126                 eventTuneFailed,   // tune failed
127                 eventNoPAT,        // no pat could be received (timeout)
128                 eventNoPATEntry,   // no pat entry for the corresponding SID could be found
129                 eventNoPMT,        // no pmt could be received (timeout)
130                 eventNewProgramInfo, // we just received a PMT
131                 eventTuned,        // a channel was sucessfully (re-)tuned in, you may start additional filters now
132                 eventNewSDT,
133                 
134                 eventPreStart,     // before start filepush thread
135                 eventSOF,          // seek pre start
136                 eventEOF,          // a file playback did end
137                 
138                 eventMisconfiguration, // a channel was not found in any list, or no frontend was found which could provide this channel
139         };
140 #ifndef SWIG
141         sigc::signal1<void,int> serviceEvent;
142
143         struct videoStream
144         {
145                 int pid;
146                 int component_tag;
147                 enum { vtMPEG2, vtMPEG4_H264, vtMPEG1, vtMPEG4_Part2, vtVC1, vtVC1_SM };
148                 int type;
149         };
150         
151         struct audioStream
152         {
153                 int pid,
154                     rdsPid; // hack for some radio services which transmit radiotext on different pid (i.e. harmony fm, HIT RADIO FFH, ...)
155                 enum { atMPEG, atAC3, atDTS, atAAC, atAACHE, atLPCM, atDTSHD };
156                 int type; // mpeg2, ac3, dts, ...
157                 
158                 int component_tag;
159                 std::string language_code; /* iso-639, if available. */
160         };
161
162         struct subtitleStream
163         {
164                 int pid;
165                 int subtitling_type;    /*  see ETSI EN 300 468 table 26 component_type
166                                                                         when stream_content is 0x03
167                                                                         0x10..0x13, 0x20..0x23 is used for dvb subtitles
168                                                                         0x01 is used for teletext subtitles */
169                 union
170                 {
171                         int composition_page_id;  // used for dvb subtitles
172                         int teletext_page_number;  // used for teletext subtitles
173                 };
174                 union
175                 {
176                         int ancillary_page_id;  // used for dvb subtitles
177                         int teletext_magazine_number;  // used for teletext subtitles
178                 };
179                 std::string language_code;
180                 bool operator<(const subtitleStream &s) const
181                 {
182                         if (pid != s.pid)
183                                 return pid < s.pid;
184                         if (teletext_page_number != s.teletext_page_number)
185                                 return teletext_page_number < s.teletext_page_number;
186                         return teletext_magazine_number < s.teletext_magazine_number;
187                 }
188         };
189
190         struct aitStream
191         {
192                 int pid;
193                 std::vector<std::pair<int, int> > type_version_pairs;
194         };
195
196         struct program
197         {
198                 struct capid_pair
199                 {
200                         uint16_t caid;
201                         int capid;
202                         bool operator< (const struct capid_pair &t) const { return t.caid < caid; }
203                 };
204                 std::vector<videoStream> videoStreams;
205                 std::vector<audioStream> audioStreams;
206                 int defaultAudioStream;
207                 std::vector<subtitleStream> subtitleStreams;
208                 std::vector<aitStream> aitStreams;
209                 std::list<capid_pair> caids;
210                 int pcrPid;
211                 int pmtPid;
212                 int textPid;
213                 bool isCrypted() { return !caids.empty(); }
214                 PyObject *createPythonObject();
215         };
216
217         int getProgramInfo(program &program);
218         int getDataDemux(ePtr<iDVBDemux> &demux);
219         int getDecodeDemux(ePtr<iDVBDemux> &demux);
220         PyObject *getCaIds(bool pair=false); // caid / ecmpid pair
221         
222         int getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel);
223         int getServiceReference(eServiceReferenceDVB &service) { service = m_reference; return 0; }
224         int getService(ePtr<eDVBService> &service) { service = m_service; return 0; }
225         int getPMT(ePtr<eTable<ProgramMapSection> > &ptr) { return m_PMT.getCurrent(ptr); }
226         int getChannel(eUsePtr<iDVBChannel> &channel);
227         void resetCachedProgram() { m_have_cached_program = false; }
228         void sendEventNoPatEntry();
229
230         /* deprecated interface */
231         int tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0);
232
233         /* new interface */
234         int tuneExt(eServiceReferenceDVB &ref, int use_decode_demux, sigc::slot1<ePtr<iTsSource>, const eServiceReferenceDVB&> createTsSource, const char *streaminfo_file, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0);
235
236         void free();
237 private:
238         bool m_have_cached_program;
239         program m_cached_program;
240 #endif
241 };
242
243 #endif