gst-plugins-mythtv/src/gstmythtvsrc.c
author melunko
Wed Dec 13 16:09:47 2006 +0000 (2006-12-13)
branchtrunk
changeset 218 3d84c6170960
parent 210 b3c3e10c3293
child 224 b3cb9c7ec1a9
permissions -rwxr-xr-x
[svn r219] frames with timestamp less than zero are now skiped
     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	2
    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 	} else if ( len == GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN ) { /* -315 */
   382 	  continue;	  	
   383 	}
   384 	goto done;
   385       }
   386 
   387     } else if ( len == 0 )
   388     	goto done;
   389 
   390     if ( read == sizetoread )
   391       goto done;
   392   }
   393 
   394   if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
   395     goto eos;
   396 
   397   goto done;
   398 
   399 eos:
   400   src->eos = TRUE;
   401 
   402 done:
   403   return read;
   404 }
   405 
   406 static GstFlowReturn
   407 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
   408 {
   409   GstMythtvSrc *src;
   410   GstFlowReturn ret = GST_FLOW_OK;
   411   gint read = -1;
   412 
   413   src = GST_MYTHTV_SRC ( psrc );
   414 
   415   /* The caller should know the number of bytes and not read beyond EOS. */
   416   if (G_UNLIKELY (src->eos))
   417     goto eos;
   418   if ( G_UNLIKELY (src->update_prog_chain) )
   419     goto change_progchain;
   420 
   421   //g_static_rec_mutex_lock( &th_mutex );
   422   GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__, 
   423   		src->read_offset, MAX_READ_SIZE );
   424 
   425   GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__, 
   426       (gint) src->buffer_remain, src->bytes_queue->len );
   427 
   428   /* just get from the byte array, no network effort... */
   429   if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) { 
   430   			//( ( INTERNAL_BUFFER_SIZE - src->buffer_remain )  >=  REQUEST_MAX_SIZE ) ) {
   431   	GByteArray *buffer = NULL;
   432   	guint buffer_size_inter = ( INTERNAL_BUFFER_SIZE - src->buffer_remain );
   433   	if ( buffer_size_inter > REQUEST_MAX_SIZE )
   434   		buffer_size_inter = REQUEST_MAX_SIZE;
   435   		
   436   	buffer = g_byte_array_new();
   437 
   438     read = do_read_request_response( src, buffer_size_inter, buffer );
   439 
   440     if (G_UNLIKELY (read < 0)) {
   441       if ( src->live_tv )
   442 	goto change_progchain;
   443       else
   444 	goto read_error;
   445     } else if (G_UNLIKELY (read == 0))
   446     {
   447 	    if (!src->live_tv )
   448 	    	goto eos;
   449 	    else
   450 	    	goto done;
   451     }
   452 
   453     if ( G_UNLIKELY (src->update_prog_chain) )
   454       goto change_progchain;
   455     
   456     src->bytes_queue = g_byte_array_append( src->bytes_queue, buffer->data, read );
   457     if ( read > buffer_size_inter )
   458     	GST_WARNING_OBJECT( src, "\n\n\n[%s]\t\tINCREASED buffer size! Backend sent more than we ask him... (%d)\n\n\n", 
   459     			__FUNCTION__, abs( read - buffer_size_inter ) );
   460 
   461     src->buffer_remain += read;
   462 
   463     if ( buffer != NULL ) {
   464     	g_byte_array_free( buffer, TRUE );
   465     	buffer = NULL;    	
   466     }
   467 	
   468 	    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   469 		"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   470 		src->read_offset, src->content_size );
   471 	
   472   }
   473   
   474   guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
   475   
   476   /* Create the buffer. */
   477   ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   478       GST_BUFFER_OFFSET_NONE, buffer_size,
   479       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
   480 
   481   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
   482     if ( src->live_tv )
   483       goto change_progchain;
   484     else
   485       goto done;
   486   }
   487   //*outbuf = gst_buffer_new_and_alloc( buffer_size );
   488   
   489   /* gets the first buffer_size bytes from the byte array buffer variable */ 
   490   //guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
   491 
   492   GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__, 
   493   	read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
   494 
   495   GST_BUFFER_SIZE (*outbuf) = buffer_size;
   496   GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
   497   GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
   498   g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->bytes_queue->data, GST_BUFFER_SIZE(*outbuf) );
   499   GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
   500   GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
   501 
   502   src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
   503 
   504   src->read_offset += GST_BUFFER_SIZE (*outbuf);
   505   src->bytes_read += GST_BUFFER_SIZE (*outbuf);
   506   GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
   507   
   508   /* flushs the newly buffer got from byte array */
   509   src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
   510 
   511   GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
   512       "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
   513       GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
   514 
   515   GST_DEBUG_OBJECT( src, "[%s]\tCONTENT_SIZE = %llu, BYTES_READ = %llu.\n", __FUNCTION__, 
   516   		src->content_size, src->bytes_read );
   517       
   518   if ( G_UNLIKELY (src->eos) || ( !src->live_tv && ( src->bytes_read >= src->content_size ) ) )
   519    goto eos;
   520 
   521 done:
   522   {
   523     const gchar *reason = gst_flow_get_name (ret);
   524 
   525     GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
   526     return ret;
   527   }
   528 eos:
   529   {
   530     const gchar *reason = gst_flow_get_name (ret);
   531 
   532     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
   533     return GST_FLOW_UNEXPECTED;
   534   }
   535   /* ERRORS */
   536 read_error:
   537   {
   538     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   539 	(NULL), ("Could not read any bytes (%i, %s)", read,
   540 		 src->uri_name));
   541     return GST_FLOW_ERROR;
   542   }
   543 change_progchain:
   544   {
   545     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   546 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   547 		 src->uri_name));
   548 
   549     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   550 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   551     // go to the next program chain
   552     src->unique_setup = FALSE;
   553     src->update_prog_chain = TRUE;
   554 
   555     gst_mythtv_src_next_program_chain( src );
   556 
   557     return GST_FLOW_ERROR_NO_DATA;
   558   }
   559 
   560 }
   561 
   562 gint64
   563 gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
   564 {
   565 
   566   gint64 size_tmp = 0;
   567   guint max_tries = 2;
   568   if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
   569 		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   570 
   571 get_file_pos:
   572     g_usleep( 10 );
   573     size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   574     if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
   575       src->content_size = size_tmp;
   576     else if ( size_tmp > 0 && --max_tries > 0 )
   577       goto get_file_pos;
   578     GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
   579 	__FUNCTION__, size_tmp );
   580     /* sets the last content size amount before it can be updated */
   581     src->prev_content_size = src->content_size;
   582   }
   583 
   584   return src->content_size;	
   585 
   586 }
   587 
   588 static gboolean
   589 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
   590 {
   591   GstMythtvSrc *src = GST_MYTHTV_SRC( base );
   592   gint64 new_offset = -1;
   593   gint64 actual_seek = segment->start;
   594   gboolean ret = TRUE;
   595 
   596   GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, 
   597   		segment->start, segment->stop );
   598 
   599   if ( segment->format == GST_FORMAT_TIME ) 
   600   {
   601     goto done; 
   602     //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
   603   }
   604   GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
   605   /* verify if it needs to seek */
   606   if ( src->read_offset != actual_seek )
   607   {
   608 
   609     new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
   610 
   611     GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
   612 			__FUNCTION__, segment->start, src->read_offset, new_offset );
   613     if ( G_UNLIKELY (new_offset < 0 ) )
   614     {
   615       ret = FALSE;
   616       if ( src->live_tv )
   617 	goto change_progchain;
   618       else
   619 	goto eos;
   620     }
   621 
   622     src->read_offset = new_offset;
   623 
   624     if ( ret == FALSE ) {
   625       GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
   626     }
   627 
   628   }
   629 
   630 done:
   631    return ret;
   632 
   633 eos:
   634   {
   635 
   636     GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
   637     return FALSE;
   638   }
   639 change_progchain:
   640   {
   641     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   642 	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
   643 		 src->uri_name));
   644 
   645     gst_pad_push_event ( GST_BASE_SRC_PAD (base),
   646 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   647     /* go to the next program chain */
   648     src->unique_setup = FALSE;
   649     src->update_prog_chain = TRUE;
   650 
   651     gst_mythtv_src_next_program_chain( src );
   652 
   653     return TRUE;
   654   }
   655 
   656 }
   657 
   658 /* create a socket for connecting to remote server */
   659 static gboolean
   660 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   661 {
   662   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   663 
   664   GString *chain_id_local = NULL;
   665 
   666   gboolean ret = TRUE;
   667 
   668   if ( G_UNLIKELY (src->update_prog_chain) )
   669     goto change_progchain;
   670 
   671   if (src->unique_setup == FALSE) {
   672     src->unique_setup = TRUE;
   673   } else {
   674     goto done;
   675   }
   676   
   677   src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
   678   if ( src->live_tv ) {  	
   679     src->spawn_livetv = gmyth_livetv_new( );
   680     if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
   681       ret = FALSE;
   682       goto init_failed;
   683     }    
   684 
   685     src->file_transfer = gmyth_livetv_create_file_transfer(src->spawn_livetv);
   686     
   687     if (NULL == src->file_transfer ) 
   688 	  {
   689 	  	ret = FALSE;
   690 	    goto init_failed;
   691 	  }
   692 		/*
   693     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   694     if ( chain_id_local != NULL ) {
   695       src->live_chain_id = g_strdup( chain_id_local->str );
   696       GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );      
   697     }
   698     	
   699     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   700     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   701     */ 
   702   } else {
   703   	
   704   	src->file_transfer = gmyth_file_transfer_new( src->backend_info );
   705 
   706 		ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name );
   707 		
   708   }
   709 
   710   if ( NULL == src->file_transfer ) 
   711   {
   712     goto init_failed;
   713   }
   714   /*GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->spawn_livetv->file_transfer );*/
   715 
   716   if ( src->live_tv == TRUE && ret == TRUE ) {
   717     /* loop finished, set the max tries variable to zero again... */
   718     wait_to_transfer = 0;
   719 
   720     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
   721 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   722       g_usleep( 500 );
   723       
   724     /* IS_RECORDING again, just like the MythTV backend does... */
   725 	  gmyth_livetv_is_recording( src->spawn_livetv );
   726 	  
   727 	  sleep( 3 );  
   728 
   729   }
   730 
   731   if ( ret == FALSE ) {
   732 #ifndef GST_DISABLE_GST_DEBUG  
   733     if ( src->mythtv_msgs_dbg )
   734       GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   735 #endif
   736     goto begin_req_failed;
   737   }
   738   
   739   GST_INFO_OBJECT( src, "MythTV FileTransfer filesize = %lld, content_size = %lld!\n", 
   740   			src->file_transfer->filesize, src->content_size );
   741 
   742   src->content_size = src->file_transfer->filesize;
   743 
   744   src->do_start = FALSE;
   745   
   746   /* this is used for the buffer cache */
   747   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   748   src->buffer_remain = 0;
   749   
   750   gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   751 		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
   752 
   753 done:
   754 
   755 	if ( chain_id_local != NULL )
   756 	{
   757 		g_string_free( chain_id_local, TRUE );
   758 		chain_id_local = NULL;
   759 	}
   760 		
   761   return TRUE;
   762 
   763   /* ERRORS */
   764 init_failed:
   765   {
   766     if (src->spawn_livetv != NULL )
   767       g_object_unref( src->spawn_livetv );
   768 
   769     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   770 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   771     return FALSE;
   772   }
   773 begin_req_failed:
   774   {
   775     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   776 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   777     return FALSE;
   778   }
   779 change_progchain:
   780   {
   781     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   782 	(NULL), ("Seek failed, go to the next program info... (%s)",
   783 		 src->uri_name));
   784 
   785     gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   786 	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
   787 
   788     // go to the next program chain
   789     src->unique_setup = FALSE;
   790     src->update_prog_chain = TRUE;
   791 
   792     gst_mythtv_src_next_program_chain( src );
   793 
   794     return TRUE;
   795   }
   796 }
   797 
   798 /* create a new socket for connecting to the next program chain */
   799 static gboolean
   800 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
   801 {
   802   GString *chain_id_local = NULL;
   803 
   804   gboolean ret = TRUE;
   805 
   806   if ( !src->live_tv )
   807     goto init_failed;
   808     
   809   if (src->unique_setup == FALSE) {
   810     src->unique_setup = TRUE;
   811   } else {
   812     goto done;
   813   }
   814   
   815 	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   816 
   817   if (src->file_transfer) {
   818     g_object_unref (src->file_transfer);
   819     src->file_transfer = NULL;
   820   }
   821 
   822   if (src->uri_name) {
   823     g_free (src->uri_name);
   824   }
   825   
   826   if ( src->backend_info == NULL )  
   827   	src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
   828 
   829   if ( src->live_tv ) {
   830     if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
   831     	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
   832       ret = FALSE;
   833       goto init_failed;
   834     }
   835     /* set up the uri variable */
   836     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   837     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   838     if ( chain_id_local != NULL ) {
   839       src->live_chain_id = g_strdup( chain_id_local->str );
   840       GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   841     }
   842     src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
   843     GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   844   }
   845 
   846   src->file_transfer = gmyth_file_transfer_new(src->backend_info);
   847 
   848   if ( src->file_transfer == NULL ) {
   849     goto init_failed;
   850   }
   851   
   852 	ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name );
   853 
   854   /* sets the Playback monitor connection */
   855   //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   856 
   857   if ( src->live_tv == TRUE && ret == TRUE ) {
   858     /* loop finished, set the max tries variable to zero again... */
   859     wait_to_transfer = 0;
   860 
   861     g_usleep( 200 );
   862 
   863     while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
   864 				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
   865       g_usleep( 1000 );
   866   }
   867 
   868   /* sets the FileTransfer instance connection (video/audio download) */
   869   //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   870 
   871   if ( ret == FALSE ) {
   872 #ifndef GST_DISABLE_GST_DEBUG  
   873     if ( src->mythtv_msgs_dbg )
   874       GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   875 #endif
   876     goto begin_req_failed;
   877   }
   878   src->content_size_last = src->content_size;
   879 
   880   src->content_size = src->file_transfer->filesize;
   881   if ( src->live_tv ) {
   882   	wait_to_transfer = 0;
   883 	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
   884 	    src->content_size = gst_mythtv_src_get_position( src );
   885   }
   886 
   887   src->read_offset = 0;  
   888   
   889   if ( src->bytes_queue != NULL ) {
   890   	g_byte_array_free( src->bytes_queue, TRUE );
   891   }
   892   
   893   src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
   894   
   895   //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   896 	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
   897   
   898 done:
   899 	src->update_prog_chain = FALSE;
   900 	
   901 	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
   902 	
   903   return TRUE;
   904 
   905   /* ERRORS */
   906 init_failed:
   907   {
   908     if (src->spawn_livetv != NULL )
   909       g_object_unref( src->spawn_livetv );
   910 
   911     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   912 	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   913     return FALSE;
   914   }
   915 begin_req_failed:
   916   {
   917     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   918 	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   919     return FALSE;
   920   }
   921 
   922 }
   923 
   924 static gboolean
   925 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   926 {
   927   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   928   gboolean ret = TRUE;
   929   GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
   930   			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
   931   			
   932   if ( src->live_tv ) {
   933   	ret = FALSE;
   934 	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
   935 				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
   936     
   937     gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
   938     if ( new_offset > 0 && new_offset > src->content_size ) {
   939 			src->content_size = new_offset;
   940     } else if ( new_offset < src->content_size ) {
   941 			src->update_prog_chain = TRUE;
   942 	  }
   943 
   944   }
   945 
   946   *size = src->content_size;
   947   GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
   948   
   949   return ret;
   950 
   951 }
   952 
   953 /* close the socket and associated resources
   954  * used both to recover from errors and go to NULL state */
   955 static gboolean
   956 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   957 {
   958   GstMythtvSrc *src;
   959 
   960   src = GST_MYTHTV_SRC (bsrc);
   961 
   962   if (src->uri_name) {
   963     g_free (src->uri_name);
   964     src->uri_name = NULL;
   965   }
   966 
   967   if (src->mythtv_caps) {
   968     gst_caps_unref (src->mythtv_caps);
   969     src->mythtv_caps = NULL;
   970   }
   971 
   972   src->eos = FALSE;
   973 
   974   return TRUE;
   975 }
   976 
   977 static gboolean
   978 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   979 {
   980   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   981   gint64 cont_size = 0;
   982   gboolean ret = FALSE;
   983 
   984   switch (GST_EVENT_TYPE (event)) {
   985     case GST_EVENT_EOS:
   986       GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
   987 
   988       if ( src->live_tv ) {
   989 	cont_size = gst_mythtv_src_get_position (src);
   990 	if ( cont_size > src->content_size ) {
   991 	  src->content_size = cont_size;
   992 	  src->eos = FALSE;
   993 	} else {
   994 	  src->eos = TRUE;
   995 	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
   996 	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
   997 	}
   998       }
   999       break;
  1000     default:
  1001       ret = gst_pad_event_default (pad, event);
  1002   }
  1003 
  1004   return ret;
  1005 }
  1006 
  1007 static gboolean
  1008 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
  1009 {
  1010   return TRUE;
  1011 }
  1012 
  1013 static gboolean
  1014 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
  1015 {
  1016   gboolean res = FALSE;
  1017   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
  1018   GstFormat formt;
  1019 
  1020   switch (GST_QUERY_TYPE (query)) {
  1021     case GST_QUERY_POSITION:
  1022     {
  1023     	gint64 pos = -1;
  1024       gst_query_parse_position (query, &formt, &pos );
  1025       res = TRUE;
  1026       if ( formt == GST_FORMAT_BYTES ) {
  1027       	gst_query_set_position (query, formt, pos = myth->read_offset );
  1028       	GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
  1029       } else if ( formt == GST_FORMAT_TIME ) {
  1030       	GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
  1031       	res = gst_pad_query_default(pad, query);
  1032       }
  1033       break;
  1034     }
  1035     case GST_QUERY_DURATION:
  1036     {
  1037 #if 0
  1038       if (myth->duration != 0) {
  1039 	gint64 total;
  1040 	gint64 fps;
  1041 
  1042 	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
  1043 	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
  1044       }
  1045 #endif
  1046 	gint64 dur = -1;
  1047 	gst_query_parse_duration ( query, &formt, &dur );	
  1048   if ( formt == GST_FORMAT_BYTES ) {
  1049   	gst_query_set_duration (query, formt, dur = myth->content_size);
  1050   	GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
  1051   } else if ( formt == GST_FORMAT_TIME ) {
  1052   	GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
  1053   	gst_query_ref(query);
  1054   	res = gst_pad_query_default(pad, query);  	
  1055   }
  1056 	res = TRUE;
  1057       break;
  1058     }
  1059     default:
  1060     {
  1061       res = gst_pad_query_default(pad, query);
  1062       break;
  1063     }
  1064   }
  1065 
  1066   gst_object_unref (myth);
  1067 
  1068   return res;
  1069 }
  1070 
  1071 static GstStateChangeReturn
  1072 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
  1073 {
  1074   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
  1075   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
  1076 
  1077   switch (transition) {
  1078     case GST_STATE_CHANGE_NULL_TO_READY:
  1079       //src->do_start = TRUE;
  1080       //src->unique_setup = FALSE;
  1081       break;
  1082     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1083     	GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ );
  1084     	break;
  1085     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
  1086     	GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ );
  1087       if ( src->live_tv ) {
  1088 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1089 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1090 	      else
  1091 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1092       }
  1093 
  1094       break;
  1095     default:
  1096       break;
  1097   }
  1098 
  1099   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1100   if (ret == GST_STATE_CHANGE_FAILURE)
  1101     return ret;
  1102 
  1103   switch (transition) {
  1104     case GST_STATE_CHANGE_READY_TO_NULL:
  1105       GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
  1106       break;
  1107     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
  1108       GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
  1109     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1110       GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
  1111       if ( src->live_tv ) {
  1112 	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
  1113 	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
  1114 	      else
  1115 	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
  1116       	
  1117       /*
  1118       	
  1119   			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
  1120       			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
  1121 
  1122 				src->read_offset = 0;
  1123 				src->bytes_read = 0;
  1124 				src->unique_setup = FALSE;				
  1125 				gst_mythtv_src_next_program_chain( src );
  1126 				*/
  1127       }
  1128       
  1129       break;
  1130     default:
  1131       break;
  1132   }
  1133 
  1134   return ret;
  1135 }
  1136 
  1137 static void
  1138 gst_mythtv_src_set_property (GObject * object, guint prop_id,
  1139     const GValue * value, GParamSpec * pspec)
  1140 {
  1141   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1142 
  1143   GST_OBJECT_LOCK (mythtvsrc);
  1144   switch (prop_id) {
  1145     case PROP_URI:
  1146     case PROP_LOCATION:
  1147       {
  1148 	if (!g_value_get_string (value)) {
  1149 	  GST_WARNING ("location property cannot be NULL");
  1150 	  goto done;
  1151 	}
  1152 
  1153 	if (mythtvsrc->uri_name != NULL) {
  1154 	  g_free (mythtvsrc->uri_name);
  1155 	  mythtvsrc->uri_name = NULL;
  1156 	}
  1157 	mythtvsrc->uri_name = g_value_dup_string (value);
  1158 
  1159 	break;
  1160       }
  1161 #ifndef GST_DISABLE_GST_DEBUG
  1162     case PROP_GMYTHTV_DBG:
  1163       {
  1164 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1165 	break;
  1166       }
  1167 #endif
  1168     case PROP_GMYTHTV_VERSION:
  1169       {
  1170 	mythtvsrc->mythtv_version = g_value_get_int (value);
  1171 	break;
  1172       }
  1173     case PROP_GMYTHTV_LIVEID:
  1174       {
  1175 	mythtvsrc->live_tv_id = g_value_get_int (value);
  1176 	break;
  1177       }
  1178     case PROP_GMYTHTV_LIVE:
  1179       {
  1180 	mythtvsrc->live_tv = g_value_get_boolean (value);
  1181 	break;
  1182       }
  1183     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1184       {
  1185 	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1186 	break;
  1187       }      
  1188     case PROP_GMYTHTV_LIVE_CHAINID:
  1189       {
  1190 	if (!g_value_get_string (value)) {
  1191 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1192 	  goto done;
  1193 	}
  1194 
  1195 	if (mythtvsrc->live_chain_id != NULL) {
  1196 	  g_free (mythtvsrc->live_chain_id);
  1197 	  mythtvsrc->live_chain_id = NULL;
  1198 	}
  1199 	mythtvsrc->live_chain_id = g_value_dup_string (value);
  1200 	break;
  1201       }
  1202     case PROP_GMYTHTV_CHANNEL_NUM:
  1203       {
  1204 	mythtvsrc->channel_num = g_value_get_int (value);
  1205 	break;
  1206       }
  1207     default:
  1208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1209       break;
  1210   }
  1211   GST_OBJECT_UNLOCK (mythtvsrc);
  1212 done:
  1213   return;
  1214 }
  1215 
  1216 static void
  1217 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1218     GValue * value, GParamSpec * pspec)
  1219 {
  1220   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1221 
  1222   GST_OBJECT_LOCK (mythtvsrc);
  1223   switch (prop_id) {
  1224     case PROP_URI:
  1225     case PROP_LOCATION:
  1226       {
  1227 	gchar *str = g_strdup( "" );
  1228 
  1229 	if ( mythtvsrc->uri_name == NULL ) {
  1230 	  g_free (mythtvsrc->uri_name);
  1231 	  mythtvsrc->uri_name = NULL;
  1232 	} else {
  1233 	  str = g_strdup( mythtvsrc->uri_name );
  1234 	}
  1235 	g_value_set_string ( value, str );
  1236 	break;
  1237       }
  1238 #ifndef GST_DISABLE_GST_DEBUG
  1239     case PROP_GMYTHTV_DBG:
  1240       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
  1241       break;
  1242 #endif
  1243     case PROP_GMYTHTV_VERSION:
  1244       {
  1245 	g_value_set_int ( value, mythtvsrc->mythtv_version );
  1246 	break;
  1247       }
  1248     case PROP_GMYTHTV_LIVEID:
  1249       {
  1250 	g_value_set_int ( value, mythtvsrc->live_tv_id );
  1251 	break;
  1252       }
  1253     case PROP_GMYTHTV_LIVE:
  1254       g_value_set_boolean ( value, mythtvsrc->live_tv );
  1255       break;
  1256     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1257       g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
  1258       break;
  1259     case PROP_GMYTHTV_LIVE_CHAINID:
  1260       {
  1261 	gchar *str = g_strdup( "" );
  1262 
  1263 	if ( mythtvsrc->live_chain_id == NULL ) {
  1264 	  g_free (mythtvsrc->live_chain_id);
  1265 	  mythtvsrc->live_chain_id = NULL;
  1266 	} else {
  1267 	  str = g_strdup( mythtvsrc->live_chain_id );
  1268 	}
  1269 	g_value_set_string ( value, str );
  1270 	break;
  1271       }
  1272     case PROP_GMYTHTV_CHANNEL_NUM:
  1273       {
  1274 	g_value_set_int ( value, mythtvsrc->channel_num );
  1275 	break;
  1276       }
  1277     default:
  1278       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1279       break;
  1280   }
  1281   GST_OBJECT_UNLOCK (mythtvsrc);
  1282 }
  1283 
  1284 /* entry point to initialize the plug-in
  1285  * initialize the plug-in itself
  1286  * register the element factories and pad templates
  1287  * register the features
  1288  */
  1289 static gboolean
  1290 plugin_init (GstPlugin * plugin)
  1291 {
  1292   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1293       GST_TYPE_MYTHTV_SRC);
  1294 }
  1295 
  1296 /* this is the structure that gst-register looks for
  1297  * so keep the name plugin_desc, or you cannot get your plug-in registered */
  1298 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1299     GST_VERSION_MINOR,
  1300     "mythtv",
  1301     "lib MythTV src",
  1302     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
  1303 
  1304 
  1305 /*** GSTURIHANDLER INTERFACE *************************************************/
  1306   static guint 
  1307 gst_mythtv_src_uri_get_type (void)
  1308 {
  1309   return GST_URI_SRC;
  1310 }
  1311 
  1312   static gchar **
  1313 gst_mythtv_src_uri_get_protocols (void)
  1314 {
  1315   static gchar *protocols[] = { "myth", "myths", NULL };
  1316 
  1317   return protocols;
  1318 }
  1319 
  1320   static const gchar *
  1321 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1322 {
  1323   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1324 
  1325   return src->uri_name;
  1326 }
  1327 
  1328   static gboolean
  1329 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1330 {
  1331   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1332 
  1333   gchar *protocol;
  1334 
  1335   protocol = gst_uri_get_protocol (uri);
  1336   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1337     g_free (protocol);
  1338     return FALSE;
  1339   }
  1340   g_free (protocol);
  1341   g_object_set (src, "location", uri, NULL);
  1342 
  1343   return TRUE;
  1344 }
  1345 
  1346  static void
  1347 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1348 {
  1349   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1350 
  1351   iface->get_type = gst_mythtv_src_uri_get_type;
  1352   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1353   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1354   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1355 }
  1356 
  1357   void
  1358 size_header_handler (void *userdata, const char *value)
  1359 {
  1360   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1361 
  1362   //src->content_size = g_ascii_strtoull (value, NULL, 10);
  1363 
  1364   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1365 }