gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Fri Nov 17 18:18:24 2006 +0000 (2006-11-17)
branchtrunk
changeset 94 da398435732b
parent 93 ae73b4837eed
child 95 51d013028674
permissions -rwxr-xr-x
[svn r95] Minor fixes on the GstAdapter (cache buffer).
     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              		4*1024
    46 //( 32*1024 )
    47 
    48 #define GST_FLOW_ERROR_NO_DATA  		-101
    49 
    50 #define INTERNAL_BUFFER_SIZE			28*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, guint8 **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, guint8 **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   g_print ( "[%s] %d - Adapter size = (%d)...\n", __FUNCTION__, max_adapter_rep, adapter_size );
   576 
   577   while ( src->adapter != NULL && ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < MAX_READ_SIZE ) &&
   578       --max_adapter_rep > 0 )
   579   {
   580     g_print ( "[%s] %d - Adapter size = (%d)...\n", __FUNCTION__, max_adapter_rep, adapter_size );
   581     //GST_TASK_WAIT( src->th_read_ahead );
   582   }
   583 
   584   g_print ( "[%s] %d - Adapter size = (%d)...\n", __FUNCTION__, max_adapter_rep, adapter_size );
   585 
   586   //gst_task_pause( src->th_read_ahead );
   587 
   588   //g_static_rec_mutex_unlock( &th_mutex );
   589 
   590   /* just get from the adapter, no network effort... */
   591   if ( FALSE )	
   592   {
   593     //g_static_rec_mutex_lock( &th_mutex );
   594 
   595     *outbuf = gst_adapter_take_buffer( src->adapter, adapter_size );
   596     // src->read_offset += size;
   597     read = adapter_size;
   598 
   599     gst_adapter_flush( src->adapter, adapter_size );
   600     //g_static_rec_mutex_unlock( &th_mutex );
   601 
   602   } else { 
   603     GstBuffer *buffer;
   604     guint size = (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   605 
   606     g_print ( "[%s]\t\tCreate: buffer_offset: %d, buffer_remain: %d\n", __FUNCTION__, 
   607 	(gint) src->buffer_offset,
   608 	(gint) src->buffer_remain);
   609 
   610     /* Create the buffer. */
   611     ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   612 	src->read_offset /*GST_BUFFER_OFFSET_NONE*/, size,
   613 	GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
   614 
   615     if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   616       if ( src->live_tv )
   617 	goto change_progchain;
   618       else
   619 	goto done;
   620     }
   621 
   622     if ( ( src->buffer_remain = gst_adapter_available_fast( src->adapter ) ) < MAX_READ_SIZE ) {
   623    
   624       buffer = gst_buffer_new_and_alloc( INTERNAL_BUFFER_SIZE - src->buffer_remain );
   625 
   626       read = do_read_request_response( src, 0, INTERNAL_BUFFER_SIZE - src->buffer_remain, &(GST_BUFFER_DATA(buffer)) );
   627 
   628       if (G_UNLIKELY (read < 0)) {
   629 	if ( src->live_tv )
   630 	  goto change_progchain;
   631 	else
   632 	  goto read_error;
   633       }
   634 
   635       if ( G_UNLIKELY (src->update_prog_chain) )
   636 	goto change_progchain;
   637 
   638       gst_adapter_push( src->adapter, buffer );
   639 
   640       src->buffer_offset = 0;
   641       src->buffer_remain = src->buffer_remain + read;
   642 
   643       g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   644 	  "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   645 	  src->read_offset, src->content_size );
   646 
   647     }
   648 
   649     size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   650 
   651     buffer = gst_adapter_take_buffer( src->adapter, size );
   652 
   653     g_print( "[%s] read = %d, buffer_remain = %d\n", __FUNCTION__, read, src->buffer_remain );
   654 
   655     GST_BUFFER_SIZE (*outbuf) = size;
   656     GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   657     GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   658     g_memmove( GST_BUFFER_DATA( (*outbuf) ), GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(*outbuf) );
   659     GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
   660     GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
   661 
   662     src->buffer_offset += GST_BUFFER_SIZE (*outbuf);
   663     src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
   664 
   665     src->read_offset += GST_BUFFER_SIZE (*outbuf);
   666     src->bytes_read += GST_BUFFER_SIZE (*outbuf);
   667     g_print ( "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
   668 
   669     //gst_adapter_flush( src->adapter, size );
   670     gst_buffer_unref( buffer );
   671 
   672    g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   673 	"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   674 	GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   675 
   676     /* just get from the adapter, no network effort... */
   677 
   678   }
   679 
   680   return ret;
   681 
   682   //g_static_rec_mutex_lock( &th_mutex );
   683   
   684   //GST_TASK_SIGNAL( src->th_read_ahead );
   685 
   686   //g_static_rec_mutex_unlock( &th_mutex );
   687   
   688 done:
   689   {
   690     const gchar *reason = gst_flow_get_name (ret);
   691 
   692     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   693     return ret;
   694   }
   695 eos:
   696   {
   697     const gchar *reason = gst_flow_get_name (ret);
   698 
   699     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   700     return GST_FLOW_UNEXPECTED;
   701   }
   702   /* ERRORS */
   703 read_error:
   704   {
   705     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   706 	(NULL), ("Could not read any bytes (%i, %s)", read,
   707 		 src->uri_name));
   708     return GST_FLOW_ERROR;
   709   }
   710 change_progchain:
   711   {
   712     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   713 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   714 		 src->uri_name));
   715 
   716     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   717 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   718     // go to the next program chain
   719     src->unique_setup = FALSE;
   720     src->update_prog_chain = TRUE;
   721 
   722     gst_mythtv_src_next_program_chain( src );
   723 
   724     return GST_FLOW_ERROR_NO_DATA;
   725   }
   726 
   727 }
   728 
   729 gint64
   730 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   731 {
   732 
   733   gint64 size_tmp = 0;
   734   guint max_tries = 2;
   735   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   736 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   737 
   738 get_file_pos:
   739     g_usleep( 10 );
   740     size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   741     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   742       src->content_size = size_tmp;
   743     else if ( size_tmp > 0 && --max_tries > 0 )
   744       goto get_file_pos;
   745     g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   746 	__FUNCTION__, size_tmp );
   747     /* sets the last content size amount before it can be updated */
   748     src->prev_content_size = src->content_size;
   749   }
   750 
   751   return src->content_size;	
   752 
   753 }
   754 
   755 static gboolean
   756 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
   757 {
   758   GstMythtvSrc *src = GST_MYTHTV_SRC( base );
   759   gint64 new_offset = -1;
   760   gint64 actual_seek = segment->start;
   761   gboolean ret = TRUE;
   762 
   763   g_print( "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, segment->start, segment->stop );
   764   //g_static_rec_mutex_lock( &th_mutex );
   765 
   766   //GST_TASK_WAIT( src->th_read_ahead );
   767 
   768   //g_static_rec_mutex_unlock( &th_mutex );
   769 
   770   if ( segment->format == GST_FORMAT_TIME ) 
   771   {
   772     goto done; 
   773     //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
   774   }
   775   g_print( "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
   776   /* verify if it needs to seek */
   777   if ( src->read_offset != actual_seek )
   778   {
   779 
   780     new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
   781 
   782     g_print( "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   783 	__FUNCTION__, segment->start, src->read_offset, new_offset );
   784     if ( G_UNLIKELY (new_offset < 0 ) )
   785     {
   786       ret = FALSE;
   787       if ( src->live_tv )
   788 	goto change_progchain;
   789       else
   790 	goto eos;
   791     }
   792 
   793     src->read_offset = new_offset;
   794 
   795     //gst_segment_set_seek( segment, segment->rate, GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE, 
   796     //GST_SEEK_TYPE_CUR, src->read_offset, GST_SEEK_TYPE_CUR, segment->stop, &ret );
   797 
   798     if ( ret == FALSE ) {
   799       g_print( "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
   800     }
   801 
   802   }
   803    //g_static_rec_mutex_lock( &th_mutex );
   804 
   805    //GST_TASK_SIGNAL( src->th_read_ahead );
   806 
   807    //g_static_rec_mutex_unlock( &th_mutex );
   808 done:
   809    return ret;
   810 
   811 eos:
   812   {
   813 
   814     GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
   815     //gst_object_unref( src );
   816     return FALSE;
   817   }
   818 change_progchain:
   819   {
   820     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   821 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   822 		 src->uri_name));
   823 
   824     gst_pad_push_event ( GST_BASE_SRC_PAD (base),
   825 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   826     /* go to the next program chain */
   827     src->unique_setup = FALSE;
   828     src->update_prog_chain = TRUE;
   829 
   830     gst_mythtv_src_next_program_chain( src );
   831 
   832     return TRUE;
   833   }
   834 
   835 }
   836 
   837 #if 0
   838 static void 
   839 gst_mythtv_src_read_ahead ( void *data ) {
   840 
   841   GstMythtvSrc *src = NULL;
   842 
   843   GstBuffer *outbuf = NULL;
   844 
   845   guint size = 5*2048;
   846   gint total = 0;
   847   gint read = -1;
   848 
   849   src = GST_MYTHTV_SRC( data );
   850 
   851   //GST_PAD_STREAM_TRYLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   852 
   853   do {
   854     GST_TASK_WAIT( src->th_read_ahead );
   855 
   856     gint8 *data = NULL;
   857     
   858     outbuf = gst_buffer_new_and_alloc( size );
   859 
   860     read = do_read_request_response ( src, src->adapter_offset, size, &data );
   861 
   862     if ( read > 0 ) {
   863       src->read_offset += read;
   864       src->bytes_read += read;
   865       total += read;
   866 
   867       g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   868 	  "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   869 	  src->read_offset, src->content_size );
   870 
   871       GST_BUFFER_SIZE (outbuf) = read;
   872       GST_BUFFER_MALLOCDATA( outbuf ) = g_malloc0( GST_BUFFER_SIZE (outbuf) );
   873       GST_BUFFER_DATA( outbuf ) = GST_BUFFER_MALLOCDATA( outbuf );
   874       g_memmove( GST_BUFFER_DATA( outbuf ), data, read );
   875       GST_BUFFER_OFFSET (outbuf) = src->adapter_offset;  
   876       GST_BUFFER_OFFSET_END (outbuf) = src->adapter_offset + read;
   877       g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   878 	  "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (outbuf), 
   879 	  GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf) );
   880 
   881     } 
   882 
   883     gst_adapter_push( src->adapter, outbuf );
   884 
   885     GST_TASK_SIGNAL( src->th_read_ahead );
   886 
   887   } while ( read < size );
   888 
   889   //GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   890 
   891   return;	
   892 }
   893 #endif
   894 
   895 /* create a socket for connecting to remote server */
   896 static gboolean
   897 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   898 {
   899   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   900 
   901   GString *chain_id_local = NULL;
   902 
   903   gboolean ret = TRUE;
   904 
   905   if ( G_UNLIKELY (src->update_prog_chain) )
   906     goto change_progchain;
   907 
   908   if (src->unique_setup == FALSE) {
   909     src->unique_setup = TRUE;
   910   } else {
   911     goto done;
   912   }
   913 
   914   if ( src->live_tv ) {
   915     src->spawn_livetv = gmyth_livetv_new( );
   916     if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   917       ret = FALSE;
   918       goto init_failed;
   919     }    
   920 
   921     /* set up the uri variable */
   922     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   923     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   924     if ( chain_id_local != NULL ) {
   925       src->live_chain_id = g_strdup( chain_id_local->str );
   926       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   927     }
   928     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   929     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
   930   }
   931 
   932   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   933       g_string_new( src->uri_name ), -1, src->mythtv_version );
   934 
   935   if ( src->file_transfer == NULL ) {
   936     goto init_failed;
   937   }
   938 
   939   /* sets the Playback monitor connection */
   940   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   941 
   942   if ( src->live_tv == TRUE && ret == TRUE ) {
   943     /* loop finished, set the max tries variable to zero again... */
   944     wait_to_transfer = 0;
   945 
   946     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   947 	( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   948 	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   949       g_usleep( 100 );
   950   }
   951 
   952   /* sets the FileTransfer instance connection (video/audio download) */
   953   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   954 
   955   if ( ret == FALSE ) {
   956 #ifndef GST_DISABLE_GST_DEBUG  
   957     if ( src->mythtv_msgs_dbg )
   958       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   959 #endif
   960     goto begin_req_failed;
   961   }
   962 
   963   src->content_size = src->file_transfer->filesize;
   964 
   965   src->do_start = FALSE;
   966 
   967   //if ( src->live_tv ) {
   968   src->adapter = gst_adapter_new();
   969   //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
   970   //gst_task_set_lock( src->th_read_ahead, &th_mutex );
   971   //gst_task_start( src->th_read_ahead );
   972   // }
   973   //gst_pad_push_event ( GST_BASE_SRC_PAD (bsrc),
   974   //	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, src->content_size, 0 ) );
   975 
   976   src->buffer = g_malloc0 (INTERNAL_BUFFER_SIZE);
   977   src->buffer_offset = 0;
   978   src->buffer_remain = 0;
   979 
   980 
   981 done:
   982   return TRUE;
   983 
   984   /* ERRORS */
   985 init_failed:
   986   {
   987     if (src->spawn_livetv != NULL )
   988       g_object_unref( src->spawn_livetv );
   989 
   990     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   991 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   992     return FALSE;
   993   }
   994 begin_req_failed:
   995   {
   996     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   997 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   998     return FALSE;
   999   }
  1000 change_progchain:
  1001   {
  1002     GST_ELEMENT_ERROR (src, RESOURCE, READ,
  1003 	(NULL), ("Seek failed, go to the next program info... (%s)",
  1004 		 src->uri_name));
  1005 
  1006     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1007 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1008 
  1009     // go to the next program chain
  1010     src->unique_setup = FALSE;
  1011     src->update_prog_chain = TRUE;
  1012 
  1013     gst_mythtv_src_next_program_chain( src );
  1014 
  1015     return TRUE;
  1016   }
  1017 }
  1018 
  1019 /* create a new socket for connecting to the next program chain */
  1020 static gboolean
  1021 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
  1022 {
  1023   GString *chain_id_local = NULL;
  1024 
  1025   gboolean ret = TRUE;
  1026 
  1027   if ( !src->live_tv )
  1028     goto init_failed;
  1029     
  1030   if (src->unique_setup == FALSE) {
  1031     src->unique_setup = TRUE;
  1032   } else {
  1033     goto done;
  1034   }
  1035   
  1036 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
  1037 
  1038   if (src->file_transfer) {
  1039     g_object_unref (src->file_transfer);
  1040     src->file_transfer = NULL;
  1041   }
  1042 
  1043   if (src->uri_name) {
  1044     g_free (src->uri_name);
  1045   }
  1046 
  1047   if ( src->live_tv ) {
  1048     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
  1049     	g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
  1050       ret = FALSE;
  1051       goto init_failed;
  1052     }
  1053     /* set up the uri variable */
  1054     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
  1055     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
  1056     if ( chain_id_local != NULL ) {
  1057       src->live_chain_id = g_strdup( chain_id_local->str );
  1058       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
  1059     }
  1060     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
  1061     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
  1062   }
  1063 
  1064   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
  1065       g_string_new( src->uri_name ), -1, src->mythtv_version );
  1066 
  1067   if ( src->file_transfer == NULL ) {
  1068     goto init_failed;
  1069   }
  1070 
  1071   /* sets the Playback monitor connection */
  1072   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
  1073 
  1074   if ( src->live_tv == TRUE && ret == TRUE ) {
  1075     /* loop finished, set the max tries variable to zero again... */
  1076     wait_to_transfer = 0;
  1077 
  1078     g_usleep( 200 );
  1079 
  1080     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
  1081 				( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
  1082       g_usleep( 1000 );
  1083   }
  1084 
  1085   /* sets the FileTransfer instance connection (video/audio download) */
  1086   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
  1087 
  1088   if ( ret == FALSE ) {
  1089 #ifndef GST_DISABLE_GST_DEBUG  
  1090     if ( src->mythtv_msgs_dbg )
  1091       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
  1092 #endif
  1093     goto begin_req_failed;
  1094   }
  1095   src->content_size_last = src->content_size;
  1096 
  1097 #if 0
  1098   if ( src->content_size < src->file_transfer->filesize ) {
  1099     src->content_size = src->file_transfer->filesize;
  1100   } else {
  1101     //gint64 pos = gst_mythtv_src_get_position(src);
  1102     //if ( pos > src->file_transfer->filesize )
  1103     //	src->content_size = pos;  	
  1104 
  1105   }
  1106 #endif
  1107 
  1108   src->content_size = src->file_transfer->filesize;
  1109   if ( src->live_tv ) {
  1110   	wait_to_transfer = 0;
  1111 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
  1112 	    src->content_size = gst_mythtv_src_get_position( src );
  1113   }
  1114 
  1115   src->read_offset = 0;  
  1116   
  1117 done:
  1118 	src->update_prog_chain = FALSE;
  1119 	
  1120 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
  1121 	
  1122   return TRUE;
  1123 
  1124   /* ERRORS */
  1125 init_failed:
  1126   {
  1127     if (src->spawn_livetv != NULL )
  1128       g_object_unref( src->spawn_livetv );
  1129 
  1130     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
  1131 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
  1132     return FALSE;
  1133   }
  1134 begin_req_failed:
  1135   {
  1136     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
  1137 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
  1138     return FALSE;
  1139   }
  1140 
  1141 }
  1142 
  1143 static gboolean
  1144 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
  1145 {
  1146   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
  1147   gboolean ret = TRUE;
  1148   g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
  1149   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
  1150 
  1151   if (src->content_size == -1) {
  1152     //ret= FALSE;
  1153   } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) < 
  1154 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
  1155     //g_static_mutex_lock( &update_size_mutex );
  1156     //GST_OBJECT_LOCK(src);
  1157     
  1158     gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
  1159     if ( new_offset > 0 && new_offset > src->content_size ) {
  1160 			src->content_size = new_offset;
  1161     } else if ( new_offset < src->content_size ) {
  1162 			src->update_prog_chain = TRUE;
  1163 	  }
  1164 
  1165     if ( src->enable_timing_position ) {
  1166       gint64 size_tmp = 0;
  1167       if (src->live_tv == TRUE) {
  1168 get_file_pos:
  1169 	g_usleep( 5 );
  1170 	size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
  1171 	if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
  1172 	  src->content_size = size_tmp;
  1173 	else if ( size_tmp > 0  )
  1174 	  goto get_file_pos;
  1175 	g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
  1176 	    __FUNCTION__, size_tmp );
  1177       }
  1178     }
  1179     
  1180     src->prev_content_size = src->content_size;
  1181     
  1182     //GST_OBJECT_UNLOCK(src);
  1183     //g_static_mutex_unlock( &update_size_mutex );
  1184   }
  1185 
  1186   *size = src->content_size;
  1187   g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
  1188   
  1189   return ret;
  1190 
  1191 }
  1192 
  1193 /* close the socket and associated resources
  1194  * used both to recover from errors and go to NULL state */
  1195 static gboolean
  1196 gst_mythtv_src_stop (GstBaseSrc * bsrc)
  1197 {
  1198   GstMythtvSrc *src;
  1199 
  1200   src = GST_MYTHTV_SRC (bsrc);
  1201 
  1202   if (src->uri_name) {
  1203     g_free (src->uri_name);
  1204     src->uri_name = NULL;
  1205   }
  1206 
  1207   if (src->mythtv_caps) {
  1208     gst_caps_unref (src->mythtv_caps);
  1209     src->mythtv_caps = NULL;
  1210   }
  1211 
  1212   src->eos = FALSE;
  1213 
  1214   return TRUE;
  1215 }
  1216 
  1217 static gboolean
  1218 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
  1219 {
  1220   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
  1221   gint64 cont_size = 0;
  1222   gboolean ret = FALSE;
  1223 
  1224   switch (GST_EVENT_TYPE (event)) {
  1225 #if 0
  1226     case GST_EVENT_FLUSH_START:
  1227       //src->eos = FALSE;
  1228       g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
  1229       cont_size = gst_mythtv_src_get_position (src);
  1230       if ( !src->live_tv ) {
  1231 	if ( cont_size > src->content_size ) {
  1232 	  src->content_size = cont_size;
  1233 	  src->eos = FALSE;
  1234 	} else {
  1235 	  src->eos = TRUE;
  1236 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1237 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1238 	}
  1239       } else {
  1240 	if ( cont_size <= 0 ) {
  1241 	  src->update_prog_chain = TRUE;
  1242 	  src->eos = TRUE;
  1243 	  src->unique_setup = FALSE;
  1244 	  src->do_start = TRUE;		  				  		
  1245 	}		  	
  1246       }
  1247       break;
  1248     case GST_EVENT_FLUSH_STOP:
  1249       src->do_start = TRUE;
  1250       src->eos = FALSE;
  1251       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
  1252       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
  1253       break;
  1254 #endif
  1255     case GST_EVENT_EOS:
  1256       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
  1257 
  1258       if ( src->live_tv ) {
  1259 	cont_size = gst_mythtv_src_get_position (src);
  1260 	if ( cont_size > src->content_size ) {
  1261 	  src->content_size = cont_size;
  1262 	  src->eos = FALSE;
  1263 	} else {
  1264 	  src->eos = TRUE;
  1265 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1266 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1267 	}
  1268       } else 
  1269 	src->eos = TRUE;
  1270       ret = TRUE;
  1271       break;
  1272     case GST_EVENT_NEWSEGMENT:
  1273       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
  1274       ret = gst_pad_event_default (pad, event);
  1275       break;
  1276     case GST_EVENT_SEEK:  	  
  1277       {
  1278 	gst_event_ref( event );
  1279 
  1280 	gdouble rate;
  1281 	//gboolean update = TRUE;
  1282 	GstFormat format;
  1283 	GstSeekType cur_type, stop_type;
  1284 	GstSeekFlags flags;
  1285 	gint64 cur = 0, stop = 0;
  1286 	gst_event_parse_seek ( event, &rate, &format,
  1287 	    &flags, &cur_type, &cur,
  1288 	    &stop_type, &stop );
  1289 
  1290 	g_print( "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
  1291 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
  1292 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
  1293 	}
  1294 	if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
  1295 	  gst_event_unref( event );
  1296 	  break;
  1297 	}
  1298 	
  1299 	break;
  1300       }
  1301     default:
  1302       ret = gst_pad_event_default (pad, event);
  1303   }
  1304 
  1305   return ret;
  1306 }
  1307 
  1308 static gboolean
  1309 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
  1310 {
  1311   return TRUE;
  1312 }
  1313 
  1314 static gboolean
  1315 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
  1316 {
  1317   gboolean res = FALSE;
  1318   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
  1319 
  1320   switch (GST_QUERY_TYPE (query)) {
  1321     case GST_QUERY_POSITION:
  1322       gst_query_set_position (query, GST_FORMAT_BYTES,
  1323 	  myth->read_offset );
  1324       res = TRUE;
  1325       GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
  1326       break;
  1327     case GST_QUERY_DURATION:
  1328 #if 0
  1329       if (myth->duration != 0) {
  1330 	gint64 total;
  1331 	gint64 fps;
  1332 
  1333 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1334 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1335 #endif
  1336 	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
  1337 	GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
  1338 	res = FALSE;
  1339       break;
  1340     default:
  1341       res = FALSE;
  1342       break;
  1343   }
  1344 
  1345   gst_object_unref (myth);
  1346 
  1347   return res;
  1348 }
  1349 
  1350 static GstStateChangeReturn
  1351 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1352 {
  1353   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1354   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1355 
  1356   switch (transition) {
  1357     case GST_STATE_CHANGE_NULL_TO_READY:
  1358       //src->do_start = TRUE;
  1359       //src->unique_setup = FALSE;
  1360       break;
  1361     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1362     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1363       //src->eos = FALSE;
  1364       break;
  1365     default:
  1366       break;
  1367   }
  1368 
  1369   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1370   if (ret == GST_STATE_CHANGE_FAILURE)
  1371     return ret;
  1372 
  1373   switch (transition) {
  1374     case GST_STATE_CHANGE_READY_TO_NULL:
  1375       g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
  1376       break;
  1377     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1378       g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1379     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1380       g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1381       if ( src->live_tv && src->update_prog_chain ) {
  1382       	
  1383   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1384       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1385 
  1386 				src->read_offset = 0;
  1387 				src->bytes_read = 0;
  1388 				src->unique_setup = FALSE;				
  1389 				gst_mythtv_src_next_program_chain( src );
  1390       }
  1391       break;
  1392     default:
  1393       break;
  1394   }
  1395 
  1396   return ret;
  1397 }
  1398 
  1399 static void
  1400 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1401     const GValue * value, GParamSpec * pspec)
  1402 {
  1403   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1404 
  1405   GST_OBJECT_LOCK (mythtvsrc);
  1406   switch (prop_id) {
  1407     case PROP_URI:
  1408     case PROP_LOCATION:
  1409       {
  1410 	if (!g_value_get_string (value)) {
  1411 	  GST_WARNING ("location property cannot be NULL");
  1412 	  goto done;
  1413 	}
  1414 
  1415 	if (mythtvsrc->uri_name != NULL) {
  1416 	  g_free (mythtvsrc->uri_name);
  1417 	  mythtvsrc->uri_name = NULL;
  1418 	}
  1419 	mythtvsrc->uri_name = g_value_dup_string (value);
  1420 
  1421 	break;
  1422       }
  1423 #ifndef GST_DISABLE_GST_DEBUG
  1424     case PROP_GMYTHTV_DBG:
  1425       {
  1426 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1427 	break;
  1428       }
  1429 #endif
  1430     case PROP_GMYTHTV_VERSION:
  1431       {
  1432 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1433 	break;
  1434       }
  1435     case PROP_GMYTHTV_LIVEID:
  1436       {
  1437 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1438 	break;
  1439       }
  1440     case PROP_GMYTHTV_LIVE:
  1441       {
  1442 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1443 	break;
  1444       }
  1445     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1446       {
  1447 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1448 	break;
  1449       }      
  1450     case PROP_GMYTHTV_LIVE_CHAINID:
  1451       {
  1452 	if (!g_value_get_string (value)) {
  1453 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1454 	  goto done;
  1455 	}
  1456 
  1457 	if (mythtvsrc->live_chain_id != NULL) {
  1458 	  g_free (mythtvsrc->live_chain_id);
  1459 	  mythtvsrc->live_chain_id = NULL;
  1460 	}
  1461 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1462 	break;
  1463       }
  1464     case PROP_GMYTHTV_CHANNEL_NUM:
  1465       {
  1466 	mythtvsrc->channel_num = g_value_get_int (value);
  1467 	break;
  1468       }
  1469     default:
  1470       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1471       break;
  1472   }
  1473   GST_OBJECT_UNLOCK (mythtvsrc);
  1474 done:
  1475   return;
  1476 }
  1477 
  1478   static void
  1479 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1480     GValue * value, GParamSpec * pspec)
  1481 {
  1482   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1483 
  1484   GST_OBJECT_LOCK (mythtvsrc);
  1485   switch (prop_id) {
  1486     case PROP_URI:
  1487     case PROP_LOCATION:
  1488       {
  1489 	gchar *str = g_strdup( "" );
  1490 
  1491 	if ( mythtvsrc->uri_name == NULL ) {
  1492 	  g_free (mythtvsrc->uri_name);
  1493 	  mythtvsrc->uri_name = NULL;
  1494 	} else {
  1495 	  str = g_strdup( mythtvsrc->uri_name );
  1496 	}
  1497 	g_value_set_string ( value, str );
  1498 	break;
  1499       }
  1500 #ifndef GST_DISABLE_GST_DEBUG
  1501     case PROP_GMYTHTV_DBG:
  1502       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1503       break;
  1504 #endif
  1505     case PROP_GMYTHTV_VERSION:
  1506       {
  1507 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1508 	break;
  1509       }
  1510     case PROP_GMYTHTV_LIVEID:
  1511       {
  1512 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1513 	break;
  1514       }
  1515     case PROP_GMYTHTV_LIVE:
  1516       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1517       break;
  1518     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1519       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1520       break;
  1521     case PROP_GMYTHTV_LIVE_CHAINID:
  1522       {
  1523 	gchar *str = g_strdup( "" );
  1524 
  1525 	if ( mythtvsrc->live_chain_id == NULL ) {
  1526 	  g_free (mythtvsrc->live_chain_id);
  1527 	  mythtvsrc->live_chain_id = NULL;
  1528 	} else {
  1529 	  str = g_strdup( mythtvsrc->live_chain_id );
  1530 	}
  1531 	g_value_set_string ( value, str );
  1532 	break;
  1533       }
  1534     case PROP_GMYTHTV_CHANNEL_NUM:
  1535       {
  1536 	g_value_set_int ( value, mythtvsrc->channel_num );
  1537 	break;
  1538       }
  1539     default:
  1540       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1541       break;
  1542   }
  1543   GST_OBJECT_UNLOCK (mythtvsrc);
  1544 }
  1545 
  1546 /* entry point to initialize the plug-in
  1547  * initialize the plug-in itself
  1548  * register the element factories and pad templates
  1549  * register the features
  1550  */
  1551 static gboolean
  1552 plugin_init (GstPlugin * plugin)
  1553 {
  1554   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1555       GST_TYPE_MYTHTV_SRC);
  1556 }
  1557 
  1558 /* this is the structure that gst-register looks for
  1559  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1560 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1561     GST_VERSION_MINOR,
  1562     "mythtv",
  1563     "lib MythTV src",
  1564     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1565 
  1566 
  1567 /*** GSTURIHANDLER INTERFACE *************************************************/
  1568   static guint 
  1569 gst_mythtv_src_uri_get_type (void)
  1570 {
  1571   return GST_URI_SRC;
  1572 }
  1573 
  1574   static gchar **
  1575 gst_mythtv_src_uri_get_protocols (void)
  1576 {
  1577   static gchar *protocols[] = { "myth", "myths", NULL };
  1578 
  1579   return protocols;
  1580 }
  1581 
  1582   static const gchar *
  1583 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1584 {
  1585   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1586 
  1587   return src->uri_name;
  1588 }
  1589 
  1590   static gboolean
  1591 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1592 {
  1593   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1594 
  1595   gchar *protocol;
  1596 
  1597   protocol = gst_uri_get_protocol (uri);
  1598   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1599     g_free (protocol);
  1600     return FALSE;
  1601   }
  1602   g_free (protocol);
  1603   g_object_set (src, "location", uri, NULL);
  1604 
  1605   return TRUE;
  1606 }
  1607 
  1608  static void
  1609 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1610 {
  1611   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1612 
  1613   iface->get_type = gst_mythtv_src_uri_get_type;
  1614   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1615   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1616   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1617 }
  1618 
  1619   void
  1620 size_header_handler (void *userdata, const char *value)
  1621 {
  1622   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1623 
  1624   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1625 
  1626   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1627 }