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