gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Fri Nov 17 13:09:12 2006 +0000 (2006-11-17)
branchtrunk
changeset 90 f6a9705509a1
parent 87 9ea342c364de
child 93 ae73b4837eed
permissions -rwxr-xr-x
[svn r91] Added changes proposed by melunko (buffer on plug-in).
     1 /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
     2 /* GStreamer MythTV Plug-in
     3  * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
     4  *
     5  * This library is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU Library Lesser General 
     7  * Public License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more 
    14  */
    15 
    16 #ifdef HAVE_CONFIG_H
    17 #include "config.h"
    18 #endif
    19 
    20 #include "gstmythtvsrc.h"
    21 #include <gmyth/gmyth_file_transfer.h>
    22 #include <gmyth/gmyth_livetv.h>
    23 
    24 #include <gmyth/gmyth_socket.h>
    25 #include <gmyth/gmyth_tvchain.h>
    26 
    27 #include <string.h>
    28 #include <unistd.h>
    29 
    30 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
    31 #define GST_CAT_DEFAULT mythtvsrc_debug
    32 
    33 #define GST_GMYTHTV_ID_NUM					1
    34 
    35 #define GST_GMYTHTV_CHANNEL_NUM			1000
    36 
    37 #define GMYTHTV_VERSION_DEFAULT			30
    38  
    39 #define GMYTHTV_TRANSFER_MAX_WAITS		100
    40 
    41 #define GMYTHTV_TRANSFER_MAX_BUFFER		128*1024
    42 //( 32*1024  )
    43 
    44 /* 4*1024 ??? */
    45 #define MAX_READ_SIZE              		12*1024
    46 //( 32*1024 )
    47 
    48 #define GST_FLOW_ERROR_NO_DATA  		-101
    49 
    50 #define INTERNAL_BUFFER_SIZE			18*1024
    51 
    52 /* stablish a maximum iteration value to the IS_RECORDING message */
    53 static guint wait_to_transfer = 0;
    54 
    55 static const GstElementDetails gst_mythtv_src_details =
    56 GST_ELEMENT_DETAILS ( "MythTV client source",
    57     "Source/Network",
    58     "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
    59     "Rosfran Borges <rosfran.borges@indt.org.br>" );
    60 
    61 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
    62     GST_PAD_SRC,
    63     GST_PAD_ALWAYS,
    64     GST_STATIC_CAPS ("video/x-nuv") );
    65     
    66 enum
    67 {
    68   PROP_0,
    69   PROP_LOCATION,
    70   PROP_URI,
    71 #ifndef GST_DISABLE_GST_DEBUG
    72   PROP_GMYTHTV_DBG,
    73 #endif
    74   PROP_GMYTHTV_VERSION,
    75   PROP_GMYTHTV_LIVE,
    76   PROP_GMYTHTV_LIVEID,
    77   PROP_GMYTHTV_LIVE_CHAINID,
    78   PROP_GMYTHTV_ENABLE_TIMING_POSITION,
    79   PROP_GMYTHTV_CHANNEL_NUM
    80 };
    81 
    82 static void gst_mythtv_src_finalize (GObject * gobject);
    83 
    84 /*
    85 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
    86 	guint size, GstBuffer ** outbuf);
    87 */
    88 
    89 //static GstFlowReturn gst_mythtv_src_chain ( GstPad* pad, GstBuffer* outbuf );
    90 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
    91 
    92 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
    93 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
    94 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
    95 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
    96 
    97 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
    98 
    99 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
   100 
   101 static GstStateChangeReturn
   102 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
   103 
   104 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
   105     const GValue * value, GParamSpec * pspec);
   106 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
   107     GValue * value, GParamSpec * pspec);
   108 
   109 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
   110 
   111 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
   112 
   113 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
   114 
   115 static gint do_read_request_response (GstMythtvSrc * src, guint64 offset, 
   116     guint size, gint8 **data_ptr);
   117 
   118 GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
   119 
   120 static void
   121 _urihandler_init (GType type)
   122 {
   123   static const GInterfaceInfo urihandler_info = {
   124     gst_mythtv_src_uri_handler_init,
   125     NULL,
   126     NULL
   127   };
   128 
   129   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
   130 
   131   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   132       "MythTV src");
   133 }
   134 
   135 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
   136 //    GST_TYPE_BASE_SRC, _urihandler_init)
   137     
   138 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
   139     GST_TYPE_PUSH_SRC, _urihandler_init)
   140     
   141 static void
   142 gst_mythtv_src_base_init (gpointer g_class)
   143 {
   144   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   145 
   146   gst_element_class_add_pad_template (element_class,
   147       gst_static_pad_template_get (&srctemplate));
   148       
   149   gst_element_class_set_details (element_class, &gst_mythtv_src_details);
   150   
   151   element_class->change_state = gst_mythtv_src_change_state;
   152   
   153 }
   154 
   155 static void
   156 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
   157 {
   158   GObjectClass *gobject_class; 
   159   GstPushSrcClass *gstpushsrc_class;
   160   GstBaseSrcClass *gstbasesrc_class;
   161 
   162   gobject_class = (GObjectClass *) klass;
   163   gstbasesrc_class = (GstBaseSrcClass *) klass;
   164   gstpushsrc_class = (GstPushSrcClass *) klass;
   165 
   166   gobject_class->set_property = gst_mythtv_src_set_property;
   167   gobject_class->get_property = gst_mythtv_src_get_property;
   168   gobject_class->finalize = gst_mythtv_src_finalize;
   169 
   170   g_object_class_install_property
   171     (gobject_class, PROP_LOCATION,
   172      g_param_spec_string ("location", "Location",
   173        "The location. In the form:"
   174        "\n\t\t\tmyth://a.com/file.nuv"
   175        "\n\t\t\tmyth://a.com:23223/file.nuv "
   176        "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
   177        "", G_PARAM_READWRITE));
   178 
   179   g_object_class_install_property
   180     (gobject_class, PROP_URI,
   181      g_param_spec_string ("uri", "Uri",
   182        "The location in form of a URI (deprecated; use location)",
   183        "", G_PARAM_READWRITE));
   184 
   185   g_object_class_install_property
   186     (gobject_class, PROP_GMYTHTV_VERSION,
   187      g_param_spec_int ("mythtv-version", "mythtv-version",
   188        "Change MythTV version",
   189        26, 30, 26, G_PARAM_READWRITE));
   190 
   191   g_object_class_install_property
   192     (gobject_class, PROP_GMYTHTV_LIVEID,
   193      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   194        "Change MythTV version",
   195        0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
   196 
   197   g_object_class_install_property
   198     (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
   199      g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
   200        "Sets the MythTV chain ID (from TV Chain)",
   201        "", G_PARAM_READWRITE));
   202 
   203   g_object_class_install_property
   204     (gobject_class, PROP_GMYTHTV_LIVE,
   205      g_param_spec_boolean ("mythtv-live", "mythtv-live",
   206        "Enable MythTV Live TV content streaming",
   207        FALSE, G_PARAM_READWRITE));
   208 
   209   g_object_class_install_property
   210     (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
   211      g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
   212        "Enable MythTV Live TV content size continuous updating",
   213        FALSE, G_PARAM_READWRITE));
   214        
   215   g_object_class_install_property
   216     (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
   217      g_param_spec_int ("mythtv-channel", "mythtv-channel",
   218        "Change MythTV channel number",
   219        0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
   220 
   221 #ifndef GST_DISABLE_GST_DEBUG
   222   g_object_class_install_property
   223     (gobject_class, PROP_GMYTHTV_DBG,
   224      g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
   225        "Enable MythTV debug messages",
   226        FALSE, G_PARAM_READWRITE));
   227 #endif
   228 
   229   gstbasesrc_class->start = gst_mythtv_src_start;
   230   gstbasesrc_class->stop = gst_mythtv_src_stop;
   231   gstbasesrc_class->get_size = gst_mythtv_src_get_size;
   232   gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
   233   
   234   gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
   235   gstpushsrc_class->create = gst_mythtv_src_create;
   236     
   237   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   238       "MythTV Client Source");
   239 }
   240 
   241 static void
   242 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
   243 {
   244   this->file_transfer = NULL;
   245 
   246   this->unique_setup = FALSE;
   247 
   248   this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
   249   
   250   this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
   251 
   252   this->bytes_read = 0;
   253   
   254   this->prev_content_size = 0;
   255 
   256   this->content_size = 0;
   257   this->read_offset = 0;
   258 
   259   this->content_size_last = 0;
   260 
   261   this->live_tv = FALSE;
   262   
   263   this->enable_timing_position = FALSE;
   264   this->update_prog_chain = FALSE;    
   265 
   266   this->user_agent = g_strdup ("mythtvsrc");
   267   this->mythtv_caps = NULL;
   268   this->update_prog_chain = FALSE;
   269   
   270   this->channel_num = 0;
   271   
   272   this->eos = FALSE;
   273   
   274   this->adapter = NULL;
   275   
   276   //this->th_read_ahead = NULL;
   277   
   278   this->th_mutex = NULL;
   279   
   280   this->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
   281   gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
   282   
   283   gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
   284 
   285   //gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
   286   
   287   gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   288       gst_mythtv_src_handle_event );
   289   gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   290      gst_mythtv_src_handle_query );
   291 
   292 }
   293 
   294 static void
   295 gst_mythtv_src_finalize (GObject * gobject)
   296 {
   297   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   298   
   299   if ( this->th_read_ahead != NULL ) {
   300   	gst_task_stop( this->th_read_ahead );
   301   	this->th_read_ahead = NULL;
   302   }
   303 
   304   if (this->mythtv_caps) {
   305     gst_caps_unref (this->mythtv_caps);
   306     this->mythtv_caps = NULL;
   307   }
   308 
   309   if (this->file_transfer) {
   310     g_object_unref (this->file_transfer);
   311     this->file_transfer = NULL;
   312   }
   313 
   314 	if (this->spawn_livetv) {
   315     g_object_unref (this->spawn_livetv);
   316     this->spawn_livetv = NULL;
   317   }
   318 
   319   if (this->uri_name) {
   320     g_free (this->uri_name);
   321   }
   322 
   323   if (this->user_agent) {
   324     g_free (this->user_agent);
   325   }
   326 
   327   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   328 }
   329 
   330 static gint
   331 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, gint8 **data_ptr)
   332 {
   333   gint read = 0;
   334   guint sizetoread = size;
   335 
   336   g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   337 
   338   /* Loop sending the Myth File Transfer request:
   339    * Retry whilst authentication fails and we supply it. */
   340   gint len = 0;
   341   //*data_ptr = g_malloc0( size );
   342   
   343   //while ( sizetoread > 0 ) {
   344 
   345     len = gmyth_file_transfer_read( src->file_transfer,
   346 	*data_ptr + offset + read, sizetoread, TRUE );
   347 
   348     if ( len > 0 ) {
   349       read += len;
   350       sizetoread -= len;
   351     } 
   352     else if ( len < 0 )
   353     {
   354       read = -1;
   355 
   356       if ( src->live_tv == FALSE ) 
   357       {
   358 	goto eos;
   359       } 
   360       else  
   361       {
   362 	if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
   363 	  src->update_prog_chain = TRUE;
   364 	  goto done;	  	
   365 	} else if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
   366 	    src->update_prog_chain = TRUE;
   367 	    if ( src->enable_timing_position ) {
   368 	      gint64 size_tmp = 0;
   369 get_file_pos:
   370 	      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   371 	      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   372 		src->content_size = size_tmp;
   373 	      else if ( size_tmp > 0 )
   374 		goto get_file_pos;
   375 	      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   376 		  __FUNCTION__, size_tmp );
   377 	    }
   378 	  }
   379 	goto done;
   380       }
   381 
   382     }
   383 
   384     if ( read == sizetoread )
   385       goto done;
   386   //}
   387 
   388   if ( read < 0 && !src->live_tv )
   389     goto eos;
   390 
   391   goto done;
   392 
   393 eos:
   394   src->eos = TRUE;
   395 
   396 done:
   397 
   398   return read;
   399 }
   400 
   401 #if 0
   402 static GstFlowReturn
   403 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
   404 {
   405   GstMythtvSrc *src;
   406   GstFlowReturn ret = GST_FLOW_OK;
   407   gint read = -1;
   408   gint adapter_size = -1;
   409   
   410   guint max_adapter_rep = 40;
   411   
   412   src = GST_MYTHTV_SRC (psrc);
   413   
   414   /* The caller should know the number of bytes and not read beyond EOS. */
   415   if (G_UNLIKELY (src->eos))
   416     goto eos;
   417   if ( G_UNLIKELY (src->update_prog_chain) )
   418     goto change_progchain;
   419 
   420   g_static_rec_mutex_lock( th_mutexth_mutex );
   421   
   422   while ( ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < size ) &&
   423   		--max_adapter_rep > 0 )
   424   {
   425   	g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
   426   	GST_TASK_WAIT( src->th_read_ahead );
   427   }
   428   	
   429   g_static_rec_mutex_unlock( th_mutexth_mutex );
   430 
   431 	gint64 new_offset = -1;
   432 	/* just get from the adapter, no network effort... */
   433 	if ( offset > src->adapter_offset && size <= adapter_size )	
   434 	{
   435 		
   436 		GstBuffer *buf = gst_adapter_take_buffer( src->adapter, size );
   437 		*outbuf = gst_buffer_create_sub( buf, offset, size );
   438 		src->read_offset = new_offset = offset;
   439 		read = size;
   440 		
   441 		gst_adapter_flush( src->adapter, size );
   442 		
   443 	} else {
   444 		/* no data on adapter... do all these mythtv network calls! */
   445 		
   446 		/* verify if it needs to seek */
   447 		if ( src->read_offset != offset ) 
   448   	{
   449 		
   450 	  	new_offset = gmyth_file_transfer_seek( src->file_transfer, offset, SEEK_SET );
   451 	  	
   452 	    g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   453 	    	__FUNCTION__, src->read_offset, new_offset );
   454 	    if ( G_UNLIKELY (new_offset < 0 ) )
   455 	    {
   456 	      if ( src->live_tv )
   457 	      	goto change_progchain;
   458 	      else
   459 	      	goto eos;
   460 	    }
   461 	    
   462   	}
   463 	
   464     src->read_offset = offset;
   465   	
   466 	  /* Create the buffer. */
   467 	  ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   468 	      src->read_offset, size,
   469 	      GST_PAD_CAPS ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)) ), outbuf);
   470 	
   471 	  if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   472 	    if ( src->live_tv )
   473 	    	goto change_progchain;
   474 	    else
   475 	    	goto done;
   476 	  }
   477 	  
   478 	  read = do_read_request_response ( src, src->read_offset, size, outbuf ); 
   479   	
   480 	}
   481 
   482   if (G_UNLIKELY (src->update_prog_chain) )
   483     goto change_progchain;
   484 
   485   if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
   486   	if ( src->live_tv )
   487     	goto change_progchain;
   488     else
   489     	goto read_error;
   490   }
   491   
   492   if ( read > 0 ) {
   493     src->read_offset += read;
   494     src->bytes_read += read;
   495     
   496     #if 0
   497     g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   498 			"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   499 			src->read_offset, src->content_size );
   500 
   501     GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
   502     //GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   503     //GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   504     //g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
   505     GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
   506     GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
   507 
   508     g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   509 			"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   510 			GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   511 		#endif
   512 			
   513   }
   514 
   515 done:
   516  {
   517     const gchar *reason = gst_flow_get_name (ret);
   518 
   519     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   520   	return ret;
   521  }
   522 eos:
   523   {
   524     const gchar *reason = gst_flow_get_name (ret);
   525 
   526     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   527     return GST_FLOW_UNEXPECTED;
   528   }
   529   /* ERRORS */
   530 read_error:
   531   {
   532     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   533 	(NULL), ("Could not read any bytes (%i, %s)", read,
   534 	  src->uri_name));
   535     return GST_FLOW_ERROR;
   536   }
   537 change_progchain:
   538   {
   539     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   540 		(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   541 		  src->uri_name));
   542 		  
   543 		gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   544   			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   545 		// go to the next program chain
   546 		src->unique_setup = FALSE;
   547 		src->update_prog_chain = TRUE;
   548 		
   549 		gst_mythtv_src_next_program_chain( src );
   550 		
   551     return GST_FLOW_ERROR_NO_DATA;
   552   }
   553 
   554 }
   555 #endif
   556 
   557 static GstFlowReturn
   558 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
   559 {
   560   GstMythtvSrc *src;
   561   GstFlowReturn ret = GST_FLOW_OK;
   562   gint read = -1;
   563   gint adapter_size = 0;
   564   guint max_adapter_rep = 1;
   565 
   566   src = GST_MYTHTV_SRC ( psrc );
   567 
   568   /* The caller should know the number of bytes and not read beyond EOS. */
   569   if (G_UNLIKELY (src->eos))
   570     goto eos;
   571   if ( G_UNLIKELY (src->update_prog_chain) )
   572     goto change_progchain;
   573   
   574   //g_static_rec_mutex_lock( &th_mutex );
   575 
   576   while ( src->adapter != NULL && ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < MAX_READ_SIZE ) &&
   577       --max_adapter_rep > 0 )
   578   {
   579     g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
   580     //GST_TASK_WAIT( src->th_read_ahead );
   581   }
   582 
   583   //gst_task_pause( src->th_read_ahead );
   584 
   585   //g_static_rec_mutex_unlock( &th_mutex );
   586 
   587   /* just get from the adapter, no network effort... */
   588   if ( adapter_size > 0 )	
   589   {
   590     //g_static_rec_mutex_lock( &th_mutex );
   591 
   592     *outbuf = gst_adapter_take_buffer( src->adapter, adapter_size );
   593     // src->read_offset += size;
   594     read = adapter_size;
   595 
   596     gst_adapter_flush( src->adapter, adapter_size );
   597     //g_static_rec_mutex_unlock( &th_mutex );
   598 
   599   } else { 
   600     guint size = (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   601 
   602     g_print ( "[%s]\t\tCreate: buffer_offset: %d, buffer_remain: %d\n", __FUNCTION__, 
   603 	(gint) src->buffer_offset,
   604 	(gint) src->buffer_remain);
   605 
   606     /* Create the buffer. */
   607     ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   608 	src->read_offset /*GST_BUFFER_OFFSET_NONE*/, size,
   609 	GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
   610 
   611     if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   612       if ( src->live_tv )
   613 	goto change_progchain;
   614       else
   615 	goto done;
   616     }
   617 
   618     if (src->buffer_remain < MAX_READ_SIZE) {
   619       gint8 *tmp_buffer = g_malloc0( INTERNAL_BUFFER_SIZE ); // FIXME: DON'T ALLOC EVERY TIME
   620 
   621       memcpy (tmp_buffer, src->buffer + src->buffer_offset, src->buffer_remain);
   622 
   623       read = do_read_request_response( src, 0, INTERNAL_BUFFER_SIZE - src->buffer_remain, &tmp_buffer );
   624 
   625       if (G_UNLIKELY (read < 0)) {
   626 	if ( src->live_tv )
   627 	  goto change_progchain;
   628 	else
   629 	  goto read_error;
   630       }
   631 
   632       if ( G_UNLIKELY (src->update_prog_chain) )
   633 	goto change_progchain;
   634 
   635       //len = gmyth_file_transfer_read( src->file_transfer,
   636       //  tmp_buffer + src->buffer_remain, INTERNAL_BUFFER_SIZE - src->buffer_remain, TRUE );
   637       // fixme: handle eos
   638       // fixme: can I deallocate the previous buffer here?
   639       g_memmove( src->buffer + src->buffer_remain, tmp_buffer, read);
   640       src->buffer_offset = 0;
   641       src->buffer_remain = src->buffer_remain + read;
   642 
   643       // g_free( tmp_buffer );
   644 
   645     }
   646 
   647     g_print( "[%s] read = %d, buffer_remain = %d\n", __FUNCTION__, read, src->buffer_remain );
   648 
   649     GST_BUFFER_SIZE (*outbuf) = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   650     GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   651     GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   652     g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->buffer + src->buffer_offset, GST_BUFFER_SIZE(*outbuf) );
   653     GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
   654     GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
   655 
   656     src->buffer_offset += GST_BUFFER_SIZE (*outbuf);
   657     src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
   658 
   659     src->read_offset += GST_BUFFER_SIZE (*outbuf);
   660     src->bytes_read += GST_BUFFER_SIZE (*outbuf);
   661     g_print ( "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
   662 
   663     g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   664 	"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   665 	src->read_offset, src->content_size );
   666 
   667     g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   668 	"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   669 	GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   670 
   671     /* just get from the adapter, no network effort... */
   672 
   673   }
   674 
   675   return ret;
   676 
   677   //g_static_rec_mutex_lock( &th_mutex );
   678   
   679   //GST_TASK_SIGNAL( src->th_read_ahead );
   680 
   681   //g_static_rec_mutex_unlock( &th_mutex );
   682   
   683 done:
   684   {
   685     const gchar *reason = gst_flow_get_name (ret);
   686 
   687     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   688     return ret;
   689   }
   690 eos:
   691   {
   692     const gchar *reason = gst_flow_get_name (ret);
   693 
   694     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   695     return GST_FLOW_UNEXPECTED;
   696   }
   697   /* ERRORS */
   698 read_error:
   699   {
   700     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   701 	(NULL), ("Could not read any bytes (%i, %s)", read,
   702 		 src->uri_name));
   703     return GST_FLOW_ERROR;
   704   }
   705 change_progchain:
   706   {
   707     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   708 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   709 		 src->uri_name));
   710 
   711     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   712 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   713     // go to the next program chain
   714     src->unique_setup = FALSE;
   715     src->update_prog_chain = TRUE;
   716 
   717     gst_mythtv_src_next_program_chain( src );
   718 
   719     return GST_FLOW_ERROR_NO_DATA;
   720   }
   721 
   722 }
   723 
   724 gint64
   725 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   726 {
   727 
   728   gint64 size_tmp = 0;
   729   guint max_tries = 2;
   730   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   731 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   732 
   733 get_file_pos:
   734     g_usleep( 10 );
   735     size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   736     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   737       src->content_size = size_tmp;
   738     else if ( size_tmp > 0 && --max_tries > 0 )
   739       goto get_file_pos;
   740     g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   741 	__FUNCTION__, size_tmp );
   742     /* sets the last content size amount before it can be updated */
   743     src->prev_content_size = src->content_size;
   744   }
   745 
   746   return src->content_size;	
   747 
   748 }
   749 
   750 static gboolean
   751 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
   752 {
   753   GstMythtvSrc *src = GST_MYTHTV_SRC( base );
   754   gint64 new_offset = -1;
   755   gint64 actual_seek = segment->start;
   756   gboolean ret = TRUE;
   757 
   758   g_print( "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, segment->start, segment->stop );
   759   //g_static_rec_mutex_lock( &th_mutex );
   760 
   761   //GST_TASK_WAIT( src->th_read_ahead );
   762 
   763   //g_static_rec_mutex_unlock( &th_mutex );
   764 
   765   if ( segment->format == GST_FORMAT_TIME ) 
   766   {
   767     goto done; 
   768     //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
   769   }
   770   g_print( "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
   771   /* verify if it needs to seek */
   772   if ( src->read_offset != actual_seek )
   773   {
   774 
   775     new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
   776 
   777     g_print( "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   778 	__FUNCTION__, segment->start, src->read_offset, new_offset );
   779     if ( G_UNLIKELY (new_offset < 0 ) )
   780     {
   781       ret = FALSE;
   782       if ( src->live_tv )
   783 	goto change_progchain;
   784       else
   785 	goto eos;
   786     }
   787 
   788     src->read_offset = new_offset;
   789 
   790     //gst_segment_set_seek( segment, segment->rate, GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE, 
   791     //GST_SEEK_TYPE_CUR, src->read_offset, GST_SEEK_TYPE_CUR, segment->stop, &ret );
   792 
   793     if ( ret == FALSE ) {
   794       g_print( "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
   795     }
   796 
   797   }
   798    //g_static_rec_mutex_lock( &th_mutex );
   799 
   800    //GST_TASK_SIGNAL( src->th_read_ahead );
   801 
   802    //g_static_rec_mutex_unlock( &th_mutex );
   803 done:
   804    return ret;
   805 
   806 eos:
   807   {
   808 
   809     GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
   810     //gst_object_unref( src );
   811     return FALSE;
   812   }
   813 change_progchain:
   814   {
   815     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   816 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   817 		 src->uri_name));
   818 
   819     gst_pad_push_event ( GST_BASE_SRC_PAD (base),
   820 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   821     /* go to the next program chain */
   822     src->unique_setup = FALSE;
   823     src->update_prog_chain = TRUE;
   824 
   825     gst_mythtv_src_next_program_chain( src );
   826 
   827     return TRUE;
   828   }
   829 
   830 }
   831 
   832 #if 0
   833 static void 
   834 gst_mythtv_src_read_ahead ( void *data ) {
   835 
   836   GstMythtvSrc *src = NULL;
   837 
   838   GstBuffer *outbuf = NULL;
   839 
   840   guint size = 5*2048;
   841   gint total = 0;
   842   gint read = -1;
   843 
   844   src = GST_MYTHTV_SRC( data );
   845 
   846   //GST_PAD_STREAM_TRYLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   847 
   848   do {
   849     GST_TASK_WAIT( src->th_read_ahead );
   850 
   851     gint8 *data = NULL;
   852     
   853     outbuf = gst_buffer_new_and_alloc( size );
   854 
   855     read = do_read_request_response ( src, src->adapter_offset, size, &data );
   856 
   857     if ( read > 0 ) {
   858       src->read_offset += read;
   859       src->bytes_read += read;
   860       total += read;
   861 
   862       g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   863 	  "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   864 	  src->read_offset, src->content_size );
   865 
   866       GST_BUFFER_SIZE (outbuf) = read;
   867       GST_BUFFER_MALLOCDATA( outbuf ) = g_malloc0( GST_BUFFER_SIZE (outbuf) );
   868       GST_BUFFER_DATA( outbuf ) = GST_BUFFER_MALLOCDATA( outbuf );
   869       g_memmove( GST_BUFFER_DATA( outbuf ), data, read );
   870       GST_BUFFER_OFFSET (outbuf) = src->adapter_offset;  
   871       GST_BUFFER_OFFSET_END (outbuf) = src->adapter_offset + read;
   872       g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   873 	  "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (outbuf), 
   874 	  GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf) );
   875 
   876     } 
   877 
   878     gst_adapter_push( src->adapter, outbuf );
   879 
   880     GST_TASK_SIGNAL( src->th_read_ahead );
   881 
   882   } while ( read < size );
   883 
   884   //GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   885 
   886   return;	
   887 }
   888 #endif
   889 
   890 /* create a socket for connecting to remote server */
   891 static gboolean
   892 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   893 {
   894   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   895 
   896   GString *chain_id_local = NULL;
   897 
   898   gboolean ret = TRUE;
   899 
   900   if ( G_UNLIKELY (src->update_prog_chain) )
   901     goto change_progchain;
   902 
   903   if (src->unique_setup == FALSE) {
   904     src->unique_setup = TRUE;
   905   } else {
   906     goto done;
   907   }
   908 
   909   if ( src->live_tv ) {
   910     src->spawn_livetv = gmyth_livetv_new( );
   911     if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   912       ret = FALSE;
   913       goto init_failed;
   914     }    
   915 
   916     /* set up the uri variable */
   917     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   918     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   919     if ( chain_id_local != NULL ) {
   920       src->live_chain_id = g_strdup( chain_id_local->str );
   921       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   922     }
   923     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   924     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
   925   }
   926 
   927   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   928       g_string_new( src->uri_name ), -1, src->mythtv_version );
   929 
   930   if ( src->file_transfer == NULL ) {
   931     goto init_failed;
   932   }
   933 
   934   /* sets the Playback monitor connection */
   935   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   936 
   937   if ( src->live_tv == TRUE && ret == TRUE ) {
   938     /* loop finished, set the max tries variable to zero again... */
   939     wait_to_transfer = 0;
   940 
   941     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   942 	( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   943 	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   944       g_usleep( 100 );
   945   }
   946 
   947   /* sets the FileTransfer instance connection (video/audio download) */
   948   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   949 
   950   if ( ret == FALSE ) {
   951 #ifndef GST_DISABLE_GST_DEBUG  
   952     if ( src->mythtv_msgs_dbg )
   953       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   954 #endif
   955     goto begin_req_failed;
   956   }
   957 
   958   src->content_size = src->file_transfer->filesize;
   959 
   960   src->do_start = FALSE;
   961 
   962   //if ( src->live_tv ) {
   963   src->adapter = gst_adapter_new();
   964   //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
   965   //gst_task_set_lock( src->th_read_ahead, &th_mutex );
   966   //gst_task_start( src->th_read_ahead );
   967   // }
   968   //gst_pad_push_event ( GST_BASE_SRC_PAD (bsrc),
   969   //	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, src->content_size, 0 ) );
   970 
   971   src->buffer = g_malloc0 (INTERNAL_BUFFER_SIZE);
   972   src->buffer_offset = 0;
   973   src->buffer_remain = 0;
   974 
   975 
   976 done:
   977   return TRUE;
   978 
   979   /* ERRORS */
   980 init_failed:
   981   {
   982     if (src->spawn_livetv != NULL )
   983       g_object_unref( src->spawn_livetv );
   984 
   985     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   986 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   987     return FALSE;
   988   }
   989 begin_req_failed:
   990   {
   991     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   992 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   993     return FALSE;
   994   }
   995 change_progchain:
   996   {
   997     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   998 	(NULL), ("Seek failed, go to the next program info... (%s)",
   999 		 src->uri_name));
  1000 
  1001     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1002 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1003 
  1004     // go to the next program chain
  1005     src->unique_setup = FALSE;
  1006     src->update_prog_chain = TRUE;
  1007 
  1008     gst_mythtv_src_next_program_chain( src );
  1009 
  1010     return TRUE;
  1011   }
  1012 }
  1013 
  1014 /* create a new socket for connecting to the next program chain */
  1015 static gboolean
  1016 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
  1017 {
  1018   GString *chain_id_local = NULL;
  1019 
  1020   gboolean ret = TRUE;
  1021 
  1022   if ( !src->live_tv )
  1023     goto init_failed;
  1024     
  1025   if (src->unique_setup == FALSE) {
  1026     src->unique_setup = TRUE;
  1027   } else {
  1028     goto done;
  1029   }
  1030   
  1031 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
  1032 
  1033   if (src->file_transfer) {
  1034     g_object_unref (src->file_transfer);
  1035     src->file_transfer = NULL;
  1036   }
  1037 
  1038   if (src->uri_name) {
  1039     g_free (src->uri_name);
  1040   }
  1041 
  1042   if ( src->live_tv ) {
  1043     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
  1044     	g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
  1045       ret = FALSE;
  1046       goto init_failed;
  1047     }
  1048     /* set up the uri variable */
  1049     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
  1050     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
  1051     if ( chain_id_local != NULL ) {
  1052       src->live_chain_id = g_strdup( chain_id_local->str );
  1053       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
  1054     }
  1055     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
  1056     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
  1057   }
  1058 
  1059   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
  1060       g_string_new( src->uri_name ), -1, src->mythtv_version );
  1061 
  1062   if ( src->file_transfer == NULL ) {
  1063     goto init_failed;
  1064   }
  1065 
  1066   /* sets the Playback monitor connection */
  1067   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
  1068 
  1069   if ( src->live_tv == TRUE && ret == TRUE ) {
  1070     /* loop finished, set the max tries variable to zero again... */
  1071     wait_to_transfer = 0;
  1072 
  1073     g_usleep( 200 );
  1074 
  1075     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
  1076 				( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
  1077       g_usleep( 1000 );
  1078   }
  1079 
  1080   /* sets the FileTransfer instance connection (video/audio download) */
  1081   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
  1082 
  1083   if ( ret == FALSE ) {
  1084 #ifndef GST_DISABLE_GST_DEBUG  
  1085     if ( src->mythtv_msgs_dbg )
  1086       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
  1087 #endif
  1088     goto begin_req_failed;
  1089   }
  1090   src->content_size_last = src->content_size;
  1091 
  1092 #if 0
  1093   if ( src->content_size < src->file_transfer->filesize ) {
  1094     src->content_size = src->file_transfer->filesize;
  1095   } else {
  1096     //gint64 pos = gst_mythtv_src_get_position(src);
  1097     //if ( pos > src->file_transfer->filesize )
  1098     //	src->content_size = pos;  	
  1099 
  1100   }
  1101 #endif
  1102 
  1103   src->content_size = src->file_transfer->filesize;
  1104   if ( src->live_tv ) {
  1105   	wait_to_transfer = 0;
  1106 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
  1107 	    src->content_size = gst_mythtv_src_get_position( src );
  1108   }
  1109 
  1110   src->read_offset = 0;  
  1111   
  1112 done:
  1113 	src->update_prog_chain = FALSE;
  1114 	
  1115 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
  1116 	
  1117   return TRUE;
  1118 
  1119   /* ERRORS */
  1120 init_failed:
  1121   {
  1122     if (src->spawn_livetv != NULL )
  1123       g_object_unref( src->spawn_livetv );
  1124 
  1125     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
  1126 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
  1127     return FALSE;
  1128   }
  1129 begin_req_failed:
  1130   {
  1131     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
  1132 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
  1133     return FALSE;
  1134   }
  1135 
  1136 }
  1137 
  1138 static gboolean
  1139 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
  1140 {
  1141   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
  1142   gboolean ret = TRUE;
  1143   g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
  1144   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
  1145 
  1146   if (src->content_size == -1) {
  1147     //ret= FALSE;
  1148   } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) < 
  1149 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
  1150     //g_static_mutex_lock( &update_size_mutex );
  1151     //GST_OBJECT_LOCK(src);
  1152     
  1153     gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
  1154     if ( new_offset > 0 && new_offset > src->content_size ) {
  1155 			src->content_size = new_offset;
  1156     } else if ( new_offset < src->content_size ) {
  1157 			src->update_prog_chain = TRUE;
  1158 	  }
  1159 
  1160     if ( src->enable_timing_position ) {
  1161       gint64 size_tmp = 0;
  1162       if (src->live_tv == TRUE) {
  1163 get_file_pos:
  1164 	g_usleep( 5 );
  1165 	size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
  1166 	if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
  1167 	  src->content_size = size_tmp;
  1168 	else if ( size_tmp > 0  )
  1169 	  goto get_file_pos;
  1170 	g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
  1171 	    __FUNCTION__, size_tmp );
  1172       }
  1173     }
  1174     
  1175     src->prev_content_size = src->content_size;
  1176     
  1177     //GST_OBJECT_UNLOCK(src);
  1178     //g_static_mutex_unlock( &update_size_mutex );
  1179   }
  1180 
  1181   *size = src->content_size;
  1182   g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
  1183   
  1184   return ret;
  1185 
  1186 }
  1187 
  1188 /* close the socket and associated resources
  1189  * used both to recover from errors and go to NULL state */
  1190 static gboolean
  1191 gst_mythtv_src_stop (GstBaseSrc * bsrc)
  1192 {
  1193   GstMythtvSrc *src;
  1194 
  1195   src = GST_MYTHTV_SRC (bsrc);
  1196 
  1197   if (src->uri_name) {
  1198     g_free (src->uri_name);
  1199     src->uri_name = NULL;
  1200   }
  1201 
  1202   if (src->mythtv_caps) {
  1203     gst_caps_unref (src->mythtv_caps);
  1204     src->mythtv_caps = NULL;
  1205   }
  1206 
  1207   src->eos = FALSE;
  1208 
  1209   return TRUE;
  1210 }
  1211 
  1212 static gboolean
  1213 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
  1214 {
  1215   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
  1216   gint64 cont_size = 0;
  1217   gboolean ret = FALSE;
  1218 
  1219   switch (GST_EVENT_TYPE (event)) {
  1220 #if 0
  1221     case GST_EVENT_FLUSH_START:
  1222       //src->eos = FALSE;
  1223       g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
  1224       cont_size = gst_mythtv_src_get_position (src);
  1225       if ( !src->live_tv ) {
  1226 	if ( cont_size > src->content_size ) {
  1227 	  src->content_size = cont_size;
  1228 	  src->eos = FALSE;
  1229 	} else {
  1230 	  src->eos = TRUE;
  1231 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1232 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1233 	}
  1234       } else {
  1235 	if ( cont_size <= 0 ) {
  1236 	  src->update_prog_chain = TRUE;
  1237 	  src->eos = TRUE;
  1238 	  src->unique_setup = FALSE;
  1239 	  src->do_start = TRUE;		  				  		
  1240 	}		  	
  1241       }
  1242       break;
  1243     case GST_EVENT_FLUSH_STOP:
  1244       src->do_start = TRUE;
  1245       src->eos = FALSE;
  1246       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
  1247       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
  1248       break;
  1249 #endif
  1250     case GST_EVENT_EOS:
  1251       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
  1252 
  1253       if ( src->live_tv ) {
  1254 	cont_size = gst_mythtv_src_get_position (src);
  1255 	if ( cont_size > src->content_size ) {
  1256 	  src->content_size = cont_size;
  1257 	  src->eos = FALSE;
  1258 	} else {
  1259 	  src->eos = TRUE;
  1260 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1261 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1262 	}
  1263       } else 
  1264 	src->eos = TRUE;
  1265       ret = TRUE;
  1266       break;
  1267     case GST_EVENT_NEWSEGMENT:
  1268       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
  1269       ret = gst_pad_event_default (pad, event);
  1270       break;
  1271     case GST_EVENT_SEEK:  	  
  1272       {
  1273 	gst_event_ref( event );
  1274 
  1275 	gdouble rate;
  1276 	//gboolean update = TRUE;
  1277 	GstFormat format;
  1278 	GstSeekType cur_type, stop_type;
  1279 	GstSeekFlags flags;
  1280 	gint64 cur = 0, stop = 0;
  1281 	gst_event_parse_seek ( event, &rate, &format,
  1282 	    &flags, &cur_type, &cur,
  1283 	    &stop_type, &stop );
  1284 
  1285 	g_print( "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
  1286 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
  1287 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
  1288 	}
  1289 	if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
  1290 	  gst_event_unref( event );
  1291 	  break;
  1292 	}
  1293 	
  1294 	break;
  1295       }
  1296     default:
  1297       ret = gst_pad_event_default (pad, event);
  1298   }
  1299 
  1300   return ret;
  1301 }
  1302 
  1303 static gboolean
  1304 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
  1305 {
  1306   return TRUE;
  1307 }
  1308 
  1309 static gboolean
  1310 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
  1311 {
  1312   gboolean res = FALSE;
  1313   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
  1314 
  1315   switch (GST_QUERY_TYPE (query)) {
  1316     case GST_QUERY_POSITION:
  1317       gst_query_set_position (query, GST_FORMAT_BYTES,
  1318 	  myth->read_offset );
  1319       res = TRUE;
  1320       GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
  1321       break;
  1322     case GST_QUERY_DURATION:
  1323 #if 0
  1324       if (myth->duration != 0) {
  1325 	gint64 total;
  1326 	gint64 fps;
  1327 
  1328 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1329 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1330 #endif
  1331 	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
  1332 	GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
  1333 	res = FALSE;
  1334       break;
  1335     default:
  1336       res = FALSE;
  1337       break;
  1338   }
  1339 
  1340   gst_object_unref (myth);
  1341 
  1342   return res;
  1343 }
  1344 
  1345 static GstStateChangeReturn
  1346 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1347 {
  1348   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1349   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1350 
  1351   switch (transition) {
  1352     case GST_STATE_CHANGE_NULL_TO_READY:
  1353       //src->do_start = TRUE;
  1354       //src->unique_setup = FALSE;
  1355       break;
  1356     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1357     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1358       //src->eos = FALSE;
  1359       break;
  1360     default:
  1361       break;
  1362   }
  1363 
  1364   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1365   if (ret == GST_STATE_CHANGE_FAILURE)
  1366     return ret;
  1367 
  1368   switch (transition) {
  1369     case GST_STATE_CHANGE_READY_TO_NULL:
  1370       g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
  1371       break;
  1372     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1373       g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1374     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1375       g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1376       if ( src->live_tv && src->update_prog_chain ) {
  1377       	
  1378   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1379       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1380 
  1381 				src->read_offset = 0;
  1382 				src->bytes_read = 0;
  1383 				src->unique_setup = FALSE;				
  1384 				gst_mythtv_src_next_program_chain( src );
  1385       }
  1386       break;
  1387     default:
  1388       break;
  1389   }
  1390 
  1391   return ret;
  1392 }
  1393 
  1394 static void
  1395 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1396     const GValue * value, GParamSpec * pspec)
  1397 {
  1398   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1399 
  1400   GST_OBJECT_LOCK (mythtvsrc);
  1401   switch (prop_id) {
  1402     case PROP_URI:
  1403     case PROP_LOCATION:
  1404       {
  1405 	if (!g_value_get_string (value)) {
  1406 	  GST_WARNING ("location property cannot be NULL");
  1407 	  goto done;
  1408 	}
  1409 
  1410 	if (mythtvsrc->uri_name != NULL) {
  1411 	  g_free (mythtvsrc->uri_name);
  1412 	  mythtvsrc->uri_name = NULL;
  1413 	}
  1414 	mythtvsrc->uri_name = g_value_dup_string (value);
  1415 
  1416 	break;
  1417       }
  1418 #ifndef GST_DISABLE_GST_DEBUG
  1419     case PROP_GMYTHTV_DBG:
  1420       {
  1421 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1422 	break;
  1423       }
  1424 #endif
  1425     case PROP_GMYTHTV_VERSION:
  1426       {
  1427 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1428 	break;
  1429       }
  1430     case PROP_GMYTHTV_LIVEID:
  1431       {
  1432 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1433 	break;
  1434       }
  1435     case PROP_GMYTHTV_LIVE:
  1436       {
  1437 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1438 	break;
  1439       }
  1440     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1441       {
  1442 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1443 	break;
  1444       }      
  1445     case PROP_GMYTHTV_LIVE_CHAINID:
  1446       {
  1447 	if (!g_value_get_string (value)) {
  1448 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1449 	  goto done;
  1450 	}
  1451 
  1452 	if (mythtvsrc->live_chain_id != NULL) {
  1453 	  g_free (mythtvsrc->live_chain_id);
  1454 	  mythtvsrc->live_chain_id = NULL;
  1455 	}
  1456 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1457 	break;
  1458       }
  1459     case PROP_GMYTHTV_CHANNEL_NUM:
  1460       {
  1461 	mythtvsrc->channel_num = g_value_get_int (value);
  1462 	break;
  1463       }
  1464     default:
  1465       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1466       break;
  1467   }
  1468   GST_OBJECT_UNLOCK (mythtvsrc);
  1469 done:
  1470   return;
  1471 }
  1472 
  1473   static void
  1474 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1475     GValue * value, GParamSpec * pspec)
  1476 {
  1477   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1478 
  1479   GST_OBJECT_LOCK (mythtvsrc);
  1480   switch (prop_id) {
  1481     case PROP_URI:
  1482     case PROP_LOCATION:
  1483       {
  1484 	gchar *str = g_strdup( "" );
  1485 
  1486 	if ( mythtvsrc->uri_name == NULL ) {
  1487 	  g_free (mythtvsrc->uri_name);
  1488 	  mythtvsrc->uri_name = NULL;
  1489 	} else {
  1490 	  str = g_strdup( mythtvsrc->uri_name );
  1491 	}
  1492 	g_value_set_string ( value, str );
  1493 	break;
  1494       }
  1495 #ifndef GST_DISABLE_GST_DEBUG
  1496     case PROP_GMYTHTV_DBG:
  1497       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1498       break;
  1499 #endif
  1500     case PROP_GMYTHTV_VERSION:
  1501       {
  1502 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1503 	break;
  1504       }
  1505     case PROP_GMYTHTV_LIVEID:
  1506       {
  1507 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1508 	break;
  1509       }
  1510     case PROP_GMYTHTV_LIVE:
  1511       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1512       break;
  1513     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1514       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1515       break;
  1516     case PROP_GMYTHTV_LIVE_CHAINID:
  1517       {
  1518 	gchar *str = g_strdup( "" );
  1519 
  1520 	if ( mythtvsrc->live_chain_id == NULL ) {
  1521 	  g_free (mythtvsrc->live_chain_id);
  1522 	  mythtvsrc->live_chain_id = NULL;
  1523 	} else {
  1524 	  str = g_strdup( mythtvsrc->live_chain_id );
  1525 	}
  1526 	g_value_set_string ( value, str );
  1527 	break;
  1528       }
  1529     case PROP_GMYTHTV_CHANNEL_NUM:
  1530       {
  1531 	g_value_set_int ( value, mythtvsrc->channel_num );
  1532 	break;
  1533       }
  1534     default:
  1535       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1536       break;
  1537   }
  1538   GST_OBJECT_UNLOCK (mythtvsrc);
  1539 }
  1540 
  1541 /* entry point to initialize the plug-in
  1542  * initialize the plug-in itself
  1543  * register the element factories and pad templates
  1544  * register the features
  1545  */
  1546 static gboolean
  1547 plugin_init (GstPlugin * plugin)
  1548 {
  1549   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1550       GST_TYPE_MYTHTV_SRC);
  1551 }
  1552 
  1553 /* this is the structure that gst-register looks for
  1554  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1555 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1556     GST_VERSION_MINOR,
  1557     "mythtv",
  1558     "lib MythTV src",
  1559     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1560 
  1561 
  1562 /*** GSTURIHANDLER INTERFACE *************************************************/
  1563   static guint 
  1564 gst_mythtv_src_uri_get_type (void)
  1565 {
  1566   return GST_URI_SRC;
  1567 }
  1568 
  1569   static gchar **
  1570 gst_mythtv_src_uri_get_protocols (void)
  1571 {
  1572   static gchar *protocols[] = { "myth", "myths", NULL };
  1573 
  1574   return protocols;
  1575 }
  1576 
  1577   static const gchar *
  1578 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1579 {
  1580   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1581 
  1582   return src->uri_name;
  1583 }
  1584 
  1585   static gboolean
  1586 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1587 {
  1588   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1589 
  1590   gchar *protocol;
  1591 
  1592   protocol = gst_uri_get_protocol (uri);
  1593   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1594     g_free (protocol);
  1595     return FALSE;
  1596   }
  1597   g_free (protocol);
  1598   g_object_set (src, "location", uri, NULL);
  1599 
  1600   return TRUE;
  1601 }
  1602 
  1603  static void
  1604 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1605 {
  1606   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1607 
  1608   iface->get_type = gst_mythtv_src_uri_get_type;
  1609   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1610   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1611   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1612 }
  1613 
  1614   void
  1615 size_header_handler (void *userdata, const char *value)
  1616 {
  1617   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1618 
  1619   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1620 
  1621   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1622 }