predeclare eDVBCAService because it is used by the type definition of CAServiceMap,
[enigma2.git] / lib / service / event.cpp
1 #include <lib/service/event.h>
2 #include <lib/base/estring.h>
3 #include <lib/base/encoding.h>
4 #include <lib/dvb/dvbtime.h>
5 #include <lib/dvb/idvb.h>
6 #include <dvbsi++/event_information_section.h>
7 #include <dvbsi++/short_event_descriptor.h>
8 #include <dvbsi++/extended_event_descriptor.h>
9 #include <dvbsi++/linkage_descriptor.h>
10 #include <dvbsi++/component_descriptor.h>
11 #include <dvbsi++/descriptor_tag.h>
12
13 DEFINE_REF(eServiceEvent);
14 DEFINE_REF(eComponentData);
15
16 const char MAX_LANG = 37;
17 /* OSD language (see /share/locales/locales) to iso639 conversion table */
18 std::string ISOtbl[MAX_LANG][2] =
19 {
20         {"ar_AE","ara"},
21         {"C","eng"},
22         {"cs_CZ","ces"},     /* or 'cze' */
23         {"cs_CZ","cze"},
24         {"da_DK","dan"},
25         {"de_DE","deu"},     /* also 'ger' is valid iso639 code!! */
26         {"de_DE","ger"},
27         {"el_GR","gre"},     /* also 'ell' is valid */
28         {"el_GR","ell"},
29         {"es_ES","esl"},     /* also 'spa' is ok */
30         {"es_ES","spa"},
31         {"et_EE","est"},
32         {"fi_FI","fin"},
33         {"fr_FR","fra"},
34         {"hr_HR","hrv"},     /* or 'scr' */
35         {"hr_HR","scr"},
36         {"hu_HU","hun"},
37         {"is_IS","isl"},     /* or 'ice' */
38         {"is_IS","ice"},
39         {"it_IT","ita"},
40         {"lt_LT","lit"},
41         {"nl_NL","nld"},     /* or 'dut' */
42         {"nl_NL","dut"},
43         {"no_NO","nor"},
44         {"pl_PL","pol"},
45         {"pt_PT","por"},
46         {"ro_RO","ron"},     /* or 'rum' */
47         {"ro_RO","rum"},
48         {"ru_RU","rus"},
49         {"sk_SK","slk"},     /* or 'slo' */
50         {"sk_SK","slo"},
51         {"sl_SI","slv"},
52         {"sr_YU","srp"},     /* or 'scc' */
53         {"sr_YU","scc"},
54         {"sv_SE","swe"},
55         {"tr_TR","tur"},
56         {"ur_IN","urd"}
57 };
58
59 /* search for the presence of language from given EIT event descriptors*/
60 bool eServiceEvent::loadLanguage(Event *evt, std::string lang, int tsidonid)
61 {
62         bool retval=0;
63         for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
64         {
65                 switch ((*desc)->getTag())
66                 {
67                         case LINKAGE_DESCRIPTOR:
68                                 m_linkage_services.clear();
69                                 break;
70                         case SHORT_EVENT_DESCRIPTOR:
71                         {
72                                 const ShortEventDescriptor *sed = (ShortEventDescriptor*)*desc;
73                                 const std::string &cc = sed->getIso639LanguageCode();
74                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
75                                 if (lang.empty() || cc == lang)
76                                 {
77                                         m_event_name = convertDVBUTF8(sed->getEventName(), table, tsidonid);
78                                         m_short_description = convertDVBUTF8(sed->getText(), table, tsidonid);
79                                         retval=1;
80                                 }
81                                 break;
82                         }
83                         case EXTENDED_EVENT_DESCRIPTOR:
84                         {
85                                 const ExtendedEventDescriptor *eed = (ExtendedEventDescriptor*)*desc;
86                                 const std::string &cc = eed->getIso639LanguageCode();
87                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
88                                 if (lang.empty() || cc == lang)
89                                 {
90                                         m_extended_description += convertDVBUTF8(eed->getText(), table, tsidonid);
91                                         retval=1;
92                                 }
93 #if 0
94                                 const ExtendedEventList *itemlist = eed->getItems();
95                                 for (ExtendedEventConstIterator it = itemlist->begin(); it != itemlist->end(); ++it)
96                                 {
97                                         m_extended_description += '\n';
98                                         m_extended_description += convertDVBUTF8((*it)->getItemDescription());
99                                         m_extended_description += ' ';
100                                         m_extended_description += convertDVBUTF8((*it)->getItem());
101                                 }
102 #endif
103                                 break;
104                         }
105                         default:
106                                 break;
107                 }
108         }
109         if ( retval == 1 )
110         {
111                 for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
112                 {
113                         switch ((*desc)->getTag())
114                         {
115                                 case COMPONENT_DESCRIPTOR:
116                                 {
117                                         const ComponentDescriptor *cp = (ComponentDescriptor*)*desc;
118                                         eComponentData data;
119                                         data.m_streamContent = cp->getStreamContent();
120                                         data.m_componentType = cp->getComponentType();
121                                         data.m_componentTag = cp->getComponentTag();
122                                         data.m_iso639LanguageCode = cp->getIso639LanguageCode();
123                                         int table=encodingHandler.getCountryCodeDefaultMapping(data.m_iso639LanguageCode);
124                                         data.m_text = convertDVBUTF8(cp->getText(),table,tsidonid);
125                                         m_component_data.push_back(data);
126                                         break;
127                                 }
128                                 case LINKAGE_DESCRIPTOR:
129                                 {
130                                         const LinkageDescriptor  *ld = (LinkageDescriptor*)*desc;
131                                         if ( ld->getLinkageType() == 0xB0 )
132                                         {
133                                                 eServiceReference ref;
134                                                 ref.type = eServiceReference::idDVB;
135                                                 eServiceReferenceDVB &dvb_ref = (eServiceReferenceDVB&) ref;
136                                                 dvb_ref.setServiceType(1);
137                                                 dvb_ref.setTransportStreamID(ld->getTransportStreamId());
138                                                 dvb_ref.setOriginalNetworkID(ld->getOriginalNetworkId());
139                                                 dvb_ref.setServiceID(ld->getServiceId());
140                                                 const PrivateDataByteVector *privateData = ld->getPrivateDataBytes();
141                                                 dvb_ref.name = convertDVBUTF8((const unsigned char*)&((*privateData)[0]), privateData->size(), 0, tsidonid);
142                                                 m_linkage_services.push_back(ref);
143                                         }
144                                         break;
145                                 }
146                         }
147                 }
148         }
149         if ( m_extended_description.find(m_short_description) == 0 )
150                 m_short_description="";
151         return retval;
152 }
153
154 RESULT eServiceEvent::parseFrom(Event *evt, int tsidonid)
155 {
156         uint16_t stime_mjd = evt->getStartTimeMjd();
157         uint32_t stime_bcd = evt->getStartTimeBcd();
158         uint32_t duration = evt->getDuration();
159         m_begin = parseDVBtime(
160                 stime_mjd >> 8,
161                 stime_mjd&0xFF,
162                 stime_bcd >> 16,
163                 (stime_bcd >> 8)&0xFF,
164                 stime_bcd & 0xFF
165         );
166         m_event_id = evt->getEventId();
167         m_duration = fromBCD(duration>>16)*3600+fromBCD(duration>>8)*60+fromBCD(duration);
168         std::string country="de_DE";  // TODO use local data here
169         for (int i=0; i < MAX_LANG; i++)
170                 if (country==ISOtbl[i][0])
171                         if (loadLanguage(evt, ISOtbl[i][1], tsidonid))
172                                 return 0;
173         if (loadLanguage(evt, "eng", tsidonid))
174                 return 0;
175         if (loadLanguage(evt, std::string(), tsidonid))
176                 return 0;
177         return 0;
178 }
179
180 std::string eServiceEvent::getBeginTimeString() const
181 {
182         tm t;
183         localtime_r(&m_begin, &t);
184         char tmp[13];
185         snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
186                 t.tm_mday, t.tm_mon+1,
187                 t.tm_hour, t.tm_min);
188         return std::string(tmp, 12);
189 }
190
191 RESULT eServiceEvent::getComponentData(ePtr<eComponentData> &dest, int tagnum) const
192 {
193         std::list<eComponentData>::const_iterator it =
194                 m_component_data.begin();
195         for(;it != m_component_data.end(); ++it)
196         {
197                 if ( it->m_componentTag == tagnum )
198                 {
199                         dest=new eComponentData(*it);
200                         return 0;
201                 }
202         }
203         dest=0;
204         return -1;
205 }
206
207 RESULT eServiceEvent::getLinkageService(eServiceReference &service, eServiceReference &parent, int num) const
208 {
209         std::list<eServiceReference>::const_iterator it =
210                 m_linkage_services.begin();
211         while( it != m_linkage_services.end() && num-- )
212                 ++it;
213         if ( it != m_linkage_services.end() )
214         {
215                 service = *it;
216                 eServiceReferenceDVB &subservice = (eServiceReferenceDVB&) service;
217                 eServiceReferenceDVB &current = (eServiceReferenceDVB&) parent;
218                 subservice.setDVBNamespace(current.getDVBNamespace());
219                 if ( current.getParentTransportStreamID().get() )
220                 {
221                         subservice.setParentTransportStreamID( current.getParentTransportStreamID() );
222                         subservice.setParentServiceID( current.getParentServiceID() );
223                 }
224                 else
225                 {
226                         subservice.setParentTransportStreamID( current.getTransportStreamID() );
227                         subservice.setParentServiceID( current.getServiceID() );
228                 }
229                 if ( subservice.getParentTransportStreamID() == subservice.getTransportStreamID() &&
230                         subservice.getParentServiceID() == subservice.getServiceID() )
231                 {
232                         subservice.setParentTransportStreamID( eTransportStreamID(0) );
233                         subservice.setParentServiceID( eServiceID(0) );
234                 }
235                 return 0;
236         }
237         service.type = eServiceReference::idInvalid;
238         return -1;
239 }
240
241 DEFINE_REF(eDebugClass);