gst-plugins-mythtv/src/gstmythtvsrc.c
author melunko
Fri Nov 24 21:19:49 2006 +0000 (2006-11-24)
branchtrunk
changeset 111 dfa72795bd32
parent 108 f2b53549deef
child 114 04cead6ae39b
permissions -rwxr-xr-x
[svn r112] Added cache to request more data from the backend by time
     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					40*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_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   678       g_usleep( 500 );
   679       
   680     /* IS_RECORDING again, just like the MythTV backend does... */
   681 	  gmyth_livetv_is_recording( src->spawn_livetv );
   682 	  
   683 	  sleep( 5 );  
   684 
   685   }
   686   /* sets the FileTransfer instance connection (video/audio download) */
   687   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   688 
   689   if ( ret == FALSE ) {
   690 #ifndef GST_DISABLE_GST_DEBUG  
   691     if ( src->mythtv_msgs_dbg )
   692       GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   693 #endif
   694     goto begin_req_failed;
   695   }
   696 
   697   src->content_size = src->file_transfer->filesize;
   698 
   699   src->do_start = FALSE;
   700 
   701   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   702   //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
   703   //gst_task_set_lock( src->th_read_ahead, &th_mutex );
   704   //gst_task_start( src->th_read_ahead );
   705   src->buffer_remain = 0;
   706 
   707 done:
   708   return TRUE;
   709 
   710   /* ERRORS */
   711 init_failed:
   712   {
   713     if (src->spawn_livetv != NULL )
   714       g_object_unref( src->spawn_livetv );
   715 
   716     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   717 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   718     return FALSE;
   719   }
   720 begin_req_failed:
   721   {
   722     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   723 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   724     return FALSE;
   725   }
   726 change_progchain:
   727   {
   728     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   729 	(NULL), ("Seek failed, go to the next program info... (%s)",
   730 		 src->uri_name));
   731 
   732     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   733 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   734 
   735     // go to the next program chain
   736     src->unique_setup = FALSE;
   737     src->update_prog_chain = TRUE;
   738 
   739     gst_mythtv_src_next_program_chain( src );
   740 
   741     return TRUE;
   742   }
   743 }
   744 
   745 /* create a new socket for connecting to the next program chain */
   746 static gboolean
   747 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
   748 {
   749   GString *chain_id_local = NULL;
   750 
   751   gboolean ret = TRUE;
   752 
   753   if ( !src->live_tv )
   754     goto init_failed;
   755     
   756   if (src->unique_setup == FALSE) {
   757     src->unique_setup = TRUE;
   758   } else {
   759     goto done;
   760   }
   761   
   762 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   763 
   764   if (src->file_transfer) {
   765     g_object_unref (src->file_transfer);
   766     src->file_transfer = NULL;
   767   }
   768 
   769   if (src->uri_name) {
   770     g_free (src->uri_name);
   771   }
   772 
   773   if ( src->live_tv ) {
   774     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
   775     	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
   776       ret = FALSE;
   777       goto init_failed;
   778     }
   779     /* set up the uri variable */
   780     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   781     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   782     if ( chain_id_local != NULL ) {
   783       src->live_chain_id = g_strdup( chain_id_local->str );
   784       GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   785     }
   786     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   787     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   788   }
   789 
   790   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   791       g_string_new( src->uri_name ), -1, src->mythtv_version );
   792 
   793   if ( src->file_transfer == NULL ) {
   794     goto init_failed;
   795   }
   796 
   797   /* sets the Playback monitor connection */
   798   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   799 
   800   if ( src->live_tv == TRUE && ret == TRUE ) {
   801     /* loop finished, set the max tries variable to zero again... */
   802     wait_to_transfer = 0;
   803 
   804     g_usleep( 200 );
   805 
   806     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
   807 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   808       g_usleep( 1000 );
   809   }
   810 
   811   /* sets the FileTransfer instance connection (video/audio download) */
   812   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   813 
   814   if ( ret == FALSE ) {
   815 #ifndef GST_DISABLE_GST_DEBUG  
   816     if ( src->mythtv_msgs_dbg )
   817       GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   818 #endif
   819     goto begin_req_failed;
   820   }
   821   src->content_size_last = src->content_size;
   822 
   823   src->content_size = src->file_transfer->filesize;
   824   if ( src->live_tv ) {
   825   	wait_to_transfer = 0;
   826 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
   827 	    src->content_size = gst_mythtv_src_get_position( src );
   828   }
   829 
   830   src->read_offset = 0;  
   831   
   832   if ( src->bytes_queue != NULL ) {
   833   	g_byte_array_free( src->bytes_queue, TRUE );
   834   }
   835   
   836   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   837   
   838 done:
   839 	src->update_prog_chain = FALSE;
   840 	
   841 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   842 	
   843   return TRUE;
   844 
   845   /* ERRORS */
   846 init_failed:
   847   {
   848     if (src->spawn_livetv != NULL )
   849       g_object_unref( src->spawn_livetv );
   850 
   851     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   852 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   853     return FALSE;
   854   }
   855 begin_req_failed:
   856   {
   857     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   858 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   859     return FALSE;
   860   }
   861 
   862 }
   863 
   864 static gboolean
   865 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   866 {
   867   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   868   gboolean ret = TRUE;
   869   GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
   870   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
   871   			
   872   if ( src->live_tv ) {
   873   	ret = FALSE;
   874 	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
   875 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   876     
   877     gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
   878     if ( new_offset > 0 && new_offset > src->content_size ) {
   879 			src->content_size = new_offset;
   880     } else if ( new_offset < src->content_size ) {
   881 			src->update_prog_chain = TRUE;
   882 	  }
   883 
   884   }
   885 
   886   *size = src->content_size;
   887   GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
   888   
   889   return ret;
   890 
   891 }
   892 
   893 /* close the socket and associated resources
   894  * used both to recover from errors and go to NULL state */
   895 static gboolean
   896 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   897 {
   898   GstMythtvSrc *src;
   899 
   900   src = GST_MYTHTV_SRC (bsrc);
   901 
   902   if (src->uri_name) {
   903     g_free (src->uri_name);
   904     src->uri_name = NULL;
   905   }
   906 
   907   if (src->mythtv_caps) {
   908     gst_caps_unref (src->mythtv_caps);
   909     src->mythtv_caps = NULL;
   910   }
   911 
   912   src->eos = FALSE;
   913 
   914   return TRUE;
   915 }
   916 
   917 static gboolean
   918 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   919 {
   920   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   921   gint64 cont_size = 0;
   922   gboolean ret = FALSE;
   923 
   924   switch (GST_EVENT_TYPE (event)) {
   925     case GST_EVENT_EOS:
   926       GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
   927 
   928       if ( src->live_tv ) {
   929 	cont_size = gst_mythtv_src_get_position (src);
   930 	if ( cont_size > src->content_size ) {
   931 	  src->content_size = cont_size;
   932 	  src->eos = FALSE;
   933 	} else {
   934 	  src->eos = TRUE;
   935 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   936 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   937 	}
   938       } else 
   939 	src->eos = TRUE;
   940       ret = TRUE;
   941       break;
   942     case GST_EVENT_NEWSEGMENT:
   943       GST_INFO_OBJECT( src, "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
   944       ret = gst_pad_event_default (pad, event);
   945       break;
   946     case GST_EVENT_SEEK:  	  
   947       {
   948 	gst_event_ref( event );
   949 
   950 	gdouble rate;
   951 	GstFormat format;
   952 	GstSeekType cur_type, stop_type;
   953 	GstSeekFlags flags;
   954 	gint64 cur = 0, stop = 0;
   955 	gst_event_parse_seek ( event, &rate, &format,
   956 	    &flags, &cur_type, &cur,
   957 	    &stop_type, &stop );
   958 
   959 	GST_INFO_OBJECT( src, "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
   960 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   961 	  GST_WARNING_OBJECT( src, "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   962 	}
   963 	if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
   964 	  gst_event_unref( event );
   965 	  break;
   966 	}
   967 	
   968 	break;
   969       }
   970     default:
   971       ret = gst_pad_event_default (pad, event);
   972   }
   973 
   974   return ret;
   975 }
   976 
   977 static gboolean
   978 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
   979 {
   980   return TRUE;
   981 }
   982 
   983 static gboolean
   984 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
   985 {
   986   gboolean res = FALSE;
   987   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
   988 
   989   switch (GST_QUERY_TYPE (query)) {
   990     case GST_QUERY_POSITION:
   991       gst_query_set_position (query, GST_FORMAT_BYTES,
   992 	  myth->read_offset );
   993       res = TRUE;
   994       GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
   995       break;
   996     case GST_QUERY_DURATION:
   997 #if 0
   998       if (myth->duration != 0) {
   999 	gint64 total;
  1000 	gint64 fps;
  1001 
  1002 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1003 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1004 #endif
  1005 	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
  1006 	GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
  1007 	res = FALSE;
  1008       break;
  1009     default:
  1010       res = FALSE;
  1011       break;
  1012   }
  1013 
  1014   gst_object_unref (myth);
  1015 
  1016   return res;
  1017 }
  1018 
  1019 static GstStateChangeReturn
  1020 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1021 {
  1022   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1023   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1024 
  1025   switch (transition) {
  1026     case GST_STATE_CHANGE_NULL_TO_READY:
  1027       //src->do_start = TRUE;
  1028       //src->unique_setup = FALSE;
  1029       break;
  1030     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1031     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1032       //src->eos = FALSE;
  1033       break;
  1034     default:
  1035       break;
  1036   }
  1037 
  1038   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1039   if (ret == GST_STATE_CHANGE_FAILURE)
  1040     return ret;
  1041 
  1042   switch (transition) {
  1043     case GST_STATE_CHANGE_READY_TO_NULL:
  1044       GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
  1045       break;
  1046     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1047       GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1048     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1049       GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1050       if ( src->live_tv && src->update_prog_chain ) {
  1051 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1052 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1053 	      else
  1054 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1055       	
  1056       /*
  1057       	
  1058   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1059       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1060 
  1061 				src->read_offset = 0;
  1062 				src->bytes_read = 0;
  1063 				src->unique_setup = FALSE;				
  1064 				gst_mythtv_src_next_program_chain( src );
  1065 				*/
  1066       }
  1067       
  1068       break;
  1069     default:
  1070       break;
  1071   }
  1072 
  1073   return ret;
  1074 }
  1075 
  1076 static void
  1077 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1078     const GValue * value, GParamSpec * pspec)
  1079 {
  1080   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1081 
  1082   GST_OBJECT_LOCK (mythtvsrc);
  1083   switch (prop_id) {
  1084     case PROP_URI:
  1085     case PROP_LOCATION:
  1086       {
  1087 	if (!g_value_get_string (value)) {
  1088 	  GST_WARNING ("location property cannot be NULL");
  1089 	  goto done;
  1090 	}
  1091 
  1092 	if (mythtvsrc->uri_name != NULL) {
  1093 	  g_free (mythtvsrc->uri_name);
  1094 	  mythtvsrc->uri_name = NULL;
  1095 	}
  1096 	mythtvsrc->uri_name = g_value_dup_string (value);
  1097 
  1098 	break;
  1099       }
  1100 #ifndef GST_DISABLE_GST_DEBUG
  1101     case PROP_GMYTHTV_DBG:
  1102       {
  1103 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1104 	break;
  1105       }
  1106 #endif
  1107     case PROP_GMYTHTV_VERSION:
  1108       {
  1109 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1110 	break;
  1111       }
  1112     case PROP_GMYTHTV_LIVEID:
  1113       {
  1114 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1115 	break;
  1116       }
  1117     case PROP_GMYTHTV_LIVE:
  1118       {
  1119 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1120 	break;
  1121       }
  1122     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1123       {
  1124 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1125 	break;
  1126       }      
  1127     case PROP_GMYTHTV_LIVE_CHAINID:
  1128       {
  1129 	if (!g_value_get_string (value)) {
  1130 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1131 	  goto done;
  1132 	}
  1133 
  1134 	if (mythtvsrc->live_chain_id != NULL) {
  1135 	  g_free (mythtvsrc->live_chain_id);
  1136 	  mythtvsrc->live_chain_id = NULL;
  1137 	}
  1138 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1139 	break;
  1140       }
  1141     case PROP_GMYTHTV_CHANNEL_NUM:
  1142       {
  1143 	mythtvsrc->channel_num = g_value_get_int (value);
  1144 	break;
  1145       }
  1146     default:
  1147       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1148       break;
  1149   }
  1150   GST_OBJECT_UNLOCK (mythtvsrc);
  1151 done:
  1152   return;
  1153 }
  1154 
  1155 static void
  1156 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1157     GValue * value, GParamSpec * pspec)
  1158 {
  1159   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1160 
  1161   GST_OBJECT_LOCK (mythtvsrc);
  1162   switch (prop_id) {
  1163     case PROP_URI:
  1164     case PROP_LOCATION:
  1165       {
  1166 	gchar *str = g_strdup( "" );
  1167 
  1168 	if ( mythtvsrc->uri_name == NULL ) {
  1169 	  g_free (mythtvsrc->uri_name);
  1170 	  mythtvsrc->uri_name = NULL;
  1171 	} else {
  1172 	  str = g_strdup( mythtvsrc->uri_name );
  1173 	}
  1174 	g_value_set_string ( value, str );
  1175 	break;
  1176       }
  1177 #ifndef GST_DISABLE_GST_DEBUG
  1178     case PROP_GMYTHTV_DBG:
  1179       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1180       break;
  1181 #endif
  1182     case PROP_GMYTHTV_VERSION:
  1183       {
  1184 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1185 	break;
  1186       }
  1187     case PROP_GMYTHTV_LIVEID:
  1188       {
  1189 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1190 	break;
  1191       }
  1192     case PROP_GMYTHTV_LIVE:
  1193       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1194       break;
  1195     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1196       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1197       break;
  1198     case PROP_GMYTHTV_LIVE_CHAINID:
  1199       {
  1200 	gchar *str = g_strdup( "" );
  1201 
  1202 	if ( mythtvsrc->live_chain_id == NULL ) {
  1203 	  g_free (mythtvsrc->live_chain_id);
  1204 	  mythtvsrc->live_chain_id = NULL;
  1205 	} else {
  1206 	  str = g_strdup( mythtvsrc->live_chain_id );
  1207 	}
  1208 	g_value_set_string ( value, str );
  1209 	break;
  1210       }
  1211     case PROP_GMYTHTV_CHANNEL_NUM:
  1212       {
  1213 	g_value_set_int ( value, mythtvsrc->channel_num );
  1214 	break;
  1215       }
  1216     default:
  1217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1218       break;
  1219   }
  1220   GST_OBJECT_UNLOCK (mythtvsrc);
  1221 }
  1222 
  1223 /* entry point to initialize the plug-in
  1224  * initialize the plug-in itself
  1225  * register the element factories and pad templates
  1226  * register the features
  1227  */
  1228 static gboolean
  1229 plugin_init (GstPlugin * plugin)
  1230 {
  1231   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1232       GST_TYPE_MYTHTV_SRC);
  1233 }
  1234 
  1235 /* this is the structure that gst-register looks for
  1236  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1237 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1238     GST_VERSION_MINOR,
  1239     "mythtv",
  1240     "lib MythTV src",
  1241     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1242 
  1243 
  1244 /*** GSTURIHANDLER INTERFACE *************************************************/
  1245   static guint 
  1246 gst_mythtv_src_uri_get_type (void)
  1247 {
  1248   return GST_URI_SRC;
  1249 }
  1250 
  1251   static gchar **
  1252 gst_mythtv_src_uri_get_protocols (void)
  1253 {
  1254   static gchar *protocols[] = { "myth", "myths", NULL };
  1255 
  1256   return protocols;
  1257 }
  1258 
  1259   static const gchar *
  1260 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1261 {
  1262   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1263 
  1264   return src->uri_name;
  1265 }
  1266 
  1267   static gboolean
  1268 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1269 {
  1270   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1271 
  1272   gchar *protocol;
  1273 
  1274   protocol = gst_uri_get_protocol (uri);
  1275   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1276     g_free (protocol);
  1277     return FALSE;
  1278   }
  1279   g_free (protocol);
  1280   g_object_set (src, "location", uri, NULL);
  1281 
  1282   return TRUE;
  1283 }
  1284 
  1285  static void
  1286 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1287 {
  1288   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1289 
  1290   iface->get_type = gst_mythtv_src_uri_get_type;
  1291   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1292   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1293   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1294 }
  1295 
  1296   void
  1297 size_header_handler (void *userdata, const char *value)
  1298 {
  1299   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1300 
  1301   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1302 
  1303   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1304 }