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