use video stc for display. make skipping look a bit better.
[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_duration = fromBCD(duration>>16)*3600+fromBCD(duration>>8)*60+fromBCD(duration);
167         std::string country="de_DE";  // TODO use local data here
168         for (int i=0; i < MAX_LANG; i++)
169                 if (country==ISOtbl[i][0])
170                         if (loadLanguage(evt, ISOtbl[i][1], tsidonid))
171                                 return 0;
172         if (loadLanguage(evt, "eng", tsidonid))
173                 return 0;
174         if (loadLanguage(evt, std::string(), tsidonid))
175                 return 0;
176         return 0;
177 }
178
179 std::string eServiceEvent::getBeginTimeString() const
180 {
181         tm t;
182         localtime_r(&m_begin, &t);
183         char tmp[13];
184         snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
185                 t.tm_mday, t.tm_mon+1,
186                 t.tm_hour, t.tm_min);
187         return std::string(tmp, 12);
188 }
189
190 RESULT eServiceEvent::getComponentData(ePtr<eComponentData> &dest, int tagnum) const
191 {
192         std::list<eComponentData>::const_iterator it =
193                 m_component_data.begin();
194         for(;it != m_component_data.end(); ++it)
195         {
196                 if ( it->m_componentTag == tagnum )
197                 {
198                         dest=new eComponentData(*it);
199                         return 0;
200                 }
201         }
202         dest=0;
203         return -1;
204 }
205
206 RESULT eServiceEvent::getLinkageService(eServiceReference &service, int num) const
207 {
208         std::list<eServiceReference>::const_iterator it =
209                 m_linkage_services.begin();
210         while( it != m_linkage_services.end() && num-- )
211                 ++it;
212         if ( it != m_linkage_services.end() )
213         {
214                 service = *it;
215                 return 0;
216         }
217         service.type = eServiceReference::idInvalid;
218         return -1;
219 }
220
221 DEFINE_REF(eDebugClass);