gmyth/src/gmyth_livetv.c
author renatofilho
Thu Apr 05 19:39:13 2007 +0100 (2007-04-05)
branchtrunk
changeset 509 f88d659b2d50
parent 493 32148e79242d
child 550 aaf53e3d0b26
permissions -rwxr-xr-x
[svn r514] added a sleep on livetv read file
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"
rosfran@307
    36
#include "gmyth_backendinfo.h"
renatofilho@131
    37
#include "gmyth_debug.h"
rosfran@41
    38
rosfran@42
    39
#include "gmyth_file_transfer.h"
rosfran@216
    40
#include "gmyth_monitor_handler.h"
rosfran@41
    41
rosfran@428
    42
#include "gmyth_common.h"
rosfran@428
    43
rosfran@41
    44
static void gmyth_livetv_class_init          (GMythLiveTVClass *klass);
rosfran@41
    45
static void gmyth_livetv_init                (GMythLiveTV *object);
rosfran@41
    46
rosfran@41
    47
static void gmyth_livetv_dispose  (GObject *object);
rosfran@41
    48
static void gmyth_livetv_finalize (GObject *object);
rosfran@41
    49
rosfran@41
    50
static gint tvchain_curr_index = -1; 
rosfran@41
    51
rosfran@358
    52
/*static GStaticMutex lock = G_STATIC_MUTEX_INIT;*/
melunko@107
    53
rosfran@288
    54
#define GMYTHTV_TRANSFER_MAX_WAITS	    100
melunko@107
    55
rosfran@41
    56
G_DEFINE_TYPE(GMythLiveTV, gmyth_livetv, G_TYPE_OBJECT)
rosfran@41
    57
rosfran@41
    58
static void
rosfran@41
    59
gmyth_livetv_class_init (GMythLiveTVClass *klass)
rosfran@41
    60
{
rosfran@41
    61
	GObjectClass *gobject_class;
rosfran@41
    62
rosfran@41
    63
	gobject_class = (GObjectClass *) klass;
rosfran@41
    64
rosfran@41
    65
	gobject_class->dispose  = gmyth_livetv_dispose;
rosfran@41
    66
	gobject_class->finalize = gmyth_livetv_finalize;	
rosfran@41
    67
}
rosfran@41
    68
rosfran@41
    69
static void
rosfran@41
    70
gmyth_livetv_init (GMythLiveTV *livetv)
rosfran@41
    71
{
leo_sobral@437
    72
    livetv->monitor = NULL;
rosfran@216
    73
	livetv->backend_info = NULL;
rosfran@41
    74
	livetv->local_hostname = NULL;
rosfran@216
    75
	livetv->file_transfer = NULL;
rosfran@216
    76
	livetv->setup_done = FALSE;
rosfran@337
    77
	
rosfran@337
    78
	livetv->socket = NULL;
rosfran@65
    79
	livetv->recorder = NULL;
rosfran@41
    80
	livetv->tvchain = NULL;
rosfran@41
    81
	livetv->proginfo = NULL;
rosfran@307
    82
	livetv->uri = NULL;
rosfran@358
    83
	
leo_sobral@437
    84
	livetv->mutex = g_mutex_new();
rosfran@41
    85
}
rosfran@41
    86
rosfran@41
    87
static void
rosfran@41
    88
gmyth_livetv_dispose  (GObject *object)
rosfran@336
    89
{	
rosfran@41
    90
	GMythLiveTV *livetv = GMYTH_LIVETV (object);
rosfran@41
    91
leo_sobral@437
    92
    if (livetv->disposed) {
leo_sobral@437
    93
        /* If dispose did already run, return. */
leo_sobral@437
    94
        return;
leo_sobral@437
    95
    }
leo_sobral@437
    96
    
leo_sobral@437
    97
    /* Make sure dispose does not run twice. */
leo_sobral@437
    98
    livetv->disposed = TRUE;
leo_sobral@437
    99
rosfran@216
   100
	if ( livetv->monitor != NULL ) {
rosfran@216
   101
		g_object_unref (livetv->monitor);
rosfran@216
   102
		livetv->monitor = NULL;
rosfran@216
   103
	}
rosfran@337
   104
	
rosfran@65
   105
	if ( livetv->recorder != NULL ) {
rosfran@490
   106
        //gmyth_recorder_close(livetv->recorder);
rosfran@65
   107
		g_object_unref (livetv->recorder);
rosfran@65
   108
		livetv->recorder = NULL;
rosfran@41
   109
	}
rosfran@41
   110
rosfran@337
   111
	if ( livetv->socket != NULL ) {
rosfran@337
   112
		g_object_unref (livetv->socket);
rosfran@337
   113
		livetv->socket = NULL;
rosfran@337
   114
	}
rosfran@337
   115
	
rosfran@41
   116
	if ( livetv->tvchain != NULL ) {
rosfran@41
   117
		g_object_unref (livetv->tvchain);
rosfran@41
   118
		livetv->tvchain = NULL;
rosfran@41
   119
	}
rosfran@41
   120
rosfran@41
   121
	if ( livetv->proginfo != NULL ) {
rosfran@41
   122
		g_object_unref (livetv->proginfo);
rosfran@41
   123
		livetv->proginfo = NULL;
rosfran@41
   124
	}
rosfran@216
   125
	
leo_sobral@437
   126
	if ( livetv->file_transfer != NULL ) {
rosfran@216
   127
		g_object_unref (livetv->file_transfer);
rosfran@216
   128
		livetv->file_transfer = NULL;
rosfran@216
   129
	}
rosfran@216
   130
	
rosfran@216
   131
	if ( livetv->backend_info != NULL ) {
rosfran@216
   132
		g_object_unref (livetv->backend_info);
rosfran@216
   133
		livetv->backend_info = NULL;
rosfran@216
   134
	}
rosfran@307
   135
	
leo_sobral@437
   136
	if ( livetv->uri != NULL ) {
rosfran@307
   137
		g_object_unref (livetv->uri);
rosfran@307
   138
		livetv->uri = NULL;
rosfran@307
   139
	}
rosfran@336
   140
	
leo_sobral@437
   141
	if ( livetv->mutex != NULL ) {
rosfran@358
   142
		g_mutex_free (livetv->mutex);
rosfran@358
   143
		livetv->mutex = NULL;
rosfran@358
   144
	}
leo_sobral@452
   145
    
leo_sobral@452
   146
    if ( livetv->local_hostname != NULL ) {
leo_sobral@452
   147
        g_string_free (livetv->local_hostname, TRUE);
leo_sobral@452
   148
        livetv->local_hostname = NULL;
leo_sobral@452
   149
    }
leo_sobral@437
   150
rosfran@336
   151
	G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
rosfran@336
   152
}
rosfran@336
   153
