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