gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Thu Oct 12 01:50:27 2006 +0100 (2006-10-12)
branchtrunk
changeset 30 abe0ee48d78b
parent 13 f3cdc7844178
child 31 eb4a812d4073
permissions -rwxr-xr-x
[svn r31] Some changes, in order to use the newly added nuvdemux features.
     1 /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
     2 /* GStreamer MythTV Plug-in
     3  * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
     4  *
     5  * This library is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU Library Lesser General 
     7  * Public License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more 
    14  */
    15 
    16 #ifdef HAVE_CONFIG_H
    17 #include "config.h"
    18 #endif
    19 
    20 #include "gstmythtvsrc.h"
    21 #include "myth_file_transfer.h"
    22 #include "myth_livetv.h"
    23 
    24 #include <gmyth/gmyth_socket.h>
    25 #include <gmyth/gmyth_tvchain.h>
    26 
    27 #include <string.h>
    28 #include <unistd.h>
    29 
    30 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
    31 #define GST_CAT_DEFAULT mythtvsrc_debug
    32 
    33 #define GST_MYTHTV_ID_NUM		1
    34 
    35 #define MYTHTV_VERSION_DEFAULT		30
    36 
    37 #define MYTHTV_TRANSFER_MAX_WAITS	100
    38 
    39 #define MYTHTV_TRANSFER_MAX_BUFFER	32*1024
    40 //( 32*1024  )
    41 
    42 /* 4*1024 ??? */
    43 #define MAX_READ_SIZE              	12*1024
    44 //( 32*1024 )
    45 
    46 #define ENABLE_TIMING_POSITION		1
    47 
    48 /* stablish a maximum iteration value to the IS_RECORDING message */
    49 static guint wait_to_transfer = 0;
    50 
    51 static const GstElementDetails gst_mythtv_src_details =
    52 GST_ELEMENT_DETAILS ( "MythTV client source",
    53     "Source/Network",
    54     "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
    55     "Rosfran Borges <rosfran.borges@indt.org.br>" );
    56 
    57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    58     GST_PAD_SRC,
    59     GST_PAD_ALWAYS,
    60     GST_STATIC_CAPS ("video/x-nuv") );
    61     //GST_STATIC_CAPS_ANY);
    62 
    63 static GThread *update_size_task = NULL;
    64 
    65 static GStaticMutex update_size_mutex = G_STATIC_MUTEX_INIT;
    66 
    67 enum
    68 {
    69   PROP_0,
    70   PROP_LOCATION,
    71   PROP_URI,
    72 #ifndef GST_DISABLE_GST_DEBUG
    73   PROP_MYTHTV_DBG,
    74 #endif
    75   PROP_MYTHTV_VERSION,
    76   PROP_MYTHTV_LIVE,
    77   PROP_MYTHTV_LIVEID,
    78   PROP_MYTHTV_LIVE_CHAINID
    79 };
    80 
    81 static void gst_mythtv_src_finalize (GObject * gobject);
    82 
    83 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
    84 	guint size, GstBuffer ** outbuf);
    85 //static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
    86 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
    87 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
    88 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
    89 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
    90 //static gboolean gst_mythtv_new_segment ( GstBaseSrc * psrc );
    91 
    92 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
    93     const GValue * value, GParamSpec * pspec);
    94 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
    95     GValue * value, GParamSpec * pspec);
    96 
    97 //static GstFlowReturn gst_mythtv_src_chain (GstPad * pad, GstBuffer * buf);
    98 
    99 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
   100 
   101 //static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
   102 //static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
   103 
   104 static guint do_read_request_response (GstMythtvSrc *src, guint64 offset, 
   105 		guint size, GstBuffer **outbuf);
   106 //static gboolean gst_mythtv_src_sink_activate_pull (GstPad * srcpad, gboolean active);
   107 
   108   static void
   109 _urihandler_init (GType type)
   110 {
   111   static const GInterfaceInfo urihandler_info = {
   112     gst_mythtv_src_uri_handler_init,
   113     NULL,
   114     NULL
   115   };
   116 
   117   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
   118 
   119   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   120       "MythTV src");
   121 }
   122 
   123 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
   124     GST_TYPE_BASE_SRC, _urihandler_init)
   125     
   126 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
   127 //    GST_TYPE_PUSH_SRC, _urihandler_init)
   128     
   129   static void
   130 gst_mythtv_src_base_init (gpointer g_class)
   131 {
   132   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   133 
   134   gst_element_class_add_pad_template (element_class,
   135       gst_static_pad_template_get (&srctemplate));
   136 
   137   gst_element_class_set_details (element_class, &gst_mythtv_src_details);
   138 }
   139 
   140 static void
   141 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
   142 {
   143   GObjectClass *gobject_class;
   144   //GstPushSrcClass *gstpushsrc_class;
   145   GstBaseSrcClass *gstbasesrc_class;
   146 
   147   gobject_class = (GObjectClass *) klass;
   148   gstbasesrc_class = (GstBaseSrcClass *) klass;
   149   //gstpushsrc_class = (GstPushSrcClass *) klass;
   150 
   151   gobject_class->set_property = gst_mythtv_src_set_property;
   152   gobject_class->get_property = gst_mythtv_src_get_property;
   153   gobject_class->finalize = gst_mythtv_src_finalize;
   154 
   155   g_object_class_install_property
   156     (gobject_class, PROP_LOCATION,
   157      g_param_spec_string ("location", "Location",
   158        "The location. In the form:"
   159        "\n\t\t\tmyth://a.com/file.nuv"
   160        "\n\t\t\tmyth://a.com:23223/file.nuv "
   161        "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
   162        "", G_PARAM_READWRITE));
   163 
   164   g_object_class_install_property
   165     (gobject_class, PROP_URI,
   166      g_param_spec_string ("uri", "Uri",
   167        "The location in form of a URI (deprecated; use location)",
   168        "", G_PARAM_READWRITE));
   169 
   170   g_object_class_install_property
   171     (gobject_class, PROP_MYTHTV_VERSION,
   172      g_param_spec_int ("mythtv-version", "mythtv-version",
   173        "Change Myth TV version",
   174        26, 30, 26, G_PARAM_READWRITE));
   175 
   176   g_object_class_install_property
   177     (gobject_class, PROP_MYTHTV_LIVEID,
   178      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   179        "Change Myth TV version",
   180        0, 200, GST_MYTHTV_ID_NUM, G_PARAM_READWRITE));
   181 
   182   g_object_class_install_property
   183     (gobject_class, PROP_MYTHTV_LIVE_CHAINID,
   184      g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
   185        "Sets the Myth TV chain ID (from TV Chain)",
   186        "", G_PARAM_READWRITE));
   187 
   188   g_object_class_install_property
   189     (gobject_class, PROP_MYTHTV_LIVE,
   190      g_param_spec_boolean ("mythtv-live", "mythtv-live",
   191        "Enable MythTV Live TV content streaming",
   192        FALSE, G_PARAM_READWRITE));
   193 
   194 #ifndef GST_DISABLE_GST_DEBUG
   195   g_object_class_install_property
   196     (gobject_class, PROP_MYTHTV_DBG,
   197      g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
   198        "Enable MythTV debug messages",
   199        FALSE, G_PARAM_READWRITE));
   200 #endif
   201 
   202   gstbasesrc_class->start = gst_mythtv_src_start;
   203   gstbasesrc_class->stop = gst_mythtv_src_stop;
   204   gstbasesrc_class->get_size = gst_mythtv_src_get_size;
   205   gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
   206   //gstpushsrc_class->newsegment = gst_mythtv_src_new_segment;
   207 
   208   gstbasesrc_class->create = gst_mythtv_src_create;
   209   
   210   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   211       "MythTV Client Source");
   212 }
   213 
   214 static void
   215 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
   216 {
   217   this->file_transfer = NULL;
   218 
   219   this->unique_setup = FALSE;
   220 
   221   this->mythtv_version = MYTHTV_VERSION_DEFAULT;
   222 
   223   this->bytes_read = 0;
   224 
   225   this->content_size = -1;
   226   this->read_offset = 0;
   227 
   228   this->live_tv = FALSE;
   229 
   230   this->user_agent = g_strdup ("mythtvsrc");
   231   this->mythtv_caps = NULL;  
   232   
   233   gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
   234 
   235   gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
   236 
   237   //gst_pad_set_chain_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   238   //    gst_mythtv_src_chain );
   239 
   240 /*
   241   gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   242       gst_mythtv_src_handle_event );
   243 
   244   gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   245       gst_mythtv_src_query );
   246 */
   247   //gst_pad_set_activatepull_function( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   248   //    gst_mythtv_src_sink_activate_pull );
   249 
   250 
   251 }
   252 
   253 static void
   254 gst_mythtv_src_finalize (GObject * gobject)
   255 {
   256   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   257 
   258   g_free (this->user_agent);
   259 
   260   if (this->mythtv_caps) {
   261     gst_caps_unref (this->mythtv_caps);
   262     this->mythtv_caps = NULL;
   263   }
   264 
   265   if (this->file_transfer) {
   266     g_object_unref (this->file_transfer);
   267     this->file_transfer = NULL;
   268   }
   269 
   270   if (this->uri_name) {
   271     g_free (this->uri_name);
   272   }
   273 
   274   if (this->user_agent) {
   275     g_free (this->user_agent);
   276   }
   277 
   278   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   279 }
   280 
   281 static guint
   282 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
   283 {
   284   guint read = 0;
   285   guint sizetoread = size; //GST_BUFFER_SIZE (outbuf);
   286   GstBuffer *buffer = gst_buffer_new_and_alloc( size ); 
   287 
   288   g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   289 
   290   /* Loop sending the Myth File Transfer request:
   291    * Retry whilst authentication fails and we supply it. */
   292   guint len = 0;
   293 
   294   //GST_OBJECT_LOCK(src);
   295 
   296   while ( sizetoread > 0 ) {
   297 
   298     len = myth_file_transfer_read( src->file_transfer,
   299 			GST_BUFFER_DATA( buffer ) + read, sizetoread, TRUE );
   300 
   301     if ( len > 0 ) {
   302       read += len;
   303       src->read_offset += read;
   304       sizetoread -= len;
   305     } else if ( len <= 0 ) {
   306     	
   307       if ( src->live_tv == FALSE ) {
   308 		goto done;
   309       } else if ( src->content_size >= src->read_offset && 
   310       			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
   311       	guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
   312       	if ( src->content_size < new_offset ) {
   313       	  src->content_size = new_offset;
   314       	  goto done;
   315       	} else 
   316 		  goto eos;
   317       } else
   318       	goto eos;
   319 
   320     }
   321 
   322     if ( read == sizetoread )
   323       break;
   324   }
   325   
   326   if ( read > 0 ) {
   327     src->bytes_read += read;
   328   }
   329 
   330   g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   331       "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   332       src->read_offset, src->content_size );
   333 
   334   *outbuf = buffer;
   335   memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
   336   GST_BUFFER_SIZE (buffer) = read;
   337   GST_BUFFER_OFFSET (buffer) = offset;
   338   GST_BUFFER_OFFSET_END (buffer) = offset + read;
   339      
   340   g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   341       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   342       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   343   //GST_OBJECT_UNLOCK(src);
   344   goto done;
   345 
   346 eos:
   347   //GST_OBJECT_UNLOCK(src);
   348 
   349   src->eos = TRUE;
   350   
   351 done:
   352 
   353   return read;
   354 }
   355 
   356 static GstFlowReturn
   357 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
   358 {
   359   GstMythtvSrc *src;
   360   GstFlowReturn ret = GST_FLOW_OK;
   361   guint read = 0;
   362   //GstBuffer *buffer = NULL;
   363   //guint size = MAX_READ_SIZE;
   364   
   365   src = GST_MYTHTV_SRC (psrc);
   366   
   367   if ( src->content_size >= src->read_offset && 
   368       			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
   369       	guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
   370       	if ( src->content_size < new_offset ) {
   371       	  src->content_size = new_offset;
   372       	}
   373   }
   374 
   375   if (G_UNLIKELY (src->read_offset != offset)) {
   376     guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
   377 
   378     if (G_UNLIKELY (new_offset < 0 || new_offset != offset))
   379       goto read_error;
   380 
   381     src->read_offset = offset;
   382   }
   383   
   384   /* The caller should know the number of bytes and not read beyond EOS. */
   385   if (G_UNLIKELY (src->eos))
   386     goto eos;
   387 
   388   /* Create the buffer. */
   389   //ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   390   //    GST_BUFFER_OFFSET_NONE, size, src->mythtv_caps ? src->mythtv_caps :
   391   //    GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
   392   
   393   //if (G_UNLIKELY (ret != GST_FLOW_OK))
   394   //  goto eos;
   395   //if (G_UNLIKELY (ret == GST_FLOW_ERROR))
   396   //  goto read_error;
   397   
   398   g_static_mutex_lock( &update_size_mutex );
   399   read = do_read_request_response ( src, 0, size, outbuf );
   400   g_static_mutex_unlock( &update_size_mutex );  
   401 
   402   if (G_UNLIKELY (read < 0) || *outbuf == NULL)
   403     goto read_error;
   404 
   405   gst_buffer_set_caps( *outbuf, GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))) );
   406   
   407   //*outbuf = buffer;
   408  
   409   if (G_UNLIKELY(src->eos))
   410     goto eos;
   411   else
   412     goto done;
   413 
   414 done:
   415   return ret;
   416 eos:
   417   {
   418     const gchar *reason = gst_flow_get_name (ret);
   419 
   420     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   421     return GST_FLOW_UNEXPECTED;
   422   }
   423   /* ERRORS */
   424 read_error:
   425   {
   426     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   427 	(NULL), ("Could not read any bytes (%i, %s)", read,
   428 	  src->uri_name));
   429     return GST_FLOW_ERROR;
   430   }
   431 
   432 #if 0
   433 need_pause:
   434   {
   435     const gchar *reason = gst_flow_get_name (ret);
   436 
   437     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   438     return GST_FLOW_UNEXPECTED;
   439   }
   440 #endif
   441 }
   442 
   443 void
   444 update_size_func( void *mythtv_data ) 
   445 {
   446   GstMythtvSrc *src;
   447 
   448   g_return_if_fail( mythtv_data != NULL );
   449 
   450   src = GST_MYTHTV_SRC ( mythtv_data );
   451 
   452   g_static_mutex_lock( &update_size_mutex );
   453 
   454   if ( src->do_start ) {
   455 #if ENABLE_TIMING_POSITION == 1
   456     guint64 size_tmp = 0;
   457     if (src->live_tv == TRUE) {
   458 get_file_pos:
   459       g_usleep( 50 );
   460       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   461       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   462 	src->content_size = size_tmp;
   463       else
   464 	goto get_file_pos;
   465       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   466 	  __FUNCTION__, size_tmp );
   467     }
   468 #endif
   469   }
   470   g_static_mutex_unlock( &update_size_mutex );
   471 
   472 }
   473 
   474 guint64
   475 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   476 {
   477 
   478   if ( src->do_start ) {
   479 #if ENABLE_TIMING_POSITION == 1
   480     guint64 size_tmp = 0;
   481     if (src->live_tv == TRUE) {
   482 get_file_pos:
   483       g_usleep( 50 );
   484       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   485       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   486 		src->content_size = size_tmp;
   487       else
   488 	goto get_file_pos;
   489       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   490 	  __FUNCTION__, size_tmp );
   491     }
   492 #endif
   493   }
   494   
   495   return src->content_size;	
   496 
   497 }
   498 
   499 /* create a socket for connecting to remote server */
   500 static gboolean
   501 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   502 {
   503   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   504 
   505   GString *chain_id_local = NULL;
   506 
   507   gboolean ret = TRUE;
   508 
   509   if (src->unique_setup == FALSE) {
   510     src->unique_setup = TRUE;
   511   } else {
   512     goto done;
   513   }
   514 
   515   GST_OBJECT_LOCK(src);
   516 
   517   if ( src->live_tv ) {
   518     src->spawn_livetv = myth_livetv_new( );
   519     if ( myth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   520       ret = FALSE;
   521       goto init_failed;
   522     }
   523     /* set up the uri variable */
   524     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   525     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   526     if ( chain_id_local != NULL ) {
   527       src->live_chain_id = g_strdup( chain_id_local->str );
   528       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   529     }
   530     src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
   531     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   532   }
   533 
   534   src->file_transfer = myth_file_transfer_new( src->live_tv_id, 
   535       g_string_new( src->uri_name ), -1, src->mythtv_version );
   536 
   537   if ( src->file_transfer == NULL ) {
   538     GST_OBJECT_UNLOCK(src);
   539 
   540     goto init_failed;
   541   }
   542 
   543   if ( src->live_tv ) {
   544     g_print ( "[%s] GST MYTHTVSRC: live_chain_id = %s\n", __FUNCTION__, src->live_chain_id );
   545     /* sets the MythSocket to the FileTransfer */
   546     //ret = myth_file_transfer_livetv_setup( &(src->file_transfer), src->spawn_livetv->remote_encoder->myth_socket );
   547   }
   548   /* sets the Playback monitor connection */
   549   ret = myth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   550 
   551   if ( src->live_tv == TRUE && ret == TRUE ) {
   552     /* loop finished, set the max tries variable to zero again... */
   553     wait_to_transfer = 0;
   554 
   555     while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS && ( myth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   556 	  /*|| ( myth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   557       g_usleep( 100 );
   558   }
   559 
   560   /* sets the FileTransfer instance connection (video/audio download) */
   561   ret = myth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   562 
   563   if ( ret == FALSE ) {
   564     GST_OBJECT_UNLOCK(src);
   565 #ifndef GST_DISABLE_GST_DEBUG  
   566     if ( src->mythtv_msgs_dbg )
   567       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   568 #endif
   569     goto begin_req_failed;
   570   }
   571 
   572   src->content_size = src->file_transfer->filesize;
   573 
   574   GST_OBJECT_UNLOCK(src);
   575   if ( src->live_tv ) {
   576 
   577     //GError* error;
   578 
   579     //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
   580 
   581     g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
   582 
   583   }
   584   src->do_start = TRUE;  
   585 
   586 done:
   587   return TRUE;
   588 
   589   /* ERRORS */
   590 init_failed:
   591   {
   592     if (src->spawn_livetv != NULL )
   593       g_object_unref( src->spawn_livetv );
   594 
   595     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   596 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   597     return FALSE;
   598   }
   599 begin_req_failed:
   600   {
   601     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   602 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   603     return FALSE;
   604   }
   605 }
   606 
   607 #if 0
   608 /* handles queries for location in the stream in the requested format */
   609 static gboolean
   610 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
   611 {
   612   gboolean res = TRUE;
   613   GstMythtvSrc *mythtv;
   614 
   615   guint64 size = 0;
   616 
   617   mythtv = GST_MYTHTV_SRC( GST_PAD_PARENT (pad) );
   618 
   619   size = gst_mythtv_src_get_position (mythtv);
   620 
   621   switch (GST_QUERY_TYPE (query)) {
   622 
   623     case GST_QUERY_POSITION:
   624       {
   625 
   626 	//GstFormat format;
   627 	gint64 cur = 0;
   628 
   629 	/* save requested format */
   630 	gst_query_parse_position (query, NULL, &cur);
   631 
   632 	/* query peer for current position in time */
   633 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   634 	    ( size > cur ) ? "greater" : "lower", size, cur );
   635 	gst_query_set_position (query, GST_FORMAT_BYTES, size);
   636 	if ( size < cur )
   637 	  goto error;
   638 
   639 	break;
   640       }
   641      #if 0
   642     case GST_QUERY_DURATION:
   643       {
   644 	//GstFormat format;
   645 	gint64 cur = 0;
   646 
   647 	/* save requested format */
   648 	gst_query_parse_position (query, NULL, &cur);
   649 
   650 	/* query peer for current position in time */
   651 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   652 	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
   653 	    cur * GST_SECOND );
   654 	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
   655 
   656 	if ( size * GST_SECOND < cur * GST_SECOND )
   657 	  goto error;
   658 
   659 	break;
   660       }
   661 	#endif
   662     default:
   663       res = FALSE;
   664       break;
   665   }
   666 
   667   return res;
   668 
   669 error:
   670 
   671   return FALSE;
   672 }
   673 #endif
   674 
   675 static gboolean
   676 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   677 {
   678   GstMythtvSrc *src;
   679   gboolean ret = TRUE;
   680 
   681   src = GST_MYTHTV_SRC (bsrc);
   682 
   683   if (src->content_size <= 0) {
   684     ret= FALSE;
   685 
   686   } else if ( src->live_tv && src->content_size < MYTHTV_TRANSFER_MAX_BUFFER ) {
   687     g_static_mutex_lock( &update_size_mutex );
   688 
   689 #if ENABLE_TIMING_POSITION == 1
   690     guint64 size_tmp = 0;
   691     if (src->live_tv == TRUE) {
   692 get_file_pos:
   693       g_usleep( 50 );
   694       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   695       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   696 	src->content_size = size_tmp;
   697       else
   698 	goto get_file_pos;
   699       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   700 	  __FUNCTION__, size_tmp );
   701     }
   702 #endif
   703     g_static_mutex_unlock( &update_size_mutex );
   704 
   705   }
   706 
   707   *size = src->content_size;
   708   g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
   709 
   710   return ret;
   711 
   712 }
   713 
   714 /* close the socket and associated resources
   715  * used both to recover from errors and go to NULL state */
   716   static gboolean
   717 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   718 {
   719   GstMythtvSrc *src;
   720 
   721   src = GST_MYTHTV_SRC (bsrc);
   722 
   723   if (src->uri_name) {
   724     g_free (src->uri_name);
   725     src->uri_name = NULL;
   726   }
   727 
   728   if (src->mythtv_caps) {
   729     gst_caps_unref (src->mythtv_caps);
   730     src->mythtv_caps = NULL;
   731   }
   732 
   733   src->eos = FALSE;
   734 
   735   return TRUE;
   736 }
   737 
   738 #if 0
   739 static gboolean
   740 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   741 {
   742   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   743 
   744   switch (GST_EVENT_TYPE (event)) {
   745 #if 0
   746     case GST_EVENT_FLUSH_START:
   747       src->eos = FALSE;
   748       break;
   749       //return TRUE;
   750     case GST_EVENT_FLUSH_STOP:
   751       src->do_start = TRUE;
   752       src->eos = FALSE;
   753       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
   754       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
   755       break;
   756 #endif
   757     case GST_EVENT_EOS:
   758       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
   759 	  src->content_size = gst_mythtv_src_get_position (src);
   760 	  if ( cont_size > src->content_size ) {
   761 	  	src->content_size = cont_size;
   762   	  } else {
   763       	src->eos = TRUE;
   764       	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   765       	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   766   	  }
   767       break;
   768 #if 0
   769     case GST_EVENT_NEWSEGMENT:
   770       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
   771       src->eos = FALSE;
   772       break;
   773     case GST_EVENT_SEEK:  	  
   774       {
   775 	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
   776 	gdouble rate;
   777 	//gboolean update = TRUE;
   778 	GstFormat format;
   779 	GstSeekType cur_type, stop_type;
   780 	GstSeekFlags flags;
   781 	gint64 cur = 0, stop = 0;
   782 	gst_event_parse_seek ( event, &rate, &format,
   783 	    &flags, &cur_type, &cur,
   784 	    &stop_type, &stop );
   785 
   786 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
   787 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   788 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   789 	}
   790 	//gboolean ret = gst_event_parse_new_segment ( event,
   791 	//    &update, &rate, &format, &start, &stop,
   792 	//    &position );
   793 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
   794 	//			cur, stop - cur + 1, GstBuffer)
   795       }
   796 #endif
   797     default:
   798       return gst_pad_event_default (pad, event);
   799   }
   800 
   801   return gst_pad_event_default (pad, event);
   802 }
   803 #endif
   804 
   805 static gboolean
   806 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
   807 {
   808   return TRUE;
   809 }
   810 
   811 static void
   812 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   813     const GValue * value, GParamSpec * pspec)
   814 {
   815   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   816 
   817   GST_OBJECT_LOCK (mythtvsrc);
   818   switch (prop_id) {
   819     case PROP_URI:
   820     case PROP_LOCATION:
   821       {
   822 	if (!g_value_get_string (value)) {
   823 	  GST_WARNING ("location property cannot be NULL");
   824 	  goto done;
   825 	}
   826 
   827 	if (mythtvsrc->uri_name != NULL) {
   828 	  g_free (mythtvsrc->uri_name);
   829 	  mythtvsrc->uri_name = NULL;
   830 	}
   831 	mythtvsrc->uri_name = g_value_dup_string (value);
   832 
   833 	break;
   834       }
   835 #ifndef GST_DISABLE_GST_DEBUG
   836     case PROP_MYTHTV_DBG:
   837       {
   838 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
   839 	break;
   840       }
   841 #endif
   842     case PROP_MYTHTV_VERSION:
   843       {
   844 	mythtvsrc->mythtv_version = g_value_get_int (value);
   845 	break;
   846       }
   847     case PROP_MYTHTV_LIVEID:
   848       {
   849 	mythtvsrc->live_tv_id = g_value_get_int (value);
   850 	break;
   851       }
   852     case PROP_MYTHTV_LIVE:
   853       {
   854 	mythtvsrc->live_tv = g_value_get_boolean (value);
   855 	break;
   856       }
   857     case PROP_MYTHTV_LIVE_CHAINID:
   858       {
   859 	if (!g_value_get_string (value)) {
   860 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
   861 	  goto done;
   862 	}
   863 
   864 	if (mythtvsrc->live_chain_id != NULL) {
   865 	  g_free (mythtvsrc->live_chain_id);
   866 	  mythtvsrc->live_chain_id = NULL;
   867 	}
   868 	mythtvsrc->live_chain_id = g_value_dup_string (value);
   869 
   870 	break;
   871       }
   872 
   873     default:
   874       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   875       break;
   876   }
   877   GST_OBJECT_UNLOCK (mythtvsrc);
   878 done:
   879   return;
   880 }
   881 
   882   static void
   883 gst_mythtv_src_get_property (GObject * object, guint prop_id,
   884     GValue * value, GParamSpec * pspec)
   885 {
   886   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   887 
   888   GST_OBJECT_LOCK (mythtvsrc);
   889   switch (prop_id) {
   890     case PROP_URI:
   891     case PROP_LOCATION:
   892       {
   893 	gchar *str = g_strdup( "" );
   894 
   895 	if ( mythtvsrc->uri_name == NULL ) {
   896 	  g_free (mythtvsrc->uri_name);
   897 	  mythtvsrc->uri_name = NULL;
   898 	} else {
   899 	  str = g_strdup( mythtvsrc->uri_name );
   900 	}
   901 	g_value_set_string ( value, str );
   902 	break;
   903       }
   904 #ifndef GST_DISABLE_GST_DEBUG
   905     case PROP_MYTHTV_DBG:
   906       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
   907       break;
   908 #endif
   909     case PROP_MYTHTV_VERSION:
   910       {
   911 	g_value_set_int ( value, mythtvsrc->mythtv_version );
   912 	break;
   913       }
   914     case PROP_MYTHTV_LIVEID:
   915       {
   916 	g_value_set_int ( value, mythtvsrc->live_tv_id );
   917 	break;
   918       }
   919     case PROP_MYTHTV_LIVE:
   920       g_value_set_boolean ( value, mythtvsrc->live_tv );
   921       break;
   922     case PROP_MYTHTV_LIVE_CHAINID:
   923       {
   924 	gchar *str = g_strdup( "" );
   925 
   926 	if ( mythtvsrc->live_chain_id == NULL ) {
   927 	  g_free (mythtvsrc->live_chain_id);
   928 	  mythtvsrc->live_chain_id = NULL;
   929 	} else {
   930 	  str = g_strdup( mythtvsrc->live_chain_id );
   931 	}
   932 	g_value_set_string ( value, str );
   933 	break;
   934       }
   935     default:
   936       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   937       break;
   938   }
   939   GST_OBJECT_UNLOCK (mythtvsrc);
   940 }
   941 
   942 /* entry point to initialize the plug-in
   943  * initialize the plug-in itself
   944  * register the element factories and pad templates
   945  * register the features
   946  */
   947   static gboolean
   948 plugin_init (GstPlugin * plugin)
   949 {
   950   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
   951       GST_TYPE_MYTHTV_SRC);
   952 }
   953 
   954 /* this is the structure that gst-register looks for
   955  * so keep the name plugin_desc, or you cannot get your plug-in registered */
   956 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   957     GST_VERSION_MINOR,
   958     "mythtv",
   959     "lib MythTV src",
   960     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
   961 
   962 
   963 /*** GSTURIHANDLER INTERFACE *************************************************/
   964   static guint 
   965 gst_mythtv_src_uri_get_type (void)
   966 {
   967   return GST_URI_SRC;
   968 }
   969 
   970   static gchar **
   971 gst_mythtv_src_uri_get_protocols (void)
   972 {
   973   static gchar *protocols[] = { "myth", "myths", NULL };
   974 
   975   return protocols;
   976 }
   977 
   978   static const gchar *
   979 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
   980 {
   981   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   982 
   983   return src->uri_name;
   984 }
   985 
   986   static gboolean
   987 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
   988 {
   989   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   990 
   991   gchar *protocol;
   992 
   993   protocol = gst_uri_get_protocol (uri);
   994   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
   995     g_free (protocol);
   996     return FALSE;
   997   }
   998   g_free (protocol);
   999   g_object_set (src, "location", uri, NULL);
  1000 
  1001   return TRUE;
  1002 }
  1003 
  1004   static void
  1005 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1006 {
  1007   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1008 
  1009   iface->get_type = gst_mythtv_src_uri_get_type;
  1010   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1011   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1012   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1013 }
  1014 
  1015   void
  1016 size_header_handler (void *userdata, const char *value)
  1017 {
  1018   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1019 
  1020   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1021 
  1022   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1023 }