gst-plugins-mythtv/myth_livetv.c
author leo_sobral
Thu Sep 21 00:05:27 2006 +0100 (2006-09-21)
branchtrunk
changeset 3 265cdb1c59e3
parent 2 mythtv_plugin/myth_livetv.c@bd3829c2e9c9
permissions -rwxr-xr-x
[svn r4] Renamed the mythtv GStreamer module name.
     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 G_DEFINE_TYPE(MythLiveTV, myth_livetv, G_TYPE_OBJECT)
    16 
    17 static void
    18 myth_livetv_class_init (MythLiveTVClass *klass)
    19 {
    20 	GObjectClass *gobject_class;
    21 
    22 	gobject_class = (GObjectClass *) klass;
    23 
    24 	gobject_class->dispose  = myth_livetv_dispose;
    25 	gobject_class->finalize = myth_livetv_finalize;	
    26 }
    27 
    28 static void
    29 myth_livetv_init (MythLiveTV *livetv)
    30 {
    31 	livetv->backend_hostname = NULL;
    32 	livetv->backend_port = 0;
    33 	livetv->local_hostname = NULL;
    34 
    35 	livetv->remote_encoder = NULL;
    36 	livetv->tvchain = NULL;
    37 	livetv->proginfo = NULL;
    38 }
    39 
    40 static void
    41 myth_livetv_dispose  (GObject *object)
    42 {
    43 
    44 	G_OBJECT_CLASS (myth_livetv_parent_class)->dispose (object);
    45 }
    46 
    47 static void
    48 myth_livetv_finalize (GObject *object)
    49 {
    50 	g_signal_handlers_destroy (object);
    51 
    52 	MythLiveTV *livetv = MYTH_LIVETV (object);
    53 
    54 	g_debug ("[%s] Finalizing livetv", __FUNCTION__);
    55 
    56 	if ( livetv->remote_encoder != NULL ) {
    57 		g_object_unref (livetv->remote_encoder);
    58 		livetv->remote_encoder = NULL;
    59 	}
    60 
    61 	if ( livetv->tvchain != NULL ) {
    62 		g_object_unref (livetv->tvchain);
    63 		livetv->tvchain = NULL;
    64 	}
    65 
    66 	if ( livetv->proginfo != NULL ) {
    67 		g_object_unref (livetv->proginfo);
    68 		livetv->proginfo = NULL;
    69 	}
    70 
    71 	G_OBJECT_CLASS ( myth_livetv_parent_class )->finalize ( object );
    72 }
    73 
    74 MythLiveTV*
    75 myth_livetv_new ()
    76 {
    77 	MythLiveTV *livetv = MYTH_LIVETV ( g_object_new( MYTH_LIVETV_TYPE, NULL ) );
    78 
    79 	return livetv;
    80 }
    81 
    82 gboolean
    83 myth_livetv_setup ( MythLiveTV *livetv )
    84 {
    85 
    86 	// FIXME: Get from the settings
    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, -1);
   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