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