gmyth/src/gmyth_livetv.c
author rosfran
Thu Dec 21 21:48:28 2006 +0000 (2006-12-21)
branchtrunk
changeset 240 bb2752e9fe02
parent 237 678cf278c11a
child 266 1098f58ae8e1
permissions -rwxr-xr-x
[svn r241] Removed the need for the mysql_config utility.
rosfran@68
     1
/**
rosfran@68
     2
 * GMyth Library
rosfran@68
     3
 *
rosfran@68
     4
 * @file gmyth/gmyth_livetv.c
rosfran@68
     5
 * 
rosfran@68
     6
 * @brief <p> GMythLiveTV starts a remote TV session with the MythTV backend.
rosfran@68
     7
 *
rosfran@68
     8
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
rosfran@68
     9
 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
rosfran@68
    10
 *
rosfran@68
    11
 *//*
rosfran@68
    12
 * 
rosfran@68
    13
 * This program is free software; you can redistribute it and/or modify
rosfran@68
    14
 * it under the terms of the GNU Lesser General Public License as published by
rosfran@68
    15
 * the Free Software Foundation; either version 2 of the License, or
rosfran@68
    16
 * (at your option) any later version.
rosfran@68
    17
 *
rosfran@68
    18
 * This program is distributed in the hope that it will be useful,
rosfran@68
    19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rosfran@68
    20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rosfran@68
    21
 * GNU General Public License for more details.
rosfran@68
    22
 *
rosfran@68
    23
 * You should have received a copy of the GNU Lesser General Public License
rosfran@68
    24
 * along with this program; if not, write to the Free Software
rosfran@68
    25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rosfran@68
    26
 */
leo_sobral@213
    27
  
leo_sobral@213
    28
#ifdef HAVE_CONFIG_H
leo_sobral@213
    29
#include "config.h"
leo_sobral@213
    30
#endif
leo_sobral@213
    31
rosfran@240
    32
#include "gmyth_livetv.h" 
rosfran@42
    33
#include "gmyth_remote_util.h"
rosfran@42
    34
#include "gmyth_tvchain.h"
melunko@117
    35
#include "gmyth_socket.h"
renatofilho@131
    36
#include "gmyth_debug.h"
rosfran@41
    37
rosfran@42
    38
#include "gmyth_file_transfer.h"
rosfran@216
    39
#include "gmyth_monitor_handler.h"
rosfran@41
    40
rosfran@41
    41
static void gmyth_livetv_class_init          (GMythLiveTVClass *klass);
rosfran@41
    42
static void gmyth_livetv_init                (GMythLiveTV *object);
rosfran@41
    43
rosfran@41
    44
static void gmyth_livetv_dispose  (GObject *object);
rosfran@41
    45
static void gmyth_livetv_finalize (GObject *object);
rosfran@41
    46
rosfran@41
    47
static gint tvchain_curr_index = -1; 
rosfran@41
    48
melunko@107
    49
#define GMYTHTV_RECORDER_HEADER         "QUERY_RECORDER"
melunko@107
    50
melunko@107
    51
rosfran@41
    52
G_DEFINE_TYPE(GMythLiveTV, gmyth_livetv, G_TYPE_OBJECT)
rosfran@41
    53
rosfran@41
    54
static void
rosfran@41
    55
gmyth_livetv_class_init (GMythLiveTVClass *klass)
rosfran@41
    56
{
rosfran@41
    57
	GObjectClass *gobject_class;
rosfran@41
    58
rosfran@41
    59
	gobject_class = (GObjectClass *) klass;
rosfran@41
    60
rosfran@41
    61
	gobject_class->dispose  = gmyth_livetv_dispose;
rosfran@41
    62
	gobject_class->finalize = gmyth_livetv_finalize;	
rosfran@41
    63
}
rosfran@41
    64
rosfran@41
    65
static void
rosfran@41
    66
