source: titan/titan/channel.h @ 15297

Last change on this file since 15297 was 15143, checked in by nit, 12 years ago

[titan] plugin optimize, plugin scriptexec, rcgui stanby fix, optimize transponder, optimize channel, optimize png render

File size: 10.0 KB
Line 
1#ifndef CHANNEL_H
2#define CHANNEL_H
3
4void debugchannel()
5{
6        struct channel* node = channel;
7
8        while(node != NULL)
9        {
10                printf("%s %p <%p >%p\n", node->name, node, node->prev, node->next);
11                node = node->next;
12        }
13}
14
15int channelnottunable(struct channel* node)
16{
17        if(node == NULL) return 1;
18        if(node->transponder == NULL) return 1;
19        if(node->transponder->tunablestatus == 0)
20        {
21                if(fegetfree(node->transponder, 1, NULL) == NULL)
22                {
23                        node->transponder->tunablestatus = 2;
24                        return 1;
25                }
26                else
27                {
28                        node->transponder->tunablestatus = 1;
29                        return 0;
30                }
31        }
32        else if(node->transponder->tunablestatus == 2)
33                return 1;
34
35        return 0;
36}
37
38struct channel* getlastchannel(struct channel* node)
39{
40        debug(1000, "in");
41        struct channel *prev = NULL;
42
43        m_lock(&status.channelmutex, 5);
44        while(node != NULL)
45        {
46                prev = node;
47                node = node->next;
48        }
49        m_unlock(&status.channelmutex, 5);
50
51        debug(1000, "out");
52        return prev;
53}
54
55struct channel* gettmpchannel()
56{
57        debug(1000, "in");
58        struct channel *node = channel;
59
60        m_lock(&status.channelmutex, 5);
61        while(node != NULL)
62        {
63                if(node->servicetype == 99)
64                        break;
65                node = node->next;
66        }
67        m_unlock(&status.channelmutex, 5);
68
69        debug(1000, "out");
70        return node;
71}
72
73int movechanneldown(struct channel* node)
74{
75        struct channel* prev = NULL, *next = NULL;
76
77        if(node == NULL || channel == NULL)
78        {
79                debug(1000, "NULL detect");
80                return 1;
81        }
82
83        m_lock(&status.channelmutex, 5);
84
85        //last node
86        if(node->next == NULL)
87        {
88                if(node->prev != NULL)
89                        node->prev->next = NULL;
90                node->prev = NULL;
91                node->next = channel;
92                channel->prev = node;
93                channel = node;
94                m_unlock(&status.channelmutex, 5);
95                return 0;
96        }
97
98        //haenge node aus
99        if(node->prev != NULL)
100                node->prev->next = node->next;
101        else
102                channel = node->next;
103        node->next->prev = node->prev;
104
105        //save nodes next and prev
106        next = node->next;
107        prev = node->prev;
108
109        //haenge es eine pos nacher ein
110        node->next = next->next;
111        node->prev = next;
112       
113        if(next->next != NULL)
114                next->next->prev = node;
115        next->next = node;
116
117        m_unlock(&status.channelmutex, 5);
118
119        status.writechannel = 1;
120        return 0;
121}
122
123int movechannelup(struct channel* node)
124{
125        struct channel* prev = NULL, *next = NULL, *last = NULL;
126
127        if(node == NULL || channel == NULL)
128        {
129                debug(1000, "NULL detect");
130                return 1;
131        }
132
133        m_lock(&status.channelmutex, 5);
134
135        //first node
136        if(node->prev == NULL)
137        {
138                last = getlastchannel(channel);
139
140                if(node->next != NULL)
141                        node->next->prev = NULL;
142                channel = node->next;
143                node->next = NULL;
144                last->next = node;
145                node->prev = last;
146                m_unlock(&status.channelmutex, 5);
147                return 0;
148        }
149
150        //haenge node aus
151        node->prev->next = node->next;
152        if(node->next != NULL)
153                node->next->prev = node->prev;
154
155        //save nodes next and prev
156        next = node->next;
157        prev = node->prev;
158
159        //haenge es eine pos voher ein
160        node->next = prev;
161        node->prev = prev->prev;
162       
163        if(prev->prev != NULL)
164                prev->prev->next = node;
165        else
166                channel = node;
167        prev->prev = node;
168
169        m_unlock(&status.channelmutex, 5);
170
171        status.writechannel = 1;
172        return 0;
173}
174
175struct channel* addchannel(char *line, int count, struct channel* last)
176{
177        //debug(1000, "in");
178        struct channel *newnode = NULL, *prev = NULL, *node = channel;
179        char *name = NULL;
180        int ret = 0;
181
182        if(line == NULL) return NULL;
183
184        newnode = (struct channel*)malloc(sizeof(struct channel));     
185        if(newnode == NULL)
186        {
187                err("no memory");
188                return NULL;
189        }
190
191        name = malloc(MINMALLOC);
192        if(name == NULL)
193        {
194                err("no memory");
195                free(newnode);
196                return NULL;
197        }
198
199        memset(newnode, 0, sizeof(struct channel));
200
201        ret = sscanf(line, "%[^#]#%lu#%d#%d#%d#%"SCNu8"#%"SCNu8"#%"SCNu8"#%"SCNu16"#%"SCNu16"#%"SCNu8, name, &newnode->transponderid, &newnode->providerid, &newnode->serviceid, &newnode->servicetype, &newnode->flag, &newnode->videocodec, &newnode->audiocodec, &newnode->videopid, &newnode->audiopid, &newnode->protect);
202        if(ret != 11 || getchannel(newnode->serviceid, newnode->transponderid) != NULL)
203        {
204                if(count > 0)
205                {
206                        err("channellist line %d not ok or double", count);
207                }
208                else
209                {
210                        err("add channel");
211                }
212                free(name);
213                free(newnode);
214                return NULL;
215        }
216
217        newnode->name = ostrcat(name, "", 1, 0);
218        //99 = tmp channel
219        if(newnode->servicetype != 99)
220        {
221                newnode->transponder = gettransponder(newnode->transponderid);
222                newnode->provider = getprovider(newnode->providerid);
223                status.writechannel = 1;
224        }
225
226        m_lock(&status.channelmutex, 5);
227
228        modifychannelcache(newnode->serviceid, newnode->transponderid, newnode);
229
230        if(last == NULL)
231        {
232                while(node != NULL && strcasecmp(newnode->name, node->name) > 0)
233                {
234                        prev = node;
235                        node = node->next;
236                }
237        }
238        else
239        {
240                prev = last;
241                node = last->next;
242        }
243
244        if(prev == NULL)
245                channel = newnode;
246        else
247        {
248                prev->next = newnode;
249                newnode->prev = prev;
250        }
251        newnode->next = node;
252        if(node != NULL) node->prev = newnode;
253       
254        m_unlock(&status.channelmutex, 5);
255        //debug(1000, "out");
256        return newnode;
257}
258
259struct channel* createchannel(char* name, unsigned long transponderid, int providerid, int serviceid, int servicetype, int flag, int videocodec, int audiocodec, int videopid, int audiopid, int protect)
260{
261        struct channel* chnode = NULL;
262        char* tmpstr = NULL;
263
264        tmpstr = ostrcat(tmpstr, name, 1, 0);
265        tmpstr = ostrcat(tmpstr, "#", 1, 0);
266        tmpstr = ostrcat(tmpstr, olutoa(transponderid), 1, 1);
267        tmpstr = ostrcat(tmpstr, "#", 1, 0);
268        tmpstr = ostrcat(tmpstr, oitoa(providerid), 1, 1);
269        tmpstr = ostrcat(tmpstr, "#", 1, 0);
270        tmpstr = ostrcat(tmpstr, oitoa(serviceid), 1, 1);
271        tmpstr = ostrcat(tmpstr, "#", 1, 0);
272        tmpstr = ostrcat(tmpstr, oitoa(servicetype), 1, 1);
273        tmpstr = ostrcat(tmpstr, "#", 1, 0);
274        tmpstr = ostrcat(tmpstr, oitoa(flag), 1, 1);
275        tmpstr = ostrcat(tmpstr, "#", 1, 0);
276        tmpstr = ostrcat(tmpstr, oitoa(videocodec), 1, 1);
277        tmpstr = ostrcat(tmpstr, "#", 1, 0);
278        tmpstr = ostrcat(tmpstr, oitoa(audiocodec), 1, 1);
279        tmpstr = ostrcat(tmpstr, "#", 1, 0);
280        tmpstr = ostrcat(tmpstr, oitoa(videopid), 1, 1);
281        tmpstr = ostrcat(tmpstr, "#", 1, 0);
282        tmpstr = ostrcat(tmpstr, oitoa(audiopid), 1, 1);
283        tmpstr = ostrcat(tmpstr, "#", 1, 0);
284        tmpstr = ostrcat(tmpstr, oitoa(protect), 1, 1);
285
286        chnode = addchannel(tmpstr, 1, NULL);
287
288        free(tmpstr);
289        return chnode;
290}
291
292int readchannel(const char* filename)
293{
294        debug(1000, "in");
295        FILE *fd = NULL;
296        char *fileline = NULL;
297        int linecount = 0;
298        struct channel* last = NULL, *tmplast = NULL;
299
300        fileline = malloc(MINMALLOC);
301        if(fileline == NULL)
302        {
303                err("no memory");
304                return 1;
305        }
306
307        fd = fopen(filename, "r");
308        if(fd == NULL)
309        {
310                perr("can't open %s", filename);
311                free(fileline);
312                return 1;
313        }
314
315        while(fgets(fileline, MINMALLOC, fd) != NULL)
316        {
317                if(fileline[0] == '#' || fileline[0] == '\n')
318                        continue;
319                if(fileline[strlen(fileline) - 1] == '\n')
320                        fileline[strlen(fileline) - 1] = '\0';
321                if(fileline[strlen(fileline) - 1] == '\r')
322                        fileline[strlen(fileline) - 1] = '\0';
323
324                linecount++;
325
326                if(last == NULL) last = tmplast;
327                last = addchannel(fileline, linecount, last);
328                if(last != NULL) tmplast = last;
329        }
330
331        status.writechannel = 0;
332        free(fileline);
333        fclose(fd);
334        return 0;
335}
336
337//flag 0: del bouquet
338//flag 1: don't del bouquet
339int delchannel(int serviceid, unsigned long transponderid, int flag)
340{
341        debug(1000, "in");
342        int ret = 1;
343        struct channel *node = channel, *prev = channel;
344        struct provider* providernode = NULL;
345
346        m_lock(&status.channelmutex, 5);
347
348        while(node != NULL)
349        {
350                if(node->serviceid == serviceid && node->transponderid == transponderid && getservicebychannel(node) == NULL)
351                {
352                        ret = 0;
353                        if(node->servicetype != 99) status.writechannel = 1;
354                        if(node == channel)
355                        {
356                                channel = node->next;
357                                if(channel != NULL)
358                                        channel->prev = NULL;
359                        }
360                        else
361                        {
362                                prev->next = node->next;
363                                if(node->next != NULL)
364                                        node->next->prev = prev;
365                        }
366
367                        if(flag == 0)
368                        {
369                                delbouquetbychannel(node->serviceid, node->transponderid);
370                                delepgscanlist(node->serviceid, node->transponderid);
371                        }
372                        else
373                                setbouquetchanneltonullmain(node->serviceid, node->transponderid);
374
375                        providernode = node->provider;
376                        delchannelcache(node->serviceid, node->transponderid);
377                        delchannelhistory(node);
378
379                        freeaudiotrack(node);
380                        free(node->audiotrack);
381                        node->audiotrack = NULL;
382
383                        freesubtitle(node);
384                        free(node->subtitle);
385                        node->subtitle = NULL;
386                       
387                        freelinkedchannel(node);
388                        free(node->linkedchannel);
389                        node->linkedchannel = NULL;
390
391                        freepmt(node);
392                        free(node->pmt);
393                        node->pmt = NULL;
394
395                        freecadesc(node);
396                        free(node->cadesc);
397                        node->cadesc = NULL;
398
399                        freeesinfo(node);
400                        free(node->esinfo);
401                        node->esinfo = NULL;
402
403                        freeepg(node);
404                        node->epg = NULL;
405
406                        free(node->name);
407                        node->name = NULL;
408
409                        free(node);
410                        node = NULL;
411
412                        delprovidernotused(providernode);
413                        break;
414                }
415
416                prev = node;
417                node = node->next;
418        }
419
420        recalcbouquetnr();
421        m_unlock(&status.channelmutex, 5);
422        debug(1000, "out");
423        return ret;
424}
425
426void delchannelbytransponder(unsigned long transponderid)
427{
428        debug(1000, "in");
429        struct channel *node = channel, *prev = channel;
430
431        while(node != NULL)
432        {
433                prev = node;
434                node = node->next;
435                if(prev != NULL && prev->transponderid == transponderid)
436                        delchannel(prev->serviceid, prev->transponderid, 0);
437        }
438        debug(1000, "out");
439}
440
441//flag 0: del bouquet
442//flag 1: don't del bouquet
443void freechannel(int flag)
444{
445        debug(1000, "in");
446        struct channel *node = channel, *prev = channel;
447
448        while(node != NULL)
449        {
450                prev = node;
451                node = node->next;
452                if(prev != NULL)
453                        delchannel(prev->serviceid, prev->transponderid, flag);
454        }
455        debug(1000, "out");
456}
457
458int writechannel(const char *filename)
459{
460        debug(1000, "in");
461        FILE *fd = NULL;
462        struct channel *node = channel;
463        int ret = 0;
464
465        fd = fopen(filename, "w");
466        if(fd == NULL)
467        {
468                perr("can't open %s", filename);
469                return 1;
470        }
471
472        m_lock(&status.channelmutex, 5);
473        while(node != NULL)
474        {
475                if(node->servicetype == 99)
476                {
477                        node = node->next;
478                        continue;
479                }
480                ret = fprintf(fd, "%s#%lu#%d#%d#%d#%d#%d#%d#%d#%d#%d\n", node->name, node->transponderid, node->providerid, node->serviceid, node->servicetype, node->flag, node->videocodec, node->audiocodec, node->videopid, node->audiopid, node->protect);
481                if(ret < 0)
482                {
483                        perr("writting file %s", filename);
484                }
485                node = node->next;
486        }
487        m_unlock(&status.channelmutex, 5);
488
489        fclose(fd);
490        debug(1000, "out");
491        return 0;
492}
493
494#endif
Note: See TracBrowser for help on using the repository browser.