gmyth/src/gmyth_tvchain.c
branchtrunk
changeset 18 79f6da40f6e9
parent 11 79e798d40577
child 54 51fd70b817a8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth/src/gmyth_tvchain.c	Thu Sep 28 15:41:06 2006 +0100
     1.3 @@ -0,0 +1,355 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_tvchain.c
     1.8 + * 
     1.9 + * @brief <p> This component contains functions for creating and accessing
    1.10 + * the tvchain functions for live tv playback.
    1.11 + * 
    1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.13 + * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
    1.14 + *
    1.15 + *//*
    1.16 + * 
    1.17 + * This program is free software; you can redistribute it and/or modify
    1.18 + * it under the terms of the GNU Lesser General Public License as published by
    1.19 + * the Free Software Foundation; either version 2 of the License, or
    1.20 + * (at your option) any later version.
    1.21 + *
    1.22 + * This program is distributed in the hope that it will be useful,
    1.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.25 + * GNU General Public License for more details.
    1.26 + *
    1.27 + * You should have received a copy of the GNU Lesser General Public License
    1.28 + * along with this program; if not, write to the Free Software
    1.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.30 + */
    1.31 +
    1.32 +#include <glib.h>
    1.33 +#include <time.h>
    1.34 +#include <stdio.h>
    1.35 +#include <stdlib.h>
    1.36 +
    1.37 +#include "gmyth_epg.h"
    1.38 +#include "gmyth_tvchain.h"
    1.39 +#include "gmyth_util.h"
    1.40 +#include "gmyth_query.h"
    1.41 +#include "gmyth_scheduler.h"
    1.42 +
    1.43 +static void gmyth_tvchain_class_init          (GMythTVChainClass *klass);
    1.44 +static void gmyth_tvchain_init                (GMythTVChain *object);
    1.45 +
    1.46 +static void gmyth_tvchain_dispose  (GObject *object);
    1.47 +static void gmyth_tvchain_finalize (GObject *object);
    1.48 +
    1.49 +G_DEFINE_TYPE(GMythTVChain, gmyth_tvchain, G_TYPE_OBJECT)
    1.50 +
    1.51 +static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.52 +
    1.53 +static void
    1.54 +gmyth_tvchain_class_init (GMythTVChainClass *klass)
    1.55 +{
    1.56 +	GObjectClass *gobject_class;
    1.57 +
    1.58 +	gobject_class = (GObjectClass *) klass;
    1.59 +
    1.60 +	gobject_class->dispose  = gmyth_tvchain_dispose;
    1.61 +	gobject_class->finalize = gmyth_tvchain_finalize;	
    1.62 +}
    1.63 +
    1.64 +static void
    1.65 +gmyth_tvchain_init (GMythTVChain *tvchain)
    1.66 +{
    1.67 +	tvchain->tvchain_id = NULL;
    1.68 +
    1.69 +	tvchain->cur_chanid = g_string_new ("");
    1.70 +	tvchain->cur_startts = 0;
    1.71 +}
    1.72 +
    1.73 +static void
    1.74 +gmyth_tvchain_dispose  (GObject *object)
    1.75 +{
    1.76 +	GMythTVChain *tvchain = GMYTH_TVCHAIN(object);
    1.77 +
    1.78 +	if ( tvchain->tvchain_id != NULL ) {
    1.79 +		g_string_free( tvchain->tvchain_id, TRUE );
    1.80 +		tvchain->tvchain_id = NULL;
    1.81 +	}
    1.82 +
    1.83 +	if ( tvchain->tvchain_list != NULL ) {
    1.84 +		g_list_free( tvchain->tvchain_list );
    1.85 +		tvchain->tvchain_list = NULL;
    1.86 +	}
    1.87 +
    1.88 +	if ( tvchain->cur_chanid != NULL ) {
    1.89 +		g_string_free( tvchain->cur_chanid, TRUE );
    1.90 +		tvchain->cur_chanid = NULL;
    1.91 +	}
    1.92 +
    1.93 +	G_OBJECT_CLASS (gmyth_tvchain_parent_class)->dispose (object);
    1.94 +}
    1.95 +
    1.96 +static void
    1.97 +gmyth_tvchain_finalize (GObject *object)
    1.98 +{
    1.99 +	g_signal_handlers_destroy (object);
   1.100 +
   1.101 +	G_OBJECT_CLASS (gmyth_tvchain_parent_class)->finalize (object);
   1.102 +}
   1.103 +
   1.104 +/** Initializes the tvchain and generates the tvchain id.
   1.105 + * 
   1.106 + * @param tvchain The GMythTVChain instance.
   1.107 + * @param hostname The local hostname used to generate the tvchain id.
   1.108 + */
   1.109 +void
   1.110 +gmyth_tvchain_initialize (GMythTVChain *tvchain, GString *hostname)
   1.111 +{
   1.112 +	if (tvchain->tvchain_id == NULL) {
   1.113 +		GString *isodate;
   1.114 +		time_t cur_time;
   1.115 +
   1.116 +		time(&cur_time);
   1.117 +		isodate = gmyth_util_time_to_isoformat (cur_time);
   1.118 +
   1.119 +		tvchain->tvchain_id = g_string_sized_new(7 + hostname->len + isodate->len);
   1.120 +		g_string_printf(tvchain->tvchain_id, 
   1.121 +				"live-%s-%s", hostname->str, isodate->str);
   1.122 +
   1.123 +		g_print("tv_chain_id: %s\n", tvchain->tvchain_id->str);
   1.124 +
   1.125 +		g_string_free(isodate, TRUE);
   1.126 +
   1.127 +	} else {
   1.128 +		g_warning ("[%s] TVchain already initialized", __FUNCTION__);
   1.129 +	}
   1.130 +}
   1.131 +
   1.132 +/** Gets the tvchain id.
   1.133 + * 
   1.134 + * @param tvchain The GMythTVChain instance.
   1.135 + * @return The tvchain id.
   1.136 + */
   1.137 +GString*
   1.138 +gmyth_tvchain_get_id (GMythTVChain *tvchain)
   1.139 +{
   1.140 +	g_return_val_if_fail( tvchain != NULL && tvchain->tvchain_id != NULL, NULL );
   1.141 +
   1.142 +	return g_string_new (tvchain->tvchain_id->str);
   1.143 +}
   1.144 +
   1.145 +/** Reloads all tvchain entries in the database.
   1.146 + * 
   1.147 + * @param tvchain The GMythTVChain instance.
   1.148 + * @return  TRUE if success, or FALSE if error.
   1.149 + */
   1.150 +gboolean
   1.151 +gmyth_tvchain_reload_all (GMythTVChain *tvchain)
   1.152 +{
   1.153 +	MYSQL_ROW msql_row;
   1.154 +	MYSQL_RES *msql_res;
   1.155 +	GMythQuery *gmyth_query;
   1.156 +	gboolean ret = TRUE;
   1.157 +
   1.158 +	GString *stmt_str;
   1.159 +
   1.160 +	g_static_mutex_lock( &mutex );
   1.161 +
   1.162 +	guint prev_size = g_list_length (tvchain->tvchain_list);
   1.163 +
   1.164 +	g_debug ("[%s] chainid: %s", __FUNCTION__, tvchain->tvchain_id->str);
   1.165 +
   1.166 +	if ( tvchain != NULL && tvchain->tvchain_list != NULL ) {
   1.167 +		g_list_free (tvchain->tvchain_list);
   1.168 +		tvchain->tvchain_list = NULL;
   1.169 +	}
   1.170 +
   1.171 +	// TODO: Reuse gmyth_query already connected from context
   1.172 +	gmyth_query = gmyth_query_new ();
   1.173 +	if (!gmyth_query_connect (gmyth_query)) {
   1.174 +		g_warning ("[%s] Could not connect to db", __FUNCTION__);
   1.175 +		g_static_mutex_unlock( &mutex );
   1.176 +
   1.177 +		ret = FALSE;
   1.178 +
   1.179 +		goto done;
   1.180 +	}
   1.181 +
   1.182 +	stmt_str = g_string_new ("");
   1.183 +	g_string_printf (stmt_str, 
   1.184 +			"SELECT chanid, starttime, endtime, discontinuity, "
   1.185 +			"chainpos, hostprefix, cardtype, channame, input "
   1.186 +			"FROM tvchain "
   1.187 +			"WHERE chainid = \"%s\" ORDER BY chainpos;",
   1.188 +			tvchain->tvchain_id->str);
   1.189 +
   1.190 +	msql_res = gmyth_query_process_statement(gmyth_query, stmt_str->str);
   1.191 +	if (msql_res != NULL) {
   1.192 +
   1.193 +		while ((msql_row = mysql_fetch_row (msql_res)) != NULL) {
   1.194 +			struct LiveTVChainEntry *entry = g_new0 (struct LiveTVChainEntry, 1);
   1.195 +			entry->chanid = g_string_new (msql_row[0]);
   1.196 +			entry->starttime = gmyth_util_string_to_time (g_string_new ((gchar*)msql_row[1]));
   1.197 +			entry->endtime = gmyth_util_string_to_time (g_string_new (msql_row[2]));
   1.198 +			entry->discontinuity = atoi (msql_row[3]) != 0;
   1.199 +			entry->hostprefix = g_string_new (msql_row[5]);
   1.200 +			entry->cardtype = g_string_new (msql_row[6]);
   1.201 +			entry->channum = g_string_new (msql_row[7]);
   1.202 +			entry->inputname = g_string_new (msql_row[8]);
   1.203 +
   1.204 +			//m_maxpos = query.value(4).toInt() + 1;
   1.205 +
   1.206 +			tvchain->tvchain_list = g_list_append (tvchain->tvchain_list, entry);			
   1.207 +		}
   1.208 +	} else {
   1.209 +		g_warning ("gmyth_tvchain_reload_all query error!\n");
   1.210 +		g_static_mutex_unlock( &mutex );
   1.211 +
   1.212 +		ret = FALSE;
   1.213 +		goto done;
   1.214 +	}
   1.215 +
   1.216 +	g_static_mutex_unlock( &mutex );
   1.217 +	
   1.218 +	tvchain->cur_pos = gmyth_tvchain_program_is_at (tvchain, tvchain->cur_chanid, tvchain->cur_startts);
   1.219 +
   1.220 +	if (tvchain->cur_pos < 0)
   1.221 +		tvchain->cur_pos = 0;
   1.222 +
   1.223 +	//    if (m_switchid >= 0)
   1.224 +	//        m_switchid = ProgramIsAt(m_switchentry.chanid,m_switchentry.starttime);
   1.225 +
   1.226 +	if (prev_size != g_list_length (tvchain->tvchain_list)) {
   1.227 +		g_debug ("[%s] Added new recording", __FUNCTION__);
   1.228 +	}	
   1.229 +
   1.230 +done:
   1.231 +	if ( stmt_str != NULL )
   1.232 +		g_string_free (stmt_str, TRUE);
   1.233 +
   1.234 +	if ( msql_res != NULL )
   1.235 +		mysql_free_result (msql_res);
   1.236 +
   1.237 +	if ( gmyth_query != NULL )
   1.238 +		g_object_unref (gmyth_query);
   1.239 +
   1.240 +	return ret;
   1.241 +}
   1.242 +
   1.243 +/** Returns the internal index for the TV chain related to the given
   1.244 + * channel and start time.
   1.245 + * 
   1.246 + * @param tvchain The GMythTVChain instance.
   1.247 + * @param chanid The channel id.
   1.248 + * @param startts The program start time.
   1.249 + */
   1.250 +int
   1.251 +gmyth_tvchain_program_is_at (GMythTVChain *tvchain, GString *chanid, time_t startts)
   1.252 +{
   1.253 +	int count = 0;
   1.254 +	struct LiveTVChainEntry *entry;
   1.255 +	GList *tmp_list = tvchain->tvchain_list;
   1.256 +
   1.257 +	g_static_mutex_lock( &mutex );
   1.258 +	
   1.259 +	for (; tmp_list; tmp_list = tvchain->tvchain_list->next, ++count)
   1.260 +	{
   1.261 +		entry = (struct LiveTVChainEntry*) tmp_list->data;
   1.262 +		if (!g_strncasecmp (entry->chanid->str, chanid->str, chanid->len)
   1.263 +				&& entry->starttime == startts)
   1.264 +		{
   1.265 +			g_static_mutex_unlock( &mutex );
   1.266 +			return count;
   1.267 +		}
   1.268 +	}
   1.269 +	g_static_mutex_unlock( &mutex );
   1.270 +
   1.271 +	return -1;	
   1.272 +}
   1.273 +
   1.274 +/** Get the program info associated to the tvchain.
   1.275 + * 
   1.276 + * @param tvchain The GMythTVChain instance.
   1.277 + * @param index The tvchain index.
   1.278 + * @return The program info structure.
   1.279 + */
   1.280 +GMythProgramInfo*
   1.281 +gmyth_tvchain_get_program_at (GMythTVChain *tvchain, int index)
   1.282 +{
   1.283 +	struct LiveTVChainEntry *entry;
   1.284 +
   1.285 +	entry = gmyth_tvchain_get_entry_at (tvchain, index);
   1.286 +
   1.287 +	if (entry)
   1.288 +		return gmyth_tvchain_entry_to_program (tvchain, entry);	
   1.289 +
   1.290 +	return NULL;
   1.291 +}
   1.292 +
   1.293 +/** Gets a LiveTVChainEntry associated to the tvchain by its index.
   1.294 + * 
   1.295 + * @param tvchain The GMythTVChain instance.
   1.296 + * @param index The tvchain entry index
   1.297 + * @return The LiveTVchainEntry structure.
   1.298 + */
   1.299 +struct LiveTVChainEntry*
   1.300 +gmyth_tvchain_get_entry_at (GMythTVChain *tvchain, int index)
   1.301 +{
   1.302 +	struct LiveTVChainEntry* chain_entry = NULL;
   1.303 +
   1.304 +	g_return_val_if_fail( tvchain != NULL && tvchain->tvchain_list != NULL, NULL );
   1.305 +	
   1.306 +	g_static_mutex_lock( &mutex );
   1.307 +	
   1.308 +	int size = g_list_length (tvchain->tvchain_list);
   1.309 +	int new_index = (index < 0 || index >= size) ? size - 1 : index;
   1.310 +
   1.311 +	if (new_index >= 0) 
   1.312 +		chain_entry = (struct LiveTVChainEntry*) g_list_nth_data (tvchain->tvchain_list, new_index);
   1.313 +
   1.314 +	g_static_mutex_unlock( &mutex );
   1.315 +	
   1.316 +	if ( chain_entry != NULL ) {
   1.317 +		g_debug ("[%s] Got TV Chain entry at %d.\n", __FUNCTION__, new_index );
   1.318 +
   1.319 +	} else {
   1.320 +		g_warning ("[%s] failed to get entry at index %d", __FUNCTION__, index);
   1.321 +	}
   1.322 +
   1.323 +	return chain_entry;
   1.324 +}
   1.325 +
   1.326 +/** Gets the program info from backend database associated to the tv chain entry.
   1.327 + * 
   1.328 + * @param tvchain The GMythTVChain instance.
   1.329 + * @param entry the LiveTVChainEntry to be converted.
   1.330 + * @return The progrma info.
   1.331 + */
   1.332 +GMythProgramInfo*
   1.333 +gmyth_tvchain_entry_to_program (GMythTVChain *tvchain, struct LiveTVChainEntry *entry)
   1.334 +{
   1.335 +	GMythProgramInfo *proginfo = NULL;
   1.336 +
   1.337 +	g_return_val_if_fail( tvchain != NULL, NULL );
   1.338 +
   1.339 +	if (!entry || !tvchain) {
   1.340 +		g_warning ("gmyth_tvchain_entry_to_program() received NULL argument");
   1.341 +		return NULL;
   1.342 +	}
   1.343 +
   1.344 +	GMythScheduler *scheduler = gmyth_scheduler_new ();
   1.345 +
   1.346 +	gmyth_scheduler_connect( scheduler );
   1.347 +	proginfo = gmyth_scheduler_get_recorded (scheduler, 
   1.348 +			entry->chanid, entry->starttime);
   1.349 +	gmyth_scheduler_disconnect( scheduler );
   1.350 +
   1.351 +	if (proginfo) {
   1.352 +		proginfo->pathname = g_string_prepend (proginfo->pathname, entry->hostprefix->str);
   1.353 +	} else {
   1.354 +		g_warning ("tvchain_entry_to_program(%s, %ld) failed!", entry->chanid->str, entry->starttime);
   1.355 +	}
   1.356 +
   1.357 +	return proginfo;
   1.358 +}