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