rosfran@336
   154
static void
rosfran@336
   155
gmyth_livetv_finalize (GObject *object)
rosfran@336
   156
{
rosfran@336
   157
	g_signal_handlers_destroy (object);
rosfran@336
   158
rosfran@41
   159
	G_OBJECT_CLASS ( gmyth_livetv_parent_class )->finalize ( object );
rosfran@41
   160
}
rosfran@41
   161
rosfran@428
   162
/**
rosfran@428
   163
 * Creates a new GMythLiveTV instance
rosfran@428
   164
 * 
rosfran@428
   165
 * @return a newly allocated GMythLiveTV instance
rosfran@428
   166
 */
rosfran@41
   167
GMythLiveTV*
leo_sobral@437
   168
gmyth_livetv_new (GMythBackendInfo *backend_info)
rosfran@41
   169
{
rosfran@41
   170
	GMythLiveTV *livetv = GMYTH_LIVETV ( g_object_new( GMYTH_LIVETV_TYPE, NULL ) );
leo_sobral@437
   171
    
leo_sobral@437
   172
	livetv->backend_info = backend_info;
leo_sobral@437
   173
    g_object_ref (livetv->backend_info);
leo_sobral@437
   174
rosfran@41
   175
	return livetv;
rosfran@41
   176
}
rosfran@41
   177
rosfran@428
   178
/**
rosfran@428
   179
 * The GObject signal handler function, from which all status messages 
rosfran@428
   180
 * from the Monitor Handler will be advertized, all time it receives
rosfran@428
   181
 * LiveTV status messages from the MythTV backend
rosfran@428
   182
 * 
rosfran@428
   183
 * @param monitor a GMythMonitorHandler instance
rosfran@428
   184
 * @param msg_code the MythTV's server numeric status code
rosfran@428
   185
 * @param message the message's string description
rosfran@428
   186
 * @param user_data pointer to the GMythLiveTV instance
rosfran@428
   187
 */
rosfran@212
   188
static void
rosfran@212
   189
gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code, 
rosfran@232
   190
							gchar* message, gpointer user_data )
rosfran@212
   191
{
rosfran@233
   192
	GMythLiveTV *live_tv = GMYTH_LIVETV ( user_data );
rosfran@216
   193
	
rosfran@232
   194
	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
   195
					"NULL", user_data != NULL ? "" : "NULL" );
rosfran@216
   196
	
rosfran@232
   197
	if ( NULL == live_tv )
rosfran@232
   198
	{
rosfran@232
   199
		gmyth_debug( "LiveTV_obj is equals to NULL!!!" );
rosfran@232
   200
		return;
rosfran@232
   201
	}		
rosfran@216
   202
	
rosfran@216
   203
	switch ( msg_code ) 
rosfran@216
   204
	{
rosfran@216
   205
		
rosfran@216
   206
		case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
rosfran@216
   207
		{
rosfran@234
   208
			gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
rosfran@234
   209
				"TV Chain ID is the same as the old one...\n", message );
rosfran@234
   210
			if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {				
rosfran@234
   211
				gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!", 
rosfran@234
   212
										(gmyth_tvchain_get_id( live_tv->tvchain ))->str );
rosfran@234
   213
				/* advertises the FileTransfer about the program info changed */
rosfran@234
   214
				if ( live_tv->file_transfer != NULL )
rosfran@234
   215
				{
rosfran@234
   216
					gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
rosfran@234
   217
					
rosfran@234
   218
					gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
rosfran@435
   219
		             msg_code, (gpointer)(live_tv->recorder) );
rosfran@234
   220
		             
rosfran@435
   221
					/* gmyth_livetv_monitor_handler_stop( live_tv ); */
rosfran@234
   222
				} else
rosfran@234
   223
					gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");				
rosfran@234
   224
			}
rosfran@399
   225
			break;
rosfran@298
   226
		}
rosfran@298
   227
		case GMYTH_BACKEND_DONE_RECORDING:
rosfran@298
   228
		{
rosfran@298
   229
			gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
rosfran@298
   230
				"TV Chain ID is the same as the old one...\n", message );