gmyth_livetv_init (GMythLiveTV *livetv)
rosfran@41
    67
{
rosfran@216
    68
	livetv->backend_info = NULL;
rosfran@41
    69
	livetv->local_hostname = NULL;
rosfran@216
    70
	livetv->file_transfer = NULL;
rosfran@216
    71
	livetv->setup_done = FALSE;
rosfran@41
    72
rosfran@65
    73
	livetv->recorder = NULL;
rosfran@41
    74
	livetv->tvchain = NULL;
rosfran@41
    75
	livetv->proginfo = NULL;
rosfran@41
    76
}
rosfran@41
    77
rosfran@41
    78
static void
rosfran@41
    79
gmyth_livetv_dispose  (GObject *object)
rosfran@41
    80
{
rosfran@41
    81
	G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
rosfran@41
    82
}
rosfran@41
    83
rosfran@41
    84
static void
rosfran@41
    85
gmyth_livetv_finalize (GObject *object)
rosfran@41
    86
{
rosfran@41
    87
	g_signal_handlers_destroy (object);
rosfran@41
    88
rosfran@41
    89
	GMythLiveTV *livetv = GMYTH_LIVETV (object);
rosfran@41
    90
renatofilho@131
    91
	gmyth_debug ("[%s] Finalizing livetv", __FUNCTION__);
rosfran@41
    92
rosfran@216
    93
	if ( livetv->monitor != NULL ) {
rosfran@216
    94
		g_object_unref (livetv->monitor);
rosfran@216
    95
		livetv->monitor = NULL;
rosfran@216
    96
	}
rosfran@216
    97
rosfran@65
    98
	if ( livetv->recorder != NULL ) {
rosfran@65
    99
		g_object_unref (livetv->recorder);
rosfran@65
   100
		livetv->recorder = NULL;
rosfran@41
   101
	}
rosfran@41
   102
rosfran@41
   103
	if ( livetv->tvchain != NULL ) {
rosfran@41
   104
		g_object_unref (livetv->tvchain);
rosfran@41
   105
		livetv->tvchain = NULL;
rosfran@41
   106
	}
rosfran@41
   107
rosfran@41
   108
	if ( livetv->proginfo != NULL ) {
rosfran@41
   109
		g_object_unref (livetv->proginfo);
rosfran@41
   110
		livetv->proginfo = NULL;
rosfran@41
   111
	}
rosfran@216
   112
	
rosfran@216
   113
	if ( livetv->file_transfer != NULL ) {
rosfran@216
   114
		g_object_unref (livetv->file_transfer);
rosfran@216
   115
		livetv->file_transfer = NULL;
rosfran@216
   116
	}
rosfran@216
   117
	
rosfran@216
   118
	if ( livetv->backend_info != NULL ) {
rosfran@216
   119
		g_object_unref (livetv->backend_info);
rosfran@216
   120
		livetv->backend_info = NULL;
rosfran@216
   121
	}
rosfran@41
   122
rosfran@41
   123
	G_OBJECT_CLASS ( gmyth_livetv_parent_class )->finalize ( object );
rosfran@41
   124
}
rosfran@41
   125
rosfran@41
   126
GMythLiveTV*
rosfran@41
   127
gmyth_livetv_new ()
rosfran@41
   128
{
rosfran@41
   129
	GMythLiveTV *livetv = GMYTH_LIVETV ( g_object_new( GMYTH_LIVETV_TYPE, NULL ) );
rosfran@41
   130
rosfran@41
   131
	return livetv;
rosfran@41
   132
}
rosfran@41
   133
rosfran@212
   134
static void
rosfran@212
   135
gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code, 
rosfran@232
   136
							gchar* message, gpointer user_data )
rosfran@212
   137
{
rosfran@233
   138
	GMythLiveTV *live_tv = GMYTH_LIVETV ( user_data );
rosfran@233
   139
	//g_object_ref( live_tv );
rosfran@216
   140
	
rosfran@232
   141
	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, live_tv param = %s, user_data = %s )\n", message, msg_code, live_tv != NULL ? "" : 
rosfran@220
   142
					"NULL", user_data != NULL ? "" : "NULL" );
rosfran@216
   143
	
rosfran@232
   144
	if ( NULL == live_tv )
rosfran@232
   145
	{
rosfran@232
   146
		gmyth_debug( "LiveTV_obj is equals to NULL!!!" );
rosfran@232
   147
		return;
rosfran@232
   148
	}		
