gst-plugins-mythtv/src/gstmythtvsrc.c
author melunko
Fri Nov 24 21:19:49 2006 +0000 (2006-11-24)
branchtrunk
changeset 111 dfa72795bd32
parent 108 f2b53549deef
child 114 04cead6ae39b
permissions -rwxr-xr-x
[svn r112] Added cache to request more data from the backend by time
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@81
   294
  
rosfran@81
   295
  if ( this->th_read_ahead != NULL ) {
rosfran@81
   296
  	gst_task_stop( this->th_read_ahead );
rosfran@81
   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@52
   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@100
   322
  
rosfran@100
   323
  if ( this->bytes_queue ) {
rosfran@100
   324
  	g_byte_array_free( this->bytes_queue, TRUE );
rosfran@100
   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@52
   539
    size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
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@98
   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@106
   656
      GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
leo_sobral@2
   657
    }
rosfran@70
   658
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@106
   659
    GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
leo_sobral@2
   660
  }
leo_sobral@2
   661
rosfran@40
   662
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
leo_sobral@2
   663
      g_string_new( src->uri_name ), -1, src->mythtv_version );
leo_sobral@2
   664
leo_sobral@2
   665
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   666
    goto init_failed;
leo_sobral@2
   667
  }
leo_sobral@2
   668
leo_sobral@2
   669
  /* sets the Playback monitor connection */
rosfran@40
   670
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   671
leo_sobral@2
   672
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   673
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   674
    wait_to_transfer = 0;
leo_sobral@2
   675
rosfran@70
   676
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
rosfran@108
   677
				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
rosfran@110
   678
      g_usleep( 500 );
rosfran@110
   679
      
rosfran@110
   680
    /* IS_RECORDING again, just like the MythTV backend does... */
rosfran@110
   681
	  gmyth_livetv_is_recording( src->spawn_livetv );
rosfran@110
   682
	  
rosfran@110
   683
	  sleep( 5 );  
rosfran@110
   684
leo_sobral@2
   685
  }
leo_sobral@2
   686
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@40
   687
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   688
leo_sobral@2
   689
  if ( ret == FALSE ) {
leo_sobral@2
   690
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   691
    if ( src->mythtv_msgs_dbg )
rosfran@106
   692
      GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   693
#endif
leo_sobral@2
   694
    goto begin_req_failed;
leo_sobral@2
   695
  }
leo_sobral@2
   696
leo_sobral@2
   697
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   698
rosfran@71
   699
  src->do_start = FALSE;
rosfran@90
   700
rosfran@100
   701
  src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
rosfran@90
   702
  //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
rosfran@90
   703
  //gst_task_set_lock( src->th_read_ahead, &th_mutex );
rosfran@90
   704
  //gst_task_start( src->th_read_ahead );
rosfran@90
   705
  src->buffer_remain = 0;
rosfran@90
   706
rosfran@30
   707
done:
rosfran@30
   708
  return TRUE;
rosfran@30
   709
rosfran@30
   710
  /* ERRORS */
rosfran@30
   711
init_failed:
rosfran@30
   712
  {
rosfran@30
   713
    if (src->spawn_livetv != NULL )
rosfran@30
   714
      g_object_unref( src->spawn_livetv );
rosfran@30
   715
rosfran@30
   716
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   717
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   718
    return FALSE;
rosfran@30
   719
  }
rosfran@30
   720
begin_req_failed:
rosfran@30
   721
  {
rosfran@30
   722
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   723
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   724
    return FALSE;
rosfran@30
   725
  }
rosfran@71
   726
change_progchain:
rosfran@71
   727
  {
rosfran@71
   728
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@90
   729
	(NULL), ("Seek failed, go to the next program info... (%s)",
rosfran@90
   730
		 src->uri_name));
rosfran@90
   731
rosfran@90
   732
    gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@90
   733
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@90
   734
rosfran@90
   735
    // go to the next program chain
rosfran@90
   736
    src->unique_setup = FALSE;
rosfran@90
   737
    src->update_prog_chain = TRUE;
rosfran@90
   738
rosfran@90
   739
    gst_mythtv_src_next_program_chain( src );
rosfran@90
   740
rosfran@71
   741
    return TRUE;
rosfran@71
   742
  }
rosfran@30
   743
}
leo_sobral@2
   744
rosfran@52
   745
/* create a new socket for connecting to the next program chain */
rosfran@52
   746
