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 g_object_unref (livetv->recorder);
107 livetv->recorder = NULL;
110 if ( livetv->socket != NULL ) {
111 g_object_unref (livetv->socket);
112 livetv->socket = NULL;
115 if ( livetv->tvchain != NULL ) {
116 g_object_unref (livetv->tvchain);
117 livetv->tvchain = NULL;
120 if ( livetv->proginfo != NULL ) {
121 g_object_unref (livetv->proginfo);
122 livetv->proginfo = NULL;
125 if ( livetv->file_transfer != NULL ) {
126 g_object_unref (livetv->file_transfer);
127 livetv->file_transfer = NULL;
130 if ( livetv->backend_info != NULL ) {
131 g_object_unref (livetv->backend_info);
132 livetv->backend_info = NULL;
135 if ( livetv->uri != NULL ) {
136 g_object_unref (livetv->uri);
140 if ( livetv->mutex != NULL ) {
141 g_mutex_free (livetv->mutex);
142 livetv->mutex = NULL;
145 if ( livetv->local_hostname != NULL ) {
146 g_string_free (livetv->local_hostname, TRUE);
147 livetv->local_hostname = NULL;
150 G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
154 gmyth_livetv_finalize (GObject *object)
156 g_signal_handlers_destroy (object);
158 G_OBJECT_CLASS ( gmyth_livetv_parent_class )->finalize ( object );
162 * Creates a new GMythLiveTV instance
164 * @return a newly allocated GMythLiveTV instance
167 gmyth_livetv_new (GMythBackendInfo *backend_info)
169 GMythLiveTV *livetv = GMYTH_LIVETV ( g_object_new( GMYTH_LIVETV_TYPE, NULL ) );
171 livetv->backend_info = backend_info;
172 g_object_ref (livetv->backend_info);
178 * The GObject signal handler function, from which all status messages
179 * from the Monitor Handler will be advertized, all time it receives
180 * LiveTV status messages from the MythTV backend
182 * @param monitor a GMythMonitorHandler instance
183 * @param msg_code the MythTV's server numeric status code
184 * @param message the message's string description
185 * @param user_data pointer to the GMythLiveTV instance
188 gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code,
189 gchar* message, gpointer user_data )
191 GMythLiveTV *live_tv = GMYTH_LIVETV ( user_data );
193 gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, live_tv param = %s, user_data = %s )\n", message, msg_code, live_tv != NULL ? "" :
194 "NULL", user_data != NULL ? "" : "NULL" );
196 if ( NULL == live_tv )
198 gmyth_debug( "LiveTV_obj is equals to NULL!!!" );
205 case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
207 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
208 "TV Chain ID is the same as the old one...\n", message );
209 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
210 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
211 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
212 /* advertises the FileTransfer about the program info changed */
213 if ( live_tv->file_transfer != NULL )
215 gmyth_debug( "Emitting signal to the FileTransfer... [ \"program-info-changed \" ]" );
217 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
218 msg_code, (gpointer)(live_tv->recorder) );
220 /* gmyth_livetv_monitor_handler_stop( live_tv ); */
222 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
226 case GMYTH_BACKEND_DONE_RECORDING:
228 gmyth_debug( "LIVETV Program Changed request received [ msg = %s ]. Watching if the new "\
229 "TV Chain ID is the same as the old one...\n", message );
230 if ( g_ascii_strcasecmp ( message, (gmyth_tvchain_get_id( live_tv->tvchain ))->str ) != 0 ) {
231 gmyth_debug( "OK!!! MOVED to the next program chain [actual == %s]!",
232 (gmyth_tvchain_get_id( live_tv->tvchain ))->str );
233 /* advertises the FileTransfer about the program info changed */
234 if ( live_tv->file_transfer != NULL )
236 gmyth_debug( "Emitting signal to the FileTransfer... [ \"backend-done-recording\" ]" );
238 gmyth_file_transfer_emit_program_info_changed_signal( live_tv->file_transfer,
239 msg_code, (gpointer)(live_tv->recorder) );
242 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
246 case GMYTH_BACKEND_STOP_LIVETV:
248 gmyth_debug( "LIVETV Stop LiveTV request received [ msg = %s ]. Going out the "\
249 "LiveTV...\n", message );
250 /* stops the LiveTV */
251 if ( live_tv != NULL )
253 gmyth_debug( "Going out the LiveTV... [ \"quit-livetv\" ]" );
255 g_object_unref( live_tv );
257 gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
263 } /* switch (Monitor Handler messages) */
268 * Starts the Monitor Handler to this GMythLiveTV session, in order
269 * to receive the status messages from the MythTV's backend server
271 * @param live_tv the GMythLiveTV instance
273 * @return <code>true</code> if the Monitor Handler start-up process
274 * had been concluded succcesfully
277 gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
281 if ( livetv->monitor != NULL )
283 g_object_unref( livetv->monitor );
284 livetv->monitor = NULL;
287 livetv->monitor = gmyth_monitor_handler_new ( );
289 res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname,
290 livetv->backend_info->port );
294 gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
296 res = gmyth_monitor_handler_start ( livetv->monitor );
300 gmyth_debug("MythTV Monitor event socket connected and listening!");
301 g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
302 (GCallback)gmyth_livetv_monitor_signal_handler,
307 gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
318 * Stops the Monitor Handler to this GMythLiveTV session, in order
319 * to stop receiving the status messages from the MythTV's backend server
321 * @param live_tv the GMythLiveTV instance
323 * @return <code>true</code> if the Monitor Handler shutdown process
324 * had been concluded succcesfully
327 gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
330 if ( livetv->monitor != NULL )
332 g_object_unref( livetv->monitor );
333 livetv->monitor = NULL;
340 gmyth_livetv_create_remote_url( GMythLiveTV *livetv )
342 gchar *uri = g_strdup("");
343 gmyth_backend_info_get_remote_h
345 //gmyth_backend(livetv->backend_info)
352 * Configures the GMythLiveTV session, sends SPAWN_LIVETV message,
353 * sets the channel name, and gets the first program info about the
356 * @param live_tv the GMythLiveTV instance
357 * @param channel the channel name (the chan_name field, from the tvchain table)
358 * @param backend_info the GMythBackendInfo describing the remote server
360 * @return <code>true</code> if the LiveTV's recorder instance configuration
361 * had been concluded succcesfully
364 gmyth_livetv_setup_recorder_channel_name (GMythLiveTV *livetv, gchar* channel)
368 g_return_val_if_fail (livetv != NULL, FALSE);
370 if ( NULL == livetv->socket ) {
371 livetv->socket = gmyth_socket_new ();
373 /* FIME: Implement this at gmyth_socket */
374 res = gmyth_socket_connect_to_backend (livetv->socket, livetv->backend_info->hostname,
375 livetv->backend_info->port, TRUE);
377 gmyth_debug ("[%s] LiveTV can not connect to backend", __FUNCTION__);
383 g_mutex_lock( livetv->mutex );
385 livetv->is_livetv = TRUE;
387 livetv->local_hostname = gmyth_socket_get_local_hostname ();
389 if ( livetv->local_hostname == NULL ) {
394 if ( livetv->recorder != NULL )
396 g_object_unref( livetv->recorder );
397 livetv->recorder= NULL;
400 if ( gmyth_remote_util_get_free_recorder_count (livetv->socket) <= 0 ) {
402 #ifdef GMYTH_USE_DEBUG
403 gmyth_debug ("No free remote encoder available.");
409 /* Gets the recorder num */
410 livetv->recorder = remote_request_next_free_recorder (livetv->socket, -1);
411 gmyth_socket_close_connection (livetv->socket);
413 if ( NULL == livetv->recorder ) {
414 gmyth_debug ("[%s] None remote encoder available", __FUNCTION__);
419 /* Init remote encoder. Opens its control socket. */
420 res = gmyth_recorder_setup(livetv->recorder);
422 gmyth_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
427 /* Creates livetv chain handler */
428 livetv->tvchain = gmyth_tvchain_new();
429 gmyth_tvchain_initialize ( livetv->tvchain, livetv->backend_info );
431 if ( livetv->tvchain == NULL || livetv->tvchain->tvchain_id == NULL ) {
436 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
437 res = gmyth_recorder_spawntv ( livetv->recorder,
438 gmyth_tvchain_get_id(livetv->tvchain) );
440 gmyth_debug ("[%s] Fail while spawn tv\n", __FUNCTION__);
446 /* loop finished, set the max tries variable to zero again... */
447 gint wait_to_transfer = 0;
449 while (wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
450 (gmyth_recorder_is_recording (livetv->recorder) == FALSE))
453 if ( channel != NULL )
455 /* Pauses remote encoder. */
456 res = gmyth_recorder_pause_recording(livetv->recorder);
458 gmyth_debug ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
463 if ( gmyth_recorder_check_channel_name( livetv->recorder, channel ) )
465 if ( gmyth_recorder_set_channel_name( livetv->recorder, channel ) )
467 gmyth_debug( "Channel changed!!! [%s].\n", channel );
471 } /* if - changes the channel number */
472 sleep (4); /* FIXME: this is evil (tpm) */
476 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
478 if ( NULL == prog_info )
480 gmyth_debug( "ProgramInfo is equals to NULL!!!" );
483 gchar *channame = NULL;
485 gmyth_debug( "Problem getting current proginfo!\n" );
488 * mythbackend must not be tuned in to a channel, so keep
489 * changing channels until we find a valid one, or until
490 * we decide to give up.
492 for (i=1; i<1000; i++) {
493 if ( channame != NULL )
495 channame = g_strdup_printf( "%d", i );
496 if (gmyth_recorder_set_channel_name(livetv->recorder, channame) < 0) {
499 prog_info = gmyth_recorder_get_next_program_info(livetv->recorder, BROWSE_DIRECTION_UP);
500 if (prog_info != NULL)
504 } /* if - Program Info */
506 /* prints program info data text */
507 #ifdef GMYTH_USE_DEBUG
508 gmyth_debug( "New ProgramInfo...\n" );
509 gmyth_program_info_print( prog_info );
512 /* check if the program chain could be obtained from the MythTV protocol message */
513 if ( prog_info != NULL )
515 #ifdef GMYTH_USE_DEBUG
516 gmyth_debug( "Program Info: %s\n", gmyth_program_info_to_string( prog_info ) );
518 livetv->proginfo = prog_info;
519 /* testing change channel */
520 //gmyth_recorder_spawntv_no_tvchain( livetv->recorder );
523 /* check for the program info in the TV program chain could be obtained
524 from the MythTV MySQL database */
526 /* Reload all TV chain from Mysql database. */
527 gmyth_tvchain_reload_all (livetv->tvchain);
529 if ( livetv->tvchain == NULL ) {
534 /* Get program info from database using chanid and starttime */
535 livetv->proginfo = gmyth_tvchain_get_program_at (livetv->tvchain, tvchain_curr_index++ );
536 if ( livetv->proginfo == NULL ) {
537 gmyth_debug ("[%s] LiveTV not successfully started.\n", __FUNCTION__ );
542 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
547 livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( livetv->backend_info );
549 g_mutex_unlock( livetv->mutex );
551 if ( !gmyth_livetv_monitor_handler_start( livetv ) )
554 gmyth_debug( "LiveTV MONITOR handler error on setup!" );
558 livetv->setup_done = TRUE;
563 gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
567 g_string_free( livetv->local_hostname, TRUE );
569 if ( livetv->recorder != NULL ) {
570 g_object_unref (livetv->recorder);
571 livetv->recorder = NULL;
574 if ( livetv->tvchain != NULL ) {
575 g_object_unref (livetv->tvchain);
576 livetv->tvchain = NULL;
579 if ( livetv->proginfo != NULL ) {
580 g_object_unref (livetv->proginfo);
581 livetv->proginfo = NULL;
584 if ( livetv->monitor != NULL ) {
585 g_object_unref (livetv->monitor);
586 livetv->monitor = NULL;
594 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
595 * sets the channel name, and gets the first program info about the
598 * @param live_tv the GMythLiveTV instance
599 * @param channel the channel name, in numerical format
600 * @param backend_info the GMythBackendInfo describing the remote server
602 * @return <code>true</code> if the LiveTV's recorder instance configuration
603 * had been concluded succcesfully
606 gmyth_livetv_setup_recorder ( GMythLiveTV *livetv, gint channel)
608 return gmyth_livetv_setup_recorder_channel_name ( livetv, ( channel != -1 ) ?
609 g_strdup_printf( "%d", channel ) : NULL );
613 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
614 * sets the channel name (numerical format), and gets the first program info about the
617 * @param live_tv the GMythLiveTV instance
618 * @param channel the channel name, in numerical format
619 * @param backend_info the GMythBackendInfo describing the remote server
621 * @return <code>true</code> if the LiveTV's recorder instance configuration
622 * had been concluded succcesfully
625 gmyth_livetv_channel_setup ( GMythLiveTV *livetv, gint channel )
627 return gmyth_livetv_setup_recorder ( livetv, channel );
631 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
632 * sets the channel name (string format), and gets the first program info about the
635 * @param live_tv the GMythLiveTV instance
636 * @param channel the channel name, in numerical format
637 * @param backend_info the GMythBackendInfo describing the remote server
639 * @return <code>true</code> if the LiveTV's recorder instance configuration
640 * had been concluded succcesfully
643 gmyth_livetv_channel_name_setup ( GMythLiveTV *livetv, gchar* channel )
645 return gmyth_livetv_setup_recorder_channel_name ( livetv, channel );
649 * Setup the GMythLiveTV session, sends SPAWN_LIVETV message,
650 * and gets the first program info about the actual recording
651 * (doesn't changes the channel).
653 * @param live_tv the GMythLiveTV instance
654 * @param backend_info the GMythBackendInfo describing the remote server
656 * @return <code>true</code> if the LiveTV's recorder instance configuration
657 * had been concluded succcesfully
660 gmyth_livetv_setup ( GMythLiveTV *livetv )
662 return gmyth_livetv_setup_recorder ( livetv, -1 );
666 * Gets the next program info from this GMythLiveTV session.
668 * @param live_tv the GMythLiveTV instance
670 * @return <code>true</code> if the next program info could be got
673 gmyth_livetv_next_program_chain ( GMythLiveTV *livetv )
676 GMythProgramInfo *prog_info = NULL;
678 if ( !livetv->setup_done )
680 gmyth_debug ( "Call the setup function first!" );
684 gmyth_debug( "Current ProgramInfo...\n" );
685 prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
687 if ( prog_info != NULL )
689 livetv->proginfo = prog_info;
691 gmyth_debug( "ProgramInfo equals to NULL!!! Getting the next program info..." );
692 prog_info = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_RIGHT );
693 livetv->proginfo = prog_info;
695 /* prints program info data text */
696 #ifdef GMYTH_USE_DEBUG
697 gmyth_program_info_print( prog_info );
700 if ( prog_info != NULL ) {
702 livetv->proginfo = prog_info;
703 gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK, program info changed.");
705 gmyth_debug ("[%s] LiveTV not successfully started on the next program chain.\n", __FUNCTION__ );
709 livetv->setup_done = TRUE;
714 gmyth_debug( "[%s] ERROR running LiveTV setup.\n", __FUNCTION__ );
718 g_string_free( livetv->local_hostname, TRUE );
720 if ( livetv->recorder != NULL ) {
721 g_object_unref (livetv->recorder);
722 livetv->recorder = NULL;
725 if ( livetv->tvchain != NULL ) {
726 g_object_unref (livetv->tvchain);
727 livetv->tvchain = NULL;
730 if ( livetv->proginfo != NULL ) {
731 g_object_unref (livetv->proginfo);
732 livetv->proginfo = NULL;
739 * Creates a File Transfer session, using all configuration information
740 * got from the actual program info.
742 * @param live_tv the GMythLiveTV instance
744 * @return the actual GMythFileTransfer instance, generated using the
745 * data got from the actual program info.
748 gmyth_livetv_create_file_transfer( GMythLiveTV *livetv )
750 //GMythURI* uri = NULL;
752 if ( NULL == livetv )
755 if ( !livetv->setup_done )
757 gmyth_debug( "Error: You must do the LiveTV setup, just before generating the FileTransfer from LiveTV source!" );
761 #ifdef GMYTH_USE_DEBUG
762 if ( livetv->proginfo != NULL )
763 gmyth_debug( "URI path (from program info) = %s.\n", livetv->proginfo->pathname->str );
765 gmyth_debug( "URI path (from URI) = %s.\n", livetv->uri->uri->str );
768 g_mutex_lock( livetv->mutex );
770 if ( livetv->file_transfer != NULL )
772 /*gmyth_file_transfer_close( livetv->file_transfer );*/
773 g_object_unref( livetv->file_transfer );
774 livetv->file_transfer = NULL;
777 if ( livetv->uri != NULL )
780 if ( livetv->uri->path != NULL )
782 g_string_free( livetv->uri->path, FALSE );
783 livetv->uri->path = NULL;
786 #ifdef GMYTH_USE_DEBUG
787 gmyth_debug( "URI is not NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
789 livetv->uri->path = g_string_erase(livetv->uri->path, 0, -1);
790 livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
792 #ifdef GMYTH_USE_DEBUG
793 gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
795 livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
798 if ( NULL == livetv->uri )
800 gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
804 livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
806 if ( NULL == livetv->file_transfer )
808 gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
812 g_object_ref( livetv->file_transfer );
814 /* gmyth_file_transfer_settimeout( livetv->file_transfer, TRUE ); */
816 g_mutex_unlock( livetv->mutex );
822 g_object_unref( uri );
827 return livetv->file_transfer;
832 * Stops this LiveTV session.
834 * @param live_tv the GMythLiveTV instance
837 gmyth_livetv_stop_playing (GMythLiveTV *livetv)
839 gmyth_debug ("Stopping the LiveTV...\n");
841 if (livetv->is_livetv) {
842 if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
843 gmyth_debug ("[%s] Error while stoping remote encoder", __FUNCTION__);
846 if ( !gmyth_recorder_finish_recording(livetv->recorder) ) {
847 gmyth_debug ("[%s] Error while finishing recording on remote encoder", __FUNCTION__);
853 gmyth_livetv_is_playing (GMythLiveTV *livetv)
859 gmyth_livetv_start_playing (GMythLiveTV *livetv)