[svn r323] Some updates in the LiveTV.
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!!!" );
385 gchar *channame = NULL;
387 fprintf(stderr, "problem getting current proginfo!\n");
390 * mythbackend must not be tuned in to a channel, so keep
391 * changing channels until we find a valid one, or until
392 * we decide to give up.
394 for (i=1; i<1000; i++) {
395 if ( channame != NULL )
397 channame = g_strdup_printf( "%d", i );
398 if (gmyth_recorder_set_channel_name(livetv->recorder, channame) < 0) {
401 prog_info = gmyth_recorder_get_next_program_info(livetv->recorder, BROWSE_DIRECTION_UP);
402 if (prog_info != NULL)
407 /* prints program info data text */
408 gmyth_debug( "New ProgramInfo...\n" );
409 gmyth_program_info_print( prog_info );
411 gmyth_debug( "Old ProgramInfo...\n" );
412 gmyth_program_info_print( livetv->proginfo );
414 GMythProgramInfo* prog_inf = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_UP );
416 gmyth_debug( "Next ProgramInfo...\n" );
417 gmyth_program_info_print( prog_inf );
419 /* check if the program chain could be obtained from the MythTV protocol message */
420 if ( prog_info != NULL )
422 livetv->proginfo = prog_info;
423 /* testing change channel */
424 //gmyth_recorder_spawntv_no_tvchain( livetv->recorder );
427 /* check for the program info in the TV program chain could be obtained
428 from the MythTV MySQL database */
430 /* Reload all TV chain from Mysql database. */
431 gmyth_tvchain_reload_all (livetv->tvchain);
433 if ( livetv->tvchain == NULL ) {
438 /* Get program info from database using chanid and starttime */
439 livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
440 if ( livetv->proginfo == NULL ) {
441 g_warning ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
446 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
451 livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( backend_info );
453 g_static_mutex_unlock( &lock );
455 if ( !gmyth_livetv_monitor_handler_start( livetv ) )
458 gmyth_debug( "LiveTV MONITOR handler error on setup!" );
462 livetv->setup_done = TRUE;
467 g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
469 if ( livetv->local_hostname != NULL ) {
470 g_string_free( livetv->local_hostname, FALSE );
474 if ( livetv->recorder != NULL ) {
475 g_object_unref (livetv->recorder);
476 livetv->recorder = NULL;
479 if ( livetv->tvchain != NULL ) {
480 g_object_unref (livetv->tvchain);
481 livetv->tvchain = NULL;
484 if ( livetv->proginfo != NULL ) {
485 g_object_unref (livetv->proginfo);
486 livetv->proginfo = NULL;
489 if ( livetv->monitor != NULL ) {
490 g_object_unref (livetv->monitor);
491 livetv->monitor = NULL;
499 gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
501 return gmyth_livetv_setup_recorder_channel_name ( livetv, ( channel != -1 ) ?
502 g_strdup_printf( "%d", channel ) : NULL, backend_info );
506 gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel, GMythBackendInfo *backend_info )
508 return gmyth_livetv_setup_recorder ( livetv, channel, backend_info );
512 gmyth_livetv_channel_name_setup ( GMythLiveTV *livetv, gchar* channel, GMythBackendInfo *backend_info )
514 return gmyth_livetv_setup_recorder_channel_name ( livetv, channel, backend_info );
518 gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
520 return gmyth_livetv_setup_recorder ( livetv, -1, backend_info );
524 gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
527 GMythProgramInfo *prog_info = NULL;
529 if ( !livetv->setup_done )
531 gmyth_debug ( "Call the setup function first!" );
536 prog_info = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_RIGHT );
538 gmyth_debug( "Next ProgramInfo...\n" );
540 if ( NULL == prog_info )
542 gmyth_debug( "ProgramInfo is equals to NULL!!!" );
546 /* prints program info data text */
547 gmyth_program_info_print( prog_info );
549 if ( prog_info != NULL ) {
551 livetv->proginfo = prog_info;
552 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK, program info changed.");
554 g_warning ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
559 livetv->setup_done = TRUE;
564 g_print( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
566 if ( livetv->local_hostname != NULL ) {
567 g_string_free( livetv->local_hostname, FALSE );
571 if ( livetv->recorder != NULL ) {
572 g_object_unref (livetv->recorder);
573 livetv->recorder = NULL;
576 if ( livetv->tvchain != NULL ) {
577 g_object_unref (livetv->tvchain);
578 livetv->tvchain = NULL;
581 if ( livetv->proginfo != NULL ) {
582 g_object_unref (livetv->proginfo);
583 livetv->proginfo = NULL;
591 gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
593 //GMythURI* uri = NULL;
595 if ( NULL == livetv )
598 if ( !livetv->setup_done )
600 gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
604 if ( livetv->proginfo != NULL )
605 gmyth_debug( "URI path (from program info) = %s.\n", livetv->proginfo->pathname->str );
607 gmyth_debug( "URI path (from URI) = %s.\n", livetv->uri->uri->str );
609 g_static_mutex_lock( &lock );
611 if ( livetv->file_transfer != NULL )
613 /*gmyth_file_transfer_close( livetv->file_transfer );*/
614 g_object_unref( livetv->file_transfer );
615 livetv->file_transfer = NULL;
618 if ( livetv->uri != NULL )
621 if ( livetv->uri->path != NULL )
623 g_string_free( livetv->uri->path, FALSE );
624 livetv->uri->path = NULL;
627 gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
628 livetv->uri->path = g_string_erase(livetv->uri->path, 0, -1);
629 livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
631 gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
632 livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
635 if ( NULL == livetv->uri )
637 gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
641 livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
643 if ( NULL == livetv->file_transfer )
645 gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
649 if ( !gmyth_file_transfer_open( livetv->file_transfer, livetv->uri != NULL ? gmyth_uri_get_path(livetv->uri) :
650 livetv->proginfo->pathname->str ) )
652 gmyth_debug( "Error: couldn't open the FileTransfer from LiveTV source!" );
653 g_object_unref( livetv->file_transfer );
654 livetv->file_transfer = NULL;
658 g_static_mutex_unlock( &lock );
664 g_object_unref( uri );
669 return livetv->file_transfer;
673 /* FIXME: How to proceed differently between livetv and recorded content */
675 gmyth_livetv_stop_playing (GMythLiveTV *livetv)
677 gmyth_debug ("Stopping the LiveTV...\n");
679 if (livetv->is_livetv) {
680 if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
681 g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);
687 gmyth_livetv_is_playing (GMythLiveTV *livetv)
693 gmyth_livetv_start_playing (GMythLiveTV *livetv)