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