gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Tue Dec 12 18:19:35 2006 +0000 (2006-12-12)
branchtrunk
changeset 216 001205bb0f40
parent 204 5b07de56c816
child 217 aba1e4be0053
permissions -rwxr-xr-x
[svn r217] Added signals to program changed messages coming from backend (MythTV).
     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   * When using the LiveTV content, put the location URI in the following
    17   * format:
    18   * 
    19   * 	myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/#mythconverg
    20   * 
    21   * Where the first field is the protocol (myth), the second and third are user 
    22   * name (mythtv) and password (mythtv), then backend host name and port number, 
    23   * and the last field is the database name (mythconverg).
    24   */
    25 
    26 #ifdef HAVE_CONFIG_H
    27 #include "config.h"
    28 #endif
    29 
    30 #include "gstmythtvsrc.h"
    31 #include <gmyth/gmyth_file_transfer.h>
    32 #include <gmyth/gmyth_livetv.h>
    33 
    34 #include <gmyth/gmyth_socket.h>
    35 #include <gmyth/gmyth_tvchain.h>
    36 
    37 #include <string.h>
    38 #include <unistd.h>
    39 
    40 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
    41 #define GST_CAT_DEFAULT mythtvsrc_debug
    42 
    43 #define GST_GMYTHTV_ID_NUM			1
    44 
    45 #define GST_GMYTHTV_CHANNEL_NUM			1000
    46 
    47 #define GMYTHTV_VERSION_DEFAULT			30
    48  
    49 #define GMYTHTV_TRANSFER_MAX_WAITS		100
    50 
    51 #define GMYTHTV_TRANSFER_MAX_RESENDS	5
    52 
    53 #define GMYTHTV_TRANSFER_MAX_BUFFER		128*1024
    54 //( 32*1024  )
    55 
    56 /* 4*1024 ??? */
    57 #define MAX_READ_SIZE              		4*1024
    58 //( 32*1024 )
    59 
    60 #define GST_FLOW_ERROR_NO_DATA  			-101
    61 
    62 #define REQUEST_MAX_SIZE							64*1024
    63 
    64 #define INTERNAL_BUFFER_SIZE					90*1024
    65 
    66 /* stablish a maximum iteration value to the IS_RECORDING message */
    67 static guint wait_to_transfer = 0;
    68 
    69 static const GstElementDetails gst_mythtv_src_details =
    70 GST_ELEMENT_DETAILS ( "MythTV client source",
    71     "Source/Network",
    72     "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
    73     "Rosfran Borges <rosfran.borges@indt.org.br>" );
    74 
    75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
    76     GST_PAD_SRC,
    77     GST_PAD_ALWAYS,
    78     GST_STATIC_CAPS ("video/x-nuv") );
    79     
    80 enum
    81 {
    82   PROP_0,
    83   PROP_LOCATION,
    84   PROP_URI,
    85 #ifndef GST_DISABLE_GST_DEBUG
    86   PROP_GMYTHTV_DBG,
    87 #endif
    88   PROP_GMYTHTV_VERSION,
    89   PROP_GMYTHTV_LIVE,
    90   PROP_GMYTHTV_LIVEID,
    91   PROP_GMYTHTV_LIVE_CHAINID,
    92   PROP_GMYTHTV_ENABLE_TIMING_POSITION,
    93   PROP_GMYTHTV_CHANNEL_NUM
    94 };
    95 
    96 static void gst_mythtv_src_finalize (GObject * gobject);
    97 
    98 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
    99 
   100 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
   101 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
   102 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
   103 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
   104 
   105 /*
   106 static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
   107    GstClockTime * start, GstClockTime * end);
   108 */
   109 
   110 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
   111 
   112 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
   113 
   114 static GstStateChangeReturn
   115 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
   116 
   117 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
   118     const GValue * value, GParamSpec * pspec);
   119 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
   120     GValue * value, GParamSpec * pspec);
   121 
   122 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
   123 
   124 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
   125 
   126 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
   127 
   128 static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr);
   129 
   130 GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
   131 
   132 static void
   133 _urihandler_init (GType type)
   134 {
   135   static const GInterfaceInfo urihandler_info = {
   136     gst_mythtv_src_uri_handler_init,
   137     NULL,
   138     NULL
   139   };
   140 
   141   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
   142 
   143   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   144       "MythTV src");
   145 }
   146 
   147 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
   148     GST_TYPE_PUSH_SRC, _urihandler_init)
   149     
   150 static void
   151 gst_mythtv_src_base_init (gpointer g_class)
   152 {
   153   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   154 
   155   gst_element_class_add_pad_template (element_class,
   156       gst_static_pad_template_get (&srctemplate));
   157       
   158   gst_element_class_set_details (element_class, &gst_mythtv_src_details);
   159   
   160   element_class->change_state = gst_mythtv_src_change_state;
   161   
   162 }
   163 
   164 static void
   165 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
   166 {
   167   GObjectClass *gobject_class; 
   168   GstPushSrcClass *gstpushsrc_class;
   169   GstBaseSrcClass *gstbasesrc_class;
   170 
   171   gobject_class = (GObjectClass *) klass;
   172   gstbasesrc_class = (GstBaseSrcClass *) klass;
   173   gstpushsrc_class = (GstPushSrcClass *) klass;
   174 
   175   gobject_class->set_property = gst_mythtv_src_set_property;
   176   gobject_class->get_property = gst_mythtv_src_get_property;
   177   gobject_class->finalize = gst_mythtv_src_finalize;
   178 
   179   g_object_class_install_property
   180     (gobject_class, PROP_LOCATION,
   181      g_param_spec_string ("location", "Location",
   182        "The location. In the form:"
   183        "\n\t\t\tmyth://a.com/file.nuv"
   184        "\n\t\t\tmyth://a.com:23223/file.nuv "
   185        "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
   186        "", G_PARAM_READWRITE));
   187 
   188   g_object_class_install_property
   189     (gobject_class, PROP_URI,
   190      g_param_spec_string ("uri", "Uri",
   191        "The location in form of a URI (deprecated; use location)",
   192        "", G_PARAM_READWRITE));
   193 
   194   g_object_class_install_property
   195     (gobject_class, PROP_GMYTHTV_VERSION,
   196      g_param_spec_int ("mythtv-version", "mythtv-version",
   197        "Change MythTV version",
   198        26, 30, 26, G_PARAM_READWRITE));
   199 
   200   g_object_class_install_property
   201     (gobject_class, PROP_GMYTHTV_LIVEID,
   202      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   203        "Change MythTV version",
   204        0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
   205 
   206   g_object_class_install_property
   207     (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
   208      g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
   209        "Sets the MythTV chain ID (from TV Chain)",
   210        "", G_PARAM_READWRITE));
   211 
   212   g_object_class_install_property
   213     (gobject_class, PROP_GMYTHTV_LIVE,
   214      g_param_spec_boolean ("mythtv-live", "mythtv-live",
   215        "Enable MythTV Live TV content streaming",
   216        FALSE, G_PARAM_READWRITE));
   217 
   218   g_object_class_install_property
   219     (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
   220      g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
   221        "Enable MythTV Live TV content size continuous updating",
   222        FALSE, G_PARAM_READWRITE));
   223        
   224   g_object_class_install_property
   225     (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
   226      g_param_spec_int ("mythtv-channel", "mythtv-channel",
   227        "Change MythTV channel number",
   228        0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
   229 
   230 #ifndef GST_DISABLE_GST_DEBUG
   231   g_object_class_install_property
   232     (gobject_class, PROP_GMYTHTV_DBG,
   233      g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
   234        "Enable MythTV debug messages",
   235        FALSE, G_PARAM_READWRITE));
   236 #endif
   237 
   238   gstbasesrc_class->start = gst_mythtv_src_start;
   239   gstbasesrc_class->stop = gst_mythtv_src_stop;
   240   gstbasesrc_class->get_size = gst_mythtv_src_get_size;
   241   gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
   242   
   243   //gstbasesrc_class->get_times = gst_mythtv_src_get_times;
   244   
   245   gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
   246   gstpushsrc_class->create = gst_mythtv_src_create;
   247     
   248   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   249       "MythTV Client Source");
   250 }
   251 
   252 static void
   253 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
   254 {
   255   this->file_transfer = NULL;
   256 
   257   this->unique_setup = FALSE;
   258 
   259   this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
   260   
   261   this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
   262 
   263   this->bytes_read = 0;
   264   
   265   this->prev_content_size = 0;
   266 
   267   this->content_size = 0;
   268   this->read_offset = 0;
   269 
   270   this->content_size_last = 0;
   271 
   272   this->live_tv = FALSE;
   273   
   274   this->enable_timing_position = FALSE;
   275   this->update_prog_chain = FALSE;    
   276 
   277   this->user_agent = g_strdup ("mythtvsrc");
   278   this->mythtv_caps = NULL;
   279   this->update_prog_chain = FALSE;
   280   
   281   this->channel_num = 0;
   282   
   283   this->eos = FALSE;
   284   
   285   this->bytes_queue = NULL;
   286   
   287   this->th_mutex = NULL;
   288   
   289   gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
   290 
   291   /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */
   292   
   293   gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   294       gst_mythtv_src_handle_event );
   295   gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   296      gst_mythtv_src_handle_query );
   297 
   298 }
   299 
   300 static void
   301 gst_mythtv_src_finalize (GObject * gobject)
   302 {
   303   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   304 
   305   if ( this->th_read_ahead != NULL ) {
   306     gst_task_stop( this->th_read_ahead );
   307     this->th_read_ahead = NULL;
   308   }
   309 
   310   if (this->mythtv_caps) {
   311     gst_caps_unref (this->mythtv_caps);
   312     this->mythtv_caps = NULL;
   313   }
   314 
   315   if (this->file_transfer) {
   316     g_object_unref (this->file_transfer);
   317     this->file_transfer = NULL;
   318   }
   319 
   320   if (this->spawn_livetv) {
   321     g_object_unref (this->spawn_livetv);
   322     this->spawn_livetv = NULL;
   323   }
   324   
   325   if (this->backend_info) {
   326     g_object_unref (this->backend_info);
   327     this->backend_info = NULL;
   328   }
   329 
   330   if (this->uri_name) {
   331     g_free (this->uri_name);
   332   }
   333 
   334   if (this->user_agent) {
   335     g_free (this->user_agent);
   336   }
   337 
   338   if ( this->bytes_queue ) {
   339     g_byte_array_free( this->bytes_queue, TRUE );
   340     this->bytes_queue = NULL;
   341   }
   342 
   343   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   344 }
   345 
   346 static gint
   347 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
   348 {
   349   gint read = 0;
   350   guint sizetoread = size;
   351   gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
   352   
   353   GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   354 
   355   /* Loop sending the Myth File Transfer request:
   356    * Retry whilst authentication fails and we supply it. */
   357   gint len = 0;
   358   
   359   while ( sizetoread == size && --max_iters > 0) {
   360 
   361     len = gmyth_file_transfer_read( src->file_transfer,
   362 				data_ptr, sizetoread, TRUE );
   363 
   364     if ( len > 0 ) {
   365       read += len;
   366       sizetoread -= len;
   367     } 
   368     else if ( len < 0 )
   369     {
   370       read = -1;
   371 
   372       if ( src->live_tv == FALSE ) 
   373       {
   374 	goto eos;
   375       } 
   376       else  
   377       {
   378 	if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
   379 	  src->update_prog_chain = TRUE;
   380 	  goto done;	  	
   381 	} 
   382 	goto done;
   383       }
   384 
   385     } else if ( len == 0 )
   386     	goto done;
   387 
   388     if ( read == sizetoread )
   389       goto done;
   390   }
   391 
   392   if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
   393     goto eos;
   394 
   395   goto done;
   396 
   397 eos:
   398   src->eos = TRUE;
   399 
   400 done:
   401   return read;
   402 }
   403 
   404 static GstFlowReturn
   405 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
   406 {
   407   GstMythtvSrc *src;
   408   GstFlowReturn ret = GST_FLOW_OK;
   409   gint read = -1;
   410 
   411   src = GST_MYTHTV_SRC ( psrc );
   412 
   413   /* The caller should know the number of bytes and not read beyond EOS. */
   414   if (G_UNLIKELY (src->eos))
   415     goto eos;
   416   if ( G_UNLIKELY (src->update_prog_chain) )
   417     goto change_progchain;
   418 
   419   //g_static_rec_mutex_lock( &th_mutex );
   420   GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__, 
   421   		src->read_offset, MAX_READ_SIZE );
   422 
   423   GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__, 
   424       (gint) src->buffer_remain, src->bytes_queue->len );
   425 
   426   /* just get from the byte array, no network effort... */
   427   if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) { 
   428   			//( ( INTERNAL_BUFFER_SIZE - src->buffer_remain )  >=  REQUEST_MAX_SIZE ) ) {
   429   	GByteArray *buffer = NULL;
   430   	guint buffer_size_inter = ( INTERNAL_BUFFER_SIZE - src->buffer_remain );
   431   	if ( buffer_size_inter > REQUEST_MAX_SIZE )
   432   		buffer_size_inter = REQUEST_MAX_SIZE;
   433   		
   434   	buffer = g_byte_array_new();
   435 
   436     read = do_read_request_response( src, buffer_size_inter, buffer );
   437 
   438     if (G_UNLIKELY (read < 0)) {
   439       if ( src->live_tv )
   440 	goto change_progchain;
   441       else
   442 	goto read_error;
   443     } else if (G_UNLIKELY (read == 0))
   444     {
   445 	    if (!src->live_tv )
   446 	    	goto eos;
   447 	    else
   448 	    	goto done;
   449     }
   450 
   451     if ( G_UNLIKELY (src->update_prog_chain) )
   452       goto change_progchain;
   453     
   454     src->bytes_queue = g_byte_array_append( src->bytes_queue, buffer->data, read );
   455     if ( read > buffer_size_inter )
   456     	GST_WARNING_OBJECT( src, "\n\n\n[%s]\t\tINCREASED buffer size! Backend sent more than we ask him... (%d)\n\n\n", 
   457     			__FUNCTION__, abs( read - buffer_size_inter ) );
   458 
   459     src->buffer_remain += read;
   460 
   461     if ( buffer != NULL ) {
   462     	g_byte_array_free( buffer, TRUE );
   463     	buffer = NULL;    	
   464     }
   465 	
   466 	    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   467 		"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   468 		src->read_offset, src->content_size );
   469 	
   470   }
   471   
   472   guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   473   
   474   /* Create the buffer. */
   475   ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   476       GST_BUFFER_OFFSET_NONE, buffer_size,
   477       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
   478 
   479   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   480     if ( src->live_tv )
   481       goto change_progchain;
   482     else
   483       goto done;
   484   }
   485   //*outbuf = gst_buffer_new_and_alloc( buffer_size );
   486   
   487   /* gets the first buffer_size bytes from the byte array buffer variable */ 
   488   //guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
   489 
   490   GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__, 
   491   	read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
   492 
   493   GST_BUFFER_SIZE (*outbuf) = buffer_size;
   494   GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   495   GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   496   g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->bytes_queue->data, GST_BUFFER_SIZE(*outbuf) );
   497   GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
   498   GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
   499 
   500   src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
   501 
   502   src->read_offset += GST_BUFFER_SIZE (*outbuf);
   503   src->bytes_read += GST_BUFFER_SIZE (*outbuf);
   504   GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
   505   
   506   /* flushs the newly buffer got from byte array */
   507   src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
   508 
   509   GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   510       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   511       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   512 
   513   GST_DEBUG_OBJECT( src, "[%s]\tCONTENT_SIZE = %llu, BYTES_READ = %llu.\n", __FUNCTION__, 
   514   		src->content_size, src->bytes_read );
   515       
   516   if ( G_UNLIKELY (src->eos) || ( !src->live_tv && ( src->bytes_read >= src->content_size ) ) )
   517    goto eos;
   518 
   519 done:
   520   {
   521     const gchar *reason = gst_flow_get_name (ret);
   522 
   523     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   524     return ret;
   525   }
   526 eos:
   527   {
   528     const gchar *reason = gst_flow_get_name (ret);
   529 
   530     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   531     return GST_FLOW_UNEXPECTED;
   532   }
   533   /* ERRORS */
   534 read_error:
   535   {
   536     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   537 	(NULL), ("Could not read any bytes (%i, %s)", read,
   538 		 src->uri_name));
   539     return GST_FLOW_ERROR;
   540   }
   541 change_progchain:
   542   {
   543     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   544 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   545 		 src->uri_name));
   546 
   547     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   548 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   549     // go to the next program chain
   550     src->unique_setup = FALSE;
   551     src->update_prog_chain = TRUE;
   552 
   553     gst_mythtv_src_next_program_chain( src );
   554 
   555     return GST_FLOW_ERROR_NO_DATA;
   556   }
   557 
   558 }
   559 
   560 gint64
   561 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   562 {
   563 
   564   gint64 size_tmp = 0;
   565   guint max_tries = 2;
   566   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   567 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   568 
   569 get_file_pos:
   570     g_usleep( 10 );
   571     size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   572     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   573       src->content_size = size_tmp;
   574     else if ( size_tmp > 0 && --max_tries > 0 )
   575       goto get_file_pos;
   576     GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
   577 	__FUNCTION__, size_tmp );
   578     /* sets the last content size amount before it can be updated */
   579     src->prev_content_size = src->content_size;
   580   }
   581 
   582   return src->content_size;	
   583 
   584 }
   585 
   586 static gboolean
   587 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
   588 {
   589   GstMythtvSrc *src = GST_MYTHTV_SRC( base );
   590   gint64 new_offset = -1;
   591   gint64 actual_seek = segment->start;
   592   gboolean ret = TRUE;
   593 
   594   GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, 
   595   		segment->start, segment->stop );
   596 
   597   if ( segment->format == GST_FORMAT_TIME ) 
   598   {
   599     goto done; 
   600     //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
   601   }
   602   GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
   603   /* verify if it needs to seek */
   604   if ( src->read_offset != actual_seek )
   605   {
   606 
   607     new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
   608 
   609     GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   610 			__FUNCTION__, segment->start, src->read_offset, new_offset );
   611     if ( G_UNLIKELY (new_offset < 0 ) )
   612     {
   613       ret = FALSE;
   614       if ( src->live_tv )
   615 	goto change_progchain;
   616       else
   617 	goto eos;
   618     }
   619 
   620     src->read_offset = new_offset;
   621 
   622     if ( ret == FALSE ) {
   623       GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
   624     }
   625 
   626   }
   627 
   628 done:
   629    return ret;
   630 
   631 eos:
   632   {
   633 
   634     GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
   635     return FALSE;
   636   }
   637 change_progchain:
   638   {
   639     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   640 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   641 		 src->uri_name));
   642 
   643     gst_pad_push_event ( GST_BASE_SRC_PAD (base),
   644 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   645     /* go to the next program chain */
   646     src->unique_setup = FALSE;
   647     src->update_prog_chain = TRUE;
   648 
   649     gst_mythtv_src_next_program_chain( src );
   650 
   651     return TRUE;
   652   }
   653 
   654 }
   655 
   656 /* create a socket for connecting to remote server */
   657 static gboolean
   658 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   659 {
   660   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   661 
   662   GString *chain_id_local = NULL;
   663 
   664   gboolean ret = TRUE;
   665 
   666   if ( G_UNLIKELY (src->update_prog_chain) )
   667     goto change_progchain;
   668 
   669   if (src->unique_setup == FALSE) {
   670     src->unique_setup = TRUE;
   671   } else {
   672     goto done;
   673   }
   674   
   675   src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
   676   if ( src->live_tv ) {  	
   677     src->spawn_livetv = gmyth_livetv_new( );
   678     if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
   679       ret = FALSE;
   680       goto init_failed;
   681     }    
   682 
   683     /* set up the uri variable */
   684     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   685     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   686     if ( chain_id_local != NULL ) {
   687       src->live_chain_id = g_strdup( chain_id_local->str );
   688       GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );      
   689     }
   690     	
   691     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   692     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
   693   }
   694 
   695   //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
   696   //    g_string_new( src->uri_name ), -1, src->mythtv_version );
   697 	src->file_transfer = gmyth_file_transfer_new(src->backend_info);
   698 
   699   if ( NULL == src->file_transfer ) 
   700   {
   701     goto init_failed;
   702   }
   703   GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->uri_name );
   704 
   705 	ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name );
   706 
   707   if ( src->live_tv == TRUE && ret == TRUE ) {
   708     /* loop finished, set the max tries variable to zero again... */
   709     wait_to_transfer = 0;
   710 
   711     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   712 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   713       g_usleep( 500 );
   714       
   715     /* IS_RECORDING again, just like the MythTV backend does... */
   716 	  gmyth_livetv_is_recording( src->spawn_livetv );
   717 	  
   718 	  sleep( 3 );  
   719 
   720   }
   721   /* sets the FileTransfer instance connection (video/audio download) */
   722   //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   723 
   724   if ( ret == FALSE ) {
   725 #ifndef GST_DISABLE_GST_DEBUG  
   726     if ( src->mythtv_msgs_dbg )
   727       GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   728 #endif
   729     goto begin_req_failed;
   730   }
   731   
   732   GST_INFO_OBJECT( src, "MythTV FileTransfer filesize = %lld, content_size = %lld!\n", 
   733   			src->file_transfer->filesize, src->content_size );
   734 
   735   src->content_size = src->file_transfer->filesize;
   736 
   737   src->do_start = FALSE;
   738   
   739   /* this is used for the buffer cache */
   740   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   741   src->buffer_remain = 0;
   742   
   743   gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   744 		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
   745 
   746 done:
   747 
   748 	if ( chain_id_local != NULL )
   749 	{
   750 		g_string_free( chain_id_local, TRUE );
   751 		chain_id_local = NULL;
   752 	}
   753 		
   754   return TRUE;
   755 
   756   /* ERRORS */
   757 init_failed:
   758   {
   759     if (src->spawn_livetv != NULL )
   760       g_object_unref( src->spawn_livetv );
   761 
   762     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   763 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   764     return FALSE;
   765   }
   766 begin_req_failed:
   767   {
   768     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   769 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   770     return FALSE;
   771   }
   772 change_progchain:
   773   {
   774     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   775 	(NULL), ("Seek failed, go to the next program info... (%s)",
   776 		 src->uri_name));
   777 
   778     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   779 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   780 
   781     // go to the next program chain
   782     src->unique_setup = FALSE;
   783     src->update_prog_chain = TRUE;
   784 
   785     gst_mythtv_src_next_program_chain( src );
   786 
   787     return TRUE;
   788   }
   789 }
   790 
   791 /* create a new socket for connecting to the next program chain */
   792 static gboolean
   793 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
   794 {
   795   GString *chain_id_local = NULL;
   796 
   797   gboolean ret = TRUE;
   798 
   799   if ( !src->live_tv )
   800     goto init_failed;
   801     
   802   if (src->unique_setup == FALSE) {
   803     src->unique_setup = TRUE;
   804   } else {
   805     goto done;
   806   }
   807   
   808 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   809 
   810   if (src->file_transfer) {
   811     g_object_unref (src->file_transfer);
   812     src->file_transfer = NULL;
   813   }
   814 
   815   if (src->uri_name) {
   816     g_free (src->uri_name);
   817   }
   818   
   819   if ( src->backend_info == NULL )  
   820   	src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
   821 
   822   if ( src->live_tv ) {
   823     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
   824     	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
   825       ret = FALSE;
   826       goto init_failed;
   827     }
   828     /* set up the uri variable */
   829     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   830     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   831     if ( chain_id_local != NULL ) {
   832       src->live_chain_id = g_strdup( chain_id_local->str );
   833       GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   834     }
   835     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   836     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   837   }
   838 
   839   src->file_transfer = gmyth_file_transfer_new(src->backend_info);
   840 
   841   if ( src->file_transfer == NULL ) {
   842     goto init_failed;
   843   }
   844   
   845 	ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name );
   846 
   847   /* sets the Playback monitor connection */
   848   //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   849 
   850   if ( src->live_tv == TRUE && ret == TRUE ) {
   851     /* loop finished, set the max tries variable to zero again... */
   852     wait_to_transfer = 0;
   853 
   854     g_usleep( 200 );
   855 
   856     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
   857 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   858       g_usleep( 1000 );
   859   }
   860 
   861   /* sets the FileTransfer instance connection (video/audio download) */
   862   //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   863 
   864   if ( ret == FALSE ) {
   865 #ifndef GST_DISABLE_GST_DEBUG  
   866     if ( src->mythtv_msgs_dbg )
   867       GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   868 #endif
   869     goto begin_req_failed;
   870   }
   871   src->content_size_last = src->content_size;
   872 
   873   src->content_size = src->file_transfer->filesize;
   874   if ( src->live_tv ) {
   875   	wait_to_transfer = 0;
   876 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
   877 	    src->content_size = gst_mythtv_src_get_position( src );
   878   }
   879 
   880   src->read_offset = 0;  
   881   
   882   if ( src->bytes_queue != NULL ) {
   883   	g_byte_array_free( src->bytes_queue, TRUE );
   884   }
   885   
   886   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   887   
   888   //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   889 	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
   890   
   891 done:
   892 	src->update_prog_chain = FALSE;
   893 	
   894 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   895 	
   896   return TRUE;
   897 
   898   /* ERRORS */
   899 init_failed:
   900   {
   901     if (src->spawn_livetv != NULL )
   902       g_object_unref( src->spawn_livetv );
   903 
   904     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   905 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   906     return FALSE;
   907   }
   908 begin_req_failed:
   909   {
   910     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   911 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   912     return FALSE;
   913   }
   914 
   915 }
   916 
   917 static gboolean
   918 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   919 {
   920   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   921   gboolean ret = TRUE;
   922   GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
   923   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
   924   			
   925   if ( src->live_tv ) {
   926   	ret = FALSE;
   927 	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
   928 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   929     
   930     gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   931     if ( new_offset > 0 && new_offset > src->content_size ) {
   932 			src->content_size = new_offset;
   933     } else if ( new_offset < src->content_size ) {
   934 			src->update_prog_chain = TRUE;
   935 	  }
   936 
   937   }
   938 
   939   *size = src->content_size;
   940   GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
   941   
   942   return ret;
   943 
   944 }
   945 
   946 /* close the socket and associated resources
   947  * used both to recover from errors and go to NULL state */
   948 static gboolean
   949 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   950 {
   951   GstMythtvSrc *src;
   952 
   953   src = GST_MYTHTV_SRC (bsrc);
   954 
   955   if (src->uri_name) {
   956     g_free (src->uri_name);
   957     src->uri_name = NULL;
   958   }
   959 
   960   if (src->mythtv_caps) {
   961     gst_caps_unref (src->mythtv_caps);
   962     src->mythtv_caps = NULL;
   963   }
   964 
   965   src->eos = FALSE;
   966 
   967   return TRUE;
   968 }
   969 
   970 static gboolean
   971 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   972 {
   973   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   974   gint64 cont_size = 0;
   975   gboolean ret = FALSE;
   976 
   977   switch (GST_EVENT_TYPE (event)) {
   978     case GST_EVENT_EOS:
   979       GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
   980 
   981       if ( src->live_tv ) {
   982 	cont_size = gst_mythtv_src_get_position (src);
   983 	if ( cont_size > src->content_size ) {
   984 	  src->content_size = cont_size;
   985 	  src->eos = FALSE;
   986 	} else {
   987 	  src->eos = TRUE;
   988 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   989 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   990 	}
   991       }
   992       break;
   993     default:
   994       ret = gst_pad_event_default (pad, event);
   995   }
   996 
   997   return ret;
   998 }
   999 
  1000 static gboolean
  1001 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
  1002 {
  1003   return TRUE;
  1004 }
  1005 
  1006 static gboolean
  1007 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
  1008 {
  1009   gboolean res = FALSE;
  1010   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
  1011   GstFormat formt;
  1012 
  1013   switch (GST_QUERY_TYPE (query)) {
  1014     case GST_QUERY_POSITION:
  1015     {
  1016     	gint64 pos = -1;
  1017       gst_query_parse_position (query, &formt, &pos );
  1018       res = TRUE;
  1019       if ( formt == GST_FORMAT_BYTES ) {
  1020       	gst_query_set_position (query, formt, pos = myth->read_offset );
  1021       	GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
  1022       } else if ( formt == GST_FORMAT_TIME ) {
  1023       	GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
  1024       	res = gst_pad_query_default(pad, query);
  1025       }
  1026       break;
  1027     }
  1028     case GST_QUERY_DURATION:
  1029     {
  1030 #if 0
  1031       if (myth->duration != 0) {
  1032 	gint64 total;
  1033 	gint64 fps;
  1034 
  1035 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1036 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1037       }
  1038 #endif
  1039 	gint64 dur = -1;
  1040 	gst_query_parse_duration ( query, &formt, &dur );	
  1041   if ( formt == GST_FORMAT_BYTES ) {
  1042   	gst_query_set_duration (query, formt, dur = myth->content_size);
  1043   	GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
  1044   } else if ( formt == GST_FORMAT_TIME ) {
  1045   	GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
  1046   	gst_query_ref(query);
  1047   	res = gst_pad_query_default(pad, query);  	
  1048   }
  1049 	res = TRUE;
  1050       break;
  1051     }
  1052     default:
  1053     {
  1054       res = gst_pad_query_default(pad, query);
  1055       break;
  1056     }
  1057   }
  1058 
  1059   gst_object_unref (myth);
  1060 
  1061   return res;
  1062 }
  1063 
  1064 static GstStateChangeReturn
  1065 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1066 {
  1067   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1068   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1069 
  1070   switch (transition) {
  1071     case GST_STATE_CHANGE_NULL_TO_READY:
  1072       //src->do_start = TRUE;
  1073       //src->unique_setup = FALSE;
  1074       break;
  1075     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1076     	GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ );
  1077     	break;
  1078     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1079     	GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ );
  1080       if ( src->live_tv ) {
  1081 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1082 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1083 	      else
  1084 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1085       }
  1086 
  1087       break;
  1088     default:
  1089       break;
  1090   }
  1091 
  1092   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1093   if (ret == GST_STATE_CHANGE_FAILURE)
  1094     return ret;
  1095 
  1096   switch (transition) {
  1097     case GST_STATE_CHANGE_READY_TO_NULL:
  1098       GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
  1099       break;
  1100     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1101       GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1102     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1103       GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1104       if ( src->live_tv ) {
  1105 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1106 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1107 	      else
  1108 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1109       	
  1110       /*
  1111       	
  1112   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1113       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1114 
  1115 				src->read_offset = 0;
  1116 				src->bytes_read = 0;
  1117 				src->unique_setup = FALSE;				
  1118 				gst_mythtv_src_next_program_chain( src );
  1119 				*/
  1120       }
  1121       
  1122       break;
  1123     default:
  1124       break;
  1125   }
  1126 
  1127   return ret;
  1128 }
  1129 
  1130 static void
  1131 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1132     const GValue * value, GParamSpec * pspec)
  1133 {
  1134   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1135 
  1136   GST_OBJECT_LOCK (mythtvsrc);
  1137   switch (prop_id) {
  1138     case PROP_URI:
  1139     case PROP_LOCATION:
  1140       {
  1141 	if (!g_value_get_string (value)) {
  1142 	  GST_WARNING ("location property cannot be NULL");
  1143 	  goto done;
  1144 	}
  1145 
  1146 	if (mythtvsrc->uri_name != NULL) {
  1147 	  g_free (mythtvsrc->uri_name);
  1148 	  mythtvsrc->uri_name = NULL;
  1149 	}
  1150 	mythtvsrc->uri_name = g_value_dup_string (value);
  1151 
  1152 	break;
  1153       }
  1154 #ifndef GST_DISABLE_GST_DEBUG
  1155     case PROP_GMYTHTV_DBG:
  1156       {
  1157 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1158 	break;
  1159       }
  1160 #endif
  1161     case PROP_GMYTHTV_VERSION:
  1162       {
  1163 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1164 	break;
  1165       }
  1166     case PROP_GMYTHTV_LIVEID:
  1167       {
  1168 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1169 	break;
  1170       }
  1171     case PROP_GMYTHTV_LIVE:
  1172       {
  1173 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1174 	break;
  1175       }
  1176     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1177       {
  1178 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1179 	break;
  1180       }      
  1181     case PROP_GMYTHTV_LIVE_CHAINID:
  1182       {
  1183 	if (!g_value_get_string (value)) {
  1184 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1185 	  goto done;
  1186 	}
  1187 
  1188 	if (mythtvsrc->live_chain_id != NULL) {
  1189 	  g_free (mythtvsrc->live_chain_id);
  1190 	  mythtvsrc->live_chain_id = NULL;
  1191 	}
  1192 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1193 	break;
  1194       }
  1195     case PROP_GMYTHTV_CHANNEL_NUM:
  1196       {
  1197 	mythtvsrc->channel_num = g_value_get_int (value);
  1198 	break;
  1199       }
  1200     default:
  1201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1202       break;
  1203   }
  1204   GST_OBJECT_UNLOCK (mythtvsrc);
  1205 done:
  1206   return;
  1207 }
  1208 
  1209 static void
  1210 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1211     GValue * value, GParamSpec * pspec)
  1212 {
  1213   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1214 
  1215   GST_OBJECT_LOCK (mythtvsrc);
  1216   switch (prop_id) {
  1217     case PROP_URI:
  1218     case PROP_LOCATION:
  1219       {
  1220 	gchar *str = g_strdup( "" );
  1221 
  1222 	if ( mythtvsrc->uri_name == NULL ) {
  1223 	  g_free (mythtvsrc->uri_name);
  1224 	  mythtvsrc->uri_name = NULL;
  1225 	} else {
  1226 	  str = g_strdup( mythtvsrc->uri_name );
  1227 	}
  1228 	g_value_set_string ( value, str );
  1229 	break;
  1230       }
  1231 #ifndef GST_DISABLE_GST_DEBUG
  1232     case PROP_GMYTHTV_DBG:
  1233       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1234       break;
  1235 #endif
  1236     case PROP_GMYTHTV_VERSION:
  1237       {
  1238 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1239 	break;
  1240       }
  1241     case PROP_GMYTHTV_LIVEID:
  1242       {
  1243 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1244 	break;
  1245       }
  1246     case PROP_GMYTHTV_LIVE:
  1247       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1248       break;
  1249     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1250       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1251       break;
  1252     case PROP_GMYTHTV_LIVE_CHAINID:
  1253       {
  1254 	gchar *str = g_strdup( "" );
  1255 
  1256 	if ( mythtvsrc->live_chain_id == NULL ) {
  1257 	  g_free (mythtvsrc->live_chain_id);
  1258 	  mythtvsrc->live_chain_id = NULL;
  1259 	} else {
  1260 	  str = g_strdup( mythtvsrc->live_chain_id );
  1261 	}
  1262 	g_value_set_string ( value, str );
  1263 	break;
  1264       }
  1265     case PROP_GMYTHTV_CHANNEL_NUM:
  1266       {
  1267 	g_value_set_int ( value, mythtvsrc->channel_num );
  1268 	break;
  1269       }
  1270     default:
  1271       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1272       break;
  1273   }
  1274   GST_OBJECT_UNLOCK (mythtvsrc);
  1275 }
  1276 
  1277 /* entry point to initialize the plug-in
  1278  * initialize the plug-in itself
  1279  * register the element factories and pad templates
  1280  * register the features
  1281  */
  1282 static gboolean
  1283 plugin_init (GstPlugin * plugin)
  1284 {
  1285   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1286       GST_TYPE_MYTHTV_SRC);
  1287 }
  1288 
  1289 /* this is the structure that gst-register looks for
  1290  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1291 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1292     GST_VERSION_MINOR,
  1293     "mythtv",
  1294     "lib MythTV src",
  1295     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1296 
  1297 
  1298 /*** GSTURIHANDLER INTERFACE *************************************************/
  1299   static guint 
  1300 gst_mythtv_src_uri_get_type (void)
  1301 {
  1302   return GST_URI_SRC;
  1303 }
  1304 
  1305   static gchar **
  1306 gst_mythtv_src_uri_get_protocols (void)
  1307 {
  1308   static gchar *protocols[] = { "myth", "myths", NULL };
  1309 
  1310   return protocols;
  1311 }
  1312 
  1313   static const gchar *
  1314 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1315 {
  1316   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1317 
  1318   return src->uri_name;
  1319 }
  1320 
  1321   static gboolean
  1322 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1323 {
  1324   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1325 
  1326   gchar *protocol;
  1327 
  1328   protocol = gst_uri_get_protocol (uri);
  1329   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1330     g_free (protocol);
  1331     return FALSE;
  1332   }
  1333   g_free (protocol);
  1334   g_object_set (src, "location", uri, NULL);
  1335 
  1336   return TRUE;
  1337 }
  1338 
  1339  static void
  1340 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1341 {
  1342   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1343 
  1344   iface->get_type = gst_mythtv_src_uri_get_type;
  1345   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1346   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1347   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1348 }
  1349 
  1350   void
  1351 size_header_handler (void *userdata, const char *value)
  1352 {
  1353   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1354 
  1355   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1356 
  1357   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1358 }