rosfran@298
   231
			if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {				
rosfran@298
   232
				gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!", 
rosfran@298
   233
										(gmyth_tvchain_get_id( live_tv->tvchain ))->str );
rosfran@298
   234
				/* advertises the FileTransfer about the program info changed */
rosfran@298
   235
				if ( live_tv->file_transfer != NULL )
rosfran@298
   236
				{
rosfran@342
   237
					gmyth_debug( "Emitting signal to the FileTransfer... [ \"backend-done-recording\" ]" );
rosfran@298
   238
					
rosfran@298
   239
					gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
rosfran@435
   240
		             msg_code, (gpointer)(live_tv->recorder) );
rosfran@298
   241
		             
rosfran@298
   242
				} else
rosfran@298
   243
					gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");				
rosfran@298
   244
			}
rosfran@399
   245
			break;
rosfran@374
   246
		}
rosfran@374
   247
		case GMYTH_BACKEND_STOP_LIVETV:
rosfran@374
   248
		{
rosfran@374
   249
			gmyth_debug( "LIVETV Stop LiveTV request received [ msg = %s ]. Going out the "\
rosfran@374
   250
				"LiveTV...\n", message );
rosfran@374
   251
			/* stops the LiveTV */
rosfran@374
   252
			if ( live_tv != NULL )
rosfran@374
   253
			{
rosfran@374
   254
				gmyth_debug( "Going out the LiveTV... [ \"quit-livetv\" ]" );
rosfran@374
   255
				
leo_sobral@437
   256
				g_object_unref( live_tv );
rosfran@374
   257
			} else
rosfran@374
   258
				gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");				
rosfran@298
   259
			
rosfran@216
   260
			break;
rosfran@216
   261
		}
rosfran@216
   262
		default:
rosfran@220
   263
			break;		
rosfran@220
   264
	} /* switch (Monitor Handler messages) */
rosfran@220
   265
	
rosfran@220
   266
}
rosfran@220
   267
rosfran@428
   268
/**
rosfran@428
   269
 * Starts the Monitor Handler to this GMythLiveTV session, in order
rosfran@428
   270
 * to receive the status messages from the MythTV's backend server 
rosfran@428
   271
 * 
rosfran@428
   272
 * @param live_tv the GMythLiveTV instance
rosfran@428
   273
 * 
rosfran@428
   274
 * @return <code>true</code> if the Monitor Handler start-up process
rosfran@428
   275
 * 	   had been concluded succcesfully 
rosfran@428
   276
 */
rosfran@220
   277
gboolean
rosfran@220
   278
gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
rosfran@220
   279
{
rosfran@220
   280
	gboolean res = TRUE;
rosfran@220
   281
	
rosfran@220
   282
	if ( livetv->monitor != NULL )
rosfran@220
   283
	{
rosfran@220
   284
		g_object_unref( livetv->monitor );
rosfran@220
   285
		livetv->monitor	= NULL;
rosfran@216
   286
	}
rosfran@216
   287
	
rosfran@220
   288
  livetv->monitor = gmyth_monitor_handler_new ( );
rosfran@220
   289
  
rosfran@220
   290
  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
rosfran@220
   291
  				livetv->backend_info->port );
rosfran@220
   292
  
rosfran@220
   293
  if ( res == TRUE )
rosfran@220
   294
  {
rosfran@220
   295
  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
rosfran@220
   296
  	
rosfran@220
   297
  	res = gmyth_monitor_handler_start ( livetv->monitor );
rosfran@220
   298
  	
rosfran@220
   299
  	if (res)
rosfran@220
   300
  	{
rosfran@220
   301
  		gmyth_debug("MythTV Monitor event socket connected and listening!");
rosfran@220
   302
  		g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
rosfran@220
   303
                  (GCallback)gmyth_livetv_monitor_signal_handler,
rosfran@220
   304
                  livetv );
rosfran@220
   305
  	}
rosfran@220
   306
  	else
rosfran@220
   307
  	{
rosfran@220
   308
  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
rosfran@220
   309
  		goto error;  		
rosfran@220
   310
  	}
rosfran@220
   311
  }
rosfran@220
   312
  
rosfran@220
   313
error:
rosfran@220
   314
	return res;
rosfran@220
   315
  
rosfran@212
   316
}
rosfran@212
   317
rosfran@428
   318
/**
rosfran@428
   319
 * Stops the Monitor Handler to this GMythLiveTV session, in order
rosfran@428
   320
 * to stop receiving the status messages from the MythTV's backend server 
rosfran@428
   321
 * 
rosfran@428
   322
 * @param live_tv the GMythLiveTV instance
rosfran@428
   323
 * 
rosfran@428
   324
 * @return <code>true</code> if the Monitor Handler shutdown process
rosfran@428
   325
 * 	   had been concluded succcesfully 
rosfran@428
   326
 */
rosfran@220
   327
void
rosfran@220
   328
gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
rosfran@220
   329
{
rosfran@220
   330
	
rosfran@220
   331
  if ( livetv->monitor != NULL )
rosfran@220
   332
  {
rosfran@220
   333
  	g_object_unref( livetv->monitor );
rosfran@220
   334
  	livetv->monitor = NULL;
rosfran@220
   335
  } 
rosfran@220
   336
  
rosfran@326
   337
}
rosfran@307
   338
rosfran@428
   339
#if 0
rosfran@307
   340
static gchar*
rosfran@307
   341
