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