gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Mon Oct 23 22:28:02 2006 +0100 (2006-10-23)
branchtrunk
changeset 44 7a654d6de88f
parent 40 ba5dc9bff3a1
child 52 67e72eadeef2
permissions -rwxr-xr-x
[svn r45] Moved Myth FileTransfer, LiveTV and Myth URI to the gmyth library.
     1 /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
     2 /* GStreamer MythTV Plug-in
     3  * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
     4  *
     5  * This library is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU Library Lesser General 
     7  * Public License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more 
    14  */
    15 
    16 #ifdef HAVE_CONFIG_H
    17 #include "config.h"
    18 #endif
    19 
    20 #include "gstmythtvsrc.h"
    21 #include "gmyth_file_transfer.h"
    22 #include "gmyth_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_GMYTHTV_ID_NUM		1
    34 
    35 #define GMYTHTV_VERSION_DEFAULT		30
    36 
    37 #define GMYTHTV_TRANSFER_MAX_WAITS	100
    38 
    39 #define GMYTHTV_TRANSFER_MAX_BUFFER	1024*1024
    40 //( 32*1024  )
    41 
    42 /* 4*1024 ??? */
    43 #define MAX_READ_SIZE              	12*1024
    44 //( 32*1024 )
    45 
    46 #define ENABLE_TIMING_POSITION		0
    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_GMYTHTV_DBG,
    74 #endif
    75   PROP_GMYTHTV_VERSION,
    76   PROP_GMYTHTV_LIVE,
    77   PROP_GMYTHTV_LIVEID,
    78   PROP_GMYTHTV_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 gint 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_GMYTHTV_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_GMYTHTV_LIVEID,
   178      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   179        "Change Myth TV version",
   180        0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
   181 
   182   g_object_class_install_property
   183     (gobject_class, PROP_GMYTHTV_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_GMYTHTV_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_GMYTHTV_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 = GMYTHTV_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   gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   241       gst_mythtv_src_handle_event );
   242 /*
   243   gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   244       gst_mythtv_src_query );
   245 */
   246   //gst_pad_set_activatepull_function( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   247   //    gst_mythtv_src_sink_activate_pull );
   248 
   249 }
   250 
   251 static void
   252 gst_mythtv_src_finalize (GObject * gobject)
   253 {
   254   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   255 
   256   g_free (this->user_agent);
   257 
   258   if (this->mythtv_caps) {
   259     gst_caps_unref (this->mythtv_caps);
   260     this->mythtv_caps = NULL;
   261   }
   262 
   263   if (this->file_transfer) {
   264     g_object_unref (this->file_transfer);
   265     this->file_transfer = NULL;
   266   }
   267 
   268   if (this->uri_name) {
   269     g_free (this->uri_name);
   270   }
   271 
   272   if (this->user_agent) {
   273     g_free (this->user_agent);
   274   }
   275 
   276   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   277 }
   278 
   279 static gint
   280 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
   281 {
   282   gint read = 0;
   283   guint sizetoread = size; //GST_BUFFER_SIZE (outbuf);
   284   //GstBuffer *buffer = gst_buffer_new_and_alloc( size ); 
   285 
   286   g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   287 
   288   /* Loop sending the Myth File Transfer request:
   289    * Retry whilst authentication fails and we supply it. */
   290   gint len = 0;
   291 
   292   GST_OBJECT_LOCK(src);
   293 
   294   while ( sizetoread > 0 ) {
   295   	
   296   	len = gmyth_file_transfer_read( src->file_transfer,
   297 			GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
   298 
   299     if ( len > 0 ) {
   300       read += len;      
   301       sizetoread -= len;
   302     } else if ( len <= 0 ) {
   303     	
   304       if ( src->live_tv == FALSE ) {
   305 		goto done;
   306       } else if ( len == 0 ) {
   307       	src->update_prog_chain = TRUE;
   308       	goto done;
   309       } else if ( /*src->content_size >= src->read_offset && 
   310       			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
   311       			( src->content_size <= ( src->read_offset + size + GMYTHTV_TRANSFER_MAX_BUFFER ) ) )
   312   {
   313 #if ENABLE_TIMING_POSITION == 1
   314     gint64 size_tmp = 0;
   315     if (src->live_tv == TRUE) {
   316 get_file_pos:
   317       size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   318       if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   319 	src->content_size = size_tmp;
   320       else if ( size_tmp > 0 )
   321 	goto get_file_pos;
   322       g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   323 	  __FUNCTION__, size_tmp );
   324     }
   325 #else
   326   	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
   327 	if ( new_offset > 0 ) {
   328 	  if ( src->content_size < new_offset ) {
   329 	    src->content_size = new_offset;
   330 	  }
   331 	} else {
   332 	  src->update_prog_chain = TRUE;
   333 	}
   334 #endif
   335 	goto done;
   336   }
   337 
   338     }
   339     
   340     if ( read == sizetoread )
   341       break;
   342   }
   343   
   344   if ( read > 0 ) {
   345   	src->read_offset += read;
   346     src->bytes_read += read;
   347   
   348   g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   349       "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   350       src->read_offset, src->content_size );
   351       
   352   //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
   353   GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
   354   GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
   355   GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
   356   //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
   357      
   358   g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   359       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   360       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   361       
   362   } else if ( !src->live_tv )
   363   	goto eos;
   364   
   365   goto done;
   366 
   367 eos:
   368   src->eos = TRUE;
   369   
   370 done:
   371   GST_OBJECT_UNLOCK(src);
   372 
   373   return read;
   374 }
   375 
   376 static GstFlowReturn
   377 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
   378 {
   379   GstMythtvSrc *src;
   380   GstFlowReturn ret = GST_FLOW_OK;
   381   gint read = -1;
   382   
   383   src = GST_MYTHTV_SRC (psrc);
   384   /* The caller should know the number of bytes and not read beyond EOS. */
   385   if (G_UNLIKELY (src->eos))
   386     goto eos;
   387   if ( G_UNLIKELY (src->update_prog_chain) )
   388     goto change_progchain;
   389   
   390   //GST_OBJECT_LOCK(src);
   391 
   392   if (G_UNLIKELY (src->read_offset != offset)) {
   393     gint64 new_offset = gmyth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
   394     g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   395     	__FUNCTION__, src->read_offset, new_offset );
   396     if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
   397     {
   398       //GST_OBJECT_UNLOCK(src);
   399       goto change_progchain;
   400     }
   401 
   402     src->read_offset = offset;
   403   }
   404   //GST_OBJECT_UNLOCK(src);
   405   
   406   /* Create the buffer. */
   407   ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   408       src->read_offset, size,
   409       //src->icy_caps ? src->icy_caps :
   410       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
   411 
   412   if (G_UNLIKELY (ret != GST_FLOW_OK))
   413     goto done;
   414   
   415   //g_static_mutex_lock( &update_size_mutex );
   416   read = do_read_request_response ( src, src->read_offset, size, outbuf );
   417   //g_static_mutex_unlock( &update_size_mutex );  
   418 
   419   if (G_UNLIKELY (src->update_prog_chain) )
   420     goto change_progchain;
   421 
   422   if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
   423   	//if ( src->live_tv )
   424     //	goto done;
   425     //else
   426     goto read_error;
   427   }
   428 
   429 /* 
   430   if (G_UNLIKELY(src->eos))
   431     goto eos;
   432   else
   433     goto done;
   434 */
   435 
   436 done:
   437  {
   438     const gchar *reason = gst_flow_get_name (ret);
   439 
   440     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   441   	return ret;
   442  }
   443 eos:
   444   {
   445     const gchar *reason = gst_flow_get_name (ret);
   446 
   447     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   448     return GST_FLOW_UNEXPECTED;
   449   }
   450   /* ERRORS */
   451 read_error:
   452   {
   453     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   454 	(NULL), ("Could not read any bytes (%i, %s)", read,
   455 	  src->uri_name));
   456     return GST_FLOW_ERROR;
   457   }
   458 change_progchain:
   459   {
   460     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   461 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   462 	  src->uri_name));
   463 	// go to the next program chain
   464 	src->unique_setup = FALSE;
   465 	gst_mythtv_src_start( psrc );
   466   
   467     return GST_FLOW_OK;
   468   }
   469 
   470 }
   471 
   472 void
   473 update_size_func( void *mythtv_data ) 
   474 {
   475   GstMythtvSrc *src;
   476 
   477   g_return_if_fail( mythtv_data != NULL );
   478 
   479   src = GST_MYTHTV_SRC ( mythtv_data );
   480 
   481   g_static_mutex_lock( &update_size_mutex );
   482 
   483   if ( src->do_start ) {
   484 #if ENABLE_TIMING_POSITION == 1
   485     gint64 size_tmp = 0;
   486     if (src->live_tv == TRUE) {
   487 get_file_pos:
   488       g_usleep( 50 );
   489       size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   490       if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   491 	src->content_size = size_tmp;
   492       else if ( size_tmp > 0 )
   493 	goto get_file_pos;
   494       g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   495 	  __FUNCTION__, size_tmp );
   496     }
   497 #endif
   498   }
   499   g_static_mutex_unlock( &update_size_mutex );
   500 
   501 }
   502 
   503 guint64
   504 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   505 {
   506 
   507   if ( src->do_start ) {
   508 #if ENABLE_TIMING_POSITION == 1
   509     gint64 size_tmp = 0;
   510     if (src->live_tv == TRUE) {
   511 get_file_pos:
   512       g_usleep( 50 );
   513       size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   514       if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   515 		src->content_size = size_tmp;
   516       else if ( size_tmp > 0 )
   517 	goto get_file_pos;
   518       g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   519 	  __FUNCTION__, size_tmp );
   520     }
   521 #endif
   522   }
   523   
   524   return src->content_size;	
   525 
   526 }
   527 
   528 /* create a socket for connecting to remote server */
   529 static gboolean
   530 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   531 {
   532   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   533 
   534   GString *chain_id_local = NULL;
   535 
   536   gboolean ret = TRUE;
   537 
   538   if (src->unique_setup == FALSE) {
   539     src->unique_setup = TRUE;
   540   } else {
   541     goto done;
   542   }
   543 
   544   GST_OBJECT_LOCK(src);
   545 
   546   if ( src->live_tv ) {
   547     src->spawn_livetv = gmyth_livetv_new( );
   548     if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   549       ret = FALSE;
   550       goto init_failed;
   551     }
   552     /* set up the uri variable */
   553     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   554     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   555     if ( chain_id_local != NULL ) {
   556       src->live_chain_id = g_strdup( chain_id_local->str );
   557       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   558     }
   559     src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
   560     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   561   }
   562 
   563   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   564       g_string_new( src->uri_name ), -1, src->mythtv_version );
   565 
   566   if ( src->file_transfer == NULL ) {
   567     GST_OBJECT_UNLOCK(src);
   568 
   569     goto init_failed;
   570   }
   571 
   572   /* sets the Playback monitor connection */
   573   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   574 
   575   if ( src->live_tv == TRUE && ret == TRUE ) {
   576     /* loop finished, set the max tries variable to zero again... */
   577     wait_to_transfer = 0;
   578 
   579     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   580 	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   581       g_usleep( 100 );
   582   }
   583 
   584   /* sets the FileTransfer instance connection (video/audio download) */
   585   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   586 
   587   if ( ret == FALSE ) {
   588     GST_OBJECT_UNLOCK(src);
   589 #ifndef GST_DISABLE_GST_DEBUG  
   590     if ( src->mythtv_msgs_dbg )
   591       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   592 #endif
   593     goto begin_req_failed;
   594   }
   595 
   596   src->content_size = src->file_transfer->filesize;
   597 
   598   GST_OBJECT_UNLOCK(src);
   599   if ( src->live_tv ) {
   600 
   601     //GError* error;
   602     //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
   603     g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
   604 
   605   }
   606   src->do_start = TRUE;  
   607 
   608 done:
   609   return TRUE;
   610 
   611   /* ERRORS */
   612 init_failed:
   613   {
   614     if (src->spawn_livetv != NULL )
   615       g_object_unref( src->spawn_livetv );
   616 
   617     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   618 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   619     return FALSE;
   620   }
   621 begin_req_failed:
   622   {
   623     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   624 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   625     return FALSE;
   626   }
   627 }
   628 
   629 #if 0
   630 /* handles queries for location in the stream in the requested format */
   631 static gboolean
   632 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
   633 {
   634   gboolean res = TRUE;
   635   GstMythtvSrc *mythtv;
   636 
   637   guint64 size = 0;
   638 
   639   mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
   640 
   641   size = gst_mythtv_src_get_position (mythtv);
   642 
   643   switch (GST_QUERY_TYPE (query)) {
   644 
   645     case GST_QUERY_POSITION:
   646       {
   647 
   648 	//GstFormat format;
   649 	gint64 cur = 0;
   650 
   651 	/* save requested format */
   652 	gst_query_parse_position (query, NULL, &cur);
   653 
   654 	/* query peer for current position in time */
   655 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   656 	    ( size > cur ) ? "greater" : "lower", size, cur );
   657 	gst_query_set_position (query, GST_FORMAT_BYTES, size);
   658 	if ( size < cur )
   659 	  goto error;
   660 
   661 	break;
   662       }
   663      #if 0
   664     case GST_QUERY_DURATION:
   665       {
   666 	//GstFormat format;
   667 	gint64 cur = 0;
   668 
   669 	/* save requested format */
   670 	gst_query_parse_position (query, NULL, &cur);
   671 
   672 	/* query peer for current position in time */
   673 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   674 	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
   675 	    cur * GST_SECOND );
   676 	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
   677 
   678 	if ( size * GST_SECOND < cur * GST_SECOND )
   679 	  goto error;
   680 
   681 	break;
   682       }
   683 	#endif
   684     default:
   685       res = FALSE;
   686       break;
   687   }
   688 
   689   return res;
   690 
   691 error:
   692 
   693   return FALSE;
   694 }
   695 #endif
   696 
   697 static gboolean
   698 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   699 {
   700   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   701   gboolean ret = TRUE;
   702 
   703   if (src->content_size <= 0) {
   704     ret= FALSE;
   705   } else if ( abs ( src->content_size - src->read_offset ) <= GMYTHTV_TRANSFER_MAX_BUFFER ) {
   706     //g_static_mutex_lock( &update_size_mutex );
   707     GST_OBJECT_LOCK(src);
   708 
   709   	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
   710 	if ( new_offset > 0 ) {
   711 	  if ( src->content_size < new_offset ) {
   712 	    src->content_size = new_offset;
   713 	  }
   714 	} else {
   715 	  src->update_prog_chain = TRUE;
   716 	  src->content_size = 0;
   717 	}
   718  
   719 #if ENABLE_TIMING_POSITION == 1
   720     gint64 size_tmp = 0;
   721     if (src->live_tv == TRUE) {
   722 get_file_pos:
   723       g_usleep( 5 );
   724       size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   725       if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   726 	src->content_size = size_tmp;
   727       else if ( size_tmp > 0  )
   728 	goto get_file_pos;
   729       g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   730 	  __FUNCTION__, size_tmp );
   731     }
   732 #endif
   733 	GST_OBJECT_UNLOCK(src);
   734     //g_static_mutex_unlock( &update_size_mutex );
   735 
   736   }
   737 
   738   *size = src->content_size;
   739   g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
   740 
   741   return ret;
   742 
   743 }
   744 
   745 /* close the socket and associated resources
   746  * used both to recover from errors and go to NULL state */
   747   static gboolean
   748 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   749 {
   750   GstMythtvSrc *src;
   751 
   752   src = GST_MYTHTV_SRC (bsrc);
   753 
   754   if (src->uri_name) {
   755     g_free (src->uri_name);
   756     src->uri_name = NULL;
   757   }
   758 
   759   if (src->mythtv_caps) {
   760     gst_caps_unref (src->mythtv_caps);
   761     src->mythtv_caps = NULL;
   762   }
   763 
   764   src->eos = FALSE;
   765 
   766   return TRUE;
   767 }
   768 
   769 static gboolean
   770 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   771 {
   772   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   773 
   774   switch (GST_EVENT_TYPE (event)) {
   775 #if 0
   776     case GST_EVENT_FLUSH_START:
   777       src->eos = FALSE;
   778       break;
   779       //return TRUE;
   780     case GST_EVENT_FLUSH_STOP:
   781       src->do_start = TRUE;
   782       src->eos = FALSE;
   783       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
   784       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
   785       break;
   786 #endif
   787     case GST_EVENT_EOS:
   788       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
   789 	  guint64 cont_size = gst_mythtv_src_get_position (src);
   790 	  if ( !src->live_tv ) {
   791 		  if ( cont_size > src->content_size ) {
   792 		  	src->content_size = cont_size;
   793 		  	src->eos = FALSE;
   794 	  	  } else {
   795 	      	src->eos = TRUE;
   796 	      	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   797 	      	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   798 	  	  }
   799 	  } else 
   800 	  	src->eos = TRUE;
   801       break;
   802 #if 0
   803     case GST_EVENT_NEWSEGMENT:
   804       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
   805       src->eos = FALSE;
   806       break;
   807     case GST_EVENT_SEEK:  	  
   808       {
   809 	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
   810 	gdouble rate;
   811 	//gboolean update = TRUE;
   812 	GstFormat format;
   813 	GstSeekType cur_type, stop_type;
   814 	GstSeekFlags flags;
   815 	gint64 cur = 0, stop = 0;
   816 	gst_event_parse_seek ( event, &rate, &format,
   817 	    &flags, &cur_type, &cur,
   818 	    &stop_type, &stop );
   819 
   820 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
   821 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   822 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   823 	}
   824 	//gboolean ret = gst_event_parse_new_segment ( event,
   825 	//    &update, &rate, &format, &start, &stop,
   826 	//    &position );
   827 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
   828 	//			cur, stop - cur + 1, GstBuffer)
   829       }
   830 #endif
   831     default:
   832       return gst_pad_event_default (pad, event);
   833   }
   834 
   835   return gst_pad_event_default (pad, event);
   836 }
   837 
   838 static gboolean
   839 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
   840 {
   841   return TRUE;
   842 }
   843 
   844 static void
   845 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   846     const GValue * value, GParamSpec * pspec)
   847 {
   848   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   849 
   850   GST_OBJECT_LOCK (mythtvsrc);
   851   switch (prop_id) {
   852     case PROP_URI:
   853     case PROP_LOCATION:
   854       {
   855 	if (!g_value_get_string (value)) {
   856 	  GST_WARNING ("location property cannot be NULL");
   857 	  goto done;
   858 	}
   859 
   860 	if (mythtvsrc->uri_name != NULL) {
   861 	  g_free (mythtvsrc->uri_name);
   862 	  mythtvsrc->uri_name = NULL;
   863 	}
   864 	mythtvsrc->uri_name = g_value_dup_string (value);
   865 
   866 	break;
   867       }
   868 #ifndef GST_DISABLE_GST_DEBUG
   869     case PROP_GMYTHTV_DBG:
   870       {
   871 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
   872 	break;
   873       }
   874 #endif
   875     case PROP_GMYTHTV_VERSION:
   876       {
   877 	mythtvsrc->mythtv_version = g_value_get_int (value);
   878 	break;
   879       }
   880     case PROP_GMYTHTV_LIVEID:
   881       {
   882 	mythtvsrc->live_tv_id = g_value_get_int (value);
   883 	break;
   884       }
   885     case PROP_GMYTHTV_LIVE:
   886       {
   887 	mythtvsrc->live_tv = g_value_get_boolean (value);
   888 	break;
   889       }
   890     case PROP_GMYTHTV_LIVE_CHAINID:
   891       {
   892 	if (!g_value_get_string (value)) {
   893 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
   894 	  goto done;
   895 	}
   896 
   897 	if (mythtvsrc->live_chain_id != NULL) {
   898 	  g_free (mythtvsrc->live_chain_id);
   899 	  mythtvsrc->live_chain_id = NULL;
   900 	}
   901 	mythtvsrc->live_chain_id = g_value_dup_string (value);
   902 
   903 	break;
   904       }
   905 
   906     default:
   907       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   908       break;
   909   }
   910   GST_OBJECT_UNLOCK (mythtvsrc);
   911 done:
   912   return;
   913 }
   914 
   915   static void
   916 gst_mythtv_src_get_property (GObject * object, guint prop_id,
   917     GValue * value, GParamSpec * pspec)
   918 {
   919   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   920 
   921   GST_OBJECT_LOCK (mythtvsrc);
   922   switch (prop_id) {
   923     case PROP_URI:
   924     case PROP_LOCATION:
   925       {
   926 	gchar *str = g_strdup( "" );
   927 
   928 	if ( mythtvsrc->uri_name == NULL ) {
   929 	  g_free (mythtvsrc->uri_name);
   930 	  mythtvsrc->uri_name = NULL;
   931 	} else {
   932 	  str = g_strdup( mythtvsrc->uri_name );
   933 	}
   934 	g_value_set_string ( value, str );
   935 	break;
   936       }
   937 #ifndef GST_DISABLE_GST_DEBUG
   938     case PROP_GMYTHTV_DBG:
   939       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
   940       break;
   941 #endif
   942     case PROP_GMYTHTV_VERSION:
   943       {
   944 	g_value_set_int ( value, mythtvsrc->mythtv_version );
   945 	break;
   946       }
   947     case PROP_GMYTHTV_LIVEID:
   948       {
   949 	g_value_set_int ( value, mythtvsrc->live_tv_id );
   950 	break;
   951       }
   952     case PROP_GMYTHTV_LIVE:
   953       g_value_set_boolean ( value, mythtvsrc->live_tv );
   954       break;
   955     case PROP_GMYTHTV_LIVE_CHAINID:
   956       {
   957 	gchar *str = g_strdup( "" );
   958 
   959 	if ( mythtvsrc->live_chain_id == NULL ) {
   960 	  g_free (mythtvsrc->live_chain_id);
   961 	  mythtvsrc->live_chain_id = NULL;
   962 	} else {
   963 	  str = g_strdup( mythtvsrc->live_chain_id );
   964 	}
   965 	g_value_set_string ( value, str );
   966 	break;
   967       }
   968     default:
   969       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   970       break;
   971   }
   972   GST_OBJECT_UNLOCK (mythtvsrc);
   973 }
   974 
   975 /* entry point to initialize the plug-in
   976  * initialize the plug-in itself
   977  * register the element factories and pad templates
   978  * register the features
   979  */
   980 static gboolean
   981 plugin_init (GstPlugin * plugin)
   982 {
   983   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
   984       GST_TYPE_MYTHTV_SRC);
   985 }
   986 
   987 /* this is the structure that gst-register looks for
   988  * so keep the name plugin_desc, or you cannot get your plug-in registered */
   989 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   990     GST_VERSION_MINOR,
   991     "mythtv",
   992     "lib MythTV src",
   993     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
   994 
   995 
   996 /*** GSTURIHANDLER INTERFACE *************************************************/
   997   static guint 
   998 gst_mythtv_src_uri_get_type (void)
   999 {
  1000   return GST_URI_SRC;
  1001 }
  1002 
  1003   static gchar **
  1004 gst_mythtv_src_uri_get_protocols (void)
  1005 {
  1006   static gchar *protocols[] = { "myth", "myths", NULL };
  1007 
  1008   return protocols;
  1009 }
  1010 
  1011   static const gchar *
  1012 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1013 {
  1014   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1015 
  1016   return src->uri_name;
  1017 }
  1018 
  1019   static gboolean
  1020 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1021 {
  1022   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1023 
  1024   gchar *protocol;
  1025 
  1026   protocol = gst_uri_get_protocol (uri);
  1027   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1028     g_free (protocol);
  1029     return FALSE;
  1030   }
  1031   g_free (protocol);
  1032   g_object_set (src, "location", uri, NULL);
  1033 
  1034   return TRUE;
  1035 }
  1036 
  1037   static void
  1038 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1039 {
  1040   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1041 
  1042   iface->get_type = gst_mythtv_src_uri_get_type;
  1043   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1044   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1045   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1046 }
  1047 
  1048   void
  1049 size_header_handler (void *userdata, const char *value)
  1050 {
  1051   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1052 
  1053   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1054 
  1055   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1056 }