Merge branch 'master' of /home/tmbinc/enigma2-git
[enigma2.git] / 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 <dvbsi++/program_map_section.h>
13 #include <dvbsi++/program_association_section.h>
14
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <sys/un.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20
21 class eDVBCAService;
22 class eDVBScan;
23
24 struct channel_data: public Object
25 {
26         ePtr<eDVBChannel> m_channel;
27         ePtr<eConnection> m_stateChangedConn;
28         int m_prevChannelState;
29         int m_dataDemux;
30 };
31
32 // TODO .. put all static stuff into a 'eDVBCAServiceHandler class'
33
34 typedef std::map<eServiceReferenceDVB, eDVBCAService*> CAServiceMap;
35 typedef std::map<iDVBChannel*, channel_data*> ChannelMap;
36
37 class eDVBCAService: public Object
38 {
39         eIOBuffer m_buffer;
40         ePtr<eSocketNotifier> m_sn;
41         eServiceReferenceDVB m_service;
42         uint8_t m_used_demux[32];
43         unsigned int m_prev_build_hash;
44
45         int m_sock, m_clilen; 
46         struct sockaddr_un m_servaddr;
47         unsigned int m_sendstate;
48         unsigned char m_capmt[2048];
49         ePtr<eTimer> m_retryTimer;
50         void sendCAPMT();
51         void Connect();
52         void socketCB(int what);
53
54         static void DVBChannelAdded(eDVBChannel*);
55         static void DVBChannelStateChanged(iDVBChannel*);
56         static CAServiceMap exist;
57         static ChannelMap exist_channels;
58         static ePtr<eConnection> m_chanAddedConn;
59         static channel_data *getChannelData(eDVBChannelID &chid);
60
61         eDVBCAService();
62         ~eDVBCAService();
63 public:
64         static void registerChannelCallback(eDVBResourceManager *res_mgr);
65         static RESULT register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice );
66         static RESULT unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr );
67         void buildCAPMT(eTable<ProgramMapSection> *ptr);
68 };
69
70 #endif
71
72 class eDVBServicePMTHandler: public Object
73 {
74 #ifndef SWIG
75         friend class eDVBCAService;
76         eServiceReferenceDVB m_reference;
77         ePtr<eDVBService> m_service;
78
79         int m_last_channel_state;
80         eDVBCAService *m_ca_servicePtr;
81         ePtr<eDVBScan> m_dvb_scan; // for sdt scan
82
83         eAUTable<eTable<ProgramMapSection> > m_PMT;
84         eAUTable<eTable<ProgramAssociationSection> > m_PAT;
85
86         eUsePtr<iDVBChannel> m_channel;
87         eUsePtr<iDVBPVRChannel> m_pvr_channel;
88         ePtr<eDVBResourceManager> m_resourceManager;
89         ePtr<iDVBDemux> m_demux;
90         
91         void channelStateChanged(iDVBChannel *);
92         ePtr<eConnection> m_channelStateChanged_connection;
93         void channelEvent(iDVBChannel *, int event);
94         ePtr<eConnection> m_channelEvent_connection;
95         void SDTScanEvent(int);
96         ePtr<eConnection> m_scan_event_connection;
97
98         void PMTready(int error);
99         void PATready(int error);
100         
101         int m_pmt_pid;
102         
103         int m_use_decode_demux;
104         uint8_t m_decode_demux_num;
105 public:
106         eDVBServicePMTHandler();
107         ~eDVBServicePMTHandler();
108 #endif
109
110 #ifdef SWIG
111 private:
112         eDVBServicePMTHandler();
113 public:
114 #endif
115
116         enum
117         {
118                 eventNoResources,  // a requested resource couldn't be allocated
119                 eventTuneFailed,   // tune failed
120                 eventNoPAT,        // no pat could be received (timeout)
121                 eventNoPATEntry,   // no pat entry for the corresponding SID could be found
122                 eventNoPMT,        // no pmt could be received (timeout)
123                 eventNewProgramInfo, // we just received a PMT
124                 eventTuned,        // a channel was sucessfully (re-)tuned in, you may start additional filters now
125                 
126                 eventSOF,          // seek pre start
127                 eventEOF,          // a file playback did end
128                 
129                 eventMisconfiguration, // a channel was not found in any list, or no frontend was found which could provide this channel
130         };
131 #ifndef SWIG
132         Signal1<void,int> serviceEvent;
133
134         struct videoStream
135         {
136                 int pid;
137                 int component_tag;
138                 enum { vtMPEG2, vtMPEG4_H264, vtMPEG1, vtMPEG4_Part2, vtVC1, vtVC1_SM };
139                 int type;
140         };
141         
142         struct audioStream
143         {
144                 int pid;
145                 enum { atMPEG, atAC3, atDTS, atAAC, atAACHE };
146                 int type; // mpeg2, ac3, dts, ...
147                 
148                 int component_tag;
149                 std::string language_code; /* iso-639, if available. */
150         };
151
152         struct subtitleStream
153         {
154                 int pid;
155                 int subtitling_type;    /*  see ETSI EN 300 468 table 26 component_type
156                                                                         when stream_content is 0x03
157                                                                         0x10..0x13, 0x20..0x23 is used for dvb subtitles
158                                                                         0x01 is used for teletext subtitles */
159                 union
160                 {
161                         int composition_page_id;  // used for dvb subtitles
162                         int teletext_page_number;  // used for teletext subtitles
163                 };
164                 union
165                 {
166                         int ancillary_page_id;  // used for dvb subtitles
167                         int teletext_magazine_number;  // used for teletext subtitles
168                 };
169                 std::string language_code;
170                 bool operator<(const subtitleStream &s) const
171                 {
172                         if (pid != s.pid)
173                                 return pid < s.pid;
174                         if (teletext_page_number != s.teletext_page_number)
175                                 return teletext_page_number < s.teletext_page_number;
176                         return teletext_magazine_number < s.teletext_magazine_number;
177                 }
178         };
179
180         struct program
181         {
182                 std::vector<videoStream> videoStreams;
183                 std::vector<audioStream> audioStreams;
184                 int defaultAudioStream;
185                 std::vector<subtitleStream> subtitleStreams;
186                 std::set<uint16_t> caids;
187                 int pcrPid;
188                 int pmtPid;
189                 int textPid;
190                 bool isCrypted() { return !caids.empty(); }
191                 
192                 PyObject *createPythonObject();
193         };
194
195         int getProgramInfo(struct program &program);
196         int getDataDemux(ePtr<iDVBDemux> &demux);
197         int getDecodeDemux(ePtr<iDVBDemux> &demux);
198         PyObject *getCaIds();
199         
200         int getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel);
201         int getServiceReference(eServiceReferenceDVB &service) { service = m_reference; return 0; }
202         int getService(ePtr<eDVBService> &service) { service = m_service; return 0; }
203         int getPMT(ePtr<eTable<ProgramMapSection> > &ptr) { return m_PMT.getCurrent(ptr); }
204         int getChannel(eUsePtr<iDVBChannel> &channel);
205         void resetCachedProgram() { m_have_cached_program = false; }
206
207         int tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0);
208         void free();
209 private:
210         bool m_have_cached_program;
211         program m_cached_program;
212 #endif
213 };
214
215 #endif