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