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