gst-plugins-mythtv/src/myth_livetv.c
author melunko
Mon Oct 23 14:41:30 2006 +0100 (2006-10-23)
branchtrunk
changeset 38 d5f5855e7800
parent 13 f3cdc7844178
permissions -rwxr-xr-x
[svn r39] Added mythtv gnomevfs module based on gmyth
     1 
     2 #include <gmyth/gmyth_context.h>
     3 #include <gmyth/gmyth_remote_util.h>
     4 #include <gmyth/gmyth_tvchain.h>
     5 
     6 #include "myth_livetv.h"
     7 #include "myth_file_transfer.h"
     8 
     9 static void myth_livetv_class_init          (MythLiveTVClass *klass);
    10 static void myth_livetv_init                (MythLiveTV *object);
    11 
    12 static void myth_livetv_dispose  (GObject *object);
    13 static void myth_livetv_finalize (GObject *object);
    14 
    15 static gint tvchain_curr_index = -1; 
    16 
    17 G_DEFINE_TYPE(MythLiveTV, myth_livetv, G_TYPE_OBJECT)
    18 
    19 static void
    20 myth_livetv_class_init (MythLiveTVClass *klass)
    21 {
    22 	GObjectClass *gobject_class;
    23 
    24 	gobject_class = (GObjectClass *) klass;
    25 
    26 	gobject_class->dispose  = myth_livetv_dispose;
    27 	gobject_class->finalize = myth_livetv_finalize;	
    28 }
    29 
    30 static void
    31 myth_livetv_init (MythLiveTV *livetv)
    32 {
    33 	livetv->backend_hostname = NULL;
    34 	livetv->backend_port = 0;
    35 	livetv->local_hostname = NULL;
    36 
    37 	livetv->remote_encoder = NULL;
    38 	livetv->tvchain = NULL;
    39 	livetv->proginfo = NULL;
    40 }
    41 
    42 static void
    43 myth_livetv_dispose  (GObject *object)
    44 {
    45 
    46 	G_OBJECT_CLASS (myth_livetv_parent_class)->dispose (object);
    47 }
    48 
    49 static void
    50 myth_livetv_finalize (GObject *object)
    51 {
    52 	g_signal_handlers_destroy (object);
    53 
    54 	MythLiveTV *livetv = MYTH_LIVETV (object);
    55 
    56 	g_debug ("[%s] Finalizing livetv", __FUNCTION__);
    57 
    58 	if ( livetv->remote_encoder != NULL ) {
    59 		g_object_unref (livetv->remote_encoder);
    60 		livetv->remote_encoder = NULL;
    61 	}
    62 
    63 	if ( livetv->tvchain != NULL ) {
    64 		g_object_unref (livetv->tvchain);
    65 		livetv->tvchain = NULL;
    66 	}
    67 
    68 	if ( livetv->proginfo != NULL ) {
    69 		g_object_unref (livetv->proginfo);
    70 		livetv->proginfo = NULL;
    71 	}
    72 
    73 	G_OBJECT_CLASS ( myth_livetv_parent_class )->finalize ( object );
    74 }
    75 
    76 MythLiveTV*
    77 myth_livetv_new ()
    78 {
    79 	MythLiveTV *livetv = MYTH_LIVETV ( g_object_new( MYTH_LIVETV_TYPE, NULL ) );
    80 
    81 	return livetv;
    82 }
    83 
    84 gboolean
    85 myth_livetv_setup ( MythLiveTV *livetv )
    86 {
    87 	GMythSettings *msettings = gmyth_context_get_settings ();
    88 	gboolean res = TRUE;
    89 
    90 	livetv->is_livetv = TRUE;
    91 
    92 	res = gmyth_context_check_connection();
    93 	if (!res) {
    94 		g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
    95 		res = FALSE;
    96 		goto error;
    97 	}
    98 
    99 	livetv->backend_hostname = gmyth_settings_get_backend_hostname(msettings);
   100 	livetv->backend_port = gmyth_settings_get_backend_port (msettings);
   101 
   102 	livetv->local_hostname  = g_string_new("");    
   103 	gmyth_context_get_local_hostname (livetv->local_hostname);
   104 
   105 	if ( livetv->local_hostname == NULL ) {
   106 		res = FALSE;
   107 		goto error;
   108 	}
   109 
   110 	// Gets the remote encoder num
   111 	livetv->remote_encoder = remote_request_next_free_recorder (-1);
   112 
   113 	if ( livetv->remote_encoder == NULL ) {
   114 		g_warning ("[%s] None remote encoder available", __FUNCTION__);
   115 		res = FALSE;
   116 		goto error;
   117 	}
   118 
   119 	// Creates livetv chain handler
   120 	livetv->tvchain = GMYTH_TVCHAIN ( g_object_new(GMYTH_TVCHAIN_TYPE, NULL) );
   121 	gmyth_tvchain_initialize ( livetv->tvchain, livetv->local_hostname );
   122 
   123 	if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
   124 		res = FALSE;
   125 		goto error;
   126 	}
   127 
   128 	// Init remote encoder. Opens its control socket.
   129 	res = gmyth_remote_encoder_setup(livetv->remote_encoder);
   130 	if ( !res ) {
   131 		g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   132 		res = FALSE;
   133 		goto error;
   134 	}
   135 	// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
   136 	res = gmyth_remote_encoder_spawntv ( livetv->remote_encoder,
   137 			gmyth_tvchain_get_id(livetv->tvchain) );
   138 	if (!res) {
   139 		g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
   140 		res = FALSE;
   141 		goto error;
   142 	}
   143 
   144 	// Reload all TV chain from Mysql database.
   145 	gmyth_tvchain_reload_all (livetv->tvchain);
   146 
   147 	if ( livetv->tvchain == NULL ) {
   148 		res = FALSE;
   149 		goto error;
   150 	}
   151 
   152 	// Get program info from database using chanid and starttime
   153 	livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
   154 	if ( livetv->proginfo == NULL ) {
   155 		g_warning ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
   156 		res = FALSE;
   157 		goto error;
   158 	} else {
   159 		g_debug ("[%s] MythLiveTV: All requests to backend to start TV were OK.\n", __FUNCTION__ );
   160 	}
   161 
   162 	return res;
   163 
   164 error:
   165 	if ( livetv->backend_hostname != NULL ) {
   166 		g_string_free( livetv->backend_hostname, TRUE );
   167 		res = FALSE;
   168 	}
   169 
   170 	if ( livetv->local_hostname != NULL ) {
   171 		g_string_free( livetv->local_hostname, TRUE );
   172 		res = FALSE;
   173 	}
   174 
   175 	if ( livetv->remote_encoder != NULL ) {
   176 		g_object_unref (livetv->remote_encoder);
   177 		livetv->remote_encoder = NULL;
   178 	}
   179 
   180 	if ( livetv->tvchain != NULL ) {
   181 		g_object_unref (livetv->tvchain);
   182 		livetv->tvchain = NULL;
   183 	}
   184 
   185 	if ( livetv->proginfo != NULL ) {
   186 		g_object_unref (livetv->proginfo);
   187 		livetv->proginfo = NULL;
   188 	}
   189 
   190 	return res;
   191 
   192 }
   193 
   194 // FIXME: How to proceed differently between livetv and recorded content
   195 void
   196 myth_livetv_stop_playing (MythLiveTV *livetv) 
   197 {
   198 	g_debug ("[%s] Stopping the LiveTV...\n", __FUNCTION__);
   199 
   200 	if (livetv->is_livetv) {
   201 		if (!gmyth_remote_encoder_stop_livetv (livetv->remote_encoder)) {
   202 			g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);	
   203 		}
   204 	}
   205 }
   206 
   207 gboolean
   208 myth_livetv_is_playing (MythLiveTV *livetv)
   209 {
   210 	return TRUE;
   211 }
   212 
   213 void
   214 myth_livetv_start_playing (MythLiveTV *livetv)
   215 {
   216 
   217 	// TODO
   218 
   219 }
   220