[svn r495] Some more code clean-ups.
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 #include "gmyth_common.h"
44 static void gmyth_livetv_class_init (GMythLiveTVClass *klass);
45 static void gmyth_livetv_init (GMythLiveTV *object);
47 static void gmyth_livetv_dispose (GObject *object);
48 static void gmyth_livetv_finalize (GObject *object);
50 static gint tvchain_curr_index = -1;
52 /*static GStaticMutex lock = G_STATIC_MUTEX_INIT;*/
54 #define GMYTHTV_TRANSFER_MAX_WAITS 100
56 G_DEFINE_TYPE(GMythLiveTV, gmyth_livetv, G_TYPE_OBJECT)
59 gmyth_livetv_class_init (GMythLiveTVClass *klass)
61 GObjectClass *gobject_class;
63 gobject_class = (GObjectClass *) klass;
65 gobject_class->dispose = gmyth_livetv_dispose;
66 gobject_class->finalize = gmyth_livetv_finalize;
70 gmyth_livetv_init (GMythLiveTV *livetv)
72 livetv->monitor = NULL;
73 livetv->backend_info = NULL;
74 livetv->local_hostname = NULL;
75 livetv->file_transfer = NULL;
76 livetv->setup_done = FALSE;
78 livetv->socket = NULL;
79 livetv->recorder = NULL;
80 livetv->tvchain = NULL;
81 livetv->proginfo = NULL;
84 livetv->mutex = g_mutex_new();
88 gmyth_livetv_dispose (GObject *object)
90 GMythLiveTV *livetv = GMYTH_LIVETV (object);
92 if (livetv->disposed) {
93 /* If dispose did already run, return. */
97 /* Make sure dispose does not run twice. */
98 livetv->disposed = TRUE;
100 if ( livetv->monitor != NULL ) {
101 g_object_unref (livetv->monitor);
102 livetv->monitor = NULL;
105 if ( livetv->recorder != NULL ) {
106 //gmyth_recorder_close(livetv->recorder);
107 g_object_unref (livetv->recorder);
108 livetv->recorder = NULL;
111 if ( livetv->socket != NULL ) {
112 g_object_unref (livetv->socket);
113 livetv->socket = NULL;
116 if ( livetv->tvchain != NULL ) {
117 g_object_unref (livetv->tvchain);
118 livetv->tvchain = NULL;
121 if ( livetv->proginfo != NULL ) {
122 g_object_unref (livetv->proginfo);
123 livetv->proginfo = NULL;
126 if ( livetv->file_transfer != NULL ) {
127 g_object_unref (livetv->file_transfer);
128 livetv->file_transfer = NULL;
131 if ( livetv->backend_info != NULL ) {
132 g_object_unref (livetv->backend_info);
133 livetv->backend_info = NULL;
136 if ( livetv->uri != NULL ) {
137 g_object_unref (livetv->uri);
141 if ( livetv->mutex != NULL ) {
142 g_mutex_free (livetv->mutex);
143 livetv->mutex = NULL;
146 if ( livetv->local_hostname != NULL ) {
147 g_string_free (livetv->local_hostname, TRUE);
148 livetv->local_hostname = NULL;
151 G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
155 gmyth_livetv_finalize (GObject *object)
157 g_signal_handlers_destroy (object);
159 G_OBJECT_CLASS ( gmyth_livetv_parent_class )->finalize ( object );
163 * Creates a new GMythLiveTV instance
165 * @return a newly allocated GMythLiveTV instance
168 gmyth_livetv_new (GMythBackendInfo *backend_info)
170 GMythLiveTV *livetv = GMYTH_LIVETV ( g_object_new( GMYTH_LIVETV_TYPE, NULL ) );
172 livetv->backend_info = backend_info;
173 g_object_ref (livetv->backend_info);
179 * The GObject signal handler function, from which all status messages
180 * from the Monitor Handler will be advertized, all time it receives
181 * LiveTV status messages from the MythTV backend
183 * @param monitor a GMythMonitorHandler instance
184 * @param msg_code the MythTV's server numeric status code
185 * @param message the message's string description
186 * @param user_data pointer to the GMythLiveTV instance
189 gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code,
190 gchar* message, gpointer user_data )
192 GMythLiveTV *live_tv = GMYTH_LIVETV ( user_data );
194 gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, live_tv param = %s, user_data = %s )\n", message, msg_code, live_tv != NULL ? "" :
195 "NULL", user_data != NULL ? "" : "NULL" );
197 if ( NULL == live_tv )
199 gmyth_debug( "LiveTV_obj is equals to NULL!!!" );
206 case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
208 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
209 "TV Chain ID is the same as the old one...\n", message );
210 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
211 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
212 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
213 /* advertises the FileTransfer about the program info changed */
214 if ( live_tv->file_transfer != NULL )
216 gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
218 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
219 msg_code, (gpointer)(live_tv->recorder) );
221 /* gmyth_livetv_monitor_handler_stop( live_tv ); */
223 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
227 case GMYTH_BACKEND_DONE_RECORDING:
229 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
230 "TV Chain ID is the same as the old one...\n", message );
231 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
232 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
233 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
234 /* advertises the FileTransfer about the program info changed */
235 if ( live_tv->file_transfer != NULL )
237 gmyth_debug( "Emitting signal to the FileTransfer... [ \"backend-done-recording\" ]" );
239 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
240 msg_code, (gpointer)(live_tv->recorder) );
243 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
247 case GMYTH_BACKEND_STOP_LIVETV:
249 gmyth_debug( "LIVETV Stop LiveTV request received [ msg = %s ]. Going out the "\
250 "LiveTV...\n", message );
251 /* stops the LiveTV */
252 if ( live_tv != NULL )
254 gmyth_debug( "Going out the LiveTV... [ \"quit-livetv\" ]" );
256 g_object_unref( live_tv );
258 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
264 } /* switch (Monitor Handler messages) */
269 * Starts the Monitor Handler to this GMythLiveTV session, in order
270 * to receive the status messages from the MythTV's backend server
272 * @param live_tv the GMythLiveTV instance
274 * @return <code>true</code> if the Monitor Handler start-up process
275 * had been concluded succcesfully
278 gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
282 if ( livetv->monitor != NULL )
284 g_object_unref( livetv->monitor );
285 livetv->monitor = NULL;
288 livetv->monitor = gmyth_monitor_handler_new ( );
290 res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname,
291 livetv->backend_info->port );
295 gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
297 res = gmyth_monitor_handler_start ( livetv->monitor );
301 gmyth_debug("MythTV Monitor event socket connected and listening!");
302 g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
303 (GCallback)gmyth_livetv_monitor_signal_handler,
308 gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
319 * Stops the Monitor Handler to this GMythLiveTV session, in order
320 * to stop receiving the status messages from the MythTV's backend server
322 * @param live_tv the GMythLiveTV instance
324 * @return <code>true</code> if the Monitor Handler shutdown process
325 * had been concluded succcesfully
328 gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
331 if ( livetv->monitor != NULL )
333 g_object_unref( livetv->monitor );
334 livetv->monitor = NULL;
341 gmyth_livetv_create_remote_url( GMythLiveTV *livetv )
343 gchar *uri = g_strdup("");
344 gmyth_backend_info_get_remote_h
346 //gmyth_backend(livetv->backend_info)
353 * Configures the GMythLiveTV session, sends SPAWN_LIVETV message,
354 * sets the channel name, and gets the first program info about the
357 * @param live_tv the GMythLiveTV instance
358 * @param channel the channel name (the chan_name field, from the tvchain table)
359 * @param backend_info the GMythBackendInfo describing the remote server
361 * @return <code>true</code> if the LiveTV's recorder instance configuration
362 * had been concluded succcesfully
365 gmyth_livetv_setup_recorder_channel_name (GMythLiveTV *livetv, gchar* channel)
369 g_return_val_if_fail (livetv != NULL, FALSE);
371 if ( NULL == livetv->socket ) {
372 livetv->socket = gmyth_socket_new ();
374 /* FIME: Implement this at gmyth_socket */
375 res = gmyth_socket_connect_to_backend (livetv->socket, livetv->backend_info->hostname,
376 livetv->backend_info->port, TRUE);
378 gmyth_debug ("[%s] LiveTV can not connect to backend", __FUNCTION__);
384 g_mutex_lock( livetv->mutex );
386 livetv->is_livetv = TRUE;
388 livetv->local_hostname = gmyth_socket_get_local_hostname ();
390 if ( livetv->local_hostname == NULL ) {
395 if ( livetv->recorder != NULL )
397 g_object_unref( livetv->recorder );
398 livetv->recorder= NULL;
401 if ( gmyth_remote_util_get_free_recorder_count (livetv->socket) <= 0 ) {
403 #ifdef GMYTH_USE_DEBUG
404 gmyth_debug ("No free remote encoder available.");
410 /* Gets the recorder num */
411 livetv->recorder = remote_request_next_free_recorder (livetv->socket, -1);
412 gmyth_socket_close_connection (livetv->socket);
414 if ( NULL == livetv->recorder ) {
415 gmyth_debug ("[%s] None remote encoder available", __FUNCTION__);
420 /* Init remote encoder. Opens its control socket. */
421 res = gmyth_recorder_setup(livetv->recorder);
423 gmyth_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
428 /* Creates livetv chain handler */
429 livetv->tvchain = gmyth_tvchain_new();
430 gmyth_tvchain_initialize ( livetv->tvchain, livetv->backend_info );
432 if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
437 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
438 res = gmyth_recorder_spawntv ( livetv->recorder,
439 gmyth_tvchain_get_id(livetv->tvchain) );
441 gmyth_debug ("[%s] Fail while spawn tv\n", __FUNCTION__);
447 /* loop finished, set the max tries variable to zero again... */
448 gint wait_to_transfer = 0;
450 while (wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
451 (gmyth_recorder_is_recording (livetv->recorder) == FALSE))
454 if ( channel != NULL )
456 /* Pauses remote encoder. */
457 res = gmyth_recorder_pause_recording(livetv->recorder);
459 gmyth_debug ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
464 if ( gmyth_recorder_check_channel_name( livetv->recorder, channel ) )
466 if ( gmyth_recorder_set_channel_name( livetv->recorder, channel ) )
468 gmyth_debug( "Channel changed!!! [%s].\n", channel );
472 } /* if - changes the channel number */
473 sleep (4); /* FIXME: this is evil (tpm) */
477 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
479 if ( NULL == prog_info )
481 gmyth_debug( "ProgramInfo is equals to NULL!!!" );
484 gchar *channame = NULL;
486 gmyth_debug( "Problem getting current proginfo!\n" );
489 * mythbackend must not be tuned in to a channel, so keep
490 * changing channels until we find a valid one, or until
491 * we decide to give up.
493 for (i=1; i<1000; i++) {
494 if ( channame != NULL )
496 channame = g_strdup_printf( "%d", i );
497 if (gmyth_recorder_set_channel_name(livetv->recorder, channame) < 0) {
500 prog_info = gmyth_recorder_get_next_program_info(livetv->recorder, BROWSE_DIRECTION_UP);
501 if (prog_info != NULL)
505 } /* if - Program Info */
507 /* prints program info data text */
508 #ifdef GMYTH_USE_DEBUG
509 gmyth_debug( "New ProgramInfo...\n" );
510 gmyth_program_info_print( prog_info );
513 /* check if the program chain could be obtained from the MythTV protocol message */
514 if ( prog_info != NULL )
516 #ifdef GMYTH_USE_DEBUG
517 gmyth_debug( "Program Info: %s\n", gmyth_program_info_to_string( prog_info ) );
519 livetv->proginfo = prog_info;
520 /* testing change channel */
521 //gmyth_recorder_spawntv_no_tvchain( livetv->recorder );
524 /* check for the program info in the TV program chain could be obtained
525 from the MythTV MySQL database */
527 /* Reload all TV chain from Mysql database. */
528 gmyth_tvchain_reload_all (livetv->tvchain);
530 if ( livetv->tvchain == NULL ) {
535 /* Get program info from database using chanid and starttime */
536 livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
537 if ( livetv->proginfo == NULL ) {
538 gmyth_debug ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
543 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
548 livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( livetv->backend_info );
550 g_mutex_unlock( livetv->mutex );
552 if ( !gmyth_livetv_monitor_handler_start( livetv ) )
555 gmyth_debug( "LiveTV MONITOR handler error on setup!" );
559 livetv->setup_done = TRUE;
564 gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
568 g_string_free( livetv->local_hostname, TRUE );
570 if ( livetv->recorder != NULL ) {
571 g_object_unref (livetv->recorder);
572 livetv->recorder = NULL;
575 if ( livetv->tvchain != NULL ) {
576 g_object_unref (livetv->tvchain);
577 livetv->tvchain = NULL;
580 if ( livetv->proginfo != NULL ) {
581 g_object_unref (livetv->proginfo);
582 livetv->proginfo = NULL;
585 if ( livetv->monitor != NULL ) {
586 g_object_unref (livetv->monitor);
587 livetv->monitor = NULL;
595 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
596 * sets the channel name, and gets the first program info about the
599 * @param live_tv the GMythLiveTV instance
600 * @param channel the channel name, in numerical format
601 * @param backend_info the GMythBackendInfo describing the remote server
603 * @return <code>true</code> if the LiveTV's recorder instance configuration
604 * had been concluded succcesfully
607 gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel)
609 return gmyth_livetv_setup_recorder_channel_name ( livetv, ( channel != -1 ) ?
610 g_strdup_printf( "%d", channel ) : NULL );
614 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
615 * sets the channel name (numerical format), and gets the first program info about the
618 * @param live_tv the GMythLiveTV instance
619 * @param channel the channel name, in numerical format
620 * @param backend_info the GMythBackendInfo describing the remote server
622 * @return <code>true</code> if the LiveTV's recorder instance configuration
623 * had been concluded succcesfully
626 gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel )
628 return gmyth_livetv_setup_recorder ( livetv, channel );
632 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
633 * sets the channel name (string format), and gets the first program info about the
636 * @param live_tv the GMythLiveTV instance
637 * @param channel the channel name, in numerical format
638 * @param backend_info the GMythBackendInfo describing the remote server
640 * @return <code>true</code> if the LiveTV's recorder instance configuration
641 * had been concluded succcesfully
644 gmyth_livetv_channel_name_setup ( GMythLiveTV *livetv, gchar* channel )
646 return gmyth_livetv_setup_recorder_channel_name ( livetv, channel );
650 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
651 * and gets the first program info about the actual recording
652 * (doesn't changes the channel).
654 * @param live_tv the GMythLiveTV instance
655 * @param backend_info the GMythBackendInfo describing the remote server
657 * @return <code>true</code> if the LiveTV's recorder instance configuration
658 * had been concluded succcesfully
661 gmyth_livetv_setup ( GMythLiveTV *livetv )
663 return gmyth_livetv_setup_recorder ( livetv, -1 );
667 * Gets the next program info from this GMythLiveTV session.
669 * @param live_tv the GMythLiveTV instance
671 * @return <code>true</code> if the next program info could be got
674 gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
677 GMythProgramInfo *prog_info = NULL;
679 if ( !livetv->setup_done )
681 gmyth_debug ( "Call the setup function first!" );
685 gmyth_debug( "Current ProgramInfo...\n" );
686 prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
688 if ( prog_info != NULL )
690 livetv->proginfo = prog_info;
692 gmyth_debug( "ProgramInfo equals to NULL!!! Getting the next program info..." );
693 prog_info = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_RIGHT );
694 livetv->proginfo = prog_info;
696 /* prints program info data text */
697 #ifdef GMYTH_USE_DEBUG
698 gmyth_program_info_print( prog_info );
701 if ( prog_info != NULL ) {
703 livetv->proginfo = prog_info;
704 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK, program info changed.");
706 gmyth_debug ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
710 livetv->setup_done = TRUE;
715 gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
719 g_string_free( livetv->local_hostname, TRUE );
721 if ( livetv->recorder != NULL ) {
722 g_object_unref (livetv->recorder);
723 livetv->recorder = NULL;
726 if ( livetv->tvchain != NULL ) {
727 g_object_unref (livetv->tvchain);
728 livetv->tvchain = NULL;
731 if ( livetv->proginfo != NULL ) {
732 g_object_unref (livetv->proginfo);
733 livetv->proginfo = NULL;
740 * Creates a File Transfer session, using all configuration information
741 * got from the actual program info.
743 * @param live_tv the GMythLiveTV instance
745 * @return the actual GMythFileTransfer instance, generated using the
746 * data got from the actual program info.
749 gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
751 //GMythURI* uri = NULL;
753 if ( NULL == livetv )
756 if ( !livetv->setup_done )
758 gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
762 #ifdef GMYTH_USE_DEBUG
763 if ( livetv->proginfo != NULL )
764 gmyth_debug( "URI path (from program info) = %s.\n", livetv->proginfo->pathname->str );
766 gmyth_debug( "URI path (from URI) = %s.\n", livetv->uri->uri->str );
769 g_mutex_lock( livetv->mutex );
771 if ( livetv->file_transfer != NULL )
773 /*gmyth_file_transfer_close( livetv->file_transfer );*/
774 g_object_unref( livetv->file_transfer );
775 livetv->file_transfer = NULL;
778 if ( livetv->uri != NULL )
781 if ( livetv->uri->path != NULL )
783 g_string_free( livetv->uri->path, FALSE );
784 livetv->uri->path = NULL;
787 #ifdef GMYTH_USE_DEBUG
788 gmyth_debug( "URI is not NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
790 livetv->uri->path = g_string_erase(livetv->uri->path, 0, -1);
791 livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
793 #ifdef GMYTH_USE_DEBUG
794 gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
796 livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
799 if ( NULL == livetv->uri )
801 gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
805 livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
807 if ( NULL == livetv->file_transfer )
809 gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
813 g_object_ref( livetv->file_transfer );
815 /* gmyth_file_transfer_settimeout( livetv->file_transfer, TRUE ); */
817 g_mutex_unlock( livetv->mutex );
823 g_object_unref( uri );
828 return livetv->file_transfer;
833 * Stops this LiveTV session.
835 * @param live_tv the GMythLiveTV instance
838 gmyth_livetv_stop_playing (GMythLiveTV *livetv)
840 gmyth_debug ("Stopping the LiveTV...\n");
842 if (livetv->is_livetv) {
843 if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
844 gmyth_debug ("[%s] Error while stoping remote encoder", __FUNCTION__);
847 if ( !gmyth_recorder_finish_recording(livetv->recorder) ) {
848 gmyth_debug ("[%s] Error while finishing recording on remote encoder", __FUNCTION__);
854 gmyth_livetv_is_playing (GMythLiveTV *livetv)
860 gmyth_livetv_start_playing (GMythLiveTV *livetv)