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