gmyth_livetv_create_remote_url( GMythLiveTV *livetv )
rosfran@307
   342
{
rosfran@307
   343
	gchar *uri = g_strdup("");
rosfran@307
   344
	gmyth_backend_info_get_remote_h
rosfran@307
   345
	
rosfran@307
   346
	//gmyth_backend(livetv->backend_info)
rosfran@307
   347
	
rosfran@307
   348
	return uri;
rosfran@307
   349
}
rosfran@428
   350
#endif
rosfran@307
   351
rosfran@428
   352
/**
rosfran@428
   353
 * Configures the GMythLiveTV session, sends SPAWN_LIVETV message, 
rosfran@428
   354
 * sets the channel name, and gets the first program info about the
rosfran@428
   355
 * actual recording 
rosfran@428
   356
 * 
rosfran@428
   357
 * @param live_tv the GMythLiveTV instance
rosfran@428
   358
 * @param channel the channel name (the chan_name field, from the tvchain table)
rosfran@428
   359
 * @param backend_info the GMythBackendInfo describing the remote server
rosfran@428
   360
 * 
rosfran@428
   361
 * @return <code>true</code> if the LiveTV's recorder instance configuration 
rosfran@428
   362
 * 				had been concluded succcesfully 
rosfran@428
   363
 */
rosfran@237
   364
static gboolean
leo_sobral@437
   365
gmyth_livetv_setup_recorder_channel_name (GMythLiveTV *livetv, gchar* channel)
rosfran@41
   366
{
rosfran@41
   367
	gboolean res = TRUE;
leo_sobral@437
   368
    
leo_sobral@437
   369
    g_return_val_if_fail (livetv != NULL, FALSE);
leo_sobral@437
   370
    
rosfran@356
   371
	if ( NULL == livetv->socket ) {
rosfran@337
   372
		livetv->socket = gmyth_socket_new ();
rosfran@343
   373
		
rosfran@343
   374
		/* FIME: Implement this at gmyth_socket */
rosfran@343
   375
		res = gmyth_socket_connect_to_backend (livetv->socket, livetv->backend_info->hostname,
rosfran@343
   376
				livetv->backend_info->port, TRUE);
rosfran@343
   377
		if (!res) {
rosfran@467
   378
			gmyth_debug ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
rosfran@343
   379
			res = FALSE;
rosfran@343
   380
			goto error;
rosfran@343
   381
		}
rosfran@356
   382
	}
rosfran@216
   383
	
rosfran@358
   384
	g_mutex_lock( livetv->mutex );
rosfran@287
   385
	
melunko@117
   386
	livetv->is_livetv = TRUE;
rosfran@203
   387
	
rosfran@229
   388
	livetv->local_hostname  = gmyth_socket_get_local_hostname ();
rosfran@41
   389
rosfran@41
   390
	if ( livetv->local_hostname == NULL ) {
rosfran@41
   391
		res = FALSE;
rosfran@41
   392
		goto error;
rosfran@41
   393
	}
rosfran@322
   394
	
rosfran@356
   395
	if  ( livetv->recorder != NULL )
rosfran@356
   396
	{
rosfran@356
   397
		g_object_unref( livetv->recorder );
rosfran@356
   398
		livetv->recorder= NULL;
rosfran@356
   399
	} 
rosfran@356
   400
	
rosfran@337
   401
	if ( gmyth_remote_util_get_free_recorder_count (livetv->socket) <= 0 ) {
rosfran@428
   402
		
rosfran@428
   403
#ifdef GMYTH_USE_DEBUG
rosfran@358
   404
		gmyth_debug ("No free remote encoder available.");
rosfran@428
   405
#endif
rosfran@358
   406
		res = FALSE;
rosfran@358
   407
		goto error;
rosfran@322
   408
	}
rosfran@428
   409
rosfran@322
   410
	/* Gets the recorder num */
rosfran@337
   411
	livetv->recorder = remote_request_next_free_recorder (livetv->socket, -1);
rosfran@343
   412
	gmyth_socket_close_connection (livetv->socket);
rosfran@41
   413
rosfran@337
   414
	if ( NULL == livetv->recorder ) {
rosfran@467
   415
		gmyth_debug ("[%s] None remote encoder available", __FUNCTION__);
rosfran@41
   416
		res = FALSE;
rosfran@41
   417
		goto error;
rosfran@41
   418
	}
rosfran@279
   419
	
rosfran@326
   420
	/* Init remote encoder. Opens its control socket. */
rosfran@287
   421
	res = gmyth_recorder_setup(livetv->recorder);
rosfran@287
   422
	if ( !res ) {
rosfran@467
   423
		gmyth_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
rosfran@287
   424
		res = FALSE;
rosfran@287
   425
		goto error;
rosfran@287
   426
	}
rosfran@486
   427
    
rosfran@326
   428
	/* Creates livetv chain handler */
rosfran@334
   429
	livetv->tvchain = gmyth_tvchain_new();
rosfran@282
   430
	gmyth_tvchain_initialize ( livetv->tvchain, livetv->backend_info );
rosfran@282
   431
rosfran@282
   432
	if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
rosfran@282
   433
		res = FALSE;
rosfran@282
   434
		goto error;
rosfran@282
   435
	}
rosfran@282
   436
	
rosfran@282
   437
	// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
rosfran@282
   438
	res = gmyth_recorder_spawntv ( livetv->recorder,
rosfran@282
   439
			gmyth_tvchain_get_id(livetv->tvchain) );
