gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Fri Apr 13 22:40:49 2007 +0100 (2007-04-13)
branchtrunk
changeset 550 aaf53e3d0b26
parent 546 992abe7045a2
child 551 3b459aedfce7
permissions -rwxr-xr-x
[svn r555] Fixes the _dispose handler, and adds the GMythFile instances.
     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 		if ( !gmyth_file_transfer_open( GMYTH_FILE_TRANSFER(src->file), src->spawn_livetv->uri != NULL ? 
   716 							gmyth_uri_get_path(src->spawn_livetv->uri) : 
   717 							src->spawn_livetv->proginfo->pathname->str ) )
   718 		{
   719 			GST_INFO_OBJECT (src, "Error: couldn't open the FileTransfer from LiveTV source!" );
   720 			g_object_unref( src->file );
   721 			src->file = NULL;
   722 			goto init_failed;
   723 		}
   724   } else {
   725 
   726     if ( gmyth_uri_is_local_file(gmyth_uri) )
   727     {
   728       src->file = GMYTH_FILE(gmyth_file_local_new(src->backend_info));
   729       ret = gmyth_file_local_open ( GMYTH_FILE_LOCAL( src->file ) );
   730     } else {
   731       src->file = GMYTH_FILE(gmyth_file_transfer_new(src->backend_info));
   732       ret = gmyth_file_transfer_open ( GMYTH_FILE_TRANSFER(src->file), src->uri_name);
   733     }
   734 
   735   } /* if (else) - recorded FileTransfer */
   736 
   737   if (NULL == src->file) {
   738     GST_INFO_OBJECT (src, "FileTransfer is NULL");
   739     goto init_failed;
   740   }
   741   /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file); */
   742 
   743   if (ret == FALSE) {
   744 #ifndef GST_DISABLE_GST_DEBUG
   745     if (src->mythtv_msgs_dbg)
   746       GST_INFO_OBJECT (src,
   747           "MythTV FileTransfer request failed when setting up socket connection!");
   748 #endif
   749     goto begin_req_failed;
   750   }
   751 
   752   GST_INFO_OBJECT (src,
   753       "MythTV FileTransfer filesize = %lld, content_size = %lld!",
   754       gmyth_file_get_filesize( src->file ), src->content_size);
   755 
   756   src->content_size = gmyth_file_get_filesize (src->file);
   757 
   758   src->do_start = FALSE;
   759 
   760   /* this is used for the buffer cache */
   761   src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
   762   src->buffer_remain = 0;
   763   
   764   gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
   765       gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   766           src->content_size, 0));
   767 
   768 done:
   769 	/*if ( gmyth_uri != NULL )
   770 	{
   771   	g_object_unref( gmyth_uri );
   772   	gmyth_uri = NULL;
   773 	}*/
   774 
   775   if (chain_id_local != NULL) {
   776     g_string_free (chain_id_local, TRUE);
   777     chain_id_local = NULL;
   778   }
   779 
   780   return TRUE;
   781 
   782   /* ERRORS */
   783 init_failed:
   784   {
   785     if (src->spawn_livetv != NULL)
   786       g_object_unref (src->spawn_livetv);
   787 
   788     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   789         (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
   790             src->uri_name));
   791     return FALSE;
   792   }
   793 begin_req_failed:
   794   {
   795     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
   796         (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
   797             src->uri_name));
   798     return FALSE;
   799   }
   800 
   801 }
   802 
   803 static gboolean
   804 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
   805 {
   806   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   807   gboolean ret = TRUE;
   808 
   809   GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
   810       abs (src->content_size - src->prev_content_size),
   811       GMYTHTV_TRANSFER_MAX_BUFFER);
   812 
   813   if (src->live_tv) {
   814     ret = FALSE;
   815   } else if (src->live_tv && src->enable_timing_position
   816       && (abs (src->content_size - src->bytes_read) <
   817           GMYTHTV_TRANSFER_MAX_BUFFER)) {
   818 
   819     gint64 new_offset =
   820         gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
   821     if (new_offset > 0 && new_offset > src->content_size) {
   822       src->content_size = new_offset;
   823     } else if (new_offset < src->content_size) {
   824       src->update_prog_chain = TRUE;
   825     }
   826 
   827   }
   828 
   829   *size = src->content_size;
   830   GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
   831 
   832   return ret;
   833 
   834 }
   835 
   836 /* close the socket and associated resources
   837  * used both to recover from errors and go to NULL state */
   838 static gboolean
   839 gst_mythtv_src_stop (GstBaseSrc * bsrc)
   840 {
   841   GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
   842   
   843   gst_mythtv_src_clear (src);
   844 
   845   /* src->eos = FALSE; */
   846 
   847   return TRUE;
   848 }
   849 
   850 static gboolean
   851 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
   852 {
   853   GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
   854   gint64 cont_size = 0;
   855   gboolean ret = FALSE;
   856 
   857   switch (GST_EVENT_TYPE (event)) {
   858     case GST_EVENT_EOS:
   859       GST_WARNING_OBJECT (src, "Got EOS event");
   860 
   861       if (src->live_tv) {
   862         cont_size = gst_mythtv_src_get_position (src);
   863         if (cont_size > src->content_size) {
   864           src->content_size = cont_size;
   865           src->eos = FALSE;
   866         } else {
   867           src->eos = TRUE;
   868           gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
   869           gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
   870         }
   871       }
   872       break;
   873     default:
   874       ret = gst_pad_event_default (pad, event);
   875   }
   876 
   877   return ret;
   878 }
   879 
   880 static gboolean
   881 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
   882 {
   883   return TRUE;
   884 }
   885 
   886 static gboolean
   887 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
   888 {
   889   gboolean res = FALSE;
   890   GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
   891   GstFormat formt;
   892 
   893   switch (GST_QUERY_TYPE (query)) {
   894     case GST_QUERY_POSITION:
   895     {
   896       gst_query_parse_position (query, &formt, NULL);
   897       if (formt == GST_FORMAT_BYTES) {
   898         gst_query_set_position (query, formt, myth->read_offset);
   899         GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
   900         res = TRUE;
   901       } else if (formt == GST_FORMAT_TIME) {
   902         res = gst_pad_query_default (pad, query);
   903       }
   904       break;
   905     }
   906     case GST_QUERY_DURATION:
   907     {
   908 #if 0
   909       if (myth->duration != 0) {
   910         gint64 total;
   911         gint64 fps;
   912 
   913         fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
   914         total =
   915             gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
   916       }
   917 #endif
   918 
   919       gst_query_parse_duration (query, &formt, NULL);
   920       if (formt == GST_FORMAT_BYTES) {
   921         gst_query_set_duration (query, formt, myth->content_size);
   922         GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, myth->content_size);
   923         res = TRUE;
   924       } else if (formt == GST_FORMAT_TIME) {
   925         res = gst_pad_query_default (pad, query);
   926       }
   927       break;
   928     }
   929     default:
   930     {
   931       res = gst_pad_query_default (pad, query);
   932       break;
   933     }
   934   }
   935 
   936   gst_object_unref (myth);
   937 
   938   return res;
   939 }
   940 
   941 static GstStateChangeReturn
   942 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
   943 {
   944   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
   945   GstMythtvSrc *src = GST_MYTHTV_SRC (element);
   946 
   947   switch (transition) {
   948     case GST_STATE_CHANGE_NULL_TO_READY:
   949       break;
   950     case GST_STATE_CHANGE_READY_TO_PAUSED:
   951     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
   952       if (src->live_tv) {
   953         if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
   954                 recorder))
   955           GST_WARNING_OBJECT (src,
   956               "Couldn't send the FRONTEND_READY message to the backend!");
   957         else
   958           GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
   959       }
   960       break;
   961     default:
   962       break;
   963   }
   964 
   965   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
   966   if (ret == GST_STATE_CHANGE_FAILURE)
   967     return ret;
   968 
   969   switch (transition) {
   970     case GST_STATE_CHANGE_READY_TO_NULL:
   971       break;
   972     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
   973     case GST_STATE_CHANGE_PAUSED_TO_READY:
   974       break;
   975     default:
   976       break;
   977   }
   978 
   979   return ret;
   980 }
   981 
   982 static void
   983 gst_mythtv_src_set_property (GObject * object, guint prop_id,
   984     const GValue * value, GParamSpec * pspec)
   985 {
   986   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
   987 
   988   GST_OBJECT_LOCK (mythtvsrc);
   989   switch (prop_id) {
   990     case PROP_LOCATION:
   991     {
   992       if (!g_value_get_string (value)) {
   993         GST_WARNING ("location property cannot be NULL");
   994 	break;
   995       }
   996 
   997       if (mythtvsrc->uri_name != NULL) {
   998         g_free (mythtvsrc->uri_name);
   999         mythtvsrc->uri_name = NULL;
  1000       }
  1001       mythtvsrc->uri_name = g_value_dup_string (value);
  1002 
  1003       break;
  1004     }
  1005 #ifndef GST_DISABLE_GST_DEBUG
  1006     case PROP_GMYTHTV_DBG:
  1007     {
  1008       mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
  1009       break;
  1010     }
  1011 #endif
  1012     case PROP_GMYTHTV_VERSION:
  1013     {
  1014       mythtvsrc->mythtv_version = g_value_get_int (value);
  1015       break;
  1016     }
  1017     case PROP_GMYTHTV_LIVEID:
  1018     {
  1019       mythtvsrc->live_tv_id = g_value_get_int (value);
  1020       break;
  1021     }
  1022     case PROP_GMYTHTV_LIVE:
  1023     {
  1024       mythtvsrc->live_tv = g_value_get_boolean (value);
  1025       break;
  1026     }
  1027     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1028     {
  1029       mythtvsrc->enable_timing_position = g_value_get_boolean (value);
  1030       break;
  1031     }
  1032     case PROP_GMYTHTV_LIVE_CHAINID:
  1033     {
  1034       if (!g_value_get_string (value)) {
  1035         GST_WARNING ("MythTV Live chainid property cannot be NULL");
  1036 	break;
  1037       }
  1038 
  1039       if (mythtvsrc->live_chain_id != NULL) {
  1040         g_free (mythtvsrc->live_chain_id);
  1041         mythtvsrc->live_chain_id = NULL;
  1042       }
  1043       mythtvsrc->live_chain_id = g_value_dup_string (value);
  1044       break;
  1045     }
  1046     case PROP_GMYTHTV_CHANNEL_NUM:
  1047     {
  1048       mythtvsrc->channel_name = g_value_dup_string (value);
  1049       break;
  1050     }
  1051     default:
  1052       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1053       break;
  1054   }
  1055 
  1056   GST_OBJECT_UNLOCK (mythtvsrc);
  1057 }
  1058 
  1059 static void
  1060 gst_mythtv_src_get_property (GObject * object, guint prop_id,
  1061     GValue * value, GParamSpec * pspec)
  1062 {
  1063   GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
  1064 
  1065   GST_OBJECT_LOCK (mythtvsrc);
  1066   switch (prop_id) {
  1067     case PROP_LOCATION:
  1068     {
  1069       g_value_set_string (value, mythtvsrc->uri_name);
  1070       break;
  1071     }
  1072 #ifndef GST_DISABLE_GST_DEBUG
  1073     case PROP_GMYTHTV_DBG:
  1074       g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
  1075       break;
  1076 #endif
  1077     case PROP_GMYTHTV_VERSION:
  1078     {
  1079       g_value_set_int (value, mythtvsrc->mythtv_version);
  1080       break;
  1081     }
  1082     case PROP_GMYTHTV_LIVEID:
  1083     {
  1084       g_value_set_int (value, mythtvsrc->live_tv_id);
  1085       break;
  1086     }
  1087     case PROP_GMYTHTV_LIVE:
  1088       g_value_set_boolean (value, mythtvsrc->live_tv);
  1089       break;
  1090     case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
  1091       g_value_set_boolean (value, mythtvsrc->enable_timing_position);
  1092       break;
  1093     case PROP_GMYTHTV_LIVE_CHAINID:
  1094     {
  1095       g_value_set_string (value, mythtvsrc->live_chain_id);
  1096       break;
  1097     }
  1098     case PROP_GMYTHTV_CHANNEL_NUM:
  1099     {
  1100       g_value_set_string (value, mythtvsrc->channel_name);
  1101       break;
  1102     }
  1103     default:
  1104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1105       break;
  1106   }
  1107   GST_OBJECT_UNLOCK (mythtvsrc);
  1108 }
  1109 
  1110 static gboolean
  1111 plugin_init (GstPlugin * plugin)
  1112 {
  1113   return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
  1114       GST_TYPE_MYTHTV_SRC);
  1115 }
  1116 
  1117 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1118     GST_VERSION_MINOR,
  1119     "mythtv",
  1120     "lib MythTV src",
  1121     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
  1122 
  1123 
  1124 /*** GSTURIHANDLER INTERFACE *************************************************/
  1125 static guint
  1126 gst_mythtv_src_uri_get_type (void)
  1127 {
  1128   return GST_URI_SRC;
  1129 }
  1130 
  1131 static gchar **
  1132 gst_mythtv_src_uri_get_protocols (void)
  1133 {
  1134   static gchar *protocols[] = { "myth", "myths", NULL };
  1135 
  1136   return protocols;
  1137 }
  1138 
  1139 static const gchar *
  1140 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
  1141 {
  1142   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1143 
  1144   return src->uri_name;
  1145 }
  1146 
  1147 static gboolean
  1148 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  1149 {
  1150   GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
  1151 
  1152   gchar *protocol;
  1153 
  1154   protocol = gst_uri_get_protocol (uri);
  1155   if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
  1156     g_free (protocol);
  1157     return FALSE;
  1158   }
  1159   g_free (protocol);
  1160   g_object_set (src, "location", uri, NULL);
  1161 
  1162   return TRUE;
  1163 }
  1164 
  1165 static void
  1166 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  1167 {
  1168   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  1169 
  1170   iface->get_type = gst_mythtv_src_uri_get_type;
  1171   iface->get_protocols = gst_mythtv_src_uri_get_protocols;
  1172   iface->get_uri = gst_mythtv_src_uri_get_uri;
  1173   iface->set_uri = gst_mythtv_src_uri_set_uri;
  1174 }
  1175 
  1176 void
  1177 size_header_handler (void *userdata, const char *value)
  1178 {
  1179   GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
  1180 
  1181   GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
  1182 }