rosfran@216
   149
	
rosfran@216
   150
	switch ( msg_code ) 
rosfran@216
   151
	{
rosfran@216
   152
		
rosfran@216
   153
		case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
rosfran@216
   154
		{
rosfran@234
   155
			gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
rosfran@234
   156
				"TV Chain ID is the same as the old one...\n", message );
rosfran@234
   157
			if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {				
rosfran@234
   158
				gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!", 
rosfran@234
   159
										(gmyth_tvchain_get_id( live_tv->tvchain ))->str );
rosfran@234
   160
				/* advertises the FileTransfer about the program info changed */
rosfran@234
   161
				if ( live_tv->file_transfer != NULL )
rosfran@234
   162
				{
rosfran@234
   163
					gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
rosfran@234
   164
					
rosfran@234
   165
					gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
rosfran@234
   166
		             msg_code, (gpointer)live_tv );
rosfran@234
   167
		             
rosfran@234
   168
		      //gmyth_livetv_monitor_handler_stop( live_tv );	      
rosfran@234
   169
				} else
rosfran@234
   170
					gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");				
rosfran@234
   171
			}
rosfran@216
   172
			break;
rosfran@216
   173
		}
rosfran@216
   174
		default:
rosfran@220
   175
			break;		
rosfran@220
   176
	} /* switch (Monitor Handler messages) */
rosfran@220
   177
	
rosfran@220
   178
}
rosfran@220
   179
rosfran@220
   180
gboolean
rosfran@220
   181
gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
rosfran@220
   182
{
rosfran@220
   183
	gboolean res = TRUE;
rosfran@220
   184
	
rosfran@220
   185
	if ( livetv->monitor != NULL )
rosfran@220
   186
	{
rosfran@220
   187
		g_object_unref( livetv->monitor );
rosfran@220
   188
		livetv->monitor	= NULL;
rosfran@216
   189
	}
rosfran@216
   190
	
rosfran@220
   191
  livetv->monitor = gmyth_monitor_handler_new ( );
rosfran@220
   192
  
rosfran@220
   193
  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
rosfran@220
   194
  				livetv->backend_info->port );
rosfran@220
   195
  
rosfran@220
   196
  if ( res == TRUE )
rosfran@220
   197
  {
rosfran@220
   198
  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
rosfran@220
   199
  	
rosfran@220
   200
  	res = gmyth_monitor_handler_start ( livetv->monitor );
rosfran@220
   201
  	
rosfran@220
   202
  	if (res)
rosfran@220
   203
  	{
rosfran@220
   204
  		gmyth_debug("MythTV Monitor event socket connected and listening!");
rosfran@220
   205
  		g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
rosfran@220
   206
                  (GCallback)gmyth_livetv_monitor_signal_handler,
rosfran@220
   207
                  livetv );
rosfran@220
   208
  	}
rosfran@220
   209
  	else
rosfran@220
   210
  	{
rosfran@220
   211
  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
rosfran@220
   212
  		goto error;  		
rosfran@220
   213
  	}
rosfran@220
   214
  }
rosfran@220
   215
  
rosfran@220
   216
error:
rosfran@220
   217
	return res;
rosfran@220
   218
  
rosfran@212
   219
}
rosfran@212
   220
rosfran@220
   221
void
rosfran@220
   222
gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
rosfran@220
   223
{
rosfran@220
   224
	
rosfran@220
   225
  if ( livetv->monitor != NULL )
rosfran@220
   226
  {
rosfran@220
   227
  	g_object_unref( livetv->monitor );
rosfran@220
   228
  	livetv->monitor = NULL;
rosfran@220
   229
  } 
rosfran@220
   230
  
rosfran@220
   231
}  
rosfran@220
   232
rosfran@237
   233
static gboolean
rosfran@237
   234
gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
rosfran@41
   235
{
rosfran@41
   236
	gboolean res = TRUE;
rosfran@41
   237
melunko@117
   238
	GMythSocket *socket = gmyth_socket_new ();
rosfran@216
   239
	
rosfran@216
   240
	livetv->backend_info = backend_info;
rosfran@41
   241
melunko@117
   242
	// FIME: Implement this at gmyth_socket
rosfran@216
   243
	res = gmyth_socket_connect_to_backend (socket, livetv->backend_info->hostname,
rosfran@216
   244
			livetv->backend_info->port, TRUE);
rosfran@41
   245
	if (!res) {
rosfran@41
   246
		g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
rosfran@41
   247
		res = FALSE;
rosfran@41
   248
		goto error;
rosfran@41
   249
	}
rosfran@41
   250
melunko@117
   251
	livetv->is_livetv = TRUE;
rosfran@203
   252
	
rosfran@229
   253
	livetv->local_hostname  = gmyth_socket_get_local_hostname ();
rosfran@41
   254
rosfran@41
   255
	if ( livetv->local_hostname == NULL ) {
rosfran@41
   256
		res = FALSE;
rosfran@41
   257
		goto error;
rosfran@41
   258
	}
rosfran@41
   259
rosfran@65
   260
	// Gets the recorder num
melunko@117
   261
	livetv->recorder = remote_request_next_free_recorder (socket, -1);
melunko@117
   262
	gmyth_socket_close_connection (socket);
rosfran@41
   263
rosfran@65
   264
	if ( livetv->recorder == NULL ) {
rosfran@41
   265
		g_warning ("[%s] None remote encoder available", __FUNCTION__);
rosfran@41
   266
		res = FALSE;
rosfran@41
   267
		goto error;
rosfran@41
   268
	}
rosfran@41
   269
rosfran@41
   270
	// Creates livetv chain handler
rosfran@41
   271
	livetv->tvchain = GMYTH_TVCHAIN ( g_object_new(GMYTH_TVCHAIN_TYPE, NULL) );
rosfran@216
   272
	gmyth_tvchain_initialize ( livetv->tvchain, livetv->backend_info );
rosfran@41
   273
rosfran@41
   274
	if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
rosfran@41
   275
		res = FALSE;
rosfran@41
   276
		goto error;
rosfran@41
   277
	}
rosfran@41
   278
rosfran@41
   279
	// Init remote encoder. Opens its control socket.
rosfran@65
   280
	res = gmyth_recorder_setup(livetv->recorder);
rosfran@41
   281
	if ( !res ) {
rosfran@41
   282
		g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
rosfran@41
   283
		res = FALSE;
rosfran@41
   284
		goto error;
rosfran@41
   285
	}
rosfran@237
   286
	
rosfran@237
   287
	if ( channel != -1 ) {
rosfran@237
   288
	  gint ch = channel;
rosfran@237
   289
	  gint ch_idx = 0;  
rosfran@237
   290
	  for ( ; ch_idx < 5; ch_idx++ ) {
rosfran@237
   291
	  	if ( gmyth_recorder_check_channel( livetv->recorder, ch ) ) {
rosfran@237
   292
		  	if ( gmyth_recorder_set_channel( livetv->recorder, ch ) ) {
rosfran@237
   293
		  		g_print( "[%s] Channel changed!!! [%d].\n", __FUNCTION__, ch );
rosfran@237
   294
		  		break;
rosfran@237
   295
		  	}
rosfran@67
   296
	  	}
rosfran@237
   297
	  	++ch;
rosfran@237
   298
	  }
rosfran@237
   299
	}
rosfran@67
   300
rosfran@41
   301
	// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
rosfran@65
   302
	res = gmyth_recorder_spawntv ( livetv->recorder,
rosfran@41
   303
			gmyth_tvchain_get_id(livetv->tvchain) );
rosfran@41
   304
	if (!res) {
rosfran@41
   305
		g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
rosfran@41
   306
		res = FALSE;
rosfran@41
   307
		goto error;
rosfran@41
   308
	}
rosfran@41
   309
rosfran@41
   310
	// Reload all TV chain from Mysql database.
rosfran@41
   311
	gmyth_tvchain_reload_all (livetv->tvchain);
rosfran@41
   312
rosfran@41
   313
	if ( livetv->tvchain == NULL ) {
rosfran@41
   314
		res = FALSE;
rosfran@41
   315
		goto error;
rosfran@41
   316
	}
