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