# HG changeset patch
# User rosfran
# Date 1175023671 -3600
# Node ID 0e6de3b59f57f136e5ebd74df4cde2ca05ae0da2
# Parent  0683ea324debc0af9d69659da0c38162f341cf97
[svn r467] Addded functions to help stopping LiveTV recording.

diff -r 0683ea324deb -r 0e6de3b59f57 gmyth/src/gmyth_recorder.c
--- a/gmyth/src/gmyth_recorder.c	Tue Mar 27 16:45:00 2007 +0100
+++ b/gmyth/src/gmyth_recorder.c	Tue Mar 27 20:27:51 2007 +0100
@@ -74,6 +74,11 @@
 
     gmyth_recorder_close(recorder);
 
+    if ( recorder->mutex != NULL) {
+        g_mutex_free(recorder->mutex);
+        recorder->mutex = NULL;
+    }
+    
     if ( recorder->myth_socket != NULL) {
         g_object_unref(recorder->myth_socket);
         recorder->myth_socket = NULL;
@@ -97,6 +102,7 @@
 void
 gmyth_recorder_close(GMythRecorder *recorder) 
 {
+    gmyth_recorder_stop_playing(recorder);
     gmyth_recorder_finish_recording(recorder);
 }
 
@@ -113,6 +119,10 @@
     encoder->recorder_num = num;
     encoder->hostname = g_string_new(hostname->str);
     encoder->port = port;
+    
+    encoder->mutex = g_mutex_new();
+    
+    encoder->progs_info_list = NULL;
 
     return encoder;
 }
@@ -162,6 +172,8 @@
     gmyth_debug ("[%s] Spawntv with tvchain_id = %s", __FUNCTION__, tvchain_id->str);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -190,6 +202,8 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
+    
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -213,6 +227,8 @@
     gmyth_debug ("[%s] Spawntv, no TV chain!", __FUNCTION__);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -237,7 +253,9 @@
         goto cleanup;
     }
 
-cleanup:
+cleanup:    
+    g_mutex_unlock( recorder->mutex );
+    
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -258,6 +276,8 @@
     gmyth_debug ("[%s]", __FUNCTION__);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -277,6 +297,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -298,6 +319,8 @@
     gmyth_debug ( "[%s] FRONTEND_READY with recorder id = %d", __FUNCTION__, recorder->recorder_num );
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -324,6 +347,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -347,6 +371,8 @@
     gmyth_debug ("[%s] CHECK_CHANNEL with channel = %s", __FUNCTION__, channel);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -374,6 +400,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -409,6 +436,8 @@
     gmyth_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -436,6 +465,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -459,6 +489,8 @@
     gmyth_debug ("[%s] SET_CHANNEL with channel name = %s", __FUNCTION__, channel);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -485,6 +517,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -516,6 +549,8 @@
     gmyth_debug ("[%s] CHANGE_CHANNEL to the channel direction = %u", __FUNCTION__, direction);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -542,6 +577,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -563,6 +599,8 @@
     gmyth_debug ("[%s] PAUSE", __FUNCTION__);
 
     str_list = gmyth_string_list_new ();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
 
@@ -588,6 +626,7 @@
     }
 
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -597,8 +636,11 @@
 static gboolean 
 gmyth_recorder_find_if_program_exists( GMythRecorder* recorder, GMythProgramInfo* prog )
 {
-    GSList *lst = NULL;
-    for ( lst = recorder->progs_info_list; lst != NULL; lst = g_slist_next( lst ) )
+    GList *lst = NULL;
+    
+    g_return_val_if_fail( recorder != NULL && recorder->progs_info_list != NULL, FALSE );
+    
+    for ( lst = recorder->progs_info_list; lst != NULL; lst = g_list_next( lst ) )
     {
         gmyth_debug ( "Got program info from list = [%s]", 
                 gmyth_program_info_to_string( (GMythProgramInfo*)lst->data ) );
@@ -623,6 +665,8 @@
     GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER);
 
     str_list = gmyth_string_list_new();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf( tmp_str, " %d", recorder->recorder_num);
 
@@ -657,9 +701,10 @@
     }
     
     if ( !gmyth_recorder_find_if_program_exists( recorder, program_info ) )
-        recorder->progs_info_list = g_slist_append( recorder->progs_info_list, 
+        recorder->progs_info_list = g_list_append( recorder->progs_info_list, 
                                                     g_object_ref(program_info) );
 cleanup:
+    g_mutex_unlock( recorder->mutex );
     g_string_free (tmp_str, TRUE);
     g_object_unref (str_list);
 
@@ -686,6 +731,8 @@
     str_list = gmyth_string_list_new();
 
     /* g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); */
+    
+    g_mutex_lock( recorder->mutex );
 
     gmyth_string_list_append_string(str_list, tmp_str);
 
@@ -725,6 +772,8 @@
     } else {
         gmyth_debug( "Cannot find a valuable recorder with the recorder ID number %d, backend server error!", rec_id );
     }