static gboolean
rosfran@52
   747
gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
rosfran@52
   748
{
rosfran@52
   749
  GString *chain_id_local = NULL;
rosfran@52
   750
rosfran@52
   751
  gboolean ret = TRUE;
rosfran@61
   752
rosfran@52
   753
  if ( !src->live_tv )
rosfran@61
   754
    goto init_failed;
rosfran@70
   755
    
rosfran@70
   756
  if (src->unique_setup == FALSE) {
rosfran@70
   757
    src->unique_setup = TRUE;
rosfran@70
   758
  } else {
rosfran@70
   759
    goto done;
rosfran@70
   760
  }
rosfran@81
   761
  
rosfran@81
   762
	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@61
   763
rosfran@52
   764
  if (src->file_transfer) {
rosfran@52
   765
    g_object_unref (src->file_transfer);
rosfran@52
   766
    src->file_transfer = NULL;
rosfran@52
   767
  }
rosfran@61
   768
rosfran@52
   769
  if (src->uri_name) {
rosfran@52
   770
    g_free (src->uri_name);
rosfran@52
   771
  }
rosfran@61
   772
rosfran@52
   773
  if ( src->live_tv ) {
rosfran@61
   774
    if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
rosfran@106
   775
    	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
rosfran@61
   776
      ret = FALSE;
rosfran@61
   777
      goto init_failed;
rosfran@52
   778
    }
rosfran@52
   779
    /* set up the uri variable */
rosfran@52
   780
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
rosfran@52
   781
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
rosfran@52
   782
    if ( chain_id_local != NULL ) {
rosfran@52
   783
      src->live_chain_id = g_strdup( chain_id_local->str );
rosfran@106
   784
      GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
rosfran@52
   785
    }
rosfran@70
   786
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@106
   787
    GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
rosfran@52
   788
  }
rosfran@52
   789
rosfran@52
   790
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
rosfran@52
   791
      g_string_new( src->uri_name ), -1, src->mythtv_version );
rosfran@52
   792
rosfran@52
   793
  if ( src->file_transfer == NULL ) {
rosfran@52
   794
    goto init_failed;
rosfran@52
   795
  }
rosfran@52
   796
rosfran@52
   797
  /* sets the Playback monitor connection */
rosfran@52
   798
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
rosfran@52
   799
rosfran@52
   800
  if ( src->live_tv == TRUE && ret == TRUE ) {
rosfran@52
   801
    /* loop finished, set the max tries variable to zero again... */
rosfran@52
   802
    wait_to_transfer = 0;
rosfran@61
   803
rosfran@61
   804
    g_usleep( 200 );
rosfran@52
   805
rosfran@52
   806
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
rosfran@108
   807
				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
rosfran@61
   808
      g_usleep( 1000 );
rosfran@52
   809
  }
rosfran@52
   810
rosfran@52
   811
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@52
   812
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
rosfran@52
   813
rosfran@52
   814
  if ( ret == FALSE ) {
rosfran@52
   815
#ifndef GST_DISABLE_GST_DEBUG  
rosfran@52
   816
    if ( src->mythtv_msgs_dbg )
rosfran@106
   817
      GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
rosfran@52
   818
#endif
rosfran@52
   819
    goto begin_req_failed;
rosfran@52
   820
  }
rosfran@61
   821
  src->content_size_last = src->content_size;
rosfran@61
   822
rosfran@61
   823
  src->content_size = src->file_transfer->filesize;
rosfran@87
   824
  if ( src->live_tv ) {
rosfran@87
   825
  	wait_to_transfer = 0;
rosfran@87
   826
	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
rosfran@87
   827
	    src->content_size = gst_mythtv_src_get_position( src );
rosfran@87
   828
  }
rosfran@63
   829
rosfran@81
   830
  src->read_offset = 0;  
rosfran@70
   831
  
rosfran@100
   832
  if ( src->bytes_queue != NULL ) {
rosfran@100
   833
  	g_byte_array_free( src->bytes_queue, TRUE );
rosfran@100
   834
  }
rosfran@100
   835
  
rosfran@100
   836
  src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
rosfran@100
   837
  
rosfran@70
   838
done:
rosfran@81
   839
	src->update_prog_chain = FALSE;
rosfran@81
   840
	
rosfran@81
   841
	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@81
   842
	
rosfran@52
   843
  return TRUE;
rosfran@52
   844
rosfran@52
   845
  /* ERRORS */
