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