+    
+    g_mutex_unlock( recorder->mutex );
 
     g_object_unref(str_list);
 
@@ -757,6 +806,8 @@
     actual_proginfo = gmyth_recorder_get_current_program_info(recorder);
 
     str_list = gmyth_string_list_new();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf( tmp_str, " %d", recorder->recorder_num);
 
@@ -819,8 +870,10 @@
 
     } /* if */
 
-done: 
-
+done:
+    
+    g_mutex_unlock( recorder->mutex );
+    
     g_object_unref(actual_proginfo);
     g_object_unref(str_list);
 
@@ -845,6 +898,8 @@
     GString *query = g_string_new( GMYTHTV_RECORDER_HEADER);
 
     GMythStringList *str_list = gmyth_string_list_new();
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_append_printf( query, " %d", recorder->recorder_num);
 
@@ -862,6 +917,8 @@
 #ifndef GMYTHTV_ENABLE_DEBUG
     g_print( "[%s] Got file position = %lld\n", __FUNCTION__, pos);
 #endif
+    
+    g_mutex_unlock( recorder->mutex );
 
     if (str_list!=NULL)
         g_object_unref(str_list);
@@ -886,6 +943,8 @@
 
     GMythStringList *str_list = gmyth_string_list_new();
     GString *message = g_string_new("");
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
             recorder->recorder_num);
@@ -908,6 +967,8 @@
 
     gmyth_debug( "%s, stream is %s being recorded!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
     //g_static_mutex_unlock (&mutex);
+    
+    g_mutex_unlock( recorder->mutex );
 
     if ( str_list != NULL )
         g_object_unref (str_list);
@@ -933,6 +994,8 @@
 
     GMythStringList *str_list = gmyth_string_list_new();
     GString *message = g_string_new("");
+    
+    g_mutex_lock( recorder->mutex );
 
     g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
             recorder->recorder_num);
@@ -954,8 +1017,10 @@
         g_string_free (str, TRUE);
     }
 
-    gmyth_debug( "%s, stream is %s being recorded!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
+    gmyth_debug( "%s, stream is %s finished!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
     //g_static_mutex_unlock (&mutex);
+    
+    g_mutex_unlock( recorder->mutex );
 
     if ( str_list != NULL )
         g_object_unref (str_list);
@@ -964,3 +1029,53 @@
 
     return ret;
 }
+
+
+/**
+ * Stops playing the remote file.
+ * 
+ * @param recorder The GMythRecorder instance.
+ * 
+ * @return <code>true</code>, if the recording had been actually stopped.
+ */
+gboolean
+gmyth_recorder_stop_playing( GMythRecorder *recorder) {
+    gboolean ret = TRUE;
+
+    g_return_val_if_fail( recorder != NULL, FALSE );
+
+    GMythStringList *str_list = gmyth_string_list_new();
+    GString *message = g_string_new("");
+    
+    g_mutex_lock( recorder->mutex );
+
+    g_string_printf( message, "%s %d", GMYTHTV_RECORDER_HEADER,
+            recorder->recorder_num);
+    gmyth_string_list_append_string(str_list, message);
+    gmyth_string_list_append_char_array(str_list, "STOP_PLAYING");
+
+    gmyth_socket_sendreceive_stringlist( recorder->myth_socket, str_list);
+
+    if ( str_list != NULL && gmyth_string_list_length(str_list)> 0) {
+        GString *str=  NULL;
+        if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && 
+                strcmp( str->str, "ok")!= 0) {
+            gint is_rec = gmyth_string_list_get_int( str_list, 0);
+            if ( is_rec != 0)
+                ret = TRUE;
+            else
+                ret = FALSE;
+        }
+        g_string_free (str, TRUE);
+    }
+
+    gmyth_debug( "%s, stream is %s stopped!\n", ret ? "YES" : "NO", ret ? "" : "NOT" );
+    g_mutex_unlock ( recorder->mutex );
+
+    if ( str_list != NULL )
+        g_object_unref (str_list);
+    
+    g_string_free (message, TRUE);
+
+    return ret;
+}
diff -r 0683ea324deb -r 0e6de3b59f57 gmyth/src/gmyth_recorder.h
--- a/gmyth/src/gmyth_recorder.h	Tue Mar 27 16:45:00 2007 +0100
+++ b/gmyth/src/gmyth_recorder.h	Tue Mar 27 20:27:51 2007 +0100
@@ -75,7 +75,9 @@
     GString *hostname;
     gint port;
     
-    GSList* progs_info_list;
+    GList* progs_info_list;
+    
+    GMutex *mutex;
 };
 
 typedef enum _GMythRecorderChannelChangeDirection {
@@ -140,6 +142,8 @@
 
 gboolean 	gmyth_recorder_finish_recording ( GMythRecorder *recorder );
 
+gboolean    gmyth_recorder_stop_playing( GMythRecorder *recorder);
+
 G_END_DECLS
 
 #endif /* __GMYTH_REMOTE_ENCODER_H__ */