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