From 08c24d441c8d768bab652a66e9d63f1f7837b448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Mon, 14 Jan 2008 19:07:46 +0000 Subject: [PATCH] Initial HTSP async support --- epg.c | 3 ++ htsp.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- htsp.h | 4 +++ rpc.c | 20 ------------- 4 files changed, 96 insertions(+), 21 deletions(-) diff --git a/epg.c b/epg.c index de2d5e12..d9956fc5 100644 --- a/epg.c +++ b/epg.c @@ -26,6 +26,7 @@ #include "epg.h" #include "dispatch.h" #include "htsclient.h" +#include "htsp.h" #define EPG_MAX_AGE 86400 @@ -378,6 +379,8 @@ epg_set_current_event(th_channel_t *ch, event_t *e) /* Notify clients that a new programme is on */ clients_send_ref(ch->ch_tag); + + htsp_async_channel_update(ch); } static void diff --git a/htsp.c b/htsp.c index d0464671..ab2b9c35 100644 --- a/htsp.c +++ b/htsp.c @@ -37,9 +37,12 @@ #include "htsp.h" #include "htsp_muxer.h" #include "tcp.h" +#include "epg.h" #include +static LIST_HEAD(, htsp) htsp_sessions; + /* * */ @@ -64,6 +67,79 @@ htsp_send_msg(htsp_t *htsp, htsmsg_t *m, int media) } +/** + * build a channel message + */ +static htsmsg_t * +htsp_build_channel_msg(th_channel_t *ch, const char *method) +{ + htsmsg_t *msg = htsmsg_create(); + event_t *e; + + htsmsg_add_str(msg, "method", method); + htsmsg_add_str(msg, "channelGroupName", ch->ch_group->tcg_name); + htsmsg_add_str(msg, "channelName", ch->ch_name); + htsmsg_add_u32(msg, "channelTag", ch->ch_tag); + if(ch->ch_icon != NULL) + htsmsg_add_str(msg, "channelIcon", refstr_get(ch->ch_icon)); + + if((e = epg_event_get_current(ch)) != NULL) + htsmsg_add_u32(msg, "currentEvent", e->e_tag); + + return msg; +} + + + + +/** + * channels_list + */ +static void +htsp_send_all_channels(htsp_t *htsp) +{ + htsmsg_t *msg; + th_channel_group_t *tcg; + th_channel_t *ch; + + TAILQ_FOREACH(tcg, &all_channel_groups, tcg_global_link) { + if(tcg->tcg_hidden) + continue; + + msg = htsmsg_create(); + htsmsg_add_str(msg, "method", "channelGroupAdd"); + htsmsg_add_str(msg, "channelGroupName", tcg->tcg_name); + htsp_send_msg(htsp, msg, 0); + + TAILQ_FOREACH(ch, &tcg->tcg_channels, ch_group_link) { + if(LIST_FIRST(&ch->ch_transports) == NULL) + continue; + + msg = htsp_build_channel_msg(ch, "channelAdd"); + htsp_send_msg(htsp, msg, 0); + } + } +} + +/** + * + */ +void +htsp_async_channel_update(th_channel_t *ch) +{ + htsp_t *htsp; + htsmsg_t *msg; + + LIST_FOREACH(htsp, &htsp_sessions, htsp_global_link) { + if(!htsp->htsp_rpc.rs_is_async) + continue; + + msg = htsp_build_channel_msg(ch, "channelUpdate"); + htsp_send_msg(htsp, msg, 0); + } +} + + /* * */ @@ -71,7 +147,7 @@ static void htsp_input(htsp_t *htsp, const void *buf, int len) { htsmsg_t *in, *out; - int i; + int i, was_async; const uint8_t *v = buf; printf("Got %d bytes\n", len); @@ -83,6 +159,9 @@ htsp_input(htsp_t *htsp, const void *buf, int len) printf("deserialize failed\n"); return; } + + was_async = htsp->htsp_rpc.rs_is_async; + printf("INPUT:\n"); htsmsg_print(in); @@ -94,6 +173,11 @@ htsp_input(htsp_t *htsp, const void *buf, int len) htsmsg_print(out); htsp_send_msg(htsp, out, 0); + + if(!was_async && htsp->htsp_rpc.rs_is_async) { + /* Session went into async state */ + htsp_send_all_channels(htsp); + } } @@ -158,6 +242,8 @@ htsp_data_input(htsp_t *htsp) static void htsp_disconnect(htsp_t *htsp) { + LIST_REMOVE(htsp, htsp_global_link); + free(htsp->htsp_buf); rpc_deinit(&htsp->htsp_rpc); } @@ -173,6 +259,8 @@ htsp_connect(htsp_t *htsp) htsp->htsp_bufsize = 1000; htsp->htsp_buf = malloc(htsp->htsp_bufsize); + + LIST_INSERT_HEAD(&htsp_sessions, htsp, htsp_global_link); } /* diff --git a/htsp.h b/htsp.h index e00a1480..78ed08dd 100644 --- a/htsp.h +++ b/htsp.h @@ -27,6 +27,8 @@ typedef struct htsp { tcp_session_t htsp_tcp_session; /* Must be first */ + LIST_ENTRY(htsp) htsp_global_link; + int htsp_bufsize; int htsp_bufptr; int htsp_msglen; @@ -42,4 +44,6 @@ void htsp_start(int port); int htsp_send_msg(htsp_t *htsp, htsmsg_t *m, int media); +void htsp_async_channel_update(th_channel_t *ch); + #endif /* HTSP_H_ */ diff --git a/rpc.c b/rpc.c index 539ee2db..bc07ee02 100644 --- a/rpc.c +++ b/rpc.c @@ -58,26 +58,6 @@ rpc_build_channel_msg(th_channel_t *ch) -#if 0 - -/** - * channels_list - */ -static void -htsp_send_all_channels(htsp_connection_t *hc) -{ - htsp_msg_t *msg; - th_channel_t *ch; - - TAILQ_FOREACH(ch, &channels, ch_global_link) { - msg = htsp_build_channel_msg(ch); - htsp_add_string(msg, "msgtype", "channelAdd"); - htsp_send_msg(hc, NULL, msg); - htsp_free_msg(msg); - } -} - -#endif