rosfran@282
   440
	if (!res) {
rosfran@467
   441
		gmyth_debug ("[%s] Fail while spawn tv\n", __FUNCTION__);
rosfran@41
   442
		res = FALSE;
rosfran@41
   443
		goto error;
rosfran@41
   444
	}
rosfran@288
   445
	
rosfran@288
   446
  if ( res == TRUE ) {
rosfran@288
   447
    /* loop finished, set the max tries variable to zero again... */
rosfran@288
   448
    gint wait_to_transfer = 0;
rosfran@288
   449
rosfran@288
   450
    while (wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
rosfran@292
   451
        (gmyth_recorder_is_recording (livetv->recorder) == FALSE))
rosfran@399
   452
      g_usleep (300);
rosfran@288
   453
rosfran@307
   454
		if ( channel != NULL ) 
rosfran@288
   455
		{
rosfran@288
   456
			/* Pauses remote encoder. */
rosfran@288
   457
			res = gmyth_recorder_pause_recording(livetv->recorder);
rosfran@288
   458
			if ( !res ) {
rosfran@467
   459
				gmyth_debug ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
rosfran@288
   460
				res = FALSE;
rosfran@288
   461
				goto error;
rosfran@288
   462
			}
rosfran@288
   463
	
rosfran@472
   464
    	  	if ( gmyth_recorder_check_channel_name( livetv->recorder, channel ) )
rosfran@472
   465
    	  	{
rosfran@472
   466
    		  	if ( gmyth_recorder_set_channel_name( livetv->recorder, channel ) )
rosfran@472
   467
    		  	{
rosfran@472
   468
    		  		gmyth_debug( "Channel changed!!! [%s].\n", channel );
rosfran@472
   469
    		  	}
rosfran@472
   470
    	  	}
rosfran@288
   471
	
rosfran@326
   472
		} /* if - changes the channel number */
renatofilho@509
   473
    //sleep (4);                  /* FIXME: this is evil (tpm) */
rosfran@288
   474
  }
rosfran@302
   475
  
rosfran@302
   476
  /* DEBUG message */  
rosfran@302
   477
	GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
rosfran@302
   478
	
rosfran@302
   479
	if ( NULL == prog_info )
rosfran@302
   480
	{
rosfran@302
   481
		gmyth_debug( "ProgramInfo is equals to NULL!!!" );
rosfran@302
   482
		
rosfran@493
   483
        gint i;
rosfran@493
   484
        gchar *channame = NULL;
rosfran@493
   485
    
rosfran@493
   486
        gmyth_debug( "Problem getting current proginfo!\n" );
rosfran@493
   487
    
rosfran@493
   488
        /*
rosfran@493
   489
         * mythbackend must not be tuned in to a channel, so keep
rosfran@493
   490
         * changing channels until we find a valid one, or until
rosfran@493
   491
         * we decide to give up.
rosfran@493
   492
         */
rosfran@493
   493
        for (i=1; i<1000; i++) {
rosfran@493
   494
    		if ( channame != NULL )
rosfran@493
   495
    			g_free(channame);
rosfran@493
   496
            channame = g_strdup_printf( "%d", i );
rosfran@493
   497
            if (gmyth_recorder_set_channel_name(livetv->recorder, channame) < 0) {
rosfran@493
   498
                continue;
rosfran@493
   499
            }
rosfran@493
   500
            prog_info = gmyth_recorder_get_next_program_info(livetv->recorder, BROWSE_DIRECTION_UP);
rosfran@493
   501
            if (prog_info != NULL)
rosfran@493
   502
                break;
rosfran@493
   503
        }
rosfran@326
   504
rosfran@326
   505
	} /* if - Program Info */
rosfran@326
   506
	
rosfran@302
   507
	/* prints program info data text */ 
melunko@412
   508
#ifdef GMYTH_USE_DEBUG	
rosfran@307
   509
	gmyth_debug( "New ProgramInfo...\n" );
rosfran@302
   510
	gmyth_program_info_print( prog_info );
melunko@412
   511
#endif	
melunko@412
   512
rosfran@307
   513
	/* check if the program chain could be obtained from the MythTV protocol message */
rosfran@307
   514
	if ( prog_info != NULL )
rosfran@307
   515
	{
rosfran@428
   516
#ifdef GMYTH_USE_DEBUG
melunko@412
   517
		gmyth_debug( "Program Info: %s\n", gmyth_program_info_to_string( prog_info ) );
rosfran@428
   518
#endif
rosfran@310
   519
		livetv->proginfo = prog_info;
rosfran@310
   520
		/* testing change channel */
rosfran@310
   521
		//gmyth_recorder_spawntv_no_tvchain( livetv->recorder );
rosfran@307
   522
	} else {
rosfran@307
   523
		
rosfran@307
   524
		/* check for the program info in the TV program chain could be obtained 
rosfran@307
   525
		   from the MythTV MySQL database */
rosfran@288
   526
rosfran@307
   527
		/* Reload all TV chain from Mysql database. */
rosfran@307
   528
		gmyth_tvchain_reload_all (livetv->tvchain);
rosfran@307
   529
	
rosfran@307
   530
		if ( livetv->tvchain == NULL ) {
rosfran@307
   531
			res = FALSE;
rosfran@307
   532
			goto error;
rosfran@307
   533
		}
rosfran@307
   534
		
rosfran@307
   535
		/* Get program info from database using chanid and starttime */
rosfran@307
   536
		livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
rosfran@307
   537
		if ( livetv->proginfo == NULL ) {
rosfran@467
   538
			gmyth_debug ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
rosfran@307
   539
			res = FALSE;
rosfran@307
   540
			goto error;
rosfran@307
   541
		} else {
rosfran@307
   542
			res = TRUE;
rosfran@307
   543
			gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
rosfran@307
   544
		}
rosfran@307
   545
		
rosfran@41
   546
	}
