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