- add opera browser.(with hbbtv)
[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         std::string m_HbbTVUrl;
102         std::string m_ApplicationName;
103 public:
104         HbbTVApplicationInfo(int controlCode, int orgid, int appid, std::string hbbtvUrl, std::string applicationName)
105                 : m_ControlCode(controlCode), m_HbbTVUrl(hbbtvUrl), m_ApplicationName(applicationName), m_OrgId(orgid), m_AppId(appid)
106         {}
107 };
108 typedef std::list<HbbTVApplicationInfo *> HbbTVApplicationInfoList;
109 typedef HbbTVApplicationInfoList::iterator HbbTVApplicationInfoListIterator;
110 typedef HbbTVApplicationInfoList::const_iterator HbbTVApplicationInfoListConstIterator;
111
112 class eDVBServicePMTHandler: public Object
113 {
114 #ifndef SWIG
115         friend class eDVBCAService;
116         eServiceReferenceDVB m_reference;
117         ePtr<eDVBService> m_service;
118
119         int m_last_channel_state;
120         eDVBCAService *m_ca_servicePtr;
121         ePtr<eDVBScan> m_dvb_scan; // for sdt scan
122
123         eAUTable<eTable<ProgramMapSection> > m_PMT;
124         eAUTable<eTable<ProgramAssociationSection> > m_PAT;
125
126         eUsePtr<iDVBChannel> m_channel;
127         eUsePtr<iDVBPVRChannel> m_pvr_channel;
128         ePtr<eDVBResourceManager> m_resourceManager;
129         ePtr<iDVBDemux> m_demux, m_pvr_demux_tmp;
130
131         void channelStateChanged(iDVBChannel *);
132         ePtr<eConnection> m_channelStateChanged_connection;
133         void channelEvent(iDVBChannel *, int event);
134         ePtr<eConnection> m_channelEvent_connection;
135         void SDTScanEvent(int);
136         ePtr<eConnection> m_scan_event_connection;
137
138         eAUTable<eTable<ApplicationInformationSection> > m_AIT;
139         eAUTable<eTable<OCSection> > m_OC;
140
141         void PMTready(int error);
142         void PATready(int error);
143         
144         int m_pmt_pid;
145         
146         void AITready(int error);
147         void OCready(int error);
148         int m_dsmcc_pid;
149         int m_ait_pid;
150         HbbTVApplicationInfoList m_HbbTVApplications;
151         std::string m_HBBTVUrl;
152         std::string m_ApplicationName;
153         unsigned char m_AITData[4096];
154         
155         int m_use_decode_demux;
156         uint8_t m_decode_demux_num;
157         ePtr<eTimer> m_no_pat_entry_delay;
158 public:
159         eDVBServicePMTHandler();
160         ~eDVBServicePMTHandler();
161 #endif
162
163 #ifdef SWIG
164 private:
165         eDVBServicePMTHandler();
166 public:
167 #endif
168
169         enum
170         {
171                 eventNoResources,  // a requested resource couldn't be allocated
172                 eventTuneFailed,   // tune failed
173                 eventNoPAT,        // no pat could be received (timeout)
174                 eventNoPATEntry,   // no pat entry for the corresponding SID could be found
175                 eventNoPMT,        // no pmt could be received (timeout)
176                 eventNewProgramInfo, // we just received a PMT
177                 eventTuned,        // a channel was sucessfully (re-)tuned in, you may start additional filters now
178                 
179                 eventPreStart,     // before start filepush thread
180                 eventSOF,          // seek pre start
181                 eventEOF,          // a file playback did end
182                 
183                 eventHBBTVInfo, /* HBBTV information was detected in the AIT */
184                 
185                 eventMisconfiguration, // a channel was not found in any list, or no frontend was found which could provide this channel
186         };
187 #ifndef SWIG
188         Signal1<void,int> serviceEvent;
189
190         struct videoStream
191         {
192                 int pid;
193                 int component_tag;
194                 enum { vtMPEG2, vtMPEG4_H264, vtMPEG1, vtMPEG4_Part2, vtVC1, vtVC1_SM };
195                 int type;
196         };
197         
198         struct audioStream
199         {
200                 int pid,
201                     rdsPid; // hack for some radio services which transmit radiotext on different pid (i.e. harmony fm, HIT RADIO FFH, ...)
202                 enum { atMPEG, atAC3, atDTS, atAAC, atAACHE, atLPCM, atDTSHD, atDDP  };
203                 int type; // mpeg2, ac3, dts, ...
204                 
205                 int component_tag;
206                 std::string language_code; /* iso-639, if available. */
207         };
208
209         struct subtitleStream
210         {
211                 int pid;
212                 int subtitling_type;    /*  see ETSI EN 300 468 table 26 component_type
213                                                                         when stream_content is 0x03
214                                                                         0x10..0x13, 0x20..0x23 is used for dvb subtitles
215                                                                         0x01 is used for teletext subtitles */
216                 union
217                 {
218                         int composition_page_id;  // used for dvb subtitles
219                         int teletext_page_number;  // used for teletext subtitles
220                 };
221                 union
222                 {
223                         int ancillary_page_id;  // used for dvb subtitles
224                         int teletext_magazine_number;  // used for teletext subtitles
225                 };
226                 std::string language_code;
227                 bool operator<(const subtitleStream &s) const
228                 {
229                         if (pid != s.pid)
230                                 return pid < s.pid;
231                         if (teletext_page_number != s.teletext_page_number)
232                                 return teletext_page_number < s.teletext_page_number;
233                         return teletext_magazine_number < s.teletext_magazine_number;
234                 }
235         };
236
237         struct program
238         {
239                 struct capid_pair
240                 {
241                         uint16_t caid;
242                         int capid;
243                         bool operator< (const struct capid_pair &t) const { return t.caid < caid; }
244                 };
245                 std::vector<videoStream> videoStreams;
246                 std::vector<audioStream> audioStreams;
247                 int defaultAudioStream;
248                 std::vector<subtitleStream> subtitleStreams;
249                 std::list<capid_pair> caids;
250                 int pcrPid;
251                 int pmtPid;
252                 int textPid;
253                 int aitPid;
254                 bool isCrypted() { return !caids.empty(); }
255                 PyObject *createPythonObject();
256         };
257
258         int getProgramInfo(program &program);
259         int getDataDemux(ePtr<iDVBDemux> &demux);
260         int getDecodeDemux(ePtr<iDVBDemux> &demux);
261         PyObject *getCaIds(bool pair=false); // caid / ecmpid pair
262         PyObject *getHbbTVApplications(void); 
263         
264         int getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel);
265         int getServiceReference(eServiceReferenceDVB &service) { service = m_reference; return 0; }
266         int getService(ePtr<eDVBService> &service) { service = m_service; return 0; }
267         int getPMT(ePtr<eTable<ProgramMapSection> > &ptr) { return m_PMT.getCurrent(ptr); }
268         int getChannel(eUsePtr<iDVBChannel> &channel);
269         void resetCachedProgram() { m_have_cached_program = false; }
270         void sendEventNoPatEntry();
271
272         void getHBBTVUrl(std::string &ret) { ret = m_HBBTVUrl; }
273
274         /* deprecated interface */
275         int tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0);
276
277         /* new interface */
278         int tuneExt(eServiceReferenceDVB &ref, int use_decode_demux, ePtr<iTsSource> &, const char *streaminfo_file, eCueSheet *sg=0, bool simulate=false, eDVBService *service = 0, bool isstreamclient=false);
279
280         void free();
281 private:
282         bool m_have_cached_program;
283         program m_cached_program;
284         bool m_isstreamclient;
285 #endif
286 };
287
288 #endif