rosfran@288
   547
	
leo_sobral@437
   548
	livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( livetv->backend_info );
rosfran@296
   549
	
rosfran@358
   550
	g_mutex_unlock( livetv->mutex );
rosfran@282
   551
rosfran@225
   552
	if ( !gmyth_livetv_monitor_handler_start( livetv ) )
rosfran@225
   553
	{
rosfran@225
   554
		res = FALSE;
rosfran@237
   555
		gmyth_debug( "LiveTV MONITOR handler error on setup!" );
rosfran@225
   556
		goto error;		
rosfran@225
   557
	}
rosfran@225
   558
	
rosfran@216
   559
	livetv->setup_done = TRUE;
rosfran@216
   560
	
rosfran@41
   561
	return res;
rosfran@41
   562
rosfran@41
   563
error:
rosfran@467
   564
	gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
rosfran@54
   565
leo_sobral@437
   566
    res = FALSE;
leo_sobral@437
   567
leo_sobral@437
   568
    g_string_free( livetv->local_hostname, TRUE );
rosfran@54
   569
rosfran@65
   570
	if ( livetv->recorder != NULL ) {
rosfran@65
   571
		g_object_unref (livetv->recorder);
rosfran@65
   572
		livetv->recorder = NULL;
rosfran@54
   573
	}
rosfran@54
   574
rosfran@54
   575
	if ( livetv->tvchain != NULL ) {
rosfran@54
   576
		g_object_unref (livetv->tvchain);
rosfran@54
   577
		livetv->tvchain = NULL;
rosfran@54
   578
	}
rosfran@54
   579
rosfran@54
   580
	if ( livetv->proginfo != NULL ) {
rosfran@54
   581
		g_object_unref (livetv->proginfo);
rosfran@54
   582
		livetv->proginfo = NULL;
rosfran@54
   583
	}
rosfran@54
   584
rosfran@220
   585
	if ( livetv->monitor != NULL ) {
rosfran@220
   586
		g_object_unref (livetv->monitor);
rosfran@220
   587
		livetv->monitor = NULL;
rosfran@220
   588
	}
rosfran@220
   589
rosfran@54
   590
	return res;
rosfran@54
   591
rosfran@54
   592
}
rosfran@54
   593
rosfran@428
   594
/**
rosfran@428
   595
 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message, 
rosfran@428
   596
 * sets the channel name, and gets the first program info about the
rosfran@428
   597
 * actual recording 
rosfran@428
   598
 * 
rosfran@428
   599
 * @param live_tv the GMythLiveTV instance
rosfran@428
   600
 * @param channel the channel name, in numerical format
rosfran@428
   601
 * @param backend_info the GMythBackendInfo describing the remote server
rosfran@428
   602
 * 
rosfran@428
   603
 * @return <code>true</code> if the LiveTV's recorder instance configuration 
rosfran@428
   604
 * 				had been concluded succcesfully 
rosfran@428
   605
 */
rosfran@307
   606
static gboolean
leo_sobral@437
   607
gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel)
rosfran@307
   608
{
rosfran@307
   609
	return gmyth_livetv_setup_recorder_channel_name ( livetv, ( channel != -1 ) ? 
leo_sobral@437
   610
				g_strdup_printf( "%d", channel ) : NULL );
rosfran@307
   611
}
rosfran@307
   612
rosfran@428
   613
/**
rosfran@428
   614
 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message, 
rosfran@428
   615
 * sets the channel name (numerical format), and gets the first program info about the
rosfran@428
   616
 * actual recording 
rosfran@428
   617
 * 
rosfran@428
   618
 * @param live_tv the GMythLiveTV instance
rosfran@428
   619
 * @param channel the channel name, in numerical format
rosfran@428
   620
 * @param backend_info the GMythBackendInfo describing the remote server
rosfran@428
   621
 * 
rosfran@428
   622
 * @return <code>true</code> if the LiveTV's recorder instance configuration 
rosfran@428
   623
 * 				had been concluded succcesfully 
rosfran@428
   624
 */
rosfran@54
   625
gboolean
leo_sobral@437
   626
gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel )
rosfran@237
   627
{
leo_sobral@437
   628
	return gmyth_livetv_setup_recorder ( livetv, channel );
rosfran@237
   629
}
rosfran@237
   630
rosfran@428
   631
/**
rosfran@428
   632
 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message, 
rosfran@428
   633
 * sets the channel name (string format), and gets the first program info about the
rosfran@428
   634
 * actual recording 
rosfran@428
   635
 * 
rosfran@428
   636
 * @param live_tv the GMythLiveTV instance
rosfran@428
   637
 * @param channel the channel name, in numerical format
rosfran@428
   638
 * @param backend_info the GMythBackendInfo describing the remote server
rosfran@428
   639
 * 
rosfran@428
   640
 * @return <code>true</code> if the LiveTV's recorder instance configuration 
rosfran@428
   641
 * 				had been concluded succcesfully 
rosfran@428
   642
 */
rosfran@237
   643
gboolean
leo_sobral@437
   644
gmyth_livetv_channel_name_setup ( GMythLiveTV *livetv, gchar* channel )
rosfran@307
   645
{
leo_sobral@437
   646
	return gmyth_livetv_setup_recorder_channel_name ( livetv, channel );
rosfran@307
   647
}
rosfran@307
   648
