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