gst-plugins-mythtv/src/gstmythtvsrc.c
author leo_sobral
Mon Nov 13 20:53:58 2006 +0000 (2006-11-13)
branchtrunk
changeset 83 7550ece15943
parent 71 55c7f31b85aa
child 87 9ea342c364de
permissions -rwxr-xr-x
[svn r84] teste case: bug debug
     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/gmyth_file_transfer.h>
    22 #include <gmyth/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 GST_GMYTHTV_CHANNEL_NUM			1000
    36 
    37 #define GMYTHTV_VERSION_DEFAULT			30
    38  
    39 #define GMYTHTV_TRANSFER_MAX_WAITS	100
    40 
    41 #define GMYTHTV_TRANSFER_MAX_BUFFER	128*1024
    42 //( 32*1024  )
    43 
    44 /* 4*1024 ??? */
    45 #define MAX_READ_SIZE              	2*1024
    46 //( 32*1024 )
    47 
    48 #define GST_FLOW_ERROR_NO_DATA  		-101
    49 
    50 /* stablish a maximum iteration value to the IS_RECORDING message */
    51 static guint wait_to_transfer = 0;
    52 
    53 static const GstElementDetails gst_mythtv_src_details =
    54 GST_ELEMENT_DETAILS ( "MythTV client source",
    55     "Source/Network",
    56     "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
    57     "Rosfran Borges <rosfran.borges@indt.org.br>" );
    58 
    59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
    60     GST_PAD_SRC,
    61     GST_PAD_ALWAYS,
    62     GST_STATIC_CAPS ("video/x-nuv") );
    63     
    64 enum
    65 {
    66   PROP_0,
    67   PROP_LOCATION,
    68   PROP_URI,
    69 #ifndef GST_DISABLE_GST_DEBUG
    70   PROP_GMYTHTV_DBG,
    71 #endif
    72   PROP_GMYTHTV_VERSION,
    73   PROP_GMYTHTV_LIVE,
    74   PROP_GMYTHTV_LIVEID,
    75   PROP_GMYTHTV_LIVE_CHAINID,
    76   PROP_GMYTHTV_ENABLE_TIMING_POSITION,
    77   PROP_GMYTHTV_CHANNEL_NUM
    78 };
    79 
    80 static void gst_mythtv_src_finalize (GObject * gobject);
    81 
    82 /*
    83 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
    84 	guint size, GstBuffer ** outbuf);
    85 */
    86 
    87 //static GstFlowReturn gst_mythtv_src_chain ( GstPad* pad, GstBuffer* outbuf );
    88 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
    89 
    90 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
    91 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
    92 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
    93 //static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
    94 
    95 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
    96 
    97 static GstStateChangeReturn
    98 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
    99 
   100 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
   101     const GValue * value, GParamSpec * pspec);
   102 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
   103     GValue * value, GParamSpec * pspec);
   104 
   105 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
   106 
   107 //static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
   108 //static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
   109 
   110 static gint do_read_request_response (GstMythtvSrc *src, guint64 offset, 
   111 		guint size, GstBuffer **outbuf);
   112 
   113 static void
   114 _urihandler_init (GType type)
   115 {
   116   static const GInterfaceInfo urihandler_info = {
   117     gst_mythtv_src_uri_handler_init,
   118     NULL,
   119     NULL
   120   };
   121 
   122   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
   123 
   124   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   125       "MythTV src");
   126 }
   127 
   128 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
   129 //    GST_TYPE_BASE_SRC, _urihandler_init)
   130     
   131 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
   132     GST_TYPE_PUSH_SRC, _urihandler_init)
   133     
   134   static void
   135 gst_mythtv_src_base_init (gpointer g_class)
   136 {
   137   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   138 
   139   gst_element_class_add_pad_template (element_class,
   140       gst_static_pad_template_get (&srctemplate));
   141       
   142   gst_element_class_set_details (element_class, &gst_mythtv_src_details);
   143   
   144   element_class->change_state = gst_mythtv_src_change_state;
   145   
   146 }
   147 
   148 static void
   149 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
   150 {
   151   GObjectClass *gobject_class; 
   152   GstPushSrcClass *gstpushsrc_class;
   153   GstBaseSrcClass *gstbasesrc_class;
   154 
   155   gobject_class = (GObjectClass *) klass;
   156   gstbasesrc_class = (GstBaseSrcClass *) klass;
   157   gstpushsrc_class = (GstPushSrcClass *) klass;
   158 
   159   gobject_class->set_property = gst_mythtv_src_set_property;
   160   gobject_class->get_property = gst_mythtv_src_get_property;
   161   gobject_class->finalize = gst_mythtv_src_finalize;
   162 
   163   g_object_class_install_property
   164     (gobject_class, PROP_LOCATION,
   165      g_param_spec_string ("location", "Location",
   166        "The location. In the form:"
   167        "\n\t\t\tmyth://a.com/file.nuv"
   168        "\n\t\t\tmyth://a.com:23223/file.nuv "
   169        "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
   170        "", G_PARAM_READWRITE));
   171 
   172   g_object_class_install_property
   173     (gobject_class, PROP_URI,
   174      g_param_spec_string ("uri", "Uri",
   175        "The location in form of a URI (deprecated; use location)",
   176        "", G_PARAM_READWRITE));
   177 
   178   g_object_class_install_property
   179     (gobject_class, PROP_GMYTHTV_VERSION,
   180      g_param_spec_int ("mythtv-version", "mythtv-version",
   181        "Change MythTV version",
   182        26, 30, 26, G_PARAM_READWRITE));
   183 
   184   g_object_class_install_property
   185     (gobject_class, PROP_GMYTHTV_LIVEID,
   186      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   187        "Change MythTV version",
   188        0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
   189 
   190   g_object_class_install_property
   191     (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
   192      g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
   193        "Sets the MythTV chain ID (from TV Chain)",
   194        "", G_PARAM_READWRITE));
   195 
   196   g_object_class_install_property
   197     (gobject_class, PROP_GMYTHTV_LIVE,
   198      g_param_spec_boolean ("mythtv-live", "mythtv-live",
   199        "Enable MythTV Live TV content streaming",
   200        FALSE, G_PARAM_READWRITE));
   201 
   202   g_object_class_install_property
   203     (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
   204      g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
   205        "Enable MythTV Live TV content size continuous updating",
   206        FALSE, G_PARAM_READWRITE));
   207        
   208   g_object_class_install_property
   209     (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
   210      g_param_spec_int ("mythtv-channel", "mythtv-channel",
   211        "Change MythTV channel number",
   212        0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
   213 
   214 #ifndef GST_DISABLE_GST_DEBUG
   215   g_object_class_install_property
   216     (gobject_class, PROP_GMYTHTV_DBG,
   217      g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
   218        "Enable MythTV debug messages",
   219        FALSE, G_PARAM_READWRITE));
   220 #endif
   221 
   222   gstbasesrc_class->start = gst_mythtv_src_start;
   223   gstbasesrc_class->stop = gst_mythtv_src_stop;
   224   gstbasesrc_class->get_size = gst_mythtv_src_get_size;
   225   //gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
   226   
   227   //gstbasesrc_class->create = gst_mythtv_src_create;
   228   gstpushsrc_class->create = gst_mythtv_src_create;
   229     
   230   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   231       "MythTV Client Source");
   232 }
   233 
   234 static void
   235 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
   236 {
   237   this->file_transfer = NULL;
   238 
   239   this->unique_setup = FALSE;
   240 
   241   this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
   242   
   243   this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
   244 
   245   this->bytes_read = 0;
   246   
   247   this->prev_content_size = 0;
   248 
   249   this->content_size = 0;
   250   this->read_offset = 0;
   251 
   252   this->content_size_last = 0;
   253 
   254   this->live_tv = FALSE;
   255   
   256   this->enable_timing_position = FALSE;
   257   this->update_prog_chain = FALSE;    
   258 
   259   this->user_agent = g_strdup ("mythtvsrc");
   260   this->mythtv_caps = NULL;
   261   this->update_prog_chain = FALSE;
   262   
   263   this->channel_num = 0;
   264   
   265   this->eos = FALSE;
   266   
   267   this->adapter = NULL;
   268   
   269   //this->th_read_ahead = NULL;
   270   
   271   this->th_mutex = NULL;
   272   
   273   this->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
   274   //gst_pad_set_chain_function ( GST_BASE_SRC_PAD( GST_BASE_SRC( this ) ), 
   275   //		GST_DEBUG_FUNCPTR ( gst_mythtv_src_chain ) );
   276   gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
   277   
   278   //gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
   279 
   280   gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
   281   
   282  // gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   283   //    gst_mythtv_src_handle_event );
   284 
   285 }
   286 
   287 static void
   288 gst_mythtv_src_finalize (GObject * gobject)
   289 {
   290   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   291   
   292   if ( this->th_mutex != NULL ) {
   293   	g_static_rec_mutex_free( this->th_mutex );
   294   	this->th_mutex = NULL;
   295   }
   296 
   297   if ( this->th_read_ahead != NULL ) {
   298   	gst_task_stop( this->th_read_ahead );
   299   	this->th_read_ahead = NULL;
   300   }
   301 
   302   if (this->mythtv_caps) {
   303     gst_caps_unref (this->mythtv_caps);
   304     this->mythtv_caps = NULL;
   305   }
   306 
   307   if (this->file_transfer) {
   308     g_object_unref (this->file_transfer);
   309     this->file_transfer = NULL;
   310   }
   311 
   312 	if (this->spawn_livetv) {
   313     g_object_unref (this->spawn_livetv);
   314     this->spawn_livetv = NULL;
   315   }
   316 
   317   if (this->uri_name) {
   318     g_free (this->uri_name);
   319   }
   320 
   321   if (this->user_agent) {
   322     g_free (this->user_agent);
   323   }
   324 
   325   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   326 }
   327 
   328 static gint
   329 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
   330 {
   331   gint read = 0;
   332   guint sizetoread = size;
   333 
   334   g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   335 
   336   /* Loop sending the Myth File Transfer request:
   337    * Retry whilst authentication fails and we supply it. */
   338   gint len = 0;
   339   gint8 *data_ptr = g_malloc0( size );
   340   
   341   //GST_OBJECT_LOCK(src);
   342 
   343   while ( sizetoread > 0 ) {
   344 
   345     len = gmyth_file_transfer_read( src->file_transfer,
   346 						data_ptr + read, sizetoread, TRUE );
   347 
   348     if ( len > 0 ) {
   349       read += len;
   350       sizetoread -= len;
   351     } 
   352     else if ( len <= 0 )
   353     {
   354     	read = -1;
   355 
   356       if ( src->live_tv == FALSE ) 
   357       {
   358 				goto eos;
   359       } 
   360       else  
   361       {
   362 				if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
   363 				  src->update_prog_chain = TRUE;
   364 					goto done;	  	
   365 				} else 
   366 				  if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
   367 				    src->update_prog_chain = TRUE;
   368 				    if ( src->enable_timing_position ) {
   369 				      gint64 size_tmp = 0;
   370 			get_file_pos:
   371 					size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   372 					if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   373 					  src->content_size = size_tmp;
   374 					else if ( size_tmp > 0 )
   375 					  goto get_file_pos;
   376 					g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   377 					    __FUNCTION__, size_tmp );
   378 				    } /*else {				      
   379 				      gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
   380 				      if ( new_offset > 0 && src->content_size <= new_offset ) {
   381 								  src->content_size = new_offset;
   382 				      } else {
   383 								src->update_prog_chain = TRUE;
   384 				      }
   385 				      src->prev_content_size = src->content_size;
   386 				    }*/
   387 				  }
   388 			goto done;
   389     }
   390 
   391     }
   392 
   393     if ( read == sizetoread )
   394       break;
   395   }
   396 
   397   if ( read > 0 ) {
   398     src->read_offset += read;
   399     src->bytes_read += read;
   400 
   401     g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   402 			"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   403 			src->read_offset, src->content_size );
   404 
   405     GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
   406     GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   407     GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   408     g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
   409     GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
   410     GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
   411 
   412     g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   413 			"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   414 			GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   415 
   416   } else if ( !src->live_tv )
   417     goto eos;
   418 
   419   goto done;
   420 
   421 eos:
   422   src->eos = TRUE;
   423 
   424 done:
   425   //GST_OBJECT_UNLOCK(src);
   426 
   427   return read;
   428 }
   429 
   430 #if 0
   431 static GstFlowReturn
   432 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
   433 {
   434   GstMythtvSrc *src;
   435   GstFlowReturn ret = GST_FLOW_OK;
   436   gint read = -1;
   437   gint adapter_size = -1;
   438   
   439   guint max_adapter_rep = 40;
   440   
   441   src = GST_MYTHTV_SRC (psrc);
   442   
   443   /* The caller should know the number of bytes and not read beyond EOS. */
   444   if (G_UNLIKELY (src->eos))
   445     goto eos;
   446   if ( G_UNLIKELY (src->update_prog_chain) )
   447     goto change_progchain;
   448 
   449   g_static_rec_mutex_lock( src->th_mutex );
   450   
   451   while ( ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < size ) &&
   452   		--max_adapter_rep > 0 )
   453   {
   454   	g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
   455   	GST_TASK_WAIT( src->th_read_ahead );
   456   }
   457   	
   458   g_static_rec_mutex_unlock( src->th_mutex );
   459 
   460 	gint64 new_offset = -1;
   461 	/* just get from the adapter, no network effort... */
   462 	if ( offset > src->adapter_offset && size <= adapter_size )	
   463 	{
   464 		
   465 		GstBuffer *buf = gst_adapter_take_buffer( src->adapter, size );
   466 		*outbuf = gst_buffer_create_sub( buf, offset, size );
   467 		src->read_offset = new_offset = offset;
   468 		read = size;
   469 		
   470 		gst_adapter_flush( src->adapter, size );
   471 		
   472 	} else {
   473 		/* no data on adapter... do all these mythtv network calls! */
   474 		
   475 		/* verify if it needs to seek */
   476 		if ( src->read_offset != offset ) 
   477   	{
   478 		
   479 	  	new_offset = gmyth_file_transfer_seek( src->file_transfer, offset, SEEK_SET );
   480 	  	
   481 	    g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   482 	    	__FUNCTION__, src->read_offset, new_offset );
   483 	    if ( G_UNLIKELY (new_offset < 0 ) )
   484 	    {
   485 	      if ( src->live_tv )
   486 	      	goto change_progchain;
   487 	      else
   488 	      	goto eos;
   489 	    }
   490 	    
   491   	}
   492 	
   493     src->read_offset = offset;
   494   	
   495 	  /* Create the buffer. */
   496 	  ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   497 	      src->read_offset, size,
   498 	      GST_PAD_CAPS ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)) ), outbuf);
   499 	
   500 	  if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   501 	    if ( src->live_tv )
   502 	    	goto change_progchain;
   503 	    else
   504 	    	goto done;
   505 	  }
   506 	  
   507 	  read = do_read_request_response ( src, src->read_offset, size, outbuf ); 
   508   	
   509 	}
   510 
   511   if (G_UNLIKELY (src->update_prog_chain) )
   512     goto change_progchain;
   513 
   514   if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
   515   	if ( src->live_tv )
   516     	goto change_progchain;
   517     else
   518     	goto read_error;
   519   }
   520   
   521   if ( read > 0 ) {
   522     src->read_offset += read;
   523     src->bytes_read += read;
   524     
   525     #if 0
   526     g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   527 			"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   528 			src->read_offset, src->content_size );
   529 
   530     GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
   531     //GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   532     //GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   533     //g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
   534     GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
   535     GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
   536 
   537     g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   538 			"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   539 			GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   540 		#endif
   541 			
   542   }
   543 
   544 done:
   545  {
   546     const gchar *reason = gst_flow_get_name (ret);
   547 
   548     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   549   	return ret;
   550  }
   551 eos:
   552   {
   553     const gchar *reason = gst_flow_get_name (ret);
   554 
   555     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   556     return GST_FLOW_UNEXPECTED;
   557   }
   558   /* ERRORS */
   559 read_error:
   560   {
   561     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   562 	(NULL), ("Could not read any bytes (%i, %s)", read,
   563 	  src->uri_name));
   564     return GST_FLOW_ERROR;
   565   }
   566 change_progchain:
   567   {
   568     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   569 		(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   570 		  src->uri_name));
   571 		  
   572 		gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   573   			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   574 		// go to the next program chain
   575 		src->unique_setup = FALSE;
   576 		src->update_prog_chain = TRUE;
   577 		
   578 		gst_mythtv_src_next_program_chain( src );
   579 		
   580     return GST_FLOW_ERROR_NO_DATA;
   581   }
   582 
   583 }
   584 #endif
   585 
   586 static GstFlowReturn
   587 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
   588 {
   589   GstMythtvSrc *src;
   590   GstFlowReturn ret = GST_FLOW_OK;
   591   gint read = -1;
   592   
   593   src = GST_MYTHTV_SRC ( psrc );
   594   
   595   /* The caller should know the number of bytes and not read beyond EOS. */
   596   if (G_UNLIKELY (src->eos))
   597     goto eos;
   598   if ( G_UNLIKELY (src->update_prog_chain) )
   599     goto change_progchain;
   600 
   601 	//gint64 new_offset = -1;
   602 	/* just get from the adapter, no network effort... */
   603 	//do	
   604 	//{		
   605 	/* Create the buffer. */
   606   ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   607       GST_BUFFER_OFFSET_NONE, MAX_READ_SIZE,
   608       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
   609 
   610   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   611     if ( src->live_tv )
   612     	goto change_progchain;
   613     else
   614     	goto done;
   615   }
   616   
   617   read = do_read_request_response ( src, src->read_offset, MAX_READ_SIZE, outbuf );	  
   618   
   619   if (G_UNLIKELY (src->update_prog_chain) )
   620     goto change_progchain;
   621 
   622   if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
   623   	if ( src->live_tv )
   624     	goto change_progchain;
   625     else
   626     	goto read_error;
   627   }
   628 	  							
   629 	//} while ( TRUE );
   630 
   631 done:
   632  {
   633     const gchar *reason = gst_flow_get_name (ret);
   634 
   635     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   636   	return ret;
   637  }
   638 eos:
   639   {
   640     const gchar *reason = gst_flow_get_name (ret);
   641 
   642     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   643     return GST_FLOW_UNEXPECTED;
   644   }
   645   /* ERRORS */
   646 read_error:
   647   {
   648     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   649 	(NULL), ("Could not read any bytes (%i, %s)", read,
   650 	  src->uri_name));
   651     return GST_FLOW_ERROR;
   652   }
   653 change_progchain:
   654   {
   655     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   656 		(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   657 		  src->uri_name));
   658 		  
   659 		gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   660   			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   661 		// go to the next program chain
   662 		src->unique_setup = FALSE;
   663 		src->update_prog_chain = TRUE;
   664 		
   665 		gst_mythtv_src_next_program_chain( src );
   666 		
   667     return GST_FLOW_ERROR_NO_DATA;
   668   }
   669 
   670 }
   671 
   672 gint64
   673 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   674 {
   675 
   676   gint64 size_tmp = 0;
   677   guint max_tries = 2;
   678   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   679 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   680 
   681 get_file_pos:
   682     g_usleep( 10 );
   683     size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
   684     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   685       src->content_size = size_tmp;
   686     else if ( size_tmp > 0 && --max_tries > 0 )
   687       goto get_file_pos;
   688     g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
   689 	__FUNCTION__, size_tmp );
   690     /* sets the last content size amount before it can be updated */
   691     src->prev_content_size = src->content_size;
   692   }
   693 
   694   return src->content_size;	
   695 
   696 }
   697 
   698 #if 0
   699 static void 
   700 gst_mythtv_src_read_ahead ( void *data ) {
   701 	
   702 	GstMythtvSrc *src = NULL;
   703 	
   704 	GstBuffer *buffer = NULL;
   705 	
   706 	guint size = 512;
   707 	
   708 	gint read = 0;
   709 	
   710 	src = GST_MYTHTV_SRC( data );
   711 	
   712 	GST_PAD_STREAM_TRYLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   713 	
   714 	/*if ( gst_adapter_available( src->adapter ) ) && ( read < MAX_READ_SIZE )) */
   715 	do {
   716 		buffer = gst_buffer_new_and_alloc( size );
   717 
   718 		read += do_read_request_response ( src, src->adapter_offset, size, &buffer );
   719 		
   720 		gst_adapter_push( src->adapter, buffer );
   721 		
   722 		//gst_buffer_unref( buffer );		
   723 	} while ( read < MAX_READ_SIZE );
   724 	
   725 	GST_PAD_BLOCK_SIGNAL( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   726 	
   727 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   728 	
   729 	gst_object_unref( src );
   730 	
   731 	return;	
   732 }
   733 #endif
   734 
   735 /* create a socket for connecting to remote server */
   736 static gboolean
   737 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   738 {
   739   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   740 
   741   GString *chain_id_local = NULL;
   742 
   743   gboolean ret = TRUE;
   744   
   745   if ( G_UNLIKELY (src->update_prog_chain) )
   746     goto change_progchain;
   747 
   748   if (src->unique_setup == FALSE) {
   749     src->unique_setup = TRUE;
   750   } else {
   751     goto done;
   752   }
   753 
   754   //GST_OBJECT_LOCK(src);
   755 
   756   if ( src->live_tv ) {
   757     src->spawn_livetv = gmyth_livetv_new( );
   758     if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   759       ret = FALSE;
   760       //GST_OBJECT_UNLOCK( src );
   761       goto init_failed;
   762     }    
   763 
   764     /* set up the uri variable */
   765     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   766     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   767     if ( chain_id_local != NULL ) {
   768       src->live_chain_id = g_strdup( chain_id_local->str );
   769       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   770     }
   771     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   772     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
   773   }
   774 
   775   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   776       g_string_new( src->uri_name ), -1, src->mythtv_version );
   777 
   778   if ( src->file_transfer == NULL ) {
   779     //GST_OBJECT_UNLOCK(src);
   780 
   781     goto init_failed;
   782   }
   783 
   784   /* sets the Playback monitor connection */
   785   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   786 
   787   if ( src->live_tv == TRUE && ret == TRUE ) {
   788     /* loop finished, set the max tries variable to zero again... */
   789     wait_to_transfer = 0;
   790 
   791     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   792     		( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   793 	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   794       g_usleep( 100 );
   795   }
   796 
   797   /* sets the FileTransfer instance connection (video/audio download) */
   798   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   799 
   800   if ( ret == FALSE ) {
   801     //GST_OBJECT_UNLOCK(src);
   802 #ifndef GST_DISABLE_GST_DEBUG  
   803     if ( src->mythtv_msgs_dbg )
   804       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   805 #endif
   806     goto begin_req_failed;
   807   }
   808 
   809   src->content_size = src->file_transfer->filesize;
   810 
   811   src->do_start = FALSE;
   812   
   813   if ( src->live_tv ) {
   814   	//src->adapter = gst_adapter_new();
   815   	//g_static_rec_mutex_init( src->th_mutex );
   816   	//src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
   817   }  	
   818 
   819   //GST_OBJECT_UNLOCK(src);
   820 
   821 done:
   822   return TRUE;
   823 
   824   /* ERRORS */
   825 init_failed:
   826   {
   827     if (src->spawn_livetv != NULL )
   828       g_object_unref( src->spawn_livetv );
   829 
   830     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   831 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   832     return FALSE;
   833   }
   834 begin_req_failed:
   835   {
   836     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   837 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   838     return FALSE;
   839   }
   840 change_progchain:
   841   {
   842     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   843 		(NULL), ("Seek failed, go to the next program info... (%s)",
   844 		  src->uri_name));
   845 		  
   846 		gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   847   			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   848   			
   849 		// go to the next program chain
   850 		src->unique_setup = FALSE;
   851 		src->update_prog_chain = TRUE;
   852 		
   853 		gst_mythtv_src_next_program_chain( src );
   854 		
   855     return TRUE;
   856   }
   857 }
   858 
   859 /* create a new socket for connecting to the next program chain */
   860 static gboolean
   861 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
   862 {
   863   GString *chain_id_local = NULL;
   864 
   865   gboolean ret = TRUE;
   866 
   867   if ( !src->live_tv )
   868     goto init_failed;
   869     
   870   if (src->unique_setup == FALSE) {
   871     src->unique_setup = TRUE;
   872   } else {
   873     goto done;
   874   }
   875   
   876 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   877 
   878   if (src->file_transfer) {
   879     g_object_unref (src->file_transfer);
   880     src->file_transfer = NULL;
   881   }
   882 
   883   if (src->uri_name) {
   884     g_free (src->uri_name);
   885   }
   886 
   887   if ( src->live_tv ) {
   888     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
   889     	g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
   890       ret = FALSE;
   891       goto init_failed;
   892     }
   893     /* set up the uri variable */
   894     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   895     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   896     if ( chain_id_local != NULL ) {
   897       src->live_chain_id = g_strdup( chain_id_local->str );
   898       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   899     }
   900     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   901     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   902   }
   903 
   904   src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   905       g_string_new( src->uri_name ), -1, src->mythtv_version );
   906 
   907   if ( src->file_transfer == NULL ) {
   908     goto init_failed;
   909   }
   910 
   911   /* sets the Playback monitor connection */
   912   ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   913 
   914   if ( src->live_tv == TRUE && ret == TRUE ) {
   915     /* loop finished, set the max tries variable to zero again... */
   916     wait_to_transfer = 0;
   917 
   918     g_usleep( 200 );
   919 
   920     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
   921 				( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
   922       g_usleep( 1000 );
   923   }
   924 
   925   /* sets the FileTransfer instance connection (video/audio download) */
   926   ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   927 
   928   if ( ret == FALSE ) {
   929 #ifndef GST_DISABLE_GST_DEBUG  
   930     if ( src->mythtv_msgs_dbg )
   931       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   932 #endif
   933     goto begin_req_failed;
   934   }
   935   src->content_size_last = src->content_size;
   936 
   937 #if 0
   938   if ( src->content_size < src->file_transfer->filesize ) {
   939     src->content_size = src->file_transfer->filesize;
   940   } else {
   941     //gint64 pos = gst_mythtv_src_get_position(src);
   942     //if ( pos > src->file_transfer->filesize )
   943     //	src->content_size = pos;  	
   944 
   945   }
   946 #endif
   947 
   948   src->content_size = src->file_transfer->filesize;
   949   while ( src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER*4 )
   950     src->content_size = gst_mythtv_src_get_position( src );
   951 
   952   src->read_offset = 0;  
   953   
   954 done:
   955 	src->update_prog_chain = FALSE;
   956 	
   957 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   958 	
   959   return TRUE;
   960 
   961   /* ERRORS */
   962 init_failed:
   963   {
   964     if (src->spawn_livetv != NULL )
   965       g_object_unref( src->spawn_livetv );
   966 
   967     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   968 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   969     return FALSE;
   970   }
   971 begin_req_failed:
   972   {
   973     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   974 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   975     return FALSE;
   976   }
   977 
   978 }
   979 
   980 #if 0
   981 /* handles queries for location in the stream in the requested format */
   982 static gboolean
   983 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
   984 {
   985   gboolean res = TRUE;
   986   GstMythtvSrc *mythtv;
   987 
   988   guint64 size = 0;
   989 
   990   mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
   991 
   992   size = gst_mythtv_src_get_position (mythtv);
   993 
   994   switch (GST_QUERY_TYPE (query)) {
   995 
   996     case GST_QUERY_POSITION:
   997       {
   998 
   999 	//GstFormat format;
  1000 	gint64 cur = 0;
  1001 
  1002 	/* save requested format */
  1003 	gst_query_parse_position (query, NULL, &cur);
  1004 
  1005 	/* query peer for current position in time */
  1006 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
  1007 	    ( size > cur ) ? "greater" : "lower", size, cur );
  1008 	gst_query_set_position (query, GST_FORMAT_BYTES, size);
  1009 	if ( size < cur )
  1010 	  goto error;
  1011 
  1012 	break;
  1013       }
  1014      #if 0
  1015     case GST_QUERY_DURATION:
  1016       {
  1017 	//GstFormat format;
  1018 	gint64 cur = 0;
  1019 
  1020 	/* save requested format */
  1021 	gst_query_parse_position (query, NULL, &cur);
  1022 
  1023 	/* query peer for current position in time */
  1024 	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
  1025 	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
  1026 	    cur * GST_SECOND );
  1027 	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
  1028 
  1029 	if ( size * GST_SECOND < cur * GST_SECOND )
  1030 	  goto error;
  1031 
  1032 	break;
  1033       }
  1034 	#endif
  1035     default:
  1036       res = FALSE;
  1037       break;
  1038   }
  1039 
  1040   return res;
  1041 
  1042 error:
  1043 
  1044   return FALSE;
  1045 }
  1046 #endif
  1047 
  1048 static gboolean
  1049 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
  1050 {
  1051   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
  1052   gboolean ret = TRUE;
  1053   g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
  1054   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
  1055 
  1056   if (src->content_size <= 0) {
  1057     ret= FALSE;
  1058   } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) < 
  1059 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
  1060     //g_static_mutex_lock( &update_size_mutex );
  1061     //GST_OBJECT_LOCK(src);
  1062     
  1063     gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
  1064     if ( new_offset > 0 && new_offset > src->content_size ) {
  1065 			src->content_size = new_offset;
  1066     } else if ( new_offset < src->content_size ) {
  1067 			src->update_prog_chain = TRUE;
  1068 	  }
  1069 
  1070     if ( src->enable_timing_position ) {
  1071       gint64 size_tmp = 0;
  1072       if (src->live_tv == TRUE) {
  1073 get_file_pos:
  1074 	g_usleep( 5 );
  1075 	size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
  1076 	if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
  1077 	  src->content_size = size_tmp;
  1078 	else if ( size_tmp > 0  )
  1079 	  goto get_file_pos;
  1080 	g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
  1081 	    __FUNCTION__, size_tmp );
  1082       }
  1083     }
  1084     
  1085     src->prev_content_size = src->content_size;
  1086     
  1087     //GST_OBJECT_UNLOCK(src);
  1088     //g_static_mutex_unlock( &update_size_mutex );
  1089   }
  1090 
  1091   *size = src->content_size;
  1092   g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
  1093 
  1094   return ret;
  1095 
  1096 }
  1097 
  1098 /* close the socket and associated resources
  1099  * used both to recover from errors and go to NULL state */
  1100 static gboolean
  1101 gst_mythtv_src_stop (GstBaseSrc * bsrc)
  1102 {
  1103   GstMythtvSrc *src;
  1104 
  1105   src = GST_MYTHTV_SRC (bsrc);
  1106 
  1107   if (src->uri_name) {
  1108     g_free (src->uri_name);
  1109     src->uri_name = NULL;
  1110   }
  1111 
  1112   if (src->mythtv_caps) {
  1113     gst_caps_unref (src->mythtv_caps);
  1114     src->mythtv_caps = NULL;
  1115   }
  1116 
  1117   src->eos = FALSE;
  1118 
  1119   return TRUE;
  1120 }
  1121 
  1122 #if 0
  1123 static gboolean
  1124 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
  1125 {
  1126   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
  1127   gint64 cont_size = 0;
  1128 
  1129   switch (GST_EVENT_TYPE (event)) {
  1130 #if 0
  1131     case GST_EVENT_FLUSH_START:
  1132       //src->eos = FALSE;
  1133       g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
  1134       cont_size = gst_mythtv_src_get_position (src);
  1135       if ( !src->live_tv ) {
  1136 	if ( cont_size > src->content_size ) {
  1137 	  src->content_size = cont_size;
  1138 	  src->eos = FALSE;
  1139 	} else {
  1140 	  src->eos = TRUE;
  1141 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1142 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1143 	}
  1144       } else {
  1145 	if ( cont_size <= 0 ) {
  1146 	  src->update_prog_chain = TRUE;
  1147 	  src->eos = TRUE;
  1148 	  src->unique_setup = FALSE;
  1149 	  src->do_start = TRUE;		  				  		
  1150 	}		  	
  1151       }
  1152       break;
  1153     case GST_EVENT_FLUSH_STOP:
  1154       src->do_start = TRUE;
  1155       src->eos = FALSE;
  1156       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
  1157       //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
  1158       break;
  1159 #endif
  1160     case GST_EVENT_EOS:
  1161       g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
  1162 
  1163       if ( src->live_tv ) {
  1164 	cont_size = gst_mythtv_src_get_position (src);
  1165 	if ( cont_size > src->content_size ) {
  1166 	  src->content_size = cont_size;
  1167 	  src->eos = FALSE;
  1168 	} else {
  1169 	  src->eos = TRUE;
  1170 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
  1171 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
  1172 	}
  1173       } else 
  1174 	src->eos = TRUE;
  1175       break;
  1176 #if 0
  1177     case GST_EVENT_NEWSEGMENT:
  1178       g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
  1179       src->eos = FALSE;
  1180       break;
  1181     case GST_EVENT_SEEK:  	  
  1182       {
  1183 	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
  1184 	gdouble rate;
  1185 	//gboolean update = TRUE;
  1186 	GstFormat format;
  1187 	GstSeekType cur_type, stop_type;
  1188 	GstSeekFlags flags;
  1189 	gint64 cur = 0, stop = 0;
  1190 	gst_event_parse_seek ( event, &rate, &format,
  1191 	    &flags, &cur_type, &cur,
  1192 	    &stop_type, &stop );
  1193 
  1194 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
  1195 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
  1196 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
  1197 	}
  1198 	//gboolean ret = gst_event_parse_new_segment ( event,
  1199 	//    &update, &rate, &format, &start, &stop,
  1200 	//    &position );
  1201 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
  1202 	//			cur, stop - cur + 1, GstBuffer)
  1203       }
  1204 #endif
  1205     default:
  1206       return gst_pad_event_default (pad, event);
  1207   }
  1208 
  1209   return gst_pad_event_default (pad, event);
  1210 }
  1211 #endif
  1212 
  1213 #if 0
  1214 static gboolean
  1215 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
  1216 {
  1217   return TRUE;
  1218 }
  1219 #endif
  1220 
  1221 #if 0
  1222 static GstFlowReturn
  1223 gst_mythtv_src_file_transfer( GstMythtvSrc *src )
  1224 {
  1225 	GstFlowReturn ret = GST_FLOW_OK;
  1226   GstBuffer *buf = NULL;
  1227   gint read = -1;
  1228   
  1229   /* The caller should know the number of bytes and not read beyond EOS. */
  1230   if ( G_UNLIKELY (src->eos) )
  1231     goto eos;
  1232   if ( G_UNLIKELY (src->update_prog_chain) )
  1233     goto change_progchain;
  1234 
  1235   do {
  1236   
  1237 	  /* Create the buffer. */
  1238 	  buf = gst_buffer_new_and_alloc (4096);
  1239 	  
  1240 	  ret = gst_mythtv_src_read_bytes( src, GST_BUFFER_SIZE(buf), TRUE, &buf );	  
  1241 		if ( ret != GST_FLOW_OK )
  1242 			return ret;
  1243 	  
  1244 	  read = do_read_request_response ( src, src->read_offset, GST_BUFFER_SIZE(buf), &buf );
  1245 	  
  1246 	  if (G_UNLIKELY (src->update_prog_chain) )
  1247 	    goto change_progchain;
  1248 	
  1249 	  if (G_UNLIKELY (read <= 0) || buf == NULL) {
  1250 	  	if ( src->live_tv )
  1251 	    	goto change_progchain;
  1252 	    else
  1253 	    	goto read_error;
  1254 	  }
  1255 	  
  1256 	  if ( GST_FLOW_OK != ( ret = gst_pad_push ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), buf ) ) ) {
  1257 	    goto done;
  1258 	  }  
  1259   
  1260 	} while(TRUE);
  1261 
  1262 done:
  1263  {
  1264     const gchar *reason = gst_flow_get_name (ret);
  1265 
  1266     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
  1267   	return ret;
  1268  }
  1269 eos:
  1270   {
  1271     const gchar *reason = gst_flow_get_name (ret);
  1272 
  1273     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
  1274     return GST_FLOW_UNEXPECTED;
  1275   }
  1276   /* ERRORS */
  1277 read_error:
  1278   {
  1279     GST_ELEMENT_ERROR (src, RESOURCE, READ,
  1280 	(NULL), ("Could not read any bytes (%i, %s)", read,
  1281 	  src->uri_name));
  1282     return GST_FLOW_ERROR;
  1283   }
  1284 change_progchain:
  1285   {
  1286     GST_ELEMENT_ERROR (src, RESOURCE, READ,
  1287 		(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
  1288 		  src->uri_name));
  1289 		// go to the next program chain
  1290 		src->unique_setup = FALSE;
  1291 		src->update_prog_chain = TRUE;
  1292 		src->mode = GST_MYTHTV_SRC_NEXT_PROGRAM_CHAIN;
  1293 		
  1294 		//GST_OBJECT_LOCK(src);
  1295 		//gst_mythtv_src_next_program_chain( src );
  1296 		//GST_OBJECT_UNLOCK(src);		  
  1297     return GST_FLOW_OK;//GST_FLOW_ERROR_NO_DATA;
  1298   }
  1299 }
  1300 
  1301 static GstFlowReturn
  1302 gst_mythtv_src_play (GstPad * pad)
  1303 {
  1304   GstFlowReturn res = GST_FLOW_OK;
  1305   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
  1306   
  1307 	switch ( src->state ) {
  1308     case GST_MYTHTV_SRC_FILE_TRANSFER:
  1309     	res = gst_mythtv_src_file_transfer( src );
  1310 			if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1311       	goto pause;
  1312       }
  1313       break;
  1314     case GST_MYTHTV_SRC_NEXT_PROGRAM_CHAIN:
  1315       src->read_offset = 0;
  1316 			src->bytes_read = 0;
  1317 			src->unique_setup = FALSE;
  1318 			if ( !gst_mythtv_src_next_program_chain( src ) ) {
  1319         goto pause;
  1320       }
  1321       src->state = GST_MYTHTV_SRC_FILE_TRANSFER;
  1322       break;
  1323     case GST_MYTHTV_SRC_INVALID_DATA:
  1324       goto pause;
  1325       break;
  1326     default:
  1327       g_assert_not_reached ();
  1328   }
  1329 
  1330   GST_DEBUG_OBJECT (src, "state: %d res:%s", src->state,
  1331       gst_flow_get_name (res));
  1332 
  1333   return GST_FLOW_OK;
  1334 
  1335 pause:
  1336   GST_LOG_OBJECT (src, "pausing task, reason %s", gst_flow_get_name (res));
  1337   gst_pad_pause_task (src->srcpad);
  1338   if (GST_FLOW_IS_FATAL (res)) {
  1339     GST_ELEMENT_ERROR (src, STREAM, FAILED,
  1340         ("Internal data stream error."),
  1341         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1342     gst_pad_send_event( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), gst_event_new_eos() );
  1343   }
  1344   return res;
  1345 }
  1346 #endif
  1347 
  1348 static GstStateChangeReturn
  1349 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1350 {
  1351   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;//GST_STATE_CHANGE_NO_PREROLL;
  1352   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1353 
  1354   switch (transition) {
  1355     case GST_STATE_CHANGE_NULL_TO_READY:
  1356       src->do_start = TRUE;
  1357       src->unique_setup = FALSE;
  1358       break;
  1359     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1360     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1361       src->eos = FALSE;
  1362       break;
  1363     default:
  1364       break;
  1365   }
  1366 
  1367   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1368   if (ret == GST_STATE_CHANGE_FAILURE)
  1369     return ret;
  1370 
  1371   switch (transition) {
  1372     case GST_STATE_CHANGE_READY_TO_NULL:
  1373       g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
  1374       break;
  1375     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1376       g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1377     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1378       g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1379       if ( src->live_tv && src->update_prog_chain ) {
  1380       	
  1381   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1382       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1383 
  1384 				src->read_offset = 0;
  1385 				src->bytes_read = 0;
  1386 				src->unique_setup = FALSE;				
  1387 				gst_mythtv_src_next_program_chain( src );
  1388       }
  1389       break;
  1390     default:
  1391       break;
  1392   }
  1393 
  1394   return ret;
  1395 }
  1396 
  1397 static void
  1398 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1399     const GValue * value, GParamSpec * pspec)
  1400 {
  1401   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1402 
  1403   GST_OBJECT_LOCK (mythtvsrc);
  1404   switch (prop_id) {
  1405     case PROP_URI:
  1406     case PROP_LOCATION:
  1407       {
  1408 	if (!g_value_get_string (value)) {
  1409 	  GST_WARNING ("location property cannot be NULL");
  1410 	  goto done;
  1411 	}
  1412 
  1413 	if (mythtvsrc->uri_name != NULL) {
  1414 	  g_free (mythtvsrc->uri_name);
  1415 	  mythtvsrc->uri_name = NULL;
  1416 	}
  1417 	mythtvsrc->uri_name = g_value_dup_string (value);
  1418 
  1419 	break;
  1420       }
  1421 #ifndef GST_DISABLE_GST_DEBUG
  1422     case PROP_GMYTHTV_DBG:
  1423       {
  1424 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1425 	break;
  1426       }
  1427 #endif
  1428     case PROP_GMYTHTV_VERSION:
  1429       {
  1430 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1431 	break;
  1432       }
  1433     case PROP_GMYTHTV_LIVEID:
  1434       {
  1435 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1436 	break;
  1437       }
  1438     case PROP_GMYTHTV_LIVE:
  1439       {
  1440 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1441 	break;
  1442       }
  1443     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1444       {
  1445 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1446 	break;
  1447       }      
  1448     case PROP_GMYTHTV_LIVE_CHAINID:
  1449       {
  1450 	if (!g_value_get_string (value)) {
  1451 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1452 	  goto done;
  1453 	}
  1454 
  1455 	if (mythtvsrc->live_chain_id != NULL) {
  1456 	  g_free (mythtvsrc->live_chain_id);
  1457 	  mythtvsrc->live_chain_id = NULL;
  1458 	}
  1459 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1460 	break;
  1461       }
  1462     case PROP_GMYTHTV_CHANNEL_NUM:
  1463       {
  1464 	mythtvsrc->channel_num = g_value_get_int (value);
  1465 	break;
  1466       }
  1467     default:
  1468       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1469       break;
  1470   }
  1471   GST_OBJECT_UNLOCK (mythtvsrc);
  1472 done:
  1473   return;
  1474 }
  1475 
  1476   static void
  1477 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1478     GValue * value, GParamSpec * pspec)
  1479 {
  1480   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1481 
  1482   GST_OBJECT_LOCK (mythtvsrc);
  1483   switch (prop_id) {
  1484     case PROP_URI:
  1485     case PROP_LOCATION:
  1486       {
  1487 	gchar *str = g_strdup( "" );
  1488 
  1489 	if ( mythtvsrc->uri_name == NULL ) {
  1490 	  g_free (mythtvsrc->uri_name);
  1491 	  mythtvsrc->uri_name = NULL;
  1492 	} else {
  1493 	  str = g_strdup( mythtvsrc->uri_name );
  1494 	}
  1495 	g_value_set_string ( value, str );
  1496 	break;
  1497       }
  1498 #ifndef GST_DISABLE_GST_DEBUG
  1499     case PROP_GMYTHTV_DBG:
  1500       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1501       break;
  1502 #endif
  1503     case PROP_GMYTHTV_VERSION:
  1504       {
  1505 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1506 	break;
  1507       }
  1508     case PROP_GMYTHTV_LIVEID:
  1509       {
  1510 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1511 	break;
  1512       }
  1513     case PROP_GMYTHTV_LIVE:
  1514       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1515       break;
  1516     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1517       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1518       break;
  1519     case PROP_GMYTHTV_LIVE_CHAINID:
  1520       {
  1521 	gchar *str = g_strdup( "" );
  1522 
  1523 	if ( mythtvsrc->live_chain_id == NULL ) {
  1524 	  g_free (mythtvsrc->live_chain_id);
  1525 	  mythtvsrc->live_chain_id = NULL;
  1526 	} else {
  1527 	  str = g_strdup( mythtvsrc->live_chain_id );
  1528 	}
  1529 	g_value_set_string ( value, str );
  1530 	break;
  1531       }
  1532     case PROP_GMYTHTV_CHANNEL_NUM:
  1533       {
  1534 	g_value_set_int ( value, mythtvsrc->channel_num );
  1535 	break;
  1536       }
  1537     default:
  1538       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1539       break;
  1540   }
  1541   GST_OBJECT_UNLOCK (mythtvsrc);
  1542 }
  1543 
  1544 /* entry point to initialize the plug-in
  1545  * initialize the plug-in itself
  1546  * register the element factories and pad templates
  1547  * register the features
  1548  */
  1549 static gboolean
  1550 plugin_init (GstPlugin * plugin)
  1551 {
  1552   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1553       GST_TYPE_MYTHTV_SRC);
  1554 }
  1555 
  1556 /* this is the structure that gst-register looks for
  1557  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1558 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1559     GST_VERSION_MINOR,
  1560     "mythtv",
  1561     "lib MythTV src",
  1562     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1563 
  1564 
  1565 /*** GSTURIHANDLER INTERFACE *************************************************/
  1566   static guint 
  1567 gst_mythtv_src_uri_get_type (void)
  1568 {
  1569   return GST_URI_SRC;
  1570 }
  1571 
  1572   static gchar **
  1573 gst_mythtv_src_uri_get_protocols (void)
  1574 {
  1575   static gchar *protocols[] = { "myth", "myths", NULL };
  1576 
  1577   return protocols;
  1578 }
  1579 
  1580   static const gchar *
  1581 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1582 {
  1583   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1584 
  1585   return src->uri_name;
  1586 }
  1587 
  1588   static gboolean
  1589 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1590 {
  1591   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1592 
  1593   gchar *protocol;
  1594 
  1595   protocol = gst_uri_get_protocol (uri);
  1596   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1597     g_free (protocol);
  1598     return FALSE;
  1599   }
  1600   g_free (protocol);
  1601   g_object_set (src, "location", uri, NULL);
  1602 
  1603   return TRUE;
  1604 }
  1605 
  1606   static void
  1607 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1608 {
  1609   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1610 
  1611   iface->get_type = gst_mythtv_src_uri_get_type;
  1612   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1613   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1614   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1615 }
  1616 
  1617   void
  1618 size_header_handler (void *userdata, const char *value)
  1619 {
  1620   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1621 
  1622   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1623 
  1624   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1625 }