rosfran@41
   317
rosfran@41
   318
	// Get program info from database using chanid and starttime
rosfran@41
   319
	livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
rosfran@41
   320
	if ( livetv->proginfo == NULL ) {
rosfran@41
   321
		g_warning ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
rosfran@41
   322
		res = FALSE;
rosfran@41
   323
		goto error;
rosfran@41
   324
	} else {
rosfran@229
   325
		res = TRUE;
rosfran@223
   326
		gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
rosfran@41
   327
	}
rosfran@216
   328
	
rosfran@225
   329
	if ( !gmyth_livetv_monitor_handler_start( livetv ) )
rosfran@225
   330
	{
rosfran@225
   331
		res = FALSE;
rosfran@237
   332
		gmyth_debug( "LiveTV MONITOR handler error on setup!" );
rosfran@225
   333
		goto error;		
rosfran@225
   334
	}
rosfran@225
   335
	
rosfran@216
   336
	livetv->setup_done = TRUE;
rosfran@216
   337
	
rosfran@41
   338
	return res;
rosfran@41
   339
rosfran@41
   340
error:
rosfran@54
   341
	g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
rosfran@54
   342
rosfran@54
   343
	if ( livetv->local_hostname != NULL ) {
rosfran@229
   344
		g_string_free( livetv->local_hostname, FALSE );
rosfran@54
   345
		res = FALSE;
rosfran@54
   346
	}
rosfran@54
   347
rosfran@65
   348
	if ( livetv->recorder != NULL ) {
rosfran@65
   349
		g_object_unref (livetv->recorder);
rosfran@65
   350
		livetv->recorder = NULL;
rosfran@54
   351
	}
rosfran@54
   352
rosfran@54
   353
	if ( livetv->tvchain != NULL ) {
rosfran@54
   354
		g_object_unref (livetv->tvchain);
rosfran@54
   355
		livetv->tvchain = NULL;
rosfran@54
   356
	}
rosfran@54
   357
rosfran@54
   358
	if ( livetv->proginfo != NULL ) {
rosfran@54
   359
		g_object_unref (livetv->proginfo);
rosfran@54
   360
		livetv->proginfo = NULL;
rosfran@54
   361
	}
rosfran@54
   362
rosfran@220
   363
	if ( livetv->monitor != NULL ) {
rosfran@220
   364
		g_object_unref (livetv->monitor);
rosfran@220
   365
		livetv->monitor = NULL;
rosfran@220
   366
	}
rosfran@220
   367
rosfran@54
   368
	return res;
rosfran@54
   369
rosfran@54
   370
}
rosfran@54
   371
rosfran@54
   372
gboolean
rosfran@237
   373
gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
rosfran@237
   374
{
rosfran@237
   375
	return gmyth_livetv_setup_recorder ( livetv, channel, backend_info );
rosfran@237
   376
}
rosfran@237
   377
rosfran@237
   378
gboolean
rosfran@237
   379
gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
rosfran@237
   380
{
rosfran@237
   381
	return gmyth_livetv_setup_recorder ( livetv, -1, backend_info );
rosfran@237
   382
}
rosfran@237
   383
rosfran@237
   384
gboolean
rosfran@54
   385
gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
rosfran@54
   386
{
rosfran@54
   387
	gboolean res = TRUE;
rosfran@54
   388
	
rosfran@216
   389
	if ( !livetv->setup_done )
rosfran@216
   390
	{
rosfran@216
   391
		gmyth_debug ( "Call the setup function first!" );
rosfran@216
   392
		res= FALSE;
rosfran@216
   393
		goto error;		
rosfran@216
   394
	}
rosfran@216
   395
	
rosfran@220
   396
	//if ( !gmyth_livetv_monitor_handler_start( livetv ) )
rosfran@220
   397
	//	goto error;
rosfran@220
   398
	
rosfran@216
   399
	/* Reload all TV chain from Mysql database. */
rosfran@54
   400
	gmyth_tvchain_reload_all (livetv->tvchain);
rosfran@54
   401
rosfran@54
   402
	if ( livetv->tvchain == NULL ) {
rosfran@54
   403
		res = FALSE;
rosfran@54
   404
		goto error;
rosfran@54
   405
	}
rosfran@54
   406
rosfran@54
   407
	// Get program info from database using chanid and starttime
rosfran@54
   408
	livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, -1 );
