gst-plugins-mythtv/gstmythtvsrc.c
author rosfran
Thu Sep 21 00:27:36 2006 +0100 (2006-09-21)
branchtrunk
changeset 5 62b5ba7616f8
parent 3 265cdb1c59e3
permissions -rwxr-xr-x
[svn r6] The first change on Sourceforge SVN!
     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 General Public
     7  * License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more 
    14  */
    15 
    16 #ifdef HAVE_CONFIG_H
    17 #include "config.h"
    18 #endif
    19 
    20 #include "gstmythtvsrc.h"
    21 #include "myth_file_transfer.h"
    22 #include "myth_livetv.h"
    23 
    24 #include <gmyth/gmyth_socket.h>
    25 #include <gmyth/gmyth_tvchain.h>
    26 
    27 #include <string.h>
    28 #include <unistd.h>
    29 
    30 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
    31 #define GST_CAT_DEFAULT mythtvsrc_debug
    32 
    33 #define GST_MYTHTV_ID_NUM		1
    34 
    35 #define MYTHTV_VERSION_DEFAULT		30
    36 
    37 #define MYTHTV_TRANSFER_MAX_WAITS	100
    38 
    39 #define MYTHTV_TRANSFER_MAX_BUFFER	( 32*1024  )
    40 
    41 /* 4*1024 ??? */
    42 #define MAX_READ_SIZE                   ( 16*1024 )
    43 
    44 #define ENABLE_TIMING_POSITION		1
    45 
    46 /* stablish a maximum iteration value to the IS_RECORDING message */
    47 static guint wait_to_transfer = 0;
    48 
    49 static const GstElementDetails gst_mythtv_src_details =
    50 GST_ELEMENT_DETAILS ("MythTV client source",
    51     "Source/Network",
    52     "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
    53     "Rosfran Borges <rosfran.borges@indt.org.br>");
    54 
    55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    56     GST_PAD_SRC,
    57     GST_PAD_ALWAYS,
    58     GST_STATIC_CAPS_ANY);
    59 
    60 enum
    61 {
    62   PROP_0,
    63   PROP_LOCATION,
    64   PROP_URI,
    65 #ifndef GST_DISABLE_GST_DEBUG
    66   PROP_MYTHTV_DBG,
    67 #endif
    68   PROP_MYTHTV_VERSION,
    69   PROP_MYTHTV_LIVE,
    70   PROP_MYTHTV_LIVEID,
    71   PROP_MYTHTV_LIVE_CHAINID
    72 };
    73 
    74 static void gst_mythtv_src_finalize (GObject * gobject);
    75 
    76 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc,
    77     guint64 offset, guint size, GstBuffer ** outbuf);
    78 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
    79 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
    80 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
    81 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *base_src );
    82 
    83 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
    84     const GValue * value, GParamSpec * pspec);
    85 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
    86     GValue * value, GParamSpec * pspec);
    87 
    88 static void
    89 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
    90 
    91 static gboolean
    92 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
    93 
    94   static void
    95 _urihandler_init (GType type)
    96 {
    97   static const GInterfaceInfo urihandler_info = {
    98     gst_mythtv_src_uri_handler_init,
    99     NULL,
   100     NULL
   101   };
   102 
   103   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
   104 
   105   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   106       "MythTV src");
   107 }
   108 
   109 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
   110     GST_TYPE_BASE_SRC, _urihandler_init);
   111 
   112   static void
   113 gst_mythtv_src_base_init (gpointer g_class)
   114 {
   115   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   116 
   117   gst_element_class_add_pad_template (element_class,
   118       gst_static_pad_template_get (&srctemplate));
   119 
   120   gst_element_class_set_details (element_class, &gst_mythtv_src_details);
   121 }
   122 
   123   static void
   124 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
   125 {
   126   GObjectClass *gobject_class;
   127   GstBaseSrcClass *gstbasesrc_class;
   128 
   129   gobject_class = (GObjectClass *) klass;
   130   gstbasesrc_class = (GstBaseSrcClass *) klass;
   131 
   132   gobject_class->set_property = gst_mythtv_src_set_property;
   133   gobject_class->get_property = gst_mythtv_src_get_property;
   134   gobject_class->finalize = gst_mythtv_src_finalize;
   135 
   136   g_object_class_install_property
   137     (gobject_class, PROP_LOCATION,
   138      g_param_spec_string ("location", "Location",
   139        "The location. In the form:"
   140        "\n\t\t\tmyth://a.com/file.nuv"
   141        "\n\t\t\tmyth://a.com:23223/file.nuv "
   142        "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
   143        "", G_PARAM_READWRITE));
   144 
   145   g_object_class_install_property
   146     (gobject_class, PROP_URI,
   147      g_param_spec_string ("uri", "Uri",
   148        "The location in form of a URI (deprecated; use location)",
   149        "", G_PARAM_READWRITE));
   150 
   151   g_object_class_install_property
   152     (gobject_class, PROP_MYTHTV_VERSION,
   153      g_param_spec_int ("mythtv-version", "mythtv-version",
   154        "Change Myth TV version",
   155        26, 30, 26, G_PARAM_READWRITE));
   156 
   157   g_object_class_install_property
   158     (gobject_class, PROP_MYTHTV_LIVEID,
   159      g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
   160        "Change Myth TV version",
   161        0, 200, GST_MYTHTV_ID_NUM, G_PARAM_READWRITE));
   162 
   163   g_object_class_install_property
   164     (gobject_class, PROP_MYTHTV_LIVE_CHAINID,
   165      g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
   166        "Sets the Myth TV chain ID (from TV Chain)",
   167        "", G_PARAM_READWRITE));
   168 
   169   g_object_class_install_property
   170     (gobject_class, PROP_MYTHTV_LIVE,
   171      g_param_spec_boolean ("mythtv-live", "mythtv-live",
   172        "Enable MythTV Live TV content streaming",
   173        FALSE, G_PARAM_READWRITE));
   174 
   175 #ifndef GST_DISABLE_GST_DEBUG
   176   g_object_class_install_property
   177     (gobject_class, PROP_MYTHTV_DBG,
   178      g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
   179        "Enable MythTV debug messages",
   180        FALSE, G_PARAM_READWRITE));
   181 #endif
   182 
   183   gstbasesrc_class->start = gst_mythtv_src_start;
   184   gstbasesrc_class->stop = gst_mythtv_src_stop;
   185   gstbasesrc_class->get_size = gst_mythtv_src_get_size;
   186   gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
   187 
   188   gstbasesrc_class->create = gst_mythtv_src_create;
   189 
   190 
   191   GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
   192       "MythTV Client Source");
   193 }
   194 
   195   static void
   196 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
   197 {
   198   this->file_transfer = NULL;
   199 
   200   this->unique_setup = FALSE;
   201 
   202   this->mythtv_version = MYTHTV_VERSION_DEFAULT;
   203 
   204   this->bytes_read = 0;
   205 
   206   this->content_size = -1;
   207   this->read_offset = 0;
   208 
   209   this->live_tv = FALSE;
   210 
   211   this->user_agent = g_strdup ("mythtvsrc");
   212   this->mythtv_caps = NULL;    
   213 
   214   gst_pad_set_event_function (GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
   215       GST_DEBUG_FUNCPTR (gst_mythtv_src_handle_event));
   216 
   217 }
   218 
   219   static void
   220 gst_mythtv_src_finalize (GObject * gobject)
   221 {
   222   GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
   223 
   224   g_free (this->user_agent);
   225 
   226   if (this->mythtv_caps) {
   227     gst_caps_unref (this->mythtv_caps);
   228     this->mythtv_caps = NULL;
   229   }
   230 
   231   if (this->file_transfer) {
   232     g_object_unref (this->file_transfer);
   233     this->file_transfer = NULL;
   234   }
   235 
   236   if (this->uri_name) {
   237     g_free (this->uri_name);
   238   }
   239 
   240   if (this->user_agent) {
   241     g_free (this->user_agent);
   242   }
   243 
   244   G_OBJECT_CLASS (parent_class)->finalize (gobject);
   245 }
   246 
   247 #if 0
   248   static guint
   249 do_seek( GstMythtvSrc *src, guint64 offset, guint size, GstBuffer *outbuf )
   250 {
   251   guint64 off_uint64 = myth_file_transfer_seek(src->file_transfer, offset, 1);
   252 
   253   g_print( "[%s] Call MythTV SEEK with offset %llu, got a new one %llu...\n", __FUNCTION__, 
   254       offset, off_uint64 );
   255 
   256   return off_uint64;
   257 
   258 }
   259 #endif
   260 
   261   static guint
   262 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer * outbuf)
   263 {
   264   guint read = 0;
   265   guint sizetoread = size; //GST_BUFFER_SIZE (outbuf);
   266 
   267   g_print( "[%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
   268 
   269   /* Loop sending the request:
   270    * Retry whilst authentication fails and we supply it. */
   271 
   272   ssize_t len = 0;
   273 
   274   GST_OBJECT_LOCK(src);
   275 
   276   while ( sizetoread > 0 ) {
   277 
   278     len = myth_file_transfer_read( src->file_transfer,
   279 	GST_BUFFER_DATA (outbuf) + read, sizetoread, TRUE );
   280 
   281     if ( len > 0 ) {
   282       read += len;
   283       src->read_offset += read;
   284       sizetoread -= len;
   285     } else if ( len < 0 ) {
   286       goto done;
   287     }
   288     /*else if ( len == 0 ) {
   289       goto eos;
   290     }*/
   291 
   292     if ( len == sizetoread )
   293       break;
   294 
   295   }
   296 
   297   if ( read > 0 ) {
   298     src->bytes_read += read;
   299 
   300     GST_BUFFER_SIZE (outbuf) = read;
   301   } else if ( read <= 0 || len <= 0 ) {
   302     if ( src->live_tv == FALSE )
   303       goto eos;
   304     else
   305       goto done;
   306   }
   307   //GST_BUFFER_OFFSET (outbuf) = src->read_offset;
   308 
   309   g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
   310       "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
   311       src->read_offset, src->content_size );
   312 
   313   GST_OBJECT_UNLOCK(src);
   314 
   315   if ( len < 0 ) {
   316     read = len;
   317     if ( src->live_tv == FALSE ) 
   318       goto eos;
   319     else
   320       goto done;
   321   }
   322 
   323   if ( src->bytes_read < src->content_size )
   324     goto done;
   325 
   326 eos:
   327   GST_OBJECT_UNLOCK(src);
   328 
   329   src->eos = TRUE;
   330 done:
   331   GST_OBJECT_UNLOCK(src);
   332 
   333   return read;
   334 }
   335 
   336   static GstFlowReturn
   337 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, 
   338     guint size, GstBuffer **outbuf )
   339 {
   340   GstMythtvSrc *src;
   341   GstFlowReturn ret = GST_FLOW_OK;
   342   guint read;
   343   guint64 size_tmp = 0;
   344 
   345   src = GST_MYTHTV_SRC (psrc);
   346 
   347   g_print( "[%s]\tBUFFER OFFSET = %llu, BUFFER SIZE = %d.\n", __FUNCTION__, offset, 
   348       size );
   349 
   350 
   351   /* The caller should know the number of bytes and not read beyond EOS. */
   352   if (G_UNLIKELY (src->eos))
   353     goto eos;
   354 
   355   /* Create the buffer. */
   356   ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
   357       //      GST_BUFFER_OFFSET_NONE, GST_BASE_SRC (psrc)->blocksize,
   358       offset, size,
   359       src->mythtv_caps ? src->mythtv_caps :
   360       GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
   361 
   362   if (G_UNLIKELY (ret == GST_FLOW_UNEXPECTED))
   363     goto eos;
   364 
   365   if (G_UNLIKELY (ret != GST_FLOW_OK))
   366     goto done;
   367 
   368   read = do_read_request_response ( src, offset, size, *outbuf );
   369 
   370 #if ENABLE_TIMING_POSITION == 1
   371   if (src->live_tv == TRUE) {
   372     //g_usleep( 1000 );
   373 get_file_pos:
   374     //g_usleep( 100 );
   375     size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   376     if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   377       src->content_size = size_tmp;
   378     else
   379       goto get_file_pos;
   380     g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", 
   381 	__FUNCTION__, size_tmp);
   382 
   383   }
   384 #endif
   385 
   386   if (G_UNLIKELY (read < 0))
   387     goto read_error;
   388 
   389   if (G_UNLIKELY(src->eos))
   390     goto eos;
   391 
   392 done:
   393   return ret;
   394 eos:
   395 #if ENABLE_TIMING_POSITION == 1
   396   if ( src->live_tv == TRUE ) {
   397     //g_usleep( 1000 );
   398     guint64 size_tmp = 0;
   399 get_file_pos_eos:
   400     //g_usleep( 100 );
   401     size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   402     if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   403       src->content_size = size_tmp;
   404     else
   405       goto get_file_pos_eos;
   406     g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", 
   407 	__FUNCTION__, size_tmp);
   408     goto done;
   409   } else 
   410 #endif
   411   {
   412     GST_DEBUG_OBJECT (src, "EOS reached");
   413     return GST_FLOW_UNEXPECTED;
   414   }
   415   /* ERRORS */
   416 read_error:
   417   {
   418     GST_ELEMENT_ERROR (src, RESOURCE, READ,
   419 	(NULL), ("Could not read any bytes (%i, %s)", read,
   420 	  src->uri_name));
   421     return GST_FLOW_ERROR;
   422   }
   423 }
   424 
   425 #if 0
   426 /* The following two charset mangling functions were copied from gnomevfssrc.
   427  * Preserve them under the unverified assumption that they do something vaguely
   428  * worthwhile.
   429  */
   430   static char *
   431 unicodify (const char *str, int len, ...)
   432 {
   433   char *ret = NULL, *cset;
   434   va_list args;
   435   gsize bytes_read, bytes_written;
   436 
   437   if (g_utf8_validate (str, len, NULL))
   438     return g_strndup (str, len >= 0 ? len : strlen (str));
   439 
   440   va_start (args, len);
   441   while ((cset = va_arg (args, char *)) != NULL)
   442   {
   443     if (!strcmp (cset, "locale"))
   444       ret = g_locale_to_utf8 (str, len, &bytes_read, &bytes_written, NULL);
   445     else
   446       ret = g_convert (str, len, "UTF-8", cset,
   447 	  &bytes_read, &bytes_written, NULL);
   448     if (ret)
   449       break;
   450   }
   451   va_end (args);
   452 
   453   return ret;
   454 }
   455 
   456   static char *
   457 gst_mythtv_src_unicodify (const char *str)
   458 {
   459   return unicodify (str, -1, "locale", "ISO-8859-1", NULL);
   460 }
   461 #endif
   462 
   463 /* create a socket for connecting to remote server */
   464   static gboolean
   465 gst_mythtv_src_start ( GstBaseSrc * bsrc )
   466 {
   467   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   468 
   469   GString *chain_id_local = NULL;
   470 
   471   gboolean ret = TRUE;
   472 #if 0
   473   if (src->live_tv == TRUE && src->file_transfer != NULL) {
   474     guint64 size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   475     if (size_tmp > src->content_size)
   476       src->content_size = size_tmp;
   477     g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", 
   478 	__FUNCTION__, size_tmp);
   479   }
   480 #endif
   481   if (src->unique_setup == FALSE) {
   482     src->unique_setup = TRUE;
   483   } else {
   484     goto done;
   485   }
   486 
   487   GST_OBJECT_LOCK(src);
   488 
   489   if ( src->live_tv ) {
   490     src->spawn_livetv = myth_livetv_new( );
   491     if ( myth_livetv_setup( src->spawn_livetv ) == FALSE ) {
   492     	ret = FALSE;
   493     	goto init_failed;
   494     }
   495     /* set up the uri variable */
   496     src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
   497     chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
   498     if ( chain_id_local != NULL ) {
   499       src->live_chain_id = g_strdup( chain_id_local->str );
   500       g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
   501     }
   502     src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
   503     g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
   504   }
   505 
   506   src->file_transfer = myth_file_transfer_new( src->live_tv_id, 
   507       g_string_new( src->uri_name ), -1, src->mythtv_version );
   508 
   509   if ( src->file_transfer == NULL ) {
   510     GST_OBJECT_UNLOCK(src);
   511 
   512     goto init_failed;
   513   }
   514 
   515   if ( src->live_tv ) {
   516     g_print ( "[%s] GST MYTHTVSRC: live_chain_id = %s\n", __FUNCTION__, src->live_chain_id );
   517     /* sets the MythSocket to the FileTransfer */
   518     //ret = myth_file_transfer_livetv_setup( &(src->file_transfer), src->spawn_livetv->remote_encoder->myth_socket );
   519   }
   520   /* sets the Playback monitor connection */
   521   ret = myth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
   522 
   523   if ( src->live_tv == TRUE && ret == TRUE ) {
   524     /* loop finished, set the max tries variable to zero again... */
   525     wait_to_transfer = 0;
   526 
   527     while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS && ( myth_file_transfer_is_recording( src->file_transfer ) == FALSE 
   528 	  /*|| ( myth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
   529       g_usleep( 100 );
   530   }
   531 
   532   /* sets the FileTransfer instance connection (video/audio download) */
   533   ret = myth_file_transfer_setup( &(src->file_transfer), src->live_tv );
   534 
   535   if ( ret == FALSE ) {
   536     GST_OBJECT_UNLOCK(src);
   537 #ifndef GST_DISABLE_GST_DEBUG  
   538     if ( src->mythtv_msgs_dbg )
   539       g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
   540 #endif
   541     goto begin_req_failed;
   542   }
   543 
   544   src->content_size = src->file_transfer->filesize;
   545 
   546   GST_OBJECT_UNLOCK(src);
   547 
   548 #if 0
   549   const char *str_value;
   550   gint gint_value;
   551 
   552   str_value = ne_get_response_header (src->request, "myth-metaint");
   553   if (str_value) {
   554     if ( sscanf (str_value, "%d", &gint_value) == 1 ) {
   555       if (src->myth_caps) {
   556 	gst_caps_unref (src->myth_caps);
   557 	src->myth_caps = NULL;
   558       }
   559       src->myth_metaint = gint_value;
   560 #endif
   561       //src->mythtv_caps = gst_caps_new_simple ("application/x-gst_ff-nuv", NULL);
   562       //   }
   563       // }
   564 done:
   565       return TRUE;
   566 
   567       /* ERRORS */
   568 init_failed:
   569       {
   570       	if (src->spawn_livetv != NULL )
   571 	  g_object_unref( src->spawn_livetv );
   572 
   573 	GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   574 	    (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
   575 	return FALSE;
   576       }
   577 begin_req_failed:
   578       {
   579 	GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   580 	    (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
   581 	return FALSE;
   582       }
   583 }
   584 
   585   static gboolean
   586 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   587 {
   588   GstMythtvSrc *src;
   589 
   590   src = GST_MYTHTV_SRC (bsrc);
   591 #if ENABLE_TIMING_POSITION == 1
   592   guint64 size_tmp = 0; 
   593   if (src->live_tv == TRUE) {
   594 get_file_pos:
   595     //g_usleep( 100 );
   596     size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   597     if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   598       src->content_size = size_tmp;
   599     else
   600       goto get_file_pos;
   601     g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", 
   602 	__FUNCTION__, size_tmp);
   603   }
   604 #endif
   605   if (src->content_size <= 0)
   606     return FALSE;
   607 
   608   *size = src->content_size;
   609 
   610   return TRUE;
   611 }
   612 
   613 /* close the socket and associated resources
   614  * used both to recover from errors and go to NULL state */
   615   static gboolean
   616 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   617 {
   618   GstMythtvSrc *src;
   619 
   620   src = GST_MYTHTV_SRC (bsrc);
   621 
   622   if (src->uri_name) {
   623     g_free (src->uri_name);
   624     src->uri_name = NULL;
   625   }
   626 
   627   if (src->mythtv_caps) {
   628     gst_caps_unref (src->mythtv_caps);
   629     src->mythtv_caps = NULL;
   630   }
   631 
   632   src->eos = FALSE;
   633 
   634   return TRUE;
   635 }
   636 
   637   static gboolean
   638 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   639 {
   640   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   641 
   642   switch (GST_EVENT_TYPE (event)) {
   643     case GST_EVENT_FLUSH_START:
   644       src->eos = FALSE;
   645       break;
   646       //return TRUE;
   647     case GST_EVENT_FLUSH_STOP:
   648       src->do_start = TRUE;
   649       src->eos = FALSE;
   650       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
   651       gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
   652       break;
   653     case GST_EVENT_SEEK:  	  
   654       {
   655 	gdouble rate;
   656 	//gboolean update = TRUE;
   657 	GstFormat format;
   658 	GstSeekType cur_type, stop_type;
   659 	GstSeekFlags flags;
   660 	gint64 cur = 0, stop = 0;
   661 	gst_event_parse_seek ( event, &rate, &format,
   662 	    &flags, &cur_type, &cur,
   663 	    &stop_type, &stop );
   664 
   665 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
   666 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   667 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   668 	}
   669 	//gboolean ret = gst_event_parse_new_segment ( event,
   670 	//    &update, &rate, &format, &start, &stop,
   671 	//    &position );
   672 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
   673 	//			cur, stop - cur + 1, GstBuffer)
   674 
   675       } 
   676     default:
   677       return gst_pad_event_default (pad, event);
   678   }
   679 
   680   return gst_pad_event_default (pad, event);
   681 }
   682 
   683   static gboolean
   684 gst_mythtv_src_is_seekable( GstBaseSrc *base_src )
   685 {
   686   return TRUE;
   687 }
   688 
   689   static void
   690 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   691     const GValue * value, GParamSpec * pspec)
   692 {
   693   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   694 
   695   GST_OBJECT_LOCK (mythtvsrc);
   696   switch (prop_id) {
   697     case PROP_URI:
   698     case PROP_LOCATION:
   699       {
   700 	if (!g_value_get_string (value)) {
   701 	  GST_WARNING ("location property cannot be NULL");
   702 	  goto done;
   703 	}
   704 
   705 	if (mythtvsrc->uri_name != NULL) {
   706 	  g_free (mythtvsrc->uri_name);
   707 	  mythtvsrc->uri_name = NULL;
   708 	}
   709 	mythtvsrc->uri_name = g_value_dup_string (value);
   710 
   711 	break;
   712       }
   713 #ifndef GST_DISABLE_GST_DEBUG
   714     case PROP_MYTHTV_DBG:
   715       {
   716 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
   717 	break;
   718       }
   719 #endif
   720     case PROP_MYTHTV_VERSION:
   721       {
   722 	mythtvsrc->mythtv_version = g_value_get_int (value);
   723 	break;
   724       }
   725     case PROP_MYTHTV_LIVEID:
   726       {
   727 	mythtvsrc->live_tv_id = g_value_get_int (value);
   728 	break;
   729       }
   730     case PROP_MYTHTV_LIVE:
   731       {
   732 	mythtvsrc->live_tv = g_value_get_boolean (value);
   733 	break;
   734       }
   735     case PROP_MYTHTV_LIVE_CHAINID:
   736       {
   737 	if (!g_value_get_string (value)) {
   738 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
   739 	  goto done;
   740 	}
   741 
   742 	if (mythtvsrc->live_chain_id != NULL) {
   743 	  g_free (mythtvsrc->live_chain_id);
   744 	  mythtvsrc->live_chain_id = NULL;
   745 	}
   746 	mythtvsrc->live_chain_id = g_value_dup_string (value);
   747 
   748 	break;
   749       }
   750 
   751     default:
   752       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   753       break;
   754   }
   755   GST_OBJECT_UNLOCK (mythtvsrc);
   756 done:
   757   return;
   758 }
   759 
   760   static void
   761 gst_mythtv_src_get_property (GObject * object, guint prop_id,
   762     GValue * value, GParamSpec * pspec)
   763 {
   764   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   765 
   766   GST_OBJECT_LOCK (mythtvsrc);
   767   switch (prop_id) {
   768     case PROP_URI:
   769     case PROP_LOCATION:
   770       {
   771 	gchar *str = g_strdup( "" );
   772 
   773 	if ( mythtvsrc->uri_name == NULL ) {
   774 	  g_free (mythtvsrc->uri_name);
   775 	  mythtvsrc->uri_name = NULL;
   776 	} else {
   777 	  str = g_strdup( mythtvsrc->uri_name );
   778 	}
   779 	g_value_set_string ( value, str );
   780 	break;
   781       }
   782 #ifndef GST_DISABLE_GST_DEBUG
   783     case PROP_MYTHTV_DBG:
   784       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
   785       break;
   786 #endif
   787     case PROP_MYTHTV_VERSION:
   788       {
   789 	g_value_set_int ( value, mythtvsrc->mythtv_version );
   790 	break;
   791       }
   792     case PROP_MYTHTV_LIVEID:
   793       {
   794 	g_value_set_int ( value, mythtvsrc->live_tv_id );
   795 	break;
   796       }
   797     case PROP_MYTHTV_LIVE:
   798       g_value_set_boolean ( value, mythtvsrc->live_tv );
   799       break;
   800     case PROP_MYTHTV_LIVE_CHAINID:
   801       {
   802 	gchar *str = g_strdup( "" );
   803 
   804 	if ( mythtvsrc->live_chain_id == NULL ) {
   805 	  g_free (mythtvsrc->live_chain_id);
   806 	  mythtvsrc->live_chain_id = NULL;
   807 	} else {
   808 	  str = g_strdup( mythtvsrc->live_chain_id );
   809 	}
   810 	g_value_set_string ( value, str );
   811 	break;
   812       }
   813     default:
   814       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   815       break;
   816   }
   817   GST_OBJECT_UNLOCK (mythtvsrc);
   818 }
   819 
   820 /* entry point to initialize the plug-in
   821  * initialize the plug-in itself
   822  * register the element factories and pad templates
   823  * register the features
   824  */
   825   static gboolean
   826 plugin_init (GstPlugin * plugin)
   827 {
   828   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
   829       GST_TYPE_MYTHTV_SRC);
   830 }
   831 
   832 /* this is the structure that gst-register looks for
   833  * so keep the name plugin_desc, or you cannot get your plug-in registered */
   834 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   835     GST_VERSION_MINOR,
   836     "mythtv",
   837     "lib MythTV src",
   838     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
   839 
   840 
   841 /*** GSTURIHANDLER INTERFACE *************************************************/
   842   static guint 
   843 gst_mythtv_src_uri_get_type (void)
   844 {
   845   return GST_URI_SRC;
   846 }
   847 
   848   static gchar **
   849 gst_mythtv_src_uri_get_protocols (void)
   850 {
   851   static gchar *protocols[] = { "myth", "myths", NULL };
   852 
   853   return protocols;
   854 }
   855 
   856   static const gchar *
   857 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
   858 {
   859   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   860 
   861   return src->uri_name;
   862 }
   863 
   864   static gboolean
   865 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
   866 {
   867   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   868 
   869   gchar *protocol;
   870 
   871   protocol = gst_uri_get_protocol (uri);
   872   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
   873     g_free (protocol);
   874     return FALSE;
   875   }
   876   g_free (protocol);
   877   g_object_set (src, "location", uri, NULL);
   878 
   879   return TRUE;
   880 }
   881 
   882   static void
   883 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
   884 {
   885   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
   886 
   887   iface->get_type = gst_mythtv_src_uri_get_type;
   888   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
   889   iface->get_uri = gst_mythtv_src_uri_get_uri;
   890   iface->set_uri = gst_mythtv_src_uri_set_uri;
   891 }
   892 
   893   void
   894 size_header_handler (void *userdata, const char *value)
   895 {
   896   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
   897 
   898   //src->content_size = g_ascii_strtoull (value, NULL, 10);
   899 
   900   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
   901 }