rosfran@428
   649
/**
rosfran@428
   650
 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message, 
rosfran@428
   651
 * and gets the first program info about the actual recording
rosfran@428
   652
 * (doesn't changes the channel). 
rosfran@428
   653
 * 
rosfran@428
   654
 * @param live_tv the GMythLiveTV instance
rosfran@428
   655
 * @param backend_info the GMythBackendInfo describing the remote server
rosfran@428
   656
 * 
rosfran@428
   657
 * @return <code>true</code> if the LiveTV's recorder instance configuration 
rosfran@428
   658
 * 				had been concluded succcesfully 
rosfran@428
   659
 */
rosfran@307
   660
gboolean
leo_sobral@437
   661
gmyth_livetv_setup ( GMythLiveTV *livetv )
rosfran@237
   662
{
leo_sobral@437
   663
	return gmyth_livetv_setup_recorder ( livetv, -1 );
rosfran@237
   664
}
rosfran@237
   665
rosfran@428
   666
/**
rosfran@428
   667
 * Gets the next program info from this GMythLiveTV session.
rosfran@428
   668
 * 
rosfran@428
   669
 * @param live_tv the GMythLiveTV instance
rosfran@428
   670
 * 
rosfran@428
   671
 * @return <code>true</code> if the next program info could be got 
rosfran@428
   672
 */
rosfran@237
   673
gboolean
rosfran@54
   674
gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
rosfran@54
   675
{
rosfran@54
   676
	gboolean res = TRUE;
rosfran@302
   677
	GMythProgramInfo *prog_info = NULL;
rosfran@54
   678
	
rosfran@216
   679
	if ( !livetv->setup_done )
rosfran@216
   680
	{
rosfran@216
   681
		gmyth_debug ( "Call the setup function first!" );
rosfran@216
   682
		goto error;		
rosfran@216
   683
	}
rosfran@216
   684
	
rosfran@342
   685
	gmyth_debug( "Current ProgramInfo...\n" );
rosfran@342
   686
	prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
rosfran@321
   687
	
rosfran@342
   688
	if ( prog_info != NULL )
rosfran@302
   689
	{
rosfran@342
   690
		livetv->proginfo = prog_info;
rosfran@342
   691
	} else {
leo_sobral@437
   692
		gmyth_debug( "ProgramInfo equals to NULL!!! Getting the next program info..." );
rosfran@342
   693
		prog_info = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_RIGHT );
rosfran@342
   694
		livetv->proginfo = prog_info;		
rosfran@302
   695
	}
rosfran@302
   696
	/* prints program info data text */ 
melunko@424
   697
#ifdef GMYTH_USE_DEBUG
rosfran@302
   698
	gmyth_program_info_print( prog_info );
melunko@424
   699
#endif
rosfran@54
   700
rosfran@302
   701
	if ( prog_info != NULL ) {		
rosfran@302
   702
		res = TRUE;
rosfran@307
   703
		livetv->proginfo = prog_info;
rosfran@302
   704
		gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK, program info changed.");
rosfran@302
   705
	} else {
rosfran@467
   706
		gmyth_debug ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
rosfran@54
   707
		goto error;
rosfran@54
   708
	}
rosfran@220
   709
	
rosfran@220
   710
	livetv->setup_done = TRUE;
rosfran@54
   711
rosfran@54
   712
	return res;
rosfran@54
   713
rosfran@54
   714
error:
rosfran@467
   715
	gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
rosfran@41
   716
leo_sobral@437
   717
    res = FALSE;
leo_sobral@437
   718
    
leo_sobral@437
   719
    g_string_free( livetv->local_hostname, TRUE );
rosfran@41
   720
rosfran@65
   721
	if ( livetv->recorder != NULL ) {
rosfran@65
   722
		g_object_unref (livetv->recorder);
rosfran@65
   723
		livetv->recorder = NULL;
rosfran@41
   724
	}
rosfran@41
   725
rosfran@41
   726
	if ( livetv->tvchain != NULL ) {
rosfran@41
   727
		g_object_unref (livetv->tvchain);
rosfran@41
   728
		livetv->tvchain = NULL;
rosfran@41
   729
	}
rosfran@41
   730
rosfran@41
   731
	if ( livetv->proginfo != NULL ) {
rosfran@41
   732
		g_object_unref (livetv->proginfo);
rosfran@41
   733
		livetv->proginfo = NULL;
rosfran@41
   734
	}
rosfran@41
   735
rosfran@41
   736
	return res;
rosfran@41
   737
}
rosfran@41
   738
rosfran@428
   739
/**
rosfran@428
   740
 * Creates a File Transfer session, using all configuration information
rosfran@428
   741
 * got from the actual program info.
rosfran@428
   742
 * 
rosfran@428
   743
 * @param live_tv the GMythLiveTV instance
rosfran@428
   744
 * 
rosfran@428
   745
 * @return the actual GMythFileTransfer instance, generated using the
rosfran@428
   746
 * 		data got from the actual program info.
rosfran@428
   747
 */
rosfran@216
   748
GMythFileTransfer *
rosfran@216
   749
gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
rosfran@216
   750
{
rosfran@307
   751
	//GMythURI* uri = NULL;
rosfran@216
   752
	
rosfran@307
   753
	if ( NULL == livetv )
rosfran@216
   754
		goto done;
rosfran@216
   755
	
rosfran@216
   756
	if ( !livetv->setup_done )
rosfran@216
   757
	{
rosfran@216
   758
		gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
rosfran@216
   759
		goto done;
rosfran@307
   760
	}
rosfran@307
   761
	
rosfran@428
   762
#ifdef GMYTH_USE_DEBUG
rosfran@307
   763
	if ( livetv->proginfo != NULL )
rosfran@321
   764
  	gmyth_debug( "URI path (from program info) = %s.\n", livetv->proginfo->pathname->str );
rosfran@307
   765
  else
rosfran@428
   766
  	gmyth_debug( "URI path (from URI) = %s.\n", livetv->uri->uri->str );
rosfran@428
   767
#endif
rosfran@292
   768
  
rosfran@358
   769
  g_mutex_lock( livetv->mutex );
rosfran@302
   770
  
rosfran@302
   771
  if ( livetv->file_transfer != NULL )
rosfran@302
   772
  {
rosfran@307
   773
  	/*gmyth_file_transfer_close( livetv->file_transfer );*/
rosfran@302
   774
  	g_object_unref( livetv->file_transfer );
rosfran@302
   775
  	livetv->file_transfer = NULL;
rosfran@302
   776
  }  	
rosfran@216
   777
rosfran@321
   778
  if ( livetv->uri != NULL )  
rosfran@216
   779
  {
rosfran@321
   780
  	/* 
rosfran@307
   781
  	if ( livetv->uri->path != NULL )
rosfran@307
   782
  	{
rosfran@307
   783
  		g_string_free( livetv->uri->path, FALSE );
rosfran@307
   784
  		livetv->uri->path = NULL;
rosfran@307
   785
  	}
rosfran@321
   786
  	*/
rosfran@428
   787
#ifdef GMYTH_USE_DEBUG
rosfran@428
   788
  	gmyth_debug( "URI is not NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
rosfran@428
   789
#endif
rosfran@321
   790
  	livetv->uri->path = g_string_erase(livetv->uri->path, 0, -1);
rosfran@307
   791
  	livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
rosfran@307
   792
  } else {
rosfran@428
   793
#ifdef GMYTH_USE_DEBUG
rosfran@321
   794
  	gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
rosfran@428
   795
#endif
rosfran@307
   796
  	livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
rosfran@307
   797
  }
rosfran@307
   798
  	
rosfran@307
   799
  if ( NULL == livetv->uri )
rosfran@229
   800
  {  	
rosfran@229
   801
  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
rosfran@229
   802
  	goto done;  	
rosfran@216
   803
  }
leo_sobral@437
   804
 
rosfran@321
   805
	livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
rosfran@321
   806
rosfran@321
   807
  if ( NULL == livetv->file_transfer ) 
rosfran@321
   808
  {
rosfran@321
   809
  	gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
rosfran@321
   810
    goto done;
rosfran@321
   811
  }
rosfran@378
   812
  
leo_sobral@437
   813
    g_object_ref( livetv->file_transfer );
leo_sobral@437
   814
rosfran@428
   815
  /* gmyth_file_transfer_settimeout( livetv->file_transfer, TRUE ); */
rosfran@292
   816
	
rosfran@428
   817
  g_mutex_unlock( livetv->mutex );
rosfran@216
   818
rosfran@216
   819
done:
rosfran@307
   820
	/*
rosfran@216
   821
	if ( uri != NULL )
rosfran@216
   822
	{
rosfran@216
   823
		g_object_unref( uri );
rosfran@216
   824
		uri = NULL;
rosfran@307
   825
	}
rosfran@307
   826
	*/	
rosfran@216
   827
	
rosfran@216
   828
	return livetv->file_transfer;
rosfran@216
   829
	
rosfran@216
   830
}
rosfran@216
   831
rosfran@428
   832
/**
rosfran@428
   833
 * Stops this LiveTV session.
rosfran@428
   834
 * 
rosfran@428
   835
 * @param live_tv the GMythLiveTV instance
rosfran@428
   836
 */
rosfran@41
   837
void
rosfran@41
   838
gmyth_livetv_stop_playing (GMythLiveTV *livetv) 
rosfran@41
   839
{
melunko@278
   840
	gmyth_debug ("Stopping the LiveTV...\n");
rosfran@41
   841
rosfran@41
   842
	if (livetv->is_livetv) {
rosfran@220
   843
		if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
rosfran@428
   844
			gmyth_debug ("[%s] Error while stoping remote encoder", __FUNCTION__);	
rosfran@41
   845
		}
rosfran@356
   846
		
rosfran@356
   847
		if ( !gmyth_recorder_finish_recording(livetv->recorder) ) {
rosfran@428
   848
			gmyth_debug ("[%s] Error while finishing recording on remote encoder", __FUNCTION__);	
rosfran@356
   849
		}
rosfran@41
   850
	}
rosfran@41
   851
}
rosfran@41
   852
melunko@107
   853
gboolean
rosfran@41
   854
gmyth_livetv_is_playing (GMythLiveTV *livetv)
rosfran@41
   855
{
rosfran@41
   856
	return TRUE;
rosfran@41
   857
}
rosfran@41
   858
rosfran@41
   859
void
rosfran@41
   860
gmyth_livetv_start_playing (GMythLiveTV *livetv)
rosfran@41
   861
{
rosfran@41
   862
rosfran@41
   863
	// TODO
rosfran@41
   864
rosfran@41
   865
}
rosfran@41
   866