enigma2 20130907 (master) -> 20130927 (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         static bool ddp_support;
82
83         int m_last_channel_state;
84         eDVBCAService *m_ca_servicePtr;
85         ePtr<eDVBScan> m_dvb_scan; // for sdt scan
86
87         eAUTable<eTable<ProgramMapSection> > m_PMT;
88         eAUTable<eTable<ProgramAssociationSection> > m_PAT;
89
90         ePtr<eConnection> m_SDTConn;
91         ePtr<iDVBSectionReader> m_SDTReader;
92         void readSDTdata(const __u8 *data, int len);
93
94         eUsePtr<iDVBChannel> m_channel;
95         eUsePtr<iDVBPVRChannel> m_pvr_channel;
96         ePtr<eDVBResourceManager> m_resourceManager;
97         ePtr<iDVBDemux> m_demux, m_pvr_demux_tmp;
98
99         void channelStateChanged(iDVBChannel *);
100         ePtr<eConnection> m_channelStateChanged_connection;
101         void channelEvent(iDVBChannel *, int event);
102         ePtr<eConnection> m_channelEvent_connection;
103         void SDTScanEvent(int);
104         ePtr<eConnection> m_scan_event_connection;
105
106         void PMTready(int error);
107         void PATready(int error);
108         int m_pmt_pid;
109
110         int m_use_decode_demux;
111         uint8_t m_decode_demux_num;
112         ePtr<eTimer> m_no_pat_entry_delay;
113 public:
114         eDVBServicePMTHandler();
115         ~eDVBServicePMTHandler();
116 #endif
117
118 #ifdef SWIG
119 private:
120         eDVBServicePMTHandler();
121 public:
122 #endif
123         static void setDDPSupport(bool b) { ddp_support = b; }
124
125         enum
126         {
127                 eventNoResources,  // a requested resource couldn't be allocated
128                 eventTuneFailed,   // tune failed
129                 eventNoPAT,        // no pat could be received (timeout)
130                 eventNoPATEntry,   // no pat entry for the corresponding SID could be found
131                 eventNoPMT,        // no pmt could be received (timeout)
132                 eventNewProgramInfo, // we just received a PMT
133                 eventTuned,        // a channel was sucessfully (re-)tuned in, you may start additional filters now
134                 eventNewSDT,
135                 
136                 eventPreStart,     // before start filepush thread
137                 eventSOF,          // seek pre start
138                 eventEOF,          // a file playback did end
139                 
140                 eventMisconfiguration, // a channel was not found in any list, or no frontend was found which could provide this channel
141         };
142 #ifndef SWIG
143         sigc::signal1<void,int> serviceEvent;
144
145         struct videoStream
146         {
147                 int pid;
148                 int component_tag;
149                 enum { vtMPEG2, vtMPEG4_H264, vtMPEG1, vtMPEG4_Part2, vtVC1, vtVC1_SM };
150                 int type;
151         };
152         
153         struct audioStream
154         {
155                 int pid,
156                     rdsPid; // hack for some radio services which transmit radiotext on different pid (i.e. harmony fm, HIT RADIO FFH, ...)
157                 enum { atMPEG, atAC3, atDTS, atAAC, atAACHE, atLPCM, atDTSHD, atDDP };
158                 int type; // mpeg2, ac3, dts, ...
159                 
160                 int component_tag;
161                 std::string language_code; /* iso-639, if available. */
162         };
163
164         struct subtitleStream
165         {
166                 int pid;
167                 int subtitling_type;    /*  see ETSI EN 300 468 table 26 component_type
168                                                                         when stream_content is 0x03
169                                                                         0x10..0x13, 0x20..0x23 is used for dvb subtitles
170                                                                         0x01 is used for teletext subtitles */
171                 union
172                 {
173                         int composition_page_id;  // used for dvb subtitles
174                         int teletext_page_number;  // used for teletext subtitles
175                 };
176                 union
177                 {
178                         int ancillary_page_id;  // used for dvb subtitles
179                         int teletext_magazine_number;  // used for teletext subtitles
180                 };
181                 std::string language_code;
182                 bool operator<(const subtitleStream &s) const
183                 {
184                         if (pid != s.pid)
185                                 return pid < s.pid;
186                         if (teletext_page_number != s.teletext_page_number)
187                                 return teletext_page_number < s.teletext_page_number;
188                         return teletext_magazine_number < s.teletext_magazine_number;
189                 }
190         };
191
192         struct aitStream
193         {
194                 int pid;
195                 std::vector<std::pair<int, int> > type_version_pairs;
196         };
197
198         struct program
199         {
200                 struct capid_pair
201                 {
202                         uint16_t caid;
203                         int capid;
204                         bool operator< (const struct capid_pair &t) const { return t.caid < caid; }
205                 };
206                 std::vector<videoStream> videoStreams;
207                 std::vector<audioStream> audioStreams;
208                 int defaultAudioStream;
209                 std::vector<subtitleStream> subtitleStreams;
210                 std::vector<aitStream> aitStreams;
211                 std::list<capid_pair> caids;
212                 int pcrPid;
213                 int pmtPid;
214                 int textPid;
215                 bool isCrypted() { return !caids.empty(); }
216                 PyObject *createPythonObject();
217         };
218
219         int getProgramInfo(program &program);
220         int getDataDemux(ePtr<iDVBDemux> &demux);
221         int getDecodeDemux(ePtr<iDVBDemux> &demux);
222         PyObject *getCaIds(bool pair=false); // caid / ecmpid pair
223         
224         int getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel);
225         int getServiceReference(eServiceReferenceDVB &service) { service = m_reference; return 0; }
226         int getService(ePtr<eDVBService> &service) { service = m_service; return 0; }
227         int getPMT(ePtr<eTable<ProgramMapSection> > &ptr) { return m_PMT.getCurrent(ptr); }
228         int getChannel(eUsePtr<iDVBChannel> &channel);
229         void resetCachedProgram() { m_have_cached_program = false; }
230         void sendEventNoPatEntry();
231
232         /* deprecated interface */
233         int tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0);
234
235         /* new interface */
236         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);
237
238         void free();
239 private:
240         bool m_have_cached_program;
241         program m_cached_program;
242 #endif
243 };
244
245 #endif