epg: Keep track of next event for all channels. (Useful if there is no current event we can refer to)

htsp: Add nextEventId to HTSP channel updates
This commit is contained in:
Andreas Öman 2011-03-07 20:58:07 +01:00
parent b80b3e0fbc
commit 069ac95251
5 changed files with 32 additions and 17 deletions

View file

@ -45,6 +45,7 @@ typedef struct channel {
struct event_tree ch_epg_events;
struct event *ch_epg_current;
struct event *ch_epg_next;
gtimer_t ch_epg_timer_head;
gtimer_t ch_epg_timer_current;

View file

@ -53,16 +53,20 @@ e_ch_cmp(const event_t *a, const event_t *b)
*
*/
static void
epg_set_current(channel_t *ch, event_t *e)
epg_set_current(channel_t *ch, event_t *e, event_t *next)
{
if(ch->ch_epg_current == e)
if(next == NULL && e != NULL)
next = RB_NEXT(e, e_channel_link);
if(ch->ch_epg_current == e && ch->ch_epg_next == next)
return;
ch->ch_epg_current = e;
ch->ch_epg_next = next;
if(e != NULL)
gtimer_arm_abs(&ch->ch_epg_timer_current, epg_ch_check_current_event,
ch, MAX(e->e_stop, dispatch_clock + 1));
htsp_event_update(ch, e);
htsp_channgel_update_current(ch);
}
/**
@ -73,17 +77,16 @@ epg_ch_check_current_event(void *aux)
{
channel_t *ch = aux;
event_t skel, *e = ch->ch_epg_current;
if(e != NULL) {
if(e->e_start <= dispatch_clock && e->e_stop > dispatch_clock) {
epg_set_current(ch, e);
epg_set_current(ch, e, NULL);
return;
}
if((e = RB_NEXT(e, e_channel_link)) != NULL) {
if(e->e_start <= dispatch_clock && e->e_stop > dispatch_clock) {
epg_set_current(ch, e);
epg_set_current(ch, e, NULL);
return;
}
}
@ -91,22 +94,22 @@ epg_ch_check_current_event(void *aux)
e = epg_event_find_by_time(ch, dispatch_clock);
if(e != NULL) {
epg_set_current(ch, e);
epg_set_current(ch, e, NULL);
return;
}
epg_set_current(ch, NULL);
skel.e_start = dispatch_clock;
e = RB_FIND_GT(&ch->ch_epg_events, &skel, e_channel_link, e_ch_cmp);
if(e != NULL) {
gtimer_arm_abs(&ch->ch_epg_timer_current, epg_ch_check_current_event,
ch, MAX(e->e_start, dispatch_clock + 1));
epg_set_current(ch, NULL, e);
} else {
epg_set_current(ch, NULL, NULL);
}
}
/**
*
*/
@ -303,11 +306,13 @@ epg_remove_event_from_channel(channel_t *ch, event_t *e)
epg_event_unref(e);
if(ch->ch_epg_current == e) {
epg_set_current(ch, NULL);
epg_set_current(ch, NULL, n);
if(n != NULL)
gtimer_arm_abs(&ch->ch_epg_timer_current, epg_ch_check_current_event,
ch, n->e_start);
} else if(ch->ch_epg_next == e) {
epg_set_current(ch, ch->ch_epg_current, NULL);
}
if(wasfirst && (e = RB_FIRST(&ch->ch_epg_events)) != NULL) {
@ -645,7 +650,12 @@ epg_load(void)
void
epg_init(void)
{
channel_t *ch;
epg_load();
RB_FOREACH(ch, &channel_name_tree, ch_name_link)
epg_ch_check_current_event(ch);
}

View file

@ -309,6 +309,8 @@ htsp_build_channel(channel_t *ch, const char *method)
htsmsg_add_u32(out, "eventId",
ch->ch_epg_current != NULL ? ch->ch_epg_current->e_id : 0);
htsmsg_add_u32(out, "nextEventId",
ch->ch_epg_next ? ch->ch_epg_next->e_id : 0);
LIST_FOREACH(ctm, &ch->ch_ctms, ctm_channel_link) {
ct = ctm->ctm_tag;
@ -1256,7 +1258,7 @@ htsp_async_send(htsmsg_t *m)
* global_lock is held
*/
void
htsp_event_update(channel_t *ch, event_t *e)
htsp_channgel_update_current(channel_t *ch)
{
htsmsg_t *m;
time_t now;
@ -1266,10 +1268,10 @@ htsp_event_update(channel_t *ch, event_t *e)
htsmsg_add_str(m, "method", "channelUpdate");
htsmsg_add_u32(m, "channelId", ch->ch_id);
if(e == NULL)
e = epg_event_find_by_time(ch, now);
htsmsg_add_u32(m, "eventId", e ? e->e_id : 0);
htsmsg_add_u32(m, "eventId",
ch->ch_epg_current ? ch->ch_epg_current->e_id : 0);
htsmsg_add_u32(m, "nextEventId",
ch->ch_epg_next ? ch->ch_epg_next->e_id : 0);
htsp_async_send(m);
}

View file

@ -24,7 +24,7 @@
void htsp_init(void);
void htsp_event_update(channel_t *ch, event_t *e);
void htsp_channgel_update_current(channel_t *ch);
void htsp_channel_add(channel_t *ch);
void htsp_channel_update(channel_t *ch);

View file

@ -357,6 +357,8 @@ main(int argc, char **argv)
pthread_mutex_lock(&global_lock);
time(&dispatch_clock);
trap_init(argv[0]);
/**