gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Wed Oct 18 23:08:56 2006 +0100 (2006-10-18)
branchtrunk
changeset 33 79fca2122593
parent 30 abe0ee48d78b
child 34 c71d37b93734
permissions -rwxr-xr-x
[svn r34] Fixes the gmyth_stringlist getuint64 function (problems on bitwise operations).
     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	4*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   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 guint
   280 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
   281 {
   282   guint 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   guint len = 0;
   291 
   292   GST_OBJECT_LOCK(src);
   293 
   294   while ( sizetoread > 0 ) {
   295   	
   296   	len = myth_file_transfer_read( src->file_transfer,
   297 			GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
   298 
   299     if ( len > 0 ) {
   300       read += len;
   301       src->read_offset += read;
   302       sizetoread -= len;
   303     } else if ( len <= 0 ) {
   304     	
   305       if ( src->live_tv == FALSE ) {
   306 		goto done;
   307       } else if ( src->content_size >= src->read_offset && 
   308       			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
   309       				goto done;
   310       }
   311 
   312     }
   313     
   314     if ( read == sizetoread )
   315       break;
   316   }
   317   
   318   if ( read > 0 ) {
   319     src->bytes_read += read;
   320   
   321   g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   322       "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   323       src->read_offset, src->content_size );
   324       
   325   //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
   326   GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
   327   GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
   328   GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
   329   //*outbuf = buffer;  
   330   //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
   331      
   332   g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   333       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   334       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   335       
   336   } else
   337   	goto eos;
   338   
   339   goto done;
   340 
   341 eos:
   342   //GST_OBJECT_UNLOCK(src);  
   343   src->eos = TRUE;
   344   
   345 done:
   346   GST_OBJECT_UNLOCK(src);
   347 
   348   return read;
   349 }
   350 
   351 static GstFlowReturn
   352 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
   353 {
   354   GstMythtvSrc *src;
   355   GstFlowReturn ret = GST_FLOW_OK;
   356   guint read = 0;
   357   
   358   src = GST_MYTHTV_SRC (psrc);
   359   /* The caller should know the number of bytes and not read beyond EOS. */
   360   if (G_UNLIKELY (src->eos))
   361     goto eos;
   362   
   363   //GST_OBJECT_LOCK(src);
   364   if ( /*src->content_size >= src->read_offset && 
   365       			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
   366       			( src->content_size <= ( src->read_offset + size ) ) )
   367   {
   368 #if ENABLE_TIMING_POSITION == 1
   369     guint64 size_tmp = 0;
   370     if (src->live_tv == TRUE) {
   371 get_file_pos:
   372       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   373       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   374 	src->content_size = size_tmp;
   375       else
   376 	goto get_file_pos;
   377       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   378 	  __FUNCTION__, size_tmp );
   379     }
   380 #endif
   381   	//guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
   382   	//if ( src->content_size < new_offset ) {
   383   	//  src->content_size = new_offset;
   384   	//}
   385   }
   386 
   387   if (G_UNLIKELY (src->read_offset != offset)) {
   388     guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
   389 
   390     if (G_UNLIKELY (new_offset < 0 || new_offset != offset)) {
   391       //GST_OBJECT_UNLOCK(src);
   392       goto read_error;
   393     }
   394 
   395     src->read_offset = offset;
   396   }
   397   //GST_OBJECT_UNLOCK(src);
   398   
   399   /* Create the buffer. */
   400   ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   401       src->read_offset, size,
   402       //src->icy_caps ? src->icy_caps :
   403       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
   404 
   405   if (G_UNLIKELY (ret != GST_FLOW_OK))
   406     goto done;
   407   
   408   //g_static_mutex_lock( &update_size_mutex );
   409   read = do_read_request_response ( src, src->read_offset, size, outbuf );
   410   //g_static_mutex_unlock( &update_size_mutex );  
   411 
   412   if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
   413   	//if ( src->live_tv )
   414     //	goto done;
   415     //else
   416     goto read_error;
   417   }
   418 
   419 /* 
   420   if (G_UNLIKELY(src->eos))
   421     goto eos;
   422   else
   423     goto done;
   424 */
   425 
   426 done:
   427  {
   428     const gchar *reason = gst_flow_get_name (ret);
   429 
   430     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   431   	return ret;
   432  }
   433 eos:
   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   /* ERRORS */
   441 read_error:
   442   {
   443     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   444 	(NULL), ("Could not read any bytes (%i, %s)", read,
   445 	  src->uri_name));
   446     return GST_FLOW_ERROR;
   447   }
   448 
   449 #if 0
   450 need_pause:
   451   {
   452     const gchar *reason = gst_flow_get_name (ret);
   453 
   454     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   455     return GST_FLOW_UNEXPECTED;
   456   }
   457 #endif
   458 }
   459 
   460 void
   461 update_size_func( void *mythtv_data ) 
   462 {
   463   GstMythtvSrc *src;
   464 
   465   g_return_if_fail( mythtv_data != NULL );
   466 
   467   src = GST_MYTHTV_SRC ( mythtv_data );
   468 
   469   g_static_mutex_lock( &update_size_mutex );
   470 
   471   if ( src->do_start ) {
   472 #if ENABLE_TIMING_POSITION == 1
   473     guint64 size_tmp = 0;
   474     if (src->live_tv == TRUE) {
   475 get_file_pos:
   476       g_usleep( 50 );
   477       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   478       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   479 	src->content_size = size_tmp;
   480       else
   481 	goto get_file_pos;
   482       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   483 	  __FUNCTION__, size_tmp );
   484     }
   485 #endif
   486   }
   487   g_static_mutex_unlock( &update_size_mutex );
   488 
   489 }
   490 
   491 guint64
   492 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   493 {
   494 
   495   if ( src->do_start ) {
   496 #if ENABLE_TIMING_POSITION == 1
   497     guint64 size_tmp = 0;
   498     if (src->live_tv == TRUE) {
   499 get_file_pos:
   500       g_usleep( 50 );
   501       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   502       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   503 		src->content_size = size_tmp;
   504       else
   505 	goto get_file_pos;
   506       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   507 	  __FUNCTION__, size_tmp );
   508     }
   509 #endif
   510   }
   511   
   512   return src->content_size;	
   513 
   514 }
   515 
   516 /* create a socket for connecting to remote server */
   517 static gboolean
   518 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   519 {
   520   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   521 
   522   GString *chain_id_local = NULL;
   523 
   524   gboolean ret = TRUE;
   525 
   526   if (src->unique_setup == FALSE) {
   527     src->unique_setup = TRUE;
   528   } else {
   529     goto done;
   530   }
   531 
   532   GST_OBJECT_LOCK(src);
   533 
   534   if ( src->live_tv ) {
   535     src->spawn_livetv = myth_livetv_new( );
   536     if ( myth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   537       ret = FALSE;
   538       goto init_failed;
   539     }
   540     /* set up the uri variable */
   541     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   542     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   543     if ( chain_id_local != NULL ) {
   544       src->live_chain_id = g_strdup( chain_id_local->str );
   545       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   546     }
   547     src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
   548     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   549   }
   550 
   551   src->file_transfer = myth_file_transfer_new( src->live_tv_id, 
   552       g_string_new( src->uri_name ), -1, src->mythtv_version );
   553 
   554   if ( src->file_transfer == NULL ) {
   555     GST_OBJECT_UNLOCK(src);
   556 
   557     goto init_failed;
   558   }
   559 
   560   /* sets the Playback monitor connection */
   561   ret = myth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   562 
   563   if ( src->live_tv == TRUE && ret == TRUE ) {
   564     /* loop finished, set the max tries variable to zero again... */
   565     wait_to_transfer = 0;
   566 
   567     while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS && ( myth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   568 	  /*|| ( myth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   569       g_usleep( 100 );
   570   }
   571 
   572   /* sets the FileTransfer instance connection (video/audio download) */
   573   ret = myth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   574 
   575   if ( ret == FALSE ) {
   576     GST_OBJECT_UNLOCK(src);
   577 #ifndef GST_DISABLE_GST_DEBUG  
   578     if ( src->mythtv_msgs_dbg )
   579       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   580 #endif
   581     goto begin_req_failed;
   582   }
   583 
   584   src->content_size = src->file_transfer->filesize;
   585 
   586   GST_OBJECT_UNLOCK(src);
   587   if ( src->live_tv ) {
   588 
   589     //GError* error;
   590     //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
   591     g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
   592 
   593   }
   594   src->do_start = TRUE;  
   595 
   596 done:
   597   return TRUE;
   598 
   599   /* ERRORS */
   600 init_failed:
   601   {
   602     if (src->spawn_livetv != NULL )
   603       g_object_unref( src->spawn_livetv );
   604 
   605     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   606 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   607     return FALSE;
   608   }
   609 begin_req_failed:
   610   {
   611     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   612 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   613     return FALSE;
   614   }
   615 }
   616 
   617 #if 0
   618 /* handles queries for location in the stream in the requested format */
   619 static gboolean
   620 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
   621 {
   622   gboolean res = TRUE;
   623   GstMythtvSrc *mythtv;
   624 
   625   guint64 size = 0;
   626 
   627   mythtv = GST_MYTHTV_SRC( GST_PAD_PARENT (pad) );
   628 
   629   size = gst_mythtv_src_get_position (mythtv);
   630 
   631   switch (GST_QUERY_TYPE (query)) {
   632 
   633     case GST_QUERY_POSITION:
   634       {
   635 
   636 	//GstFormat format;
   637 	gint64 cur = 0;
   638 
   639 	/* save requested format */
   640 	gst_query_parse_position (query, NULL, &cur);
   641 
   642 	/* query peer for current position in time */
   643 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   644 	    ( size > cur ) ? "greater" : "lower", size, cur );
   645 	gst_query_set_position (query, GST_FORMAT_BYTES, size);
   646 	if ( size < cur )
   647 	  goto error;
   648 
   649 	break;
   650       }
   651      #if 0
   652     case GST_QUERY_DURATION:
   653       {
   654 	//GstFormat format;
   655 	gint64 cur = 0;
   656 
   657 	/* save requested format */
   658 	gst_query_parse_position (query, NULL, &cur);
   659 
   660 	/* query peer for current position in time */
   661 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
   662 	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
   663 	    cur * GST_SECOND );
   664 	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
   665 
   666 	if ( size * GST_SECOND < cur * GST_SECOND )
   667 	  goto error;
   668 
   669 	break;
   670       }
   671 	#endif
   672     default:
   673       res = FALSE;
   674       break;
   675   }
   676 
   677   return res;
   678 
   679 error:
   680 
   681   return FALSE;
   682 }
   683 #endif
   684 
   685 static gboolean
   686 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   687 {
   688   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   689   gboolean ret = TRUE;
   690 
   691   if (src->content_size <= 0) {
   692     ret= FALSE;
   693   } else if ( src->content_size >= src->read_offset && 
   694       			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
   695     g_static_mutex_lock( &update_size_mutex );
   696 
   697   	guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
   698   	if ( src->content_size < new_offset ) {
   699   	  src->content_size = new_offset;
   700   	}
   701  
   702 #if 0
   703 #if ENABLE_TIMING_POSITION == 1
   704     guint64 size_tmp = 0;
   705     if (src->live_tv == TRUE) {
   706 get_file_pos:
   707       g_usleep( 5 );
   708       size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   709       if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   710 	src->content_size = size_tmp;
   711       else
   712 	goto get_file_pos;
   713       g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
   714 	  __FUNCTION__, size_tmp );
   715     }
   716 #endif
   717 #endif
   718     g_static_mutex_unlock( &update_size_mutex );
   719 
   720   }
   721 
   722   *size = src->content_size;
   723   g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
   724 
   725   return ret;
   726 
   727 }
   728 
   729 /* close the socket and associated resources
   730  * used both to recover from errors and go to NULL state */
   731   static gboolean
   732 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   733 {
   734   GstMythtvSrc *src;
   735 
   736   src = GST_MYTHTV_SRC (bsrc);
   737 
   738   if (src->uri_name) {
   739     g_free (src->uri_name);
   740     src->uri_name = NULL;
   741   }
   742 
   743   if (src->mythtv_caps) {
   744     gst_caps_unref (src->mythtv_caps);
   745     src->mythtv_caps = NULL;
   746   }
   747 
   748   src->eos = FALSE;
   749 
   750   return TRUE;
   751 }
   752 
   753 static gboolean
   754 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   755 {
   756   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   757 
   758   switch (GST_EVENT_TYPE (event)) {
   759 #if 0
   760     case GST_EVENT_FLUSH_START:
   761       src->eos = FALSE;
   762       break;
   763       //return TRUE;
   764     case GST_EVENT_FLUSH_STOP:
   765       src->do_start = TRUE;
   766       src->eos = FALSE;
   767       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
   768       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
   769       break;
   770 #endif
   771     case GST_EVENT_EOS:
   772       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
   773 	  guint64 cont_size = gst_mythtv_src_get_position (src);
   774 	  if ( cont_size > src->content_size ) {
   775 	  	src->content_size = cont_size;
   776 	  	src->eos = FALSE;
   777   	  } else {
   778       	src->eos = TRUE;
   779       	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   780       	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   781   	  }
   782       break;
   783 #if 0
   784     case GST_EVENT_NEWSEGMENT:
   785       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
   786       src->eos = FALSE;
   787       break;
   788     case GST_EVENT_SEEK:  	  
   789       {
   790 	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
   791 	gdouble rate;
   792 	//gboolean update = TRUE;
   793 	GstFormat format;
   794 	GstSeekType cur_type, stop_type;
   795 	GstSeekFlags flags;
   796 	gint64 cur = 0, stop = 0;
   797 	gst_event_parse_seek ( event, &rate, &format,
   798 	    &flags, &cur_type, &cur,
   799 	    &stop_type, &stop );
   800 
   801 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
   802 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   803 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   804 	}
   805 	//gboolean ret = gst_event_parse_new_segment ( event,
   806 	//    &update, &rate, &format, &start, &stop,
   807 	//    &position );
   808 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
   809 	//			cur, stop - cur + 1, GstBuffer)
   810       }
   811 #endif
   812     default:
   813       return gst_pad_event_default (pad, event);
   814   }
   815 
   816   return gst_pad_event_default (pad, event);
   817 }
   818 
   819 static gboolean
   820 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
   821 {
   822   return TRUE;
   823 }
   824 
   825 static void
   826 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   827     const GValue * value, GParamSpec * pspec)
   828 {
   829   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   830 
   831   GST_OBJECT_LOCK (mythtvsrc);
   832   switch (prop_id) {
   833     case PROP_URI:
   834     case PROP_LOCATION:
   835       {
   836 	if (!g_value_get_string (value)) {
   837 	  GST_WARNING ("location property cannot be NULL");
   838 	  goto done;
   839 	}
   840 
   841 	if (mythtvsrc->uri_name != NULL) {
   842 	  g_free (mythtvsrc->uri_name);
   843 	  mythtvsrc->uri_name = NULL;
   844 	}
   845 	mythtvsrc->uri_name = g_value_dup_string (value);
   846 
   847 	break;
   848       }
   849 #ifndef GST_DISABLE_GST_DEBUG
   850     case PROP_MYTHTV_DBG:
   851       {
   852 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
   853 	break;
   854       }
   855 #endif
   856     case PROP_MYTHTV_VERSION:
   857       {
   858 	mythtvsrc->mythtv_version = g_value_get_int (value);
   859 	break;
   860       }
   861     case PROP_MYTHTV_LIVEID:
   862       {
   863 	mythtvsrc->live_tv_id = g_value_get_int (value);
   864 	break;
   865       }
   866     case PROP_MYTHTV_LIVE:
   867       {
   868 	mythtvsrc->live_tv = g_value_get_boolean (value);
   869 	break;
   870       }
   871     case PROP_MYTHTV_LIVE_CHAINID:
   872       {
   873 	if (!g_value_get_string (value)) {
   874 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
   875 	  goto done;
   876 	}
   877 
   878 	if (mythtvsrc->live_chain_id != NULL) {
   879 	  g_free (mythtvsrc->live_chain_id);
   880 	  mythtvsrc->live_chain_id = NULL;
   881 	}
   882 	mythtvsrc->live_chain_id = g_value_dup_string (value);
   883 
   884 	break;
   885       }
   886 
   887     default:
   888       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   889       break;
   890   }
   891   GST_OBJECT_UNLOCK (mythtvsrc);
   892 done:
   893   return;
   894 }
   895 
   896   static void
   897 gst_mythtv_src_get_property (GObject * object, guint prop_id,
   898     GValue * value, GParamSpec * pspec)
   899 {
   900   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   901 
   902   GST_OBJECT_LOCK (mythtvsrc);
   903   switch (prop_id) {
   904     case PROP_URI:
   905     case PROP_LOCATION:
   906       {
   907 	gchar *str = g_strdup( "" );
   908 
   909 	if ( mythtvsrc->uri_name == NULL ) {
   910 	  g_free (mythtvsrc->uri_name);
   911 	  mythtvsrc->uri_name = NULL;
   912 	} else {
   913 	  str = g_strdup( mythtvsrc->uri_name );
   914 	}
   915 	g_value_set_string ( value, str );
   916 	break;
   917       }
   918 #ifndef GST_DISABLE_GST_DEBUG
   919     case PROP_MYTHTV_DBG:
   920       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
   921       break;
   922 #endif
   923     case PROP_MYTHTV_VERSION:
   924       {
   925 	g_value_set_int ( value, mythtvsrc->mythtv_version );
   926 	break;
   927       }
   928     case PROP_MYTHTV_LIVEID:
   929       {
   930 	g_value_set_int ( value, mythtvsrc->live_tv_id );
   931 	break;
   932       }
   933     case PROP_MYTHTV_LIVE:
   934       g_value_set_boolean ( value, mythtvsrc->live_tv );
   935       break;
   936     case PROP_MYTHTV_LIVE_CHAINID:
   937       {
   938 	gchar *str = g_strdup( "" );
   939 
   940 	if ( mythtvsrc->live_chain_id == NULL ) {
   941 	  g_free (mythtvsrc->live_chain_id);
   942 	  mythtvsrc->live_chain_id = NULL;
   943 	} else {
   944 	  str = g_strdup( mythtvsrc->live_chain_id );
   945 	}
   946 	g_value_set_string ( value, str );
   947 	break;
   948       }
   949     default:
   950       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   951       break;
   952   }
   953   GST_OBJECT_UNLOCK (mythtvsrc);
   954 }
   955 
   956 /* entry point to initialize the plug-in
   957  * initialize the plug-in itself
   958  * register the element factories and pad templates
   959  * register the features
   960  */
   961   static gboolean
   962 plugin_init (GstPlugin * plugin)
   963 {
   964   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
   965       GST_TYPE_MYTHTV_SRC);
   966 }
   967 
   968 /* this is the structure that gst-register looks for
   969  * so keep the name plugin_desc, or you cannot get your plug-in registered */
   970 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   971     GST_VERSION_MINOR,
   972     "mythtv",
   973     "lib MythTV src",
   974     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
   975 
   976 
   977 /*** GSTURIHANDLER INTERFACE *************************************************/
   978   static guint 
   979 gst_mythtv_src_uri_get_type (void)
   980 {
   981   return GST_URI_SRC;
   982 }
   983 
   984   static gchar **
   985 gst_mythtv_src_uri_get_protocols (void)
   986 {
   987   static gchar *protocols[] = { "myth", "myths", NULL };
   988 
   989   return protocols;
   990 }
   991 
   992   static const gchar *
   993 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
   994 {
   995   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   996 
   997   return src->uri_name;
   998 }
   999 
  1000   static gboolean
  1001 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1002 {
  1003   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1004 
  1005   gchar *protocol;
  1006 
  1007   protocol = gst_uri_get_protocol (uri);
  1008   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1009     g_free (protocol);
  1010     return FALSE;
  1011   }
  1012   g_free (protocol);
  1013   g_object_set (src, "location", uri, NULL);
  1014 
  1015   return TRUE;
  1016 }
  1017 
  1018   static void
  1019 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1020 {
  1021   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1022 
  1023   iface->get_type = gst_mythtv_src_uri_get_type;
  1024   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1025   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1026   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1027 }
  1028 
  1029   void
  1030 size_header_handler (void *userdata, const char *value)
  1031 {
  1032   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1033 
  1034   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1035 
  1036   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1037 }