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