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