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