rosfran@52
   846
init_failed:
rosfran@52
   847
  {
rosfran@52
   848
    if (src->spawn_livetv != NULL )
rosfran@52
   849
      g_object_unref( src->spawn_livetv );
rosfran@52
   850
rosfran@52
   851
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
   852
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@52
   853
    return FALSE;
rosfran@52
   854
  }
rosfran@52
   855
begin_req_failed:
rosfran@52
   856
  {
rosfran@52
   857
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
   858
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@52
   859
    return FALSE;
rosfran@52
   860
  }
rosfran@61
   861
rosfran@52
   862
}
rosfran@52
   863
rosfran@30
   864
static gboolean
leo_sobral@2
   865
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
   866
{
rosfran@31
   867
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
   868
  gboolean ret = TRUE;
rosfran@106
   869
  GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
rosfran@63
   870
  			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
rosfran@98
   871
  			
rosfran@100
   872
  if ( src->live_tv ) {
rosfran@98
   873
  	ret = FALSE;
rosfran@100
   874
	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
rosfran@70
   875
				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
rosfran@70
   876
    
rosfran@61
   877
    gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@70
   878
    if ( new_offset > 0 && new_offset > src->content_size ) {
rosfran@70
   879
			src->content_size = new_offset;
rosfran@70
   880
    } else if ( new_offset < src->content_size ) {
rosfran@70
   881
			src->update_prog_chain = TRUE;
rosfran@70
   882
	  }
rosfran@61
   883
leo_sobral@2
   884
  }
leo_sobral@2
   885
leo_sobral@2
   886
  *size = src->content_size;
rosfran@106
   887
  GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
rosfran@87
   888
  
rosfran@30
   889
  return ret;
rosfran@30
   890
leo_sobral@2
   891
}
leo_sobral@2
   892
leo_sobral@2
   893
/* close the socket and associated resources
leo_sobral@2
   894
 * used both to recover from errors and go to NULL state */
rosfran@52
   895
static gboolean
leo_sobral@2
   896
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   897
{
leo_sobral@2
   898
  GstMythtvSrc *src;
leo_sobral@2
   899
leo_sobral@2
   900
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   901
leo_sobral@2
   902
  if (src->uri_name) {
leo_sobral@2
   903
    g_free (src->uri_name);
leo_sobral@2
   904
    src->uri_name = NULL;
leo_sobral@2
   905
  }
leo_sobral@2
   906
leo_sobral@2
   907
  if (src->mythtv_caps) {
leo_sobral@2
   908
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   909
    src->mythtv_caps = NULL;
leo_sobral@2
   910
  }
leo_sobral@2
   911
leo_sobral@2
   912
  src->eos = FALSE;
leo_sobral@2
   913
leo_sobral@2
   914
  return TRUE;
leo_sobral@2
   915
}
leo_sobral@2
   916
rosfran@30
   917
static gboolean
leo_sobral@2
   918
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   919
{
leo_sobral@2
   920
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
rosfran@52
   921
  gint64 cont_size = 0;
rosfran@90
   922
  gboolean ret = FALSE;
leo_sobral@2
   923
leo_sobral@2
   924
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   925
    case GST_EVENT_EOS:
rosfran@106
   926
      GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@61
   927
rosfran@61
   928
      if ( src->live_tv ) {
rosfran@61
   929
	cont_size = gst_mythtv_src_get_position (src);
rosfran@61
   930
	if ( cont_size > src->content_size ) {
rosfran@61
   931
	  src->content_size = cont_size;
rosfran@61
   932
	  src->eos = FALSE;
rosfran@61
   933
	} else {
rosfran@61
   934
	  src->eos = TRUE;
rosfran@61
   935
	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@61
   936
	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@61
   937
	}
rosfran@61
   938
      } else 
rosfran@61
   939
	src->eos = TRUE;
rosfran@90
   940
      ret = TRUE;
rosfran@30
   941
      break;
rosfran@30
   942
    case GST_EVENT_NEWSEGMENT:
rosfran@106
   943
      GST_INFO_OBJECT( src, "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@90
   944
      ret = gst_pad_event_default (pad, event);
leo_sobral@2
   945
      break;
leo_sobral@2
   946
    case GST_EVENT_SEEK:  	  
leo_sobral@2
   947
      {
rosfran@90
   948
	gst_event_ref( event );
rosfran@90
   949
leo_sobral@2
   950
	gdouble rate;
leo_sobral@2
   951
	GstFormat format;
leo_sobral@2
   952
	GstSeekType cur_type, stop_type;
leo_sobral@2
   953
	GstSeekFlags flags;
leo_sobral@2
   954
	gint64 cur = 0, stop = 0;
leo_sobral@2
   955
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
   956
	    &flags, &cur_type, &cur,
leo_sobral@2
   957
	    &stop_type, &stop );
leo_sobral@2
   958
rosfran@106
   959
	GST_INFO_OBJECT( src, "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
leo_sobral@2
   960
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
rosfran@106
   961
	  GST_WARNING_OBJECT( src, "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
   962
	}
rosfran@90
   963
	if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
rosfran@90
   964
	  gst_event_unref( event );
rosfran@90
   965
	  break;
rosfran@90
   966
	}
rosfran@90
   967
	
rosfran@90
   968
	break;
rosfran@30
   969
      }
leo_sobral@2
   970
    default:
rosfran@90
   971
      ret = gst_pad_event_default (pad, event);
leo_sobral@2
   972
  }
