# HG changeset patch
# User rosfran
# Date 1164675293 0
# Node ID d2d5fe1c3997a0a8dd2030035709ebb27e05cc18
# Parent  253b9171a70355998cd4ef57149be58f81965a45
[svn r125] Performance aspects, remade the buffer filling.

diff -r 253b9171a703 -r d2d5fe1c3997 gst-plugins-mythtv/src/gstmythtvsrc.c
--- a/gst-plugins-mythtv/src/gstmythtvsrc.c	Tue Nov 28 00:53:25 2006 +0000
+++ b/gst-plugins-mythtv/src/gstmythtvsrc.c	Tue Nov 28 00:54:53 2006 +0000
@@ -12,6 +12,16 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more 
  */
+ /**
+  * When using the LiveTV content, put the location URI in the following
+  * format:
+  * 
+  * 	myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/#mythconverg
+  * 
+  * Where the first field is the protocol (myth), the second and third are user 
+  * name (mythtv) and password (mythtv), then backend host name and port number, 
+  * and the last field is the database name (mythconverg).
+  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -49,7 +59,9 @@
 
 #define GST_FLOW_ERROR_NO_DATA  			-101
 
-#define INTERNAL_BUFFER_SIZE					40*1024
+#define REQUEST_MAX_SIZE							64*1024
+
+#define INTERNAL_BUFFER_SIZE					200*1024
 
 /* stablish a maximum iteration value to the IS_RECORDING message */
 static guint wait_to_transfer = 0;
@@ -411,38 +423,50 @@
       (gint) src->buffer_remain, src->bytes_queue->len );
 
   /* just get from the byte array, no network effort... */
-  if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) {
-  //if ( src->buffer_remain  ) {
-  	guint buffer_size_inter = INTERNAL_BUFFER_SIZE - src->buffer_remain;
-  	GByteArray *buffer = g_byte_array_new();
+  if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) { 
+  			//( ( INTERNAL_BUFFER_SIZE - src->buffer_remain )  >=  REQUEST_MAX_SIZE ) ) {
+  	GByteArray *buffer = NULL;
+  	guint amount_to_fill = ( INTERNAL_BUFFER_SIZE - src->buffer_remain );
+  	guint buffer_size_inter = 0;
+  		
+  	//do {
+	  	if ( amount_to_fill < REQUEST_MAX_SIZE )
+	  		buffer_size_inter = amount_to_fill;
+	  	else
+	  		buffer_size_inter = REQUEST_MAX_SIZE;
 
-    read = do_read_request_response( src, buffer_size_inter, buffer );
-
-    if (G_UNLIKELY (read < 0)) {
-      if ( src->live_tv )
-	goto change_progchain;
-      else
-	goto read_error;
-    }
-
-    if ( G_UNLIKELY (src->update_prog_chain) )
-      goto change_progchain;
-      
-    src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
-    if ( read > buffer_size_inter )
-    	GST_WARNING_OBJECT( src, "[%s] INCREASED buffer size! Backend sent more than we ask him... (%d)\n", 
-    			__FUNCTION__, abs( read - buffer_size_inter ) );
-
-    src->buffer_remain = src->buffer_remain + read;
-    
-    if ( buffer != NULL ) {
-    	g_byte_array_free( buffer, TRUE );
-    	buffer = NULL;    	
-    }
-
-    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
-	"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
-	src->read_offset, src->content_size );
+	  	buffer = g_byte_array_new();
+	
+	    read = do_read_request_response( src, buffer_size_inter, buffer );
+	
+	    if (G_UNLIKELY (read < 0)) {
+	      if ( src->live_tv )
+		goto change_progchain;
+	      else
+		goto read_error;
+	    }
+	
+	    if ( G_UNLIKELY (src->update_prog_chain) )
+	      goto change_progchain;
+	      
+	    src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
+	    if ( read > buffer_size_inter )
+	    	GST_WARNING_OBJECT( src, "[%s] INCREASED buffer size! Backend sent more than we ask him... (%d)\n", 
+	    			__FUNCTION__, abs( read - buffer_size_inter ) );
+	
+	    src->buffer_remain += read;
+	    amount_to_fill -= read;
+	
+	  	//} while ( amount_to_fill > 0 );
+	    
+	    if ( buffer != NULL ) {
+	    	g_byte_array_free( buffer, TRUE );
+	    	buffer = NULL;    	
+	    }
+	
+	    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
+		"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
+		src->read_offset, src->content_size );
 	
   }
   
@@ -673,6 +697,7 @@
   if ( src->file_transfer == NULL ) {
     goto init_failed;
   }
+  GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->uri_name );
 
 	ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
 
@@ -690,7 +715,7 @@
     /* IS_RECORDING again, just like the MythTV backend does... */
 	  gmyth_livetv_is_recording( src->spawn_livetv );
 	  
-	  sleep( 4 );  
+	  sleep( 3 );  
 
   }
   /* sets the FileTransfer instance connection (video/audio download) */
@@ -712,8 +737,8 @@
   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   src->buffer_remain = 0;
   
-  //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
-	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
+  gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
+		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
 
 done:
   return TRUE;
@@ -978,10 +1003,13 @@
     	gint64 pos = -1;
       gst_query_parse_position (query, &formt, &pos );
       res = TRUE;
-      if ( formt == GST_FORMAT_BYTES )
+      if ( formt == GST_FORMAT_BYTES ) {
+      	gst_query_set_position (query, formt, pos = myth->read_offset );
       	GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
-      else if ( formt == GST_FORMAT_TIME )
+      } else if ( formt == GST_FORMAT_TIME ) {
       	GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
+      	res = gst_pad_query_default(pad, query);
+      }
       break;
     }
     case GST_QUERY_DURATION:
@@ -996,18 +1024,21 @@
       }
 #endif
 	gint64 dur = -1;
-	gst_query_parse_duration( query, &formt, &dur );
-	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
-  if ( formt == GST_FORMAT_BYTES )
+	gst_query_parse_duration ( query, &formt, &dur );	
+  if ( formt == GST_FORMAT_BYTES ) {
+  	gst_query_set_duration (query, formt, dur = myth->content_size);
   	GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
-  else if ( formt == GST_FORMAT_TIME )
+  } else if ( formt == GST_FORMAT_TIME ) {
   	GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
+  	gst_query_ref(query);
+  	res = gst_pad_query_default(pad, query);  	
+  }
 	res = TRUE;
       break;
     }
     default:
     {
-      res = FALSE;
+      res = gst_pad_query_default(pad, query);
       break;
     }
   }
@@ -1029,8 +1060,17 @@
       //src->unique_setup = FALSE;
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
+    	GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ );
+    	break;
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      //src->eos = FALSE;
+    	GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ );
+      if ( src->live_tv ) {
+	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
+	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
+	      else
+	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
+      }
+
       break;
     default:
       break;
@@ -1048,7 +1088,7 @@
       GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
     case GST_STATE_CHANGE_PAUSED_TO_READY:
       GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
-      if ( src->live_tv && src->update_prog_chain ) {
+      if ( src->live_tv ) {
 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
 	      else