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