leo_sobral@2
   973
rosfran@90
   974
  return ret;
leo_sobral@2
   975
}
leo_sobral@2
   976
rosfran@30
   977
static gboolean
rosfran@30
   978
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   979
{
rosfran@100
   980
  return TRUE;
leo_sobral@2
   981
}
rosfran@87
   982
rosfran@87
   983
static gboolean
rosfran@87
   984
gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
rosfran@87
   985
{
rosfran@87
   986
  gboolean res = FALSE;
rosfran@87
   987
  GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
rosfran@87
   988
rosfran@87
   989
  switch (GST_QUERY_TYPE (query)) {
rosfran@87
   990
    case GST_QUERY_POSITION:
rosfran@87
   991
      gst_query_set_position (query, GST_FORMAT_BYTES,
rosfran@87
   992
	  myth->read_offset );
rosfran@87
   993
      res = TRUE;
rosfran@87
   994
      GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
rosfran@87
   995
      break;
rosfran@87
   996
    case GST_QUERY_DURATION:
rosfran@87
   997
#if 0
rosfran@87
   998
      if (myth->duration != 0) {
rosfran@87
   999
	gint64 total;
rosfran@87
  1000
	gint64 fps;
rosfran@87
  1001
rosfran@87
  1002
	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
rosfran@87
  1003
	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
rosfran@81
  1004
#endif
rosfran@87
  1005
	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
rosfran@87
  1006
	GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
rosfran@87
  1007
	res = FALSE;
rosfran@70
  1008
      break;
rosfran@70
  1009
    default:
rosfran@87
  1010
      res = FALSE;
rosfran@87
  1011
      break;
rosfran@70
  1012
  }
rosfran@61
  1013
rosfran@87
  1014
  gst_object_unref (myth);
rosfran@61
  1015
rosfran@70
  1016
  return res;
rosfran@61
  1017
}
rosfran@61
  1018
rosfran@52
  1019
static GstStateChangeReturn
rosfran@52
  1020
gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
rosfran@52
  1021
{
rosfran@87
  1022
  GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
rosfran@52
  1023
  GstMythtvSrc *src = GST_MYTHTV_SRC (element);
rosfran@52
  1024
rosfran@52
  1025
  switch (transition) {
rosfran@52
  1026
    case GST_STATE_CHANGE_NULL_TO_READY:
rosfran@87
  1027
      //src->do_start = TRUE;
rosfran@87
  1028
      //src->unique_setup = FALSE;
rosfran@52
  1029
      break;
rosfran@52
  1030
    case GST_STATE_CHANGE_READY_TO_PAUSED:
rosfran@52
  1031
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
rosfran@87
  1032
      //src->eos = FALSE;
rosfran@52
  1033
      break;
rosfran@52
  1034
    default:
rosfran@52
  1035
      break;
rosfran@52
  1036
  }
rosfran@61
  1037
rosfran@61
  1038
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
rosfran@52
  1039
  if (ret == GST_STATE_CHANGE_FAILURE)
rosfran@52
  1040
    return ret;
rosfran@52
  1041
rosfran@52
  1042
  switch (transition) {
rosfran@52
  1043
    case GST_STATE_CHANGE_READY_TO_NULL:
rosfran@106
  1044
      GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
rosfran@52
  1045
      break;
rosfran@61
  1046
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
rosfran@106
  1047
      GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
rosfran@52
  1048
    case GST_STATE_CHANGE_PAUSED_TO_READY:
rosfran@106
  1049
      GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
rosfran@61
  1050
      if ( src->live_tv && src->update_prog_chain ) {
rosfran@110
  1051
	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
rosfran@110
  1052
	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
rosfran@110
  1053
	      else
rosfran@110
  1054
	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
rosfran@81
  1055
      	
rosfran@98
  1056
      /*
rosfran@98
  1057
      	
rosfran@81
  1058
  			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@81
  1059
      			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@81
  1060
rosfran@70
  1061
				src->read_offset = 0;
rosfran@70
  1062
				src->bytes_read = 0;
rosfran@81
  1063
				src->unique_setup = FALSE;				
rosfran@70
  1064
				gst_mythtv_src_next_program_chain( src );
rosfran@98
  1065
				*/
rosfran@61
  1066
      }
rosfran@98
  1067
      
rosfran@61
  1068
      break;
rosfran@52
  1069
    default:
rosfran@52
  1070
      break;
rosfran@52
  1071
  }
rosfran@52
  1072
rosfran@52
  1073
  return ret;
rosfran@52
  1074
}
rosfran@52
  1075
rosfran@30
  1076
static void
leo_sobral@2
  1077
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
  1078
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
  1079
{
leo_sobral@2
  1080
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1081
leo_sobral@2
  1082
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1083
  switch (prop_id) {
leo_sobral@2
  1084
    case PROP_URI:
leo_sobral@2
  1085
    case PROP_LOCATION:
leo_sobral@2
  1086
      {
leo_sobral@2
  1087
	if (!g_value_get_string (value)) {
leo_sobral@2
  1088
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
  1089
	  goto done;
leo_sobral@2
  1090
	}
leo_sobral@2
  1091
leo_sobral@2
  1092
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
  1093
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1094
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1095
	}
leo_sobral@2
  1096
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
  1097
leo_sobral@2
  1098
	break;
leo_sobral@2
  1099
      }
leo_sobral@2
  1100
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1101
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1102
      {
leo_sobral@2
  1103
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
  1104
	break;
leo_sobral@2
  1105
      }
leo_sobral@2
  1106
#endif
rosfran@40
  1107
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1108
      {
leo_sobral@2
  1109
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
  1110
	break;
leo_sobral@2
  1111
      }
rosfran@40
  1112
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1113
      {
leo_sobral@2
  1114
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
  1115
	break;
leo_sobral@2
  1116
      }
rosfran@40
  1117
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1118
      {
leo_sobral@2
  1119
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
  1120
	break;
leo_sobral@2
  1121
      }
rosfran@52
  1122
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1123
      {
rosfran@52
  1124
	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
rosfran@52
  1125
	break;
rosfran@52
  1126
      }      
rosfran@40
  1127
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1128
      {
leo_sobral@2
  1129
	if (!g_value_get_string (value)) {
leo_sobral@2
  1130
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
  1131
	  goto done;
leo_sobral@2
  1132
	}
leo_sobral@2
  1133
leo_sobral@2
  1134
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
  1135
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1136
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1137
	}
leo_sobral@2
  1138
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
  1139
	break;
leo_sobral@2
  1140
      }
rosfran@70
  1141
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1142
      {
rosfran@70
  1143
	mythtvsrc->channel_num = g_value_get_int (value);
rosfran@70
  1144
	break;
rosfran@70
  1145
      }
leo_sobral@2
  1146
    default:
leo_sobral@2
  1147
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1148
      break;
leo_sobral@2
  1149
  }
