gst-plugins-mythtv/src/gstmythtvsrc.c
author renatofilho
Thu Nov 30 17:49:08 2006 +0000 (2006-11-30)
branchtrunk
changeset 147 5095b85ff808
parent 124 d2d5fe1c3997
child 151 1799c8782451
permissions -rwxr-xr-x
[svn r148] code cleanup
     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	5
    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->th_read_ahead != NULL ) {
   306     gst_task_stop( this->th_read_ahead );
   307     this->th_read_ahead = NULL;
   308   }
   309 
   310   if (this->mythtv_caps) {
   311     gst_caps_unref (this->mythtv_caps);
   312     this->mythtv_caps = NULL;
   313   }
   314 
   315   if (this->file_transfer) {
   316     g_object_unref (this->file_transfer);
   317     this->file_transfer = NULL;
   318   }
   319 
   320   if (this->spawn_livetv) {
   321     g_object_unref (this->spawn_livetv);
   322     this->spawn_livetv = NULL;
   323   }
   324   
   325   if (this->backend_info) {
   326     g_object_unref (this->backend_info);
   327     this->backend_info = NULL;
   328   }
   329 
   330   if (this->uri_name) {
   331     g_free (this->uri_name);
   332   }
   333 
   334   if (this->user_agent) {
   335     g_free (this->user_agent);
   336   }
   337 
   338   if ( this->bytes_queue ) {
   339     g_byte_array_free( this->bytes_queue, TRUE );
   340     this->bytes_queue = NULL;
   341   }
   342 
   343   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   344 }
   345 
   346 static gint
   347 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
   348 {
   349   gint read = 0;
   350   guint sizetoread = size;
   351   gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
   352   
   353   GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   354 
   355   /* Loop sending the Myth File Transfer request:
   356    * Retry whilst authentication fails and we supply it. */
   357   gint len = 0;
   358   
   359   while ( sizetoread == size && --max_iters > 0) {
   360 
   361     len = gmyth_file_transfer_read( src->file_transfer,
   362 				data_ptr, sizetoread, TRUE );
   363 
   364     if ( len > 0 ) {
   365       read += len;
   366       sizetoread -= len;
   367     } 
   368     else if ( len < 0 )
   369     {
   370       read = -1;
   371 
   372       if ( src->live_tv == FALSE ) 
   373       {
   374 	goto eos;
   375       } 
   376       else  
   377       {
   378 	if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
   379 	  src->update_prog_chain = TRUE;
   380 	  goto done;	  	
   381 	} 
   382 	goto done;
   383       }
   384 
   385     } else if ( len == 0 )
   386     	goto done;
   387 
   388     if ( read == sizetoread )
   389       goto done;
   390   }
   391 
   392   if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
   393     goto eos;
   394 
   395   goto done;
   396 
   397 eos:
   398   src->eos = TRUE;
   399 
   400 done:
   401   return read;
   402 }
   403 
   404 static GstFlowReturn
   405 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
   406 {
   407   GstMythtvSrc *src;
   408   GstFlowReturn ret = GST_FLOW_OK;
   409   gint read = -1;
   410 
   411   src = GST_MYTHTV_SRC ( psrc );
   412 
   413   /* The caller should know the number of bytes and not read beyond EOS. */
   414   if (G_UNLIKELY (src->eos))
   415     goto eos;
   416   if ( G_UNLIKELY (src->update_prog_chain) )
   417     goto change_progchain;
   418 
   419   //g_static_rec_mutex_lock( &th_mutex );
   420   GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__, 
   421   		src->read_offset, MAX_READ_SIZE );
   422 
   423   GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__, 
   424       (gint) src->buffer_remain, src->bytes_queue->len );
   425 
   426   /* just get from the byte array, no network effort... */
   427   if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE  &&
   428   			abs( src->content_size - src->bytes_read ) > MAX_READ_SIZE ) { 
   429   			//( ( INTERNAL_BUFFER_SIZE - src->buffer_remain )  >=  REQUEST_MAX_SIZE ) ) {
   430   	GByteArray *buffer = NULL;
   431   	guint buffer_size_inter = ( INTERNAL_BUFFER_SIZE - src->buffer_remain );
   432   	if ( buffer_size_inter > REQUEST_MAX_SIZE )
   433   		buffer_size_inter = REQUEST_MAX_SIZE;
   434   		
   435   	buffer = g_byte_array_new();
   436 
   437     read = do_read_request_response( src, buffer_size_inter, buffer );
   438 
   439     if (G_UNLIKELY (read < 0)) {
   440       if ( src->live_tv )
   441 	goto change_progchain;
   442       else
   443 	goto read_error;
   444     } else if (G_UNLIKELY (read == 0) && !src->live_tv )
   445     	goto eos;
   446 
   447     if ( G_UNLIKELY (src->update_prog_chain) )
   448       goto change_progchain;
   449     
   450     src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
   451     if ( read > buffer_size_inter )
   452     	GST_WARNING_OBJECT( src, "\n\n\n[%s]\t\tINCREASED buffer size! Backend sent more than we ask him... (%d)\n\n\n", 
   453     			__FUNCTION__, abs( read - buffer_size_inter ) );
   454 
   455     src->buffer_remain += read;
   456 
   457     if ( buffer != NULL ) {
   458     	g_byte_array_free( buffer, TRUE );
   459     	buffer = NULL;    	
   460     }
   461 	
   462 	    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   463 		"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   464 		src->read_offset, src->content_size );
   465 	
   466   }
   467   
   468   guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   469   
   470   /* Create the buffer. */
   471   ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   472       src->read_offset, buffer_size,
   473       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
   474 
   475   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   476     if ( src->live_tv )
   477       goto change_progchain;
   478     else
   479       goto done;
   480   }
   481   
   482   /* gets the first buffer_size bytes from the byte array buffer variable */ 
   483   guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
   484 
   485   GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__, 
   486   	read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
   487 
   488   GST_BUFFER_SIZE (*outbuf) = buffer_size;
   489   GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   490   GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   491   g_memmove( GST_BUFFER_DATA( (*outbuf) ), buf, GST_BUFFER_SIZE(*outbuf) );
   492   GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
   493   GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
   494 
   495   src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
   496 
   497   src->read_offset += GST_BUFFER_SIZE (*outbuf);
   498   src->bytes_read += GST_BUFFER_SIZE (*outbuf);
   499   GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
   500   
   501   /* flushs the newly buffer got from byte array */
   502   src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
   503 
   504   GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   505       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   506       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   507 
   508   GST_DEBUG_OBJECT( src, "[%s]\tCONTENT_SIZE = %llu, BYTES_READ = %llu.\n", __FUNCTION__, 
   509   		src->content_size, src->bytes_read );
   510       
   511   if ( G_UNLIKELY (src->eos) || ( !src->live_tv && ( src->bytes_read >= src->content_size ) ) )
   512    goto eos;
   513 
   514 done:
   515   {
   516     const gchar *reason = gst_flow_get_name (ret);
   517 
   518     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   519     return ret;
   520   }
   521 eos:
   522   {
   523     const gchar *reason = gst_flow_get_name (ret);
   524 
   525     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   526     return GST_FLOW_UNEXPECTED;
   527   }
   528   /* ERRORS */
   529 read_error:
   530   {
   531     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   532 	(NULL), ("Could not read any bytes (%i, %s)", read,
   533 		 src->uri_name));
   534     return GST_FLOW_ERROR;
   535   }
   536 change_progchain:
   537   {
   538     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   539 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   540 		 src->uri_name));
   541 
   542     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   543 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   544     // go to the next program chain
   545     src->unique_setup = FALSE;
   546     src->update_prog_chain = TRUE;
   547 
   548     gst_mythtv_src_next_program_chain( src );
   549 
   550     return GST_FLOW_ERROR_NO_DATA;
   551   }
   552 
   553 }
   554 
   555 gint64
   556 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   557 {
   558 
   559   gint64 size_tmp = 0;
   560   guint max_tries = 2;
   561   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   562 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   563 
   564 get_file_pos:
   565     g_usleep( 10 );
   566     size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   567     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   568       src->content_size = size_tmp;
   569     else if ( size_tmp > 0 && --max_tries > 0 )
   570       goto get_file_pos;
   571     GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
   572 	__FUNCTION__, size_tmp );
   573     /* sets the last content size amount before it can be updated */
   574     src->prev_content_size = src->content_size;
   575   }
   576 
   577   return src->content_size;	
   578 
   579 }
   580 
   581 static gboolean
   582 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
   583 {
   584   GstMythtvSrc *src = GST_MYTHTV_SRC( base );
   585   gint64 new_offset = -1;
   586   gint64 actual_seek = segment->start;
   587   gboolean ret = TRUE;
   588 
   589   GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, 
   590   		segment->start, segment->stop );
   591 
   592   if ( segment->format == GST_FORMAT_TIME ) 
   593   {
   594     goto done; 
   595     //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
   596   }
   597   GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
   598   /* verify if it needs to seek */
   599   if ( src->read_offset != actual_seek )
   600   {
   601 
   602     new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
   603 
   604     GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   605 			__FUNCTION__, segment->start, src->read_offset, new_offset );
   606     if ( G_UNLIKELY (new_offset < 0 ) )
   607     {
   608       ret = FALSE;
   609       if ( src->live_tv )
   610 	goto change_progchain;
   611       else
   612 	goto eos;
   613     }
   614 
   615     src->read_offset = new_offset;
   616 
   617     if ( ret == FALSE ) {
   618       GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
   619     }
   620 
   621   }
   622 
   623 done:
   624    return ret;
   625 
   626 eos:
   627   {
   628 
   629     GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
   630     return FALSE;
   631   }
   632 change_progchain:
   633   {
   634     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   635 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   636 		 src->uri_name));
   637 
   638     gst_pad_push_event ( GST_BASE_SRC_PAD (base),
   639 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   640     /* go to the next program chain */
   641     src->unique_setup = FALSE;
   642     src->update_prog_chain = TRUE;
   643 
   644     gst_mythtv_src_next_program_chain( src );
   645 
   646     return TRUE;
   647   }
   648 
   649 }
   650 
   651 /* create a socket for connecting to remote server */
   652 static gboolean
   653 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   654 {
   655   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   656 
   657   GString *chain_id_local = NULL;
   658 
   659   gboolean ret = TRUE;
   660 
   661   if ( G_UNLIKELY (src->update_prog_chain) )
   662     goto change_progchain;
   663 
   664   if (src->unique_setup == FALSE) {
   665     src->unique_setup = TRUE;
   666   } else {
   667     goto done;
   668   }
   669 
   670   if ( src->live_tv ) {
   671   	src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
   672     src->spawn_livetv = gmyth_livetv_new( );
   673     if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
   674       ret = FALSE;
   675       goto init_failed;
   676     }    
   677 
   678     /* set up the uri variable */
   679     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   680     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   681     if ( chain_id_local != NULL ) {
   682       src->live_chain_id = g_strdup( chain_id_local->str );
   683       GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );      
   684     }
   685     //if ( src->uri_name == NULL || strlen( src->uri_name ) <= 0 )
   686     //	gmyth_context_initialize();
   687     	
   688     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   689     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
   690   }
   691 
   692   //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   693   //    g_string_new( src->uri_name ), -1, src->mythtv_version );
   694 	src->file_transfer = gmyth_file_transfer_new( );
   695 
   696   if ( src->file_transfer == NULL ) {
   697     goto init_failed;
   698   }
   699   GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->uri_name );
   700 
   701 	ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
   702 
   703   /* sets the Playback monitor connection */
   704   //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   705 
   706   if ( src->live_tv == TRUE && ret == TRUE ) {
   707     /* loop finished, set the max tries variable to zero again... */
   708     wait_to_transfer = 0;
   709 
   710     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   711 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   712       g_usleep( 500 );
   713       
   714     /* IS_RECORDING again, just like the MythTV backend does... */
   715 	  gmyth_livetv_is_recording( src->spawn_livetv );
   716 	  
   717 	  sleep( 3 );  
   718 
   719   }
   720   /* sets the FileTransfer instance connection (video/audio download) */
   721   //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   722 
   723   if ( ret == FALSE ) {
   724 #ifndef GST_DISABLE_GST_DEBUG  
   725     if ( src->mythtv_msgs_dbg )
   726       GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   727 #endif
   728     goto begin_req_failed;
   729   }
   730 
   731   src->content_size = src->file_transfer->filesize;
   732 
   733   src->do_start = FALSE;
   734   
   735   /* this is used for the buffer cache */
   736   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   737   src->buffer_remain = 0;
   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, src->content_size, 0 ) );
   741 
   742 done:
   743   return TRUE;
   744 
   745   /* ERRORS */
   746 init_failed:
   747   {
   748     if (src->spawn_livetv != NULL )
   749       g_object_unref( src->spawn_livetv );
   750 
   751     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   752 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   753     return FALSE;
   754   }
   755 begin_req_failed:
   756   {
   757     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   758 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   759     return FALSE;
   760   }
   761 change_progchain:
   762   {
   763     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   764 	(NULL), ("Seek failed, go to the next program info... (%s)",
   765 		 src->uri_name));
   766 
   767     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   768 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   769 
   770     // go to the next program chain
   771     src->unique_setup = FALSE;
   772     src->update_prog_chain = TRUE;
   773 
   774     gst_mythtv_src_next_program_chain( src );
   775 
   776     return TRUE;
   777   }
   778 }
   779 
   780 /* create a new socket for connecting to the next program chain */
   781 static gboolean
   782 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
   783 {
   784   GString *chain_id_local = NULL;
   785 
   786   gboolean ret = TRUE;
   787 
   788   if ( !src->live_tv )
   789     goto init_failed;
   790     
   791   if (src->unique_setup == FALSE) {
   792     src->unique_setup = TRUE;
   793   } else {
   794     goto done;
   795   }
   796   
   797 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   798 
   799   if (src->file_transfer) {
   800     g_object_unref (src->file_transfer);
   801     src->file_transfer = NULL;
   802   }
   803 
   804   if (src->uri_name) {
   805     g_free (src->uri_name);
   806   }
   807 
   808   if ( src->live_tv ) {
   809     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
   810     	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
   811       ret = FALSE;
   812       goto init_failed;
   813     }
   814     /* set up the uri variable */
   815     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   816     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   817     if ( chain_id_local != NULL ) {
   818       src->live_chain_id = g_strdup( chain_id_local->str );
   819       GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   820     }
   821     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   822     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   823   }
   824 
   825   src->file_transfer = gmyth_file_transfer_new();
   826 
   827   if ( src->file_transfer == NULL ) {
   828     goto init_failed;
   829   }
   830   
   831 	ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
   832 
   833   /* sets the Playback monitor connection */
   834   //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   835 
   836   if ( src->live_tv == TRUE && ret == TRUE ) {
   837     /* loop finished, set the max tries variable to zero again... */
   838     wait_to_transfer = 0;
   839 
   840     g_usleep( 200 );
   841 
   842     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
   843 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   844       g_usleep( 1000 );
   845   }
   846 
   847   /* sets the FileTransfer instance connection (video/audio download) */
   848   //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   849 
   850   if ( ret == FALSE ) {
   851 #ifndef GST_DISABLE_GST_DEBUG  
   852     if ( src->mythtv_msgs_dbg )
   853       GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   854 #endif
   855     goto begin_req_failed;
   856   }
   857   src->content_size_last = src->content_size;
   858 
   859   src->content_size = src->file_transfer->filesize;
   860   if ( src->live_tv ) {
   861   	wait_to_transfer = 0;
   862 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
   863 	    src->content_size = gst_mythtv_src_get_position( src );
   864   }
   865 
   866   src->read_offset = 0;  
   867   
   868   if ( src->bytes_queue != NULL ) {
   869   	g_byte_array_free( src->bytes_queue, TRUE );
   870   }
   871   
   872   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   873   
   874   //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   875 	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
   876   
   877 done:
   878 	src->update_prog_chain = FALSE;
   879 	
   880 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   881 	
   882   return TRUE;
   883 
   884   /* ERRORS */
   885 init_failed:
   886   {
   887     if (src->spawn_livetv != NULL )
   888       g_object_unref( src->spawn_livetv );
   889 
   890     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   891 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   892     return FALSE;
   893   }
   894 begin_req_failed:
   895   {
   896     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   897 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   898     return FALSE;
   899   }
   900 
   901 }
   902 
   903 static gboolean
   904 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   905 {
   906   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   907   gboolean ret = TRUE;
   908   GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
   909   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
   910   			
   911   if ( src->live_tv ) {
   912   	ret = FALSE;
   913 	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
   914 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   915     
   916     gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   917     if ( new_offset > 0 && new_offset > src->content_size ) {
   918 			src->content_size = new_offset;
   919     } else if ( new_offset < src->content_size ) {
   920 			src->update_prog_chain = TRUE;
   921 	  }
   922 
   923   }
   924 
   925   *size = src->content_size;
   926   GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
   927   
   928   return ret;
   929 
   930 }
   931 
   932 /* close the socket and associated resources
   933  * used both to recover from errors and go to NULL state */
   934 static gboolean
   935 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   936 {
   937   GstMythtvSrc *src;
   938 
   939   src = GST_MYTHTV_SRC (bsrc);
   940 
   941   if (src->uri_name) {
   942     g_free (src->uri_name);
   943     src->uri_name = NULL;
   944   }
   945 
   946   if (src->mythtv_caps) {
   947     gst_caps_unref (src->mythtv_caps);
   948     src->mythtv_caps = NULL;
   949   }
   950 
   951   src->eos = FALSE;
   952 
   953   return TRUE;
   954 }
   955 
   956 static gboolean
   957 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   958 {
   959   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   960   gint64 cont_size = 0;
   961   gboolean ret = FALSE;
   962 
   963   switch (GST_EVENT_TYPE (event)) {
   964     case GST_EVENT_EOS:
   965       GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
   966 
   967       if ( src->live_tv ) {
   968 	cont_size = gst_mythtv_src_get_position (src);
   969 	if ( cont_size > src->content_size ) {
   970 	  src->content_size = cont_size;
   971 	  src->eos = FALSE;
   972 	} else {
   973 	  src->eos = TRUE;
   974 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   975 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   976 	}
   977       }
   978       break;
   979     default:
   980       ret = gst_pad_event_default (pad, event);
   981   }
   982 
   983   return ret;
   984 }
   985 
   986 static gboolean
   987 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
   988 {
   989   return TRUE;
   990 }
   991 
   992 static gboolean
   993 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
   994 {
   995   gboolean res = FALSE;
   996   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
   997   GstFormat formt;
   998 
   999   switch (GST_QUERY_TYPE (query)) {
  1000     case GST_QUERY_POSITION:
  1001     {
  1002     	gint64 pos = -1;
  1003       gst_query_parse_position (query, &formt, &pos );
  1004       res = TRUE;
  1005       if ( formt == GST_FORMAT_BYTES ) {
  1006       	gst_query_set_position (query, formt, pos = myth->read_offset );
  1007       	GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
  1008       } else if ( formt == GST_FORMAT_TIME ) {
  1009       	GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
  1010       	res = gst_pad_query_default(pad, query);
  1011       }
  1012       break;
  1013     }
  1014     case GST_QUERY_DURATION:
  1015     {
  1016 #if 0
  1017       if (myth->duration != 0) {
  1018 	gint64 total;
  1019 	gint64 fps;
  1020 
  1021 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1022 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1023       }
  1024 #endif
  1025 	gint64 dur = -1;
  1026 	gst_query_parse_duration ( query, &formt, &dur );	
  1027   if ( formt == GST_FORMAT_BYTES ) {
  1028   	gst_query_set_duration (query, formt, dur = myth->content_size);
  1029   	GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
  1030   } else if ( formt == GST_FORMAT_TIME ) {
  1031   	GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
  1032   	gst_query_ref(query);
  1033   	res = gst_pad_query_default(pad, query);  	
  1034   }
  1035 	res = TRUE;
  1036       break;
  1037     }
  1038     default:
  1039     {
  1040       res = gst_pad_query_default(pad, query);
  1041       break;
  1042     }
  1043   }
  1044 
  1045   gst_object_unref (myth);
  1046 
  1047   return res;
  1048 }
  1049 
  1050 static GstStateChangeReturn
  1051 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1052 {
  1053   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1054   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1055 
  1056   switch (transition) {
  1057     case GST_STATE_CHANGE_NULL_TO_READY:
  1058       //src->do_start = TRUE;
  1059       //src->unique_setup = FALSE;
  1060       break;
  1061     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1062     	GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ );
  1063     	break;
  1064     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1065     	GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ );
  1066       if ( src->live_tv ) {
  1067 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1068 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1069 	      else
  1070 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1071       }
  1072 
  1073       break;
  1074     default:
  1075       break;
  1076   }
  1077 
  1078   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1079   if (ret == GST_STATE_CHANGE_FAILURE)
  1080     return ret;
  1081 
  1082   switch (transition) {
  1083     case GST_STATE_CHANGE_READY_TO_NULL:
  1084       GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
  1085       break;
  1086     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1087       GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1088     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1089       GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1090       if ( src->live_tv ) {
  1091 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1092 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1093 	      else
  1094 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1095       	
  1096       /*
  1097       	
  1098   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1099       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1100 
  1101 				src->read_offset = 0;
  1102 				src->bytes_read = 0;
  1103 				src->unique_setup = FALSE;				
  1104 				gst_mythtv_src_next_program_chain( src );
  1105 				*/
  1106       }
  1107       
  1108       break;
  1109     default:
  1110       break;
  1111   }
  1112 
  1113   return ret;
  1114 }
  1115 
  1116 static void
  1117 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1118     const GValue * value, GParamSpec * pspec)
  1119 {
  1120   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1121 
  1122   GST_OBJECT_LOCK (mythtvsrc);
  1123   switch (prop_id) {
  1124     case PROP_URI:
  1125     case PROP_LOCATION:
  1126       {
  1127 	if (!g_value_get_string (value)) {
  1128 	  GST_WARNING ("location property cannot be NULL");
  1129 	  goto done;
  1130 	}
  1131 
  1132 	if (mythtvsrc->uri_name != NULL) {
  1133 	  g_free (mythtvsrc->uri_name);
  1134 	  mythtvsrc->uri_name = NULL;
  1135 	}
  1136 	mythtvsrc->uri_name = g_value_dup_string (value);
  1137 
  1138 	break;
  1139       }
  1140 #ifndef GST_DISABLE_GST_DEBUG
  1141     case PROP_GMYTHTV_DBG:
  1142       {
  1143 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1144 	break;
  1145       }
  1146 #endif
  1147     case PROP_GMYTHTV_VERSION:
  1148       {
  1149 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1150 	break;
  1151       }
  1152     case PROP_GMYTHTV_LIVEID:
  1153       {
  1154 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1155 	break;
  1156       }
  1157     case PROP_GMYTHTV_LIVE:
  1158       {
  1159 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1160 	break;
  1161       }
  1162     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1163       {
  1164 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1165 	break;
  1166       }      
  1167     case PROP_GMYTHTV_LIVE_CHAINID:
  1168       {
  1169 	if (!g_value_get_string (value)) {
  1170 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1171 	  goto done;
  1172 	}
  1173 
  1174 	if (mythtvsrc->live_chain_id != NULL) {
  1175 	  g_free (mythtvsrc->live_chain_id);
  1176 	  mythtvsrc->live_chain_id = NULL;
  1177 	}
  1178 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1179 	break;
  1180       }
  1181     case PROP_GMYTHTV_CHANNEL_NUM:
  1182       {
  1183 	mythtvsrc->channel_num = g_value_get_int (value);
  1184 	break;
  1185       }
  1186     default:
  1187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1188       break;
  1189   }
  1190   GST_OBJECT_UNLOCK (mythtvsrc);
  1191 done:
  1192   return;
  1193 }
  1194 
  1195 static void
  1196 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1197     GValue * value, GParamSpec * pspec)
  1198 {
  1199   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1200 
  1201   GST_OBJECT_LOCK (mythtvsrc);
  1202   switch (prop_id) {
  1203     case PROP_URI:
  1204     case PROP_LOCATION:
  1205       {
  1206 	gchar *str = g_strdup( "" );
  1207 
  1208 	if ( mythtvsrc->uri_name == NULL ) {
  1209 	  g_free (mythtvsrc->uri_name);
  1210 	  mythtvsrc->uri_name = NULL;
  1211 	} else {
  1212 	  str = g_strdup( mythtvsrc->uri_name );
  1213 	}
  1214 	g_value_set_string ( value, str );
  1215 	break;
  1216       }
  1217 #ifndef GST_DISABLE_GST_DEBUG
  1218     case PROP_GMYTHTV_DBG:
  1219       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1220       break;
  1221 #endif
  1222     case PROP_GMYTHTV_VERSION:
  1223       {
  1224 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1225 	break;
  1226       }
  1227     case PROP_GMYTHTV_LIVEID:
  1228       {
  1229 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1230 	break;
  1231       }
  1232     case PROP_GMYTHTV_LIVE:
  1233       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1234       break;
  1235     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1236       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1237       break;
  1238     case PROP_GMYTHTV_LIVE_CHAINID:
  1239       {
  1240 	gchar *str = g_strdup( "" );
  1241 
  1242 	if ( mythtvsrc->live_chain_id == NULL ) {
  1243 	  g_free (mythtvsrc->live_chain_id);
  1244 	  mythtvsrc->live_chain_id = NULL;
  1245 	} else {
  1246 	  str = g_strdup( mythtvsrc->live_chain_id );
  1247 	}
  1248 	g_value_set_string ( value, str );
  1249 	break;
  1250       }
  1251     case PROP_GMYTHTV_CHANNEL_NUM:
  1252       {
  1253 	g_value_set_int ( value, mythtvsrc->channel_num );
  1254 	break;
  1255       }
  1256     default:
  1257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1258       break;
  1259   }
  1260   GST_OBJECT_UNLOCK (mythtvsrc);
  1261 }
  1262 
  1263 /* entry point to initialize the plug-in
  1264  * initialize the plug-in itself
  1265  * register the element factories and pad templates
  1266  * register the features
  1267  */
  1268 static gboolean
  1269 plugin_init (GstPlugin * plugin)
  1270 {
  1271   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1272       GST_TYPE_MYTHTV_SRC);
  1273 }
  1274 
  1275 /* this is the structure that gst-register looks for
  1276  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1277 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1278     GST_VERSION_MINOR,
  1279     "mythtv",
  1280     "lib MythTV src",
  1281     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1282 
  1283 
  1284 /*** GSTURIHANDLER INTERFACE *************************************************/
  1285   static guint 
  1286 gst_mythtv_src_uri_get_type (void)
  1287 {
  1288   return GST_URI_SRC;
  1289 }
  1290 
  1291   static gchar **
  1292 gst_mythtv_src_uri_get_protocols (void)
  1293 {
  1294   static gchar *protocols[] = { "myth", "myths", NULL };
  1295 
  1296   return protocols;
  1297 }
  1298 
  1299   static const gchar *
  1300 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1301 {
  1302   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1303 
  1304   return src->uri_name;
  1305 }
  1306 
  1307   static gboolean
  1308 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1309 {
  1310   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1311 
  1312   gchar *protocol;
  1313 
  1314   protocol = gst_uri_get_protocol (uri);
  1315   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1316     g_free (protocol);
  1317     return FALSE;
  1318   }
  1319   g_free (protocol);
  1320   g_object_set (src, "location", uri, NULL);
  1321 
  1322   return TRUE;
  1323 }
  1324 
  1325  static void
  1326 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1327 {
  1328   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1329 
  1330   iface->get_type = gst_mythtv_src_uri_get_type;
  1331   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1332   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1333   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1334 }
  1335 
  1336   void
  1337 size_header_handler (void *userdata, const char *value)
  1338 {
  1339   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1340 
  1341   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1342 
  1343   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1344 }