rosfran@54
   409
	if ( livetv->proginfo == NULL ) {
rosfran@54
   410
		g_warning ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
rosfran@54
   411
		res = FALSE;
rosfran@54
   412
		goto error;
rosfran@54
   413
	} else {
rosfran@229
   414
		res = TRUE;
renatofilho@131
   415
		gmyth_debug ("[%s] GMythLiveTV: All requests to backend to start TV were OK, TV chain changed.\n", __FUNCTION__ );
rosfran@54
   416
	}
rosfran@220
   417
	
rosfran@220
   418
	livetv->setup_done = TRUE;
rosfran@54
   419
rosfran@54
   420
	return res;
rosfran@54
   421
rosfran@54
   422
error:
rosfran@54
   423
	g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
rosfran@41
   424
rosfran@41
   425
	if ( livetv->local_hostname != NULL ) {
rosfran@229
   426
		g_string_free( livetv->local_hostname, FALSE );
rosfran@41
   427
		res = FALSE;
rosfran@41
   428
	}
rosfran@41
   429
rosfran@65
   430
	if ( livetv->recorder != NULL ) {
rosfran@65
   431
		g_object_unref (livetv->recorder);
rosfran@65
   432
		livetv->recorder = NULL;
rosfran@41
   433
	}
rosfran@41
   434
rosfran@41
   435
	if ( livetv->tvchain != NULL ) {
rosfran@41
   436
		g_object_unref (livetv->tvchain);
rosfran@41
   437
		livetv->tvchain = NULL;
rosfran@41
   438
	}
rosfran@41
   439
rosfran@41
   440
	if ( livetv->proginfo != NULL ) {
rosfran@41
   441
		g_object_unref (livetv->proginfo);
rosfran@41
   442
		livetv->proginfo = NULL;
rosfran@41
   443
	}
rosfran@41
   444
rosfran@41
   445
	return res;
rosfran@41
   446
rosfran@41
   447
}
rosfran@41
   448
rosfran@216
   449
GMythFileTransfer *
rosfran@216
   450
gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
rosfran@216
   451
{
rosfran@216
   452
	GMythURI* uri = NULL;
rosfran@216
   453
	
rosfran@216
   454
	if ( NULL == livetv || NULL == livetv->proginfo )
rosfran@216
   455
		goto done;
rosfran@216
   456
	
rosfran@216
   457
	if ( !livetv->setup_done )
rosfran@216
   458
	{
rosfran@216
   459
		gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
rosfran@216
   460
		goto done;
rosfran@216
   461
	}	
rosfran@216
   462
rosfran@216
   463
  gmyth_debug( "URI path = %s.\n", livetv->proginfo->pathname->str ); 
rosfran@216
   464
rosfran@216
   465
	livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
rosfran@216
   466
rosfran@216
   467
  if ( NULL == livetv->file_transfer ) 
rosfran@216
   468
  {
rosfran@216
   469
  	gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
rosfran@216
   470
    goto done;
rosfran@216
   471
  }
rosfran@216
   472
  
rosfran@216
   473
  uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
rosfran@216
   474
  if ( NULL == uri )
rosfran@229
   475
  {  	
rosfran@229
   476
  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
rosfran@229
   477
  	goto done;  	
rosfran@216
   478
  }
rosfran@216
   479
rosfran@216
   480
	if ( !gmyth_file_transfer_open( livetv->file_transfer, uri != NULL ? gmyth_uri_get_path(uri) : 
rosfran@216
   481
				livetv->proginfo->pathname->str ) )
rosfran@216
   482
	{
rosfran@216
   483
		gmyth_debug( "Error: couldn't open the FileTransfer from LiveTV source!" );
rosfran@216
   484
		g_object_unref( livetv->file_transfer );
rosfran@216
   485
		livetv->file_transfer = NULL;
rosfran@216
   486
		goto done;
rosfran@216
   487
	}
rosfran@216
   488
rosfran@216
   489
done:
rosfran@216
   490
	if ( uri != NULL )
rosfran@216
   491
	{
rosfran@216
   492
		g_object_unref( uri );
rosfran@216
   493
		uri = NULL;
rosfran@216
   494
	}	
rosfran@216
   495
	
rosfran@216
   496
	return livetv->file_transfer;
rosfran@216
   497
	
rosfran@216
   498
}
rosfran@216
   499