leo_sobral@2
  1150
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1151
done:
leo_sobral@2
  1152
  return;
leo_sobral@2
  1153
}
leo_sobral@2
  1154
rosfran@100
  1155
static void
leo_sobral@2
  1156
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
  1157
    GValue * value, GParamSpec * pspec)
leo_sobral@2
  1158
{
leo_sobral@2
  1159
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1160
leo_sobral@2
  1161
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1162
  switch (prop_id) {
leo_sobral@2
  1163
    case PROP_URI:
leo_sobral@2
  1164
    case PROP_LOCATION:
leo_sobral@2
  1165
      {
leo_sobral@2
  1166
	gchar *str = g_strdup( "" );
leo_sobral@2
  1167
leo_sobral@2
  1168
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
  1169
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1170
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1171
	} else {
leo_sobral@2
  1172
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
  1173
	}
leo_sobral@2
  1174
	g_value_set_string ( value, str );
leo_sobral@2
  1175
	break;
leo_sobral@2
  1176
      }
leo_sobral@2
  1177
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1178
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1179
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
  1180
      break;
leo_sobral@2
  1181
#endif
rosfran@40
  1182
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1183
      {
leo_sobral@2
  1184
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
  1185
	break;
leo_sobral@2
  1186
      }
rosfran@40
  1187
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1188
      {
leo_sobral@2
  1189
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
  1190
	break;
leo_sobral@2
  1191
      }
rosfran@40
  1192
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1193
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
  1194
      break;
rosfran@52
  1195
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1196
      g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
rosfran@52
  1197
      break;
rosfran@40
  1198
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1199
      {
leo_sobral@2
  1200
	gchar *str = g_strdup( "" );
leo_sobral@2
  1201
leo_sobral@2
  1202
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
  1203
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1204
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1205
	} else {
leo_sobral@2
  1206
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
  1207
	}
leo_sobral@2
  1208
	g_value_set_string ( value, str );
leo_sobral@2
  1209
	break;
leo_sobral@2
  1210
      }
