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