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