branches/gmyth-0.1b/src/gmyth_tvchain.c
branchtrunk
changeset 366 8aa32fa19a8f
parent 365 28c358053693
child 367 839c0fc4f285
     1.1 --- a/branches/gmyth-0.1b/src/gmyth_tvchain.c	Wed Feb 14 23:06:17 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,390 +0,0 @@
     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 -#ifdef HAVE_CONFIG_H
    1.33 -#include "config.h"
    1.34 -#endif
    1.35 -
    1.36 -#include "gmyth_tvchain.h"
    1.37 -
    1.38 -#include <glib.h>
    1.39 -#include <time.h>
    1.40 -#include <stdio.h>
    1.41 -#include <stdlib.h>
    1.42 -#include <assert.h>
    1.43 -
    1.44 -#include "gmyth_epg.h"
    1.45 -#include "gmyth_util.h"
    1.46 -#include "gmyth_query.h"
    1.47 -#include "gmyth_scheduler.h"
    1.48 -#include "gmyth_debug.h"
    1.49 -
    1.50 -static void gmyth_tvchain_class_init          (GMythTVChainClass *klass);
    1.51 -static void gmyth_tvchain_init                (GMythTVChain *object);
    1.52 -
    1.53 -static void gmyth_tvchain_dispose  (GObject *object);
    1.54 -static void gmyth_tvchain_finalize (GObject *object);
    1.55 -
    1.56 -G_DEFINE_TYPE(GMythTVChain, gmyth_tvchain, G_TYPE_OBJECT)
    1.57 -
    1.58 -static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.59 -
    1.60 -static void
    1.61 -gmyth_tvchain_class_init (GMythTVChainClass *klass)
    1.62 -{
    1.63 -	GObjectClass *gobject_class;
    1.64 -
    1.65 -	gobject_class = (GObjectClass *) klass;
    1.66 -
    1.67 -	gobject_class->dispose  = gmyth_tvchain_dispose;
    1.68 -	gobject_class->finalize = gmyth_tvchain_finalize;	
    1.69 -}
    1.70 -
    1.71 -static void
    1.72 -gmyth_tvchain_init (GMythTVChain *tvchain)
    1.73 -{
    1.74 -	tvchain->tvchain_id = NULL;
    1.75 -
    1.76 -	tvchain->cur_chanid = g_string_new ("");
    1.77 -	tvchain->cur_startts = NULL;
    1.78 -}
    1.79 -
    1.80 -static void
    1.81 -gmyth_tvchain_dispose  (GObject *object)
    1.82 -{
    1.83 -    GMythTVChain *tvchain = GMYTH_TVCHAIN(object);
    1.84 -
    1.85 -    if ( tvchain->tvchain_id != NULL ) {
    1.86 -	g_string_free( tvchain->tvchain_id, TRUE );
    1.87 -	tvchain->tvchain_id = NULL;
    1.88 -    }
    1.89 -
    1.90 -    if ( tvchain->tvchain_list != NULL ) {
    1.91 -	g_list_free( tvchain->tvchain_list );
    1.92 -	tvchain->tvchain_list = NULL;
    1.93 -    }
    1.94 -
    1.95 -    if ( tvchain->cur_chanid != NULL ) {
    1.96 -	g_string_free( tvchain->cur_chanid, TRUE );
    1.97 -	tvchain->cur_chanid = NULL;
    1.98 -    }
    1.99 -
   1.100 -    if ( tvchain->backend_info) {
   1.101 -        g_object_unref (tvchain->backend_info);
   1.102 -        tvchain->backend_info = NULL;
   1.103 -    }
   1.104 -
   1.105 -
   1.106 -    G_OBJECT_CLASS (gmyth_tvchain_parent_class)->dispose (object);
   1.107 -}
   1.108 -
   1.109 -static void
   1.110 -gmyth_tvchain_finalize (GObject *object)
   1.111 -{
   1.112 -    g_signal_handlers_destroy (object);
   1.113 -
   1.114 -    G_OBJECT_CLASS (gmyth_tvchain_parent_class)->finalize (object);
   1.115 -}
   1.116 -
   1.117 -/** Initializes the tvchain and generates the tvchain id.
   1.118 - * 
   1.119 - * @param tvchain The GMythTVChain instance.
   1.120 - * @param hostname The local hostname used to generate the tvchain id.
   1.121 - */
   1.122 -gboolean
   1.123 -gmyth_tvchain_initialize (GMythTVChain *tvchain, GMythBackendInfo *backend_info)
   1.124 -{
   1.125 -    const char *hostname;
   1.126 -
   1.127 -    assert (tvchain);
   1.128 -    g_return_val_if_fail (backend_info != NULL, FALSE);
   1.129 -
   1.130 -    g_object_ref (backend_info);
   1.131 -    tvchain->backend_info = backend_info;
   1.132 -
   1.133 -    hostname = gmyth_backend_info_get_hostname (backend_info);
   1.134 -    
   1.135 -    if (tvchain->tvchain_id == NULL) {
   1.136 -	    gchar *isodate = NULL;
   1.137 -    	GTimeVal *cur_time = g_new0( GTimeVal, 1 );
   1.138 -    	//struct tm* gmyth_util_time_val_to_date ( const GTimeVal* time )
   1.139 -
   1.140 -	    g_get_current_time(cur_time);
   1.141 -    	isodate = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%dT%H:%M:%S", cur_time );
   1.142 -
   1.143 -	    tvchain->tvchain_id = g_string_sized_new (7 + strlen (hostname) + strlen(isodate));
   1.144 -    	g_string_printf(tvchain->tvchain_id, 
   1.145 -	    	"live-%s-%s", hostname, isodate);
   1.146 -
   1.147 -    	gmyth_debug ("[%s] tv_chain_id: %s", __FUNCTION__, tvchain->tvchain_id->str);
   1.148 -
   1.149 -			if (isodate)
   1.150 -	    	g_free(isodate);
   1.151 -	    
   1.152 -	    if ( cur_time )
   1.153 -	    	g_free( cur_time );
   1.154 -    } else {
   1.155 -    	g_warning ("[%s] TVchain already initialized", __FUNCTION__);
   1.156 -    }
   1.157 -
   1.158 -    return TRUE;
   1.159 -}
   1.160 -
   1.161 -/** Gets the tvchain id.
   1.162 - * 
   1.163 - * @param tvchain The GMythTVChain instance.
   1.164 - * @return The tvchain id.
   1.165 - */
   1.166 -GString*
   1.167 -gmyth_tvchain_get_id (GMythTVChain *tvchain)
   1.168 -{
   1.169 -	g_return_val_if_fail( tvchain != NULL && tvchain->tvchain_id != NULL, NULL );
   1.170 -
   1.171 -	return g_string_new (tvchain->tvchain_id->str);
   1.172 -}
   1.173 -
   1.174 -/** Reloads all tvchain entries in the database.
   1.175 - * 
   1.176 - * @param tvchain The GMythTVChain instance.
   1.177 - * @return  TRUE if success, or FALSE if error.
   1.178 - */
   1.179 -gboolean
   1.180 -gmyth_tvchain_reload_all (GMythTVChain *tvchain)
   1.181 -{
   1.182 -	MYSQL_ROW msql_row;
   1.183 -	MYSQL_RES *msql_res = NULL;
   1.184 -	GMythQuery *gmyth_query = NULL;
   1.185 -	gboolean ret = TRUE;
   1.186 -	GString *stmt_str = NULL;
   1.187 -
   1.188 -	g_static_mutex_lock( &mutex );
   1.189 -	
   1.190 -	/* gets the initial size of the TVChain entries list */
   1.191 -	guint prev_size = g_list_length (tvchain->tvchain_list);
   1.192 -
   1.193 -	gmyth_debug ("[%s] chainid: %s", __FUNCTION__, tvchain->tvchain_id->str);
   1.194 -
   1.195 -	if ( tvchain != NULL && tvchain->tvchain_list != NULL ) {
   1.196 -		g_list_free (tvchain->tvchain_list);
   1.197 -		tvchain->tvchain_list = NULL;
   1.198 -	}
   1.199 -
   1.200 -	// TODO: Reuse gmyth_query already connected from context
   1.201 -	gmyth_query = gmyth_query_new ();
   1.202 -	if (!gmyth_query_connect (gmyth_query, tvchain->backend_info)) {
   1.203 -		g_warning ("[%s] Could not connect to db", __FUNCTION__);
   1.204 -		g_static_mutex_unlock( &mutex );
   1.205 -		ret = FALSE;
   1.206 -		goto done;
   1.207 -	}
   1.208 -
   1.209 -	stmt_str = g_string_new ("");
   1.210 -	g_string_printf (stmt_str, 
   1.211 -			"SELECT chanid, starttime, endtime, discontinuity, "
   1.212 -			"chainpos, hostprefix, cardtype, channame, input "
   1.213 -			"FROM tvchain "
   1.214 -			"WHERE chainid = \"%s\" ORDER BY chainpos;",
   1.215 -			tvchain->tvchain_id->str);
   1.216 -
   1.217 -	msql_res = gmyth_query_process_statement(gmyth_query, stmt_str->str);
   1.218 -	if (msql_res != NULL) {
   1.219 -
   1.220 -		while ((msql_row = mysql_fetch_row (msql_res)) != NULL) {
   1.221 -			struct LiveTVChainEntry *entry = g_new0 (struct LiveTVChainEntry, 1);
   1.222 -			entry->chanid = g_string_new (msql_row[0]);
   1.223 -			entry->starttime = gmyth_util_string_to_time_val ((const gchar*) msql_row[1]);
   1.224 -			entry->endtime = gmyth_util_string_to_time_val ((const gchar*) msql_row[2]);
   1.225 -			entry->discontinuity = g_ascii_strtoull (msql_row[3], NULL, 10 ) != 0;
   1.226 -			entry->hostprefix = g_string_new (msql_row[5]);
   1.227 -			entry->cardtype = g_string_new (msql_row[6]);
   1.228 -			entry->channum = g_string_new (msql_row[7]);
   1.229 -			entry->inputname = g_string_new (msql_row[8]);
   1.230 -
   1.231 -			//m_maxpos = query.value(4).toInt() + 1;
   1.232 -			g_print( "[%s] Reading TV chain entry (channel %s): [%s, %s, %s]\n", __FUNCTION__, entry->channum->str, entry->chanid->str, 
   1.233 -					(gchar*)msql_row[1], (gchar*)msql_row[2] );
   1.234 -			
   1.235 -			/* add this to get the actual start timestamp of the last recording */
   1.236 -			if ( tvchain->cur_startts < entry->starttime )
   1.237 -				tvchain->cur_startts = entry->starttime;
   1.238 -
   1.239 -			tvchain->tvchain_list = g_list_append (tvchain->tvchain_list, entry);			
   1.240 -		}
   1.241 -	} else {
   1.242 -		g_warning ("gmyth_tvchain_reload_all query error!\n");
   1.243 -		g_static_mutex_unlock( &mutex );
   1.244 -
   1.245 -		ret = FALSE;
   1.246 -		goto done;
   1.247 -	}
   1.248 -
   1.249 -	g_static_mutex_unlock( &mutex );
   1.250 -	
   1.251 -	tvchain->cur_pos = gmyth_tvchain_program_is_at (tvchain, tvchain->cur_chanid, tvchain->cur_startts);
   1.252 -	g_print( "[%s] TVChain current position = %d.\n", __FUNCTION__, tvchain->cur_pos );
   1.253 -
   1.254 -	if (tvchain->cur_pos < 0)
   1.255 -		tvchain->cur_pos = 0;
   1.256 -
   1.257 -	//    if (m_switchid >= 0)
   1.258 -	//        m_switchid = ProgramIsAt(m_switchentry.chanid,m_switchentry.starttime);
   1.259 -
   1.260 -	if (prev_size != g_list_length (tvchain->tvchain_list)) {
   1.261 -		gmyth_debug ("[%s] Added new recording", __FUNCTION__);
   1.262 -	}	
   1.263 -
   1.264 -done:
   1.265 -	if ( stmt_str != NULL )
   1.266 -		g_string_free (stmt_str, TRUE);
   1.267 -
   1.268 -	if ( msql_res != NULL )
   1.269 -		mysql_free_result (msql_res);
   1.270 -
   1.271 -	if ( gmyth_query != NULL )
   1.272 -		g_object_unref (gmyth_query);
   1.273 -
   1.274 -	return ret;
   1.275 -}
   1.276 -
   1.277 -/** Returns the internal index for the TV chain related to the given
   1.278 - * channel and start time.
   1.279 - * 
   1.280 - * @param tvchain The GMythTVChain instance.
   1.281 - * @param chanid The channel id.
   1.282 - * @param startts The program start time.
   1.283 - */
   1.284 -gint
   1.285 -gmyth_tvchain_program_is_at (GMythTVChain *tvchain, GString *chanid, GTimeVal* startts)
   1.286 -{
   1.287 -	gint count = 0;
   1.288 -	struct LiveTVChainEntry *entry;
   1.289 -	GList *tmp_list = tvchain->tvchain_list;
   1.290 -	guint list_size = g_list_length (tvchain->tvchain_list);
   1.291 -
   1.292 -	g_static_mutex_lock( &mutex );
   1.293 -	
   1.294 -	for (; tmp_list && ( count < list_size ); tmp_list = tvchain->tvchain_list->next, count++)
   1.295 -	{
   1.296 -		entry = (struct LiveTVChainEntry*) tmp_list->data;
   1.297 -		if ( !g_strncasecmp (entry->chanid->str, chanid->str, chanid->len)
   1.298 -				&& entry->starttime == startts )
   1.299 -		{
   1.300 -			g_static_mutex_unlock( &mutex );
   1.301 -			return count;
   1.302 -		}
   1.303 -	}
   1.304 -	g_static_mutex_unlock( &mutex );
   1.305 -
   1.306 -	return -1;	
   1.307 -}
   1.308 -
   1.309 -/** Get the program info associated to the tvchain.
   1.310 - * 
   1.311 - * @param tvchain The GMythTVChain instance.
   1.312 - * @param index The tvchain index.
   1.313 - * @return The program info structure.
   1.314 - */
   1.315 -GMythProgramInfo*
   1.316 -gmyth_tvchain_get_program_at (GMythTVChain *tvchain, gint index)
   1.317 -{
   1.318 -	struct LiveTVChainEntry *entry;
   1.319 -
   1.320 -	entry = gmyth_tvchain_get_entry_at (tvchain, index);
   1.321 -
   1.322 -	if (entry)
   1.323 -		return gmyth_tvchain_entry_to_program (tvchain, entry);	
   1.324 -
   1.325 -	return NULL;
   1.326 -}
   1.327 -
   1.328 -/** Gets a LiveTVChainEntry associated to the tvchain by its index.
   1.329 - * 
   1.330 - * @param tvchain The GMythTVChain instance.
   1.331 - * @param index The tvchain entry index
   1.332 - * @return The LiveTVchainEntry structure.
   1.333 - */
   1.334 -struct LiveTVChainEntry*
   1.335 -gmyth_tvchain_get_entry_at (GMythTVChain *tvchain, gint index)
   1.336 -{
   1.337 -	struct LiveTVChainEntry* chain_entry = NULL;
   1.338 -
   1.339 -	g_return_val_if_fail( tvchain != NULL && tvchain->tvchain_list != NULL, NULL );
   1.340 -	
   1.341 -	g_static_mutex_lock( &mutex );
   1.342 -	
   1.343 -	gint size = g_list_length (tvchain->tvchain_list);
   1.344 -	gint new_index = (index < 0 || index >= size) ? size - 1 : index;
   1.345 -
   1.346 -	if (new_index >= 0) 
   1.347 -		chain_entry = (struct LiveTVChainEntry*) g_list_nth_data (tvchain->tvchain_list, new_index);
   1.348 -
   1.349 -	g_static_mutex_unlock( &mutex );
   1.350 -	
   1.351 -	if ( chain_entry != NULL ) {
   1.352 -		gmyth_debug ("[%s] Got TV Chain entry at %d.\n", __FUNCTION__, new_index );
   1.353 -
   1.354 -	} else {
   1.355 -		g_warning ("[%s] failed to get entry at index %d", __FUNCTION__, index);
   1.356 -	}
   1.357 -
   1.358 -	return chain_entry;
   1.359 -}
   1.360 -
   1.361 -/** Gets the program info from backend database associated to the tv chain entry.
   1.362 - * 
   1.363 - * @param tvchain The GMythTVChain instance.
   1.364 - * @param entry the LiveTVChainEntry to be converted.
   1.365 - * @return The progrma info.
   1.366 - */
   1.367 -GMythProgramInfo*
   1.368 -gmyth_tvchain_entry_to_program (GMythTVChain *tvchain, struct LiveTVChainEntry *entry)
   1.369 -{
   1.370 -	GMythProgramInfo *proginfo = NULL;
   1.371 -
   1.372 -	g_return_val_if_fail( tvchain != NULL, NULL );
   1.373 -
   1.374 -	if ( !entry || !tvchain ) {
   1.375 -		g_warning ("gmyth_tvchain_entry_to_program() received NULL argument");
   1.376 -		return NULL;
   1.377 -	}
   1.378 -
   1.379 -	GMythScheduler *scheduler = gmyth_scheduler_new ();
   1.380 -
   1.381 -	gmyth_scheduler_connect( scheduler, tvchain->backend_info );
   1.382 -	proginfo = gmyth_scheduler_get_recorded (scheduler, 
   1.383 -			entry->chanid, entry->starttime);
   1.384 -	gmyth_scheduler_disconnect( scheduler );
   1.385 -
   1.386 -	if (proginfo) {
   1.387 -		proginfo->pathname = g_string_prepend (proginfo->pathname, entry->hostprefix->str);
   1.388 -	} else {
   1.389 -		g_warning ("tvchain_entry_to_program( chan id = %s, starttime = %ld) failed!", entry->chanid->str, entry->starttime->tv_sec);
   1.390 -	}
   1.391 -
   1.392 -	return proginfo;
   1.393 -}