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