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