rosfran@70
  1211
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1212
      {
rosfran@70
  1213
	g_value_set_int ( value, mythtvsrc->channel_num );
rosfran@70
  1214
	break;
rosfran@70
  1215
      }
leo_sobral@2
  1216
    default:
leo_sobral@2
  1217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1218
      break;
leo_sobral@2
  1219
  }
leo_sobral@2
  1220
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1221
}
leo_sobral@2
  1222
leo_sobral@2
  1223
/* entry point to initialize the plug-in
leo_sobral@2
  1224
 * initialize the plug-in itself
leo_sobral@2
  1225
 * register the element factories and pad templates
leo_sobral@2
  1226
 * register the features
leo_sobral@2
  1227
 */
rosfran@40
  1228
static gboolean
leo_sobral@2
  1229
plugin_init (GstPlugin * plugin)
leo_sobral@2
  1230
{
leo_sobral@2
  1231
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
  1232
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
  1233
}
leo_sobral@2
  1234
leo_sobral@2
  1235
/* this is the structure that gst-register looks for
leo_sobral@2
  1236
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
  1237
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
  1238
    GST_VERSION_MINOR,
leo_sobral@2
  1239
    "mythtv",
leo_sobral@2
  1240
    "lib MythTV src",
leo_sobral@2
  1241
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
  1242
leo_sobral@2
  1243
leo_sobral@2
  1244
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
  1245
  static guint 
leo_sobral@2
  1246
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
  1247
{
leo_sobral@2
  1248
  return GST_URI_SRC;
leo_sobral@2
  1249
}
leo_sobral@2
  1250
leo_sobral@2
  1251
  static gchar **
leo_sobral@2
  1252
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
  1253
{
leo_sobral@2
  1254
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
  1255
leo_sobral@2
  1256
  return protocols;
leo_sobral@2
  1257
}
leo_sobral@2
  1258
leo_sobral@2
  1259
  static const gchar *
leo_sobral@2
  1260
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
  1261
{
leo_sobral@2
  1262
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1263
leo_sobral@2
  1264
  return src->uri_name;
leo_sobral@2
  1265
}
leo_sobral@2
  1266
leo_sobral@2
  1267
  static gboolean
leo_sobral@2
  1268
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1269
{
leo_sobral@2
  1270
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1271
leo_sobral@2
  1272
  gchar *protocol;
leo_sobral@2
  1273
leo_sobral@2
  1274
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1275
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1276
    g_free (protocol);
leo_sobral@2
  1277
    return FALSE;
leo_sobral@2
  1278
  }
leo_sobral@2
  1279
  g_free (protocol);
leo_sobral@2
  1280
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1281
leo_sobral@2
  1282
  return TRUE;
leo_sobral@2
  1283
}
leo_sobral@2
  1284
rosfran@87
  1285
 static void
leo_sobral@2
  1286
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1287
{
leo_sobral@2
  1288
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1289
leo_sobral@2
  1290
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1291
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1292
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1293
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1294
}
leo_sobral@2
  1295
leo_sobral@2
  1296
  void
leo_sobral@2
  1297
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1298
{
leo_sobral@2
  1299
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1300
leo_sobral@2
  1301
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1302
leo_sobral@2
  1303
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1304
}