4 * @file gmyth/gmyth_livetv.c
6 * @brief <p> GMythLiveTV starts a remote TV session with the MythTV backend.
8 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
9 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "gmyth_livetv.h"
33 #include "gmyth_remote_util.h"
34 #include "gmyth_tvchain.h"
35 #include "gmyth_socket.h"
36 #include "gmyth_backendinfo.h"
37 #include "gmyth_debug.h"
39 #include "gmyth_file_transfer.h"
40 #include "gmyth_monitor_handler.h"
42 static void gmyth_livetv_class_init (GMythLiveTVClass *klass);
43 static void gmyth_livetv_init (GMythLiveTV *object);
45 static void gmyth_livetv_dispose (GObject *object);
46 static void gmyth_livetv_finalize (GObject *object);
48 static gint tvchain_curr_index = -1;
50 static GStaticMutex lock = G_STATIC_MUTEX_INIT;
52 #define GMYTHTV_TRANSFER_MAX_WAITS 100
54 G_DEFINE_TYPE(GMythLiveTV, gmyth_livetv, G_TYPE_OBJECT)
57 gmyth_livetv_class_init (GMythLiveTVClass *klass)
59 GObjectClass *gobject_class;
61 gobject_class = (GObjectClass *) klass;
63 gobject_class->dispose = gmyth_livetv_dispose;
64 gobject_class->finalize = gmyth_livetv_finalize;
68 gmyth_livetv_init (GMythLiveTV *livetv)
70 livetv->backend_info = NULL;
71 livetv->local_hostname = NULL;
72 livetv->file_transfer = NULL;
73 livetv->setup_done = FALSE;
75 livetv->recorder = NULL;
76 livetv->tvchain = NULL;
77 livetv->proginfo = NULL;
83 gmyth_livetv_dispose (GObject *object)
85 G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
89 gmyth_livetv_finalize (GObject *object)
91 g_signal_handlers_destroy (object);
93 GMythLiveTV *livetv = GMYTH_LIVETV (object);
95 gmyth_debug ("Finalizing livetv");
97 if ( livetv->monitor != NULL ) {
98 g_object_unref (livetv->monitor);
99 livetv->monitor = NULL;
102 if ( livetv->recorder != NULL ) {
103 g_object_unref (livetv->recorder);
104 livetv->recorder = NULL;
107 if ( livetv->tvchain != NULL ) {
108 g_object_unref (livetv->tvchain);
109 livetv->tvchain = NULL;
112 if ( livetv->proginfo != NULL ) {
113 g_object_unref (livetv->proginfo);
114 livetv->proginfo = NULL;
117 if ( livetv->file_transfer != NULL ) {
118 g_object_unref (livetv->file_transfer);
119 livetv->file_transfer = NULL;
122 if ( livetv->backend_info != NULL ) {
123 g_object_unref (livetv->backend_info);
124 livetv->backend_info = NULL;
127 if ( livetv->uri != NULL )
129 g_object_unref (livetv->uri);
133 G_OBJECT_CLASS ( gmyth_livetv_parent_class )->finalize ( object );
139 GMythLiveTV *livetv = GMYTH_LIVETV ( g_object_new( GMYTH_LIVETV_TYPE, NULL ) );
145 gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code,
146 gchar* message, gpointer user_data )
148 GMythLiveTV *live_tv = GMYTH_LIVETV ( user_data );
149 //g_object_ref( live_tv );
151 gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, live_tv param = %s, user_data = %s )\n", message, msg_code, live_tv != NULL ? "" :
152 "NULL", user_data != NULL ? "" : "NULL" );
154 if ( NULL == live_tv )
156 gmyth_debug( "LiveTV_obj is equals to NULL!!!" );
163 case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
165 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
166 "TV Chain ID is the same as the old one...\n", message );
167 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
168 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
169 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
170 /* advertises the FileTransfer about the program info changed */
171 if ( live_tv->file_transfer != NULL )
173 gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
175 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
176 msg_code, (gpointer)live_tv );
178 //gmyth_livetv_monitor_handler_stop( live_tv );
180 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
183 case GMYTH_BACKEND_DONE_RECORDING:
185 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
186 "TV Chain ID is the same as the old one...\n", message );
187 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
188 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
189 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
190 /* advertises the FileTransfer about the program info changed */
191 if ( live_tv->file_transfer != NULL )
193 gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
195 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
196 msg_code, (gpointer)live_tv );
198 //gmyth_livetv_monitor_handler_stop( live_tv );
200 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
207 } /* switch (Monitor Handler messages) */
212 gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
216 if ( livetv->monitor != NULL )
218 g_object_unref( livetv->monitor );
219 livetv->monitor = NULL;
222 livetv->monitor = gmyth_monitor_handler_new ( );
224 res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname,
225 livetv->backend_info->port );
229 gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
231 res = gmyth_monitor_handler_start ( livetv->monitor );
235 gmyth_debug("MythTV Monitor event socket connected and listening!");
236 g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
237 (GCallback)gmyth_livetv_monitor_signal_handler,
242 gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
253 gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
256 if ( livetv->monitor != NULL )
258 g_object_unref( livetv->monitor );
259 livetv->monitor = NULL;
267 gmyth_livetv_create_remote_url( GMythLiveTV *livetv )
269 gchar *uri = g_strdup("");
270 gmyth_backend_info_get_remote_h
272 //gmyth_backend(livetv->backend_info)
279 gmyth_livetv_setup_recorder_channel_name ( GMythLiveTV *livetv, gchar* channel, GMythBackendInfo *backend_info )
283 GMythSocket *socket = gmyth_socket_new ();
285 livetv->backend_info = backend_info;
287 g_static_mutex_lock( &lock );
289 // FIME: Implement this at gmyth_socket
290 res = gmyth_socket_connect_to_backend (socket, livetv->backend_info->hostname,
291 livetv->backend_info->port, TRUE);
293 g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);
298 livetv->is_livetv = TRUE;
300 livetv->local_hostname = gmyth_socket_get_local_hostname ();
302 if ( livetv->local_hostname == NULL ) {
307 // Gets the recorder num
308 livetv->recorder = remote_request_next_free_recorder (socket, -1);
309 gmyth_socket_close_connection (socket);
311 if ( livetv->recorder == NULL ) {
312 g_warning ("[%s] None remote encoder available", __FUNCTION__);
317 // Init remote encoder. Opens its control socket.
318 res = gmyth_recorder_setup(livetv->recorder);
320 g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
325 // Creates livetv chain handler
326 livetv->tvchain = GMYTH_TVCHAIN ( g_object_new(GMYTH_TVCHAIN_TYPE, NULL) );
327 gmyth_tvchain_initialize ( livetv->tvchain, livetv->backend_info );
329 if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
334 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
335 res = gmyth_recorder_spawntv ( livetv->recorder,
336 gmyth_tvchain_get_id(livetv->tvchain) );
338 g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
344 /* loop finished, set the max tries variable to zero again... */
345 gint wait_to_transfer = 0;
347 while (wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
348 (gmyth_recorder_is_recording (livetv->recorder) == FALSE))
351 /* IS_RECORDING again, just like the MythTV backend does... */
352 gmyth_recorder_is_recording (livetv->recorder);
354 if ( channel != NULL )
356 /* Pauses remote encoder. */
357 res = gmyth_recorder_pause_recording(livetv->recorder);
359 g_warning ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
364 if ( gmyth_recorder_check_channel_name( livetv->recorder, channel ) )
366 if ( gmyth_recorder_set_channel_name( livetv->recorder, channel ) )
368 g_print( "[%s] Channel changed!!! [%s].\n", __FUNCTION__, channel );
374 sleep (9); /* FIXME: this is evil (tpm) */
378 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
380 if ( NULL == prog_info )
382 gmyth_debug( "ProgramInfo is equals to NULL!!!" );
386 /* prints program info data text */
387 gmyth_debug( "New ProgramInfo...\n" );
388 gmyth_program_info_print( prog_info );
390 gmyth_debug( "Old ProgramInfo...\n" );
391 gmyth_program_info_print( livetv->proginfo );
393 /* check if the program chain could be obtained from the MythTV protocol message */
394 if ( prog_info != NULL )
396 livetv->proginfo = prog_info;
397 /* testing change channel */
398 //gmyth_recorder_spawntv_no_tvchain( livetv->recorder );
401 /* check for the program info in the TV program chain could be obtained
402 from the MythTV MySQL database */
404 /* Reload all TV chain from Mysql database. */
405 gmyth_tvchain_reload_all (livetv->tvchain);
407 if ( livetv->tvchain == NULL ) {
412 /* Get program info from database using chanid and starttime */
413 livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
414 if ( livetv->proginfo == NULL ) {
415 g_warning ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
420 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
425 livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( backend_info );
427 g_static_mutex_unlock( &lock );
429 if ( !gmyth_livetv_monitor_handler_start( livetv ) )
432 gmyth_debug( "LiveTV MONITOR handler error on setup!" );
436 livetv->setup_done = TRUE;
441 g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
443 if ( livetv->local_hostname != NULL ) {
444 g_string_free( livetv->local_hostname, FALSE );
448 if ( livetv->recorder != NULL ) {
449 g_object_unref (livetv->recorder);
450 livetv->recorder = NULL;
453 if ( livetv->tvchain != NULL ) {
454 g_object_unref (livetv->tvchain);
455 livetv->tvchain = NULL;
458 if ( livetv->proginfo != NULL ) {
459 g_object_unref (livetv->proginfo);
460 livetv->proginfo = NULL;
463 if ( livetv->monitor != NULL ) {
464 g_object_unref (livetv->monitor);
465 livetv->monitor = NULL;
473 gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
475 return gmyth_livetv_setup_recorder_channel_name ( livetv, ( channel != -1 ) ?
476 g_strdup_printf( "%d", channel ) : NULL, backend_info );
480 gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
482 return gmyth_livetv_setup_recorder ( livetv, channel, backend_info );
486 gmyth_livetv_channel_name_setup ( GMythLiveTV *livetv, gchar* channel, GMythBackendInfo *backend_info )
488 return gmyth_livetv_setup_recorder_channel_name ( livetv, channel, backend_info );
492 gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
494 return gmyth_livetv_setup_recorder ( livetv, -1, backend_info );
498 gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
501 GMythProgramInfo *prog_info = NULL;
503 if ( !livetv->setup_done )
505 gmyth_debug ( "Call the setup function first!" );
510 //if ( !gmyth_livetv_monitor_handler_start( livetv ) )
512 prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
514 if ( NULL == prog_info )
516 gmyth_debug( "ProgramInfo is equals to NULL!!!" );
520 /* prints program info data text */
521 gmyth_program_info_print( prog_info );
523 if ( prog_info != NULL ) {
525 livetv->proginfo = prog_info;
526 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK, program info changed.");
528 g_warning ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
533 livetv->setup_done = TRUE;
538 g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
540 if ( livetv->local_hostname != NULL ) {
541 g_string_free( livetv->local_hostname, FALSE );
545 if ( livetv->recorder != NULL ) {
546 g_object_unref (livetv->recorder);
547 livetv->recorder = NULL;
550 if ( livetv->tvchain != NULL ) {
551 g_object_unref (livetv->tvchain);
552 livetv->tvchain = NULL;
555 if ( livetv->proginfo != NULL ) {
556 g_object_unref (livetv->proginfo);
557 livetv->proginfo = NULL;
565 gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
567 //GMythURI* uri = NULL;
569 if ( NULL == livetv )
572 if ( !livetv->setup_done )
574 gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
578 if ( livetv->proginfo != NULL )
579 gmyth_debug( "URI path = %s.\n", livetv->proginfo->pathname->str );
581 gmyth_debug( "URI path = %s.\n", livetv->uri->uri->str );
583 g_static_mutex_lock( &lock );
585 if ( livetv->file_transfer != NULL )
587 /*gmyth_file_transfer_close( livetv->file_transfer );*/
588 g_object_unref( livetv->file_transfer );
589 livetv->file_transfer = NULL;
592 livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
594 if ( NULL == livetv->file_transfer )
596 gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
600 if ( livetv->uri != NULL )
602 if ( livetv->uri->path != NULL )
604 g_string_free( livetv->uri->path, FALSE );
605 livetv->uri->path = NULL;
607 livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
609 livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
612 if ( NULL == livetv->uri )
614 gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
618 if ( !gmyth_file_transfer_open( livetv->file_transfer, livetv->uri != NULL ? gmyth_uri_get_path(livetv->uri) :
619 livetv->proginfo->pathname->str ) )
621 gmyth_debug( "Error: couldn't open the FileTransfer from LiveTV source!" );
622 g_object_unref( livetv->file_transfer );
623 livetv->file_transfer = NULL;
627 g_static_mutex_unlock( &lock );
633 g_object_unref( uri );
638 return livetv->file_transfer;
642 /* FIXME: How to proceed differently between livetv and recorded content */
644 gmyth_livetv_stop_playing (GMythLiveTV *livetv)
646 gmyth_debug ("Stopping the LiveTV...\n");
648 if (livetv->is_livetv) {
649 if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
650 g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);
656 gmyth_livetv_is_playing (GMythLiveTV *livetv)
662 gmyth_livetv_start_playing (GMythLiveTV *livetv)