gst-plugins-mythtv/gstmythtvsrc.c
author leo_sobral
Thu Sep 21 00:15:52 2006 +0100 (2006-09-21)
branchtrunk
changeset 4 259b9773e5c3
parent 2 bd3829c2e9c9
child 5 62b5ba7616f8
permissions -rwxr-xr-x
[svn r5] Deleted files that are not needed anymore
     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                   ( 32*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   if (src->live_tv == TRUE) {
   593 get_file_pos:
   594     g_usleep( 100 );
   595     guint64 size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
   596     if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
   597       src->content_size = size_tmp;
   598     else
   599       goto get_file_pos;
   600     g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", 
   601 	__FUNCTION__, size_tmp);
   602   }
   603 #endif
   604   if (src->content_size <= 0)
   605     return FALSE;
   606 
   607   *size = src->content_size;
   608 
   609   return TRUE;
   610 }
   611 
   612 /* close the socket and associated resources
   613  * used both to recover from errors and go to NULL state */
   614   static gboolean
   615 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   616 {
   617   GstMythtvSrc *src;
   618 
   619   src = GST_MYTHTV_SRC (bsrc);
   620 
   621   if (src->uri_name) {
   622     g_free (src->uri_name);
   623     src->uri_name = NULL;
   624   }
   625 
   626   if (src->mythtv_caps) {
   627     gst_caps_unref (src->mythtv_caps);
   628     src->mythtv_caps = NULL;
   629   }
   630 
   631   src->eos = FALSE;
   632 
   633   return TRUE;
   634 }
   635 
   636   static gboolean
   637 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   638 {
   639   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   640 
   641   switch (GST_EVENT_TYPE (event)) {
   642     case GST_EVENT_FLUSH_START:
   643       src->eos = FALSE;
   644       break;
   645       //return TRUE;
   646     case GST_EVENT_FLUSH_STOP:
   647       src->do_start = TRUE;
   648       src->eos = FALSE;
   649       gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
   650       gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
   651       break;
   652     case GST_EVENT_SEEK:  	  
   653       {
   654 	gdouble rate;
   655 	//gboolean update = TRUE;
   656 	GstFormat format;
   657 	GstSeekType cur_type, stop_type;
   658 	GstSeekFlags flags;
   659 	gint64 cur = 0, stop = 0;
   660 	gst_event_parse_seek ( event, &rate, &format,
   661 	    &flags, &cur_type, &cur,
   662 	    &stop_type, &stop );
   663 
   664 	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
   665 	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
   666 	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
   667 	}
   668 	//gboolean ret = gst_event_parse_new_segment ( event,
   669 	//    &update, &rate, &format, &start, &stop,
   670 	//    &position );
   671 	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
   672 	//			cur, stop - cur + 1, GstBuffer)
   673 
   674       } 
   675     default:
   676       return gst_pad_event_default (pad, event);
   677   }
   678 
   679   return gst_pad_event_default (pad, event);
   680 }
   681 
   682   static gboolean
   683 gst_mythtv_src_is_seekable( GstBaseSrc *base_src )
   684 {
   685   return TRUE;
   686 }
   687 
   688   static void
   689 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   690     const GValue * value, GParamSpec * pspec)
   691 {
   692   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   693 
   694   GST_OBJECT_LOCK (mythtvsrc);
   695   switch (prop_id) {
   696     case PROP_URI:
   697     case PROP_LOCATION:
   698       {
   699 	if (!g_value_get_string (value)) {
   700 	  GST_WARNING ("location property cannot be NULL");
   701 	  goto done;
   702 	}
   703 
   704 	if (mythtvsrc->uri_name != NULL) {
   705 	  g_free (mythtvsrc->uri_name);
   706 	  mythtvsrc->uri_name = NULL;
   707 	}
   708 	mythtvsrc->uri_name = g_value_dup_string (value);
   709 
   710 	break;
   711       }
   712 #ifndef GST_DISABLE_GST_DEBUG
   713     case PROP_MYTHTV_DBG:
   714       {
   715 	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
   716 	break;
   717       }
   718 #endif
   719     case PROP_MYTHTV_VERSION:
   720       {
   721 	mythtvsrc->mythtv_version = g_value_get_int (value);
   722 	break;
   723       }
   724     case PROP_MYTHTV_LIVEID:
   725       {
   726 	mythtvsrc->live_tv_id = g_value_get_int (value);
   727 	break;
   728       }
   729     case PROP_MYTHTV_LIVE:
   730       {
   731 	mythtvsrc->live_tv = g_value_get_boolean (value);
   732 	break;
   733       }
   734     case PROP_MYTHTV_LIVE_CHAINID:
   735       {
   736 	if (!g_value_get_string (value)) {
   737 	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
   738 	  goto done;
   739 	}
   740 
   741 	if (mythtvsrc->live_chain_id != NULL) {
   742 	  g_free (mythtvsrc->live_chain_id);
   743 	  mythtvsrc->live_chain_id = NULL;
   744 	}
   745 	mythtvsrc->live_chain_id = g_value_dup_string (value);
   746 
   747 	break;
   748       }
   749 
   750     default:
   751       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   752       break;
   753   }
   754   GST_OBJECT_UNLOCK (mythtvsrc);
   755 done:
   756   return;
   757 }
   758 
   759   static void
   760 gst_mythtv_src_get_property (GObject * object, guint prop_id,
   761     GValue * value, GParamSpec * pspec)
   762 {
   763   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   764 
   765   GST_OBJECT_LOCK (mythtvsrc);
   766   switch (prop_id) {
   767     case PROP_URI:
   768     case PROP_LOCATION:
   769       {
   770 	gchar *str = g_strdup( "" );
   771 
   772 	if ( mythtvsrc->uri_name == NULL ) {
   773 	  g_free (mythtvsrc->uri_name);
   774 	  mythtvsrc->uri_name = NULL;
   775 	} else {
   776 	  str = g_strdup( mythtvsrc->uri_name );
   777 	}
   778 	g_value_set_string ( value, str );
   779 	break;
   780       }
   781 #ifndef GST_DISABLE_GST_DEBUG
   782     case PROP_MYTHTV_DBG:
   783       g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
   784       break;
   785 #endif
   786     case PROP_MYTHTV_VERSION:
   787       {
   788 	g_value_set_int ( value, mythtvsrc->mythtv_version );
   789 	break;
   790       }
   791     case PROP_MYTHTV_LIVEID:
   792       {
   793 	g_value_set_int ( value, mythtvsrc->live_tv_id );
   794 	break;
   795       }
   796     case PROP_MYTHTV_LIVE:
   797       g_value_set_boolean ( value, mythtvsrc->live_tv );
   798       break;
   799     case PROP_MYTHTV_LIVE_CHAINID:
   800       {
   801 	gchar *str = g_strdup( "" );
   802 
   803 	if ( mythtvsrc->live_chain_id == NULL ) {
   804 	  g_free (mythtvsrc->live_chain_id);
   805 	  mythtvsrc->live_chain_id = NULL;
   806 	} else {
   807 	  str = g_strdup( mythtvsrc->live_chain_id );
   808 	}
   809 	g_value_set_string ( value, str );
   810 	break;
   811       }
   812     default:
   813       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   814       break;
   815   }
   816   GST_OBJECT_UNLOCK (mythtvsrc);
   817 }
   818 
   819 /* entry point to initialize the plug-in
   820  * initialize the plug-in itself
   821  * register the element factories and pad templates
   822  * register the features
   823  */
   824   static gboolean
   825 plugin_init (GstPlugin * plugin)
   826 {
   827   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
   828       GST_TYPE_MYTHTV_SRC);
   829 }
   830 
   831 /* this is the structure that gst-register looks for
   832  * so keep the name plugin_desc, or you cannot get your plug-in registered */
   833 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   834     GST_VERSION_MINOR,
   835     "mythtv",
   836     "lib MythTV src",
   837     plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
   838 
   839 
   840 /*** GSTURIHANDLER INTERFACE *************************************************/
   841   static guint 
   842 gst_mythtv_src_uri_get_type (void)
   843 {
   844   return GST_URI_SRC;
   845 }
   846 
   847   static gchar **
   848 gst_mythtv_src_uri_get_protocols (void)
   849 {
   850   static gchar *protocols[] = { "myth", "myths", NULL };
   851 
   852   return protocols;
   853 }
   854 
   855   static const gchar *
   856 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
   857 {
   858   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   859 
   860   return src->uri_name;
   861 }
   862 
   863   static gboolean
   864 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
   865 {
   866   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
   867 
   868   gchar *protocol;
   869 
   870   protocol = gst_uri_get_protocol (uri);
   871   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
   872     g_free (protocol);
   873     return FALSE;
   874   }
   875   g_free (protocol);
   876   g_object_set (src, "location", uri, NULL);
   877 
   878   return TRUE;
   879 }
   880 
   881   static void
   882 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
   883 {
   884   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
   885 
   886   iface->get_type = gst_mythtv_src_uri_get_type;
   887   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
   888   iface->get_uri = gst_mythtv_src_uri_get_uri;
   889   iface->set_uri = gst_mythtv_src_uri_set_uri;
   890 }
   891 
   892   void
   893 size_header_handler (void *userdata, const char *value)
   894 {
   895   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
   896 
   897   //src->content_size = g_ascii_strtoull (value, NULL, 10);
   898 
   899   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
   900 }