rosfran@216
   500
/* FIXME: How to proceed differently between livetv and recorded content */
rosfran@41
   501
void
rosfran@41
   502
gmyth_livetv_stop_playing (GMythLiveTV *livetv) 
rosfran@41
   503
{
renatofilho@131
   504
	gmyth_debug ("[%s] Stopping the LiveTV...\n", __FUNCTION__);
rosfran@41
   505
rosfran@41
   506
	if (livetv->is_livetv) {
rosfran@220
   507
		if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
rosfran@41
   508
			g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);	
rosfran@41
   509
		}
rosfran@41
   510
	}
rosfran@41
   511
}
rosfran@41
   512
melunko@117
   513
// FIXME: put here just a wrapper function to call gmyth_recorder_is_recording()...
rosfran@41
   514
gboolean
melunko@107
   515
gmyth_livetv_is_recording ( GMythLiveTV *livetv )
melunko@107
   516
{
melunko@107
   517
  gboolean ret = TRUE;
melunko@107
   518
melunko@107
   519
  GMythStringList *str_list = gmyth_string_list_new ();
melunko@107
   520
  GString *message = g_string_new ("");
melunko@107
   521
renatofilho@131
   522
  gmyth_debug ( "[%s]\n", __FUNCTION__ );
melunko@107
   523
  //g_static_mutex_lock (&mutex);
melunko@107
   524
  //
melunko@107
   525
  g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
melunko@107
   526
      /* FIXME file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id );*/
melunko@107
   527
        livetv->recorder->recorder_num);
melunko@107
   528
  gmyth_string_list_append_string (str_list, message);
melunko@107
   529
  gmyth_string_list_append_string (str_list, g_string_new ("IS_RECORDING"));
melunko@107
   530
melunko@107
   531
  gmyth_socket_sendreceive_stringlist ( livetv->recorder->myth_socket, str_list );
melunko@107
   532
melunko@107
   533
  if ( str_list != NULL && gmyth_string_list_length(str_list) > 0 )
melunko@107
   534
  {
melunko@107
   535
    GString *str = NULL;
melunko@107
   536
    if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strcmp( str->str, "bad" )!= 0 ) {
melunko@107
   537
      gint is_rec = gmyth_string_list_get_int( str_list, 0 );
melunko@107
   538
      if ( is_rec != 0 )
melunko@107
   539
        ret = TRUE;
melunko@107
   540
      else
melunko@107
   541
        ret = FALSE;
melunko@107
   542
    }
melunko@107
   543
  }
melunko@107
   544
  g_print( "[%s] %s, stream is %s being recorded!\n", __FUNCTION__, ret ? "YES" : "NO", ret ? "" : "NOT" );
melunko@107
   545
  //g_static_mutex_unlock (&mutex);
melunko@107
   546
melunko@107
   547
  if ( str_list != NULL )
melunko@107
   548
    g_object_unref (str_list);
melunko@107
   549
melunko@107
   550
  return ret;
melunko@107
   551
melunko@107
   552
}
melunko@107
   553
melunko@107
   554
melunko@107
   555
gboolean
rosfran@41
   556
gmyth_livetv_is_playing (GMythLiveTV *livetv)
rosfran@41
   557
{
rosfran@41
   558
	return TRUE;
rosfran@41
   559
}
rosfran@41
   560
rosfran@41
   561
void
rosfran@41
   562
gmyth_livetv_start_playing (GMythLiveTV *livetv)
rosfran@41
   563
{
rosfran@41
   564
rosfran@41
   565
	// TODO
rosfran@41
   566
rosfran@41
   567
}
rosfran@41
   568